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/Path.h"
15 #include "llvm/Support/Endian.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/FileSystem.h"
22 #if !defined(_MSC_VER) && !defined(__MINGW32__)
29 using llvm::StringRef;
30 using llvm::sys::path::is_separator;
33 const char *separators = "\\/";
34 const char prefered_separator = '\\';
36 const char separators = '/';
37 const char prefered_separator = '/';
40 StringRef find_first_component(StringRef path) {
41 // Look for this first component in the following order.
42 // * empty (in this case we return an empty string)
43 // * either C: or {//,\\}net.
46 // * {file,directory}name
53 if (path.size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) &&
55 return path.substr(0, 2);
59 if ((path.size() > 2) &&
60 is_separator(path[0]) &&
62 !is_separator(path[2])) {
63 // Find the next directory separator.
64 size_t end = path.find_first_of(separators, 2);
65 return path.substr(0, end);
69 if (is_separator(path[0]))
70 return path.substr(0, 1);
72 if (path.startswith(".."))
73 return path.substr(0, 2);
76 return path.substr(0, 1);
78 // * {file,directory}name
79 size_t end = path.find_first_of(separators, 2);
80 return path.substr(0, end);
83 size_t filename_pos(StringRef str) {
84 if (str.size() == 2 &&
85 is_separator(str[0]) &&
89 if (str.size() > 0 && is_separator(str[str.size() - 1]))
90 return str.size() - 1;
92 size_t pos = str.find_last_of(separators, str.size() - 1);
95 if (pos == StringRef::npos)
96 pos = str.find_last_of(':', str.size() - 2);
99 if (pos == StringRef::npos ||
100 (pos == 1 && is_separator(str[0])))
106 size_t root_dir_start(StringRef str) {
109 if (str.size() > 2 &&
111 is_separator(str[2]))
116 if (str.size() == 2 &&
117 is_separator(str[0]) &&
119 return StringRef::npos;
122 if (str.size() > 3 &&
123 is_separator(str[0]) &&
125 !is_separator(str[2])) {
126 return str.find_first_of(separators, 2);
130 if (str.size() > 0 && is_separator(str[0]))
133 return StringRef::npos;
136 size_t parent_path_end(StringRef path) {
137 size_t end_pos = filename_pos(path);
139 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
141 // Skip separators except for root dir.
142 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
145 (end_pos - 1) != root_dir_pos &&
146 is_separator(path[end_pos - 1]))
149 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
150 return StringRef::npos;
154 } // end unnamed namespace
160 const_iterator begin(StringRef path) {
163 i.Component = find_first_component(path);
168 const_iterator end(StringRef path) {
171 i.Position = path.size();
175 const_iterator &const_iterator::operator++() {
176 assert(Position < Path.size() && "Tried to increment past end!");
178 // Increment Position to past the current component
179 Position += Component.size();
182 if (Position == Path.size()) {
183 Component = StringRef();
187 // Both POSIX and Windows treat paths that begin with exactly two separators
189 bool was_net = Component.size() > 2 &&
190 is_separator(Component[0]) &&
191 Component[1] == Component[0] &&
192 !is_separator(Component[2]);
194 // Handle separators.
195 if (is_separator(Path[Position])) {
200 || Component.endswith(":")
203 Component = Path.substr(Position, 1);
207 // Skip extra separators.
208 while (Position != Path.size() &&
209 is_separator(Path[Position])) {
213 // Treat trailing '/' as a '.'.
214 if (Position == Path.size()) {
221 // Find next component.
222 size_t end_pos = Path.find_first_of(separators, Position);
223 Component = Path.slice(Position, end_pos);
228 const_iterator &const_iterator::operator--() {
229 // If we're at the end and the previous char was a '/', return '.'.
230 if (Position == Path.size() &&
232 is_separator(Path[Position - 1])
234 && Path[Position - 2] != ':'
242 // Skip separators unless it's the root directory.
243 size_t root_dir_pos = root_dir_start(Path);
244 size_t end_pos = Position;
247 (end_pos - 1) != root_dir_pos &&
248 is_separator(Path[end_pos - 1]))
251 // Find next separator.
252 size_t start_pos = filename_pos(Path.substr(0, end_pos));
253 Component = Path.slice(start_pos, end_pos);
254 Position = start_pos;
258 bool const_iterator::operator==(const const_iterator &RHS) const {
259 return Path.begin() == RHS.Path.begin() &&
260 Position == RHS.Position;
263 bool const_iterator::operator!=(const const_iterator &RHS) const {
264 return !(*this == RHS);
267 ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
268 return Position - RHS.Position;
271 const StringRef root_path(StringRef path) {
272 const_iterator b = begin(path),
276 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
284 if (has_net || has_drive) {
285 if ((++pos != e) && is_separator((*pos)[0])) {
286 // {C:/,//net/}, so get the first two components.
287 return path.substr(0, b->size() + pos->size());
289 // just {C:,//net}, return the first component.
294 // POSIX style root directory.
295 if (is_separator((*b)[0])) {
303 const StringRef root_name(StringRef path) {
304 const_iterator b = begin(path),
307 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
315 if (has_net || has_drive) {
316 // just {C:,//net}, return the first component.
321 // No path or no name.
325 const StringRef root_directory(StringRef path) {
326 const_iterator b = begin(path),
330 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
338 if ((has_net || has_drive) &&
339 // {C:,//net}, skip to the next component.
340 (++pos != e) && is_separator((*pos)[0])) {
344 // POSIX style root directory.
345 if (!has_net && is_separator((*b)[0])) {
350 // No path or no root.
354 const StringRef relative_path(StringRef path) {
355 StringRef root = root_path(path);
356 return path.substr(root.size());
359 void append(SmallVectorImpl<char> &path, const Twine &a,
363 SmallString<32> a_storage;
364 SmallString<32> b_storage;
365 SmallString<32> c_storage;
366 SmallString<32> d_storage;
368 SmallVector<StringRef, 4> components;
369 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
370 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
371 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
372 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
374 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
375 e = components.end();
377 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
378 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
379 bool is_root_name = has_root_name(*i);
382 // Strip separators from beginning of component.
383 size_t loc = i->find_first_not_of(separators);
384 StringRef c = i->substr(loc);
387 path.append(c.begin(), c.end());
391 if (!component_has_sep && !(path.empty() || is_root_name)) {
393 path.push_back(prefered_separator);
396 path.append(i->begin(), i->end());
400 void append(SmallVectorImpl<char> &path,
401 const_iterator begin, const_iterator end) {
402 for (; begin != end; ++begin)
403 path::append(path, *begin);
406 const StringRef parent_path(StringRef path) {
407 size_t end_pos = parent_path_end(path);
408 if (end_pos == StringRef::npos)
411 return path.substr(0, end_pos);
414 void remove_filename(SmallVectorImpl<char> &path) {
415 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
416 if (end_pos != StringRef::npos)
417 path.set_size(end_pos);
420 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
421 StringRef p(path.begin(), path.size());
422 SmallString<32> ext_storage;
423 StringRef ext = extension.toStringRef(ext_storage);
425 // Erase existing extension.
426 size_t pos = p.find_last_of('.');
427 if (pos != StringRef::npos && pos >= filename_pos(p))
430 // Append '.' if needed.
431 if (ext.size() > 0 && ext[0] != '.')
435 path.append(ext.begin(), ext.end());
438 void native(const Twine &path, SmallVectorImpl<char> &result) {
442 SmallString<128> path_storage;
443 StringRef p = path.toStringRef(path_storage);
444 result.reserve(p.size());
445 for (StringRef::const_iterator i = p.begin(),
450 result.push_back('\\');
452 result.push_back(*i);
455 path.toVector(result);
459 const StringRef filename(StringRef path) {
460 return *(--end(path));
463 const StringRef stem(StringRef path) {
464 StringRef fname = filename(path);
465 size_t pos = fname.find_last_of('.');
466 if (pos == StringRef::npos)
469 if ((fname.size() == 1 && fname == ".") ||
470 (fname.size() == 2 && fname == ".."))
473 return fname.substr(0, pos);
476 const StringRef extension(StringRef path) {
477 StringRef fname = filename(path);
478 size_t pos = fname.find_last_of('.');
479 if (pos == StringRef::npos)
482 if ((fname.size() == 1 && fname == ".") ||
483 (fname.size() == 2 && fname == ".."))
486 return fname.substr(pos);
489 bool is_separator(char value) {
492 case '\\': // fall through
494 case '/': return true;
495 default: return false;
499 void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result) {
503 // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR.
504 int ConfName = erasedOnReboot? _CS_DARWIN_USER_TEMP_DIR
505 : _CS_DARWIN_USER_CACHE_DIR;
506 size_t ConfLen = confstr(ConfName, 0, 0);
509 result.resize(ConfLen);
510 ConfLen = confstr(ConfName, result.data(), result.size());
511 } while (ConfLen > 0 && ConfLen != result.size());
514 assert(result.back() == 0);
523 // Check whether the temporary directory is specified by an environment
525 const char *EnvironmentVariable;
527 EnvironmentVariable = "TEMP";
529 EnvironmentVariable = "TMPDIR";
531 if (char *RequestedDir = getenv(EnvironmentVariable)) {
532 result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
536 // Fall back to a system default.
537 const char *DefaultResult;
539 (void)erasedOnReboot;
540 DefaultResult = "C:\\TEMP";
543 DefaultResult = "/tmp";
545 DefaultResult = "/var/tmp";
547 result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
550 bool has_root_name(const Twine &path) {
551 SmallString<128> path_storage;
552 StringRef p = path.toStringRef(path_storage);
554 return !root_name(p).empty();
557 bool has_root_directory(const Twine &path) {
558 SmallString<128> path_storage;
559 StringRef p = path.toStringRef(path_storage);
561 return !root_directory(p).empty();
564 bool has_root_path(const Twine &path) {
565 SmallString<128> path_storage;
566 StringRef p = path.toStringRef(path_storage);
568 return !root_path(p).empty();
571 bool has_relative_path(const Twine &path) {
572 SmallString<128> path_storage;
573 StringRef p = path.toStringRef(path_storage);
575 return !relative_path(p).empty();
578 bool has_filename(const Twine &path) {
579 SmallString<128> path_storage;
580 StringRef p = path.toStringRef(path_storage);
582 return !filename(p).empty();
585 bool has_parent_path(const Twine &path) {
586 SmallString<128> path_storage;
587 StringRef p = path.toStringRef(path_storage);
589 return !parent_path(p).empty();
592 bool has_stem(const Twine &path) {
593 SmallString<128> path_storage;
594 StringRef p = path.toStringRef(path_storage);
596 return !stem(p).empty();
599 bool has_extension(const Twine &path) {
600 SmallString<128> path_storage;
601 StringRef p = path.toStringRef(path_storage);
603 return !extension(p).empty();
606 bool is_absolute(const Twine &path) {
607 SmallString<128> path_storage;
608 StringRef p = path.toStringRef(path_storage);
610 bool rootDir = has_root_directory(p),
612 rootName = has_root_name(p);
617 return rootDir && rootName;
620 bool is_relative(const Twine &path) {
621 return !is_absolute(path);
624 } // end namespace path
628 error_code unique_file(const Twine &Model, SmallVectorImpl<char> &ResultPath,
629 bool MakeAbsolute, unsigned Mode) {
630 // FIXME: This is really inefficient. unique_path creates a path an tries to
631 // open it. We should factor the code so that we just don't create/open the
632 // file when we don't need it.
634 error_code Ret = unique_file(Model, FD, ResultPath, MakeAbsolute, Mode);
639 return error_code(errno, system_category());
641 StringRef P(ResultPath.begin(), ResultPath.size());
642 return fs::remove(P);
645 error_code make_absolute(SmallVectorImpl<char> &path) {
646 StringRef p(path.data(), path.size());
648 bool rootDirectory = path::has_root_directory(p),
650 rootName = path::has_root_name(p);
656 if (rootName && rootDirectory)
657 return error_code::success();
659 // All of the following conditions will need the current directory.
660 SmallString<128> current_dir;
661 if (error_code ec = current_path(current_dir)) return ec;
663 // Relative path. Prepend the current directory.
664 if (!rootName && !rootDirectory) {
665 // Append path to the current directory.
666 path::append(current_dir, p);
667 // Set path to the result.
668 path.swap(current_dir);
669 return error_code::success();
672 if (!rootName && rootDirectory) {
673 StringRef cdrn = path::root_name(current_dir);
674 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
675 path::append(curDirRootName, p);
676 // Set path to the result.
677 path.swap(curDirRootName);
678 return error_code::success();
681 if (rootName && !rootDirectory) {
682 StringRef pRootName = path::root_name(p);
683 StringRef bRootDirectory = path::root_directory(current_dir);
684 StringRef bRelativePath = path::relative_path(current_dir);
685 StringRef pRelativePath = path::relative_path(p);
687 SmallString<128> res;
688 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
690 return error_code::success();
693 llvm_unreachable("All rootName and rootDirectory combinations should have "
697 error_code create_directories(const Twine &path, bool &existed) {
698 SmallString<128> path_storage;
699 StringRef p = path.toStringRef(path_storage);
701 StringRef parent = path::parent_path(p);
702 if (!parent.empty()) {
704 if (error_code ec = fs::exists(parent, parent_exists)) return ec;
707 if (error_code ec = create_directories(parent, existed)) return ec;
710 return create_directory(p, existed);
713 bool exists(file_status status) {
714 return status_known(status) && status.type() != file_type::file_not_found;
717 bool status_known(file_status s) {
718 return s.type() != file_type::status_error;
721 bool is_directory(file_status status) {
722 return status.type() == file_type::directory_file;
725 error_code is_directory(const Twine &path, bool &result) {
727 if (error_code ec = status(path, st))
729 result = is_directory(st);
730 return error_code::success();
733 bool is_regular_file(file_status status) {
734 return status.type() == file_type::regular_file;
737 error_code is_regular_file(const Twine &path, bool &result) {
739 if (error_code ec = status(path, st))
741 result = is_regular_file(st);
742 return error_code::success();
745 bool is_symlink(file_status status) {
746 return status.type() == file_type::symlink_file;
749 error_code is_symlink(const Twine &path, bool &result) {
751 if (error_code ec = status(path, st))
753 result = is_symlink(st);
754 return error_code::success();
757 bool is_other(file_status status) {
758 return exists(status) &&
759 !is_regular_file(status) &&
760 !is_directory(status) &&
764 void directory_entry::replace_filename(const Twine &filename, file_status st) {
765 SmallString<128> path(Path.begin(), Path.end());
766 path::remove_filename(path);
767 path::append(path, filename);
772 error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
773 SmallString<32> MagicStorage;
774 StringRef Magic = magic.toStringRef(MagicStorage);
775 SmallString<32> Buffer;
777 if (error_code ec = get_magic(path, Magic.size(), Buffer)) {
778 if (ec == errc::value_too_large) {
779 // Magic.size() > file_size(Path).
781 return error_code::success();
786 result = Magic == Buffer;
787 return error_code::success();
790 /// @brief Identify the magic in magic.
791 file_magic identify_magic(StringRef Magic) {
792 if (Magic.size() < 4)
793 return file_magic::unknown;
794 switch ((unsigned char)Magic[0]) {
795 case 0xDE: // 0x0B17C0DE = BC wraper
796 if (Magic[1] == (char)0xC0 && Magic[2] == (char)0x17 &&
797 Magic[3] == (char)0x0B)
798 return file_magic::bitcode;
801 if (Magic[1] == 'C' && Magic[2] == (char)0xC0 && Magic[3] == (char)0xDE)
802 return file_magic::bitcode;
805 if (Magic.size() >= 8)
806 if (memcmp(Magic.data(),"!<arch>\n",8) == 0)
807 return file_magic::archive;
811 if (Magic.size() >= 18 && Magic[1] == 'E' && Magic[2] == 'L' &&
813 bool Data2MSB = Magic[5] == 2;
814 unsigned high = Data2MSB ? 16 : 17;
815 unsigned low = Data2MSB ? 17 : 16;
816 if (Magic[high] == 0)
817 switch (Magic[low]) {
819 case 1: return file_magic::elf_relocatable;
820 case 2: return file_magic::elf_executable;
821 case 3: return file_magic::elf_shared_object;
822 case 4: return file_magic::elf_core;
828 if (Magic[1] == char(0xFE) && Magic[2] == char(0xBA) &&
829 Magic[3] == char(0xBE)) {
830 // This is complicated by an overlap with Java class files.
831 // See the Mach-O section in /usr/share/file/magic for details.
832 if (Magic.size() >= 8 && Magic[7] < 43)
833 return file_magic::macho_universal_binary;
837 // The two magic numbers for mach-o are:
838 // 0xfeedface - 32-bit mach-o
839 // 0xfeedfacf - 64-bit mach-o
844 if (Magic[0] == char(0xFE) && Magic[1] == char(0xED) &&
845 Magic[2] == char(0xFA) &&
846 (Magic[3] == char(0xCE) || Magic[3] == char(0xCF))) {
848 if (Magic.size() >= 16) type = Magic[14] << 8 | Magic[15];
849 } else if ((Magic[0] == char(0xCE) || Magic[0] == char(0xCF)) &&
850 Magic[1] == char(0xFA) && Magic[2] == char(0xED) &&
851 Magic[3] == char(0xFE)) {
853 if (Magic.size() >= 14) type = Magic[13] << 8 | Magic[12];
857 case 1: return file_magic::macho_object;
858 case 2: return file_magic::macho_executable;
859 case 3: return file_magic::macho_fixed_virtual_memory_shared_lib;
860 case 4: return file_magic::macho_core;
861 case 5: return file_magic::macho_preload_executable;
862 case 6: return file_magic::macho_dynamically_linked_shared_lib;
863 case 7: return file_magic::macho_dynamic_linker;
864 case 8: return file_magic::macho_bundle;
865 case 9: return file_magic::macho_dynamic_linker;
866 case 10: return file_magic::macho_dsym_companion;
870 case 0xF0: // PowerPC Windows
871 case 0x83: // Alpha 32-bit
872 case 0x84: // Alpha 64-bit
873 case 0x66: // MPS R4000 Windows
875 case 0x4c: // 80386 Windows
876 if (Magic[1] == 0x01)
877 return file_magic::coff_object;
879 case 0x90: // PA-RISC Windows
880 case 0x68: // mc68K Windows
881 if (Magic[1] == 0x02)
882 return file_magic::coff_object;
885 case 0x4d: // Possible MS-DOS stub on Windows PE file
886 if (Magic[1] == 0x5a) {
888 *reinterpret_cast<const support::ulittle32_t*>(Magic.data() + 0x3c);
889 // PE/COFF file, either EXE or DLL.
890 if (off < Magic.size() && memcmp(Magic.data() + off, "PE\0\0",4) == 0)
891 return file_magic::pecoff_executable;
895 case 0x64: // x86-64 Windows.
896 if (Magic[1] == char(0x86))
897 return file_magic::coff_object;
903 return file_magic::unknown;
906 error_code identify_magic(const Twine &path, file_magic &result) {
907 SmallString<32> Magic;
908 error_code ec = get_magic(path, Magic.capacity(), Magic);
909 if (ec && ec != errc::value_too_large)
912 result = identify_magic(Magic);
913 return error_code::success();
917 error_code remove_all_r(StringRef path, file_type ft, uint32_t &count) {
918 if (ft == file_type::directory_file) {
919 // This code would be a lot better with exceptions ;/.
921 directory_iterator i(path, ec);
923 for (directory_iterator e; i != e; i.increment(ec)) {
926 if (error_code ec = i->status(st)) return ec;
927 if (error_code ec = remove_all_r(i->path(), st.type(), count)) return ec;
929 bool obviously_this_exists;
930 if (error_code ec = remove(path, obviously_this_exists)) return ec;
931 assert(obviously_this_exists);
932 ++count; // Include the directory itself in the items removed.
934 bool obviously_this_exists;
935 if (error_code ec = remove(path, obviously_this_exists)) return ec;
936 assert(obviously_this_exists);
940 return error_code::success();
942 } // end unnamed namespace
944 error_code remove_all(const Twine &path, uint32_t &num_removed) {
945 SmallString<128> path_storage;
946 StringRef p = path.toStringRef(path_storage);
949 if (error_code ec = status(path, fs))
952 return remove_all_r(p, fs.type(), num_removed);
955 error_code directory_entry::status(file_status &result) const {
956 return fs::status(Path, result);
959 } // end namespace fs
960 } // end namespace sys
961 } // end namespace llvm
963 // Include the truly platform-specific parts.
964 #if defined(LLVM_ON_UNIX)
965 #include "Unix/PathV2.inc"
967 #if defined(LLVM_ON_WIN32)
968 #include "Windows/PathV2.inc"