LLVM 19.0.0git
Path.cpp
Go to the documentation of this file.
1//===-- Path.cpp - Implement OS Path Concept ------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the operating system Path API.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Support/Path.h"
14#include "llvm/ADT/ArrayRef.h"
15#include "llvm/ADT/ScopeExit.h"
17#include "llvm/Config/config.h"
18#include "llvm/Config/llvm-config.h"
19#include "llvm/Support/Endian.h"
20#include "llvm/Support/Errc.h"
25#include <cctype>
26#include <cerrno>
27
28#if !defined(_MSC_VER) && !defined(__MINGW32__)
29#include <unistd.h>
30#else
31#include <io.h>
32#endif
33
34using namespace llvm;
35using namespace llvm::support::endian;
36
37namespace {
38 using llvm::StringRef;
41
42 inline Style real_style(Style style) {
43 if (style != Style::native)
44 return style;
45 if (is_style_posix(style))
46 return Style::posix;
47 return LLVM_WINDOWS_PREFER_FORWARD_SLASH ? Style::windows_slash
48 : Style::windows_backslash;
49 }
50
51 inline const char *separators(Style style) {
52 if (is_style_windows(style))
53 return "\\/";
54 return "/";
55 }
56
57 inline char preferred_separator(Style style) {
58 if (real_style(style) == Style::windows)
59 return '\\';
60 return '/';
61 }
62
63 StringRef find_first_component(StringRef path, Style style) {
64 // Look for this first component in the following order.
65 // * empty (in this case we return an empty string)
66 // * either C: or {//,\\}net.
67 // * {/,\}
68 // * {file,directory}name
69
70 if (path.empty())
71 return path;
72
73 if (is_style_windows(style)) {
74 // C:
75 if (path.size() >= 2 &&
76 std::isalpha(static_cast<unsigned char>(path[0])) && path[1] == ':')
77 return path.substr(0, 2);
78 }
79
80 // //net
81 if ((path.size() > 2) && is_separator(path[0], style) &&
82 path[0] == path[1] && !is_separator(path[2], style)) {
83 // Find the next directory separator.
84 size_t end = path.find_first_of(separators(style), 2);
85 return path.substr(0, end);
86 }
87
88 // {/,\}
89 if (is_separator(path[0], style))
90 return path.substr(0, 1);
91
92 // * {file,directory}name
93 size_t end = path.find_first_of(separators(style));
94 return path.substr(0, end);
95 }
96
97 // Returns the first character of the filename in str. For paths ending in
98 // '/', it returns the position of the '/'.
99 size_t filename_pos(StringRef str, Style style) {
100 if (str.size() > 0 && is_separator(str[str.size() - 1], style))
101 return str.size() - 1;
102
103 size_t pos = str.find_last_of(separators(style), str.size() - 1);
104
105 if (is_style_windows(style)) {
106 if (pos == StringRef::npos)
107 pos = str.find_last_of(':', str.size() - 1);
108 }
109
110 if (pos == StringRef::npos || (pos == 1 && is_separator(str[0], style)))
111 return 0;
112
113 return pos + 1;
114 }
115
116 // Returns the position of the root directory in str. If there is no root
117 // directory in str, it returns StringRef::npos.
118 size_t root_dir_start(StringRef str, Style style) {
119 // case "c:/"
120 if (is_style_windows(style)) {
121 if (str.size() > 2 && str[1] == ':' && is_separator(str[2], style))
122 return 2;
123 }
124
125 // case "//net"
126 if (str.size() > 3 && is_separator(str[0], style) && str[0] == str[1] &&
127 !is_separator(str[2], style)) {
128 return str.find_first_of(separators(style), 2);
129 }
130
131 // case "/"
132 if (str.size() > 0 && is_separator(str[0], style))
133 return 0;
134
135 return StringRef::npos;
136 }
137
138 // Returns the position past the end of the "parent path" of path. The parent
139 // path will not end in '/', unless the parent is the root directory. If the
140 // path has no parent, 0 is returned.
141 size_t parent_path_end(StringRef path, Style style) {
142 size_t end_pos = filename_pos(path, style);
143
144 bool filename_was_sep =
145 path.size() > 0 && is_separator(path[end_pos], style);
146
147 // Skip separators until we reach root dir (or the start of the string).
148 size_t root_dir_pos = root_dir_start(path, style);
149 while (end_pos > 0 &&
150 (root_dir_pos == StringRef::npos || end_pos > root_dir_pos) &&
151 is_separator(path[end_pos - 1], style))
152 --end_pos;
153
154 if (end_pos == root_dir_pos && !filename_was_sep) {
155 // We've reached the root dir and the input path was *not* ending in a
156 // sequence of slashes. Include the root dir in the parent path.
157 return root_dir_pos + 1;
158 }
159
160 // Otherwise, just include before the last slash.
161 return end_pos;
162 }
163} // end unnamed namespace
164
168 FS_Name
170
171static std::error_code
172createUniqueEntity(const Twine &Model, int &ResultFD,
173 SmallVectorImpl<char> &ResultPath, bool MakeAbsolute,
175 unsigned Mode = 0) {
176
177 // Limit the number of attempts we make, so that we don't infinite loop. E.g.
178 // "permission denied" could be for a specific file (so we retry with a
179 // different name) or for the whole directory (retry would always fail).
180 // Checking which is racy, so we try a number of times, then give up.
181 std::error_code EC;
182 for (int Retries = 128; Retries > 0; --Retries) {
183 sys::fs::createUniquePath(Model, ResultPath, MakeAbsolute);
184 // Try to open + create the file.
185 switch (Type) {
186 case FS_File: {
187 EC = sys::fs::openFileForReadWrite(Twine(ResultPath.begin()), ResultFD,
188 sys::fs::CD_CreateNew, Flags, Mode);
189 if (EC) {
190 // errc::permission_denied happens on Windows when we try to open a file
191 // that has been marked for deletion.
192 if (EC == errc::file_exists || EC == errc::permission_denied)
193 continue;
194 return EC;
195 }
196
197 return std::error_code();
198 }
199
200 case FS_Name: {
201 EC = sys::fs::access(ResultPath.begin(), sys::fs::AccessMode::Exist);
202 if (EC == errc::no_such_file_or_directory)
203 return std::error_code();
204 if (EC)
205 return EC;
206 continue;
207 }
208
209 case FS_Dir: {
210 EC = sys::fs::create_directory(ResultPath.begin(), false);
211 if (EC) {
212 if (EC == errc::file_exists)
213 continue;
214 return EC;
215 }
216 return std::error_code();
217 }
218 }
219 llvm_unreachable("Invalid Type");
220 }
221 return EC;
222}
223
224namespace llvm {
225namespace sys {
226namespace path {
227
230 i.Path = path;
231 i.Component = find_first_component(path, style);
232 i.Position = 0;
233 i.S = style;
234 return i;
235}
236
239 i.Path = path;
240 i.Position = path.size();
241 return i;
242}
243
245 assert(Position < Path.size() && "Tried to increment past end!");
246
247 // Increment Position to past the current component
248 Position += Component.size();
249
250 // Check for end.
251 if (Position == Path.size()) {
252 Component = StringRef();
253 return *this;
254 }
255
256 // Both POSIX and Windows treat paths that begin with exactly two separators
257 // specially.
258 bool was_net = Component.size() > 2 && is_separator(Component[0], S) &&
259 Component[1] == Component[0] && !is_separator(Component[2], S);
260
261 // Handle separators.
262 if (is_separator(Path[Position], S)) {
263 // Root dir.
264 if (was_net ||
265 // c:/
266 (is_style_windows(S) && Component.ends_with(":"))) {
267 Component = Path.substr(Position, 1);
268 return *this;
269 }
270
271 // Skip extra separators.
272 while (Position != Path.size() && is_separator(Path[Position], S)) {
273 ++Position;
274 }
275
276 // Treat trailing '/' as a '.', unless it is the root dir.
277 if (Position == Path.size() && Component != "/") {
278 --Position;
279 Component = ".";
280 return *this;
281 }
282 }
283
284 // Find next component.
285 size_t end_pos = Path.find_first_of(separators(S), Position);
286 Component = Path.slice(Position, end_pos);
287
288 return *this;
289}
290
292 return Path.begin() == RHS.Path.begin() && Position == RHS.Position;
293}
294
296 return Position - RHS.Position;
297}
298
301 I.Path = Path;
302 I.Position = Path.size();
303 I.S = style;
304 ++I;
305 return I;
306}
307
310 I.Path = Path;
311 I.Component = Path.substr(0, 0);
312 I.Position = 0;
313 return I;
314}
315
317 size_t root_dir_pos = root_dir_start(Path, S);
318
319 // Skip separators unless it's the root directory.
320 size_t end_pos = Position;
321 while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
322 is_separator(Path[end_pos - 1], S))
323 --end_pos;
324
325 // Treat trailing '/' as a '.', unless it is the root dir.
326 if (Position == Path.size() && !Path.empty() &&
327 is_separator(Path.back(), S) &&
328 (root_dir_pos == StringRef::npos || end_pos - 1 > root_dir_pos)) {
329 --Position;
330 Component = ".";
331 return *this;
332 }
333
334 // Find next separator.
335 size_t start_pos = filename_pos(Path.substr(0, end_pos), S);
336 Component = Path.slice(start_pos, end_pos);
337 Position = start_pos;
338 return *this;
339}
340
342 return Path.begin() == RHS.Path.begin() && Component == RHS.Component &&
343 Position == RHS.Position;
344}
345
347 return Position - RHS.Position;
348}
349
351 const_iterator b = begin(path, style), pos = b, e = end(path);
352 if (b != e) {
353 bool has_net =
354 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
355 bool has_drive = is_style_windows(style) && b->ends_with(":");
356
357 if (has_net || has_drive) {
358 if ((++pos != e) && is_separator((*pos)[0], style)) {
359 // {C:/,//net/}, so get the first two components.
360 return path.substr(0, b->size() + pos->size());
361 }
362 // just {C:,//net}, return the first component.
363 return *b;
364 }
365
366 // POSIX style root directory.
367 if (is_separator((*b)[0], style)) {
368 return *b;
369 }
370 }
371
372 return StringRef();
373}
374
376 const_iterator b = begin(path, style), e = end(path);
377 if (b != e) {
378 bool has_net =
379 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
380 bool has_drive = is_style_windows(style) && b->ends_with(":");
381
382 if (has_net || has_drive) {
383 // just {C:,//net}, return the first component.
384 return *b;
385 }
386 }
387
388 // No path or no name.
389 return StringRef();
390}
391
393 const_iterator b = begin(path, style), pos = b, e = end(path);
394 if (b != e) {
395 bool has_net =
396 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
397 bool has_drive = is_style_windows(style) && b->ends_with(":");
398
399 if ((has_net || has_drive) &&
400 // {C:,//net}, skip to the next component.
401 (++pos != e) && is_separator((*pos)[0], style)) {
402 return *pos;
403 }
404
405 // POSIX style root directory.
406 if (!has_net && is_separator((*b)[0], style)) {
407 return *b;
408 }
409 }
410
411 // No path or no root.
412 return StringRef();
413}
414
416 StringRef root = root_path(path, style);
417 return path.substr(root.size());
418}
419
420void append(SmallVectorImpl<char> &path, Style style, const Twine &a,
421 const Twine &b, const Twine &c, const Twine &d) {
422 SmallString<32> a_storage;
423 SmallString<32> b_storage;
424 SmallString<32> c_storage;
425 SmallString<32> d_storage;
426
427 SmallVector<StringRef, 4> components;
428 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
429 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
430 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
431 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
432
433 for (auto &component : components) {
434 bool path_has_sep =
435 !path.empty() && is_separator(path[path.size() - 1], style);
436 if (path_has_sep) {
437 // Strip separators from beginning of component.
438 size_t loc = component.find_first_not_of(separators(style));
439 StringRef c = component.substr(loc);
440
441 // Append it.
442 path.append(c.begin(), c.end());
443 continue;
444 }
445
446 bool component_has_sep =
447 !component.empty() && is_separator(component[0], style);
448 if (!component_has_sep &&
449 !(path.empty() || has_root_name(component, style))) {
450 // Add a separator.
451 path.push_back(preferred_separator(style));
452 }
453
454 path.append(component.begin(), component.end());
455 }
456}
457
458void append(SmallVectorImpl<char> &path, const Twine &a, const Twine &b,
459 const Twine &c, const Twine &d) {
460 append(path, Style::native, a, b, c, d);
461}
462
464 const_iterator end, Style style) {
465 for (; begin != end; ++begin)
466 path::append(path, style, *begin);
467}
468
470 size_t end_pos = parent_path_end(path, style);
471 if (end_pos == StringRef::npos)
472 return StringRef();
473 return path.substr(0, end_pos);
474}
475
477 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()), style);
478 if (end_pos != StringRef::npos)
479 path.truncate(end_pos);
480}
481
483 Style style) {
484 StringRef p(path.begin(), path.size());
485 SmallString<32> ext_storage;
486 StringRef ext = extension.toStringRef(ext_storage);
487
488 // Erase existing extension.
489 size_t pos = p.find_last_of('.');
490 if (pos != StringRef::npos && pos >= filename_pos(p, style))
491 path.truncate(pos);
492
493 // Append '.' if needed.
494 if (ext.size() > 0 && ext[0] != '.')
495 path.push_back('.');
496
497 // Append extension.
498 path.append(ext.begin(), ext.end());
499}
500
501static bool starts_with(StringRef Path, StringRef Prefix,
502 Style style = Style::native) {
503 // Windows prefix matching : case and separator insensitive
504 if (is_style_windows(style)) {
505 if (Path.size() < Prefix.size())
506 return false;
507 for (size_t I = 0, E = Prefix.size(); I != E; ++I) {
508 bool SepPath = is_separator(Path[I], style);
509 bool SepPrefix = is_separator(Prefix[I], style);
510 if (SepPath != SepPrefix)
511 return false;
512 if (!SepPath && toLower(Path[I]) != toLower(Prefix[I]))
513 return false;
514 }
515 return true;
516 }
517 return Path.starts_with(Prefix);
518}
519
521 StringRef NewPrefix, Style style) {
522 if (OldPrefix.empty() && NewPrefix.empty())
523 return false;
524
525 StringRef OrigPath(Path.begin(), Path.size());
526 if (!starts_with(OrigPath, OldPrefix, style))
527 return false;
528
529 // If prefixes have the same size we can simply copy the new one over.
530 if (OldPrefix.size() == NewPrefix.size()) {
531 llvm::copy(NewPrefix, Path.begin());
532 return true;
533 }
534
535 StringRef RelPath = OrigPath.substr(OldPrefix.size());
536 SmallString<256> NewPath;
537 (Twine(NewPrefix) + RelPath).toVector(NewPath);
538 Path.swap(NewPath);
539 return true;
540}
541
542void native(const Twine &path, SmallVectorImpl<char> &result, Style style) {
543 assert((!path.isSingleStringRef() ||
544 path.getSingleStringRef().data() != result.data()) &&
545 "path and result are not allowed to overlap!");
546 // Clear result.
547 result.clear();
548 path.toVector(result);
549 native(result, style);
550}
551
553 if (Path.empty())
554 return;
555 if (is_style_windows(style)) {
556 for (char &Ch : Path)
557 if (is_separator(Ch, style))
558 Ch = preferred_separator(style);
559 if (Path[0] == '~' && (Path.size() == 1 || is_separator(Path[1], style))) {
560 SmallString<128> PathHome;
561 home_directory(PathHome);
562 PathHome.append(Path.begin() + 1, Path.end());
563 Path = PathHome;
564 }
565 } else {
566 std::replace(Path.begin(), Path.end(), '\\', '/');
567 }
568}
569
570std::string convert_to_slash(StringRef path, Style style) {
571 if (is_style_posix(style))
572 return std::string(path);
573
574 std::string s = path.str();
575 std::replace(s.begin(), s.end(), '\\', '/');
576 return s;
577}
578
579StringRef filename(StringRef path, Style style) { return *rbegin(path, style); }
580
582 StringRef fname = filename(path, style);
583 size_t pos = fname.find_last_of('.');
584 if (pos == StringRef::npos)
585 return fname;
586 if ((fname.size() == 1 && fname == ".") ||
587 (fname.size() == 2 && fname == ".."))
588 return fname;
589 return fname.substr(0, pos);
590}
591
593 StringRef fname = filename(path, style);
594 size_t pos = fname.find_last_of('.');
595 if (pos == StringRef::npos)
596 return StringRef();
597 if ((fname.size() == 1 && fname == ".") ||
598 (fname.size() == 2 && fname == ".."))
599 return StringRef();
600 return fname.substr(pos);
601}
602
603bool is_separator(char value, Style style) {
604 if (value == '/')
605 return true;
606 if (is_style_windows(style))
607 return value == '\\';
608 return false;
609}
610
612 if (real_style(style) == Style::windows)
613 return "\\";
614 return "/";
615}
616
617bool has_root_name(const Twine &path, Style style) {
618 SmallString<128> path_storage;
619 StringRef p = path.toStringRef(path_storage);
620
621 return !root_name(p, style).empty();
622}
623
624bool has_root_directory(const Twine &path, Style style) {
625 SmallString<128> path_storage;
626 StringRef p = path.toStringRef(path_storage);
627
628 return !root_directory(p, style).empty();
629}
630
631bool has_root_path(const Twine &path, Style style) {
632 SmallString<128> path_storage;
633 StringRef p = path.toStringRef(path_storage);
634
635 return !root_path(p, style).empty();
636}
637
638bool has_relative_path(const Twine &path, Style style) {
639 SmallString<128> path_storage;
640 StringRef p = path.toStringRef(path_storage);
641
642 return !relative_path(p, style).empty();
643}
644
645bool has_filename(const Twine &path, Style style) {
646 SmallString<128> path_storage;
647 StringRef p = path.toStringRef(path_storage);
648
649 return !filename(p, style).empty();
650}
651
652bool has_parent_path(const Twine &path, Style style) {
653 SmallString<128> path_storage;
654 StringRef p = path.toStringRef(path_storage);
655
656 return !parent_path(p, style).empty();
657}
658
659bool has_stem(const Twine &path, Style style) {
660 SmallString<128> path_storage;
661 StringRef p = path.toStringRef(path_storage);
662
663 return !stem(p, style).empty();
664}
665
666bool has_extension(const Twine &path, Style style) {
667 SmallString<128> path_storage;
668 StringRef p = path.toStringRef(path_storage);
669
670 return !extension(p, style).empty();
671}
672
673bool is_absolute(const Twine &path, Style style) {
674 SmallString<128> path_storage;
675 StringRef p = path.toStringRef(path_storage);
676
677 bool rootDir = has_root_directory(p, style);
678 bool rootName = is_style_posix(style) || has_root_name(p, style);
679
680 return rootDir && rootName;
681}
682
683bool is_absolute_gnu(const Twine &path, Style style) {
684 SmallString<128> path_storage;
685 StringRef p = path.toStringRef(path_storage);
686
687 // Handle '/' which is absolute for both Windows and POSIX systems.
688 // Handle '\\' on Windows.
689 if (!p.empty() && is_separator(p.front(), style))
690 return true;
691
692 if (is_style_windows(style)) {
693 // Handle drive letter pattern (a character followed by ':') on Windows.
694 if (p.size() >= 2 && (p[0] && p[1] == ':'))
695 return true;
696 }
697
698 return false;
699}
700
701bool is_relative(const Twine &path, Style style) {
702 return !is_absolute(path, style);
703}
704
706 // Remove leading "./" (or ".//" or "././" etc.)
707 while (Path.size() > 2 && Path[0] == '.' && is_separator(Path[1], style)) {
708 Path = Path.substr(2);
709 while (Path.size() > 0 && is_separator(Path[0], style))
710 Path = Path.substr(1);
711 }
712 return Path;
713}
714
715// Remove path traversal components ("." and "..") when possible, and
716// canonicalize slashes.
717bool remove_dots(SmallVectorImpl<char> &the_path, bool remove_dot_dot,
718 Style style) {
719 style = real_style(style);
720 StringRef remaining(the_path.data(), the_path.size());
721 bool needs_change = false;
723
724 // Consume the root path, if present.
725 StringRef root = path::root_path(remaining, style);
726 bool absolute = !root.empty();
727 if (absolute)
728 remaining = remaining.drop_front(root.size());
729
730 // Loop over path components manually. This makes it easier to detect
731 // non-preferred slashes and double separators that must be canonicalized.
732 while (!remaining.empty()) {
733 size_t next_slash = remaining.find_first_of(separators(style));
734 if (next_slash == StringRef::npos)
735 next_slash = remaining.size();
736 StringRef component = remaining.take_front(next_slash);
737 remaining = remaining.drop_front(next_slash);
738
739 // Eat the slash, and check if it is the preferred separator.
740 if (!remaining.empty()) {
741 needs_change |= remaining.front() != preferred_separator(style);
742 remaining = remaining.drop_front();
743 // The path needs to be rewritten if it has a trailing slash.
744 // FIXME: This is emergent behavior that could be removed.
745 needs_change |= remaining.empty();
746 }
747
748 // Check for path traversal components or double separators.
749 if (component.empty() || component == ".") {
750 needs_change = true;
751 } else if (remove_dot_dot && component == "..") {
752 needs_change = true;
753 // Do not allow ".." to remove the root component. If this is the
754 // beginning of a relative path, keep the ".." component.
755 if (!components.empty() && components.back() != "..") {
756 components.pop_back();
757 } else if (!absolute) {
758 components.push_back(component);
759 }
760 } else {
761 components.push_back(component);
762 }
763 }
764
765 SmallString<256> buffer = root;
766 // "root" could be "/", which may need to be translated into "\".
767 make_preferred(buffer, style);
768 needs_change |= root != buffer;
769
770 // Avoid rewriting the path unless we have to.
771 if (!needs_change)
772 return false;
773
774 if (!components.empty()) {
775 buffer += components[0];
776 for (StringRef C : ArrayRef(components).drop_front()) {
777 buffer += preferred_separator(style);
778 buffer += C;
779 }
780 }
781 the_path.swap(buffer);
782 return true;
783}
784
785} // end namespace path
786
787namespace fs {
788
789std::error_code getUniqueID(const Twine Path, UniqueID &Result) {
791 std::error_code EC = status(Path, Status);
792 if (EC)
793 return EC;
794 Result = Status.getUniqueID();
795 return std::error_code();
796}
797
798void createUniquePath(const Twine &Model, SmallVectorImpl<char> &ResultPath,
799 bool MakeAbsolute) {
800 SmallString<128> ModelStorage;
801 Model.toVector(ModelStorage);
802
803 if (MakeAbsolute) {
804 // Make model absolute by prepending a temp directory if it's not already.
805 if (!sys::path::is_absolute(Twine(ModelStorage))) {
806 SmallString<128> TDir;
808 sys::path::append(TDir, Twine(ModelStorage));
809 ModelStorage.swap(TDir);
810 }
811 }
812
813 ResultPath = ModelStorage;
814 ResultPath.push_back(0);
815 ResultPath.pop_back();
816
817 // Replace '%' with random chars.
818 for (unsigned i = 0, e = ModelStorage.size(); i != e; ++i) {
819 if (ModelStorage[i] == '%')
820 ResultPath[i] = "0123456789abcdef"[sys::Process::GetRandomNumber() & 15];
821 }
822}
823
824std::error_code createUniqueFile(const Twine &Model, int &ResultFd,
825 SmallVectorImpl<char> &ResultPath,
826 OpenFlags Flags, unsigned Mode) {
827 return createUniqueEntity(Model, ResultFd, ResultPath, false, FS_File, Flags,
828 Mode);
829}
830
831std::error_code createUniqueFile(const Twine &Model,
832 SmallVectorImpl<char> &ResultPath,
833 unsigned Mode) {
834 int FD;
835 auto EC = createUniqueFile(Model, FD, ResultPath, OF_None, Mode);
836 if (EC)
837 return EC;
838 // FD is only needed to avoid race conditions. Close it right away.
839 close(FD);
840 return EC;
841}
842
843static std::error_code
844createTemporaryFile(const Twine &Model, int &ResultFD,
847 SmallString<128> Storage;
848 StringRef P = Model.toNullTerminatedStringRef(Storage);
849 assert(P.find_first_of(separators(Style::native)) == StringRef::npos &&
850 "Model must be a simple filename.");
851 // Use P.begin() so that createUniqueEntity doesn't need to recreate Storage.
852 return createUniqueEntity(P.begin(), ResultFD, ResultPath, true, Type, Flags,
854}
855
856static std::error_code
857createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
860 const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
861 return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
862 Type, Flags);
863}
864
865std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
866 int &ResultFD,
867 SmallVectorImpl<char> &ResultPath,
868 sys::fs::OpenFlags Flags) {
869 return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File,
870 Flags);
871}
872
873std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
874 SmallVectorImpl<char> &ResultPath,
875 sys::fs::OpenFlags Flags) {
876 int FD;
877 auto EC = createTemporaryFile(Prefix, Suffix, FD, ResultPath, Flags);
878 if (EC)
879 return EC;
880 // FD is only needed to avoid race conditions. Close it right away.
881 close(FD);
882 return EC;
883}
884
885// This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
886// for consistency. We should try using mkdtemp.
887std::error_code createUniqueDirectory(const Twine &Prefix,
888 SmallVectorImpl<char> &ResultPath) {
889 int Dummy;
890 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath, true,
891 FS_Dir);
892}
893
894std::error_code
896 SmallVectorImpl<char> &ResultPath) {
897 int Dummy;
898 return createUniqueEntity(Model, Dummy, ResultPath, false, FS_Name);
899}
900
901std::error_code
903 SmallVectorImpl<char> &ResultPath) {
904 int Dummy;
905 return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
906}
907
908void make_absolute(const Twine &current_directory,
909 SmallVectorImpl<char> &path) {
910 StringRef p(path.data(), path.size());
911
912 bool rootDirectory = path::has_root_directory(p);
913 bool rootName = path::has_root_name(p);
914
915 // Already absolute.
916 if ((rootName || is_style_posix(Style::native)) && rootDirectory)
917 return;
918
919 // All of the following conditions will need the current directory.
920 SmallString<128> current_dir;
921 current_directory.toVector(current_dir);
922
923 // Relative path. Prepend the current directory.
924 if (!rootName && !rootDirectory) {
925 // Append path to the current directory.
926 path::append(current_dir, p);
927 // Set path to the result.
928 path.swap(current_dir);
929 return;
930 }
931
932 if (!rootName && rootDirectory) {
933 StringRef cdrn = path::root_name(current_dir);
934 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
935 path::append(curDirRootName, p);
936 // Set path to the result.
937 path.swap(curDirRootName);
938 return;
939 }
940
941 if (rootName && !rootDirectory) {
942 StringRef pRootName = path::root_name(p);
943 StringRef bRootDirectory = path::root_directory(current_dir);
944 StringRef bRelativePath = path::relative_path(current_dir);
945 StringRef pRelativePath = path::relative_path(p);
946
948 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
949 path.swap(res);
950 return;
951 }
952
953 llvm_unreachable("All rootName and rootDirectory combinations should have "
954 "occurred above!");
955}
956
957std::error_code make_absolute(SmallVectorImpl<char> &path) {
958 if (path::is_absolute(path))
959 return {};
960
961 SmallString<128> current_dir;
962 if (std::error_code ec = current_path(current_dir))
963 return ec;
964
965 make_absolute(current_dir, path);
966 return {};
967}
968
969std::error_code create_directories(const Twine &Path, bool IgnoreExisting,
970 perms Perms) {
971 SmallString<128> PathStorage;
972 StringRef P = Path.toStringRef(PathStorage);
973
974 // Be optimistic and try to create the directory
975 std::error_code EC = create_directory(P, IgnoreExisting, Perms);
976 // If we succeeded, or had any error other than the parent not existing, just
977 // return it.
979 return EC;
980
981 // We failed because of a no_such_file_or_directory, try to create the
982 // parent.
983 StringRef Parent = path::parent_path(P);
984 if (Parent.empty())
985 return EC;
986
987 if ((EC = create_directories(Parent, IgnoreExisting, Perms)))
988 return EC;
989
990 return create_directory(P, IgnoreExisting, Perms);
991}
992
993static std::error_code copy_file_internal(int ReadFD, int WriteFD) {
994 const size_t BufSize = 4096;
995 char *Buf = new char[BufSize];
996 int BytesRead = 0, BytesWritten = 0;
997 for (;;) {
998 BytesRead = read(ReadFD, Buf, BufSize);
999 if (BytesRead <= 0)
1000 break;
1001 while (BytesRead) {
1002 BytesWritten = write(WriteFD, Buf, BytesRead);
1003 if (BytesWritten < 0)
1004 break;
1005 BytesRead -= BytesWritten;
1006 }
1007 if (BytesWritten < 0)
1008 break;
1009 }
1010 delete[] Buf;
1011
1012 if (BytesRead < 0 || BytesWritten < 0)
1013 return std::error_code(errno, std::generic_category());
1014 return std::error_code();
1015}
1016
1017#ifndef __APPLE__
1018std::error_code copy_file(const Twine &From, const Twine &To) {
1019 int ReadFD, WriteFD;
1020 if (std::error_code EC = openFileForRead(From, ReadFD, OF_None))
1021 return EC;
1022 if (std::error_code EC =
1023 openFileForWrite(To, WriteFD, CD_CreateAlways, OF_None)) {
1024 close(ReadFD);
1025 return EC;
1026 }
1027
1028 std::error_code EC = copy_file_internal(ReadFD, WriteFD);
1029
1030 close(ReadFD);
1031 close(WriteFD);
1032
1033 return EC;
1034}
1035#endif
1036
1037std::error_code copy_file(const Twine &From, int ToFD) {
1038 int ReadFD;
1039 if (std::error_code EC = openFileForRead(From, ReadFD, OF_None))
1040 return EC;
1041
1042 std::error_code EC = copy_file_internal(ReadFD, ToFD);
1043
1044 close(ReadFD);
1045
1046 return EC;
1047}
1048
1050 MD5 Hash;
1051
1052 constexpr size_t BufSize = 4096;
1053 std::vector<uint8_t> Buf(BufSize);
1054 int BytesRead = 0;
1055 for (;;) {
1056 BytesRead = read(FD, Buf.data(), BufSize);
1057 if (BytesRead <= 0)
1058 break;
1059 Hash.update(ArrayRef(Buf.data(), BytesRead));
1060 }
1061
1062 if (BytesRead < 0)
1063 return std::error_code(errno, std::generic_category());
1064 MD5::MD5Result Result;
1065 Hash.final(Result);
1066 return Result;
1067}
1068
1070 int FD;
1071 if (auto EC = openFileForRead(Path, FD, OF_None))
1072 return EC;
1073
1074 auto Result = md5_contents(FD);
1075 close(FD);
1076 return Result;
1077}
1078
1081}
1082
1084 return s.type() != file_type::status_error;
1085}
1086
1087file_type get_file_type(const Twine &Path, bool Follow) {
1088 file_status st;
1089 if (status(Path, st, Follow))
1091 return st.type();
1092}
1093
1095 return status.type() == file_type::directory_file;
1096}
1097
1098std::error_code is_directory(const Twine &path, bool &result) {
1099 file_status st;
1100 if (std::error_code ec = status(path, st))
1101 return ec;
1102 result = is_directory(st);
1103 return std::error_code();
1104}
1105
1107 return status.type() == file_type::regular_file;
1108}
1109
1110std::error_code is_regular_file(const Twine &path, bool &result) {
1111 file_status st;
1112 if (std::error_code ec = status(path, st))
1113 return ec;
1114 result = is_regular_file(st);
1115 return std::error_code();
1116}
1117
1119 return status.type() == file_type::symlink_file;
1120}
1121
1122std::error_code is_symlink_file(const Twine &path, bool &result) {
1123 file_status st;
1124 if (std::error_code ec = status(path, st, false))
1125 return ec;
1126 result = is_symlink_file(st);
1127 return std::error_code();
1128}
1129
1131 return exists(status) &&
1134}
1135
1136std::error_code is_other(const Twine &Path, bool &Result) {
1137 file_status FileStatus;
1138 if (std::error_code EC = status(Path, FileStatus))
1139 return EC;
1140 Result = is_other(FileStatus);
1141 return std::error_code();
1142}
1143
1146 SmallString<128> PathStr = path::parent_path(Path);
1147 path::append(PathStr, Filename);
1148 this->Path = std::string(PathStr);
1149 this->Type = Type;
1150 this->Status = Status;
1151}
1152
1155 if (std::error_code EC = status(Path, Status))
1156 return EC;
1157
1158 return Status.permissions();
1159}
1160
1162 assert(Mapping && "Mapping failed but used anyway!");
1163 return Size;
1164}
1165
1167 assert(Mapping && "Mapping failed but used anyway!");
1168 return reinterpret_cast<char *>(Mapping);
1169}
1170
1172 assert(Mapping && "Mapping failed but used anyway!");
1173 return reinterpret_cast<const char *>(Mapping);
1174}
1175
1177 ssize_t ChunkSize) {
1178 // Install a handler to truncate the buffer to the correct size on exit.
1179 size_t Size = Buffer.size();
1180 auto TruncateOnExit = make_scope_exit([&]() { Buffer.truncate(Size); });
1181
1182 // Read into Buffer until we hit EOF.
1183 for (;;) {
1184 Buffer.resize_for_overwrite(Size + ChunkSize);
1185 Expected<size_t> ReadBytes = readNativeFile(
1186 FileHandle, MutableArrayRef(Buffer.begin() + Size, ChunkSize));
1187 if (!ReadBytes)
1188 return ReadBytes.takeError();
1189 if (*ReadBytes == 0)
1190 return Error::success();
1191 Size += *ReadBytes;
1192 }
1193}
1194
1195} // end namespace fs
1196} // end namespace sys
1197} // end namespace llvm
1198
1199// Include the truly platform-specific parts.
1200#if defined(LLVM_ON_UNIX)
1201#include "Unix/Path.inc"
1202#endif
1203#if defined(_WIN32)
1204#include "Windows/Path.inc"
1205#endif
1206
1207namespace llvm {
1208namespace sys {
1209namespace fs {
1210
1211TempFile::TempFile(StringRef Name, int FD)
1212 : TmpName(std::string(Name)), FD(FD) {}
1213TempFile::TempFile(TempFile &&Other) { *this = std::move(Other); }
1214TempFile &TempFile::operator=(TempFile &&Other) {
1215 TmpName = std::move(Other.TmpName);
1216 FD = Other.FD;
1217 Other.Done = true;
1218 Other.FD = -1;
1219#ifdef _WIN32
1220 RemoveOnClose = Other.RemoveOnClose;
1221 Other.RemoveOnClose = false;
1222#endif
1223 return *this;
1224}
1225
1226TempFile::~TempFile() { assert(Done); }
1227
1228Error TempFile::discard() {
1229 Done = true;
1230 if (FD != -1 && close(FD) == -1) {
1231 std::error_code EC = std::error_code(errno, std::generic_category());
1232 return errorCodeToError(EC);
1233 }
1234 FD = -1;
1235
1236#ifdef _WIN32
1237 // On Windows, closing will remove the file, if we set the delete
1238 // disposition. If not, remove it manually.
1239 bool Remove = RemoveOnClose;
1240#else
1241 // Always try to remove the file.
1242 bool Remove = true;
1243#endif
1244 std::error_code RemoveEC;
1245 if (Remove && !TmpName.empty()) {
1246 RemoveEC = fs::remove(TmpName);
1248 if (!RemoveEC)
1249 TmpName = "";
1250 } else {
1251 TmpName = "";
1252 }
1253 return errorCodeToError(RemoveEC);
1254}
1255
1256Error TempFile::keep(const Twine &Name) {
1257 assert(!Done);
1258 Done = true;
1259 // Always try to close and rename.
1260#ifdef _WIN32
1261 // If we can't cancel the delete don't rename.
1262 auto H = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
1263 std::error_code RenameEC =
1264 RemoveOnClose ? std::error_code() : setDeleteDisposition(H, false);
1265 bool ShouldDelete = false;
1266 if (!RenameEC) {
1267 RenameEC = rename_handle(H, Name);
1268 // If rename failed because it's cross-device, copy instead
1269 if (RenameEC ==
1270 std::error_code(ERROR_NOT_SAME_DEVICE, std::system_category())) {
1271 RenameEC = copy_file(TmpName, Name);
1272 ShouldDelete = true;
1273 }
1274 }
1275
1276 // If we can't rename or copy, discard the temporary file.
1277 if (RenameEC)
1278 ShouldDelete = true;
1279 if (ShouldDelete) {
1280 if (!RemoveOnClose)
1281 setDeleteDisposition(H, true);
1282 else
1283 remove(TmpName);
1284 }
1285#else
1286 std::error_code RenameEC = fs::rename(TmpName, Name);
1287 if (RenameEC) {
1288 // If we can't rename, try to copy to work around cross-device link issues.
1289 RenameEC = sys::fs::copy_file(TmpName, Name);
1290 // If we can't rename or copy, discard the temporary file.
1291 if (RenameEC)
1292 remove(TmpName);
1293 }
1294#endif
1296
1297 if (!RenameEC)
1298 TmpName = "";
1299
1300 if (close(FD) == -1) {
1301 std::error_code EC(errno, std::generic_category());
1302 return errorCodeToError(EC);
1303 }
1304 FD = -1;
1305
1306 return errorCodeToError(RenameEC);
1307}
1308
1309Error TempFile::keep() {
1310 assert(!Done);
1311 Done = true;
1312
1313#ifdef _WIN32
1314 auto H = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
1315 if (std::error_code EC = setDeleteDisposition(H, false))
1316 return errorCodeToError(EC);
1317#endif
1319
1320 TmpName = "";
1321
1322 if (close(FD) == -1) {
1323 std::error_code EC(errno, std::generic_category());
1324 return errorCodeToError(EC);
1325 }
1326 FD = -1;
1327
1328 return Error::success();
1329}
1330
1331Expected<TempFile> TempFile::create(const Twine &Model, unsigned Mode,
1332 OpenFlags ExtraFlags) {
1333 int FD;
1334 SmallString<128> ResultPath;
1335 if (std::error_code EC =
1336 createUniqueFile(Model, FD, ResultPath, OF_Delete | ExtraFlags, Mode))
1337 return errorCodeToError(EC);
1338
1339 TempFile Ret(ResultPath, FD);
1340#ifdef _WIN32
1341 auto H = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
1342 bool SetSignalHandler = false;
1343 if (std::error_code EC = setDeleteDisposition(H, true)) {
1344 Ret.RemoveOnClose = true;
1345 SetSignalHandler = true;
1346 }
1347#else
1348 bool SetSignalHandler = true;
1349#endif
1350 if (SetSignalHandler && sys::RemoveFileOnSignal(ResultPath)) {
1351 // Make sure we delete the file when RemoveFileOnSignal fails.
1352 consumeError(Ret.discard());
1353 std::error_code EC(errc::operation_not_permitted);
1354 return errorCodeToError(EC);
1355 }
1356 return std::move(Ret);
1357}
1358} // namespace fs
1359
1360} // namespace sys
1361} // namespace llvm
BlockVerifier::State From
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Given that RA is a live value
std::string Name
uint64_t Size
#define I(x, y, z)
Definition: MD5.cpp:58
#define H(x, y, z)
Definition: MD5.cpp:57
#define P(N)
static std::error_code createUniqueEntity(const Twine &Model, int &ResultFD, SmallVectorImpl< char > &ResultPath, bool MakeAbsolute, FSEntity Type, sys::fs::OpenFlags Flags=sys::fs::OF_None, unsigned Mode=0)
Definition: Path.cpp:172
FSEntity
Definition: Path.cpp:165
@ FS_Dir
Definition: Path.cpp:166
@ FS_File
Definition: Path.cpp:167
@ FS_Name
Definition: Path.cpp:168
Provides a library for accessing information about this process and other processes on the operating ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file contains some functions that are useful when dealing with strings.
DEMANGLE_NAMESPACE_BEGIN bool starts_with(std::string_view self, char C) noexcept
Value * RHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Represents either an error or a value T.
Definition: ErrorOr.h:56
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
Definition: MD5.h:41
void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
Definition: MD5.cpp:189
void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
Definition: MD5.cpp:234
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
void append(StringRef RHS)
Append from a StringRef.
Definition: SmallString.h:68
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void resize_for_overwrite(size_type N)
Like resize, but T is POD, the new values won't be initialized.
Definition: SmallVector.h:654
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:696
void truncate(size_type N)
Like resize, but requires that N is less than size().
Definition: SmallVector.h:657
void swap(SmallVectorImpl &RHS)
Definition: SmallVector.h:981
void push_back(const T &Elt)
Definition: SmallVector.h:426
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:299
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:222
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:567
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:605
iterator begin() const
Definition: StringRef.h:111
char back() const
back - Get the last character in the string.
Definition: StringRef.h:146
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:680
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
char front() const
front - Get the first character in the string.
Definition: StringRef.h:140
size_t find_last_of(char C, size_t From=npos) const
Find the last character in the string that is C, or npos if not found.
Definition: StringRef.h:396
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition: StringRef.h:373
iterator end() const
Definition: StringRef.h:113
StringRef take_front(size_t N=1) const
Return a StringRef equal to 'this' but with only the first N elements remaining.
Definition: StringRef.h:576
bool ends_with(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:271
static constexpr size_t npos
Definition: StringRef.h:52
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
bool isSingleStringRef() const
Return true if this twine can be dynamically accessed as a single StringRef value with getSingleStrin...
Definition: Twine.h:440
bool isTriviallyEmpty() const
Check if this twine is trivially empty; a false return value does not necessarily mean the twine is e...
Definition: Twine.h:429
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:492
StringRef getSingleStringRef() const
This returns the twine as a single StringRef.
Definition: Twine.h:473
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Definition: Twine.cpp:32
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static unsigned GetRandomNumber()
Get the result of a process wide random number generator.
Represents a temporary file.
Definition: FileSystem.h:848
Represents the result of a call to directory_iterator::status().
Definition: FileSystem.h:137
void replace_filename(const Twine &Filename, file_type Type, basic_file_status Status=basic_file_status())
Definition: Path.cpp:1144
Represents the result of a call to sys::fs::status().
Definition: FileSystem.h:226
const char * const_data() const
Get a const view of the data.
Definition: Path.cpp:1171
const_iterator & operator++()
Definition: Path.cpp:244
bool operator==(const const_iterator &RHS) const
Definition: Path.cpp:291
ptrdiff_t operator-(const const_iterator &RHS) const
Difference in bytes between this and RHS.
Definition: Path.cpp:295
Reverse path iterator.
Definition: Path.h:101
bool operator==(const reverse_iterator &RHS) const
Definition: Path.cpp:341
ptrdiff_t operator-(const reverse_iterator &RHS) const
Difference in bytes between this and RHS.
Definition: Path.cpp:346
reverse_iterator & operator++()
Definition: Path.cpp:316
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
Definition: Endian.h:58
bool is_regular_file(const basic_file_status &status)
Does status represent a regular file?
Definition: Path.cpp:1106
bool is_symlink_file(const basic_file_status &status)
Does status represent a symlink file?
Definition: Path.cpp:1118
void make_absolute(const Twine &current_directory, SmallVectorImpl< char > &path)
Make path an absolute path.
Definition: Path.cpp:908
std::error_code openFileForReadWrite(const Twine &Name, int &ResultFD, CreationDisposition Disp, OpenFlags Flags, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
Definition: FileSystem.h:1104
Error readNativeFileToEOF(file_t FileHandle, SmallVectorImpl< char > &Buffer, ssize_t ChunkSize=DefaultReadChunkSize)
Reads from FileHandle until EOF, appending to Buffer in chunks of size ChunkSize.
Definition: Path.cpp:1176
Expected< size_t > readNativeFile(file_t FileHandle, MutableArrayRef< char > Buf)
Reads Buf.size() bytes from FileHandle into Buf.
ErrorOr< perms > getPermissions(const Twine &Path)
Get file permissions.
Definition: Path.cpp:1153
std::error_code getPotentiallyUniqueFileName(const Twine &Model, SmallVectorImpl< char > &ResultPath)
Get a unique name, not currently exisiting in the filesystem.
Definition: Path.cpp:895
std::error_code rename(const Twine &from, const Twine &to)
Rename from to to.
std::error_code openFileForRead(const Twine &Name, int &ResultFD, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
bool is_other(const basic_file_status &status)
Does this status represent something that exists but is not a directory or regular file?
Definition: Path.cpp:1130
std::error_code access(const Twine &Path, AccessMode Mode)
Can the file be accessed?
std::error_code getPotentiallyUniqueTempFileName(const Twine &Prefix, StringRef Suffix, SmallVectorImpl< char > &ResultPath)
Get a unique temporary file name, not currently exisiting in the filesystem.
Definition: Path.cpp:902
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
bool exists(const basic_file_status &status)
Does file exist?
Definition: Path.cpp:1079
@ OF_Delete
The returned handle can be used for deleting the file.
Definition: FileSystem.h:775
file_type
An enumeration for the file system's view of the type.
Definition: FileSystem.h:66
std::error_code getUniqueID(const Twine Path, UniqueID &Result)
Definition: Path.cpp:789
std::error_code createUniqueFile(const Twine &Model, int &ResultFD, SmallVectorImpl< char > &ResultPath, OpenFlags Flags=OF_None, unsigned Mode=all_read|all_write)
Create a uniquely named file.
Definition: Path.cpp:824
std::error_code create_directory(const Twine &path, bool IgnoreExisting=true, perms Perms=owner_all|group_all)
Create the directory in path.
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
@ CD_CreateAlways
CD_CreateAlways - When opening a file:
Definition: FileSystem.h:731
@ CD_CreateNew
CD_CreateNew - When opening a file:
Definition: FileSystem.h:736
void createUniquePath(const Twine &Model, SmallVectorImpl< char > &ResultPath, bool MakeAbsolute)
Create a potentially unique file name but does not create it.
Definition: Path.cpp:798
std::error_code openFileForWrite(const Twine &Name, int &ResultFD, CreationDisposition Disp=CD_CreateAlways, OpenFlags Flags=OF_None, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
Definition: FileSystem.h:1063
std::error_code create_directories(const Twine &path, bool IgnoreExisting=true, perms Perms=owner_all|group_all)
Create all the non-existent directories in path.
Definition: Path.cpp:969
bool status_known(const basic_file_status &s)
Is status available?
Definition: Path.cpp:1083
std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, SmallVectorImpl< char > &ResultPath, OpenFlags Flags=OF_None)
Create a file in the system temporary directory.
Definition: Path.cpp:865
file_type get_file_type(const Twine &Path, bool Follow=true)
Does status represent a directory?
Definition: Path.cpp:1087
std::error_code copy_file(const Twine &From, const Twine &To)
Copy the contents of From to To.
Definition: Path.cpp:1018
std::error_code createUniqueDirectory(const Twine &Prefix, SmallVectorImpl< char > &ResultPath)
Definition: Path.cpp:887
ErrorOr< MD5::MD5Result > md5_contents(int FD)
Compute an MD5 hash of a file's contents.
Definition: Path.cpp:1049
std::error_code current_path(SmallVectorImpl< char > &result)
Get the current path.
static std::error_code copy_file_internal(int ReadFD, int WriteFD)
Definition: Path.cpp:993
bool is_directory(const basic_file_status &status)
Does status represent a directory?
Definition: Path.cpp:1094
StringRef get_separator(Style style=Style::native)
Return the preferred separator for this platform.
Definition: Path.cpp:611
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:228
void remove_filename(SmallVectorImpl< char > &path, Style style=Style::native)
Remove the last component from path unless it is the root dir.
Definition: Path.cpp:476
bool has_relative_path(const Twine &path, Style style=Style::native)
Has relative path?
Definition: Path.cpp:638
bool home_directory(SmallVectorImpl< char > &result)
Get the user's home directory.
StringRef stem(StringRef path, Style style=Style::native)
Get stem.
Definition: Path.cpp:581
reverse_iterator rend(StringRef path)
Get reverse end iterator over path.
Definition: Path.cpp:308
bool has_root_name(const Twine &path, Style style=Style::native)
Has root name?
Definition: Path.cpp:617
void replace_extension(SmallVectorImpl< char > &path, const Twine &extension, Style style=Style::native)
Replace the file extension of path with extension.
Definition: Path.cpp:482
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:237
bool remove_dots(SmallVectorImpl< char > &path, bool remove_dot_dot=false, Style style=Style::native)
In-place remove any '.
Definition: Path.cpp:717
bool has_root_path(const Twine &path, Style style=Style::native)
Has root path?
Definition: Path.cpp:631
constexpr bool is_style_posix(Style S)
Check if S uses POSIX path rules.
Definition: Path.h:36
StringRef root_name(StringRef path, Style style=Style::native)
Get root name.
Definition: Path.cpp:375
StringRef root_directory(StringRef path, Style style=Style::native)
Get root directory.
Definition: Path.cpp:392
bool has_parent_path(const Twine &path, Style style=Style::native)
Has parent path?
Definition: Path.cpp:652
void make_preferred(SmallVectorImpl< char > &path, Style style=Style::native)
For Windows path styles, convert path to use the preferred path separators.
Definition: Path.h:269
bool is_relative(const Twine &path, Style style=Style::native)
Is path relative?
Definition: Path.cpp:701
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:579
reverse_iterator rbegin(StringRef path, Style style=Style::native)
Get reverse begin iterator over path.
Definition: Path.cpp:299
bool has_extension(const Twine &path, Style style=Style::native)
Has extension?
Definition: Path.cpp:666
bool is_absolute_gnu(const Twine &path, Style style=Style::native)
Is path absolute using GNU rules?
Definition: Path.cpp:683
void system_temp_directory(bool erasedOnReboot, SmallVectorImpl< char > &result)
Get the typical temporary directory for the system, e.g., "/var/tmp" or "C:/TEMP".
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
Definition: Path.cpp:469
std::string convert_to_slash(StringRef path, Style style=Style::native)
Replaces backslashes with slashes if Windows.
Definition: Path.cpp:570
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition: Path.cpp:673
bool has_stem(const Twine &path, Style style=Style::native)
Has stem?
Definition: Path.cpp:659
constexpr bool is_style_windows(Style S)
Check if S uses Windows path rules.
Definition: Path.h:49
bool replace_path_prefix(SmallVectorImpl< char > &Path, StringRef OldPrefix, StringRef NewPrefix, Style style=Style::native)
Replace matching path prefix with another path.
Definition: Path.cpp:520
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:458
StringRef extension(StringRef path, Style style=Style::native)
Get extension.
Definition: Path.cpp:592
bool has_filename(const Twine &path, Style style=Style::native)
Has filename?
Definition: Path.cpp:645
StringRef root_path(StringRef path, Style style=Style::native)
Get root path.
Definition: Path.cpp:350
StringRef relative_path(StringRef path, Style style=Style::native)
Get relative path.
Definition: Path.cpp:415
bool is_separator(char value, Style style=Style::native)
Check whether the given char is a path separator on the host OS.
Definition: Path.cpp:603
StringRef remove_leading_dotslash(StringRef path, Style style=Style::native)
Remove redundant leading "./" pieces and consecutive separators.
Definition: Path.cpp:705
bool has_root_directory(const Twine &path, Style style=Style::native)
Has root directory?
Definition: Path.cpp:624
void DontRemoveFileOnSignal(StringRef Filename)
This function removes a file from the list of files to be removed on signal delivery.
bool RemoveFileOnSignal(StringRef Filename, std::string *ErrMsg=nullptr)
This function registers signal handlers to ensure that if a signal gets delivered that the named file...
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
@ Done
Definition: Threading.h:61
@ no_such_file_or_directory
@ operation_not_permitted
Error write(MCStreamer &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue)
Definition: DWP.cpp:601
@ Other
Any other memory.
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1833
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:103
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1041
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858