1 //===-- Path.cpp - Implement OS Path Concept ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the operating system Path API.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Errc.h"
15 #include "llvm/Support/Path.h"
16 #include "llvm/Support/Endian.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/Process.h"
25 #if !defined(_MSC_VER) && !defined(__MINGW32__)
34 using llvm::StringRef;
35 using llvm::sys::path::is_separator;
38 const char *separators = "\\/";
39 const char preferred_separator = '\\';
41 const char separators = '/';
42 const char preferred_separator = '/';
45 StringRef find_first_component(StringRef path) {
46 // Look for this first component in the following order.
47 // * empty (in this case we return an empty string)
48 // * either C: or {//,\\}net.
51 // * {file,directory}name
58 if (path.size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) &&
60 return path.substr(0, 2);
64 if ((path.size() > 2) &&
65 is_separator(path[0]) &&
67 !is_separator(path[2])) {
68 // Find the next directory separator.
69 size_t end = path.find_first_of(separators, 2);
70 return path.substr(0, end);
74 if (is_separator(path[0]))
75 return path.substr(0, 1);
77 if (path.startswith(".."))
78 return path.substr(0, 2);
81 return path.substr(0, 1);
83 // * {file,directory}name
84 size_t end = path.find_first_of(separators);
85 return path.substr(0, end);
88 size_t filename_pos(StringRef str) {
89 if (str.size() == 2 &&
90 is_separator(str[0]) &&
94 if (str.size() > 0 && is_separator(str[str.size() - 1]))
95 return str.size() - 1;
97 size_t pos = str.find_last_of(separators, str.size() - 1);
100 if (pos == StringRef::npos)
101 pos = str.find_last_of(':', str.size() - 2);
104 if (pos == StringRef::npos ||
105 (pos == 1 && is_separator(str[0])))
111 size_t root_dir_start(StringRef str) {
114 if (str.size() > 2 &&
116 is_separator(str[2]))
121 if (str.size() == 2 &&
122 is_separator(str[0]) &&
124 return StringRef::npos;
127 if (str.size() > 3 &&
128 is_separator(str[0]) &&
130 !is_separator(str[2])) {
131 return str.find_first_of(separators, 2);
135 if (str.size() > 0 && is_separator(str[0]))
138 return StringRef::npos;
141 size_t parent_path_end(StringRef path) {
142 size_t end_pos = filename_pos(path);
144 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
146 // Skip separators except for root dir.
147 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
150 (end_pos - 1) != root_dir_pos &&
151 is_separator(path[end_pos - 1]))
154 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
155 return StringRef::npos;
159 } // end unnamed namespace
167 // Implemented in Unix/Path.inc and Windows/Path.inc.
168 static std::error_code TempDir(SmallVectorImpl<char> &result);
170 static std::error_code createUniqueEntity(const Twine &Model, int &ResultFD,
171 SmallVectorImpl<char> &ResultPath,
172 bool MakeAbsolute, unsigned Mode,
174 SmallString<128> ModelStorage;
175 Model.toVector(ModelStorage);
178 // Make model absolute by prepending a temp directory if it's not already.
179 if (!sys::path::is_absolute(Twine(ModelStorage))) {
180 SmallString<128> TDir;
181 if (std::error_code EC = TempDir(TDir))
183 sys::path::append(TDir, Twine(ModelStorage));
184 ModelStorage.swap(TDir);
188 // From here on, DO NOT modify model. It may be needed if the randomly chosen
189 // path already exists.
190 ResultPath = ModelStorage;
192 ResultPath.push_back(0);
193 ResultPath.pop_back();
196 // Replace '%' with random chars.
197 for (unsigned i = 0, e = ModelStorage.size(); i != e; ++i) {
198 if (ModelStorage[i] == '%')
199 ResultPath[i] = "0123456789abcdef"[sys::Process::GetRandomNumber() & 15];
202 // Try to open + create the file.
205 if (std::error_code EC =
206 sys::fs::openFileForWrite(Twine(ResultPath.begin()), ResultFD,
207 sys::fs::F_RW | sys::fs::F_Excl, Mode)) {
208 if (EC == errc::file_exists)
209 goto retry_random_path;
213 return std::error_code();
218 std::error_code EC = sys::fs::exists(ResultPath.begin(), Exists);
222 goto retry_random_path;
223 return std::error_code();
227 if (std::error_code EC =
228 sys::fs::create_directory(ResultPath.begin(), false)) {
229 if (EC == errc::file_exists)
230 goto retry_random_path;
233 return std::error_code();
236 llvm_unreachable("Invalid Type");
243 const_iterator begin(StringRef path) {
246 i.Component = find_first_component(path);
251 const_iterator end(StringRef path) {
254 i.Position = path.size();
258 const_iterator &const_iterator::operator++() {
259 assert(Position < Path.size() && "Tried to increment past end!");
261 // Increment Position to past the current component
262 Position += Component.size();
265 if (Position == Path.size()) {
266 Component = StringRef();
270 // Both POSIX and Windows treat paths that begin with exactly two separators
272 bool was_net = Component.size() > 2 &&
273 is_separator(Component[0]) &&
274 Component[1] == Component[0] &&
275 !is_separator(Component[2]);
277 // Handle separators.
278 if (is_separator(Path[Position])) {
283 || Component.endswith(":")
286 Component = Path.substr(Position, 1);
290 // Skip extra separators.
291 while (Position != Path.size() &&
292 is_separator(Path[Position])) {
296 // Treat trailing '/' as a '.'.
297 if (Position == Path.size()) {
304 // Find next component.
305 size_t end_pos = Path.find_first_of(separators, Position);
306 Component = Path.slice(Position, end_pos);
311 const_iterator &const_iterator::operator--() {
312 // If we're at the end and the previous char was a '/', return '.' unless
313 // we are the root path.
314 size_t root_dir_pos = root_dir_start(Path);
315 if (Position == Path.size() &&
316 Path.size() > root_dir_pos + 1 &&
317 is_separator(Path[Position - 1])) {
323 // Skip separators unless it's the root directory.
324 size_t end_pos = Position;
327 (end_pos - 1) != root_dir_pos &&
328 is_separator(Path[end_pos - 1]))
331 // Find next separator.
332 size_t start_pos = filename_pos(Path.substr(0, end_pos));
333 Component = Path.slice(start_pos, end_pos);
334 Position = start_pos;
338 bool const_iterator::operator==(const const_iterator &RHS) const {
339 return Path.begin() == RHS.Path.begin() &&
340 Position == RHS.Position;
343 bool const_iterator::operator!=(const const_iterator &RHS) const {
344 return !(*this == RHS);
347 ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
348 return Position - RHS.Position;
351 const StringRef root_path(StringRef path) {
352 const_iterator b = begin(path),
356 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
364 if (has_net || has_drive) {
365 if ((++pos != e) && is_separator((*pos)[0])) {
366 // {C:/,//net/}, so get the first two components.
367 return path.substr(0, b->size() + pos->size());
369 // just {C:,//net}, return the first component.
374 // POSIX style root directory.
375 if (is_separator((*b)[0])) {
383 const StringRef root_name(StringRef path) {
384 const_iterator b = begin(path),
387 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
395 if (has_net || has_drive) {
396 // just {C:,//net}, return the first component.
401 // No path or no name.
405 const StringRef root_directory(StringRef path) {
406 const_iterator b = begin(path),
410 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
418 if ((has_net || has_drive) &&
419 // {C:,//net}, skip to the next component.
420 (++pos != e) && is_separator((*pos)[0])) {
424 // POSIX style root directory.
425 if (!has_net && is_separator((*b)[0])) {
430 // No path or no root.
434 const StringRef relative_path(StringRef path) {
435 StringRef root = root_path(path);
436 return path.substr(root.size());
439 void append(SmallVectorImpl<char> &path, const Twine &a,
443 SmallString<32> a_storage;
444 SmallString<32> b_storage;
445 SmallString<32> c_storage;
446 SmallString<32> d_storage;
448 SmallVector<StringRef, 4> components;
449 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
450 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
451 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
452 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
454 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
455 e = components.end();
457 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
458 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
459 bool is_root_name = has_root_name(*i);
462 // Strip separators from beginning of component.
463 size_t loc = i->find_first_not_of(separators);
464 StringRef c = i->substr(loc);
467 path.append(c.begin(), c.end());
471 if (!component_has_sep && !(path.empty() || is_root_name)) {
473 path.push_back(preferred_separator);
476 path.append(i->begin(), i->end());
480 void append(SmallVectorImpl<char> &path,
481 const_iterator begin, const_iterator end) {
482 for (; begin != end; ++begin)
483 path::append(path, *begin);
486 const StringRef parent_path(StringRef path) {
487 size_t end_pos = parent_path_end(path);
488 if (end_pos == StringRef::npos)
491 return path.substr(0, end_pos);
494 void remove_filename(SmallVectorImpl<char> &path) {
495 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
496 if (end_pos != StringRef::npos)
497 path.set_size(end_pos);
500 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
501 StringRef p(path.begin(), path.size());
502 SmallString<32> ext_storage;
503 StringRef ext = extension.toStringRef(ext_storage);
505 // Erase existing extension.
506 size_t pos = p.find_last_of('.');
507 if (pos != StringRef::npos && pos >= filename_pos(p))
510 // Append '.' if needed.
511 if (ext.size() > 0 && ext[0] != '.')
515 path.append(ext.begin(), ext.end());
518 void native(const Twine &path, SmallVectorImpl<char> &result) {
519 assert((!path.isSingleStringRef() ||
520 path.getSingleStringRef().data() != result.data()) &&
521 "path and result are not allowed to overlap!");
524 path.toVector(result);
528 void native(SmallVectorImpl<char> &path) {
530 std::replace(path.begin(), path.end(), '/', '\\');
534 const StringRef filename(StringRef path) {
535 return *(--end(path));
538 const StringRef stem(StringRef path) {
539 StringRef fname = filename(path);
540 size_t pos = fname.find_last_of('.');
541 if (pos == StringRef::npos)
544 if ((fname.size() == 1 && fname == ".") ||
545 (fname.size() == 2 && fname == ".."))
548 return fname.substr(0, pos);
551 const StringRef extension(StringRef path) {
552 StringRef fname = filename(path);
553 size_t pos = fname.find_last_of('.');
554 if (pos == StringRef::npos)
557 if ((fname.size() == 1 && fname == ".") ||
558 (fname.size() == 2 && fname == ".."))
561 return fname.substr(pos);
564 bool is_separator(char value) {
567 case '\\': // fall through
569 case '/': return true;
570 default: return false;
574 static const char preferred_separator_string[] = { preferred_separator, '\0' };
576 const StringRef get_separator() {
577 return preferred_separator_string;
580 void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result) {
583 #if defined(_CS_DARWIN_USER_TEMP_DIR) && defined(_CS_DARWIN_USER_CACHE_DIR)
584 // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR.
585 // macros defined in <unistd.h> on darwin >= 9
586 int ConfName = erasedOnReboot? _CS_DARWIN_USER_TEMP_DIR
587 : _CS_DARWIN_USER_CACHE_DIR;
588 size_t ConfLen = confstr(ConfName, nullptr, 0);
591 result.resize(ConfLen);
592 ConfLen = confstr(ConfName, result.data(), result.size());
593 } while (ConfLen > 0 && ConfLen != result.size());
596 assert(result.back() == 0);
605 // Check whether the temporary directory is specified by an environment
607 const char *EnvironmentVariable;
609 EnvironmentVariable = "TEMP";
611 EnvironmentVariable = "TMPDIR";
613 if (char *RequestedDir = getenv(EnvironmentVariable)) {
614 result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
618 // Fall back to a system default.
619 const char *DefaultResult;
621 (void)erasedOnReboot;
622 DefaultResult = "C:\\TEMP";
625 DefaultResult = "/tmp";
627 DefaultResult = "/var/tmp";
629 result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
632 bool has_root_name(const Twine &path) {
633 SmallString<128> path_storage;
634 StringRef p = path.toStringRef(path_storage);
636 return !root_name(p).empty();
639 bool has_root_directory(const Twine &path) {
640 SmallString<128> path_storage;
641 StringRef p = path.toStringRef(path_storage);
643 return !root_directory(p).empty();
646 bool has_root_path(const Twine &path) {
647 SmallString<128> path_storage;
648 StringRef p = path.toStringRef(path_storage);
650 return !root_path(p).empty();
653 bool has_relative_path(const Twine &path) {
654 SmallString<128> path_storage;
655 StringRef p = path.toStringRef(path_storage);
657 return !relative_path(p).empty();
660 bool has_filename(const Twine &path) {
661 SmallString<128> path_storage;
662 StringRef p = path.toStringRef(path_storage);
664 return !filename(p).empty();
667 bool has_parent_path(const Twine &path) {
668 SmallString<128> path_storage;
669 StringRef p = path.toStringRef(path_storage);
671 return !parent_path(p).empty();
674 bool has_stem(const Twine &path) {
675 SmallString<128> path_storage;
676 StringRef p = path.toStringRef(path_storage);
678 return !stem(p).empty();
681 bool has_extension(const Twine &path) {
682 SmallString<128> path_storage;
683 StringRef p = path.toStringRef(path_storage);
685 return !extension(p).empty();
688 bool is_absolute(const Twine &path) {
689 SmallString<128> path_storage;
690 StringRef p = path.toStringRef(path_storage);
692 bool rootDir = has_root_directory(p),
694 rootName = has_root_name(p);
699 return rootDir && rootName;
702 bool is_relative(const Twine &path) {
703 return !is_absolute(path);
706 } // end namespace path
710 std::error_code getUniqueID(const Twine Path, UniqueID &Result) {
712 std::error_code EC = status(Path, Status);
715 Result = Status.getUniqueID();
716 return std::error_code();
719 std::error_code createUniqueFile(const Twine &Model, int &ResultFd,
720 SmallVectorImpl<char> &ResultPath,
722 return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
725 std::error_code createUniqueFile(const Twine &Model,
726 SmallVectorImpl<char> &ResultPath) {
728 return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
731 static std::error_code
732 createTemporaryFile(const Twine &Model, int &ResultFD,
733 llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) {
734 SmallString<128> Storage;
735 StringRef P = Model.toNullTerminatedStringRef(Storage);
736 assert(P.find_first_of(separators) == StringRef::npos &&
737 "Model must be a simple filename.");
738 // Use P.begin() so that createUniqueEntity doesn't need to recreate Storage.
739 return createUniqueEntity(P.begin(), ResultFD, ResultPath,
740 true, owner_read | owner_write, Type);
743 static std::error_code
744 createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
745 llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) {
746 const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
747 return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
751 std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
753 SmallVectorImpl<char> &ResultPath) {
754 return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File);
757 std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
758 SmallVectorImpl<char> &ResultPath) {
760 return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
764 // This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
765 // for consistency. We should try using mkdtemp.
766 std::error_code createUniqueDirectory(const Twine &Prefix,
767 SmallVectorImpl<char> &ResultPath) {
769 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath,
773 std::error_code make_absolute(SmallVectorImpl<char> &path) {
774 StringRef p(path.data(), path.size());
776 bool rootDirectory = path::has_root_directory(p),
778 rootName = path::has_root_name(p);
784 if (rootName && rootDirectory)
785 return std::error_code();
787 // All of the following conditions will need the current directory.
788 SmallString<128> current_dir;
789 if (std::error_code ec = current_path(current_dir))
792 // Relative path. Prepend the current directory.
793 if (!rootName && !rootDirectory) {
794 // Append path to the current directory.
795 path::append(current_dir, p);
796 // Set path to the result.
797 path.swap(current_dir);
798 return std::error_code();
801 if (!rootName && rootDirectory) {
802 StringRef cdrn = path::root_name(current_dir);
803 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
804 path::append(curDirRootName, p);
805 // Set path to the result.
806 path.swap(curDirRootName);
807 return std::error_code();
810 if (rootName && !rootDirectory) {
811 StringRef pRootName = path::root_name(p);
812 StringRef bRootDirectory = path::root_directory(current_dir);
813 StringRef bRelativePath = path::relative_path(current_dir);
814 StringRef pRelativePath = path::relative_path(p);
816 SmallString<128> res;
817 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
819 return std::error_code();
822 llvm_unreachable("All rootName and rootDirectory combinations should have "
826 std::error_code create_directories(const Twine &Path, bool IgnoreExisting) {
827 SmallString<128> PathStorage;
828 StringRef P = Path.toStringRef(PathStorage);
830 // Be optimistic and try to create the directory
831 std::error_code EC = create_directory(P, IgnoreExisting);
832 // If we succeeded, or had any error other than the parent not existing, just
834 if (EC != errc::no_such_file_or_directory)
837 // We failed because of a no_such_file_or_directory, try to create the
839 StringRef Parent = path::parent_path(P);
843 if ((EC = create_directories(Parent)))
846 return create_directory(P, IgnoreExisting);
849 std::error_code copy_file(const Twine &From, const Twine &To) {
851 if (std::error_code EC = openFileForRead(From, ReadFD))
853 if (std::error_code EC = openFileForWrite(To, WriteFD, F_None)) {
858 const size_t BufSize = 4096;
859 char *Buf = new char[BufSize];
860 int BytesRead = 0, BytesWritten = 0;
862 BytesRead = read(ReadFD, Buf, BufSize);
866 BytesWritten = write(WriteFD, Buf, BytesRead);
867 if (BytesWritten < 0)
869 BytesRead -= BytesWritten;
871 if (BytesWritten < 0)
878 if (BytesRead < 0 || BytesWritten < 0)
879 return std::error_code(errno, std::generic_category());
880 return std::error_code();
883 bool exists(file_status status) {
884 return status_known(status) && status.type() != file_type::file_not_found;
887 bool status_known(file_status s) {
888 return s.type() != file_type::status_error;
891 bool is_directory(file_status status) {
892 return status.type() == file_type::directory_file;
895 std::error_code is_directory(const Twine &path, bool &result) {
897 if (std::error_code ec = status(path, st))
899 result = is_directory(st);
900 return std::error_code();
903 bool is_regular_file(file_status status) {
904 return status.type() == file_type::regular_file;
907 std::error_code is_regular_file(const Twine &path, bool &result) {
909 if (std::error_code ec = status(path, st))
911 result = is_regular_file(st);
912 return std::error_code();
915 bool is_other(file_status status) {
916 return exists(status) &&
917 !is_regular_file(status) &&
918 !is_directory(status);
921 void directory_entry::replace_filename(const Twine &filename, file_status st) {
922 SmallString<128> path(Path.begin(), Path.end());
923 path::remove_filename(path);
924 path::append(path, filename);
929 /// @brief Identify the magic in magic.
930 file_magic identify_magic(StringRef Magic) {
931 if (Magic.size() < 4)
932 return file_magic::unknown;
933 switch ((unsigned char)Magic[0]) {
935 // COFF short import library file
936 if (Magic[1] == (char)0x00 && Magic[2] == (char)0xff &&
937 Magic[3] == (char)0xff)
938 return file_magic::coff_import_library;
939 // Windows resource file
940 const char Expected[] = { 0, 0, 0, 0, '\x20', 0, 0, 0, '\xff' };
941 if (Magic.size() >= sizeof(Expected) &&
942 memcmp(Magic.data(), Expected, sizeof(Expected)) == 0)
943 return file_magic::windows_resource;
944 // 0x0000 = COFF unknown machine type
946 return file_magic::coff_object;
949 case 0xDE: // 0x0B17C0DE = BC wraper
950 if (Magic[1] == (char)0xC0 && Magic[2] == (char)0x17 &&
951 Magic[3] == (char)0x0B)
952 return file_magic::bitcode;
955 if (Magic[1] == 'C' && Magic[2] == (char)0xC0 && Magic[3] == (char)0xDE)
956 return file_magic::bitcode;
959 if (Magic.size() >= 8)
960 if (memcmp(Magic.data(),"!<arch>\n",8) == 0)
961 return file_magic::archive;
965 if (Magic.size() >= 18 && Magic[1] == 'E' && Magic[2] == 'L' &&
967 bool Data2MSB = Magic[5] == 2;
968 unsigned high = Data2MSB ? 16 : 17;
969 unsigned low = Data2MSB ? 17 : 16;
970 if (Magic[high] == 0)
971 switch (Magic[low]) {
973 case 1: return file_magic::elf_relocatable;
974 case 2: return file_magic::elf_executable;
975 case 3: return file_magic::elf_shared_object;
976 case 4: return file_magic::elf_core;
982 if (Magic[1] == char(0xFE) && Magic[2] == char(0xBA) &&
983 Magic[3] == char(0xBE)) {
984 // This is complicated by an overlap with Java class files.
985 // See the Mach-O section in /usr/share/file/magic for details.
986 if (Magic.size() >= 8 && Magic[7] < 43)
987 return file_magic::macho_universal_binary;
991 // The two magic numbers for mach-o are:
992 // 0xfeedface - 32-bit mach-o
993 // 0xfeedfacf - 64-bit mach-o
998 if (Magic[0] == char(0xFE) && Magic[1] == char(0xED) &&
999 Magic[2] == char(0xFA) &&
1000 (Magic[3] == char(0xCE) || Magic[3] == char(0xCF))) {
1002 if (Magic.size() >= 16) type = Magic[14] << 8 | Magic[15];
1003 } else if ((Magic[0] == char(0xCE) || Magic[0] == char(0xCF)) &&
1004 Magic[1] == char(0xFA) && Magic[2] == char(0xED) &&
1005 Magic[3] == char(0xFE)) {
1006 /* Reverse endian */
1007 if (Magic.size() >= 14) type = Magic[13] << 8 | Magic[12];
1011 case 1: return file_magic::macho_object;
1012 case 2: return file_magic::macho_executable;
1013 case 3: return file_magic::macho_fixed_virtual_memory_shared_lib;
1014 case 4: return file_magic::macho_core;
1015 case 5: return file_magic::macho_preload_executable;
1016 case 6: return file_magic::macho_dynamically_linked_shared_lib;
1017 case 7: return file_magic::macho_dynamic_linker;
1018 case 8: return file_magic::macho_bundle;
1019 case 9: return file_magic::macho_dynamic_linker;
1020 case 10: return file_magic::macho_dsym_companion;
1024 case 0xF0: // PowerPC Windows
1025 case 0x83: // Alpha 32-bit
1026 case 0x84: // Alpha 64-bit
1027 case 0x66: // MPS R4000 Windows
1029 case 0x4c: // 80386 Windows
1030 case 0xc4: // ARMNT Windows
1031 if (Magic[1] == 0x01)
1032 return file_magic::coff_object;
1034 case 0x90: // PA-RISC Windows
1035 case 0x68: // mc68K Windows
1036 if (Magic[1] == 0x02)
1037 return file_magic::coff_object;
1040 case 0x4d: // Possible MS-DOS stub on Windows PE file
1041 if (Magic[1] == 0x5a) {
1043 *reinterpret_cast<const support::ulittle32_t*>(Magic.data() + 0x3c);
1044 // PE/COFF file, either EXE or DLL.
1045 if (off < Magic.size() && memcmp(Magic.data() + off, "PE\0\0",4) == 0)
1046 return file_magic::pecoff_executable;
1050 case 0x64: // x86-64 Windows.
1051 if (Magic[1] == char(0x86))
1052 return file_magic::coff_object;
1058 return file_magic::unknown;
1061 std::error_code identify_magic(const Twine &Path, file_magic &Result) {
1063 if (std::error_code EC = openFileForRead(Path, FD))
1067 int Length = read(FD, Buffer, sizeof(Buffer));
1068 if (close(FD) != 0 || Length < 0)
1069 return std::error_code(errno, std::generic_category());
1071 Result = identify_magic(StringRef(Buffer, Length));
1072 return std::error_code();
1075 std::error_code directory_entry::status(file_status &result) const {
1076 return fs::status(Path, result);
1079 } // end namespace fs
1080 } // end namespace sys
1081 } // end namespace llvm
1083 // Include the truly platform-specific parts.
1084 #if defined(LLVM_ON_UNIX)
1085 #include "Unix/Path.inc"
1087 #if defined(LLVM_ON_WIN32)
1088 #include "Windows/Path.inc"