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 '.' unless
311 // we are the root path.
312 size_t root_dir_pos = root_dir_start(Path);
313 if (Position == Path.size() &&
314 Path.size() > root_dir_pos + 1 &&
315 is_separator(Path[Position - 1])) {
321 // Skip separators unless it's the root directory.
322 size_t end_pos = Position;
325 (end_pos - 1) != root_dir_pos &&
326 is_separator(Path[end_pos - 1]))
329 // Find next separator.
330 size_t start_pos = filename_pos(Path.substr(0, end_pos));
331 Component = Path.slice(start_pos, end_pos);
332 Position = start_pos;
336 bool const_iterator::operator==(const const_iterator &RHS) const {
337 return Path.begin() == RHS.Path.begin() &&
338 Position == RHS.Position;
341 bool const_iterator::operator!=(const const_iterator &RHS) const {
342 return !(*this == RHS);
345 ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
346 return Position - RHS.Position;
349 const StringRef root_path(StringRef path) {
350 const_iterator b = begin(path),
354 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
362 if (has_net || has_drive) {
363 if ((++pos != e) && is_separator((*pos)[0])) {
364 // {C:/,//net/}, so get the first two components.
365 return path.substr(0, b->size() + pos->size());
367 // just {C:,//net}, return the first component.
372 // POSIX style root directory.
373 if (is_separator((*b)[0])) {
381 const StringRef root_name(StringRef path) {
382 const_iterator b = begin(path),
385 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
393 if (has_net || has_drive) {
394 // just {C:,//net}, return the first component.
399 // No path or no name.
403 const StringRef root_directory(StringRef path) {
404 const_iterator b = begin(path),
408 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
416 if ((has_net || has_drive) &&
417 // {C:,//net}, skip to the next component.
418 (++pos != e) && is_separator((*pos)[0])) {
422 // POSIX style root directory.
423 if (!has_net && is_separator((*b)[0])) {
428 // No path or no root.
432 const StringRef relative_path(StringRef path) {
433 StringRef root = root_path(path);
434 return path.substr(root.size());
437 void append(SmallVectorImpl<char> &path, const Twine &a,
441 SmallString<32> a_storage;
442 SmallString<32> b_storage;
443 SmallString<32> c_storage;
444 SmallString<32> d_storage;
446 SmallVector<StringRef, 4> components;
447 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
448 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
449 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
450 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
452 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
453 e = components.end();
455 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
456 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
457 bool is_root_name = has_root_name(*i);
460 // Strip separators from beginning of component.
461 size_t loc = i->find_first_not_of(separators);
462 StringRef c = i->substr(loc);
465 path.append(c.begin(), c.end());
469 if (!component_has_sep && !(path.empty() || is_root_name)) {
471 path.push_back(preferred_separator);
474 path.append(i->begin(), i->end());
478 void append(SmallVectorImpl<char> &path,
479 const_iterator begin, const_iterator end) {
480 for (; begin != end; ++begin)
481 path::append(path, *begin);
484 const StringRef parent_path(StringRef path) {
485 size_t end_pos = parent_path_end(path);
486 if (end_pos == StringRef::npos)
489 return path.substr(0, end_pos);
492 void remove_filename(SmallVectorImpl<char> &path) {
493 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
494 if (end_pos != StringRef::npos)
495 path.set_size(end_pos);
498 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
499 StringRef p(path.begin(), path.size());
500 SmallString<32> ext_storage;
501 StringRef ext = extension.toStringRef(ext_storage);
503 // Erase existing extension.
504 size_t pos = p.find_last_of('.');
505 if (pos != StringRef::npos && pos >= filename_pos(p))
508 // Append '.' if needed.
509 if (ext.size() > 0 && ext[0] != '.')
513 path.append(ext.begin(), ext.end());
516 void native(const Twine &path, SmallVectorImpl<char> &result) {
517 assert((!path.isSingleStringRef() ||
518 path.getSingleStringRef().data() != result.data()) &&
519 "path and result are not allowed to overlap!");
522 path.toVector(result);
526 void native(SmallVectorImpl<char> &path) {
528 std::replace(path.begin(), path.end(), '/', '\\');
532 const StringRef filename(StringRef path) {
533 return *(--end(path));
536 const StringRef stem(StringRef path) {
537 StringRef fname = filename(path);
538 size_t pos = fname.find_last_of('.');
539 if (pos == StringRef::npos)
542 if ((fname.size() == 1 && fname == ".") ||
543 (fname.size() == 2 && fname == ".."))
546 return fname.substr(0, pos);
549 const StringRef extension(StringRef path) {
550 StringRef fname = filename(path);
551 size_t pos = fname.find_last_of('.');
552 if (pos == StringRef::npos)
555 if ((fname.size() == 1 && fname == ".") ||
556 (fname.size() == 2 && fname == ".."))
559 return fname.substr(pos);
562 bool is_separator(char value) {
565 case '\\': // fall through
567 case '/': return true;
568 default: return false;
572 void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result) {
575 #if defined(_CS_DARWIN_USER_TEMP_DIR) && defined(_CS_DARWIN_USER_CACHE_DIR)
576 // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR.
577 // macros defined in <unistd.h> on darwin >= 9
578 int ConfName = erasedOnReboot? _CS_DARWIN_USER_TEMP_DIR
579 : _CS_DARWIN_USER_CACHE_DIR;
580 size_t ConfLen = confstr(ConfName, 0, 0);
583 result.resize(ConfLen);
584 ConfLen = confstr(ConfName, result.data(), result.size());
585 } while (ConfLen > 0 && ConfLen != result.size());
588 assert(result.back() == 0);
597 // Check whether the temporary directory is specified by an environment
599 const char *EnvironmentVariable;
601 EnvironmentVariable = "TEMP";
603 EnvironmentVariable = "TMPDIR";
605 if (char *RequestedDir = getenv(EnvironmentVariable)) {
606 result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
610 // Fall back to a system default.
611 const char *DefaultResult;
613 (void)erasedOnReboot;
614 DefaultResult = "C:\\TEMP";
617 DefaultResult = "/tmp";
619 DefaultResult = "/var/tmp";
621 result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
624 bool has_root_name(const Twine &path) {
625 SmallString<128> path_storage;
626 StringRef p = path.toStringRef(path_storage);
628 return !root_name(p).empty();
631 bool has_root_directory(const Twine &path) {
632 SmallString<128> path_storage;
633 StringRef p = path.toStringRef(path_storage);
635 return !root_directory(p).empty();
638 bool has_root_path(const Twine &path) {
639 SmallString<128> path_storage;
640 StringRef p = path.toStringRef(path_storage);
642 return !root_path(p).empty();
645 bool has_relative_path(const Twine &path) {
646 SmallString<128> path_storage;
647 StringRef p = path.toStringRef(path_storage);
649 return !relative_path(p).empty();
652 bool has_filename(const Twine &path) {
653 SmallString<128> path_storage;
654 StringRef p = path.toStringRef(path_storage);
656 return !filename(p).empty();
659 bool has_parent_path(const Twine &path) {
660 SmallString<128> path_storage;
661 StringRef p = path.toStringRef(path_storage);
663 return !parent_path(p).empty();
666 bool has_stem(const Twine &path) {
667 SmallString<128> path_storage;
668 StringRef p = path.toStringRef(path_storage);
670 return !stem(p).empty();
673 bool has_extension(const Twine &path) {
674 SmallString<128> path_storage;
675 StringRef p = path.toStringRef(path_storage);
677 return !extension(p).empty();
680 bool is_absolute(const Twine &path) {
681 SmallString<128> path_storage;
682 StringRef p = path.toStringRef(path_storage);
684 bool rootDir = has_root_directory(p),
686 rootName = has_root_name(p);
691 return rootDir && rootName;
694 bool is_relative(const Twine &path) {
695 return !is_absolute(path);
698 } // end namespace path
702 error_code getUniqueID(const Twine Path, UniqueID &Result) {
704 error_code EC = status(Path, Status);
707 Result = Status.getUniqueID();
708 return error_code::success();
711 error_code createUniqueFile(const Twine &Model, int &ResultFd,
712 SmallVectorImpl<char> &ResultPath, unsigned Mode) {
713 return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
716 error_code createUniqueFile(const Twine &Model,
717 SmallVectorImpl<char> &ResultPath) {
719 return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
722 static error_code createTemporaryFile(const Twine &Model, int &ResultFD,
723 llvm::SmallVectorImpl<char> &ResultPath,
725 SmallString<128> Storage;
726 StringRef P = Model.toNullTerminatedStringRef(Storage);
727 assert(P.find_first_of(separators) == StringRef::npos &&
728 "Model must be a simple filename.");
729 // Use P.begin() so that createUniqueEntity doesn't need to recreate Storage.
730 return createUniqueEntity(P.begin(), ResultFD, ResultPath,
731 true, owner_read | owner_write, Type);
735 createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
736 llvm::SmallVectorImpl<char> &ResultPath,
738 const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
739 return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
744 error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
746 SmallVectorImpl<char> &ResultPath) {
747 return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File);
750 error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
751 SmallVectorImpl<char> &ResultPath) {
753 return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
757 // This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
758 // for consistency. We should try using mkdtemp.
759 error_code createUniqueDirectory(const Twine &Prefix,
760 SmallVectorImpl<char> &ResultPath) {
762 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath,
766 error_code make_absolute(SmallVectorImpl<char> &path) {
767 StringRef p(path.data(), path.size());
769 bool rootDirectory = path::has_root_directory(p),
771 rootName = path::has_root_name(p);
777 if (rootName && rootDirectory)
778 return error_code::success();
780 // All of the following conditions will need the current directory.
781 SmallString<128> current_dir;
782 if (error_code ec = current_path(current_dir)) return ec;
784 // Relative path. Prepend the current directory.
785 if (!rootName && !rootDirectory) {
786 // Append path to the current directory.
787 path::append(current_dir, p);
788 // Set path to the result.
789 path.swap(current_dir);
790 return error_code::success();
793 if (!rootName && rootDirectory) {
794 StringRef cdrn = path::root_name(current_dir);
795 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
796 path::append(curDirRootName, p);
797 // Set path to the result.
798 path.swap(curDirRootName);
799 return error_code::success();
802 if (rootName && !rootDirectory) {
803 StringRef pRootName = path::root_name(p);
804 StringRef bRootDirectory = path::root_directory(current_dir);
805 StringRef bRelativePath = path::relative_path(current_dir);
806 StringRef pRelativePath = path::relative_path(p);
808 SmallString<128> res;
809 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
811 return error_code::success();
814 llvm_unreachable("All rootName and rootDirectory combinations should have "
818 error_code create_directories(const Twine &Path, bool IgnoreExisting) {
819 SmallString<128> PathStorage;
820 StringRef P = Path.toStringRef(PathStorage);
822 // Be optimistic and try to create the directory
823 error_code EC = create_directory(P, IgnoreExisting);
824 // If we succeeded, or had any error other than the parent not existing, just
826 if (EC != errc::no_such_file_or_directory)
829 // We failed because of a no_such_file_or_directory, try to create the
831 StringRef Parent = path::parent_path(P);
835 if ((EC = create_directories(Parent)))
838 return create_directory(P, IgnoreExisting);
841 bool exists(file_status status) {
842 return status_known(status) && status.type() != file_type::file_not_found;
845 bool status_known(file_status s) {
846 return s.type() != file_type::status_error;
849 bool is_directory(file_status status) {
850 return status.type() == file_type::directory_file;
853 error_code is_directory(const Twine &path, bool &result) {
855 if (error_code ec = status(path, st))
857 result = is_directory(st);
858 return error_code::success();
861 bool is_regular_file(file_status status) {
862 return status.type() == file_type::regular_file;
865 error_code is_regular_file(const Twine &path, bool &result) {
867 if (error_code ec = status(path, st))
869 result = is_regular_file(st);
870 return error_code::success();
873 bool is_symlink(file_status status) {
874 return status.type() == file_type::symlink_file;
877 error_code is_symlink(const Twine &path, bool &result) {
879 if (error_code ec = status(path, st))
881 result = is_symlink(st);
882 return error_code::success();
885 bool is_other(file_status status) {
886 return exists(status) &&
887 !is_regular_file(status) &&
888 !is_directory(status) &&
892 void directory_entry::replace_filename(const Twine &filename, file_status st) {
893 SmallString<128> path(Path.begin(), Path.end());
894 path::remove_filename(path);
895 path::append(path, filename);
900 error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
901 SmallString<32> MagicStorage;
902 StringRef Magic = magic.toStringRef(MagicStorage);
903 SmallString<32> Buffer;
905 if (error_code ec = get_magic(path, Magic.size(), Buffer)) {
906 if (ec == errc::value_too_large) {
907 // Magic.size() > file_size(Path).
909 return error_code::success();
914 result = Magic == Buffer;
915 return error_code::success();
918 /// @brief Identify the magic in magic.
919 file_magic identify_magic(StringRef Magic) {
920 if (Magic.size() < 4)
921 return file_magic::unknown;
922 switch ((unsigned char)Magic[0]) {
924 // COFF short import library file
925 if (Magic[1] == (char)0x00 && Magic[2] == (char)0xff &&
926 Magic[3] == (char)0xff)
927 return file_magic::coff_import_library;
928 // Windows resource file
929 const char Expected[] = { 0, 0, 0, 0, '\x20', 0, 0, 0, '\xff' };
930 if (Magic.size() >= sizeof(Expected) &&
931 memcmp(Magic.data(), Expected, sizeof(Expected)) == 0)
932 return file_magic::windows_resource;
933 // 0x0000 = COFF unknown machine type
935 return file_magic::coff_object;
938 case 0xDE: // 0x0B17C0DE = BC wraper
939 if (Magic[1] == (char)0xC0 && Magic[2] == (char)0x17 &&
940 Magic[3] == (char)0x0B)
941 return file_magic::bitcode;
944 if (Magic[1] == 'C' && Magic[2] == (char)0xC0 && Magic[3] == (char)0xDE)
945 return file_magic::bitcode;
948 if (Magic.size() >= 8)
949 if (memcmp(Magic.data(),"!<arch>\n",8) == 0)
950 return file_magic::archive;
954 if (Magic.size() >= 18 && Magic[1] == 'E' && Magic[2] == 'L' &&
956 bool Data2MSB = Magic[5] == 2;
957 unsigned high = Data2MSB ? 16 : 17;
958 unsigned low = Data2MSB ? 17 : 16;
959 if (Magic[high] == 0)
960 switch (Magic[low]) {
962 case 1: return file_magic::elf_relocatable;
963 case 2: return file_magic::elf_executable;
964 case 3: return file_magic::elf_shared_object;
965 case 4: return file_magic::elf_core;
971 if (Magic[1] == char(0xFE) && Magic[2] == char(0xBA) &&
972 Magic[3] == char(0xBE)) {
973 // This is complicated by an overlap with Java class files.
974 // See the Mach-O section in /usr/share/file/magic for details.
975 if (Magic.size() >= 8 && Magic[7] < 43)
976 return file_magic::macho_universal_binary;
980 // The two magic numbers for mach-o are:
981 // 0xfeedface - 32-bit mach-o
982 // 0xfeedfacf - 64-bit mach-o
987 if (Magic[0] == char(0xFE) && Magic[1] == char(0xED) &&
988 Magic[2] == char(0xFA) &&
989 (Magic[3] == char(0xCE) || Magic[3] == char(0xCF))) {
991 if (Magic.size() >= 16) type = Magic[14] << 8 | Magic[15];
992 } else if ((Magic[0] == char(0xCE) || Magic[0] == char(0xCF)) &&
993 Magic[1] == char(0xFA) && Magic[2] == char(0xED) &&
994 Magic[3] == char(0xFE)) {
996 if (Magic.size() >= 14) type = Magic[13] << 8 | Magic[12];
1000 case 1: return file_magic::macho_object;
1001 case 2: return file_magic::macho_executable;
1002 case 3: return file_magic::macho_fixed_virtual_memory_shared_lib;
1003 case 4: return file_magic::macho_core;
1004 case 5: return file_magic::macho_preload_executable;
1005 case 6: return file_magic::macho_dynamically_linked_shared_lib;
1006 case 7: return file_magic::macho_dynamic_linker;
1007 case 8: return file_magic::macho_bundle;
1008 case 9: return file_magic::macho_dynamic_linker;
1009 case 10: return file_magic::macho_dsym_companion;
1013 case 0xF0: // PowerPC Windows
1014 case 0x83: // Alpha 32-bit
1015 case 0x84: // Alpha 64-bit
1016 case 0x66: // MPS R4000 Windows
1018 case 0x4c: // 80386 Windows
1019 case 0xc4: // ARMNT Windows
1020 if (Magic[1] == 0x01)
1021 return file_magic::coff_object;
1023 case 0x90: // PA-RISC Windows
1024 case 0x68: // mc68K Windows
1025 if (Magic[1] == 0x02)
1026 return file_magic::coff_object;
1029 case 0x4d: // Possible MS-DOS stub on Windows PE file
1030 if (Magic[1] == 0x5a) {
1032 *reinterpret_cast<const support::ulittle32_t*>(Magic.data() + 0x3c);
1033 // PE/COFF file, either EXE or DLL.
1034 if (off < Magic.size() && memcmp(Magic.data() + off, "PE\0\0",4) == 0)
1035 return file_magic::pecoff_executable;
1039 case 0x64: // x86-64 Windows.
1040 if (Magic[1] == char(0x86))
1041 return file_magic::coff_object;
1047 return file_magic::unknown;
1050 error_code identify_magic(const Twine &path, file_magic &result) {
1051 SmallString<32> Magic;
1052 error_code ec = get_magic(path, Magic.capacity(), Magic);
1053 if (ec && ec != errc::value_too_large)
1056 result = identify_magic(Magic);
1057 return error_code::success();
1060 error_code directory_entry::status(file_status &result) const {
1061 return fs::status(Path, result);
1064 } // end namespace fs
1065 } // end namespace sys
1066 } // end namespace llvm
1068 // Include the truly platform-specific parts.
1069 #if defined(LLVM_ON_UNIX)
1070 #include "Unix/Path.inc"
1072 #if defined(LLVM_ON_WIN32)
1073 #include "Windows/Path.inc"