LLVM  9.0.0svn
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/Config/llvm-config.h"
16 #include "llvm/Support/Endian.h"
17 #include "llvm/Support/Errc.h"
20 #include "llvm/Support/Process.h"
21 #include "llvm/Support/Signals.h"
22 #include <cctype>
23 #include <cstring>
24 
25 #if !defined(_MSC_VER) && !defined(__MINGW32__)
26 #include <unistd.h>
27 #else
28 #include <io.h>
29 #endif
30 
31 using namespace llvm;
32 using namespace llvm::support::endian;
33 
34 namespace {
35  using llvm::StringRef;
38 
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
45  }
46 
47  inline const char *separators(Style style) {
48  if (real_style(style) == Style::windows)
49  return "\\/";
50  return "/";
51  }
52 
53  inline char preferred_separator(Style style) {
54  if (real_style(style) == Style::windows)
55  return '\\';
56  return '/';
57  }
58 
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
65 
66  if (path.empty())
67  return path;
68 
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);
74  }
75 
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);
82  }
83 
84  // {/,\}
85  if (is_separator(path[0], style))
86  return path.substr(0, 1);
87 
88  // * {file,directory}name
89  size_t end = path.find_first_of(separators(style));
90  return path.substr(0, end);
91  }
92 
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;
98 
99  size_t pos = str.find_last_of(separators(style), str.size() - 1);
100 
101  if (real_style(style) == Style::windows) {
102  if (pos == StringRef::npos)
103  pos = str.find_last_of(':', str.size() - 2);
104  }
105 
106  if (pos == StringRef::npos || (pos == 1 && is_separator(str[0], style)))
107  return 0;
108 
109  return pos + 1;
110  }
111 
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;
119  }
120 
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);
125  }
126 
127  // case "/"
128  if (str.size() > 0 && is_separator(str[0], style))
129  return 0;
130 
131  return StringRef::npos;
132  }
133 
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);
139 
140  bool filename_was_sep =
141  path.size() > 0 && is_separator(path[end_pos], style);
142 
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;
149 
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;
154  }
155 
156  // Otherwise, just include before the last slash.
157  return end_pos;
158  }
159 } // end unnamed namespace
160 
161 enum FSEntity {
165 };
166 
167 static std::error_code
168 createUniqueEntity(const Twine &Model, int &ResultFD,
169  SmallVectorImpl<char> &ResultPath, bool MakeAbsolute,
170  unsigned Mode, FSEntity Type,
172  SmallString<128> ModelStorage;
173  Model.toVector(ModelStorage);
174 
175  if (MakeAbsolute) {
176  // Make model absolute by prepending a temp directory if it's not already.
177  if (!sys::path::is_absolute(Twine(ModelStorage))) {
178  SmallString<128> TDir;
180  sys::path::append(TDir, Twine(ModelStorage));
181  ModelStorage.swap(TDir);
182  }
183  }
184 
185  // From here on, DO NOT modify model. It may be needed if the randomly chosen
186  // path already exists.
187  ResultPath = ModelStorage;
188  // Null terminate.
189  ResultPath.push_back(0);
190  ResultPath.pop_back();
191 
192  // Limit the number of attempts we make, so that we don't infinite loop. E.g.
193  // "permission denied" could be for a specific file (so we retry with a
194  // different name) or for the whole directory (retry would always fail).
195  // Checking which is racy, so we try a number of times, then give up.
196  std::error_code EC;
197  for (int Retries = 128; Retries > 0; --Retries) {
198  // Replace '%' with random chars.
199  for (unsigned i = 0, e = ModelStorage.size(); i != e; ++i) {
200  if (ModelStorage[i] == '%')
201  ResultPath[i] =
202  "0123456789abcdef"[sys::Process::GetRandomNumber() & 15];
203  }
204 
205  // Try to open + create the file.
206  switch (Type) {
207  case FS_File: {
208  EC = sys::fs::openFileForReadWrite(Twine(ResultPath.begin()), ResultFD,
209  sys::fs::CD_CreateNew, Flags, Mode);
210  if (EC) {
211  // errc::permission_denied happens on Windows when we try to open a file
212  // that has been marked for deletion.
213  if (EC == errc::file_exists || EC == errc::permission_denied)
214  continue;
215  return EC;
216  }
217 
218  return std::error_code();
219  }
220 
221  case FS_Name: {
224  return std::error_code();
225  if (EC)
226  return EC;
227  continue;
228  }
229 
230  case FS_Dir: {
231  EC = sys::fs::create_directory(ResultPath.begin(), false);
232  if (EC) {
233  if (EC == errc::file_exists)
234  continue;
235  return EC;
236  }
237  return std::error_code();
238  }
239  }
240  llvm_unreachable("Invalid Type");
241  }
242  return EC;
243 }
244 
245 namespace llvm {
246 namespace sys {
247 namespace path {
248 
250  const_iterator i;
251  i.Path = path;
252  i.Component = find_first_component(path, style);
253  i.Position = 0;
254  i.S = style;
255  return i;
256 }
257 
259  const_iterator i;
260  i.Path = path;
261  i.Position = path.size();
262  return i;
263 }
264 
265 const_iterator &const_iterator::operator++() {
266  assert(Position < Path.size() && "Tried to increment past end!");
267 
268  // Increment Position to past the current component
269  Position += Component.size();
270 
271  // Check for end.
272  if (Position == Path.size()) {
273  Component = StringRef();
274  return *this;
275  }
276 
277  // Both POSIX and Windows treat paths that begin with exactly two separators
278  // specially.
279  bool was_net = Component.size() > 2 && is_separator(Component[0], S) &&
280  Component[1] == Component[0] && !is_separator(Component[2], S);
281 
282  // Handle separators.
283  if (is_separator(Path[Position], S)) {
284  // Root dir.
285  if (was_net ||
286  // c:/
287  (real_style(S) == Style::windows && Component.endswith(":"))) {
288  Component = Path.substr(Position, 1);
289  return *this;
290  }
291 
292  // Skip extra separators.
293  while (Position != Path.size() && is_separator(Path[Position], S)) {
294  ++Position;
295  }
296 
297  // Treat trailing '/' as a '.', unless it is the root dir.
298  if (Position == Path.size() && Component != "/") {
299  --Position;
300  Component = ".";
301  return *this;
302  }
303  }
304 
305  // Find next component.
306  size_t end_pos = Path.find_first_of(separators(S), Position);
307  Component = Path.slice(Position, end_pos);
308 
309  return *this;
310 }
311 
313  return Path.begin() == RHS.Path.begin() && Position == RHS.Position;
314 }
315 
317  return Position - RHS.Position;
318 }
319 
322  I.Path = Path;
323  I.Position = Path.size();
324  I.S = style;
325  return ++I;
326 }
327 
330  I.Path = Path;
331  I.Component = Path.substr(0, 0);
332  I.Position = 0;
333  return I;
334 }
335 
336 reverse_iterator &reverse_iterator::operator++() {
337  size_t root_dir_pos = root_dir_start(Path, S);
338 
339  // Skip separators unless it's the root directory.
340  size_t end_pos = Position;
341  while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
342  is_separator(Path[end_pos - 1], S))
343  --end_pos;
344 
345  // Treat trailing '/' as a '.', unless it is the root dir.
346  if (Position == Path.size() && !Path.empty() &&
347  is_separator(Path.back(), S) &&
348  (root_dir_pos == StringRef::npos || end_pos - 1 > root_dir_pos)) {
349  --Position;
350  Component = ".";
351  return *this;
352  }
353 
354  // Find next separator.
355  size_t start_pos = filename_pos(Path.substr(0, end_pos), S);
356  Component = Path.slice(start_pos, end_pos);
357  Position = start_pos;
358  return *this;
359 }
360 
362  return Path.begin() == RHS.Path.begin() && Component == RHS.Component &&
363  Position == RHS.Position;
364 }
365 
367  return Position - RHS.Position;
368 }
369 
371  const_iterator b = begin(path, style), pos = b, e = end(path);
372  if (b != e) {
373  bool has_net =
374  b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
375  bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
376 
377  if (has_net || has_drive) {
378  if ((++pos != e) && is_separator((*pos)[0], style)) {
379  // {C:/,//net/}, so get the first two components.
380  return path.substr(0, b->size() + pos->size());
381  } else {
382  // just {C:,//net}, return the first component.
383  return *b;
384  }
385  }
386 
387  // POSIX style root directory.
388  if (is_separator((*b)[0], style)) {
389  return *b;
390  }
391  }
392 
393  return StringRef();
394 }
395 
397  const_iterator b = begin(path, style), e = end(path);
398  if (b != e) {
399  bool has_net =
400  b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
401  bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
402 
403  if (has_net || has_drive) {
404  // just {C:,//net}, return the first component.
405  return *b;
406  }
407  }
408 
409  // No path or no name.
410  return StringRef();
411 }
412 
414  const_iterator b = begin(path, style), pos = b, e = end(path);
415  if (b != e) {
416  bool has_net =
417  b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
418  bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
419 
420  if ((has_net || has_drive) &&
421  // {C:,//net}, skip to the next component.
422  (++pos != e) && is_separator((*pos)[0], style)) {
423  return *pos;
424  }
425 
426  // POSIX style root directory.
427  if (!has_net && is_separator((*b)[0], style)) {
428  return *b;
429  }
430  }
431 
432  // No path or no root.
433  return StringRef();
434 }
435 
437  StringRef root = root_path(path, style);
438  return path.substr(root.size());
439 }
440 
441 void append(SmallVectorImpl<char> &path, Style style, const Twine &a,
442  const Twine &b, const Twine &c, const Twine &d) {
443  SmallString<32> a_storage;
444  SmallString<32> b_storage;
445  SmallString<32> c_storage;
446  SmallString<32> d_storage;
447 
448  SmallVector<StringRef, 4> components;
449  if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
450  if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
451  if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
452  if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
453 
454  for (auto &component : components) {
455  bool path_has_sep =
456  !path.empty() && is_separator(path[path.size() - 1], style);
457  if (path_has_sep) {
458  // Strip separators from beginning of component.
459  size_t loc = component.find_first_not_of(separators(style));
460  StringRef c = component.substr(loc);
461 
462  // Append it.
463  path.append(c.begin(), c.end());
464  continue;
465  }
466 
467  bool component_has_sep =
468  !component.empty() && is_separator(component[0], style);
469  if (!component_has_sep &&
470  !(path.empty() || has_root_name(component, style))) {
471  // Add a separator.
472  path.push_back(preferred_separator(style));
473  }
474 
475  path.append(component.begin(), component.end());
476  }
477 }
478 
479 void append(SmallVectorImpl<char> &path, const Twine &a, const Twine &b,
480  const Twine &c, const Twine &d) {
481  append(path, Style::native, a, b, c, d);
482 }
483 
485  const_iterator end, Style style) {
486  for (; begin != end; ++begin)
487  path::append(path, style, *begin);
488 }
489 
491  size_t end_pos = parent_path_end(path, style);
492  if (end_pos == StringRef::npos)
493  return StringRef();
494  else
495  return path.substr(0, end_pos);
496 }
497 
499  size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()), style);
500  if (end_pos != StringRef::npos)
501  path.set_size(end_pos);
502 }
503 
505  Style style) {
506  StringRef p(path.begin(), path.size());
507  SmallString<32> ext_storage;
508  StringRef ext = extension.toStringRef(ext_storage);
509 
510  // Erase existing extension.
511  size_t pos = p.find_last_of('.');
512  if (pos != StringRef::npos && pos >= filename_pos(p, style))
513  path.set_size(pos);
514 
515  // Append '.' if needed.
516  if (ext.size() > 0 && ext[0] != '.')
517  path.push_back('.');
518 
519  // Append extension.
520  path.append(ext.begin(), ext.end());
521 }
522 
524  const StringRef &OldPrefix, const StringRef &NewPrefix,
525  Style style) {
526  if (OldPrefix.empty() && NewPrefix.empty())
527  return;
528 
529  StringRef OrigPath(Path.begin(), Path.size());
530  if (!OrigPath.startswith(OldPrefix))
531  return;
532 
533  // If prefixes have the same size we can simply copy the new one over.
534  if (OldPrefix.size() == NewPrefix.size()) {
535  llvm::copy(NewPrefix, Path.begin());
536  return;
537  }
538 
539  StringRef RelPath = OrigPath.substr(OldPrefix.size());
540  SmallString<256> NewPath;
541  path::append(NewPath, style, NewPrefix);
542  path::append(NewPath, style, RelPath);
543  Path.swap(NewPath);
544 }
545 
546 void native(const Twine &path, SmallVectorImpl<char> &result, Style style) {
547  assert((!path.isSingleStringRef() ||
548  path.getSingleStringRef().data() != result.data()) &&
549  "path and result are not allowed to overlap!");
550  // Clear result.
551  result.clear();
552  path.toVector(result);
553  native(result, style);
554 }
555 
556 void native(SmallVectorImpl<char> &Path, Style style) {
557  if (Path.empty())
558  return;
559  if (real_style(style) == Style::windows) {
560  std::replace(Path.begin(), Path.end(), '/', '\\');
561  if (Path[0] == '~' && (Path.size() == 1 || is_separator(Path[1], style))) {
562  SmallString<128> PathHome;
563  home_directory(PathHome);
564  PathHome.append(Path.begin() + 1, Path.end());
565  Path = PathHome;
566  }
567  } else {
568  for (auto PI = Path.begin(), PE = Path.end(); PI < PE; ++PI) {
569  if (*PI == '\\') {
570  auto PN = PI + 1;
571  if (PN < PE && *PN == '\\')
572  ++PI; // increment once, the for loop will move over the escaped slash
573  else
574  *PI = '/';
575  }
576  }
577  }
578 }
579 
580 std::string convert_to_slash(StringRef path, Style style) {
581  if (real_style(style) != Style::windows)
582  return path;
583 
584  std::string s = path.str();
585  std::replace(s.begin(), s.end(), '\\', '/');
586  return s;
587 }
588 
589 StringRef filename(StringRef path, Style style) { return *rbegin(path, style); }
590 
592  StringRef fname = filename(path, style);
593  size_t pos = fname.find_last_of('.');
594  if (pos == StringRef::npos)
595  return fname;
596  else
597  if ((fname.size() == 1 && fname == ".") ||
598  (fname.size() == 2 && fname == ".."))
599  return fname;
600  else
601  return fname.substr(0, pos);
602 }
603 
605  StringRef fname = filename(path, style);
606  size_t pos = fname.find_last_of('.');
607  if (pos == StringRef::npos)
608  return StringRef();
609  else
610  if ((fname.size() == 1 && fname == ".") ||
611  (fname.size() == 2 && fname == ".."))
612  return StringRef();
613  else
614  return fname.substr(pos);
615 }
616 
617 bool is_separator(char value, Style style) {
618  if (value == '/')
619  return true;
620  if (real_style(style) == Style::windows)
621  return value == '\\';
622  return false;
623 }
624 
626  if (real_style(style) == Style::windows)
627  return "\\";
628  return "/";
629 }
630 
631 bool has_root_name(const Twine &path, Style style) {
632  SmallString<128> path_storage;
633  StringRef p = path.toStringRef(path_storage);
634 
635  return !root_name(p, style).empty();
636 }
637 
638 bool has_root_directory(const Twine &path, Style style) {
639  SmallString<128> path_storage;
640  StringRef p = path.toStringRef(path_storage);
641 
642  return !root_directory(p, style).empty();
643 }
644 
645 bool has_root_path(const Twine &path, Style style) {
646  SmallString<128> path_storage;
647  StringRef p = path.toStringRef(path_storage);
648 
649  return !root_path(p, style).empty();
650 }
651 
652 bool has_relative_path(const Twine &path, Style style) {
653  SmallString<128> path_storage;
654  StringRef p = path.toStringRef(path_storage);
655 
656  return !relative_path(p, style).empty();
657 }
658 
659 bool has_filename(const Twine &path, Style style) {
660  SmallString<128> path_storage;
661  StringRef p = path.toStringRef(path_storage);
662 
663  return !filename(p, style).empty();
664 }
665 
666 bool has_parent_path(const Twine &path, Style style) {
667  SmallString<128> path_storage;
668  StringRef p = path.toStringRef(path_storage);
669 
670  return !parent_path(p, style).empty();
671 }
672 
673 bool has_stem(const Twine &path, Style style) {
674  SmallString<128> path_storage;
675  StringRef p = path.toStringRef(path_storage);
676 
677  return !stem(p, style).empty();
678 }
679 
680 bool has_extension(const Twine &path, Style style) {
681  SmallString<128> path_storage;
682  StringRef p = path.toStringRef(path_storage);
683 
684  return !extension(p, style).empty();
685 }
686 
687 bool is_absolute(const Twine &path, Style style) {
688  SmallString<128> path_storage;
689  StringRef p = path.toStringRef(path_storage);
690 
691  bool rootDir = has_root_directory(p, style);
692  bool rootName =
693  (real_style(style) != Style::windows) || has_root_name(p, style);
694 
695  return rootDir && rootName;
696 }
697 
698 bool is_relative(const Twine &path, Style style) {
699  return !is_absolute(path, style);
700 }
701 
703  // Remove leading "./" (or ".//" or "././" etc.)
704  while (Path.size() > 2 && Path[0] == '.' && is_separator(Path[1], style)) {
705  Path = Path.substr(2);
706  while (Path.size() > 0 && is_separator(Path[0], style))
707  Path = Path.substr(1);
708  }
709  return Path;
710 }
711 
712 static SmallString<256> remove_dots(StringRef path, bool remove_dot_dot,
713  Style style) {
714  SmallVector<StringRef, 16> components;
715 
716  // Skip the root path, then look for traversal in the components.
717  StringRef rel = path::relative_path(path, style);
718  for (StringRef C :
719  llvm::make_range(path::begin(rel, style), path::end(rel))) {
720  if (C == ".")
721  continue;
722  // Leading ".." will remain in the path unless it's at the root.
723  if (remove_dot_dot && C == "..") {
724  if (!components.empty() && components.back() != "..") {
725  components.pop_back();
726  continue;
727  }
728  if (path::is_absolute(path, style))
729  continue;
730  }
731  components.push_back(C);
732  }
733 
734  SmallString<256> buffer = path::root_path(path, style);
735  for (StringRef C : components)
736  path::append(buffer, style, C);
737  return buffer;
738 }
739 
740 bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot,
741  Style style) {
742  StringRef p(path.data(), path.size());
743 
744  SmallString<256> result = remove_dots(p, remove_dot_dot, style);
745  if (result == path)
746  return false;
747 
748  path.swap(result);
749  return true;
750 }
751 
752 } // end namespace path
753 
754 namespace fs {
755 
756 std::error_code getUniqueID(const Twine Path, UniqueID &Result) {
758  std::error_code EC = status(Path, Status);
759  if (EC)
760  return EC;
761  Result = Status.getUniqueID();
762  return std::error_code();
763 }
764 
765 std::error_code createUniqueFile(const Twine &Model, int &ResultFd,
766  SmallVectorImpl<char> &ResultPath,
767  unsigned Mode) {
768  return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
769 }
770 
771 static std::error_code createUniqueFile(const Twine &Model, int &ResultFd,
772  SmallVectorImpl<char> &ResultPath,
773  unsigned Mode, OpenFlags Flags) {
774  return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File,
775  Flags);
776 }
777 
778 std::error_code createUniqueFile(const Twine &Model,
779  SmallVectorImpl<char> &ResultPath,
780  unsigned Mode) {
781  int FD;
782  auto EC = createUniqueFile(Model, FD, ResultPath, Mode);
783  if (EC)
784  return EC;
785  // FD is only needed to avoid race conditions. Close it right away.
786  close(FD);
787  return EC;
788 }
789 
790 static std::error_code
791 createTemporaryFile(const Twine &Model, int &ResultFD,
793  SmallString<128> Storage;
794  StringRef P = Model.toNullTerminatedStringRef(Storage);
796  "Model must be a simple filename.");
797  // Use P.begin() so that createUniqueEntity doesn't need to recreate Storage.
798  return createUniqueEntity(P.begin(), ResultFD, ResultPath, true,
799  owner_read | owner_write, Type);
800 }
801 
802 static std::error_code
803 createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
805  const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
806  return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
807  Type);
808 }
809 
810 std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
811  int &ResultFD,
812  SmallVectorImpl<char> &ResultPath) {
813  return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File);
814 }
815 
816 std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
817  SmallVectorImpl<char> &ResultPath) {
818  int FD;
819  auto EC = createTemporaryFile(Prefix, Suffix, FD, ResultPath);
820  if (EC)
821  return EC;
822  // FD is only needed to avoid race conditions. Close it right away.
823  close(FD);
824  return EC;
825 }
826 
827 
828 // This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
829 // for consistency. We should try using mkdtemp.
830 std::error_code createUniqueDirectory(const Twine &Prefix,
831  SmallVectorImpl<char> &ResultPath) {
832  int Dummy;
833  return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath, true, 0,
834  FS_Dir);
835 }
836 
837 std::error_code
839  SmallVectorImpl<char> &ResultPath) {
840  int Dummy;
841  return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
842 }
843 
844 std::error_code
846  SmallVectorImpl<char> &ResultPath) {
847  int Dummy;
848  return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
849 }
850 
851 void make_absolute(const Twine &current_directory,
852  SmallVectorImpl<char> &path) {
853  StringRef p(path.data(), path.size());
854 
855  bool rootDirectory = path::has_root_directory(p);
856  bool rootName =
857  (real_style(Style::native) != Style::windows) || path::has_root_name(p);
858 
859  // Already absolute.
860  if (rootName && rootDirectory)
861  return;
862 
863  // All of the following conditions will need the current directory.
864  SmallString<128> current_dir;
865  current_directory.toVector(current_dir);
866 
867  // Relative path. Prepend the current directory.
868  if (!rootName && !rootDirectory) {
869  // Append path to the current directory.
870  path::append(current_dir, p);
871  // Set path to the result.
872  path.swap(current_dir);
873  return;
874  }
875 
876  if (!rootName && rootDirectory) {
877  StringRef cdrn = path::root_name(current_dir);
878  SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
879  path::append(curDirRootName, p);
880  // Set path to the result.
881  path.swap(curDirRootName);
882  return;
883  }
884 
885  if (rootName && !rootDirectory) {
886  StringRef pRootName = path::root_name(p);
887  StringRef bRootDirectory = path::root_directory(current_dir);
888  StringRef bRelativePath = path::relative_path(current_dir);
889  StringRef pRelativePath = path::relative_path(p);
890 
891  SmallString<128> res;
892  path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
893  path.swap(res);
894  return;
895  }
896 
897  llvm_unreachable("All rootName and rootDirectory combinations should have "
898  "occurred above!");
899 }
900 
901 std::error_code make_absolute(SmallVectorImpl<char> &path) {
902  if (path::is_absolute(path))
903  return {};
904 
905  SmallString<128> current_dir;
906  if (std::error_code ec = current_path(current_dir))
907  return ec;
908 
909  make_absolute(current_dir, path);
910  return {};
911 }
912 
913 std::error_code create_directories(const Twine &Path, bool IgnoreExisting,
914  perms Perms) {
915  SmallString<128> PathStorage;
916  StringRef P = Path.toStringRef(PathStorage);
917 
918  // Be optimistic and try to create the directory
919  std::error_code EC = create_directory(P, IgnoreExisting, Perms);
920  // If we succeeded, or had any error other than the parent not existing, just
921  // return it.
923  return EC;
924 
925  // We failed because of a no_such_file_or_directory, try to create the
926  // parent.
927  StringRef Parent = path::parent_path(P);
928  if (Parent.empty())
929  return EC;
930 
931  if ((EC = create_directories(Parent, IgnoreExisting, Perms)))
932  return EC;
933 
934  return create_directory(P, IgnoreExisting, Perms);
935 }
936 
937 static std::error_code copy_file_internal(int ReadFD, int WriteFD) {
938  const size_t BufSize = 4096;
939  char *Buf = new char[BufSize];
940  int BytesRead = 0, BytesWritten = 0;
941  for (;;) {
942  BytesRead = read(ReadFD, Buf, BufSize);
943  if (BytesRead <= 0)
944  break;
945  while (BytesRead) {
946  BytesWritten = write(WriteFD, Buf, BytesRead);
947  if (BytesWritten < 0)
948  break;
949  BytesRead -= BytesWritten;
950  }
951  if (BytesWritten < 0)
952  break;
953  }
954  delete[] Buf;
955 
956  if (BytesRead < 0 || BytesWritten < 0)
957  return std::error_code(errno, std::generic_category());
958  return std::error_code();
959 }
960 
961 std::error_code copy_file(const Twine &From, const Twine &To) {
962  int ReadFD, WriteFD;
963  if (std::error_code EC = openFileForRead(From, ReadFD, OF_None))
964  return EC;
965  if (std::error_code EC =
966  openFileForWrite(To, WriteFD, CD_CreateAlways, OF_None)) {
967  close(ReadFD);
968  return EC;
969  }
970 
971  std::error_code EC = copy_file_internal(ReadFD, WriteFD);
972 
973  close(ReadFD);
974  close(WriteFD);
975 
976  return EC;
977 }
978 
979 std::error_code copy_file(const Twine &From, int ToFD) {
980  int ReadFD;
981  if (std::error_code EC = openFileForRead(From, ReadFD, OF_None))
982  return EC;
983 
984  std::error_code EC = copy_file_internal(ReadFD, ToFD);
985 
986  close(ReadFD);
987 
988  return EC;
989 }
990 
992  MD5 Hash;
993 
994  constexpr size_t BufSize = 4096;
995  std::vector<uint8_t> Buf(BufSize);
996  int BytesRead = 0;
997  for (;;) {
998  BytesRead = read(FD, Buf.data(), BufSize);
999  if (BytesRead <= 0)
1000  break;
1001  Hash.update(makeArrayRef(Buf.data(), BytesRead));
1002  }
1003 
1004  if (BytesRead < 0)
1005  return std::error_code(errno, std::generic_category());
1006  MD5::MD5Result Result;
1007  Hash.final(Result);
1008  return Result;
1009 }
1010 
1012  int FD;
1013  if (auto EC = openFileForRead(Path, FD, OF_None))
1014  return EC;
1015 
1016  auto Result = md5_contents(FD);
1017  close(FD);
1018  return Result;
1019 }
1020 
1022  return status_known(status) && status.type() != file_type::file_not_found;
1023 }
1024 
1026  return s.type() != file_type::status_error;
1027 }
1028 
1029 file_type get_file_type(const Twine &Path, bool Follow) {
1030  file_status st;
1031  if (status(Path, st, Follow))
1032  return file_type::status_error;
1033  return st.type();
1034 }
1035 
1037  return status.type() == file_type::directory_file;
1038 }
1039 
1040 std::error_code is_directory(const Twine &path, bool &result) {
1041  file_status st;
1042  if (std::error_code ec = status(path, st))
1043  return ec;
1044  result = is_directory(st);
1045  return std::error_code();
1046 }
1047 
1049  return status.type() == file_type::regular_file;
1050 }
1051 
1052 std::error_code is_regular_file(const Twine &path, bool &result) {
1053  file_status st;
1054  if (std::error_code ec = status(path, st))
1055  return ec;
1056  result = is_regular_file(st);
1057  return std::error_code();
1058 }
1059 
1061  return status.type() == file_type::symlink_file;
1062 }
1063 
1064 std::error_code is_symlink_file(const Twine &path, bool &result) {
1065  file_status st;
1066  if (std::error_code ec = status(path, st, false))
1067  return ec;
1068  result = is_symlink_file(st);
1069  return std::error_code();
1070 }
1071 
1073  return exists(status) &&
1074  !is_regular_file(status) &&
1075  !is_directory(status);
1076 }
1077 
1078 std::error_code is_other(const Twine &Path, bool &Result) {
1079  file_status FileStatus;
1080  if (std::error_code EC = status(Path, FileStatus))
1081  return EC;
1082  Result = is_other(FileStatus);
1083  return std::error_code();
1084 }
1085 
1086 void directory_entry::replace_filename(const Twine &Filename, file_type Type,
1088  SmallString<128> PathStr = path::parent_path(Path);
1089  path::append(PathStr, Filename);
1090  this->Path = PathStr.str();
1091  this->Type = Type;
1092  this->Status = Status;
1093 }
1094 
1097  if (std::error_code EC = status(Path, Status))
1098  return EC;
1099 
1100  return Status.permissions();
1101 }
1102 
1103 } // end namespace fs
1104 } // end namespace sys
1105 } // end namespace llvm
1106 
1107 // Include the truly platform-specific parts.
1108 #if defined(LLVM_ON_UNIX)
1109 #include "Unix/Path.inc"
1110 #endif
1111 #if defined(_WIN32)
1112 #include "Windows/Path.inc"
1113 #endif
1114 
1115 namespace llvm {
1116 namespace sys {
1117 namespace fs {
1118 TempFile::TempFile(StringRef Name, int FD) : TmpName(Name), FD(FD) {}
1119 TempFile::TempFile(TempFile &&Other) { *this = std::move(Other); }
1120 TempFile &TempFile::operator=(TempFile &&Other) {
1121  TmpName = std::move(Other.TmpName);
1122  FD = Other.FD;
1123  Other.Done = true;
1124  return *this;
1125 }
1126 
1127 TempFile::~TempFile() { assert(Done); }
1128 
1129 Error TempFile::discard() {
1130  Done = true;
1131  if (FD != -1 && close(FD) == -1) {
1132  std::error_code EC = std::error_code(errno, std::generic_category());
1133  return errorCodeToError(EC);
1134  }
1135  FD = -1;
1136 
1137 #ifdef _WIN32
1138  // On windows closing will remove the file.
1139  TmpName = "";
1140  return Error::success();
1141 #else
1142  // Always try to close and remove.
1143  std::error_code RemoveEC;
1144  if (!TmpName.empty()) {
1145  RemoveEC = fs::remove(TmpName);
1146  sys::DontRemoveFileOnSignal(TmpName);
1147  if (!RemoveEC)
1148  TmpName = "";
1149  }
1150  return errorCodeToError(RemoveEC);
1151 #endif
1152 }
1153 
1154 Error TempFile::keep(const Twine &Name) {
1155  assert(!Done);
1156  Done = true;
1157  // Always try to close and rename.
1158 #ifdef _WIN32
1159  // If we can't cancel the delete don't rename.
1160  auto H = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
1161  std::error_code RenameEC = setDeleteDisposition(H, false);
1162  if (!RenameEC) {
1163  RenameEC = rename_fd(FD, Name);
1164  // If rename failed because it's cross-device, copy instead
1165  if (RenameEC ==
1166  std::error_code(ERROR_NOT_SAME_DEVICE, std::system_category())) {
1167  RenameEC = copy_file(TmpName, Name);
1168  setDeleteDisposition(H, true);
1169  }
1170  }
1171 
1172  // If we can't rename, discard the temporary file.
1173  if (RenameEC)
1174  setDeleteDisposition(H, true);
1175 #else
1176  std::error_code RenameEC = fs::rename(TmpName, Name);
1177  if (RenameEC) {
1178  // If we can't rename, try to copy to work around cross-device link issues.
1179  RenameEC = sys::fs::copy_file(TmpName, Name);
1180  // If we can't rename or copy, discard the temporary file.
1181  if (RenameEC)
1182  remove(TmpName);
1183  }
1184  sys::DontRemoveFileOnSignal(TmpName);
1185 #endif
1186 
1187  if (!RenameEC)
1188  TmpName = "";
1189 
1190  if (close(FD) == -1) {
1191  std::error_code EC(errno, std::generic_category());
1192  return errorCodeToError(EC);
1193  }
1194  FD = -1;
1195 
1196  return errorCodeToError(RenameEC);
1197 }
1198 
1199 Error TempFile::keep() {
1200  assert(!Done);
1201  Done = true;
1202 
1203 #ifdef _WIN32
1204  auto H = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
1205  if (std::error_code EC = setDeleteDisposition(H, false))
1206  return errorCodeToError(EC);
1207 #else
1208  sys::DontRemoveFileOnSignal(TmpName);
1209 #endif
1210 
1211  TmpName = "";
1212 
1213  if (close(FD) == -1) {
1214  std::error_code EC(errno, std::generic_category());
1215  return errorCodeToError(EC);
1216  }
1217  FD = -1;
1218 
1219  return Error::success();
1220 }
1221 
1222 Expected<TempFile> TempFile::create(const Twine &Model, unsigned Mode) {
1223  int FD;
1224  SmallString<128> ResultPath;
1225  if (std::error_code EC =
1226  createUniqueFile(Model, FD, ResultPath, Mode, OF_Delete))
1227  return errorCodeToError(EC);
1228 
1229  TempFile Ret(ResultPath, FD);
1230 #ifndef _WIN32
1231  if (sys::RemoveFileOnSignal(ResultPath)) {
1232  // Make sure we delete the file when RemoveFileOnSignal fails.
1233  consumeError(Ret.discard());
1234  std::error_code EC(errc::operation_not_permitted);
1235  return errorCodeToError(EC);
1236  }
1237 #endif
1238  return std::move(Ret);
1239 }
1240 }
1241 
1242 } // end namsspace sys
1243 } // end namespace llvm
uint64_t CallInst * C
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:617
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:913
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:258
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:991
Represents either an error or a value T.
Definition: ErrorOr.h:56
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:498
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:218
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:249
void replace_path_prefix(SmallVectorImpl< char > &Path, const StringRef &OldPrefix, const StringRef &NewPrefix, Style style=Style::native)
Replace matching path prefix with another path.
Definition: Path.cpp:523
SI Whole Quad Mode
bool status_known(const basic_file_status &s)
Is status available?
Definition: Path.cpp:1025
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
void push_back(const T &Elt)
Definition: SmallVector.h:211
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:400
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:455
static std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, llvm::SmallVectorImpl< char > &ResultPath, FSEntity Type)
Definition: Path.cpp:803
bool has_extension(const Twine &path, Style style=Style::native)
Has extension?
Definition: Path.cpp:680
LLVM_NODISCARD 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:406
std::error_code current_path(SmallVectorImpl< char > &result)
Get the current path.
bool is_directory(const basic_file_status &status)
Does status represent a directory?
Definition: Path.cpp:1036
std::string convert_to_slash(StringRef path, Style style=Style::native)
Replaces backslashes with slashes if Windows.
Definition: Path.cpp:580
bool is_regular_file(const basic_file_status &status)
Does status represent a regular file?
Definition: Path.cpp:1048
StringRef remove_leading_dotslash(StringRef path, Style style=Style::native)
Remove redundant leading "./" pieces and consecutive separators.
Definition: Path.cpp:702
Represents the result of a call to sys::fs::status().
Definition: FileSystem.h:246
Definition: Path.cpp:162
ErrorOr< MD5::MD5Result > md5_contents(int FD)
Compute an MD5 hash of a file&#39;s contents.
Definition: Path.cpp:991
ErrorOr< perms > getPermissions(const Twine &Path)
Get file permissions.
Definition: Path.cpp:1095
bool has_stem(const Twine &path, Style style=Style::native)
Has stem?
Definition: Path.cpp:673
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:479
reverse_iterator rbegin(StringRef path, Style style=Style::native)
Get reverse begin iterator over path.
Definition: Path.cpp:320
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:450
void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
Definition: MD5.cpp:189
Represents a temporary file.
Definition: FileSystem.h:805
void make_absolute(const Twine &current_directory, SmallVectorImpl< char > &path)
Make path an absolute path.
Definition: Path.cpp:851
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition: Path.cpp:687
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
StringRef root_path(StringRef path, Style style=Style::native)
Get root path.
Definition: Path.cpp:370
StringRef getSingleStringRef() const
This returns the twine as a single StringRef.
Definition: Twine.h:439
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:578
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
Position
Position to insert a new instruction relative to an existing instruction.
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:810
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:266
CD_CreateNew - When opening a file:
Definition: FileSystem.h:725
bool isSingleStringRef() const
Return true if this twine can be dynamically accessed as a single StringRef value with getSingleStrin...
Definition: Twine.h:406
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:130
Delete the file on close. Only makes a difference on windows.
Definition: FileSystem.h:757
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition: Endian.h:99
void append(in_iter S, in_iter E)
Append from an iterator pair.
Definition: SmallString.h:74
Represents the result of a call to directory_iterator::status().
Definition: FileSystem.h:157
std::error_code copy_file(const Twine &From, const Twine &To)
Copy the contents of From to To.
Definition: Path.cpp:961
#define P(N)
static SmallString< 256 > remove_dots(StringRef path, bool remove_dot_dot, Style style)
Definition: Path.cpp:712
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
void swap(SmallVectorImpl &RHS)
Definition: SmallVector.h:670
bool has_parent_path(const Twine &path, Style style=Style::native)
Has parent path?
Definition: Path.cpp:666
UniqueID getUniqueID() const
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:87
#define H(x, y, z)
Definition: MD5.cpp:57
bool has_filename(const Twine &path, Style style=Style::native)
Has filename?
Definition: Path.cpp:659
StringRef toNullTerminatedStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single null terminated StringRef if it can be represented as such...
Definition: Twine.cpp:37
std::error_code getUniqueID(const Twine Path, UniqueID &Result)
Definition: Path.cpp:756
static void replace(Module &M, GlobalVariable *Old, GlobalVariable *New)
file_type get_file_type(const Twine &Path, bool Follow=true)
Does status represent a directory?
Definition: Path.cpp:1029
void DontRemoveFileOnSignal(StringRef Filename)
This function removes a file from the list of files to be removed on signal delivery.
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
Definition: Path.cpp:490
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Definition: Twine.cpp:32
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:950
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:981
size_t size() const
Definition: SmallVector.h:52
StringRef get_separator(Style style=Style::native)
Return the preferred separator for this platform.
Definition: Path.cpp:625
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
BlockVerifier::State From
static std::error_code createUniqueFile(const Twine &Model, int &ResultFd, SmallVectorImpl< char > &ResultPath, unsigned Mode, OpenFlags Flags)
Definition: Path.cpp:771
static std::error_code createUniqueEntity(const Twine &Model, int &ResultFD, SmallVectorImpl< char > &ResultPath, bool MakeAbsolute, unsigned Mode, FSEntity Type, sys::fs::OpenFlags Flags=sys::fs::OF_None)
Definition: Path.cpp:168
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:839
StringRef root_directory(StringRef path, Style style=Style::native)
Get root directory.
Definition: Path.cpp:413
void replace_extension(SmallVectorImpl< char > &path, const Twine &extension, Style style=Style::native)
Replace the file extension of path with extension.
Definition: Path.cpp:504
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...
StringRef root_name(StringRef path, Style style=Style::native)
Get root name.
Definition: Path.cpp:396
std::error_code createUniqueDirectory(const Twine &Prefix, SmallVectorImpl< char > &ResultPath)
Definition: Path.cpp:830
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:1072
bool has_root_directory(const Twine &path, Style style=Style::native)
Has root directory?
Definition: Path.cpp:638
Path iterator.
Definition: Path.h:52
iterator begin() const
Definition: StringRef.h:101
Reverse path iterator.
Definition: Path.h:78
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:386
bool operator==(const StringView &LHS, const StringView &RHS)
Definition: StringView.h:119
bool has_root_path(const Twine &path, Style style=Style::native)
Has root path?
Definition: Path.cpp:645
static unsigned GetRandomNumber()
Get the result of a process wide random number generator.
bool has_relative_path(const Twine &path, Style style=Style::native)
Has relative path?
Definition: Path.cpp:652
Provides a library for accessing information about this process and other processes on the operating ...
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:144
static const size_t npos
Definition: StringRef.h:50
file_type
An enumeration for the file system&#39;s view of the type.
Definition: FileSystem.h:66
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:589
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
Definition: Endian.h:65
LLVM_NODISCARD 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:380
#define I(x, y, z)
Definition: MD5.cpp:58
bool is_relative(const Twine &path, Style style=Style::native)
Is path relative?
Definition: Path.cpp:698
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:845
APInt operator-(APInt)
Definition: APInt.h:2043
Definition: MD5.h:41
static void rename(GlobalValue *GV)
Definition: AutoUpgrade.cpp:33
std::error_code create_directory(const Twine &path, bool IgnoreExisting=true, perms Perms=owner_all|group_all)
Create the directory in path.
reverse_iterator rend(StringRef path)
Get reverse end iterator over path.
Definition: Path.cpp:328
std::error_code getPotentiallyUniqueFileName(const Twine &Model, SmallVectorImpl< char > &ResultPath)
Get a unique name, not currently exisiting in the filesystem.
Definition: Path.cpp:838
void system_temp_directory(bool erasedOnReboot, SmallVectorImpl< char > &result)
Get the typical temporary directory for the system, e.g., "/var/tmp" or "C:/TEMP".
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:122
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
CD_CreateAlways - When opening a file:
Definition: FileSystem.h:720
StringRef relative_path(StringRef path, Style style=Style::native)
Get relative path.
Definition: Path.cpp:436
void set_size(size_t Size)
Set the array size to N, which the current array must have enough capacity for.
Definition: SmallVector.h:66
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
bool home_directory(SmallVectorImpl< char > &result)
Get the user&#39;s home directory.
std::error_code access(const Twine &Path, AccessMode Mode)
Can the file be accessed?
void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
Definition: MD5.cpp:234
StringRef stem(StringRef path, Style style=Style::native)
Get stem.
Definition: Path.cpp:591
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
bool has_root_name(const Twine &path, Style style=Style::native)
Has root name?
Definition: Path.cpp:631
bool is_symlink_file(const basic_file_status &status)
Does status represent a symlink file?
Definition: Path.cpp:1060
bool exists(const basic_file_status &status)
Does file exist?
Definition: Path.cpp:1021
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1237
iterator end() const
Definition: StringRef.h:103
FSEntity
Definition: Path.cpp:161
StringRef extension(StringRef path, Style style=Style::native)
Get extension.
Definition: Path.cpp:604
static std::error_code copy_file_internal(int ReadFD, int WriteFD)
Definition: Path.cpp:937