1 //===-- PathV2.cpp - Implement OS Path Concept ------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the operating system PathV2 API.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/PathV2.h"
15 #include "llvm/Support/FileSystem.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include <cctype>
18 #include <cstdio>
19 #include <cstring>
20
21 namespace {
22 using llvm::StringRef;
23 using llvm::sys::path::is_separator;
24
25 #ifdef LLVM_ON_WIN32
26 const StringRef separators = "\\/";
27 const char prefered_separator = '\\';
28 #else
29 const StringRef separators = "/";
30 const char prefered_separator = '/';
31 #endif
32
33 const llvm::error_code success;
34
find_first_component(StringRef path)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.
39 // * {/,\}
40 // * {.,..}
41 // * {file,directory}name
42
43 if (path.empty())
44 return path;
45
46 #ifdef LLVM_ON_WIN32
47 // C:
48 if (path.size() >= 2 && std::isalpha(path[0]) && path[1] == ':')
49 return path.substr(0, 2);
50 #endif
51
52 // //net
53 if ((path.size() > 2) &&
54 is_separator(path[0]) &&
55 path[0] == path[1] &&
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);
60 }
61
62 // {/,\}
63 if (is_separator(path[0]))
64 return path.substr(0, 1);
65
66 if (path.startswith(".."))
67 return path.substr(0, 2);
68
69 if (path[0] == '.')
70 return path.substr(0, 1);
71
72 // * {file,directory}name
73 size_t end = path.find_first_of(separators, 2);
74 return path.substr(0, end);
75 }
76
filename_pos(StringRef str)77 size_t filename_pos(StringRef str) {
78 if (str.size() == 2 &&
79 is_separator(str[0]) &&
80 str[0] == str[1])
81 return 0;
82
83 if (str.size() > 0 && is_separator(str[str.size() - 1]))
84 return str.size() - 1;
85
86 size_t pos = str.find_last_of(separators, str.size() - 1);
87
88 #ifdef LLVM_ON_WIN32
89 if (pos == StringRef::npos)
90 pos = str.find_last_of(':', str.size() - 2);
91 #endif
92
93 if (pos == StringRef::npos ||
94 (pos == 1 && is_separator(str[0])))
95 return 0;
96
97 return pos + 1;
98 }
99
root_dir_start(StringRef str)100 size_t root_dir_start(StringRef str) {
101 // case "c:/"
102 #ifdef LLVM_ON_WIN32
103 if (str.size() > 2 &&
104 str[1] == ':' &&
105 is_separator(str[2]))
106 return 2;
107 #endif
108
109 // case "//"
110 if (str.size() == 2 &&
111 is_separator(str[0]) &&
112 str[0] == str[1])
113 return StringRef::npos;
114
115 // case "//net"
116 if (str.size() > 3 &&
117 is_separator(str[0]) &&
118 str[0] == str[1] &&
119 !is_separator(str[2])) {
120 return str.find_first_of(separators, 2);
121 }
122
123 // case "/"
124 if (str.size() > 0 && is_separator(str[0]))
125 return 0;
126
127 return StringRef::npos;
128 }
129
parent_path_end(StringRef path)130 size_t parent_path_end(StringRef path) {
131 size_t end_pos = filename_pos(path);
132
133 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
134
135 // Skip separators except for root dir.
136 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
137
138 while(end_pos > 0 &&
139 (end_pos - 1) != root_dir_pos &&
140 is_separator(path[end_pos - 1]))
141 --end_pos;
142
143 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
144 return StringRef::npos;
145
146 return end_pos;
147 }
148 } // end unnamed namespace
149
150 namespace llvm {
151 namespace sys {
152 namespace path {
153
begin(StringRef path)154 const_iterator begin(StringRef path) {
155 const_iterator i;
156 i.Path = path;
157 i.Component = find_first_component(path);
158 i.Position = 0;
159 return i;
160 }
161
end(StringRef path)162 const_iterator end(StringRef path) {
163 const_iterator i;
164 i.Path = path;
165 i.Position = path.size();
166 return i;
167 }
168
operator ++()169 const_iterator &const_iterator::operator++() {
170 assert(Position < Path.size() && "Tried to increment past end!");
171
172 // Increment Position to past the current component
173 Position += Component.size();
174
175 // Check for end.
176 if (Position == Path.size()) {
177 Component = StringRef();
178 return *this;
179 }
180
181 // Both POSIX and Windows treat paths that begin with exactly two separators
182 // specially.
183 bool was_net = Component.size() > 2 &&
184 is_separator(Component[0]) &&
185 Component[1] == Component[0] &&
186 !is_separator(Component[2]);
187
188 // Handle separators.
189 if (is_separator(Path[Position])) {
190 // Root dir.
191 if (was_net
192 #ifdef LLVM_ON_WIN32
193 // c:/
194 || Component.endswith(":")
195 #endif
196 ) {
197 Component = Path.substr(Position, 1);
198 return *this;
199 }
200
201 // Skip extra separators.
202 while (Position != Path.size() &&
203 is_separator(Path[Position])) {
204 ++Position;
205 }
206
207 // Treat trailing '/' as a '.'.
208 if (Position == Path.size()) {
209 --Position;
210 Component = ".";
211 return *this;
212 }
213 }
214
215 // Find next component.
216 size_t end_pos = Path.find_first_of(separators, Position);
217 Component = Path.slice(Position, end_pos);
218
219 return *this;
220 }
221
operator --()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() &&
225 Path.size() > 1 &&
226 is_separator(Path[Position - 1])
227 #ifdef LLVM_ON_WIN32
228 && Path[Position - 2] != ':'
229 #endif
230 ) {
231 --Position;
232 Component = ".";
233 return *this;
234 }
235
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;
239
240 while(end_pos > 0 &&
241 (end_pos - 1) != root_dir_pos &&
242 is_separator(Path[end_pos - 1]))
243 --end_pos;
244
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;
249 return *this;
250 }
251
operator ==(const const_iterator & RHS) const252 bool const_iterator::operator==(const const_iterator &RHS) const {
253 return Path.begin() == RHS.Path.begin() &&
254 Position == RHS.Position;
255 }
256
operator !=(const const_iterator & RHS) const257 bool const_iterator::operator!=(const const_iterator &RHS) const {
258 return !(*this == RHS);
259 }
260
operator -(const const_iterator & RHS) const261 ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
262 return Position - RHS.Position;
263 }
264
root_path(StringRef path)265 const StringRef root_path(StringRef path) {
266 const_iterator b = begin(path),
267 pos = b,
268 e = end(path);
269 if (b != e) {
270 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
271 bool has_drive =
272 #ifdef LLVM_ON_WIN32
273 b->endswith(":");
274 #else
275 false;
276 #endif
277
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());
282 } else {
283 // just {C:,//net}, return the first component.
284 return *b;
285 }
286 }
287
288 // POSIX style root directory.
289 if (is_separator((*b)[0])) {
290 return *b;
291 }
292 }
293
294 return StringRef();
295 }
296
root_name(StringRef path)297 const StringRef root_name(StringRef path) {
298 const_iterator b = begin(path),
299 e = end(path);
300 if (b != e) {
301 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
302 bool has_drive =
303 #ifdef LLVM_ON_WIN32
304 b->endswith(":");
305 #else
306 false;
307 #endif
308
309 if (has_net || has_drive) {
310 // just {C:,//net}, return the first component.
311 return *b;
312 }
313 }
314
315 // No path or no name.
316 return StringRef();
317 }
318
root_directory(StringRef path)319 const StringRef root_directory(StringRef path) {
320 const_iterator b = begin(path),
321 pos = b,
322 e = end(path);
323 if (b != e) {
324 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
325 bool has_drive =
326 #ifdef LLVM_ON_WIN32
327 b->endswith(":");
328 #else
329 false;
330 #endif
331
332 if ((has_net || has_drive) &&
333 // {C:,//net}, skip to the next component.
334 (++pos != e) && is_separator((*pos)[0])) {
335 return *pos;
336 }
337
338 // POSIX style root directory.
339 if (!has_net && is_separator((*b)[0])) {
340 return *b;
341 }
342 }
343
344 // No path or no root.
345 return StringRef();
346 }
347
relative_path(StringRef path)348 const StringRef relative_path(StringRef path) {
349 StringRef root = root_path(path);
350 return root.substr(root.size());
351 }
352
append(SmallVectorImpl<char> & path,const Twine & a,const Twine & b,const Twine & c,const Twine & d)353 void append(SmallVectorImpl<char> &path, const Twine &a,
354 const Twine &b,
355 const Twine &c,
356 const Twine &d) {
357 SmallString<32> a_storage;
358 SmallString<32> b_storage;
359 SmallString<32> c_storage;
360 SmallString<32> d_storage;
361
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));
367
368 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
369 e = components.end();
370 i != e; ++i) {
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);
374
375 if (path_has_sep) {
376 // Strip separators from beginning of component.
377 size_t loc = i->find_first_not_of(separators);
378 StringRef c = i->substr(loc);
379
380 // Append it.
381 path.append(c.begin(), c.end());
382 continue;
383 }
384
385 if (!component_has_sep && !(path.empty() || is_root_name)) {
386 // Add a separator.
387 path.push_back(prefered_separator);
388 }
389
390 path.append(i->begin(), i->end());
391 }
392 }
393
append(SmallVectorImpl<char> & path,const_iterator begin,const_iterator end)394 void append(SmallVectorImpl<char> &path,
395 const_iterator begin, const_iterator end) {
396 for (; begin != end; ++begin)
397 path::append(path, *begin);
398 }
399
parent_path(StringRef path)400 const StringRef parent_path(StringRef path) {
401 size_t end_pos = parent_path_end(path);
402 if (end_pos == StringRef::npos)
403 return StringRef();
404 else
405 return path.substr(0, end_pos);
406 }
407
remove_filename(SmallVectorImpl<char> & path)408 void remove_filename(SmallVectorImpl<char> &path) {
409 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
410 if (end_pos != StringRef::npos)
411 path.set_size(end_pos);
412 }
413
replace_extension(SmallVectorImpl<char> & path,const Twine & extension)414 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
415 StringRef p(path.begin(), path.size());
416 SmallString<32> ext_storage;
417 StringRef ext = extension.toStringRef(ext_storage);
418
419 // Erase existing extension.
420 size_t pos = p.find_last_of('.');
421 if (pos != StringRef::npos && pos >= filename_pos(p))
422 path.set_size(pos);
423
424 // Append '.' if needed.
425 if (ext.size() > 0 && ext[0] != '.')
426 path.push_back('.');
427
428 // Append extension.
429 path.append(ext.begin(), ext.end());
430 }
431
native(const Twine & path,SmallVectorImpl<char> & result)432 void native(const Twine &path, SmallVectorImpl<char> &result) {
433 // Clear result.
434 result.clear();
435 #ifdef LLVM_ON_WIN32
436 SmallString<128> path_storage;
437 StringRef p = path.toStringRef(path_storage);
438 result.reserve(p.size());
439 for (StringRef::const_iterator i = p.begin(),
440 e = p.end();
441 i != e;
442 ++i) {
443 if (*i == '/')
444 result.push_back('\\');
445 else
446 result.push_back(*i);
447 }
448 #else
449 path.toVector(result);
450 #endif
451 }
452
filename(StringRef path)453 const StringRef filename(StringRef path) {
454 return *(--end(path));
455 }
456
stem(StringRef path)457 const StringRef stem(StringRef path) {
458 StringRef fname = filename(path);
459 size_t pos = fname.find_last_of('.');
460 if (pos == StringRef::npos)
461 return fname;
462 else
463 if ((fname.size() == 1 && fname == ".") ||
464 (fname.size() == 2 && fname == ".."))
465 return fname;
466 else
467 return fname.substr(0, pos);
468 }
469
extension(StringRef path)470 const StringRef extension(StringRef path) {
471 StringRef fname = filename(path);
472 size_t pos = fname.find_last_of('.');
473 if (pos == StringRef::npos)
474 return StringRef();
475 else
476 if ((fname.size() == 1 && fname == ".") ||
477 (fname.size() == 2 && fname == ".."))
478 return StringRef();
479 else
480 return fname.substr(pos);
481 }
482
is_separator(char value)483 bool is_separator(char value) {
484 switch(value) {
485 #ifdef LLVM_ON_WIN32
486 case '\\': // fall through
487 #endif
488 case '/': return true;
489 default: return false;
490 }
491 }
492
has_root_name(const Twine & path)493 bool has_root_name(const Twine &path) {
494 SmallString<128> path_storage;
495 StringRef p = path.toStringRef(path_storage);
496
497 return !root_name(p).empty();
498 }
499
has_root_directory(const Twine & path)500 bool has_root_directory(const Twine &path) {
501 SmallString<128> path_storage;
502 StringRef p = path.toStringRef(path_storage);
503
504 return !root_directory(p).empty();
505 }
506
has_root_path(const Twine & path)507 bool has_root_path(const Twine &path) {
508 SmallString<128> path_storage;
509 StringRef p = path.toStringRef(path_storage);
510
511 return !root_path(p).empty();
512 }
513
has_relative_path(const Twine & path)514 bool has_relative_path(const Twine &path) {
515 SmallString<128> path_storage;
516 StringRef p = path.toStringRef(path_storage);
517
518 return !relative_path(p).empty();
519 }
520
has_filename(const Twine & path)521 bool has_filename(const Twine &path) {
522 SmallString<128> path_storage;
523 StringRef p = path.toStringRef(path_storage);
524
525 return !filename(p).empty();
526 }
527
has_parent_path(const Twine & path)528 bool has_parent_path(const Twine &path) {
529 SmallString<128> path_storage;
530 StringRef p = path.toStringRef(path_storage);
531
532 return !parent_path(p).empty();
533 }
534
has_stem(const Twine & path)535 bool has_stem(const Twine &path) {
536 SmallString<128> path_storage;
537 StringRef p = path.toStringRef(path_storage);
538
539 return !stem(p).empty();
540 }
541
has_extension(const Twine & path)542 bool has_extension(const Twine &path) {
543 SmallString<128> path_storage;
544 StringRef p = path.toStringRef(path_storage);
545
546 return !extension(p).empty();
547 }
548
is_absolute(const Twine & path)549 bool is_absolute(const Twine &path) {
550 SmallString<128> path_storage;
551 StringRef p = path.toStringRef(path_storage);
552
553 bool rootDir = has_root_directory(p),
554 #ifdef LLVM_ON_WIN32
555 rootName = has_root_name(p);
556 #else
557 rootName = true;
558 #endif
559
560 return rootDir && rootName;
561 }
562
is_relative(const Twine & path)563 bool is_relative(const Twine &path) {
564 return !is_absolute(path);
565 }
566
567 } // end namespace path
568
569 namespace fs {
570
make_absolute(SmallVectorImpl<char> & path)571 error_code make_absolute(SmallVectorImpl<char> &path) {
572 StringRef p(path.data(), path.size());
573
574 bool rootName = path::has_root_name(p),
575 rootDirectory = path::has_root_directory(p);
576
577 // Already absolute.
578 if (rootName && rootDirectory)
579 return success;
580
581 // All of the following conditions will need the current directory.
582 SmallString<128> current_dir;
583 if (error_code ec = current_path(current_dir)) return ec;
584
585 // Relative path. Prepend the current directory.
586 if (!rootName && !rootDirectory) {
587 // Append path to the current directory.
588 path::append(current_dir, p);
589 // Set path to the result.
590 path.swap(current_dir);
591 return success;
592 }
593
594 if (!rootName && rootDirectory) {
595 StringRef cdrn = path::root_name(current_dir);
596 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
597 path::append(curDirRootName, p);
598 // Set path to the result.
599 path.swap(curDirRootName);
600 return success;
601 }
602
603 if (rootName && !rootDirectory) {
604 StringRef pRootName = path::root_name(p);
605 StringRef bRootDirectory = path::root_directory(current_dir);
606 StringRef bRelativePath = path::relative_path(current_dir);
607 StringRef pRelativePath = path::relative_path(p);
608
609 SmallString<128> res;
610 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
611 path.swap(res);
612 return success;
613 }
614
615 llvm_unreachable("All rootName and rootDirectory combinations should have "
616 "occurred above!");
617 }
618
create_directories(const Twine & path,bool & existed)619 error_code create_directories(const Twine &path, bool &existed) {
620 SmallString<128> path_storage;
621 StringRef p = path.toStringRef(path_storage);
622
623 StringRef parent = path::parent_path(p);
624 bool parent_exists;
625
626 if (error_code ec = fs::exists(parent, parent_exists)) return ec;
627
628 if (!parent_exists)
629 return create_directories(parent, existed);
630
631 return create_directory(p, existed);
632 }
633
exists(file_status status)634 bool exists(file_status status) {
635 return status_known(status) && status.type() != file_type::file_not_found;
636 }
637
status_known(file_status s)638 bool status_known(file_status s) {
639 return s.type() != file_type::status_error;
640 }
641
is_directory(file_status status)642 bool is_directory(file_status status) {
643 return status.type() == file_type::directory_file;
644 }
645
is_directory(const Twine & path,bool & result)646 error_code is_directory(const Twine &path, bool &result) {
647 file_status st;
648 if (error_code ec = status(path, st))
649 return ec;
650 result = is_directory(st);
651 return success;
652 }
653
is_regular_file(file_status status)654 bool is_regular_file(file_status status) {
655 return status.type() == file_type::regular_file;
656 }
657
is_regular_file(const Twine & path,bool & result)658 error_code is_regular_file(const Twine &path, bool &result) {
659 file_status st;
660 if (error_code ec = status(path, st))
661 return ec;
662 result = is_regular_file(st);
663 return success;
664 }
665
is_symlink(file_status status)666 bool is_symlink(file_status status) {
667 return status.type() == file_type::symlink_file;
668 }
669
is_symlink(const Twine & path,bool & result)670 error_code is_symlink(const Twine &path, bool &result) {
671 file_status st;
672 if (error_code ec = status(path, st))
673 return ec;
674 result = is_symlink(st);
675 return success;
676 }
677
is_other(file_status status)678 bool is_other(file_status status) {
679 return exists(status) &&
680 !is_regular_file(status) &&
681 !is_directory(status) &&
682 !is_symlink(status);
683 }
684
replace_filename(const Twine & filename,file_status st,file_status symlink_st)685 void directory_entry::replace_filename(const Twine &filename, file_status st,
686 file_status symlink_st) {
687 SmallString<128> path(Path.begin(), Path.end());
688 path::remove_filename(path);
689 path::append(path, filename);
690 Path = path.str();
691 Status = st;
692 SymlinkStatus = symlink_st;
693 }
694
has_magic(const Twine & path,const Twine & magic,bool & result)695 error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
696 SmallString<32> MagicStorage;
697 StringRef Magic = magic.toStringRef(MagicStorage);
698 SmallString<32> Buffer;
699
700 if (error_code ec = get_magic(path, Magic.size(), Buffer)) {
701 if (ec == errc::value_too_large) {
702 // Magic.size() > file_size(Path).
703 result = false;
704 return success;
705 }
706 return ec;
707 }
708
709 result = Magic == Buffer;
710 return success;
711 }
712
identify_magic(const Twine & path,LLVMFileType & result)713 error_code identify_magic(const Twine &path, LLVMFileType &result) {
714 SmallString<32> Magic;
715 error_code ec = get_magic(path, Magic.capacity(), Magic);
716 if (ec && ec != errc::value_too_large)
717 return ec;
718
719 result = IdentifyFileType(Magic.data(), Magic.size());
720 return success;
721 }
722
723 namespace {
remove_all_r(StringRef path,file_type ft,uint32_t & count)724 error_code remove_all_r(StringRef path, file_type ft, uint32_t &count) {
725 if (ft == file_type::directory_file) {
726 // This code would be a lot better with exceptions ;/.
727 error_code ec;
728 for (directory_iterator i(path, ec), e; i != e; i.increment(ec)) {
729 if (ec) return ec;
730 file_status st;
731 if (error_code ec = i->status(st)) return ec;
732 if (error_code ec = remove_all_r(i->path(), st.type(), count)) return ec;
733 }
734 bool obviously_this_exists;
735 if (error_code ec = remove(path, obviously_this_exists)) return ec;
736 assert(obviously_this_exists);
737 ++count; // Include the directory itself in the items removed.
738 } else {
739 bool obviously_this_exists;
740 if (error_code ec = remove(path, obviously_this_exists)) return ec;
741 assert(obviously_this_exists);
742 ++count;
743 }
744
745 return success;
746 }
747 } // end unnamed namespace
748
remove_all(const Twine & path,uint32_t & num_removed)749 error_code remove_all(const Twine &path, uint32_t &num_removed) {
750 SmallString<128> path_storage;
751 StringRef p = path.toStringRef(path_storage);
752
753 file_status fs;
754 if (error_code ec = status(path, fs))
755 return ec;
756 num_removed = 0;
757 return remove_all_r(p, fs.type(), num_removed);
758 }
759
status(file_status & result) const760 error_code directory_entry::status(file_status &result) const {
761 return fs::status(Path, result);
762 }
763
764 } // end namespace fs
765 } // end namespace sys
766 } // end namespace llvm
767
768 // Include the truly platform-specific parts.
769 #if defined(LLVM_ON_UNIX)
770 #include "Unix/PathV2.inc"
771 #endif
772 #if defined(LLVM_ON_WIN32)
773 #include "Windows/PathV2.inc"
774 #endif
775