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

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of llvm/lib/Support/Path.cpp