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"
23 using llvm::StringRef;
24 using llvm::sys::path::is_separator;
27 const char *separators = "\\/";
28 const char prefered_separator = '\\';
30 const char separators = '/';
31 const char prefered_separator = '/';
34 StringRef find_first_component(StringRef path) {
35 // Look for this first component in the following order.
36 // * empty (in this case we return an empty string)
37 // * either C: or {//,\\}net.
40 // * {file,directory}name
47 if (path.size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) &&
49 return path.substr(0, 2);
53 if ((path.size() > 2) &&
54 is_separator(path[0]) &&
56 !is_separator(path[2])) {
57 // Find the next directory separator.
58 size_t end = path.find_first_of(separators, 2);
59 return path.substr(0, end);
63 if (is_separator(path[0]))
64 return path.substr(0, 1);
66 if (path.startswith(".."))
67 return path.substr(0, 2);
70 return path.substr(0, 1);
72 // * {file,directory}name
73 size_t end = path.find_first_of(separators, 2);
74 return path.substr(0, end);
77 size_t filename_pos(StringRef str) {
78 if (str.size() == 2 &&
79 is_separator(str[0]) &&
83 if (str.size() > 0 && is_separator(str[str.size() - 1]))
84 return str.size() - 1;
86 size_t pos = str.find_last_of(separators, str.size() - 1);
89 if (pos == StringRef::npos)
90 pos = str.find_last_of(':', str.size() - 2);
93 if (pos == StringRef::npos ||
94 (pos == 1 && is_separator(str[0])))
100 size_t root_dir_start(StringRef str) {
103 if (str.size() > 2 &&
105 is_separator(str[2]))
110 if (str.size() == 2 &&
111 is_separator(str[0]) &&
113 return StringRef::npos;
116 if (str.size() > 3 &&
117 is_separator(str[0]) &&
119 !is_separator(str[2])) {
120 return str.find_first_of(separators, 2);
124 if (str.size() > 0 && is_separator(str[0]))
127 return StringRef::npos;
130 size_t parent_path_end(StringRef path) {
131 size_t end_pos = filename_pos(path);
133 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
135 // Skip separators except for root dir.
136 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
139 (end_pos - 1) != root_dir_pos &&
140 is_separator(path[end_pos - 1]))
143 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
144 return StringRef::npos;
148 } // end unnamed namespace
154 const_iterator begin(StringRef path) {
157 i.Component = find_first_component(path);
162 const_iterator end(StringRef path) {
165 i.Position = path.size();
169 const_iterator &const_iterator::operator++() {
170 assert(Position < Path.size() && "Tried to increment past end!");
172 // Increment Position to past the current component
173 Position += Component.size();
176 if (Position == Path.size()) {
177 Component = StringRef();
181 // Both POSIX and Windows treat paths that begin with exactly two separators
183 bool was_net = Component.size() > 2 &&
184 is_separator(Component[0]) &&
185 Component[1] == Component[0] &&
186 !is_separator(Component[2]);
188 // Handle separators.
189 if (is_separator(Path[Position])) {
194 || Component.endswith(":")
197 Component = Path.substr(Position, 1);
201 // Skip extra separators.
202 while (Position != Path.size() &&
203 is_separator(Path[Position])) {
207 // Treat trailing '/' as a '.'.
208 if (Position == Path.size()) {
215 // Find next component.
216 size_t end_pos = Path.find_first_of(separators, Position);
217 Component = Path.slice(Position, end_pos);
222 const_iterator &const_iterator::operator--() {
223 // If we're at the end and the previous char was a '/', return '.'.
224 if (Position == Path.size() &&
226 is_separator(Path[Position - 1])
228 && Path[Position - 2] != ':'
236 // Skip separators unless it's the root directory.
237 size_t root_dir_pos = root_dir_start(Path);
238 size_t end_pos = Position;
241 (end_pos - 1) != root_dir_pos &&
242 is_separator(Path[end_pos - 1]))
245 // Find next separator.
246 size_t start_pos = filename_pos(Path.substr(0, end_pos));
247 Component = Path.slice(start_pos, end_pos);
248 Position = start_pos;
252 bool const_iterator::operator==(const const_iterator &RHS) const {
253 return Path.begin() == RHS.Path.begin() &&
254 Position == RHS.Position;
257 bool const_iterator::operator!=(const const_iterator &RHS) const {
258 return !(*this == RHS);
261 ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
262 return Position - RHS.Position;
265 const StringRef root_path(StringRef path) {
266 const_iterator b = begin(path),
270 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
278 if (has_net || has_drive) {
279 if ((++pos != e) && is_separator((*pos)[0])) {
280 // {C:/,//net/}, so get the first two components.
281 return path.substr(0, b->size() + pos->size());
283 // just {C:,//net}, return the first component.
288 // POSIX style root directory.
289 if (is_separator((*b)[0])) {
297 const StringRef root_name(StringRef path) {
298 const_iterator b = begin(path),
301 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
309 if (has_net || has_drive) {
310 // just {C:,//net}, return the first component.
315 // No path or no name.
319 const StringRef root_directory(StringRef path) {
320 const_iterator b = begin(path),
324 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
332 if ((has_net || has_drive) &&
333 // {C:,//net}, skip to the next component.
334 (++pos != e) && is_separator((*pos)[0])) {
338 // POSIX style root directory.
339 if (!has_net && is_separator((*b)[0])) {
344 // No path or no root.
348 const StringRef relative_path(StringRef path) {
349 StringRef root = root_path(path);
350 return path.substr(root.size());
353 void append(SmallVectorImpl<char> &path, const Twine &a,
357 SmallString<32> a_storage;
358 SmallString<32> b_storage;
359 SmallString<32> c_storage;
360 SmallString<32> d_storage;
362 SmallVector<StringRef, 4> components;
363 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
364 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
365 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
366 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
368 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
369 e = components.end();
371 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
372 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
373 bool is_root_name = has_root_name(*i);
376 // Strip separators from beginning of component.
377 size_t loc = i->find_first_not_of(separators);
378 StringRef c = i->substr(loc);
381 path.append(c.begin(), c.end());
385 if (!component_has_sep && !(path.empty() || is_root_name)) {
387 path.push_back(prefered_separator);
390 path.append(i->begin(), i->end());
394 void append(SmallVectorImpl<char> &path,
395 const_iterator begin, const_iterator end) {
396 for (; begin != end; ++begin)
397 path::append(path, *begin);
400 const StringRef parent_path(StringRef path) {
401 size_t end_pos = parent_path_end(path);
402 if (end_pos == StringRef::npos)
405 return path.substr(0, end_pos);
408 void remove_filename(SmallVectorImpl<char> &path) {
409 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
410 if (end_pos != StringRef::npos)
411 path.set_size(end_pos);
414 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
415 StringRef p(path.begin(), path.size());
416 SmallString<32> ext_storage;
417 StringRef ext = extension.toStringRef(ext_storage);
419 // Erase existing extension.
420 size_t pos = p.find_last_of('.');
421 if (pos != StringRef::npos && pos >= filename_pos(p))
424 // Append '.' if needed.
425 if (ext.size() > 0 && ext[0] != '.')
429 path.append(ext.begin(), ext.end());
432 void native(const Twine &path, SmallVectorImpl<char> &result) {
436 SmallString<128> path_storage;
437 StringRef p = path.toStringRef(path_storage);
438 result.reserve(p.size());
439 for (StringRef::const_iterator i = p.begin(),
444 result.push_back('\\');
446 result.push_back(*i);
449 path.toVector(result);
453 const StringRef filename(StringRef path) {
454 return *(--end(path));
457 const StringRef stem(StringRef path) {
458 StringRef fname = filename(path);
459 size_t pos = fname.find_last_of('.');
460 if (pos == StringRef::npos)
463 if ((fname.size() == 1 && fname == ".") ||
464 (fname.size() == 2 && fname == ".."))
467 return fname.substr(0, pos);
470 const StringRef extension(StringRef path) {
471 StringRef fname = filename(path);
472 size_t pos = fname.find_last_of('.');
473 if (pos == StringRef::npos)
476 if ((fname.size() == 1 && fname == ".") ||
477 (fname.size() == 2 && fname == ".."))
480 return fname.substr(pos);
483 bool is_separator(char value) {
486 case '\\': // fall through
488 case '/': return true;
489 default: return false;
493 void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result) {
496 // Check whether the temporary directory is specified by an environment
498 const char *EnvironmentVariable;
500 EnvironmentVariable = "TEMP";
502 EnvironmentVariable = "TMPDIR";
504 if (char *RequestedDir = getenv(EnvironmentVariable)) {
505 result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
509 // Fall back to a system default.
510 const char *DefaultResult;
512 (void)erasedOnReboot;
513 DefaultResult = "C:\\TEMP";
516 DefaultResult = "/tmp";
518 DefaultResult = "/var/tmp";
520 result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
523 bool has_root_name(const Twine &path) {
524 SmallString<128> path_storage;
525 StringRef p = path.toStringRef(path_storage);
527 return !root_name(p).empty();
530 bool has_root_directory(const Twine &path) {
531 SmallString<128> path_storage;
532 StringRef p = path.toStringRef(path_storage);
534 return !root_directory(p).empty();
537 bool has_root_path(const Twine &path) {
538 SmallString<128> path_storage;
539 StringRef p = path.toStringRef(path_storage);
541 return !root_path(p).empty();
544 bool has_relative_path(const Twine &path) {
545 SmallString<128> path_storage;
546 StringRef p = path.toStringRef(path_storage);
548 return !relative_path(p).empty();
551 bool has_filename(const Twine &path) {
552 SmallString<128> path_storage;
553 StringRef p = path.toStringRef(path_storage);
555 return !filename(p).empty();
558 bool has_parent_path(const Twine &path) {
559 SmallString<128> path_storage;
560 StringRef p = path.toStringRef(path_storage);
562 return !parent_path(p).empty();
565 bool has_stem(const Twine &path) {
566 SmallString<128> path_storage;
567 StringRef p = path.toStringRef(path_storage);
569 return !stem(p).empty();
572 bool has_extension(const Twine &path) {
573 SmallString<128> path_storage;
574 StringRef p = path.toStringRef(path_storage);
576 return !extension(p).empty();
579 bool is_absolute(const Twine &path) {
580 SmallString<128> path_storage;
581 StringRef p = path.toStringRef(path_storage);
583 bool rootDir = has_root_directory(p),
585 rootName = has_root_name(p);
590 return rootDir && rootName;
593 bool is_relative(const Twine &path) {
594 return !is_absolute(path);
597 } // end namespace path
601 error_code make_absolute(SmallVectorImpl<char> &path) {
602 StringRef p(path.data(), path.size());
604 bool rootDirectory = path::has_root_directory(p),
606 rootName = path::has_root_name(p);
612 if (rootName && rootDirectory)
613 return error_code::success();
615 // All of the following conditions will need the current directory.
616 SmallString<128> current_dir;
617 if (error_code ec = current_path(current_dir)) return ec;
619 // Relative path. Prepend the current directory.
620 if (!rootName && !rootDirectory) {
621 // Append path to the current directory.
622 path::append(current_dir, p);
623 // Set path to the result.
624 path.swap(current_dir);
625 return error_code::success();
628 if (!rootName && rootDirectory) {
629 StringRef cdrn = path::root_name(current_dir);
630 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
631 path::append(curDirRootName, p);
632 // Set path to the result.
633 path.swap(curDirRootName);
634 return error_code::success();
637 if (rootName && !rootDirectory) {
638 StringRef pRootName = path::root_name(p);
639 StringRef bRootDirectory = path::root_directory(current_dir);
640 StringRef bRelativePath = path::relative_path(current_dir);
641 StringRef pRelativePath = path::relative_path(p);
643 SmallString<128> res;
644 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
646 return error_code::success();
649 llvm_unreachable("All rootName and rootDirectory combinations should have "
653 error_code create_directories(const Twine &path, bool &existed) {
654 SmallString<128> path_storage;
655 StringRef p = path.toStringRef(path_storage);
657 StringRef parent = path::parent_path(p);
658 if (!parent.empty()) {
660 if (error_code ec = fs::exists(parent, parent_exists)) return ec;
663 if (error_code ec = create_directories(parent, existed)) return ec;
666 return create_directory(p, existed);
669 bool exists(file_status status) {
670 return status_known(status) && status.type() != file_type::file_not_found;
673 bool status_known(file_status s) {
674 return s.type() != file_type::status_error;
677 bool is_directory(file_status status) {
678 return status.type() == file_type::directory_file;
681 error_code is_directory(const Twine &path, bool &result) {
683 if (error_code ec = status(path, st))
685 result = is_directory(st);
686 return error_code::success();
689 bool is_regular_file(file_status status) {
690 return status.type() == file_type::regular_file;
693 error_code is_regular_file(const Twine &path, bool &result) {
695 if (error_code ec = status(path, st))
697 result = is_regular_file(st);
698 return error_code::success();
701 bool is_symlink(file_status status) {
702 return status.type() == file_type::symlink_file;
705 error_code is_symlink(const Twine &path, bool &result) {
707 if (error_code ec = status(path, st))
709 result = is_symlink(st);
710 return error_code::success();
713 bool is_other(file_status status) {
714 return exists(status) &&
715 !is_regular_file(status) &&
716 !is_directory(status) &&
720 void directory_entry::replace_filename(const Twine &filename, file_status st) {
721 SmallString<128> path(Path.begin(), Path.end());
722 path::remove_filename(path);
723 path::append(path, filename);
728 error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
729 SmallString<32> MagicStorage;
730 StringRef Magic = magic.toStringRef(MagicStorage);
731 SmallString<32> Buffer;
733 if (error_code ec = get_magic(path, Magic.size(), Buffer)) {
734 if (ec == errc::value_too_large) {
735 // Magic.size() > file_size(Path).
737 return error_code::success();
742 result = Magic == Buffer;
743 return error_code::success();
746 /// @brief Identify the magic in magic.
747 file_magic identify_magic(StringRef magic) {
748 if (magic.size() < 4)
749 return file_magic::unknown;
750 switch ((unsigned char)magic[0]) {
751 case 0xDE: // 0x0B17C0DE = BC wraper
752 if (magic[1] == (char)0xC0 && magic[2] == (char)0x17 &&
753 magic[3] == (char)0x0B)
754 return file_magic::bitcode;
757 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
758 return file_magic::bitcode;
761 if (magic.size() >= 8)
762 if (memcmp(magic.data(),"!<arch>\n",8) == 0)
763 return file_magic::archive;
767 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
768 if (magic.size() >= 18 && magic[17] == 0)
771 case 1: return file_magic::elf_relocatable;
772 case 2: return file_magic::elf_executable;
773 case 3: return file_magic::elf_shared_object;
774 case 4: return file_magic::elf_core;
780 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
781 magic[3] == char(0xBE)) {
782 // This is complicated by an overlap with Java class files.
783 // See the Mach-O section in /usr/share/file/magic for details.
784 if (magic.size() >= 8 && magic[7] < 43)
785 // FIXME: Universal Binary of any type.
786 return file_magic::macho_dynamically_linked_shared_lib;
790 // The two magic numbers for mach-o are:
791 // 0xfeedface - 32-bit mach-o
792 // 0xfeedfacf - 64-bit mach-o
797 if (magic[0] == char(0xFE) && magic[1] == char(0xED) &&
798 magic[2] == char(0xFA) &&
799 (magic[3] == char(0xCE) || magic[3] == char(0xCF))) {
801 if (magic.size() >= 16) type = magic[14] << 8 | magic[15];
802 } else if ((magic[0] == char(0xCE) || magic[0] == char(0xCF)) &&
803 magic[1] == char(0xFA) && magic[2] == char(0xED) &&
804 magic[3] == char(0xFE)) {
806 if (magic.size() >= 14) type = magic[13] << 8 | magic[12];
810 case 1: return file_magic::macho_object;
811 case 2: return file_magic::macho_executable;
812 case 3: return file_magic::macho_fixed_virtual_memory_shared_lib;
813 case 4: return file_magic::macho_core;
814 case 5: return file_magic::macho_preload_executabl;
815 case 6: return file_magic::macho_dynamically_linked_shared_lib;
816 case 7: return file_magic::macho_dynamic_linker;
817 case 8: return file_magic::macho_bundle;
818 case 9: return file_magic::macho_dynamic_linker;
819 case 10: return file_magic::macho_dsym_companion;
823 case 0xF0: // PowerPC Windows
824 case 0x83: // Alpha 32-bit
825 case 0x84: // Alpha 64-bit
826 case 0x66: // MPS R4000 Windows
828 case 0x4c: // 80386 Windows
829 if (magic[1] == 0x01)
830 return file_magic::coff_object;
832 case 0x90: // PA-RISC Windows
833 case 0x68: // mc68K Windows
834 if (magic[1] == 0x02)
835 return file_magic::coff_object;
838 case 0x4d: // Possible MS-DOS stub on Windows PE file
839 if (magic[1] == 0x5a) {
841 *reinterpret_cast<const support::ulittle32_t*>(magic.data() + 0x3c);
842 // PE/COFF file, either EXE or DLL.
843 if (off < magic.size() && memcmp(magic.data() + off, "PE\0\0",4) == 0)
844 return file_magic::pecoff_executable;
848 case 0x64: // x86-64 Windows.
849 if (magic[1] == char(0x86))
850 return file_magic::coff_object;
856 return file_magic::unknown;
859 error_code identify_magic(const Twine &path, file_magic &result) {
860 SmallString<32> Magic;
861 error_code ec = get_magic(path, Magic.capacity(), Magic);
862 if (ec && ec != errc::value_too_large)
865 result = identify_magic(Magic);
866 return error_code::success();
870 error_code remove_all_r(StringRef path, file_type ft, uint32_t &count) {
871 if (ft == file_type::directory_file) {
872 // This code would be a lot better with exceptions ;/.
874 directory_iterator i(path, ec);
876 for (directory_iterator e; i != e; i.increment(ec)) {
879 if (error_code ec = i->status(st)) return ec;
880 if (error_code ec = remove_all_r(i->path(), st.type(), count)) return ec;
882 bool obviously_this_exists;
883 if (error_code ec = remove(path, obviously_this_exists)) return ec;
884 assert(obviously_this_exists);
885 ++count; // Include the directory itself in the items removed.
887 bool obviously_this_exists;
888 if (error_code ec = remove(path, obviously_this_exists)) return ec;
889 assert(obviously_this_exists);
893 return error_code::success();
895 } // end unnamed namespace
897 error_code remove_all(const Twine &path, uint32_t &num_removed) {
898 SmallString<128> path_storage;
899 StringRef p = path.toStringRef(path_storage);
902 if (error_code ec = status(path, fs))
905 return remove_all_r(p, fs.type(), num_removed);
908 error_code directory_entry::status(file_status &result) const {
909 return fs::status(Path, result);
912 } // end namespace fs
913 } // end namespace sys
914 } // end namespace llvm
916 // Include the truly platform-specific parts.
917 #if defined(LLVM_ON_UNIX)
918 #include "Unix/PathV2.inc"
920 #if defined(LLVM_ON_WIN32)
921 #include "Windows/PathV2.inc"