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/Path.h"
15 #include "llvm/Support/Endian.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/Process.h"
24 #if !defined(_MSC_VER) && !defined(__MINGW32__)
33 using llvm::StringRef;
34 using llvm::sys::path::is_separator;
37 const char *separators = "\\/";
38 const char preferred_separator = '\\';
40 const char separators = '/';
41 const char preferred_separator = '/';
44 StringRef find_first_component(StringRef path) {
45 // Look for this first component in the following order.
46 // * empty (in this case we return an empty string)
47 // * either C: or {//,\\}net.
50 // * {file,directory}name
57 if (path.size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) &&
59 return path.substr(0, 2);
63 if ((path.size() > 2) &&
64 is_separator(path[0]) &&
66 !is_separator(path[2])) {
67 // Find the next directory separator.
68 size_t end = path.find_first_of(separators, 2);
69 return path.substr(0, end);
73 if (is_separator(path[0]))
74 return path.substr(0, 1);
76 if (path.startswith(".."))
77 return path.substr(0, 2);
80 return path.substr(0, 1);
82 // * {file,directory}name
83 size_t end = path.find_first_of(separators);
84 return path.substr(0, end);
87 size_t filename_pos(StringRef str) {
88 if (str.size() == 2 &&
89 is_separator(str[0]) &&
93 if (str.size() > 0 && is_separator(str[str.size() - 1]))
94 return str.size() - 1;
96 size_t pos = str.find_last_of(separators, str.size() - 1);
99 if (pos == StringRef::npos)
100 pos = str.find_last_of(':', str.size() - 2);
103 if (pos == StringRef::npos ||
104 (pos == 1 && is_separator(str[0])))
110 size_t root_dir_start(StringRef str) {
113 if (str.size() > 2 &&
115 is_separator(str[2]))
120 if (str.size() == 2 &&
121 is_separator(str[0]) &&
123 return StringRef::npos;
126 if (str.size() > 3 &&
127 is_separator(str[0]) &&
129 !is_separator(str[2])) {
130 return str.find_first_of(separators, 2);
134 if (str.size() > 0 && is_separator(str[0]))
137 return StringRef::npos;
140 size_t parent_path_end(StringRef path) {
141 size_t end_pos = filename_pos(path);
143 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
145 // Skip separators except for root dir.
146 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
149 (end_pos - 1) != root_dir_pos &&
150 is_separator(path[end_pos - 1]))
153 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
154 return StringRef::npos;
158 } // end unnamed namespace
166 // Implemented in Unix/Path.inc and Windows/Path.inc.
167 static error_code TempDir(SmallVectorImpl<char> &result);
169 static error_code createUniqueEntity(const Twine &Model, int &ResultFD,
170 SmallVectorImpl<char> &ResultPath,
171 bool MakeAbsolute, unsigned Mode,
173 SmallString<128> ModelStorage;
174 Model.toVector(ModelStorage);
177 // Make model absolute by prepending a temp directory if it's not already.
178 if (!sys::path::is_absolute(Twine(ModelStorage))) {
179 SmallString<128> TDir;
180 if (error_code EC = TempDir(TDir))
182 sys::path::append(TDir, Twine(ModelStorage));
183 ModelStorage.swap(TDir);
187 // From here on, DO NOT modify model. It may be needed if the randomly chosen
188 // path already exists.
189 ResultPath = ModelStorage;
191 ResultPath.push_back(0);
192 ResultPath.pop_back();
195 // Replace '%' with random chars.
196 for (unsigned i = 0, e = ModelStorage.size(); i != e; ++i) {
197 if (ModelStorage[i] == '%')
198 ResultPath[i] = "0123456789abcdef"[sys::Process::GetRandomNumber() & 15];
201 // Try to open + create the file.
205 sys::fs::openFileForWrite(Twine(ResultPath.begin()), ResultFD,
206 sys::fs::F_RW | sys::fs::F_Excl, Mode)) {
207 if (EC == errc::file_exists)
208 goto retry_random_path;
212 return error_code::success();
217 error_code EC = sys::fs::exists(ResultPath.begin(), Exists);
221 goto retry_random_path;
222 return error_code::success();
226 if (error_code EC = sys::fs::create_directory(ResultPath.begin(), false)) {
227 if (EC == errc::file_exists)
228 goto retry_random_path;
231 return error_code::success();
234 llvm_unreachable("Invalid Type");
241 const_iterator begin(StringRef path) {
244 i.Component = find_first_component(path);
249 const_iterator end(StringRef path) {
252 i.Position = path.size();
256 const_iterator &const_iterator::operator++() {
257 assert(Position < Path.size() && "Tried to increment past end!");
259 // Increment Position to past the current component
260 Position += Component.size();
263 if (Position == Path.size()) {
264 Component = StringRef();
268 // Both POSIX and Windows treat paths that begin with exactly two separators
270 bool was_net = Component.size() > 2 &&
271 is_separator(Component[0]) &&
272 Component[1] == Component[0] &&
273 !is_separator(Component[2]);
275 // Handle separators.
276 if (is_separator(Path[Position])) {
281 || Component.endswith(":")
284 Component = Path.substr(Position, 1);
288 // Skip extra separators.
289 while (Position != Path.size() &&
290 is_separator(Path[Position])) {
294 // Treat trailing '/' as a '.'.
295 if (Position == Path.size()) {
302 // Find next component.
303 size_t end_pos = Path.find_first_of(separators, Position);
304 Component = Path.slice(Position, end_pos);
309 const_iterator &const_iterator::operator--() {
310 // If we're at the end and the previous char was a '/', return '.'.
311 if (Position == Path.size() &&
313 is_separator(Path[Position - 1])
315 && Path[Position - 2] != ':'
323 // Skip separators unless it's the root directory.
324 size_t root_dir_pos = root_dir_start(Path);
325 size_t end_pos = Position;
328 (end_pos - 1) != root_dir_pos &&
329 is_separator(Path[end_pos - 1]))
332 // Find next separator.
333 size_t start_pos = filename_pos(Path.substr(0, end_pos));
334 Component = Path.slice(start_pos, end_pos);
335 Position = start_pos;
339 bool const_iterator::operator==(const const_iterator &RHS) const {
340 return Path.begin() == RHS.Path.begin() &&
341 Position == RHS.Position;
344 bool const_iterator::operator!=(const const_iterator &RHS) const {
345 return !(*this == RHS);
348 ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
349 return Position - RHS.Position;
352 const StringRef root_path(StringRef path) {
353 const_iterator b = begin(path),
357 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
365 if (has_net || has_drive) {
366 if ((++pos != e) && is_separator((*pos)[0])) {
367 // {C:/,//net/}, so get the first two components.
368 return path.substr(0, b->size() + pos->size());
370 // just {C:,//net}, return the first component.
375 // POSIX style root directory.
376 if (is_separator((*b)[0])) {
384 const StringRef root_name(StringRef path) {
385 const_iterator b = begin(path),
388 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
396 if (has_net || has_drive) {
397 // just {C:,//net}, return the first component.
402 // No path or no name.
406 const StringRef root_directory(StringRef path) {
407 const_iterator b = begin(path),
411 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
419 if ((has_net || has_drive) &&
420 // {C:,//net}, skip to the next component.
421 (++pos != e) && is_separator((*pos)[0])) {
425 // POSIX style root directory.
426 if (!has_net && is_separator((*b)[0])) {
431 // No path or no root.
435 const StringRef relative_path(StringRef path) {
436 StringRef root = root_path(path);
437 return path.substr(root.size());
440 void append(SmallVectorImpl<char> &path, const Twine &a,
444 SmallString<32> a_storage;
445 SmallString<32> b_storage;
446 SmallString<32> c_storage;
447 SmallString<32> d_storage;
449 SmallVector<StringRef, 4> components;
450 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
451 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
452 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
453 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
455 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
456 e = components.end();
458 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
459 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
460 bool is_root_name = has_root_name(*i);
463 // Strip separators from beginning of component.
464 size_t loc = i->find_first_not_of(separators);
465 StringRef c = i->substr(loc);
468 path.append(c.begin(), c.end());
472 if (!component_has_sep && !(path.empty() || is_root_name)) {
474 path.push_back(preferred_separator);
477 path.append(i->begin(), i->end());
481 void append(SmallVectorImpl<char> &path,
482 const_iterator begin, const_iterator end) {
483 for (; begin != end; ++begin)
484 path::append(path, *begin);
487 const StringRef parent_path(StringRef path) {
488 size_t end_pos = parent_path_end(path);
489 if (end_pos == StringRef::npos)
492 return path.substr(0, end_pos);
495 void remove_filename(SmallVectorImpl<char> &path) {
496 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
497 if (end_pos != StringRef::npos)
498 path.set_size(end_pos);
501 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
502 StringRef p(path.begin(), path.size());
503 SmallString<32> ext_storage;
504 StringRef ext = extension.toStringRef(ext_storage);
506 // Erase existing extension.
507 size_t pos = p.find_last_of('.');
508 if (pos != StringRef::npos && pos >= filename_pos(p))
511 // Append '.' if needed.
512 if (ext.size() > 0 && ext[0] != '.')
516 path.append(ext.begin(), ext.end());
519 void native(const Twine &path, SmallVectorImpl<char> &result) {
520 assert((!path.isSingleStringRef() ||
521 path.getSingleStringRef().data() != result.data()) &&
522 "path and result are not allowed to overlap!");
525 path.toVector(result);
529 void native(SmallVectorImpl<char> &path) {
531 std::replace(path.begin(), path.end(), '/', '\\');
535 const StringRef filename(StringRef path) {
536 return *(--end(path));
539 const StringRef stem(StringRef path) {
540 StringRef fname = filename(path);
541 size_t pos = fname.find_last_of('.');
542 if (pos == StringRef::npos)
545 if ((fname.size() == 1 && fname == ".") ||
546 (fname.size() == 2 && fname == ".."))
549 return fname.substr(0, pos);
552 const StringRef extension(StringRef path) {
553 StringRef fname = filename(path);
554 size_t pos = fname.find_last_of('.');
555 if (pos == StringRef::npos)
558 if ((fname.size() == 1 && fname == ".") ||
559 (fname.size() == 2 && fname == ".."))
562 return fname.substr(pos);
565 bool is_separator(char value) {
568 case '\\': // fall through
570 case '/': return true;
571 default: return false;
575 void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result) {
578 #if defined(_CS_DARWIN_USER_TEMP_DIR) && defined(_CS_DARWIN_USER_CACHE_DIR)
579 // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR.
580 // macros defined in <unistd.h> on darwin >= 9
581 int ConfName = erasedOnReboot? _CS_DARWIN_USER_TEMP_DIR
582 : _CS_DARWIN_USER_CACHE_DIR;
583 size_t ConfLen = confstr(ConfName, 0, 0);
586 result.resize(ConfLen);
587 ConfLen = confstr(ConfName, result.data(), result.size());
588 } while (ConfLen > 0 && ConfLen != result.size());
591 assert(result.back() == 0);
600 // Check whether the temporary directory is specified by an environment
602 const char *EnvironmentVariable;
604 EnvironmentVariable = "TEMP";
606 EnvironmentVariable = "TMPDIR";
608 if (char *RequestedDir = getenv(EnvironmentVariable)) {
609 result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
613 // Fall back to a system default.
614 const char *DefaultResult;
616 (void)erasedOnReboot;
617 DefaultResult = "C:\\TEMP";
620 DefaultResult = "/tmp";
622 DefaultResult = "/var/tmp";
624 result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
627 bool has_root_name(const Twine &path) {
628 SmallString<128> path_storage;
629 StringRef p = path.toStringRef(path_storage);
631 return !root_name(p).empty();
634 bool has_root_directory(const Twine &path) {
635 SmallString<128> path_storage;
636 StringRef p = path.toStringRef(path_storage);
638 return !root_directory(p).empty();
641 bool has_root_path(const Twine &path) {
642 SmallString<128> path_storage;
643 StringRef p = path.toStringRef(path_storage);
645 return !root_path(p).empty();
648 bool has_relative_path(const Twine &path) {
649 SmallString<128> path_storage;
650 StringRef p = path.toStringRef(path_storage);
652 return !relative_path(p).empty();
655 bool has_filename(const Twine &path) {
656 SmallString<128> path_storage;
657 StringRef p = path.toStringRef(path_storage);
659 return !filename(p).empty();
662 bool has_parent_path(const Twine &path) {
663 SmallString<128> path_storage;
664 StringRef p = path.toStringRef(path_storage);
666 return !parent_path(p).empty();
669 bool has_stem(const Twine &path) {
670 SmallString<128> path_storage;
671 StringRef p = path.toStringRef(path_storage);
673 return !stem(p).empty();
676 bool has_extension(const Twine &path) {
677 SmallString<128> path_storage;
678 StringRef p = path.toStringRef(path_storage);
680 return !extension(p).empty();
683 bool is_absolute(const Twine &path) {
684 SmallString<128> path_storage;
685 StringRef p = path.toStringRef(path_storage);
687 bool rootDir = has_root_directory(p),
689 rootName = has_root_name(p);
694 return rootDir && rootName;
697 bool is_relative(const Twine &path) {
698 return !is_absolute(path);
701 } // end namespace path
705 error_code getUniqueID(const Twine Path, UniqueID &Result) {
707 error_code EC = status(Path, Status);
710 Result = Status.getUniqueID();
711 return error_code::success();
714 error_code createUniqueFile(const Twine &Model, int &ResultFd,
715 SmallVectorImpl<char> &ResultPath, unsigned Mode) {
716 return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
719 error_code createUniqueFile(const Twine &Model,
720 SmallVectorImpl<char> &ResultPath) {
722 return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
725 static error_code createTemporaryFile(const Twine &Model, int &ResultFD,
726 llvm::SmallVectorImpl<char> &ResultPath,
728 SmallString<128> Storage;
729 StringRef P = Model.toNullTerminatedStringRef(Storage);
730 assert(P.find_first_of(separators) == StringRef::npos &&
731 "Model must be a simple filename.");
732 // Use P.begin() so that createUniqueEntity doesn't need to recreate Storage.
733 return createUniqueEntity(P.begin(), ResultFD, ResultPath,
734 true, owner_read | owner_write, Type);
738 createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
739 llvm::SmallVectorImpl<char> &ResultPath,
741 const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
742 return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
747 error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
749 SmallVectorImpl<char> &ResultPath) {
750 return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File);
753 error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
754 SmallVectorImpl<char> &ResultPath) {
756 return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
760 // This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
761 // for consistency. We should try using mkdtemp.
762 error_code createUniqueDirectory(const Twine &Prefix,
763 SmallVectorImpl<char> &ResultPath) {
765 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath,
769 error_code make_absolute(SmallVectorImpl<char> &path) {
770 StringRef p(path.data(), path.size());
772 bool rootDirectory = path::has_root_directory(p),
774 rootName = path::has_root_name(p);
780 if (rootName && rootDirectory)
781 return error_code::success();
783 // All of the following conditions will need the current directory.
784 SmallString<128> current_dir;
785 if (error_code ec = current_path(current_dir)) return ec;
787 // Relative path. Prepend the current directory.
788 if (!rootName && !rootDirectory) {
789 // Append path to the current directory.
790 path::append(current_dir, p);
791 // Set path to the result.
792 path.swap(current_dir);
793 return error_code::success();
796 if (!rootName && rootDirectory) {
797 StringRef cdrn = path::root_name(current_dir);
798 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
799 path::append(curDirRootName, p);
800 // Set path to the result.
801 path.swap(curDirRootName);
802 return error_code::success();
805 if (rootName && !rootDirectory) {
806 StringRef pRootName = path::root_name(p);
807 StringRef bRootDirectory = path::root_directory(current_dir);
808 StringRef bRelativePath = path::relative_path(current_dir);
809 StringRef pRelativePath = path::relative_path(p);
811 SmallString<128> res;
812 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
814 return error_code::success();
817 llvm_unreachable("All rootName and rootDirectory combinations should have "
821 error_code create_directories(const Twine &Path, bool IgnoreExisting) {
822 SmallString<128> PathStorage;
823 StringRef P = Path.toStringRef(PathStorage);
825 // Be optimistic and try to create the directory
826 error_code EC = create_directory(P, IgnoreExisting);
827 // If we succeeded, or had any error other than the parent not existing, just
829 if (EC != errc::no_such_file_or_directory)
832 // We failed because of a no_such_file_or_directory, try to create the
834 StringRef Parent = path::parent_path(P);
838 if ((EC = create_directories(Parent)))
841 return create_directory(P, IgnoreExisting);
844 bool exists(file_status status) {
845 return status_known(status) && status.type() != file_type::file_not_found;
848 bool status_known(file_status s) {
849 return s.type() != file_type::status_error;
852 bool is_directory(file_status status) {
853 return status.type() == file_type::directory_file;
856 error_code is_directory(const Twine &path, bool &result) {
858 if (error_code ec = status(path, st))
860 result = is_directory(st);
861 return error_code::success();
864 bool is_regular_file(file_status status) {
865 return status.type() == file_type::regular_file;
868 error_code is_regular_file(const Twine &path, bool &result) {
870 if (error_code ec = status(path, st))
872 result = is_regular_file(st);
873 return error_code::success();
876 bool is_symlink(file_status status) {
877 return status.type() == file_type::symlink_file;
880 error_code is_symlink(const Twine &path, bool &result) {
882 if (error_code ec = status(path, st))
884 result = is_symlink(st);
885 return error_code::success();
888 bool is_other(file_status status) {
889 return exists(status) &&
890 !is_regular_file(status) &&
891 !is_directory(status) &&
895 void directory_entry::replace_filename(const Twine &filename, file_status st) {
896 SmallString<128> path(Path.begin(), Path.end());
897 path::remove_filename(path);
898 path::append(path, filename);
903 error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
904 SmallString<32> MagicStorage;
905 StringRef Magic = magic.toStringRef(MagicStorage);
906 SmallString<32> Buffer;
908 if (error_code ec = get_magic(path, Magic.size(), Buffer)) {
909 if (ec == errc::value_too_large) {
910 // Magic.size() > file_size(Path).
912 return error_code::success();
917 result = Magic == Buffer;
918 return error_code::success();
921 /// @brief Identify the magic in magic.
922 file_magic identify_magic(StringRef Magic) {
923 if (Magic.size() < 4)
924 return file_magic::unknown;
925 switch ((unsigned char)Magic[0]) {
927 // COFF short import library file
928 if (Magic[1] == (char)0x00 && Magic[2] == (char)0xff &&
929 Magic[3] == (char)0xff)
930 return file_magic::coff_import_library;
931 // Windows resource file
932 const char Expected[] = { 0, 0, 0, 0, '\x20', 0, 0, 0, '\xff' };
933 if (Magic.size() >= sizeof(Expected) &&
934 memcmp(Magic.data(), Expected, sizeof(Expected)) == 0)
935 return file_magic::windows_resource;
936 // 0x0000 = COFF unknown machine type
938 return file_magic::coff_object;
941 case 0xDE: // 0x0B17C0DE = BC wraper
942 if (Magic[1] == (char)0xC0 && Magic[2] == (char)0x17 &&
943 Magic[3] == (char)0x0B)
944 return file_magic::bitcode;
947 if (Magic[1] == 'C' && Magic[2] == (char)0xC0 && Magic[3] == (char)0xDE)
948 return file_magic::bitcode;
951 if (Magic.size() >= 8)
952 if (memcmp(Magic.data(),"!<arch>\n",8) == 0)
953 return file_magic::archive;
957 if (Magic.size() >= 18 && Magic[1] == 'E' && Magic[2] == 'L' &&
959 bool Data2MSB = Magic[5] == 2;
960 unsigned high = Data2MSB ? 16 : 17;
961 unsigned low = Data2MSB ? 17 : 16;
962 if (Magic[high] == 0)
963 switch (Magic[low]) {
965 case 1: return file_magic::elf_relocatable;
966 case 2: return file_magic::elf_executable;
967 case 3: return file_magic::elf_shared_object;
968 case 4: return file_magic::elf_core;
974 if (Magic[1] == char(0xFE) && Magic[2] == char(0xBA) &&
975 Magic[3] == char(0xBE)) {
976 // This is complicated by an overlap with Java class files.
977 // See the Mach-O section in /usr/share/file/magic for details.
978 if (Magic.size() >= 8 && Magic[7] < 43)
979 return file_magic::macho_universal_binary;
983 // The two magic numbers for mach-o are:
984 // 0xfeedface - 32-bit mach-o
985 // 0xfeedfacf - 64-bit mach-o
990 if (Magic[0] == char(0xFE) && Magic[1] == char(0xED) &&
991 Magic[2] == char(0xFA) &&
992 (Magic[3] == char(0xCE) || Magic[3] == char(0xCF))) {
994 if (Magic.size() >= 16) type = Magic[14] << 8 | Magic[15];
995 } else if ((Magic[0] == char(0xCE) || Magic[0] == char(0xCF)) &&
996 Magic[1] == char(0xFA) && Magic[2] == char(0xED) &&
997 Magic[3] == char(0xFE)) {
999 if (Magic.size() >= 14) type = Magic[13] << 8 | Magic[12];
1003 case 1: return file_magic::macho_object;
1004 case 2: return file_magic::macho_executable;
1005 case 3: return file_magic::macho_fixed_virtual_memory_shared_lib;
1006 case 4: return file_magic::macho_core;
1007 case 5: return file_magic::macho_preload_executable;
1008 case 6: return file_magic::macho_dynamically_linked_shared_lib;
1009 case 7: return file_magic::macho_dynamic_linker;
1010 case 8: return file_magic::macho_bundle;
1011 case 9: return file_magic::macho_dynamic_linker;
1012 case 10: return file_magic::macho_dsym_companion;
1016 case 0xF0: // PowerPC Windows
1017 case 0x83: // Alpha 32-bit
1018 case 0x84: // Alpha 64-bit
1019 case 0x66: // MPS R4000 Windows
1021 case 0x4c: // 80386 Windows
1022 if (Magic[1] == 0x01)
1023 return file_magic::coff_object;
1025 case 0x90: // PA-RISC Windows
1026 case 0x68: // mc68K Windows
1027 if (Magic[1] == 0x02)
1028 return file_magic::coff_object;
1031 case 0x4d: // Possible MS-DOS stub on Windows PE file
1032 if (Magic[1] == 0x5a) {
1034 *reinterpret_cast<const support::ulittle32_t*>(Magic.data() + 0x3c);
1035 // PE/COFF file, either EXE or DLL.
1036 if (off < Magic.size() && memcmp(Magic.data() + off, "PE\0\0",4) == 0)
1037 return file_magic::pecoff_executable;
1041 case 0x64: // x86-64 Windows.
1042 if (Magic[1] == char(0x86))
1043 return file_magic::coff_object;
1049 return file_magic::unknown;
1052 error_code identify_magic(const Twine &path, file_magic &result) {
1053 SmallString<32> Magic;
1054 error_code ec = get_magic(path, Magic.capacity(), Magic);
1055 if (ec && ec != errc::value_too_large)
1058 result = identify_magic(Magic);
1059 return error_code::success();
1062 error_code directory_entry::status(file_status &result) const {
1063 return fs::status(Path, result);
1066 } // end namespace fs
1067 } // end namespace sys
1068 } // end namespace llvm
1070 // Include the truly platform-specific parts.
1071 #if defined(LLVM_ON_UNIX)
1072 #include "Unix/Path.inc"
1074 #if defined(LLVM_ON_WIN32)
1075 #include "Windows/Path.inc"