1 //===-- PathV2.cpp - Implement OS Path Concept ------------------*- C++ -*-===//
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 PathV2 API.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/PathV2.h"
15 #include "llvm/Support/Endian.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/FileSystem.h"
26 using llvm::StringRef;
27 using llvm::sys::path::is_separator;
30 const char *separators = "\\/";
31 const char prefered_separator = '\\';
33 const char separators = '/';
34 const char prefered_separator = '/';
37 StringRef find_first_component(StringRef path) {
38 // Look for this first component in the following order.
39 // * empty (in this case we return an empty string)
40 // * either C: or {//,\\}net.
43 // * {file,directory}name
50 if (path.size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) &&
52 return path.substr(0, 2);
56 if ((path.size() > 2) &&
57 is_separator(path[0]) &&
59 !is_separator(path[2])) {
60 // Find the next directory separator.
61 size_t end = path.find_first_of(separators, 2);
62 return path.substr(0, end);
66 if (is_separator(path[0]))
67 return path.substr(0, 1);
69 if (path.startswith(".."))
70 return path.substr(0, 2);
73 return path.substr(0, 1);
75 // * {file,directory}name
76 size_t end = path.find_first_of(separators, 2);
77 return path.substr(0, end);
80 size_t filename_pos(StringRef str) {
81 if (str.size() == 2 &&
82 is_separator(str[0]) &&
86 if (str.size() > 0 && is_separator(str[str.size() - 1]))
87 return str.size() - 1;
89 size_t pos = str.find_last_of(separators, str.size() - 1);
92 if (pos == StringRef::npos)
93 pos = str.find_last_of(':', str.size() - 2);
96 if (pos == StringRef::npos ||
97 (pos == 1 && is_separator(str[0])))
103 size_t root_dir_start(StringRef str) {
106 if (str.size() > 2 &&
108 is_separator(str[2]))
113 if (str.size() == 2 &&
114 is_separator(str[0]) &&
116 return StringRef::npos;
119 if (str.size() > 3 &&
120 is_separator(str[0]) &&
122 !is_separator(str[2])) {
123 return str.find_first_of(separators, 2);
127 if (str.size() > 0 && is_separator(str[0]))
130 return StringRef::npos;
133 size_t parent_path_end(StringRef path) {
134 size_t end_pos = filename_pos(path);
136 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
138 // Skip separators except for root dir.
139 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
142 (end_pos - 1) != root_dir_pos &&
143 is_separator(path[end_pos - 1]))
146 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
147 return StringRef::npos;
151 } // end unnamed namespace
157 const_iterator begin(StringRef path) {
160 i.Component = find_first_component(path);
165 const_iterator end(StringRef path) {
168 i.Position = path.size();
172 const_iterator &const_iterator::operator++() {
173 assert(Position < Path.size() && "Tried to increment past end!");
175 // Increment Position to past the current component
176 Position += Component.size();
179 if (Position == Path.size()) {
180 Component = StringRef();
184 // Both POSIX and Windows treat paths that begin with exactly two separators
186 bool was_net = Component.size() > 2 &&
187 is_separator(Component[0]) &&
188 Component[1] == Component[0] &&
189 !is_separator(Component[2]);
191 // Handle separators.
192 if (is_separator(Path[Position])) {
197 || Component.endswith(":")
200 Component = Path.substr(Position, 1);
204 // Skip extra separators.
205 while (Position != Path.size() &&
206 is_separator(Path[Position])) {
210 // Treat trailing '/' as a '.'.
211 if (Position == Path.size()) {
218 // Find next component.
219 size_t end_pos = Path.find_first_of(separators, Position);
220 Component = Path.slice(Position, end_pos);
225 const_iterator &const_iterator::operator--() {
226 // If we're at the end and the previous char was a '/', return '.'.
227 if (Position == Path.size() &&
229 is_separator(Path[Position - 1])
231 && Path[Position - 2] != ':'
239 // Skip separators unless it's the root directory.
240 size_t root_dir_pos = root_dir_start(Path);
241 size_t end_pos = Position;
244 (end_pos - 1) != root_dir_pos &&
245 is_separator(Path[end_pos - 1]))
248 // Find next separator.
249 size_t start_pos = filename_pos(Path.substr(0, end_pos));
250 Component = Path.slice(start_pos, end_pos);
251 Position = start_pos;
255 bool const_iterator::operator==(const const_iterator &RHS) const {
256 return Path.begin() == RHS.Path.begin() &&
257 Position == RHS.Position;
260 bool const_iterator::operator!=(const const_iterator &RHS) const {
261 return !(*this == RHS);
264 ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
265 return Position - RHS.Position;
268 const StringRef root_path(StringRef path) {
269 const_iterator b = begin(path),
273 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
281 if (has_net || has_drive) {
282 if ((++pos != e) && is_separator((*pos)[0])) {
283 // {C:/,//net/}, so get the first two components.
284 return path.substr(0, b->size() + pos->size());
286 // just {C:,//net}, return the first component.
291 // POSIX style root directory.
292 if (is_separator((*b)[0])) {
300 const StringRef root_name(StringRef path) {
301 const_iterator b = begin(path),
304 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
312 if (has_net || has_drive) {
313 // just {C:,//net}, return the first component.
318 // No path or no name.
322 const StringRef root_directory(StringRef path) {
323 const_iterator b = begin(path),
327 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
335 if ((has_net || has_drive) &&
336 // {C:,//net}, skip to the next component.
337 (++pos != e) && is_separator((*pos)[0])) {
341 // POSIX style root directory.
342 if (!has_net && is_separator((*b)[0])) {
347 // No path or no root.
351 const StringRef relative_path(StringRef path) {
352 StringRef root = root_path(path);
353 return path.substr(root.size());
356 void append(SmallVectorImpl<char> &path, const Twine &a,
360 SmallString<32> a_storage;
361 SmallString<32> b_storage;
362 SmallString<32> c_storage;
363 SmallString<32> d_storage;
365 SmallVector<StringRef, 4> components;
366 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
367 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
368 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
369 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
371 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
372 e = components.end();
374 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
375 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
376 bool is_root_name = has_root_name(*i);
379 // Strip separators from beginning of component.
380 size_t loc = i->find_first_not_of(separators);
381 StringRef c = i->substr(loc);
384 path.append(c.begin(), c.end());
388 if (!component_has_sep && !(path.empty() || is_root_name)) {
390 path.push_back(prefered_separator);
393 path.append(i->begin(), i->end());
397 void append(SmallVectorImpl<char> &path,
398 const_iterator begin, const_iterator end) {
399 for (; begin != end; ++begin)
400 path::append(path, *begin);
403 const StringRef parent_path(StringRef path) {
404 size_t end_pos = parent_path_end(path);
405 if (end_pos == StringRef::npos)
408 return path.substr(0, end_pos);
411 void remove_filename(SmallVectorImpl<char> &path) {
412 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
413 if (end_pos != StringRef::npos)
414 path.set_size(end_pos);
417 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
418 StringRef p(path.begin(), path.size());
419 SmallString<32> ext_storage;
420 StringRef ext = extension.toStringRef(ext_storage);
422 // Erase existing extension.
423 size_t pos = p.find_last_of('.');
424 if (pos != StringRef::npos && pos >= filename_pos(p))
427 // Append '.' if needed.
428 if (ext.size() > 0 && ext[0] != '.')
432 path.append(ext.begin(), ext.end());
435 void native(const Twine &path, SmallVectorImpl<char> &result) {
439 SmallString<128> path_storage;
440 StringRef p = path.toStringRef(path_storage);
441 result.reserve(p.size());
442 for (StringRef::const_iterator i = p.begin(),
447 result.push_back('\\');
449 result.push_back(*i);
452 path.toVector(result);
456 const StringRef filename(StringRef path) {
457 return *(--end(path));
460 const StringRef stem(StringRef path) {
461 StringRef fname = filename(path);
462 size_t pos = fname.find_last_of('.');
463 if (pos == StringRef::npos)
466 if ((fname.size() == 1 && fname == ".") ||
467 (fname.size() == 2 && fname == ".."))
470 return fname.substr(0, pos);
473 const StringRef extension(StringRef path) {
474 StringRef fname = filename(path);
475 size_t pos = fname.find_last_of('.');
476 if (pos == StringRef::npos)
479 if ((fname.size() == 1 && fname == ".") ||
480 (fname.size() == 2 && fname == ".."))
483 return fname.substr(pos);
486 bool is_separator(char value) {
489 case '\\': // fall through
491 case '/': return true;
492 default: return false;
496 void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result) {
500 // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR.
501 int ConfName = erasedOnReboot? _CS_DARWIN_USER_TEMP_DIR
502 : _CS_DARWIN_USER_CACHE_DIR;
503 size_t ConfLen = confstr(ConfName, 0, 0);
506 result.resize(ConfLen);
507 ConfLen = confstr(ConfName, result.data(), result.size());
508 } while (ConfLen > 0 && ConfLen != result.size());
511 assert(result.back() == 0);
520 // Check whether the temporary directory is specified by an environment
522 const char *EnvironmentVariable;
524 EnvironmentVariable = "TEMP";
526 EnvironmentVariable = "TMPDIR";
528 if (char *RequestedDir = getenv(EnvironmentVariable)) {
529 result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
533 // Fall back to a system default.
534 const char *DefaultResult;
536 (void)erasedOnReboot;
537 DefaultResult = "C:\\TEMP";
540 DefaultResult = "/tmp";
542 DefaultResult = "/var/tmp";
544 result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
547 bool has_root_name(const Twine &path) {
548 SmallString<128> path_storage;
549 StringRef p = path.toStringRef(path_storage);
551 return !root_name(p).empty();
554 bool has_root_directory(const Twine &path) {
555 SmallString<128> path_storage;
556 StringRef p = path.toStringRef(path_storage);
558 return !root_directory(p).empty();
561 bool has_root_path(const Twine &path) {
562 SmallString<128> path_storage;
563 StringRef p = path.toStringRef(path_storage);
565 return !root_path(p).empty();
568 bool has_relative_path(const Twine &path) {
569 SmallString<128> path_storage;
570 StringRef p = path.toStringRef(path_storage);
572 return !relative_path(p).empty();
575 bool has_filename(const Twine &path) {
576 SmallString<128> path_storage;
577 StringRef p = path.toStringRef(path_storage);
579 return !filename(p).empty();
582 bool has_parent_path(const Twine &path) {
583 SmallString<128> path_storage;
584 StringRef p = path.toStringRef(path_storage);
586 return !parent_path(p).empty();
589 bool has_stem(const Twine &path) {
590 SmallString<128> path_storage;
591 StringRef p = path.toStringRef(path_storage);
593 return !stem(p).empty();
596 bool has_extension(const Twine &path) {
597 SmallString<128> path_storage;
598 StringRef p = path.toStringRef(path_storage);
600 return !extension(p).empty();
603 bool is_absolute(const Twine &path) {
604 SmallString<128> path_storage;
605 StringRef p = path.toStringRef(path_storage);
607 bool rootDir = has_root_directory(p),
609 rootName = has_root_name(p);
614 return rootDir && rootName;
617 bool is_relative(const Twine &path) {
618 return !is_absolute(path);
621 } // end namespace path
625 error_code make_absolute(SmallVectorImpl<char> &path) {
626 StringRef p(path.data(), path.size());
628 bool rootDirectory = path::has_root_directory(p),
630 rootName = path::has_root_name(p);
636 if (rootName && rootDirectory)
637 return error_code::success();
639 // All of the following conditions will need the current directory.
640 SmallString<128> current_dir;
641 if (error_code ec = current_path(current_dir)) return ec;
643 // Relative path. Prepend the current directory.
644 if (!rootName && !rootDirectory) {
645 // Append path to the current directory.
646 path::append(current_dir, p);
647 // Set path to the result.
648 path.swap(current_dir);
649 return error_code::success();
652 if (!rootName && rootDirectory) {
653 StringRef cdrn = path::root_name(current_dir);
654 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
655 path::append(curDirRootName, p);
656 // Set path to the result.
657 path.swap(curDirRootName);
658 return error_code::success();
661 if (rootName && !rootDirectory) {
662 StringRef pRootName = path::root_name(p);
663 StringRef bRootDirectory = path::root_directory(current_dir);
664 StringRef bRelativePath = path::relative_path(current_dir);
665 StringRef pRelativePath = path::relative_path(p);
667 SmallString<128> res;
668 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
670 return error_code::success();
673 llvm_unreachable("All rootName and rootDirectory combinations should have "
677 error_code create_directories(const Twine &path, bool &existed) {
678 SmallString<128> path_storage;
679 StringRef p = path.toStringRef(path_storage);
681 StringRef parent = path::parent_path(p);
682 if (!parent.empty()) {
684 if (error_code ec = fs::exists(parent, parent_exists)) return ec;
687 if (error_code ec = create_directories(parent, existed)) return ec;
690 return create_directory(p, existed);
693 bool exists(file_status status) {
694 return status_known(status) && status.type() != file_type::file_not_found;
697 bool status_known(file_status s) {
698 return s.type() != file_type::status_error;
701 bool is_directory(file_status status) {
702 return status.type() == file_type::directory_file;
705 error_code is_directory(const Twine &path, bool &result) {
707 if (error_code ec = status(path, st))
709 result = is_directory(st);
710 return error_code::success();
713 bool is_regular_file(file_status status) {
714 return status.type() == file_type::regular_file;
717 error_code is_regular_file(const Twine &path, bool &result) {
719 if (error_code ec = status(path, st))
721 result = is_regular_file(st);
722 return error_code::success();
725 bool is_symlink(file_status status) {
726 return status.type() == file_type::symlink_file;
729 error_code is_symlink(const Twine &path, bool &result) {
731 if (error_code ec = status(path, st))
733 result = is_symlink(st);
734 return error_code::success();
737 bool is_other(file_status status) {
738 return exists(status) &&
739 !is_regular_file(status) &&
740 !is_directory(status) &&
744 void directory_entry::replace_filename(const Twine &filename, file_status st) {
745 SmallString<128> path(Path.begin(), Path.end());
746 path::remove_filename(path);
747 path::append(path, filename);
752 error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
753 SmallString<32> MagicStorage;
754 StringRef Magic = magic.toStringRef(MagicStorage);
755 SmallString<32> Buffer;
757 if (error_code ec = get_magic(path, Magic.size(), Buffer)) {
758 if (ec == errc::value_too_large) {
759 // Magic.size() > file_size(Path).
761 return error_code::success();
766 result = Magic == Buffer;
767 return error_code::success();
770 /// @brief Identify the magic in magic.
771 file_magic identify_magic(StringRef magic) {
772 if (magic.size() < 4)
773 return file_magic::unknown;
774 switch ((unsigned char)magic[0]) {
775 case 0xDE: // 0x0B17C0DE = BC wraper
776 if (magic[1] == (char)0xC0 && magic[2] == (char)0x17 &&
777 magic[3] == (char)0x0B)
778 return file_magic::bitcode;
781 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
782 return file_magic::bitcode;
785 if (magic.size() >= 8)
786 if (memcmp(magic.data(),"!<arch>\n",8) == 0)
787 return file_magic::archive;
791 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
792 bool Data2MSB = magic[5] == 2;
793 unsigned high = Data2MSB ? 16 : 17;
794 unsigned low = Data2MSB ? 17 : 16;
795 if (magic.size() >= 18 && magic[high] == 0)
796 switch (magic[low]) {
798 case 1: return file_magic::elf_relocatable;
799 case 2: return file_magic::elf_executable;
800 case 3: return file_magic::elf_shared_object;
801 case 4: return file_magic::elf_core;
807 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
808 magic[3] == char(0xBE)) {
809 // This is complicated by an overlap with Java class files.
810 // See the Mach-O section in /usr/share/file/magic for details.
811 if (magic.size() >= 8 && magic[7] < 43)
812 // FIXME: Universal Binary of any type.
813 return file_magic::macho_dynamically_linked_shared_lib;
817 // The two magic numbers for mach-o are:
818 // 0xfeedface - 32-bit mach-o
819 // 0xfeedfacf - 64-bit mach-o
824 if (magic[0] == char(0xFE) && magic[1] == char(0xED) &&
825 magic[2] == char(0xFA) &&
826 (magic[3] == char(0xCE) || magic[3] == char(0xCF))) {
828 if (magic.size() >= 16) type = magic[14] << 8 | magic[15];
829 } else if ((magic[0] == char(0xCE) || magic[0] == char(0xCF)) &&
830 magic[1] == char(0xFA) && magic[2] == char(0xED) &&
831 magic[3] == char(0xFE)) {
833 if (magic.size() >= 14) type = magic[13] << 8 | magic[12];
837 case 1: return file_magic::macho_object;
838 case 2: return file_magic::macho_executable;
839 case 3: return file_magic::macho_fixed_virtual_memory_shared_lib;
840 case 4: return file_magic::macho_core;
841 case 5: return file_magic::macho_preload_executabl;
842 case 6: return file_magic::macho_dynamically_linked_shared_lib;
843 case 7: return file_magic::macho_dynamic_linker;
844 case 8: return file_magic::macho_bundle;
845 case 9: return file_magic::macho_dynamic_linker;
846 case 10: return file_magic::macho_dsym_companion;
850 case 0xF0: // PowerPC Windows
851 case 0x83: // Alpha 32-bit
852 case 0x84: // Alpha 64-bit
853 case 0x66: // MPS R4000 Windows
855 case 0x4c: // 80386 Windows
856 if (magic[1] == 0x01)
857 return file_magic::coff_object;
859 case 0x90: // PA-RISC Windows
860 case 0x68: // mc68K Windows
861 if (magic[1] == 0x02)
862 return file_magic::coff_object;
865 case 0x4d: // Possible MS-DOS stub on Windows PE file
866 if (magic[1] == 0x5a) {
868 *reinterpret_cast<const support::ulittle32_t*>(magic.data() + 0x3c);
869 // PE/COFF file, either EXE or DLL.
870 if (off < magic.size() && memcmp(magic.data() + off, "PE\0\0",4) == 0)
871 return file_magic::pecoff_executable;
875 case 0x64: // x86-64 Windows.
876 if (magic[1] == char(0x86))
877 return file_magic::coff_object;
883 return file_magic::unknown;
886 error_code identify_magic(const Twine &path, file_magic &result) {
887 SmallString<32> Magic;
888 error_code ec = get_magic(path, Magic.capacity(), Magic);
889 if (ec && ec != errc::value_too_large)
892 result = identify_magic(Magic);
893 return error_code::success();
897 error_code remove_all_r(StringRef path, file_type ft, uint32_t &count) {
898 if (ft == file_type::directory_file) {
899 // This code would be a lot better with exceptions ;/.
901 directory_iterator i(path, ec);
903 for (directory_iterator e; i != e; i.increment(ec)) {
906 if (error_code ec = i->status(st)) return ec;
907 if (error_code ec = remove_all_r(i->path(), st.type(), count)) return ec;
909 bool obviously_this_exists;
910 if (error_code ec = remove(path, obviously_this_exists)) return ec;
911 assert(obviously_this_exists);
912 ++count; // Include the directory itself in the items removed.
914 bool obviously_this_exists;
915 if (error_code ec = remove(path, obviously_this_exists)) return ec;
916 assert(obviously_this_exists);
920 return error_code::success();
922 } // end unnamed namespace
924 error_code remove_all(const Twine &path, uint32_t &num_removed) {
925 SmallString<128> path_storage;
926 StringRef p = path.toStringRef(path_storage);
929 if (error_code ec = status(path, fs))
932 return remove_all_r(p, fs.type(), num_removed);
935 error_code directory_entry::status(file_status &result) const {
936 return fs::status(Path, result);
939 } // end namespace fs
940 } // end namespace sys
941 } // end namespace llvm
943 // Include the truly platform-specific parts.
944 #if defined(LLVM_ON_UNIX)
945 #include "Unix/PathV2.inc"
947 #if defined(LLVM_ON_WIN32)
948 #include "Windows/PathV2.inc"