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"
20 using llvm::StringRef;
22 bool is_separator(const char value) {
25 case '\\': // fall through
27 case '/': return true;
28 default: return false;
33 const StringRef separators = "\\/";
34 const char prefered_separator = '\\';
36 const StringRef separators = "/";
37 const char prefered_separator = '/';
40 const llvm::error_code success;
42 StringRef find_first_component(const StringRef &path) {
43 // Look for this first component in the following order.
44 // * empty (in this case we return an empty string)
45 // * either C: or {//,\\}net.
48 // * {file,directory}name
54 if (path.size() >= 2 && std::isalpha(path[0]) && path[1] == ':')
55 return StringRef(path.begin(), 2);
58 if ((path.size() > 2) &&
59 (path.startswith("\\\\") || path.startswith("//")) &&
60 (path[2] != '\\' && path[2] != '/')) {
61 // Find the next directory separator.
62 size_t end = path.find_first_of("\\/", 2);
63 if (end == StringRef::npos)
66 return StringRef(path.begin(), end);
70 if (path[0] == '\\' || path[0] == '/')
71 return StringRef(path.begin(), 1);
73 if (path.startswith(".."))
74 return StringRef(path.begin(), 2);
77 return StringRef(path.begin(), 1);
79 // * {file,directory}name
80 size_t end = path.find_first_of("\\/", 2);
81 if (end == StringRef::npos)
84 return StringRef(path.begin(), end);
89 size_t filename_pos(const StringRef &str) {
90 if (str.size() == 2 &&
91 is_separator(str[0]) &&
95 if (str.size() > 0 && is_separator(str[str.size() - 1]))
96 return str.size() - 1;
98 size_t pos = str.find_last_of(separators, str.size() - 1);
101 if (pos == StringRef::npos)
102 pos = str.find_last_of(':', str.size() - 2);
105 if (pos == StringRef::npos ||
106 (pos == 1 && is_separator(str[0])))
112 size_t root_dir_start(const StringRef &str) {
115 if (str.size() > 2 &&
117 is_separator(str[2]))
122 if (str.size() == 2 &&
123 is_separator(str[0]) &&
125 return StringRef::npos;
128 if (str.size() > 3 &&
129 is_separator(str[0]) &&
131 !is_separator(str[2])) {
132 return str.find_first_of(separators, 2);
136 if (str.size() > 0 && is_separator(str[0]))
139 return StringRef::npos;
142 size_t parent_path_end(const StringRef &path) {
143 size_t end_pos = filename_pos(path);
145 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
147 // Skip separators except for root dir.
148 size_t root_dir_pos = root_dir_start(StringRef(path.begin(), end_pos));
151 (end_pos - 1) != root_dir_pos &&
152 is_separator(path[end_pos - 1]))
155 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
156 return StringRef::npos;
166 const_iterator begin(const StringRef &path) {
169 i.Component = find_first_component(path);
174 const_iterator end(const StringRef &path) {
177 i.Position = path.size();
181 const_iterator &const_iterator::operator++() {
182 assert(Position < Path.size() && "Tried to increment past end!");
184 // Increment Position to past the current component
185 Position += Component.size();
188 if (Position == Path.size()) {
189 Component = StringRef();
193 // Both POSIX and Windows treat paths that begin with exactly two separators
195 bool was_net = Component.size() > 2 &&
196 is_separator(Component[0]) &&
197 Component[1] == Component[0] &&
198 !is_separator(Component[2]);
200 // Handle separators.
201 if (is_separator(Path[Position])) {
206 || Component.endswith(":")
209 Component = StringRef(Path.begin() + Position, 1);
213 // Skip extra separators.
214 while (Position != Path.size() &&
215 is_separator(Path[Position])) {
219 // Treat trailing '/' as a '.'.
220 if (Position == Path.size()) {
227 // Find next component.
228 size_t end_pos = Path.find_first_of(separators, Position);
229 if (end_pos == StringRef::npos)
230 end_pos = Path.size();
231 Component = StringRef(Path.begin() + Position, end_pos - Position);
236 const_iterator &const_iterator::operator--() {
237 // If we're at the end and the previous char was a '/', return '.'.
238 if (Position == Path.size() &&
240 is_separator(Path[Position - 1])
242 && Path[Position - 2] != ':'
250 // Skip separators unless it's the root directory.
251 size_t root_dir_pos = root_dir_start(Path);
252 size_t end_pos = Position;
255 (end_pos - 1) != root_dir_pos &&
256 is_separator(Path[end_pos - 1]))
259 // Find next separator.
260 size_t start_pos = filename_pos(StringRef(Path.begin(), end_pos));
261 Component = StringRef(Path.begin() + start_pos, end_pos - start_pos);
262 Position = start_pos;
266 bool const_iterator::operator==(const const_iterator &RHS) const {
267 return Path.begin() == RHS.Path.begin() &&
268 Position == RHS.Position;
271 bool const_iterator::operator!=(const const_iterator &RHS) const {
272 return !(*this == RHS);
275 ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
276 return Position - RHS.Position;
279 error_code root_path(const StringRef &path, StringRef &result) {
280 const_iterator b = begin(path),
284 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
292 if (has_net || has_drive) {
293 if ((++pos != e) && is_separator((*pos)[0])) {
294 // {C:/,//net/}, so get the first two components.
295 result = StringRef(path.begin(), b->size() + pos->size());
298 // just {C:,//net}, return the first component.
304 // POSIX style root directory.
305 if (is_separator((*b)[0])) {
311 result = StringRef();
316 result = StringRef();
320 error_code root_name(const StringRef &path, StringRef &result) {
321 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 // just {C:,//net}, return the first component.
339 // No path or no name.
340 result = StringRef();
344 error_code root_directory(const StringRef &path, StringRef &result) {
345 const_iterator b = begin(path),
349 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
357 if ((has_net || has_drive) &&
358 // {C:,//net}, skip to the next component.
359 (++pos != e) && is_separator((*pos)[0])) {
364 // POSIX style root directory.
365 if (!has_net && is_separator((*b)[0])) {
371 // No path or no root.
372 result = StringRef();
376 error_code relative_path(const StringRef &path, StringRef &result) {
378 if (error_code ec = root_path(path, root)) return ec;
379 result = StringRef(path.begin() + root.size(), path.size() - root.size());
383 error_code append(SmallVectorImpl<char> &path, const Twine &a,
387 SmallString<32> a_storage;
388 SmallString<32> b_storage;
389 SmallString<32> c_storage;
390 SmallString<32> d_storage;
392 SmallVector<StringRef, 4> components;
393 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
394 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
395 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
396 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
398 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
399 e = components.end();
401 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
402 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
403 bool is_root_name = false;
404 if (error_code ec = has_root_name(*i, is_root_name)) return ec;
407 // Strip separators from beginning of component.
408 size_t loc = i->find_first_not_of(separators);
409 StringRef c = StringRef(i->begin() + loc, i->size() - loc);
412 path.append(c.begin(), c.end());
416 if (!component_has_sep && !(path.empty() || is_root_name)) {
418 path.push_back(prefered_separator);
421 path.append(i->begin(), i->end());
427 error_code parent_path(const StringRef &path, StringRef &result) {
428 size_t end_pos = parent_path_end(path);
429 if (end_pos == StringRef::npos)
430 result = StringRef();
432 result = StringRef(path.data(), end_pos);
436 error_code remove_filename(SmallVectorImpl<char> &path) {
437 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
438 if (end_pos == StringRef::npos)
440 path.set_size(end_pos);
444 error_code replace_extension(SmallVectorImpl<char> &path,
445 const Twine &extension) {
446 StringRef p(path.begin(), path.size());
447 SmallString<32> ext_storage;
448 StringRef ext = extension.toStringRef(ext_storage);
450 // Erase existing extension.
451 size_t pos = p.find_last_of('.');
452 if (pos != StringRef::npos && pos >= filename_pos(p))
455 // Append '.' if needed.
456 if (ext.size() > 0 && ext[0] != '.')
460 path.append(ext.begin(), ext.end());
464 error_code native(const Twine &path, SmallVectorImpl<char> &result) {
468 SmallString<128> path_storage;
469 StringRef p = path.toStringRef(path_storage);
470 result.reserve(p.size());
471 for (StringRef::const_iterator i = p.begin(),
476 result.push_back('\\');
478 result.push_back(*i);
481 path.toVector(result);
486 error_code filename(const StringRef &path, StringRef &result) {
487 result = *(--end(path));
491 error_code stem(const StringRef &path, StringRef &result) {
493 if (error_code ec = filename(path, fname)) return ec;
494 size_t pos = fname.find_last_of('.');
495 if (pos == StringRef::npos)
498 if ((fname.size() == 1 && fname == ".") ||
499 (fname.size() == 2 && fname == ".."))
502 result = StringRef(fname.begin(), pos);
507 error_code extension(const StringRef &path, StringRef &result) {
509 if (error_code ec = filename(path, fname)) return ec;
510 size_t pos = fname.find_last_of('.');
511 if (pos == StringRef::npos)
512 result = StringRef();
514 if ((fname.size() == 1 && fname == ".") ||
515 (fname.size() == 2 && fname == ".."))
516 result = StringRef();
518 result = StringRef(fname.begin() + pos, fname.size() - pos);
523 error_code has_root_name(const Twine &path, bool &result) {
524 SmallString<128> path_storage;
525 StringRef p = path.toStringRef(path_storage);
527 if (error_code ec = root_name(p, p)) return ec;
533 error_code has_root_directory(const Twine &path, bool &result) {
534 SmallString<128> path_storage;
535 StringRef p = path.toStringRef(path_storage);
537 if (error_code ec = root_directory(p, p)) return ec;
543 error_code has_root_path(const Twine &path, bool &result) {
544 SmallString<128> path_storage;
545 StringRef p = path.toStringRef(path_storage);
547 if (error_code ec = root_path(p, p)) return ec;
553 error_code has_filename(const Twine &path, bool &result) {
554 SmallString<128> path_storage;
555 StringRef p = path.toStringRef(path_storage);
557 if (error_code ec = filename(p, p)) return ec;
563 error_code has_parent_path(const Twine &path, bool &result) {
564 SmallString<128> path_storage;
565 StringRef p = path.toStringRef(path_storage);
567 if (error_code ec = parent_path(p, p)) return ec;
573 error_code has_stem(const Twine &path, bool &result) {
574 SmallString<128> path_storage;
575 StringRef p = path.toStringRef(path_storage);
577 if (error_code ec = stem(p, p)) return ec;
583 error_code has_extension(const Twine &path, bool &result) {
584 SmallString<128> path_storage;
585 StringRef p = path.toStringRef(path_storage);
587 if (error_code ec = extension(p, p)) return ec;
593 error_code is_absolute(const Twine &path, bool &result) {
594 SmallString<128> path_storage;
595 StringRef p = path.toStringRef(path_storage);
597 bool rootDir = false,
599 if (error_code ec = has_root_directory(p, rootDir)) return ec;
601 if (error_code ec = has_root_name(p, rootName)) return ec;
606 result = rootDir && rootName;
610 error_code is_relative(const Twine &path, bool &result) {
612 error_code ec = is_absolute(path, res);
617 } // end namespace path
621 error_code make_absolute(SmallVectorImpl<char> &path) {
622 StringRef p(path.data(), path.size());
624 bool rootName = false, rootDirectory = false;
625 if (error_code ec = path::has_root_name(p, rootName)) return ec;
626 if (error_code ec = path::has_root_directory(p, rootDirectory)) return ec;
629 if (rootName && rootDirectory)
632 // All of the following conditions will need the current directory.
633 SmallString<128> current_dir;
634 if (error_code ec = current_path(current_dir)) return ec;
636 // Relative path. Prepend the current directory.
637 if (!rootName && !rootDirectory) {
638 // Append path to the current directory.
639 if (error_code ec = path::append(current_dir, p)) return ec;
640 // Set path to the result.
641 path.swap(current_dir);
645 if (!rootName && rootDirectory) {
647 if (error_code ec = path::root_name(current_dir, cdrn)) return ec;
648 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
649 if (error_code ec = path::append(curDirRootName, p)) return ec;
650 // Set path to the result.
651 path.swap(curDirRootName);
655 if (rootName && !rootDirectory) {
657 StringRef bRootDirectory;
658 StringRef bRelativePath;
659 StringRef pRelativePath;
660 if (error_code ec = path::root_name(p, pRootName)) return ec;
661 if (error_code ec = path::root_directory(current_dir, bRootDirectory))
663 if (error_code ec = path::relative_path(current_dir, bRelativePath))
665 if (error_code ec = path::relative_path(p, pRelativePath)) return ec;
667 SmallString<128> res;
668 if (error_code ec = path::append(res, pRootName, bRootDirectory,
669 bRelativePath, pRelativePath)) return ec;
674 llvm_unreachable("All rootName and rootDirectory combinations should have "
678 error_code create_directories(const Twine &path, bool &existed) {
679 SmallString<128> path_storage;
680 StringRef p = path.toStringRef(path_storage);
684 if (error_code ec = path::parent_path(p, parent)) return ec;
685 if (error_code ec = fs::exists(parent, parent_exists)) return ec;
688 return create_directories(parent, existed);
690 return create_directory(p, existed);
693 void directory_entry::replace_filename(const Twine &filename, file_status st,
694 file_status symlink_st) {
695 SmallString<128> path(Path.begin(), Path.end());
696 path::remove_filename(path);
697 path::append(path, filename);
700 SymlinkStatus = symlink_st;
703 } // end namespace fs
704 } // end namespace sys
705 } // end namespace llvm
707 // Include the truly platform-specific parts.
708 #if defined(LLVM_ON_UNIX)
709 #include "Unix/PathV2.inc"
711 #if defined(LLVM_ON_WIN32)
712 #include "Windows/PathV2.inc"