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/FileSystem.h"
16 #include "llvm/Support/ErrorHandling.h"
22 using llvm::StringRef;
23 using llvm::sys::path::is_separator;
26 const StringRef separators = "\\/";
27 const char prefered_separator = '\\';
29 const StringRef separators = "/";
30 const char prefered_separator = '/';
33 const llvm::error_code success;
35 StringRef find_first_component(StringRef path) {
36 // Look for this first component in the following order.
37 // * empty (in this case we return an empty string)
38 // * either C: or {//,\\}net.
41 // * {file,directory}name
48 if (path.size() >= 2 && std::isalpha(path[0]) && path[1] == ':')
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 root.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 const StringRef parent_path(StringRef path) {
395 size_t end_pos = parent_path_end(path);
396 if (end_pos == StringRef::npos)
399 return path.substr(0, end_pos);
402 void remove_filename(SmallVectorImpl<char> &path) {
403 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
404 if (end_pos != StringRef::npos)
405 path.set_size(end_pos);
408 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
409 StringRef p(path.begin(), path.size());
410 SmallString<32> ext_storage;
411 StringRef ext = extension.toStringRef(ext_storage);
413 // Erase existing extension.
414 size_t pos = p.find_last_of('.');
415 if (pos != StringRef::npos && pos >= filename_pos(p))
418 // Append '.' if needed.
419 if (ext.size() > 0 && ext[0] != '.')
423 path.append(ext.begin(), ext.end());
426 void native(const Twine &path, SmallVectorImpl<char> &result) {
430 SmallString<128> path_storage;
431 StringRef p = path.toStringRef(path_storage);
432 result.reserve(p.size());
433 for (StringRef::const_iterator i = p.begin(),
438 result.push_back('\\');
440 result.push_back(*i);
443 path.toVector(result);
447 const StringRef filename(StringRef path) {
448 return *(--end(path));
451 const StringRef stem(StringRef path) {
452 StringRef fname = filename(path);
453 size_t pos = fname.find_last_of('.');
454 if (pos == StringRef::npos)
457 if ((fname.size() == 1 && fname == ".") ||
458 (fname.size() == 2 && fname == ".."))
461 return fname.substr(0, pos);
464 const StringRef extension(StringRef path) {
465 StringRef fname = filename(path);
466 size_t pos = fname.find_last_of('.');
467 if (pos == StringRef::npos)
470 if ((fname.size() == 1 && fname == ".") ||
471 (fname.size() == 2 && fname == ".."))
474 return fname.substr(pos);
477 bool is_separator(char value) {
480 case '\\': // fall through
482 case '/': return true;
483 default: return false;
487 bool has_root_name(const Twine &path) {
488 SmallString<128> path_storage;
489 StringRef p = path.toStringRef(path_storage);
491 return !root_name(p).empty();
494 bool has_root_directory(const Twine &path) {
495 SmallString<128> path_storage;
496 StringRef p = path.toStringRef(path_storage);
498 return !root_directory(p).empty();
501 bool has_root_path(const Twine &path) {
502 SmallString<128> path_storage;
503 StringRef p = path.toStringRef(path_storage);
505 return !root_path(p).empty();
508 bool has_relative_path(const Twine &path) {
509 SmallString<128> path_storage;
510 StringRef p = path.toStringRef(path_storage);
512 return !relative_path(p).empty();
515 bool has_filename(const Twine &path) {
516 SmallString<128> path_storage;
517 StringRef p = path.toStringRef(path_storage);
519 return !filename(p).empty();
522 bool has_parent_path(const Twine &path) {
523 SmallString<128> path_storage;
524 StringRef p = path.toStringRef(path_storage);
526 return !parent_path(p).empty();
529 bool has_stem(const Twine &path) {
530 SmallString<128> path_storage;
531 StringRef p = path.toStringRef(path_storage);
533 return !stem(p).empty();
536 bool has_extension(const Twine &path) {
537 SmallString<128> path_storage;
538 StringRef p = path.toStringRef(path_storage);
540 return !extension(p).empty();
543 bool is_absolute(const Twine &path) {
544 SmallString<128> path_storage;
545 StringRef p = path.toStringRef(path_storage);
547 bool rootDir = has_root_directory(p),
549 rootName = has_root_name(p);
554 return rootDir && rootName;
557 bool is_relative(const Twine &path) {
558 return !is_absolute(path);
561 } // end namespace path
565 error_code make_absolute(SmallVectorImpl<char> &path) {
566 StringRef p(path.data(), path.size());
568 bool rootName = path::has_root_name(p),
569 rootDirectory = path::has_root_directory(p);
572 if (rootName && rootDirectory)
575 // All of the following conditions will need the current directory.
576 SmallString<128> current_dir;
577 if (error_code ec = current_path(current_dir)) return ec;
579 // Relative path. Prepend the current directory.
580 if (!rootName && !rootDirectory) {
581 // Append path to the current directory.
582 path::append(current_dir, p);
583 // Set path to the result.
584 path.swap(current_dir);
588 if (!rootName && rootDirectory) {
589 StringRef cdrn = path::root_name(current_dir);
590 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
591 path::append(curDirRootName, p);
592 // Set path to the result.
593 path.swap(curDirRootName);
597 if (rootName && !rootDirectory) {
598 StringRef pRootName = path::root_name(p);
599 StringRef bRootDirectory = path::root_directory(current_dir);
600 StringRef bRelativePath = path::relative_path(current_dir);
601 StringRef pRelativePath = path::relative_path(p);
603 SmallString<128> res;
604 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
609 llvm_unreachable("All rootName and rootDirectory combinations should have "
613 error_code create_directories(const Twine &path, bool &existed) {
614 SmallString<128> path_storage;
615 StringRef p = path.toStringRef(path_storage);
617 StringRef parent = path::parent_path(p);
620 if (error_code ec = fs::exists(parent, parent_exists)) return ec;
623 return create_directories(parent, existed);
625 return create_directory(p, existed);
628 bool exists(file_status status) {
629 return status_known(status) && status.type() != file_type::file_not_found;
632 bool status_known(file_status s) {
633 return s.type() != file_type::status_error;
636 bool is_directory(file_status status) {
637 return status.type() == file_type::directory_file;
640 error_code is_directory(const Twine &path, bool &result) {
642 if (error_code ec = status(path, st))
644 result = is_directory(st);
648 bool is_regular_file(file_status status) {
649 return status.type() == file_type::regular_file;
652 error_code is_regular_file(const Twine &path, bool &result) {
654 if (error_code ec = status(path, st))
656 result = is_regular_file(st);
660 bool is_symlink(file_status status) {
661 return status.type() == file_type::symlink_file;
664 error_code is_symlink(const Twine &path, bool &result) {
666 if (error_code ec = status(path, st))
668 result = is_symlink(st);
672 bool is_other(file_status status) {
673 return exists(status) &&
674 !is_regular_file(status) &&
675 !is_directory(status) &&
679 void directory_entry::replace_filename(const Twine &filename, file_status st,
680 file_status symlink_st) {
681 SmallString<128> path(Path.begin(), Path.end());
682 path::remove_filename(path);
683 path::append(path, filename);
686 SymlinkStatus = symlink_st;
689 error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
690 SmallString<32> MagicStorage;
691 StringRef Magic = magic.toStringRef(MagicStorage);
692 SmallString<32> Buffer;
694 if (error_code ec = get_magic(path, Magic.size(), Buffer)) {
695 if (ec == errc::value_too_large) {
696 // Magic.size() > file_size(Path).
703 result = Magic == Buffer;
707 error_code identify_magic(const Twine &path, LLVMFileType &result) {
708 SmallString<32> Magic;
709 error_code ec = get_magic(path, Magic.capacity(), Magic);
710 if (ec && ec != errc::value_too_large)
713 result = IdentifyFileType(Magic.data(), Magic.size());
718 error_code remove_all_r(StringRef path, file_type ft, uint32_t &count) {
719 if (ft == file_type::directory_file) {
720 // This code would be a lot better with exceptions ;/.
722 for (directory_iterator i(path, ec), e; i != e; i.increment(ec)) {
725 if (error_code ec = i->status(st)) return ec;
726 if (error_code ec = remove_all_r(i->path(), st.type(), count)) return ec;
728 bool obviously_this_exists;
729 if (error_code ec = remove(path, obviously_this_exists)) return ec;
730 assert(obviously_this_exists);
731 ++count; // Include the directory itself in the items removed.
733 bool obviously_this_exists;
734 if (error_code ec = remove(path, obviously_this_exists)) return ec;
735 assert(obviously_this_exists);
741 } // end unnamed namespace
743 error_code remove_all(const Twine &path, uint32_t &num_removed) {
744 SmallString<128> path_storage;
745 StringRef p = path.toStringRef(path_storage);
748 if (error_code ec = status(path, fs))
751 return remove_all_r(p, fs.type(), num_removed);
754 error_code directory_entry::status(file_status &result) const {
755 return fs::status(Path, result);
758 } // end namespace fs
759 } // end namespace sys
760 } // end namespace llvm
762 // Include the truly platform-specific parts.
763 #if defined(LLVM_ON_UNIX)
764 #include "Unix/PathV2.inc"
766 #if defined(LLVM_ON_WIN32)
767 #include "Windows/PathV2.inc"