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 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;
161 const_iterator begin(StringRef path) {
164 i.Component = find_first_component(path);
169 const_iterator end(StringRef path) {
172 i.Position = path.size();
176 const_iterator &const_iterator::operator++() {
177 assert(Position < Path.size() && "Tried to increment past end!");
179 // Increment Position to past the current component
180 Position += Component.size();
183 if (Position == Path.size()) {
184 Component = StringRef();
188 // Both POSIX and Windows treat paths that begin with exactly two separators
190 bool was_net = Component.size() > 2 &&
191 is_separator(Component[0]) &&
192 Component[1] == Component[0] &&
193 !is_separator(Component[2]);
195 // Handle separators.
196 if (is_separator(Path[Position])) {
201 || Component.endswith(":")
204 Component = Path.substr(Position, 1);
208 // Skip extra separators.
209 while (Position != Path.size() &&
210 is_separator(Path[Position])) {
214 // Treat trailing '/' as a '.'.
215 if (Position == Path.size()) {
222 // Find next component.
223 size_t end_pos = Path.find_first_of(separators, Position);
224 Component = Path.slice(Position, end_pos);
229 const_iterator &const_iterator::operator--() {
230 // If we're at the end and the previous char was a '/', return '.'.
231 if (Position == Path.size() &&
233 is_separator(Path[Position - 1])
235 && Path[Position - 2] != ':'
243 // Skip separators unless it's the root directory.
244 size_t root_dir_pos = root_dir_start(Path);
245 size_t end_pos = Position;
248 (end_pos - 1) != root_dir_pos &&
249 is_separator(Path[end_pos - 1]))
252 // Find next separator.
253 size_t start_pos = filename_pos(Path.substr(0, end_pos));
254 Component = Path.slice(start_pos, end_pos);
255 Position = start_pos;
259 bool const_iterator::operator==(const const_iterator &RHS) const {
260 return Path.begin() == RHS.Path.begin() &&
261 Position == RHS.Position;
264 bool const_iterator::operator!=(const const_iterator &RHS) const {
265 return !(*this == RHS);
268 ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
269 return Position - RHS.Position;
272 const StringRef root_path(StringRef path) {
273 const_iterator b = begin(path),
277 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
285 if (has_net || has_drive) {
286 if ((++pos != e) && is_separator((*pos)[0])) {
287 // {C:/,//net/}, so get the first two components.
288 return path.substr(0, b->size() + pos->size());
290 // just {C:,//net}, return the first component.
295 // POSIX style root directory.
296 if (is_separator((*b)[0])) {
304 const StringRef root_name(StringRef path) {
305 const_iterator b = begin(path),
308 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
316 if (has_net || has_drive) {
317 // just {C:,//net}, return the first component.
322 // No path or no name.
326 const StringRef root_directory(StringRef path) {
327 const_iterator b = begin(path),
331 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
339 if ((has_net || has_drive) &&
340 // {C:,//net}, skip to the next component.
341 (++pos != e) && is_separator((*pos)[0])) {
345 // POSIX style root directory.
346 if (!has_net && is_separator((*b)[0])) {
351 // No path or no root.
355 const StringRef relative_path(StringRef path) {
356 StringRef root = root_path(path);
357 return root.substr(root.size());
360 void append(SmallVectorImpl<char> &path, const Twine &a,
364 SmallString<32> a_storage;
365 SmallString<32> b_storage;
366 SmallString<32> c_storage;
367 SmallString<32> d_storage;
369 SmallVector<StringRef, 4> components;
370 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
371 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
372 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
373 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
375 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
376 e = components.end();
378 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
379 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
380 bool is_root_name = has_root_name(*i);
383 // Strip separators from beginning of component.
384 size_t loc = i->find_first_not_of(separators);
385 StringRef c = i->substr(loc);
388 path.append(c.begin(), c.end());
392 if (!component_has_sep && !(path.empty() || is_root_name)) {
394 path.push_back(prefered_separator);
397 path.append(i->begin(), i->end());
401 const StringRef parent_path(StringRef path) {
402 size_t end_pos = parent_path_end(path);
403 if (end_pos == StringRef::npos)
406 return path.substr(0, end_pos);
409 void remove_filename(SmallVectorImpl<char> &path) {
410 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
411 if (end_pos != StringRef::npos)
412 path.set_size(end_pos);
415 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
416 StringRef p(path.begin(), path.size());
417 SmallString<32> ext_storage;
418 StringRef ext = extension.toStringRef(ext_storage);
420 // Erase existing extension.
421 size_t pos = p.find_last_of('.');
422 if (pos != StringRef::npos && pos >= filename_pos(p))
425 // Append '.' if needed.
426 if (ext.size() > 0 && ext[0] != '.')
430 path.append(ext.begin(), ext.end());
433 void native(const Twine &path, SmallVectorImpl<char> &result) {
437 SmallString<128> path_storage;
438 StringRef p = path.toStringRef(path_storage);
439 result.reserve(p.size());
440 for (StringRef::const_iterator i = p.begin(),
445 result.push_back('\\');
447 result.push_back(*i);
450 path.toVector(result);
454 const StringRef filename(StringRef path) {
455 return *(--end(path));
458 const StringRef stem(StringRef path) {
459 StringRef fname = filename(path);
460 size_t pos = fname.find_last_of('.');
461 if (pos == StringRef::npos)
464 if ((fname.size() == 1 && fname == ".") ||
465 (fname.size() == 2 && fname == ".."))
468 return fname.substr(0, pos);
471 const StringRef extension(StringRef path) {
472 StringRef fname = filename(path);
473 size_t pos = fname.find_last_of('.');
474 if (pos == StringRef::npos)
477 if ((fname.size() == 1 && fname == ".") ||
478 (fname.size() == 2 && fname == ".."))
481 return fname.substr(pos);
484 bool has_root_name(const Twine &path) {
485 SmallString<128> path_storage;
486 StringRef p = path.toStringRef(path_storage);
488 return !root_name(p).empty();
491 bool has_root_directory(const Twine &path) {
492 SmallString<128> path_storage;
493 StringRef p = path.toStringRef(path_storage);
495 return !root_directory(p).empty();
498 bool has_root_path(const Twine &path) {
499 SmallString<128> path_storage;
500 StringRef p = path.toStringRef(path_storage);
502 return !root_path(p).empty();
505 bool has_relative_path(const Twine &path) {
506 SmallString<128> path_storage;
507 StringRef p = path.toStringRef(path_storage);
509 return !relative_path(p).empty();
512 bool has_filename(const Twine &path) {
513 SmallString<128> path_storage;
514 StringRef p = path.toStringRef(path_storage);
516 return !filename(p).empty();
519 bool has_parent_path(const Twine &path) {
520 SmallString<128> path_storage;
521 StringRef p = path.toStringRef(path_storage);
523 return !parent_path(p).empty();
526 bool has_stem(const Twine &path) {
527 SmallString<128> path_storage;
528 StringRef p = path.toStringRef(path_storage);
530 return !stem(p).empty();
533 bool has_extension(const Twine &path) {
534 SmallString<128> path_storage;
535 StringRef p = path.toStringRef(path_storage);
537 return !extension(p).empty();
540 bool is_absolute(const Twine &path) {
541 SmallString<128> path_storage;
542 StringRef p = path.toStringRef(path_storage);
544 bool rootDir = has_root_directory(p),
546 rootName = has_root_name(p);
551 return rootDir && rootName;
554 bool is_relative(const Twine &path) {
555 return !is_absolute(path);
558 } // end namespace path
562 error_code make_absolute(SmallVectorImpl<char> &path) {
563 StringRef p(path.data(), path.size());
565 bool rootName = path::has_root_name(p),
566 rootDirectory = path::has_root_directory(p);
569 if (rootName && rootDirectory)
572 // All of the following conditions will need the current directory.
573 SmallString<128> current_dir;
574 if (error_code ec = current_path(current_dir)) return ec;
576 // Relative path. Prepend the current directory.
577 if (!rootName && !rootDirectory) {
578 // Append path to the current directory.
579 path::append(current_dir, p);
580 // Set path to the result.
581 path.swap(current_dir);
585 if (!rootName && rootDirectory) {
586 StringRef cdrn = path::root_name(current_dir);
587 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
588 path::append(curDirRootName, p);
589 // Set path to the result.
590 path.swap(curDirRootName);
594 if (rootName && !rootDirectory) {
595 StringRef pRootName = path::root_name(p);
596 StringRef bRootDirectory = path::root_directory(current_dir);
597 StringRef bRelativePath = path::relative_path(current_dir);
598 StringRef pRelativePath = path::relative_path(p);
600 SmallString<128> res;
601 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
606 llvm_unreachable("All rootName and rootDirectory combinations should have "
610 error_code create_directories(const Twine &path, bool &existed) {
611 SmallString<128> path_storage;
612 StringRef p = path.toStringRef(path_storage);
614 StringRef parent = path::parent_path(p);
617 if (error_code ec = fs::exists(parent, parent_exists)) return ec;
620 return create_directories(parent, existed);
622 return create_directory(p, existed);
625 bool exists(file_status status) {
626 return status_known(status) && status.type() != file_type::file_not_found;
629 bool status_known(file_status s) {
630 return s.type() != file_type::status_error;
633 bool is_directory(file_status status) {
634 return status.type() == file_type::directory_file;
637 bool is_regular_file(file_status status) {
638 return status.type() == file_type::regular_file;
641 bool is_symlink(file_status status) {
642 return status.type() == file_type::symlink_file;
645 bool is_other(file_status status) {
646 return exists(status) &&
647 !is_regular_file(status) &&
648 !is_directory(status) &&
652 void directory_entry::replace_filename(const Twine &filename, file_status st,
653 file_status symlink_st) {
654 SmallString<128> path(Path.begin(), Path.end());
655 path::remove_filename(path);
656 path::append(path, filename);
659 SymlinkStatus = symlink_st;
662 } // end namespace fs
663 } // end namespace sys
664 } // end namespace llvm
666 // Include the truly platform-specific parts.
667 #if defined(LLVM_ON_UNIX)
668 #include "Unix/PathV2.inc"
670 #if defined(LLVM_ON_WIN32)
671 #include "Windows/PathV2.inc"