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"
23 #if !defined(_MSC_VER) && !defined(__MINGW32__)
30 using llvm::StringRef;
31 using llvm::sys::path::is_separator;
34 const char *separators = "\\/";
35 const char prefered_separator = '\\';
37 const char separators = '/';
38 const char prefered_separator = '/';
41 StringRef find_first_component(StringRef path) {
42 // Look for this first component in the following order.
43 // * empty (in this case we return an empty string)
44 // * either C: or {//,\\}net.
47 // * {file,directory}name
54 if (path.size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) &&
56 return path.substr(0, 2);
60 if ((path.size() > 2) &&
61 is_separator(path[0]) &&
63 !is_separator(path[2])) {
64 // Find the next directory separator.
65 size_t end = path.find_first_of(separators, 2);
66 return path.substr(0, end);
70 if (is_separator(path[0]))
71 return path.substr(0, 1);
73 if (path.startswith(".."))
74 return path.substr(0, 2);
77 return path.substr(0, 1);
79 // * {file,directory}name
80 size_t end = path.find_first_of(separators, 2);
81 return path.substr(0, end);
84 size_t filename_pos(StringRef str) {
85 if (str.size() == 2 &&
86 is_separator(str[0]) &&
90 if (str.size() > 0 && is_separator(str[str.size() - 1]))
91 return str.size() - 1;
93 size_t pos = str.find_last_of(separators, str.size() - 1);
96 if (pos == StringRef::npos)
97 pos = str.find_last_of(':', str.size() - 2);
100 if (pos == StringRef::npos ||
101 (pos == 1 && is_separator(str[0])))
107 size_t root_dir_start(StringRef str) {
110 if (str.size() > 2 &&
112 is_separator(str[2]))
117 if (str.size() == 2 &&
118 is_separator(str[0]) &&
120 return StringRef::npos;
123 if (str.size() > 3 &&
124 is_separator(str[0]) &&
126 !is_separator(str[2])) {
127 return str.find_first_of(separators, 2);
131 if (str.size() > 0 && is_separator(str[0]))
134 return StringRef::npos;
137 size_t parent_path_end(StringRef path) {
138 size_t end_pos = filename_pos(path);
140 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
142 // Skip separators except for root dir.
143 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
146 (end_pos - 1) != root_dir_pos &&
147 is_separator(path[end_pos - 1]))
150 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
151 return StringRef::npos;
155 } // end unnamed namespace
163 // Implemented in Unix/Path.inc and Windows/Path.inc.
164 static llvm::error_code
165 createUniqueEntity(const llvm::Twine &Model, int &ResultFD,
166 llvm::SmallVectorImpl<char> &ResultPath,
167 bool MakeAbsolute, unsigned Mode, FSEntity Type);
173 const_iterator begin(StringRef path) {
176 i.Component = find_first_component(path);
181 const_iterator end(StringRef path) {
184 i.Position = path.size();
188 const_iterator &const_iterator::operator++() {
189 assert(Position < Path.size() && "Tried to increment past end!");
191 // Increment Position to past the current component
192 Position += Component.size();
195 if (Position == Path.size()) {
196 Component = StringRef();
200 // Both POSIX and Windows treat paths that begin with exactly two separators
202 bool was_net = Component.size() > 2 &&
203 is_separator(Component[0]) &&
204 Component[1] == Component[0] &&
205 !is_separator(Component[2]);
207 // Handle separators.
208 if (is_separator(Path[Position])) {
213 || Component.endswith(":")
216 Component = Path.substr(Position, 1);
220 // Skip extra separators.
221 while (Position != Path.size() &&
222 is_separator(Path[Position])) {
226 // Treat trailing '/' as a '.'.
227 if (Position == Path.size()) {
234 // Find next component.
235 size_t end_pos = Path.find_first_of(separators, Position);
236 Component = Path.slice(Position, end_pos);
241 const_iterator &const_iterator::operator--() {
242 // If we're at the end and the previous char was a '/', return '.'.
243 if (Position == Path.size() &&
245 is_separator(Path[Position - 1])
247 && Path[Position - 2] != ':'
255 // Skip separators unless it's the root directory.
256 size_t root_dir_pos = root_dir_start(Path);
257 size_t end_pos = Position;
260 (end_pos - 1) != root_dir_pos &&
261 is_separator(Path[end_pos - 1]))
264 // Find next separator.
265 size_t start_pos = filename_pos(Path.substr(0, end_pos));
266 Component = Path.slice(start_pos, end_pos);
267 Position = start_pos;
271 bool const_iterator::operator==(const const_iterator &RHS) const {
272 return Path.begin() == RHS.Path.begin() &&
273 Position == RHS.Position;
276 bool const_iterator::operator!=(const const_iterator &RHS) const {
277 return !(*this == RHS);
280 ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
281 return Position - RHS.Position;
284 const StringRef root_path(StringRef path) {
285 const_iterator b = begin(path),
289 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
297 if (has_net || has_drive) {
298 if ((++pos != e) && is_separator((*pos)[0])) {
299 // {C:/,//net/}, so get the first two components.
300 return path.substr(0, b->size() + pos->size());
302 // just {C:,//net}, return the first component.
307 // POSIX style root directory.
308 if (is_separator((*b)[0])) {
316 const StringRef root_name(StringRef path) {
317 const_iterator b = begin(path),
320 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
328 if (has_net || has_drive) {
329 // just {C:,//net}, return the first component.
334 // No path or no name.
338 const StringRef root_directory(StringRef path) {
339 const_iterator b = begin(path),
343 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
351 if ((has_net || has_drive) &&
352 // {C:,//net}, skip to the next component.
353 (++pos != e) && is_separator((*pos)[0])) {
357 // POSIX style root directory.
358 if (!has_net && is_separator((*b)[0])) {
363 // No path or no root.
367 const StringRef relative_path(StringRef path) {
368 StringRef root = root_path(path);
369 return path.substr(root.size());
372 void append(SmallVectorImpl<char> &path, const Twine &a,
376 SmallString<32> a_storage;
377 SmallString<32> b_storage;
378 SmallString<32> c_storage;
379 SmallString<32> d_storage;
381 SmallVector<StringRef, 4> components;
382 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
383 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
384 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
385 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
387 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
388 e = components.end();
390 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
391 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
392 bool is_root_name = has_root_name(*i);
395 // Strip separators from beginning of component.
396 size_t loc = i->find_first_not_of(separators);
397 StringRef c = i->substr(loc);
400 path.append(c.begin(), c.end());
404 if (!component_has_sep && !(path.empty() || is_root_name)) {
406 path.push_back(prefered_separator);
409 path.append(i->begin(), i->end());
413 void append(SmallVectorImpl<char> &path,
414 const_iterator begin, const_iterator end) {
415 for (; begin != end; ++begin)
416 path::append(path, *begin);
419 const StringRef parent_path(StringRef path) {
420 size_t end_pos = parent_path_end(path);
421 if (end_pos == StringRef::npos)
424 return path.substr(0, end_pos);
427 void remove_filename(SmallVectorImpl<char> &path) {
428 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
429 if (end_pos != StringRef::npos)
430 path.set_size(end_pos);
433 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
434 StringRef p(path.begin(), path.size());
435 SmallString<32> ext_storage;
436 StringRef ext = extension.toStringRef(ext_storage);
438 // Erase existing extension.
439 size_t pos = p.find_last_of('.');
440 if (pos != StringRef::npos && pos >= filename_pos(p))
443 // Append '.' if needed.
444 if (ext.size() > 0 && ext[0] != '.')
448 path.append(ext.begin(), ext.end());
451 void native(const Twine &path, SmallVectorImpl<char> &result) {
455 SmallString<128> path_storage;
456 StringRef p = path.toStringRef(path_storage);
457 result.reserve(p.size());
458 for (StringRef::const_iterator i = p.begin(),
463 result.push_back('\\');
465 result.push_back(*i);
468 path.toVector(result);
472 const StringRef filename(StringRef path) {
473 return *(--end(path));
476 const StringRef stem(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(0, pos);
489 const StringRef extension(StringRef path) {
490 StringRef fname = filename(path);
491 size_t pos = fname.find_last_of('.');
492 if (pos == StringRef::npos)
495 if ((fname.size() == 1 && fname == ".") ||
496 (fname.size() == 2 && fname == ".."))
499 return fname.substr(pos);
502 bool is_separator(char value) {
505 case '\\': // fall through
507 case '/': return true;
508 default: return false;
512 void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result) {
516 // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR.
517 int ConfName = erasedOnReboot? _CS_DARWIN_USER_TEMP_DIR
518 : _CS_DARWIN_USER_CACHE_DIR;
519 size_t ConfLen = confstr(ConfName, 0, 0);
522 result.resize(ConfLen);
523 ConfLen = confstr(ConfName, result.data(), result.size());
524 } while (ConfLen > 0 && ConfLen != result.size());
527 assert(result.back() == 0);
536 // Check whether the temporary directory is specified by an environment
538 const char *EnvironmentVariable;
540 EnvironmentVariable = "TEMP";
542 EnvironmentVariable = "TMPDIR";
544 if (char *RequestedDir = getenv(EnvironmentVariable)) {
545 result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
549 // Fall back to a system default.
550 const char *DefaultResult;
552 (void)erasedOnReboot;
553 DefaultResult = "C:\\TEMP";
556 DefaultResult = "/tmp";
558 DefaultResult = "/var/tmp";
560 result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
563 bool has_root_name(const Twine &path) {
564 SmallString<128> path_storage;
565 StringRef p = path.toStringRef(path_storage);
567 return !root_name(p).empty();
570 bool has_root_directory(const Twine &path) {
571 SmallString<128> path_storage;
572 StringRef p = path.toStringRef(path_storage);
574 return !root_directory(p).empty();
577 bool has_root_path(const Twine &path) {
578 SmallString<128> path_storage;
579 StringRef p = path.toStringRef(path_storage);
581 return !root_path(p).empty();
584 bool has_relative_path(const Twine &path) {
585 SmallString<128> path_storage;
586 StringRef p = path.toStringRef(path_storage);
588 return !relative_path(p).empty();
591 bool has_filename(const Twine &path) {
592 SmallString<128> path_storage;
593 StringRef p = path.toStringRef(path_storage);
595 return !filename(p).empty();
598 bool has_parent_path(const Twine &path) {
599 SmallString<128> path_storage;
600 StringRef p = path.toStringRef(path_storage);
602 return !parent_path(p).empty();
605 bool has_stem(const Twine &path) {
606 SmallString<128> path_storage;
607 StringRef p = path.toStringRef(path_storage);
609 return !stem(p).empty();
612 bool has_extension(const Twine &path) {
613 SmallString<128> path_storage;
614 StringRef p = path.toStringRef(path_storage);
616 return !extension(p).empty();
619 bool is_absolute(const Twine &path) {
620 SmallString<128> path_storage;
621 StringRef p = path.toStringRef(path_storage);
623 bool rootDir = has_root_directory(p),
625 rootName = has_root_name(p);
630 return rootDir && rootName;
633 bool is_relative(const Twine &path) {
634 return !is_absolute(path);
637 } // end namespace path
641 error_code createUniqueFile(const Twine &Model, int &ResultFd,
642 SmallVectorImpl<char> &ResultPath, unsigned Mode) {
643 return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
646 error_code createUniqueFile(const Twine &Model,
647 SmallVectorImpl<char> &ResultPath) {
649 return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
652 static error_code createTemporaryFile(const Twine &Model, int &ResultFD,
653 llvm::SmallVectorImpl<char> &ResultPath,
655 SmallString<128> Storage;
656 StringRef P = Model.toNullTerminatedStringRef(Storage);
657 assert(P.find_first_of(separators) == StringRef::npos &&
658 "Model must be a simple filename.");
659 // Use P.begin() so that createUniqueEntity doesn't need to recreate Storage.
660 return createUniqueEntity(P.begin(), ResultFD, ResultPath,
661 true, owner_read | owner_write, Type);
665 createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
666 llvm::SmallVectorImpl<char> &ResultPath,
668 return createTemporaryFile(Prefix + "-%%%%%%." + Suffix, ResultFD, ResultPath,
673 error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
675 SmallVectorImpl<char> &ResultPath) {
676 return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File);
679 error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
680 SmallVectorImpl<char> &ResultPath) {
682 return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
686 // This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
687 // for consistency. We should try using mkdtemp.
688 error_code createUniqueDirectory(const Twine &Prefix,
689 SmallVectorImpl<char> &ResultPath) {
691 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath,
695 error_code openFileForWrite(const Twine &Name, int &ResultFD,
696 sys::fs::OpenFlags Flags, unsigned Mode) {
697 // Verify that we don't have both "append" and "excl".
698 assert((!(Flags & sys::fs::F_Excl) || !(Flags & sys::fs::F_Append)) &&
699 "Cannot specify both 'excl' and 'append' file creation flags!");
701 int OpenFlags = O_WRONLY | O_CREAT;
704 if (Flags & F_Binary)
705 OpenFlags |= O_BINARY;
708 if (Flags & F_Append)
709 OpenFlags |= O_APPEND;
711 OpenFlags |= O_TRUNC;
716 SmallString<128> Storage;
717 StringRef P = Name.toNullTerminatedStringRef(Storage);
718 while ((ResultFD = open(P.begin(), OpenFlags, Mode)) < 0) {
720 return error_code(errno, system_category());
722 return error_code::success();
725 error_code openFileForRead(const Twine &Name, int &ResultFD) {
726 int OpenFlags = O_RDONLY;
728 OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
731 SmallString<128> Storage;
732 StringRef P = Name.toNullTerminatedStringRef(Storage);
733 while ((ResultFD = open(P.begin(), OpenFlags)) < 0) {
735 return error_code(errno, system_category());
737 return error_code::success();
740 error_code make_absolute(SmallVectorImpl<char> &path) {
741 StringRef p(path.data(), path.size());
743 bool rootDirectory = path::has_root_directory(p),
745 rootName = path::has_root_name(p);
751 if (rootName && rootDirectory)
752 return error_code::success();
754 // All of the following conditions will need the current directory.
755 SmallString<128> current_dir;
756 if (error_code ec = current_path(current_dir)) return ec;
758 // Relative path. Prepend the current directory.
759 if (!rootName && !rootDirectory) {
760 // Append path to the current directory.
761 path::append(current_dir, p);
762 // Set path to the result.
763 path.swap(current_dir);
764 return error_code::success();
767 if (!rootName && rootDirectory) {
768 StringRef cdrn = path::root_name(current_dir);
769 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
770 path::append(curDirRootName, p);
771 // Set path to the result.
772 path.swap(curDirRootName);
773 return error_code::success();
776 if (rootName && !rootDirectory) {
777 StringRef pRootName = path::root_name(p);
778 StringRef bRootDirectory = path::root_directory(current_dir);
779 StringRef bRelativePath = path::relative_path(current_dir);
780 StringRef pRelativePath = path::relative_path(p);
782 SmallString<128> res;
783 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
785 return error_code::success();
788 llvm_unreachable("All rootName and rootDirectory combinations should have "
792 error_code create_directories(const Twine &path, bool &existed) {
793 SmallString<128> path_storage;
794 StringRef p = path.toStringRef(path_storage);
796 StringRef parent = path::parent_path(p);
797 if (!parent.empty()) {
799 if (error_code ec = fs::exists(parent, parent_exists)) return ec;
802 if (error_code ec = create_directories(parent, existed)) return ec;
805 return create_directory(p, existed);
808 bool exists(file_status status) {
809 return status_known(status) && status.type() != file_type::file_not_found;
812 bool status_known(file_status s) {
813 return s.type() != file_type::status_error;
816 bool is_directory(file_status status) {
817 return status.type() == file_type::directory_file;
820 error_code is_directory(const Twine &path, bool &result) {
822 if (error_code ec = status(path, st))
824 result = is_directory(st);
825 return error_code::success();
828 bool is_regular_file(file_status status) {
829 return status.type() == file_type::regular_file;
832 error_code is_regular_file(const Twine &path, bool &result) {
834 if (error_code ec = status(path, st))
836 result = is_regular_file(st);
837 return error_code::success();
840 bool is_symlink(file_status status) {
841 return status.type() == file_type::symlink_file;
844 error_code is_symlink(const Twine &path, bool &result) {
846 if (error_code ec = status(path, st))
848 result = is_symlink(st);
849 return error_code::success();
852 bool is_other(file_status status) {
853 return exists(status) &&
854 !is_regular_file(status) &&
855 !is_directory(status) &&
859 void directory_entry::replace_filename(const Twine &filename, file_status st) {
860 SmallString<128> path(Path.begin(), Path.end());
861 path::remove_filename(path);
862 path::append(path, filename);
867 error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
868 SmallString<32> MagicStorage;
869 StringRef Magic = magic.toStringRef(MagicStorage);
870 SmallString<32> Buffer;
872 if (error_code ec = get_magic(path, Magic.size(), Buffer)) {
873 if (ec == errc::value_too_large) {
874 // Magic.size() > file_size(Path).
876 return error_code::success();
881 result = Magic == Buffer;
882 return error_code::success();
885 /// @brief Identify the magic in magic.
886 file_magic identify_magic(StringRef Magic) {
887 if (Magic.size() < 4)
888 return file_magic::unknown;
889 switch ((unsigned char)Magic[0]) {
890 case 0xDE: // 0x0B17C0DE = BC wraper
891 if (Magic[1] == (char)0xC0 && Magic[2] == (char)0x17 &&
892 Magic[3] == (char)0x0B)
893 return file_magic::bitcode;
896 if (Magic[1] == 'C' && Magic[2] == (char)0xC0 && Magic[3] == (char)0xDE)
897 return file_magic::bitcode;
900 if (Magic.size() >= 8)
901 if (memcmp(Magic.data(),"!<arch>\n",8) == 0)
902 return file_magic::archive;
906 if (Magic.size() >= 18 && Magic[1] == 'E' && Magic[2] == 'L' &&
908 bool Data2MSB = Magic[5] == 2;
909 unsigned high = Data2MSB ? 16 : 17;
910 unsigned low = Data2MSB ? 17 : 16;
911 if (Magic[high] == 0)
912 switch (Magic[low]) {
914 case 1: return file_magic::elf_relocatable;
915 case 2: return file_magic::elf_executable;
916 case 3: return file_magic::elf_shared_object;
917 case 4: return file_magic::elf_core;
923 if (Magic[1] == char(0xFE) && Magic[2] == char(0xBA) &&
924 Magic[3] == char(0xBE)) {
925 // This is complicated by an overlap with Java class files.
926 // See the Mach-O section in /usr/share/file/magic for details.
927 if (Magic.size() >= 8 && Magic[7] < 43)
928 return file_magic::macho_universal_binary;
932 // The two magic numbers for mach-o are:
933 // 0xfeedface - 32-bit mach-o
934 // 0xfeedfacf - 64-bit mach-o
939 if (Magic[0] == char(0xFE) && Magic[1] == char(0xED) &&
940 Magic[2] == char(0xFA) &&
941 (Magic[3] == char(0xCE) || Magic[3] == char(0xCF))) {
943 if (Magic.size() >= 16) type = Magic[14] << 8 | Magic[15];
944 } else if ((Magic[0] == char(0xCE) || Magic[0] == char(0xCF)) &&
945 Magic[1] == char(0xFA) && Magic[2] == char(0xED) &&
946 Magic[3] == char(0xFE)) {
948 if (Magic.size() >= 14) type = Magic[13] << 8 | Magic[12];
952 case 1: return file_magic::macho_object;
953 case 2: return file_magic::macho_executable;
954 case 3: return file_magic::macho_fixed_virtual_memory_shared_lib;
955 case 4: return file_magic::macho_core;
956 case 5: return file_magic::macho_preload_executable;
957 case 6: return file_magic::macho_dynamically_linked_shared_lib;
958 case 7: return file_magic::macho_dynamic_linker;
959 case 8: return file_magic::macho_bundle;
960 case 9: return file_magic::macho_dynamic_linker;
961 case 10: return file_magic::macho_dsym_companion;
965 case 0xF0: // PowerPC Windows
966 case 0x83: // Alpha 32-bit
967 case 0x84: // Alpha 64-bit
968 case 0x66: // MPS R4000 Windows
970 case 0x4c: // 80386 Windows
971 if (Magic[1] == 0x01)
972 return file_magic::coff_object;
974 case 0x90: // PA-RISC Windows
975 case 0x68: // mc68K Windows
976 if (Magic[1] == 0x02)
977 return file_magic::coff_object;
980 case 0x4d: // Possible MS-DOS stub on Windows PE file
981 if (Magic[1] == 0x5a) {
983 *reinterpret_cast<const support::ulittle32_t*>(Magic.data() + 0x3c);
984 // PE/COFF file, either EXE or DLL.
985 if (off < Magic.size() && memcmp(Magic.data() + off, "PE\0\0",4) == 0)
986 return file_magic::pecoff_executable;
990 case 0x64: // x86-64 Windows.
991 if (Magic[1] == char(0x86))
992 return file_magic::coff_object;
998 return file_magic::unknown;
1001 error_code identify_magic(const Twine &path, file_magic &result) {
1002 SmallString<32> Magic;
1003 error_code ec = get_magic(path, Magic.capacity(), Magic);
1004 if (ec && ec != errc::value_too_large)
1007 result = identify_magic(Magic);
1008 return error_code::success();
1012 error_code remove_all_r(StringRef path, file_type ft, uint32_t &count) {
1013 if (ft == file_type::directory_file) {
1014 // This code would be a lot better with exceptions ;/.
1016 directory_iterator i(path, ec);
1018 for (directory_iterator e; i != e; i.increment(ec)) {
1021 if (error_code ec = i->status(st)) return ec;
1022 if (error_code ec = remove_all_r(i->path(), st.type(), count)) return ec;
1024 bool obviously_this_exists;
1025 if (error_code ec = remove(path, obviously_this_exists)) return ec;
1026 assert(obviously_this_exists);
1027 ++count; // Include the directory itself in the items removed.
1029 bool obviously_this_exists;
1030 if (error_code ec = remove(path, obviously_this_exists)) return ec;
1031 assert(obviously_this_exists);
1035 return error_code::success();
1037 } // end unnamed namespace
1039 error_code remove_all(const Twine &path, uint32_t &num_removed) {
1040 SmallString<128> path_storage;
1041 StringRef p = path.toStringRef(path_storage);
1044 if (error_code ec = status(path, fs))
1047 return remove_all_r(p, fs.type(), num_removed);
1050 error_code directory_entry::status(file_status &result) const {
1051 return fs::status(Path, result);
1054 } // end namespace fs
1055 } // end namespace sys
1056 } // end namespace llvm
1058 // Include the truly platform-specific parts.
1059 #if defined(LLVM_ON_UNIX)
1060 #include "Unix/Path.inc"
1062 #if defined(LLVM_ON_WIN32)
1063 #include "Windows/Path.inc"