[ARM] MVE integer min and max
[llvm-complete.git] / lib / Support / Path.cpp
blobc49260125dba629a00a59434be512ae53a571221
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.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/Path.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/Config/llvm-config.h"
16 #include "llvm/Support/Endian.h"
17 #include "llvm/Support/Errc.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/Process.h"
21 #include "llvm/Support/Signals.h"
22 #include <cctype>
23 #include <cstring>
25 #if !defined(_MSC_VER) && !defined(__MINGW32__)
26 #include <unistd.h>
27 #else
28 #include <io.h>
29 #endif
31 using namespace llvm;
32 using namespace llvm::support::endian;
34 namespace {
35 using llvm::StringRef;
36 using llvm::sys::path::is_separator;
37 using llvm::sys::path::Style;
39 inline Style real_style(Style style) {
40 #ifdef _WIN32
41 return (style == Style::posix) ? Style::posix : Style::windows;
42 #else
43 return (style == Style::windows) ? Style::windows : Style::posix;
44 #endif
47 inline const char *separators(Style style) {
48 if (real_style(style) == Style::windows)
49 return "\\/";
50 return "/";
53 inline char preferred_separator(Style style) {
54 if (real_style(style) == Style::windows)
55 return '\\';
56 return '/';
59 StringRef find_first_component(StringRef path, Style style) {
60 // Look for this first component in the following order.
61 // * empty (in this case we return an empty string)
62 // * either C: or {//,\\}net.
63 // * {/,\}
64 // * {file,directory}name
66 if (path.empty())
67 return path;
69 if (real_style(style) == Style::windows) {
70 // C:
71 if (path.size() >= 2 &&
72 std::isalpha(static_cast<unsigned char>(path[0])) && path[1] == ':')
73 return path.substr(0, 2);
76 // //net
77 if ((path.size() > 2) && is_separator(path[0], style) &&
78 path[0] == path[1] && !is_separator(path[2], style)) {
79 // Find the next directory separator.
80 size_t end = path.find_first_of(separators(style), 2);
81 return path.substr(0, end);
84 // {/,\}
85 if (is_separator(path[0], style))
86 return path.substr(0, 1);
88 // * {file,directory}name
89 size_t end = path.find_first_of(separators(style));
90 return path.substr(0, end);
93 // Returns the first character of the filename in str. For paths ending in
94 // '/', it returns the position of the '/'.
95 size_t filename_pos(StringRef str, Style style) {
96 if (str.size() > 0 && is_separator(str[str.size() - 1], style))
97 return str.size() - 1;
99 size_t pos = str.find_last_of(separators(style), str.size() - 1);
101 if (real_style(style) == Style::windows) {
102 if (pos == StringRef::npos)
103 pos = str.find_last_of(':', str.size() - 2);
106 if (pos == StringRef::npos || (pos == 1 && is_separator(str[0], style)))
107 return 0;
109 return pos + 1;
112 // Returns the position of the root directory in str. If there is no root
113 // directory in str, it returns StringRef::npos.
114 size_t root_dir_start(StringRef str, Style style) {
115 // case "c:/"
116 if (real_style(style) == Style::windows) {
117 if (str.size() > 2 && str[1] == ':' && is_separator(str[2], style))
118 return 2;
121 // case "//net"
122 if (str.size() > 3 && is_separator(str[0], style) && str[0] == str[1] &&
123 !is_separator(str[2], style)) {
124 return str.find_first_of(separators(style), 2);
127 // case "/"
128 if (str.size() > 0 && is_separator(str[0], style))
129 return 0;
131 return StringRef::npos;
134 // Returns the position past the end of the "parent path" of path. The parent
135 // path will not end in '/', unless the parent is the root directory. If the
136 // path has no parent, 0 is returned.
137 size_t parent_path_end(StringRef path, Style style) {
138 size_t end_pos = filename_pos(path, style);
140 bool filename_was_sep =
141 path.size() > 0 && is_separator(path[end_pos], style);
143 // Skip separators until we reach root dir (or the start of the string).
144 size_t root_dir_pos = root_dir_start(path, style);
145 while (end_pos > 0 &&
146 (root_dir_pos == StringRef::npos || end_pos > root_dir_pos) &&
147 is_separator(path[end_pos - 1], style))
148 --end_pos;
150 if (end_pos == root_dir_pos && !filename_was_sep) {
151 // We've reached the root dir and the input path was *not* ending in a
152 // sequence of slashes. Include the root dir in the parent path.
153 return root_dir_pos + 1;
156 // Otherwise, just include before the last slash.
157 return end_pos;
159 } // end unnamed namespace
161 enum FSEntity {
162 FS_Dir,
163 FS_File,
164 FS_Name
167 static std::error_code
168 createUniqueEntity(const Twine &Model, int &ResultFD,
169 SmallVectorImpl<char> &ResultPath, bool MakeAbsolute,
170 unsigned Mode, FSEntity Type,
171 sys::fs::OpenFlags Flags = sys::fs::OF_None) {
173 // Limit the number of attempts we make, so that we don't infinite loop. E.g.
174 // "permission denied" could be for a specific file (so we retry with a
175 // different name) or for the whole directory (retry would always fail).
176 // Checking which is racy, so we try a number of times, then give up.
177 std::error_code EC;
178 for (int Retries = 128; Retries > 0; --Retries) {
179 sys::fs::createUniquePath(Model, ResultPath, MakeAbsolute);
180 // Try to open + create the file.
181 switch (Type) {
182 case FS_File: {
183 EC = sys::fs::openFileForReadWrite(Twine(ResultPath.begin()), ResultFD,
184 sys::fs::CD_CreateNew, Flags, Mode);
185 if (EC) {
186 // errc::permission_denied happens on Windows when we try to open a file
187 // that has been marked for deletion.
188 if (EC == errc::file_exists || EC == errc::permission_denied)
189 continue;
190 return EC;
193 return std::error_code();
196 case FS_Name: {
197 EC = sys::fs::access(ResultPath.begin(), sys::fs::AccessMode::Exist);
198 if (EC == errc::no_such_file_or_directory)
199 return std::error_code();
200 if (EC)
201 return EC;
202 continue;
205 case FS_Dir: {
206 EC = sys::fs::create_directory(ResultPath.begin(), false);
207 if (EC) {
208 if (EC == errc::file_exists)
209 continue;
210 return EC;
212 return std::error_code();
215 llvm_unreachable("Invalid Type");
217 return EC;
220 namespace llvm {
221 namespace sys {
222 namespace path {
224 const_iterator begin(StringRef path, Style style) {
225 const_iterator i;
226 i.Path = path;
227 i.Component = find_first_component(path, style);
228 i.Position = 0;
229 i.S = style;
230 return i;
233 const_iterator end(StringRef path) {
234 const_iterator i;
235 i.Path = path;
236 i.Position = path.size();
237 return i;
240 const_iterator &const_iterator::operator++() {
241 assert(Position < Path.size() && "Tried to increment past end!");
243 // Increment Position to past the current component
244 Position += Component.size();
246 // Check for end.
247 if (Position == Path.size()) {
248 Component = StringRef();
249 return *this;
252 // Both POSIX and Windows treat paths that begin with exactly two separators
253 // specially.
254 bool was_net = Component.size() > 2 && is_separator(Component[0], S) &&
255 Component[1] == Component[0] && !is_separator(Component[2], S);
257 // Handle separators.
258 if (is_separator(Path[Position], S)) {
259 // Root dir.
260 if (was_net ||
261 // c:/
262 (real_style(S) == Style::windows && Component.endswith(":"))) {
263 Component = Path.substr(Position, 1);
264 return *this;
267 // Skip extra separators.
268 while (Position != Path.size() && is_separator(Path[Position], S)) {
269 ++Position;
272 // Treat trailing '/' as a '.', unless it is the root dir.
273 if (Position == Path.size() && Component != "/") {
274 --Position;
275 Component = ".";
276 return *this;
280 // Find next component.
281 size_t end_pos = Path.find_first_of(separators(S), Position);
282 Component = Path.slice(Position, end_pos);
284 return *this;
287 bool const_iterator::operator==(const const_iterator &RHS) const {
288 return Path.begin() == RHS.Path.begin() && Position == RHS.Position;
291 ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
292 return Position - RHS.Position;
295 reverse_iterator rbegin(StringRef Path, Style style) {
296 reverse_iterator I;
297 I.Path = Path;
298 I.Position = Path.size();
299 I.S = style;
300 ++I;
301 return I;
304 reverse_iterator rend(StringRef Path) {
305 reverse_iterator I;
306 I.Path = Path;
307 I.Component = Path.substr(0, 0);
308 I.Position = 0;
309 return I;
312 reverse_iterator &reverse_iterator::operator++() {
313 size_t root_dir_pos = root_dir_start(Path, S);
315 // Skip separators unless it's the root directory.
316 size_t end_pos = Position;
317 while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
318 is_separator(Path[end_pos - 1], S))
319 --end_pos;
321 // Treat trailing '/' as a '.', unless it is the root dir.
322 if (Position == Path.size() && !Path.empty() &&
323 is_separator(Path.back(), S) &&
324 (root_dir_pos == StringRef::npos || end_pos - 1 > root_dir_pos)) {
325 --Position;
326 Component = ".";
327 return *this;
330 // Find next separator.
331 size_t start_pos = filename_pos(Path.substr(0, end_pos), S);
332 Component = Path.slice(start_pos, end_pos);
333 Position = start_pos;
334 return *this;
337 bool reverse_iterator::operator==(const reverse_iterator &RHS) const {
338 return Path.begin() == RHS.Path.begin() && Component == RHS.Component &&
339 Position == RHS.Position;
342 ptrdiff_t reverse_iterator::operator-(const reverse_iterator &RHS) const {
343 return Position - RHS.Position;
346 StringRef root_path(StringRef path, Style style) {
347 const_iterator b = begin(path, style), pos = b, e = end(path);
348 if (b != e) {
349 bool has_net =
350 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
351 bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
353 if (has_net || has_drive) {
354 if ((++pos != e) && is_separator((*pos)[0], style)) {
355 // {C:/,//net/}, so get the first two components.
356 return path.substr(0, b->size() + pos->size());
357 } else {
358 // just {C:,//net}, return the first component.
359 return *b;
363 // POSIX style root directory.
364 if (is_separator((*b)[0], style)) {
365 return *b;
369 return StringRef();
372 StringRef root_name(StringRef path, Style style) {
373 const_iterator b = begin(path, style), e = end(path);
374 if (b != e) {
375 bool has_net =
376 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
377 bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
379 if (has_net || has_drive) {
380 // just {C:,//net}, return the first component.
381 return *b;
385 // No path or no name.
386 return StringRef();
389 StringRef root_directory(StringRef path, Style style) {
390 const_iterator b = begin(path, style), pos = b, e = end(path);
391 if (b != e) {
392 bool has_net =
393 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
394 bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
396 if ((has_net || has_drive) &&
397 // {C:,//net}, skip to the next component.
398 (++pos != e) && is_separator((*pos)[0], style)) {
399 return *pos;
402 // POSIX style root directory.
403 if (!has_net && is_separator((*b)[0], style)) {
404 return *b;
408 // No path or no root.
409 return StringRef();
412 StringRef relative_path(StringRef path, Style style) {
413 StringRef root = root_path(path, style);
414 return path.substr(root.size());
417 void append(SmallVectorImpl<char> &path, Style style, const Twine &a,
418 const Twine &b, const Twine &c, const Twine &d) {
419 SmallString<32> a_storage;
420 SmallString<32> b_storage;
421 SmallString<32> c_storage;
422 SmallString<32> d_storage;
424 SmallVector<StringRef, 4> components;
425 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
426 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
427 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
428 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
430 for (auto &component : components) {
431 bool path_has_sep =
432 !path.empty() && is_separator(path[path.size() - 1], style);
433 if (path_has_sep) {
434 // Strip separators from beginning of component.
435 size_t loc = component.find_first_not_of(separators(style));
436 StringRef c = component.substr(loc);
438 // Append it.
439 path.append(c.begin(), c.end());
440 continue;
443 bool component_has_sep =
444 !component.empty() && is_separator(component[0], style);
445 if (!component_has_sep &&
446 !(path.empty() || has_root_name(component, style))) {
447 // Add a separator.
448 path.push_back(preferred_separator(style));
451 path.append(component.begin(), component.end());
455 void append(SmallVectorImpl<char> &path, const Twine &a, const Twine &b,
456 const Twine &c, const Twine &d) {
457 append(path, Style::native, a, b, c, d);
460 void append(SmallVectorImpl<char> &path, const_iterator begin,
461 const_iterator end, Style style) {
462 for (; begin != end; ++begin)
463 path::append(path, style, *begin);
466 StringRef parent_path(StringRef path, Style style) {
467 size_t end_pos = parent_path_end(path, style);
468 if (end_pos == StringRef::npos)
469 return StringRef();
470 else
471 return path.substr(0, end_pos);
474 void remove_filename(SmallVectorImpl<char> &path, Style style) {
475 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()), style);
476 if (end_pos != StringRef::npos)
477 path.set_size(end_pos);
480 void 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(ext_storage);
486 // Erase existing extension.
487 size_t pos = p.find_last_of('.');
488 if (pos != StringRef::npos && pos >= filename_pos(p, style))
489 path.set_size(pos);
491 // Append '.' if needed.
492 if (ext.size() > 0 && ext[0] != '.')
493 path.push_back('.');
495 // Append extension.
496 path.append(ext.begin(), ext.end());
499 void replace_path_prefix(SmallVectorImpl<char> &Path,
500 const StringRef &OldPrefix, const StringRef &NewPrefix,
501 Style style) {
502 if (OldPrefix.empty() && NewPrefix.empty())
503 return;
505 StringRef OrigPath(Path.begin(), Path.size());
506 if (!OrigPath.startswith(OldPrefix))
507 return;
509 // If prefixes have the same size we can simply copy the new one over.
510 if (OldPrefix.size() == NewPrefix.size()) {
511 llvm::copy(NewPrefix, Path.begin());
512 return;
515 StringRef RelPath = OrigPath.substr(OldPrefix.size());
516 SmallString<256> NewPath;
517 path::append(NewPath, style, NewPrefix);
518 path::append(NewPath, style, RelPath);
519 Path.swap(NewPath);
522 void native(const Twine &path, SmallVectorImpl<char> &result, Style style) {
523 assert((!path.isSingleStringRef() ||
524 path.getSingleStringRef().data() != result.data()) &&
525 "path and result are not allowed to overlap!");
526 // Clear result.
527 result.clear();
528 path.toVector(result);
529 native(result, style);
532 void native(SmallVectorImpl<char> &Path, Style style) {
533 if (Path.empty())
534 return;
535 if (real_style(style) == Style::windows) {
536 std::replace(Path.begin(), Path.end(), '/', '\\');
537 if (Path[0] == '~' && (Path.size() == 1 || is_separator(Path[1], style))) {
538 SmallString<128> PathHome;
539 home_directory(PathHome);
540 PathHome.append(Path.begin() + 1, Path.end());
541 Path = PathHome;
543 } else {
544 for (auto PI = Path.begin(), PE = Path.end(); PI < PE; ++PI) {
545 if (*PI == '\\') {
546 auto PN = PI + 1;
547 if (PN < PE && *PN == '\\')
548 ++PI; // increment once, the for loop will move over the escaped slash
549 else
550 *PI = '/';
556 std::string convert_to_slash(StringRef path, Style style) {
557 if (real_style(style) != Style::windows)
558 return path;
560 std::string s = path.str();
561 std::replace(s.begin(), s.end(), '\\', '/');
562 return s;
565 StringRef filename(StringRef path, Style style) { return *rbegin(path, style); }
567 StringRef stem(StringRef path, Style style) {
568 StringRef fname = filename(path, style);
569 size_t pos = fname.find_last_of('.');
570 if (pos == StringRef::npos)
571 return fname;
572 else
573 if ((fname.size() == 1 && fname == ".") ||
574 (fname.size() == 2 && fname == ".."))
575 return fname;
576 else
577 return fname.substr(0, pos);
580 StringRef extension(StringRef path, Style style) {
581 StringRef fname = filename(path, style);
582 size_t pos = fname.find_last_of('.');
583 if (pos == StringRef::npos)
584 return StringRef();
585 else
586 if ((fname.size() == 1 && fname == ".") ||
587 (fname.size() == 2 && fname == ".."))
588 return StringRef();
589 else
590 return fname.substr(pos);
593 bool is_separator(char value, Style style) {
594 if (value == '/')
595 return true;
596 if (real_style(style) == Style::windows)
597 return value == '\\';
598 return false;
601 StringRef get_separator(Style style) {
602 if (real_style(style) == Style::windows)
603 return "\\";
604 return "/";
607 bool has_root_name(const Twine &path, Style style) {
608 SmallString<128> path_storage;
609 StringRef p = path.toStringRef(path_storage);
611 return !root_name(p, style).empty();
614 bool has_root_directory(const Twine &path, Style style) {
615 SmallString<128> path_storage;
616 StringRef p = path.toStringRef(path_storage);
618 return !root_directory(p, style).empty();
621 bool has_root_path(const Twine &path, Style style) {
622 SmallString<128> path_storage;
623 StringRef p = path.toStringRef(path_storage);
625 return !root_path(p, style).empty();
628 bool has_relative_path(const Twine &path, Style style) {
629 SmallString<128> path_storage;
630 StringRef p = path.toStringRef(path_storage);
632 return !relative_path(p, style).empty();
635 bool has_filename(const Twine &path, Style style) {
636 SmallString<128> path_storage;
637 StringRef p = path.toStringRef(path_storage);
639 return !filename(p, style).empty();
642 bool has_parent_path(const Twine &path, Style style) {
643 SmallString<128> path_storage;
644 StringRef p = path.toStringRef(path_storage);
646 return !parent_path(p, style).empty();
649 bool has_stem(const Twine &path, Style style) {
650 SmallString<128> path_storage;
651 StringRef p = path.toStringRef(path_storage);
653 return !stem(p, style).empty();
656 bool has_extension(const Twine &path, Style style) {
657 SmallString<128> path_storage;
658 StringRef p = path.toStringRef(path_storage);
660 return !extension(p, style).empty();
663 bool is_absolute(const Twine &path, Style style) {
664 SmallString<128> path_storage;
665 StringRef p = path.toStringRef(path_storage);
667 bool rootDir = has_root_directory(p, style);
668 bool rootName =
669 (real_style(style) != Style::windows) || has_root_name(p, style);
671 return rootDir && rootName;
674 bool is_relative(const Twine &path, Style style) {
675 return !is_absolute(path, style);
678 StringRef remove_leading_dotslash(StringRef Path, Style style) {
679 // Remove leading "./" (or ".//" or "././" etc.)
680 while (Path.size() > 2 && Path[0] == '.' && is_separator(Path[1], style)) {
681 Path = Path.substr(2);
682 while (Path.size() > 0 && is_separator(Path[0], style))
683 Path = Path.substr(1);
685 return Path;
688 static SmallString<256> remove_dots(StringRef path, bool remove_dot_dot,
689 Style style) {
690 SmallVector<StringRef, 16> components;
692 // Skip the root path, then look for traversal in the components.
693 StringRef rel = path::relative_path(path, style);
694 for (StringRef C :
695 llvm::make_range(path::begin(rel, style), path::end(rel))) {
696 if (C == ".")
697 continue;
698 // Leading ".." will remain in the path unless it's at the root.
699 if (remove_dot_dot && C == "..") {
700 if (!components.empty() && components.back() != "..") {
701 components.pop_back();
702 continue;
704 if (path::is_absolute(path, style))
705 continue;
707 components.push_back(C);
710 SmallString<256> buffer = path::root_path(path, style);
711 for (StringRef C : components)
712 path::append(buffer, style, C);
713 return buffer;
716 bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot,
717 Style style) {
718 StringRef p(path.data(), path.size());
720 SmallString<256> result = remove_dots(p, remove_dot_dot, style);
721 if (result == path)
722 return false;
724 path.swap(result);
725 return true;
728 } // end namespace path
730 namespace fs {
732 std::error_code getUniqueID(const Twine Path, UniqueID &Result) {
733 file_status Status;
734 std::error_code EC = status(Path, Status);
735 if (EC)
736 return EC;
737 Result = Status.getUniqueID();
738 return std::error_code();
741 void createUniquePath(const Twine &Model, SmallVectorImpl<char> &ResultPath,
742 bool MakeAbsolute) {
743 SmallString<128> ModelStorage;
744 Model.toVector(ModelStorage);
746 if (MakeAbsolute) {
747 // Make model absolute by prepending a temp directory if it's not already.
748 if (!sys::path::is_absolute(Twine(ModelStorage))) {
749 SmallString<128> TDir;
750 sys::path::system_temp_directory(true, TDir);
751 sys::path::append(TDir, Twine(ModelStorage));
752 ModelStorage.swap(TDir);
756 ResultPath = ModelStorage;
757 ResultPath.push_back(0);
758 ResultPath.pop_back();
760 // Replace '%' with random chars.
761 for (unsigned i = 0, e = ModelStorage.size(); i != e; ++i) {
762 if (ModelStorage[i] == '%')
763 ResultPath[i] = "0123456789abcdef"[sys::Process::GetRandomNumber() & 15];
767 std::error_code createUniqueFile(const Twine &Model, int &ResultFd,
768 SmallVectorImpl<char> &ResultPath,
769 unsigned Mode) {
770 return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
773 static std::error_code createUniqueFile(const Twine &Model, int &ResultFd,
774 SmallVectorImpl<char> &ResultPath,
775 unsigned Mode, OpenFlags Flags) {
776 return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File,
777 Flags);
780 std::error_code createUniqueFile(const Twine &Model,
781 SmallVectorImpl<char> &ResultPath,
782 unsigned Mode) {
783 int FD;
784 auto EC = createUniqueFile(Model, FD, ResultPath, Mode);
785 if (EC)
786 return EC;
787 // FD is only needed to avoid race conditions. Close it right away.
788 close(FD);
789 return EC;
792 static std::error_code
793 createTemporaryFile(const Twine &Model, int &ResultFD,
794 llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) {
795 SmallString<128> Storage;
796 StringRef P = Model.toNullTerminatedStringRef(Storage);
797 assert(P.find_first_of(separators(Style::native)) == StringRef::npos &&
798 "Model must be a simple filename.");
799 // Use P.begin() so that createUniqueEntity doesn't need to recreate Storage.
800 return createUniqueEntity(P.begin(), ResultFD, ResultPath, true,
801 owner_read | owner_write, Type);
804 static std::error_code
805 createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
806 llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) {
807 const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
808 return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
809 Type);
812 std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
813 int &ResultFD,
814 SmallVectorImpl<char> &ResultPath) {
815 return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File);
818 std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
819 SmallVectorImpl<char> &ResultPath) {
820 int FD;
821 auto EC = createTemporaryFile(Prefix, Suffix, FD, ResultPath);
822 if (EC)
823 return EC;
824 // FD is only needed to avoid race conditions. Close it right away.
825 close(FD);
826 return EC;
830 // This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
831 // for consistency. We should try using mkdtemp.
832 std::error_code createUniqueDirectory(const Twine &Prefix,
833 SmallVectorImpl<char> &ResultPath) {
834 int Dummy;
835 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath, true, 0,
836 FS_Dir);
839 std::error_code
840 getPotentiallyUniqueFileName(const Twine &Model,
841 SmallVectorImpl<char> &ResultPath) {
842 int Dummy;
843 return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
846 std::error_code
847 getPotentiallyUniqueTempFileName(const Twine &Prefix, StringRef Suffix,
848 SmallVectorImpl<char> &ResultPath) {
849 int Dummy;
850 return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
853 void make_absolute(const Twine &current_directory,
854 SmallVectorImpl<char> &path) {
855 StringRef p(path.data(), path.size());
857 bool rootDirectory = path::has_root_directory(p);
858 bool rootName =
859 (real_style(Style::native) != Style::windows) || path::has_root_name(p);
861 // Already absolute.
862 if (rootName && rootDirectory)
863 return;
865 // All of the following conditions will need the current directory.
866 SmallString<128> current_dir;
867 current_directory.toVector(current_dir);
869 // Relative path. Prepend the current directory.
870 if (!rootName && !rootDirectory) {
871 // Append path to the current directory.
872 path::append(current_dir, p);
873 // Set path to the result.
874 path.swap(current_dir);
875 return;
878 if (!rootName && rootDirectory) {
879 StringRef cdrn = path::root_name(current_dir);
880 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
881 path::append(curDirRootName, p);
882 // Set path to the result.
883 path.swap(curDirRootName);
884 return;
887 if (rootName && !rootDirectory) {
888 StringRef pRootName = path::root_name(p);
889 StringRef bRootDirectory = path::root_directory(current_dir);
890 StringRef bRelativePath = path::relative_path(current_dir);
891 StringRef pRelativePath = path::relative_path(p);
893 SmallString<128> res;
894 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
895 path.swap(res);
896 return;
899 llvm_unreachable("All rootName and rootDirectory combinations should have "
900 "occurred above!");
903 std::error_code make_absolute(SmallVectorImpl<char> &path) {
904 if (path::is_absolute(path))
905 return {};
907 SmallString<128> current_dir;
908 if (std::error_code ec = current_path(current_dir))
909 return ec;
911 make_absolute(current_dir, path);
912 return {};
915 std::error_code create_directories(const Twine &Path, bool IgnoreExisting,
916 perms Perms) {
917 SmallString<128> PathStorage;
918 StringRef P = Path.toStringRef(PathStorage);
920 // Be optimistic and try to create the directory
921 std::error_code EC = create_directory(P, IgnoreExisting, Perms);
922 // If we succeeded, or had any error other than the parent not existing, just
923 // return it.
924 if (EC != errc::no_such_file_or_directory)
925 return EC;
927 // We failed because of a no_such_file_or_directory, try to create the
928 // parent.
929 StringRef Parent = path::parent_path(P);
930 if (Parent.empty())
931 return EC;
933 if ((EC = create_directories(Parent, IgnoreExisting, Perms)))
934 return EC;
936 return create_directory(P, IgnoreExisting, Perms);
939 static std::error_code copy_file_internal(int ReadFD, int WriteFD) {
940 const size_t BufSize = 4096;
941 char *Buf = new char[BufSize];
942 int BytesRead = 0, BytesWritten = 0;
943 for (;;) {
944 BytesRead = read(ReadFD, Buf, BufSize);
945 if (BytesRead <= 0)
946 break;
947 while (BytesRead) {
948 BytesWritten = write(WriteFD, Buf, BytesRead);
949 if (BytesWritten < 0)
950 break;
951 BytesRead -= BytesWritten;
953 if (BytesWritten < 0)
954 break;
956 delete[] Buf;
958 if (BytesRead < 0 || BytesWritten < 0)
959 return std::error_code(errno, std::generic_category());
960 return std::error_code();
963 #ifndef __APPLE__
964 std::error_code copy_file(const Twine &From, const Twine &To) {
965 int ReadFD, WriteFD;
966 if (std::error_code EC = openFileForRead(From, ReadFD, OF_None))
967 return EC;
968 if (std::error_code EC =
969 openFileForWrite(To, WriteFD, CD_CreateAlways, OF_None)) {
970 close(ReadFD);
971 return EC;
974 std::error_code EC = copy_file_internal(ReadFD, WriteFD);
976 close(ReadFD);
977 close(WriteFD);
979 return EC;
981 #endif
983 std::error_code copy_file(const Twine &From, int ToFD) {
984 int ReadFD;
985 if (std::error_code EC = openFileForRead(From, ReadFD, OF_None))
986 return EC;
988 std::error_code EC = copy_file_internal(ReadFD, ToFD);
990 close(ReadFD);
992 return EC;
995 ErrorOr<MD5::MD5Result> md5_contents(int FD) {
996 MD5 Hash;
998 constexpr size_t BufSize = 4096;
999 std::vector<uint8_t> Buf(BufSize);
1000 int BytesRead = 0;
1001 for (;;) {
1002 BytesRead = read(FD, Buf.data(), BufSize);
1003 if (BytesRead <= 0)
1004 break;
1005 Hash.update(makeArrayRef(Buf.data(), BytesRead));
1008 if (BytesRead < 0)
1009 return std::error_code(errno, std::generic_category());
1010 MD5::MD5Result Result;
1011 Hash.final(Result);
1012 return Result;
1015 ErrorOr<MD5::MD5Result> md5_contents(const Twine &Path) {
1016 int FD;
1017 if (auto EC = openFileForRead(Path, FD, OF_None))
1018 return EC;
1020 auto Result = md5_contents(FD);
1021 close(FD);
1022 return Result;
1025 bool exists(const basic_file_status &status) {
1026 return status_known(status) && status.type() != file_type::file_not_found;
1029 bool status_known(const basic_file_status &s) {
1030 return s.type() != file_type::status_error;
1033 file_type get_file_type(const Twine &Path, bool Follow) {
1034 file_status st;
1035 if (status(Path, st, Follow))
1036 return file_type::status_error;
1037 return st.type();
1040 bool is_directory(const basic_file_status &status) {
1041 return status.type() == file_type::directory_file;
1044 std::error_code is_directory(const Twine &path, bool &result) {
1045 file_status st;
1046 if (std::error_code ec = status(path, st))
1047 return ec;
1048 result = is_directory(st);
1049 return std::error_code();
1052 bool is_regular_file(const basic_file_status &status) {
1053 return status.type() == file_type::regular_file;
1056 std::error_code is_regular_file(const Twine &path, bool &result) {
1057 file_status st;
1058 if (std::error_code ec = status(path, st))
1059 return ec;
1060 result = is_regular_file(st);
1061 return std::error_code();
1064 bool is_symlink_file(const basic_file_status &status) {
1065 return status.type() == file_type::symlink_file;
1068 std::error_code is_symlink_file(const Twine &path, bool &result) {
1069 file_status st;
1070 if (std::error_code ec = status(path, st, false))
1071 return ec;
1072 result = is_symlink_file(st);
1073 return std::error_code();
1076 bool is_other(const basic_file_status &status) {
1077 return exists(status) &&
1078 !is_regular_file(status) &&
1079 !is_directory(status);
1082 std::error_code is_other(const Twine &Path, bool &Result) {
1083 file_status FileStatus;
1084 if (std::error_code EC = status(Path, FileStatus))
1085 return EC;
1086 Result = is_other(FileStatus);
1087 return std::error_code();
1090 void directory_entry::replace_filename(const Twine &Filename, file_type Type,
1091 basic_file_status Status) {
1092 SmallString<128> PathStr = path::parent_path(Path);
1093 path::append(PathStr, Filename);
1094 this->Path = PathStr.str();
1095 this->Type = Type;
1096 this->Status = Status;
1099 ErrorOr<perms> getPermissions(const Twine &Path) {
1100 file_status Status;
1101 if (std::error_code EC = status(Path, Status))
1102 return EC;
1104 return Status.permissions();
1107 } // end namespace fs
1108 } // end namespace sys
1109 } // end namespace llvm
1111 // Include the truly platform-specific parts.
1112 #if defined(LLVM_ON_UNIX)
1113 #include "Unix/Path.inc"
1114 #endif
1115 #if defined(_WIN32)
1116 #include "Windows/Path.inc"
1117 #endif
1119 namespace llvm {
1120 namespace sys {
1121 namespace fs {
1122 TempFile::TempFile(StringRef Name, int FD) : TmpName(Name), FD(FD) {}
1123 TempFile::TempFile(TempFile &&Other) { *this = std::move(Other); }
1124 TempFile &TempFile::operator=(TempFile &&Other) {
1125 TmpName = std::move(Other.TmpName);
1126 FD = Other.FD;
1127 Other.Done = true;
1128 Other.FD = -1;
1129 return *this;
1132 TempFile::~TempFile() { assert(Done); }
1134 Error TempFile::discard() {
1135 Done = true;
1136 if (FD != -1 && close(FD) == -1) {
1137 std::error_code EC = std::error_code(errno, std::generic_category());
1138 return errorCodeToError(EC);
1140 FD = -1;
1142 #ifdef _WIN32
1143 // On windows closing will remove the file.
1144 TmpName = "";
1145 return Error::success();
1146 #else
1147 // Always try to close and remove.
1148 std::error_code RemoveEC;
1149 if (!TmpName.empty()) {
1150 RemoveEC = fs::remove(TmpName);
1151 sys::DontRemoveFileOnSignal(TmpName);
1152 if (!RemoveEC)
1153 TmpName = "";
1155 return errorCodeToError(RemoveEC);
1156 #endif
1159 Error TempFile::keep(const Twine &Name) {
1160 assert(!Done);
1161 Done = true;
1162 // Always try to close and rename.
1163 #ifdef _WIN32
1164 // If we can't cancel the delete don't rename.
1165 auto H = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
1166 std::error_code RenameEC = setDeleteDisposition(H, false);
1167 if (!RenameEC) {
1168 RenameEC = rename_fd(FD, Name);
1169 // If rename failed because it's cross-device, copy instead
1170 if (RenameEC ==
1171 std::error_code(ERROR_NOT_SAME_DEVICE, std::system_category())) {
1172 RenameEC = copy_file(TmpName, Name);
1173 setDeleteDisposition(H, true);
1177 // If we can't rename, discard the temporary file.
1178 if (RenameEC)
1179 setDeleteDisposition(H, true);
1180 #else
1181 std::error_code RenameEC = fs::rename(TmpName, Name);
1182 if (RenameEC) {
1183 // If we can't rename, try to copy to work around cross-device link issues.
1184 RenameEC = sys::fs::copy_file(TmpName, Name);
1185 // If we can't rename or copy, discard the temporary file.
1186 if (RenameEC)
1187 remove(TmpName);
1189 sys::DontRemoveFileOnSignal(TmpName);
1190 #endif
1192 if (!RenameEC)
1193 TmpName = "";
1195 if (close(FD) == -1) {
1196 std::error_code EC(errno, std::generic_category());
1197 return errorCodeToError(EC);
1199 FD = -1;
1201 return errorCodeToError(RenameEC);
1204 Error TempFile::keep() {
1205 assert(!Done);
1206 Done = true;
1208 #ifdef _WIN32
1209 auto H = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
1210 if (std::error_code EC = setDeleteDisposition(H, false))
1211 return errorCodeToError(EC);
1212 #else
1213 sys::DontRemoveFileOnSignal(TmpName);
1214 #endif
1216 TmpName = "";
1218 if (close(FD) == -1) {
1219 std::error_code EC(errno, std::generic_category());
1220 return errorCodeToError(EC);
1222 FD = -1;
1224 return Error::success();
1227 Expected<TempFile> TempFile::create(const Twine &Model, unsigned Mode) {
1228 int FD;
1229 SmallString<128> ResultPath;
1230 if (std::error_code EC =
1231 createUniqueFile(Model, FD, ResultPath, Mode, OF_Delete))
1232 return errorCodeToError(EC);
1234 TempFile Ret(ResultPath, FD);
1235 #ifndef _WIN32
1236 if (sys::RemoveFileOnSignal(ResultPath)) {
1237 // Make sure we delete the file when RemoveFileOnSignal fails.
1238 consumeError(Ret.discard());
1239 std::error_code EC(errc::operation_not_permitted);
1240 return errorCodeToError(EC);
1242 #endif
1243 return std::move(Ret);
1247 } // end namsspace sys
1248 } // end namespace llvm