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/COFF.h"
15 #include "llvm/Support/Endian.h"
16 #include "llvm/Support/Errc.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/Path.h"
20 #include "llvm/Support/Process.h"
26 #if !defined(_MSC_VER) && !defined(__MINGW32__)
35 using llvm::StringRef;
36 using llvm::sys::path::is_separator;
39 const char *separators = "\\/";
40 const char preferred_separator = '\\';
42 const char separators = '/';
43 const char preferred_separator = '/';
46 StringRef find_first_component(StringRef path) {
47 // Look for this first component in the following order.
48 // * empty (in this case we return an empty string)
49 // * either C: or {//,\\}net.
52 // * {file,directory}name
59 if (path.size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) &&
61 return path.substr(0, 2);
65 if ((path.size() > 2) &&
66 is_separator(path[0]) &&
68 !is_separator(path[2])) {
69 // Find the next directory separator.
70 size_t end = path.find_first_of(separators, 2);
71 return path.substr(0, end);
75 if (is_separator(path[0]))
76 return path.substr(0, 1);
78 if (path.startswith(".."))
79 return path.substr(0, 2);
82 return path.substr(0, 1);
84 // * {file,directory}name
85 size_t end = path.find_first_of(separators);
86 return path.substr(0, end);
89 size_t filename_pos(StringRef str) {
90 if (str.size() == 2 &&
91 is_separator(str[0]) &&
95 if (str.size() > 0 && is_separator(str[str.size() - 1]))
96 return str.size() - 1;
98 size_t pos = str.find_last_of(separators, str.size() - 1);
101 if (pos == StringRef::npos)
102 pos = str.find_last_of(':', str.size() - 2);
105 if (pos == StringRef::npos ||
106 (pos == 1 && is_separator(str[0])))
112 size_t root_dir_start(StringRef str) {
115 if (str.size() > 2 &&
117 is_separator(str[2]))
122 if (str.size() == 2 &&
123 is_separator(str[0]) &&
125 return StringRef::npos;
128 if (str.size() > 3 &&
129 is_separator(str[0]) &&
131 !is_separator(str[2])) {
132 return str.find_first_of(separators, 2);
136 if (str.size() > 0 && is_separator(str[0]))
139 return StringRef::npos;
142 size_t parent_path_end(StringRef path) {
143 size_t end_pos = filename_pos(path);
145 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
147 // Skip separators except for root dir.
148 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
151 (end_pos - 1) != root_dir_pos &&
152 is_separator(path[end_pos - 1]))
155 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
156 return StringRef::npos;
160 } // end unnamed namespace
168 static std::error_code createUniqueEntity(const Twine &Model, int &ResultFD,
169 SmallVectorImpl<char> &ResultPath,
170 bool MakeAbsolute, unsigned Mode,
172 SmallString<128> ModelStorage;
173 Model.toVector(ModelStorage);
176 // Make model absolute by prepending a temp directory if it's not already.
177 if (!sys::path::is_absolute(Twine(ModelStorage))) {
178 SmallString<128> TDir;
179 sys::path::system_temp_directory(true, TDir);
180 sys::path::append(TDir, Twine(ModelStorage));
181 ModelStorage.swap(TDir);
185 // From here on, DO NOT modify model. It may be needed if the randomly chosen
186 // path already exists.
187 ResultPath = ModelStorage;
189 ResultPath.push_back(0);
190 ResultPath.pop_back();
193 // Replace '%' with random chars.
194 for (unsigned i = 0, e = ModelStorage.size(); i != e; ++i) {
195 if (ModelStorage[i] == '%')
196 ResultPath[i] = "0123456789abcdef"[sys::Process::GetRandomNumber() & 15];
199 // Try to open + create the file.
202 if (std::error_code EC =
203 sys::fs::openFileForWrite(Twine(ResultPath.begin()), ResultFD,
204 sys::fs::F_RW | sys::fs::F_Excl, Mode)) {
205 if (EC == errc::file_exists)
206 goto retry_random_path;
210 return std::error_code();
215 sys::fs::access(ResultPath.begin(), sys::fs::AccessMode::Exist);
216 if (EC == errc::no_such_file_or_directory)
217 return std::error_code();
220 goto retry_random_path;
224 if (std::error_code EC =
225 sys::fs::create_directory(ResultPath.begin(), false)) {
226 if (EC == errc::file_exists)
227 goto retry_random_path;
230 return std::error_code();
233 llvm_unreachable("Invalid Type");
240 const_iterator begin(StringRef path) {
243 i.Component = find_first_component(path);
248 const_iterator end(StringRef path) {
251 i.Position = path.size();
255 const_iterator &const_iterator::operator++() {
256 assert(Position < Path.size() && "Tried to increment past end!");
258 // Increment Position to past the current component
259 Position += Component.size();
262 if (Position == Path.size()) {
263 Component = StringRef();
267 // Both POSIX and Windows treat paths that begin with exactly two separators
269 bool was_net = Component.size() > 2 &&
270 is_separator(Component[0]) &&
271 Component[1] == Component[0] &&
272 !is_separator(Component[2]);
274 // Handle separators.
275 if (is_separator(Path[Position])) {
280 || Component.endswith(":")
283 Component = Path.substr(Position, 1);
287 // Skip extra separators.
288 while (Position != Path.size() &&
289 is_separator(Path[Position])) {
293 // Treat trailing '/' as a '.'.
294 if (Position == Path.size()) {
301 // Find next component.
302 size_t end_pos = Path.find_first_of(separators, Position);
303 Component = Path.slice(Position, end_pos);
308 bool const_iterator::operator==(const const_iterator &RHS) const {
309 return Path.begin() == RHS.Path.begin() && Position == RHS.Position;
312 ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
313 return Position - RHS.Position;
316 reverse_iterator rbegin(StringRef Path) {
319 I.Position = Path.size();
323 reverse_iterator rend(StringRef Path) {
326 I.Component = Path.substr(0, 0);
331 reverse_iterator &reverse_iterator::operator++() {
332 // If we're at the end and the previous char was a '/', return '.' unless
333 // we are the root path.
334 size_t root_dir_pos = root_dir_start(Path);
335 if (Position == Path.size() &&
336 Path.size() > root_dir_pos + 1 &&
337 is_separator(Path[Position - 1])) {
343 // Skip separators unless it's the root directory.
344 size_t end_pos = Position;
347 (end_pos - 1) != root_dir_pos &&
348 is_separator(Path[end_pos - 1]))
351 // Find next separator.
352 size_t start_pos = filename_pos(Path.substr(0, end_pos));
353 Component = Path.slice(start_pos, end_pos);
354 Position = start_pos;
358 bool reverse_iterator::operator==(const reverse_iterator &RHS) const {
359 return Path.begin() == RHS.Path.begin() && Component == RHS.Component &&
360 Position == RHS.Position;
363 StringRef root_path(StringRef path) {
364 const_iterator b = begin(path),
368 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
376 if (has_net || has_drive) {
377 if ((++pos != e) && is_separator((*pos)[0])) {
378 // {C:/,//net/}, so get the first two components.
379 return path.substr(0, b->size() + pos->size());
381 // just {C:,//net}, return the first component.
386 // POSIX style root directory.
387 if (is_separator((*b)[0])) {
395 StringRef root_name(StringRef path) {
396 const_iterator b = begin(path),
399 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
407 if (has_net || has_drive) {
408 // just {C:,//net}, return the first component.
413 // No path or no name.
417 StringRef root_directory(StringRef path) {
418 const_iterator b = begin(path),
422 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
430 if ((has_net || has_drive) &&
431 // {C:,//net}, skip to the next component.
432 (++pos != e) && is_separator((*pos)[0])) {
436 // POSIX style root directory.
437 if (!has_net && is_separator((*b)[0])) {
442 // No path or no root.
446 StringRef relative_path(StringRef path) {
447 StringRef root = root_path(path);
448 return path.substr(root.size());
451 void append(SmallVectorImpl<char> &path, const Twine &a,
455 SmallString<32> a_storage;
456 SmallString<32> b_storage;
457 SmallString<32> c_storage;
458 SmallString<32> d_storage;
460 SmallVector<StringRef, 4> components;
461 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
462 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
463 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
464 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
466 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
467 e = components.end();
469 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
470 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
471 bool is_root_name = has_root_name(*i);
474 // Strip separators from beginning of component.
475 size_t loc = i->find_first_not_of(separators);
476 StringRef c = i->substr(loc);
479 path.append(c.begin(), c.end());
483 if (!component_has_sep && !(path.empty() || is_root_name)) {
485 path.push_back(preferred_separator);
488 path.append(i->begin(), i->end());
492 void append(SmallVectorImpl<char> &path,
493 const_iterator begin, const_iterator end) {
494 for (; begin != end; ++begin)
495 path::append(path, *begin);
498 StringRef parent_path(StringRef path) {
499 size_t end_pos = parent_path_end(path);
500 if (end_pos == StringRef::npos)
503 return path.substr(0, end_pos);
506 void remove_filename(SmallVectorImpl<char> &path) {
507 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
508 if (end_pos != StringRef::npos)
509 path.set_size(end_pos);
512 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
513 StringRef p(path.begin(), path.size());
514 SmallString<32> ext_storage;
515 StringRef ext = extension.toStringRef(ext_storage);
517 // Erase existing extension.
518 size_t pos = p.find_last_of('.');
519 if (pos != StringRef::npos && pos >= filename_pos(p))
522 // Append '.' if needed.
523 if (ext.size() > 0 && ext[0] != '.')
527 path.append(ext.begin(), ext.end());
530 void native(const Twine &path, SmallVectorImpl<char> &result) {
531 assert((!path.isSingleStringRef() ||
532 path.getSingleStringRef().data() != result.data()) &&
533 "path and result are not allowed to overlap!");
536 path.toVector(result);
540 void native(SmallVectorImpl<char> &Path) {
542 std::replace(Path.begin(), Path.end(), '/', '\\');
544 for (auto PI = Path.begin(), PE = Path.end(); PI < PE; ++PI) {
547 if (PN < PE && *PN == '\\')
548 ++PI; // increment once, the for loop will move over the escaped slash
556 StringRef filename(StringRef path) {
557 return *rbegin(path);
560 StringRef stem(StringRef path) {
561 StringRef fname = filename(path);
562 size_t pos = fname.find_last_of('.');
563 if (pos == StringRef::npos)
566 if ((fname.size() == 1 && fname == ".") ||
567 (fname.size() == 2 && fname == ".."))
570 return fname.substr(0, pos);
573 StringRef extension(StringRef path) {
574 StringRef fname = filename(path);
575 size_t pos = fname.find_last_of('.');
576 if (pos == StringRef::npos)
579 if ((fname.size() == 1 && fname == ".") ||
580 (fname.size() == 2 && fname == ".."))
583 return fname.substr(pos);
586 bool is_separator(char value) {
589 case '\\': // fall through
591 case '/': return true;
592 default: return false;
596 static const char preferred_separator_string[] = { preferred_separator, '\0' };
598 StringRef get_separator() {
599 return preferred_separator_string;
602 bool has_root_name(const Twine &path) {
603 SmallString<128> path_storage;
604 StringRef p = path.toStringRef(path_storage);
606 return !root_name(p).empty();
609 bool has_root_directory(const Twine &path) {
610 SmallString<128> path_storage;
611 StringRef p = path.toStringRef(path_storage);
613 return !root_directory(p).empty();
616 bool has_root_path(const Twine &path) {
617 SmallString<128> path_storage;
618 StringRef p = path.toStringRef(path_storage);
620 return !root_path(p).empty();
623 bool has_relative_path(const Twine &path) {
624 SmallString<128> path_storage;
625 StringRef p = path.toStringRef(path_storage);
627 return !relative_path(p).empty();
630 bool has_filename(const Twine &path) {
631 SmallString<128> path_storage;
632 StringRef p = path.toStringRef(path_storage);
634 return !filename(p).empty();
637 bool has_parent_path(const Twine &path) {
638 SmallString<128> path_storage;
639 StringRef p = path.toStringRef(path_storage);
641 return !parent_path(p).empty();
644 bool has_stem(const Twine &path) {
645 SmallString<128> path_storage;
646 StringRef p = path.toStringRef(path_storage);
648 return !stem(p).empty();
651 bool has_extension(const Twine &path) {
652 SmallString<128> path_storage;
653 StringRef p = path.toStringRef(path_storage);
655 return !extension(p).empty();
658 bool is_absolute(const Twine &path) {
659 SmallString<128> path_storage;
660 StringRef p = path.toStringRef(path_storage);
662 bool rootDir = has_root_directory(p),
664 rootName = has_root_name(p);
669 return rootDir && rootName;
672 bool is_relative(const Twine &path) {
673 return !is_absolute(path);
676 } // end namespace path
680 std::error_code getUniqueID(const Twine Path, UniqueID &Result) {
682 std::error_code EC = status(Path, Status);
685 Result = Status.getUniqueID();
686 return std::error_code();
689 std::error_code createUniqueFile(const Twine &Model, int &ResultFd,
690 SmallVectorImpl<char> &ResultPath,
692 return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
695 std::error_code createUniqueFile(const Twine &Model,
696 SmallVectorImpl<char> &ResultPath) {
698 return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
701 static std::error_code
702 createTemporaryFile(const Twine &Model, int &ResultFD,
703 llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) {
704 SmallString<128> Storage;
705 StringRef P = Model.toNullTerminatedStringRef(Storage);
706 assert(P.find_first_of(separators) == StringRef::npos &&
707 "Model must be a simple filename.");
708 // Use P.begin() so that createUniqueEntity doesn't need to recreate Storage.
709 return createUniqueEntity(P.begin(), ResultFD, ResultPath,
710 true, owner_read | owner_write, Type);
713 static std::error_code
714 createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
715 llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) {
716 const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
717 return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
721 std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
723 SmallVectorImpl<char> &ResultPath) {
724 return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File);
727 std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
728 SmallVectorImpl<char> &ResultPath) {
730 return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
734 // This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
735 // for consistency. We should try using mkdtemp.
736 std::error_code createUniqueDirectory(const Twine &Prefix,
737 SmallVectorImpl<char> &ResultPath) {
739 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath,
743 std::error_code make_absolute(SmallVectorImpl<char> &path) {
744 StringRef p(path.data(), path.size());
746 bool rootDirectory = path::has_root_directory(p),
748 rootName = path::has_root_name(p);
754 if (rootName && rootDirectory)
755 return std::error_code();
757 // All of the following conditions will need the current directory.
758 SmallString<128> current_dir;
759 if (std::error_code ec = current_path(current_dir))
762 // Relative path. Prepend the current directory.
763 if (!rootName && !rootDirectory) {
764 // Append path to the current directory.
765 path::append(current_dir, p);
766 // Set path to the result.
767 path.swap(current_dir);
768 return std::error_code();
771 if (!rootName && rootDirectory) {
772 StringRef cdrn = path::root_name(current_dir);
773 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
774 path::append(curDirRootName, p);
775 // Set path to the result.
776 path.swap(curDirRootName);
777 return std::error_code();
780 if (rootName && !rootDirectory) {
781 StringRef pRootName = path::root_name(p);
782 StringRef bRootDirectory = path::root_directory(current_dir);
783 StringRef bRelativePath = path::relative_path(current_dir);
784 StringRef pRelativePath = path::relative_path(p);
786 SmallString<128> res;
787 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
789 return std::error_code();
792 llvm_unreachable("All rootName and rootDirectory combinations should have "
796 std::error_code create_directories(const Twine &Path, bool IgnoreExisting) {
797 SmallString<128> PathStorage;
798 StringRef P = Path.toStringRef(PathStorage);
800 // Be optimistic and try to create the directory
801 std::error_code EC = create_directory(P, IgnoreExisting);
802 // If we succeeded, or had any error other than the parent not existing, just
804 if (EC != errc::no_such_file_or_directory)
807 // We failed because of a no_such_file_or_directory, try to create the
809 StringRef Parent = path::parent_path(P);
813 if ((EC = create_directories(Parent)))
816 return create_directory(P, IgnoreExisting);
819 std::error_code copy_file(const Twine &From, const Twine &To) {
821 if (std::error_code EC = openFileForRead(From, ReadFD))
823 if (std::error_code EC = openFileForWrite(To, WriteFD, F_None)) {
828 const size_t BufSize = 4096;
829 char *Buf = new char[BufSize];
830 int BytesRead = 0, BytesWritten = 0;
832 BytesRead = read(ReadFD, Buf, BufSize);
836 BytesWritten = write(WriteFD, Buf, BytesRead);
837 if (BytesWritten < 0)
839 BytesRead -= BytesWritten;
841 if (BytesWritten < 0)
848 if (BytesRead < 0 || BytesWritten < 0)
849 return std::error_code(errno, std::generic_category());
850 return std::error_code();
853 bool exists(file_status status) {
854 return status_known(status) && status.type() != file_type::file_not_found;
857 bool status_known(file_status s) {
858 return s.type() != file_type::status_error;
861 bool is_directory(file_status status) {
862 return status.type() == file_type::directory_file;
865 std::error_code is_directory(const Twine &path, bool &result) {
867 if (std::error_code ec = status(path, st))
869 result = is_directory(st);
870 return std::error_code();
873 bool is_regular_file(file_status status) {
874 return status.type() == file_type::regular_file;
877 std::error_code is_regular_file(const Twine &path, bool &result) {
879 if (std::error_code ec = status(path, st))
881 result = is_regular_file(st);
882 return std::error_code();
885 bool is_other(file_status status) {
886 return exists(status) &&
887 !is_regular_file(status) &&
888 !is_directory(status);
891 std::error_code is_other(const Twine &Path, bool &Result) {
892 file_status FileStatus;
893 if (std::error_code EC = status(Path, FileStatus))
895 Result = is_other(FileStatus);
896 return std::error_code();
899 void directory_entry::replace_filename(const Twine &filename, file_status st) {
900 SmallString<128> path(Path.begin(), Path.end());
901 path::remove_filename(path);
902 path::append(path, filename);
907 /// @brief Identify the magic in magic.
908 file_magic identify_magic(StringRef Magic) {
909 if (Magic.size() < 4)
910 return file_magic::unknown;
911 switch ((unsigned char)Magic[0]) {
913 // COFF bigobj or short import library file
914 if (Magic[1] == (char)0x00 && Magic[2] == (char)0xff &&
915 Magic[3] == (char)0xff) {
916 size_t MinSize = offsetof(COFF::BigObjHeader, UUID) + sizeof(COFF::BigObjMagic);
917 if (Magic.size() < MinSize)
918 return file_magic::coff_import_library;
920 int BigObjVersion = *reinterpret_cast<const support::ulittle16_t*>(
921 Magic.data() + offsetof(COFF::BigObjHeader, Version));
922 if (BigObjVersion < COFF::BigObjHeader::MinBigObjectVersion)
923 return file_magic::coff_import_library;
925 const char *Start = Magic.data() + offsetof(COFF::BigObjHeader, UUID);
926 if (memcmp(Start, COFF::BigObjMagic, sizeof(COFF::BigObjMagic)) != 0)
927 return file_magic::coff_import_library;
928 return file_magic::coff_object;
930 // Windows resource file
931 const char Expected[] = { 0, 0, 0, 0, '\x20', 0, 0, 0, '\xff' };
932 if (Magic.size() >= sizeof(Expected) &&
933 memcmp(Magic.data(), Expected, sizeof(Expected)) == 0)
934 return file_magic::windows_resource;
935 // 0x0000 = COFF unknown machine type
937 return file_magic::coff_object;
940 case 0xDE: // 0x0B17C0DE = BC wraper
941 if (Magic[1] == (char)0xC0 && Magic[2] == (char)0x17 &&
942 Magic[3] == (char)0x0B)
943 return file_magic::bitcode;
946 if (Magic[1] == 'C' && Magic[2] == (char)0xC0 && Magic[3] == (char)0xDE)
947 return file_magic::bitcode;
950 if (Magic.size() >= 8)
951 if (memcmp(Magic.data(),"!<arch>\n",8) == 0)
952 return file_magic::archive;
956 if (Magic.size() >= 18 && Magic[1] == 'E' && Magic[2] == 'L' &&
958 bool Data2MSB = Magic[5] == 2;
959 unsigned high = Data2MSB ? 16 : 17;
960 unsigned low = Data2MSB ? 17 : 16;
961 if (Magic[high] == 0)
962 switch (Magic[low]) {
963 default: return file_magic::elf;
964 case 1: return file_magic::elf_relocatable;
965 case 2: return file_magic::elf_executable;
966 case 3: return file_magic::elf_shared_object;
967 case 4: return file_magic::elf_core;
970 // It's still some type of ELF file.
971 return file_magic::elf;
976 if (Magic[1] == char(0xFE) && Magic[2] == char(0xBA) &&
977 Magic[3] == char(0xBE)) {
978 // This is complicated by an overlap with Java class files.
979 // See the Mach-O section in /usr/share/file/magic for details.
980 if (Magic.size() >= 8 && Magic[7] < 43)
981 return file_magic::macho_universal_binary;
985 // The two magic numbers for mach-o are:
986 // 0xfeedface - 32-bit mach-o
987 // 0xfeedfacf - 64-bit mach-o
992 if (Magic[0] == char(0xFE) && Magic[1] == char(0xED) &&
993 Magic[2] == char(0xFA) &&
994 (Magic[3] == char(0xCE) || Magic[3] == char(0xCF))) {
996 if (Magic.size() >= 16) type = Magic[14] << 8 | Magic[15];
997 } else if ((Magic[0] == char(0xCE) || Magic[0] == char(0xCF)) &&
998 Magic[1] == char(0xFA) && Magic[2] == char(0xED) &&
999 Magic[3] == char(0xFE)) {
1000 /* Reverse endian */
1001 if (Magic.size() >= 14) type = Magic[13] << 8 | Magic[12];
1005 case 1: return file_magic::macho_object;
1006 case 2: return file_magic::macho_executable;
1007 case 3: return file_magic::macho_fixed_virtual_memory_shared_lib;
1008 case 4: return file_magic::macho_core;
1009 case 5: return file_magic::macho_preload_executable;
1010 case 6: return file_magic::macho_dynamically_linked_shared_lib;
1011 case 7: return file_magic::macho_dynamic_linker;
1012 case 8: return file_magic::macho_bundle;
1013 case 9: return file_magic::macho_dynamically_linked_shared_lib_stub;
1014 case 10: return file_magic::macho_dsym_companion;
1018 case 0xF0: // PowerPC Windows
1019 case 0x83: // Alpha 32-bit
1020 case 0x84: // Alpha 64-bit
1021 case 0x66: // MPS R4000 Windows
1023 case 0x4c: // 80386 Windows
1024 case 0xc4: // ARMNT Windows
1025 if (Magic[1] == 0x01)
1026 return file_magic::coff_object;
1028 case 0x90: // PA-RISC Windows
1029 case 0x68: // mc68K Windows
1030 if (Magic[1] == 0x02)
1031 return file_magic::coff_object;
1034 case 'M': // Possible MS-DOS stub on Windows PE file
1035 if (Magic[1] == 'Z') {
1037 *reinterpret_cast<const support::ulittle32_t*>(Magic.data() + 0x3c);
1038 // PE/COFF file, either EXE or DLL.
1039 if (off < Magic.size() &&
1040 memcmp(Magic.data()+off, COFF::PEMagic, sizeof(COFF::PEMagic)) == 0)
1041 return file_magic::pecoff_executable;
1045 case 0x64: // x86-64 Windows.
1046 if (Magic[1] == char(0x86))
1047 return file_magic::coff_object;
1053 return file_magic::unknown;
1056 std::error_code identify_magic(const Twine &Path, file_magic &Result) {
1058 if (std::error_code EC = openFileForRead(Path, FD))
1062 int Length = read(FD, Buffer, sizeof(Buffer));
1063 if (close(FD) != 0 || Length < 0)
1064 return std::error_code(errno, std::generic_category());
1066 Result = identify_magic(StringRef(Buffer, Length));
1067 return std::error_code();
1070 std::error_code directory_entry::status(file_status &result) const {
1071 return fs::status(Path, result);
1074 } // end namespace fs
1075 } // end namespace sys
1076 } // end namespace llvm
1078 // Include the truly platform-specific parts.
1079 #if defined(LLVM_ON_UNIX)
1080 #include "Unix/Path.inc"
1082 #if defined(LLVM_ON_WIN32)
1083 #include "Windows/Path.inc"