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 make_absolute(SmallVectorImpl<char> &path) {
428 StringRef p(path.data(), path.size());
430 bool rootName = false, rootDirectory = false;
431 if (error_code ec = has_root_name(p, rootName)) return ec;
432 if (error_code ec = has_root_directory(p, rootDirectory)) return ec;
435 if (rootName && rootDirectory)
438 // All of the following conditions will need the current directory.
439 SmallString<128> current_dir;
440 if (error_code ec = fs::current_path(current_dir)) return ec;
442 // Relative path. Prepend the current directory.
443 if (!rootName && !rootDirectory) {
444 // Append path to the current directory.
445 if (error_code ec = append(current_dir, p)) return ec;
446 // Set path to the result.
447 path.swap(current_dir);
451 if (!rootName && rootDirectory) {
453 if (error_code ec = root_name(current_dir, cdrn)) return ec;
454 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
455 if (error_code ec = append(curDirRootName, p)) return ec;
456 // Set path to the result.
457 path.swap(curDirRootName);
461 if (rootName && !rootDirectory) {
463 StringRef bRootDirectory;
464 StringRef bRelativePath;
465 StringRef pRelativePath;
466 if (error_code ec = root_name(p, pRootName)) return ec;
467 if (error_code ec = root_directory(current_dir, bRootDirectory)) return ec;
468 if (error_code ec = relative_path(current_dir, bRelativePath)) return ec;
469 if (error_code ec = relative_path(p, pRelativePath)) return ec;
471 SmallString<128> res;
472 if (error_code ec = append(res, pRootName, bRootDirectory,
473 bRelativePath, pRelativePath)) return ec;
478 llvm_unreachable("All rootName and rootDirectory combinations should have "
482 error_code parent_path(const StringRef &path, StringRef &result) {
483 size_t end_pos = parent_path_end(path);
484 if (end_pos == StringRef::npos)
485 result = StringRef();
487 result = StringRef(path.data(), end_pos);
491 error_code remove_filename(SmallVectorImpl<char> &path) {
492 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
493 if (end_pos == StringRef::npos)
495 path.set_size(end_pos);
499 error_code replace_extension(SmallVectorImpl<char> &path,
500 const Twine &extension) {
501 StringRef p(path.begin(), path.size());
502 SmallString<32> ext_storage;
503 StringRef ext = extension.toStringRef(ext_storage);
505 // Erase existing extension.
506 size_t pos = p.find_last_of('.');
507 if (pos != StringRef::npos && pos >= filename_pos(p))
510 // Append '.' if needed.
511 if (ext.size() > 0 && ext[0] != '.')
515 path.append(ext.begin(), ext.end());
519 error_code native(const Twine &path, SmallVectorImpl<char> &result) {
523 SmallString<128> path_storage;
524 StringRef p = path.toStringRef(path_storage);
525 result.reserve(p.size());
526 for (StringRef::const_iterator i = p.begin(),
531 result.push_back('\\');
533 result.push_back(*i);
536 path.toVector(result);
541 error_code filename(const StringRef &path, StringRef &result) {
542 result = *(--end(path));
546 error_code stem(const StringRef &path, StringRef &result) {
548 if (error_code ec = filename(path, fname)) return ec;
549 size_t pos = fname.find_last_of('.');
550 if (pos == StringRef::npos)
553 if ((fname.size() == 1 && fname == ".") ||
554 (fname.size() == 2 && fname == ".."))
557 result = StringRef(fname.begin(), pos);
562 error_code extension(const StringRef &path, StringRef &result) {
564 if (error_code ec = filename(path, fname)) return ec;
565 size_t pos = fname.find_last_of('.');
566 if (pos == StringRef::npos)
567 result = StringRef();
569 if ((fname.size() == 1 && fname == ".") ||
570 (fname.size() == 2 && fname == ".."))
571 result = StringRef();
573 result = StringRef(fname.begin() + pos, fname.size() - pos);
578 error_code has_root_name(const Twine &path, bool &result) {
579 SmallString<128> path_storage;
580 StringRef p = path.toStringRef(path_storage);
582 if (error_code ec = root_name(p, p)) return ec;
588 error_code has_root_directory(const Twine &path, bool &result) {
589 SmallString<128> path_storage;
590 StringRef p = path.toStringRef(path_storage);
592 if (error_code ec = root_directory(p, p)) return ec;
598 error_code has_root_path(const Twine &path, bool &result) {
599 SmallString<128> path_storage;
600 StringRef p = path.toStringRef(path_storage);
602 if (error_code ec = root_path(p, p)) return ec;
608 error_code has_filename(const Twine &path, bool &result) {
609 SmallString<128> path_storage;
610 StringRef p = path.toStringRef(path_storage);
612 if (error_code ec = filename(p, p)) return ec;
618 error_code has_parent_path(const Twine &path, bool &result) {
619 SmallString<128> path_storage;
620 StringRef p = path.toStringRef(path_storage);
622 if (error_code ec = parent_path(p, p)) return ec;
628 error_code has_stem(const Twine &path, bool &result) {
629 SmallString<128> path_storage;
630 StringRef p = path.toStringRef(path_storage);
632 if (error_code ec = stem(p, p)) return ec;
638 error_code has_extension(const Twine &path, bool &result) {
639 SmallString<128> path_storage;
640 StringRef p = path.toStringRef(path_storage);
642 if (error_code ec = extension(p, p)) return ec;
648 error_code is_absolute(const Twine &path, bool &result) {
649 SmallString<128> path_storage;
650 StringRef p = path.toStringRef(path_storage);
652 bool rootDir = false,
654 if (error_code ec = has_root_directory(p, rootDir)) return ec;
656 if (error_code ec = has_root_name(p, rootName)) return ec;
661 result = rootDir && rootName;
665 error_code is_relative(const Twine &path, bool &result) {
667 error_code ec = is_absolute(path, res);
672 } // end namespace path
676 error_code create_directories(const Twine &path, bool &existed) {
677 SmallString<128> path_storage;
678 StringRef p = path.toStringRef(path_storage);
682 if (error_code ec = path::parent_path(p, parent)) return ec;
683 if (error_code ec = fs::exists(parent, parent_exists)) return ec;
686 return create_directories(parent, existed);
688 return create_directory(p, existed);
691 void directory_entry::replace_filename(const Twine &filename, file_status st,
692 file_status symlink_st) {
693 SmallString<128> path(Path.begin(), Path.end());
694 path::remove_filename(path);
695 path::append(path, filename);
698 SymlinkStatus = symlink_st;
701 } // end namespace fs
702 } // end namespace sys
703 } // end namespace llvm
705 // Include the truly platform-specific parts.
706 #if defined(LLVM_ON_UNIX)
707 #include "Unix/PathV2.inc"
709 #if defined(LLVM_ON_WIN32)
710 #include "Windows/PathV2.inc"