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(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
55 if (path.size() >= 2 && std::isalpha(path[0]) && path[1] == ':')
56 return StringRef(path.begin(), 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 if (end == StringRef::npos)
69 return StringRef(path.begin(), end);
73 if (is_separator(path[0]))
74 return StringRef(path.begin(), 1);
76 if (path.startswith(".."))
77 return StringRef(path.begin(), 2);
80 return StringRef(path.begin(), 1);
82 // * {file,directory}name
83 size_t end = path.find_first_of(separators, 2);
84 if (end == StringRef::npos)
87 return StringRef(path.begin(), end);
92 size_t filename_pos(StringRef str) {
93 if (str.size() == 2 &&
94 is_separator(str[0]) &&
98 if (str.size() > 0 && is_separator(str[str.size() - 1]))
99 return str.size() - 1;
101 size_t pos = str.find_last_of(separators, str.size() - 1);
104 if (pos == StringRef::npos)
105 pos = str.find_last_of(':', str.size() - 2);
108 if (pos == StringRef::npos ||
109 (pos == 1 && is_separator(str[0])))
115 size_t root_dir_start(StringRef str) {
118 if (str.size() > 2 &&
120 is_separator(str[2]))
125 if (str.size() == 2 &&
126 is_separator(str[0]) &&
128 return StringRef::npos;
131 if (str.size() > 3 &&
132 is_separator(str[0]) &&
134 !is_separator(str[2])) {
135 return str.find_first_of(separators, 2);
139 if (str.size() > 0 && is_separator(str[0]))
142 return StringRef::npos;
145 size_t parent_path_end(StringRef path) {
146 size_t end_pos = filename_pos(path);
148 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
150 // Skip separators except for root dir.
151 size_t root_dir_pos = root_dir_start(StringRef(path.begin(), end_pos));
154 (end_pos - 1) != root_dir_pos &&
155 is_separator(path[end_pos - 1]))
158 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
159 return StringRef::npos;
169 const_iterator begin(StringRef path) {
172 i.Component = find_first_component(path);
177 const_iterator end(StringRef path) {
180 i.Position = path.size();
184 const_iterator &const_iterator::operator++() {
185 assert(Position < Path.size() && "Tried to increment past end!");
187 // Increment Position to past the current component
188 Position += Component.size();
191 if (Position == Path.size()) {
192 Component = StringRef();
196 // Both POSIX and Windows treat paths that begin with exactly two separators
198 bool was_net = Component.size() > 2 &&
199 is_separator(Component[0]) &&
200 Component[1] == Component[0] &&
201 !is_separator(Component[2]);
203 // Handle separators.
204 if (is_separator(Path[Position])) {
209 || Component.endswith(":")
212 Component = StringRef(Path.begin() + Position, 1);
216 // Skip extra separators.
217 while (Position != Path.size() &&
218 is_separator(Path[Position])) {
222 // Treat trailing '/' as a '.'.
223 if (Position == Path.size()) {
230 // Find next component.
231 size_t end_pos = Path.find_first_of(separators, Position);
232 if (end_pos == StringRef::npos)
233 end_pos = Path.size();
234 Component = StringRef(Path.begin() + Position, end_pos - Position);
239 const_iterator &const_iterator::operator--() {
240 // If we're at the end and the previous char was a '/', return '.'.
241 if (Position == Path.size() &&
243 is_separator(Path[Position - 1])
245 && Path[Position - 2] != ':'
253 // Skip separators unless it's the root directory.
254 size_t root_dir_pos = root_dir_start(Path);
255 size_t end_pos = Position;
258 (end_pos - 1) != root_dir_pos &&
259 is_separator(Path[end_pos - 1]))
262 // Find next separator.
263 size_t start_pos = filename_pos(StringRef(Path.begin(), end_pos));
264 Component = StringRef(Path.begin() + start_pos, end_pos - start_pos);
265 Position = start_pos;
269 bool const_iterator::operator==(const const_iterator &RHS) const {
270 return Path.begin() == RHS.Path.begin() &&
271 Position == RHS.Position;
274 bool const_iterator::operator!=(const const_iterator &RHS) const {
275 return !(*this == RHS);
278 ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
279 return Position - RHS.Position;
282 const StringRef root_path(StringRef path) {
283 const_iterator b = begin(path),
287 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
295 if (has_net || has_drive) {
296 if ((++pos != e) && is_separator((*pos)[0])) {
297 // {C:/,//net/}, so get the first two components.
298 return StringRef(path.begin(), b->size() + pos->size());
300 // just {C:,//net}, return the first component.
305 // POSIX style root directory.
306 if (is_separator((*b)[0])) {
314 const StringRef root_name(StringRef path) {
315 const_iterator b = begin(path),
318 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
326 if (has_net || has_drive) {
327 // just {C:,//net}, return the first component.
332 // No path or no name.
336 const StringRef root_directory(StringRef path) {
337 const_iterator b = begin(path),
341 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
349 if ((has_net || has_drive) &&
350 // {C:,//net}, skip to the next component.
351 (++pos != e) && is_separator((*pos)[0])) {
355 // POSIX style root directory.
356 if (!has_net && is_separator((*b)[0])) {
361 // No path or no root.
365 const StringRef relative_path(StringRef path) {
366 StringRef root = root_path(path);
367 return StringRef(path.begin() + root.size(), path.size() - root.size());
370 void append(SmallVectorImpl<char> &path, const Twine &a,
374 SmallString<32> a_storage;
375 SmallString<32> b_storage;
376 SmallString<32> c_storage;
377 SmallString<32> d_storage;
379 SmallVector<StringRef, 4> components;
380 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
381 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
382 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
383 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
385 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
386 e = components.end();
388 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
389 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
390 bool is_root_name = has_root_name(*i);
393 // Strip separators from beginning of component.
394 size_t loc = i->find_first_not_of(separators);
395 StringRef c = StringRef(i->begin() + loc, i->size() - loc);
398 path.append(c.begin(), c.end());
402 if (!component_has_sep && !(path.empty() || is_root_name)) {
404 path.push_back(prefered_separator);
407 path.append(i->begin(), i->end());
411 const StringRef parent_path(StringRef path) {
412 size_t end_pos = parent_path_end(path);
413 if (end_pos == StringRef::npos)
416 return StringRef(path.data(), end_pos);
419 void remove_filename(SmallVectorImpl<char> &path) {
420 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
421 if (end_pos != StringRef::npos)
422 path.set_size(end_pos);
425 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
426 StringRef p(path.begin(), path.size());
427 SmallString<32> ext_storage;
428 StringRef ext = extension.toStringRef(ext_storage);
430 // Erase existing extension.
431 size_t pos = p.find_last_of('.');
432 if (pos != StringRef::npos && pos >= filename_pos(p))
435 // Append '.' if needed.
436 if (ext.size() > 0 && ext[0] != '.')
440 path.append(ext.begin(), ext.end());
443 void native(const Twine &path, SmallVectorImpl<char> &result) {
447 SmallString<128> path_storage;
448 StringRef p = path.toStringRef(path_storage);
449 result.reserve(p.size());
450 for (StringRef::const_iterator i = p.begin(),
455 result.push_back('\\');
457 result.push_back(*i);
460 path.toVector(result);
464 const StringRef filename(StringRef path) {
465 return *(--end(path));
468 const StringRef stem(StringRef path) {
469 StringRef fname = filename(path);
470 size_t pos = fname.find_last_of('.');
471 if (pos == StringRef::npos)
474 if ((fname.size() == 1 && fname == ".") ||
475 (fname.size() == 2 && fname == ".."))
478 return StringRef(fname.begin(), pos);
481 const StringRef extension(StringRef path) {
482 StringRef fname = filename(path);
483 size_t pos = fname.find_last_of('.');
484 if (pos == StringRef::npos)
487 if ((fname.size() == 1 && fname == ".") ||
488 (fname.size() == 2 && fname == ".."))
491 return StringRef(fname.begin() + pos, fname.size() - pos);
494 bool has_root_name(const Twine &path) {
495 SmallString<128> path_storage;
496 StringRef p = path.toStringRef(path_storage);
498 return !root_name(p).empty();
501 bool has_root_directory(const Twine &path) {
502 SmallString<128> path_storage;
503 StringRef p = path.toStringRef(path_storage);
505 return !root_directory(p).empty();
508 bool has_root_path(const Twine &path) {
509 SmallString<128> path_storage;
510 StringRef p = path.toStringRef(path_storage);
512 return !root_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 bool is_regular_file(file_status status) {
641 return status.type() == file_type::regular_file;
644 bool is_symlink(file_status status) {
645 return status.type() == file_type::symlink_file;
648 bool is_other(file_status status) {
649 return exists(status) &&
650 !is_regular_file(status) &&
651 !is_directory(status) &&
655 void directory_entry::replace_filename(const Twine &filename, file_status st,
656 file_status symlink_st) {
657 SmallString<128> path(Path.begin(), Path.end());
658 path::remove_filename(path);
659 path::append(path, filename);
662 SymlinkStatus = symlink_st;
665 } // end namespace fs
666 } // end namespace sys
667 } // end namespace llvm
669 // Include the truly platform-specific parts.
670 #if defined(LLVM_ON_UNIX)
671 #include "Unix/PathV2.inc"
673 #if defined(LLVM_ON_WIN32)
674 #include "Windows/PathV2.inc"