LLVM  6.0.0svn
Path.cpp
Go to the documentation of this file.
1 //===-- Path.cpp - Implement OS Path Concept ------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the operating system Path API.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Support/Path.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/Support/Endian.h"
17 #include "llvm/Support/Errc.h"
20 #include "llvm/Support/Process.h"
21 #include <cctype>
22 #include <cstring>
23 
24 #if !defined(_MSC_VER) && !defined(__MINGW32__)
25 #include <unistd.h>
26 #else
27 #include <io.h>
28 #endif
29 
30 using namespace llvm;
31 using namespace llvm::support::endian;
32 
33 namespace {
34  using llvm::StringRef;
37 
38  inline Style real_style(Style style) {
39 #ifdef LLVM_ON_WIN32
40  return (style == Style::posix) ? Style::posix : Style::windows;
41 #else
42  return (style == Style::windows) ? Style::windows : Style::posix;
43 #endif
44  }
45 
46  inline const char *separators(Style style) {
47  if (real_style(style) == Style::windows)
48  return "\\/";
49  return "/";
50  }
51 
52  inline char preferred_separator(Style style) {
53  if (real_style(style) == Style::windows)
54  return '\\';
55  return '/';
56  }
57 
58  StringRef find_first_component(StringRef path, Style style) {
59  // Look for this first component in the following order.
60  // * empty (in this case we return an empty string)
61  // * either C: or {//,\\}net.
62  // * {/,\}
63  // * {file,directory}name
64 
65  if (path.empty())
66  return path;
67 
68  if (real_style(style) == Style::windows) {
69  // C:
70  if (path.size() >= 2 &&
71  std::isalpha(static_cast<unsigned char>(path[0])) && path[1] == ':')
72  return path.substr(0, 2);
73  }
74 
75  // //net
76  if ((path.size() > 2) && is_separator(path[0], style) &&
77  path[0] == path[1] && !is_separator(path[2], style)) {
78  // Find the next directory separator.
79  size_t end = path.find_first_of(separators(style), 2);
80  return path.substr(0, end);
81  }
82 
83  // {/,\}
84  if (is_separator(path[0], style))
85  return path.substr(0, 1);
86 
87  // * {file,directory}name
88  size_t end = path.find_first_of(separators(style));
89  return path.substr(0, end);
90  }
91 
92  size_t filename_pos(StringRef str, Style style) {
93  if (str.size() == 2 && is_separator(str[0], style) && str[0] == str[1])
94  return 0;
95 
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  size_t root_dir_start(StringRef str, Style style) {
113  // case "c:/"
114  if (real_style(style) == Style::windows) {
115  if (str.size() > 2 && str[1] == ':' && is_separator(str[2], style))
116  return 2;
117  }
118 
119  // case "//"
120  if (str.size() == 2 && is_separator(str[0], style) && str[0] == str[1])
121  return StringRef::npos;
122 
123  // case "//net"
124  if (str.size() > 3 && is_separator(str[0], style) && str[0] == str[1] &&
125  !is_separator(str[2], style)) {
126  return str.find_first_of(separators(style), 2);
127  }
128 
129  // case "/"
130  if (str.size() > 0 && is_separator(str[0], style))
131  return 0;
132 
133  return StringRef::npos;
134  }
135 
136  size_t parent_path_end(StringRef path, Style style) {
137  size_t end_pos = filename_pos(path, style);
138 
139  bool filename_was_sep =
140  path.size() > 0 && is_separator(path[end_pos], style);
141 
142  // Skip separators except for root dir.
143  size_t root_dir_pos = root_dir_start(path.substr(0, end_pos), style);
144 
145  while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
146  is_separator(path[end_pos - 1], style))
147  --end_pos;
148 
149  if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
150  return StringRef::npos;
151 
152  return end_pos;
153  }
154 } // end unnamed namespace
155 
156 enum FSEntity {
160 };
161 
162 static std::error_code createUniqueEntity(const Twine &Model, int &ResultFD,
163  SmallVectorImpl<char> &ResultPath,
164  bool MakeAbsolute, unsigned Mode,
165  FSEntity Type) {
166  SmallString<128> ModelStorage;
167  Model.toVector(ModelStorage);
168 
169  if (MakeAbsolute) {
170  // Make model absolute by prepending a temp directory if it's not already.
171  if (!sys::path::is_absolute(Twine(ModelStorage))) {
172  SmallString<128> TDir;
174  sys::path::append(TDir, Twine(ModelStorage));
175  ModelStorage.swap(TDir);
176  }
177  }
178 
179  // From here on, DO NOT modify model. It may be needed if the randomly chosen
180  // path already exists.
181  ResultPath = ModelStorage;
182  // Null terminate.
183  ResultPath.push_back(0);
184  ResultPath.pop_back();
185 
186 retry_random_path:
187  // Replace '%' with random chars.
188  for (unsigned i = 0, e = ModelStorage.size(); i != e; ++i) {
189  if (ModelStorage[i] == '%')
190  ResultPath[i] = "0123456789abcdef"[sys::Process::GetRandomNumber() & 15];
191  }
192 
193  // Try to open + create the file.
194  switch (Type) {
195  case FS_File: {
196  if (std::error_code EC =
197  sys::fs::openFileForWrite(Twine(ResultPath.begin()), ResultFD,
198  sys::fs::F_RW | sys::fs::F_Excl, Mode)) {
199  if (EC == errc::file_exists)
200  goto retry_random_path;
201  return EC;
202  }
203 
204  return std::error_code();
205  }
206 
207  case FS_Name: {
208  std::error_code EC =
211  return std::error_code();
212  if (EC)
213  return EC;
214  goto retry_random_path;
215  }
216 
217  case FS_Dir: {
218  if (std::error_code EC =
219  sys::fs::create_directory(ResultPath.begin(), false)) {
220  if (EC == errc::file_exists)
221  goto retry_random_path;
222  return EC;
223  }
224  return std::error_code();
225  }
226  }
227  llvm_unreachable("Invalid Type");
228 }
229 
230 namespace llvm {
231 namespace sys {
232 namespace path {
233 
235  const_iterator i;
236  i.Path = path;
237  i.Component = find_first_component(path, style);
238  i.Position = 0;
239  i.S = style;
240  return i;
241 }
242 
244  const_iterator i;
245  i.Path = path;
246  i.Position = path.size();
247  return i;
248 }
249 
250 const_iterator &const_iterator::operator++() {
251  assert(Position < Path.size() && "Tried to increment past end!");
252 
253  // Increment Position to past the current component
254  Position += Component.size();
255 
256  // Check for end.
257  if (Position == Path.size()) {
258  Component = StringRef();
259  return *this;
260  }
261 
262  // Both POSIX and Windows treat paths that begin with exactly two separators
263  // specially.
264  bool was_net = Component.size() > 2 && is_separator(Component[0], S) &&
265  Component[1] == Component[0] && !is_separator(Component[2], S);
266 
267  // Handle separators.
268  if (is_separator(Path[Position], S)) {
269  // Root dir.
270  if (was_net ||
271  // c:/
272  (real_style(S) == Style::windows && Component.endswith(":"))) {
273  Component = Path.substr(Position, 1);
274  return *this;
275  }
276 
277  // Skip extra separators.
278  while (Position != Path.size() && is_separator(Path[Position], S)) {
279  ++Position;
280  }
281 
282  // Treat trailing '/' as a '.'.
283  if (Position == Path.size()) {
284  --Position;
285  Component = ".";
286  return *this;
287  }
288  }
289 
290  // Find next component.
291  size_t end_pos = Path.find_first_of(separators(S), Position);
292  Component = Path.slice(Position, end_pos);
293 
294  return *this;
295 }
296 
298  return Path.begin() == RHS.Path.begin() && Position == RHS.Position;
299 }
300 
302  return Position - RHS.Position;
303 }
304 
307  I.Path = Path;
308  I.Position = Path.size();
309  I.S = style;
310  return ++I;
311 }
312 
315  I.Path = Path;
316  I.Component = Path.substr(0, 0);
317  I.Position = 0;
318  return I;
319 }
320 
321 reverse_iterator &reverse_iterator::operator++() {
322  // If we're at the end and the previous char was a '/', return '.' unless
323  // we are the root path.
324  size_t root_dir_pos = root_dir_start(Path, S);
325  if (Position == Path.size() && Path.size() > root_dir_pos + 1 &&
326  is_separator(Path[Position - 1], S)) {
327  --Position;
328  Component = ".";
329  return *this;
330  }
331 
332  // Skip separators unless it's the root directory.
333  size_t end_pos = Position;
334 
335  while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
336  is_separator(Path[end_pos - 1], S))
337  --end_pos;
338 
339  // Find next separator.
340  size_t start_pos = filename_pos(Path.substr(0, end_pos), S);
341  Component = Path.slice(start_pos, end_pos);
342  Position = start_pos;
343  return *this;
344 }
345 
347  return Path.begin() == RHS.Path.begin() && Component == RHS.Component &&
348  Position == RHS.Position;
349 }
350 
352  return Position - RHS.Position;
353 }
354 
356  const_iterator b = begin(path, style), pos = b, e = end(path);
357  if (b != e) {
358  bool has_net =
359  b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
360  bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
361 
362  if (has_net || has_drive) {
363  if ((++pos != e) && is_separator((*pos)[0], style)) {
364  // {C:/,//net/}, so get the first two components.
365  return path.substr(0, b->size() + pos->size());
366  } else {
367  // just {C:,//net}, return the first component.
368  return *b;
369  }
370  }
371 
372  // POSIX style root directory.
373  if (is_separator((*b)[0], style)) {
374  return *b;
375  }
376  }
377 
378  return StringRef();
379 }
380 
382  const_iterator b = begin(path, style), e = end(path);
383  if (b != e) {
384  bool has_net =
385  b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
386  bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
387 
388  if (has_net || has_drive) {
389  // just {C:,//net}, return the first component.
390  return *b;
391  }
392  }
393 
394  // No path or no name.
395  return StringRef();
396 }
397 
399  const_iterator b = begin(path, style), pos = b, e = end(path);
400  if (b != e) {
401  bool has_net =
402  b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
403  bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
404 
405  if ((has_net || has_drive) &&
406  // {C:,//net}, skip to the next component.
407  (++pos != e) && is_separator((*pos)[0], style)) {
408  return *pos;
409  }
410 
411  // POSIX style root directory.
412  if (!has_net && is_separator((*b)[0], style)) {
413  return *b;
414  }
415  }
416 
417  // No path or no root.
418  return StringRef();
419 }
420 
422  StringRef root = root_path(path, style);
423  return path.substr(root.size());
424 }
425 
426 void append(SmallVectorImpl<char> &path, Style style, const Twine &a,
427  const Twine &b, const Twine &c, const Twine &d) {
428  SmallString<32> a_storage;
429  SmallString<32> b_storage;
430  SmallString<32> c_storage;
431  SmallString<32> d_storage;
432 
433  SmallVector<StringRef, 4> components;
434  if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
435  if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
436  if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
437  if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
438 
439  for (auto &component : components) {
440  bool path_has_sep =
441  !path.empty() && is_separator(path[path.size() - 1], style);
442  if (path_has_sep) {
443  // Strip separators from beginning of component.
444  size_t loc = component.find_first_not_of(separators(style));
445  StringRef c = component.substr(loc);
446 
447  // Append it.
448  path.append(c.begin(), c.end());
449  continue;
450  }
451 
452  bool component_has_sep =
453  !component.empty() && is_separator(component[0], style);
454  if (!component_has_sep &&
455  !(path.empty() || has_root_name(component, style))) {
456  // Add a separator.
457  path.push_back(preferred_separator(style));
458  }
459 
460  path.append(component.begin(), component.end());
461  }
462 }
463 
464 void append(SmallVectorImpl<char> &path, const Twine &a, const Twine &b,
465  const Twine &c, const Twine &d) {
466  append(path, Style::native, a, b, c, d);
467 }
468 
470  const_iterator end, Style style) {
471  for (; begin != end; ++begin)
472  path::append(path, style, *begin);
473 }
474 
476  size_t end_pos = parent_path_end(path, style);
477  if (end_pos == StringRef::npos)
478  return StringRef();
479  else
480  return path.substr(0, end_pos);
481 }
482 
484  size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()), style);
485  if (end_pos != StringRef::npos)
486  path.set_size(end_pos);
487 }
488 
490  Style style) {
491  StringRef p(path.begin(), path.size());
492  SmallString<32> ext_storage;
493  StringRef ext = extension.toStringRef(ext_storage);
494 
495  // Erase existing extension.
496  size_t pos = p.find_last_of('.');
497  if (pos != StringRef::npos && pos >= filename_pos(p, style))
498  path.set_size(pos);
499 
500  // Append '.' if needed.
501  if (ext.size() > 0 && ext[0] != '.')
502  path.push_back('.');
503 
504  // Append extension.
505  path.append(ext.begin(), ext.end());
506 }
507 
509  const StringRef &OldPrefix, const StringRef &NewPrefix,
510  Style style) {
511  if (OldPrefix.empty() && NewPrefix.empty())
512  return;
513 
514  StringRef OrigPath(Path.begin(), Path.size());
515  if (!OrigPath.startswith(OldPrefix))
516  return;
517 
518  // If prefixes have the same size we can simply copy the new one over.
519  if (OldPrefix.size() == NewPrefix.size()) {
520  std::copy(NewPrefix.begin(), NewPrefix.end(), Path.begin());
521  return;
522  }
523 
524  StringRef RelPath = OrigPath.substr(OldPrefix.size());
525  SmallString<256> NewPath;
526  path::append(NewPath, style, NewPrefix);
527  path::append(NewPath, style, RelPath);
528  Path.swap(NewPath);
529 }
530 
531 void native(const Twine &path, SmallVectorImpl<char> &result, Style style) {
532  assert((!path.isSingleStringRef() ||
533  path.getSingleStringRef().data() != result.data()) &&
534  "path and result are not allowed to overlap!");
535  // Clear result.
536  result.clear();
537  path.toVector(result);
538  native(result, style);
539 }
540 
541 void native(SmallVectorImpl<char> &Path, Style style) {
542  if (Path.empty())
543  return;
544  if (real_style(style) == Style::windows) {
545  std::replace(Path.begin(), Path.end(), '/', '\\');
546  if (Path[0] == '~' && (Path.size() == 1 || is_separator(Path[1], style))) {
547  SmallString<128> PathHome;
548  home_directory(PathHome);
549  PathHome.append(Path.begin() + 1, Path.end());
550  Path = PathHome;
551  }
552  } else {
553  for (auto PI = Path.begin(), PE = Path.end(); PI < PE; ++PI) {
554  if (*PI == '\\') {
555  auto PN = PI + 1;
556  if (PN < PE && *PN == '\\')
557  ++PI; // increment once, the for loop will move over the escaped slash
558  else
559  *PI = '/';
560  }
561  }
562  }
563 }
564 
565 std::string convert_to_slash(StringRef path, Style style) {
566  if (real_style(style) != Style::windows)
567  return path;
568 
569  std::string s = path.str();
570  std::replace(s.begin(), s.end(), '\\', '/');
571  return s;
572 }
573 
574 StringRef filename(StringRef path, Style style) { return *rbegin(path, style); }
575 
577  StringRef fname = filename(path, style);
578  size_t pos = fname.find_last_of('.');
579  if (pos == StringRef::npos)
580  return fname;
581  else
582  if ((fname.size() == 1 && fname == ".") ||
583  (fname.size() == 2 && fname == ".."))
584  return fname;
585  else
586  return fname.substr(0, pos);
587 }
588 
590  StringRef fname = filename(path, style);
591  size_t pos = fname.find_last_of('.');
592  if (pos == StringRef::npos)
593  return StringRef();
594  else
595  if ((fname.size() == 1 && fname == ".") ||
596  (fname.size() == 2 && fname == ".."))
597  return StringRef();
598  else
599  return fname.substr(pos);
600 }
601 
602 bool is_separator(char value, Style style) {
603  if (value == '/')
604  return true;
605  if (real_style(style) == Style::windows)
606  return value == '\\';
607  return false;
608 }
609 
611  if (real_style(style) == Style::windows)
612  return "\\";
613  return "/";
614 }
615 
616 bool has_root_name(const Twine &path, Style style) {
617  SmallString<128> path_storage;
618  StringRef p = path.toStringRef(path_storage);
619 
620  return !root_name(p, style).empty();
621 }
622 
623 bool has_root_directory(const Twine &path, Style style) {
624  SmallString<128> path_storage;
625  StringRef p = path.toStringRef(path_storage);
626 
627  return !root_directory(p, style).empty();
628 }
629 
630 bool has_root_path(const Twine &path, Style style) {
631  SmallString<128> path_storage;
632  StringRef p = path.toStringRef(path_storage);
633 
634  return !root_path(p, style).empty();
635 }
636 
637 bool has_relative_path(const Twine &path, Style style) {
638  SmallString<128> path_storage;
639  StringRef p = path.toStringRef(path_storage);
640 
641  return !relative_path(p, style).empty();
642 }
643 
644 bool has_filename(const Twine &path, Style style) {
645  SmallString<128> path_storage;
646  StringRef p = path.toStringRef(path_storage);
647 
648  return !filename(p, style).empty();
649 }
650 
651 bool has_parent_path(const Twine &path, Style style) {
652  SmallString<128> path_storage;
653  StringRef p = path.toStringRef(path_storage);
654 
655  return !parent_path(p, style).empty();
656 }
657 
658 bool has_stem(const Twine &path, Style style) {
659  SmallString<128> path_storage;
660  StringRef p = path.toStringRef(path_storage);
661 
662  return !stem(p, style).empty();
663 }
664 
665 bool has_extension(const Twine &path, Style style) {
666  SmallString<128> path_storage;
667  StringRef p = path.toStringRef(path_storage);
668 
669  return !extension(p, style).empty();
670 }
671 
672 bool is_absolute(const Twine &path, Style style) {
673  SmallString<128> path_storage;
674  StringRef p = path.toStringRef(path_storage);
675 
676  bool rootDir = has_root_directory(p, style);
677  bool rootName =
678  (real_style(style) != Style::windows) || has_root_name(p, style);
679 
680  return rootDir && rootName;
681 }
682 
683 bool is_relative(const Twine &path, Style style) {
684  return !is_absolute(path, style);
685 }
686 
688  // Remove leading "./" (or ".//" or "././" etc.)
689  while (Path.size() > 2 && Path[0] == '.' && is_separator(Path[1], style)) {
690  Path = Path.substr(2);
691  while (Path.size() > 0 && is_separator(Path[0], style))
692  Path = Path.substr(1);
693  }
694  return Path;
695 }
696 
697 static SmallString<256> remove_dots(StringRef path, bool remove_dot_dot,
698  Style style) {
699  SmallVector<StringRef, 16> components;
700 
701  // Skip the root path, then look for traversal in the components.
702  StringRef rel = path::relative_path(path, style);
703  for (StringRef C :
704  llvm::make_range(path::begin(rel, style), path::end(rel))) {
705  if (C == ".")
706  continue;
707  // Leading ".." will remain in the path unless it's at the root.
708  if (remove_dot_dot && C == "..") {
709  if (!components.empty() && components.back() != "..") {
710  components.pop_back();
711  continue;
712  }
713  if (path::is_absolute(path, style))
714  continue;
715  }
716  components.push_back(C);
717  }
718 
719  SmallString<256> buffer = path::root_path(path, style);
720  for (StringRef C : components)
721  path::append(buffer, style, C);
722  return buffer;
723 }
724 
725 bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot,
726  Style style) {
727  StringRef p(path.data(), path.size());
728 
729  SmallString<256> result = remove_dots(p, remove_dot_dot, style);
730  if (result == path)
731  return false;
732 
733  path.swap(result);
734  return true;
735 }
736 
737 } // end namespace path
738 
739 namespace fs {
740 
741 std::error_code getUniqueID(const Twine Path, UniqueID &Result) {
742  file_status Status;
743  std::error_code EC = status(Path, Status);
744  if (EC)
745  return EC;
746  Result = Status.getUniqueID();
747  return std::error_code();
748 }
749 
750 std::error_code createUniqueFile(const Twine &Model, int &ResultFd,
751  SmallVectorImpl<char> &ResultPath,
752  unsigned Mode) {
753  return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
754 }
755 
756 std::error_code createUniqueFile(const Twine &Model,
757  SmallVectorImpl<char> &ResultPath) {
758  int Dummy;
759  return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
760 }
761 
762 static std::error_code
763 createTemporaryFile(const Twine &Model, int &ResultFD,
765  SmallString<128> Storage;
766  StringRef P = Model.toNullTerminatedStringRef(Storage);
768  "Model must be a simple filename.");
769  // Use P.begin() so that createUniqueEntity doesn't need to recreate Storage.
770  return createUniqueEntity(P.begin(), ResultFD, ResultPath,
771  true, owner_read | owner_write, Type);
772 }
773 
774 static std::error_code
775 createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
777  const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
778  return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
779  Type);
780 }
781 
782 std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
783  int &ResultFD,
784  SmallVectorImpl<char> &ResultPath) {
785  return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File);
786 }
787 
788 std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
789  SmallVectorImpl<char> &ResultPath) {
790  int Dummy;
791  return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
792 }
793 
794 
795 // This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
796 // for consistency. We should try using mkdtemp.
797 std::error_code createUniqueDirectory(const Twine &Prefix,
798  SmallVectorImpl<char> &ResultPath) {
799  int Dummy;
800  return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath,
801  true, 0, FS_Dir);
802 }
803 
804 static std::error_code make_absolute(const Twine &current_directory,
805  SmallVectorImpl<char> &path,
806  bool use_current_directory) {
807  StringRef p(path.data(), path.size());
808 
809  bool rootDirectory = path::has_root_directory(p);
810  bool rootName =
811  (real_style(Style::native) != Style::windows) || path::has_root_name(p);
812 
813  // Already absolute.
814  if (rootName && rootDirectory)
815  return std::error_code();
816 
817  // All of the following conditions will need the current directory.
818  SmallString<128> current_dir;
819  if (use_current_directory)
820  current_directory.toVector(current_dir);
821  else if (std::error_code ec = current_path(current_dir))
822  return ec;
823 
824  // Relative path. Prepend the current directory.
825  if (!rootName && !rootDirectory) {
826  // Append path to the current directory.
827  path::append(current_dir, p);
828  // Set path to the result.
829  path.swap(current_dir);
830  return std::error_code();
831  }
832 
833  if (!rootName && rootDirectory) {
834  StringRef cdrn = path::root_name(current_dir);
835  SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
836  path::append(curDirRootName, p);
837  // Set path to the result.
838  path.swap(curDirRootName);
839  return std::error_code();
840  }
841 
842  if (rootName && !rootDirectory) {
843  StringRef pRootName = path::root_name(p);
844  StringRef bRootDirectory = path::root_directory(current_dir);
845  StringRef bRelativePath = path::relative_path(current_dir);
846  StringRef pRelativePath = path::relative_path(p);
847 
848  SmallString<128> res;
849  path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
850  path.swap(res);
851  return std::error_code();
852  }
853 
854  llvm_unreachable("All rootName and rootDirectory combinations should have "
855  "occurred above!");
856 }
857 
858 std::error_code make_absolute(const Twine &current_directory,
859  SmallVectorImpl<char> &path) {
860  return make_absolute(current_directory, path, true);
861 }
862 
863 std::error_code make_absolute(SmallVectorImpl<char> &path) {
864  return make_absolute(Twine(), path, false);
865 }
866 
867 std::error_code create_directories(const Twine &Path, bool IgnoreExisting,
868  perms Perms) {
869  SmallString<128> PathStorage;
870  StringRef P = Path.toStringRef(PathStorage);
871 
872  // Be optimistic and try to create the directory
873  std::error_code EC = create_directory(P, IgnoreExisting, Perms);
874  // If we succeeded, or had any error other than the parent not existing, just
875  // return it.
877  return EC;
878 
879  // We failed because of a no_such_file_or_directory, try to create the
880  // parent.
881  StringRef Parent = path::parent_path(P);
882  if (Parent.empty())
883  return EC;
884 
885  if ((EC = create_directories(Parent, IgnoreExisting, Perms)))
886  return EC;
887 
888  return create_directory(P, IgnoreExisting, Perms);
889 }
890 
891 std::error_code copy_file(const Twine &From, const Twine &To) {
892  int ReadFD, WriteFD;
893  if (std::error_code EC = openFileForRead(From, ReadFD))
894  return EC;
895  if (std::error_code EC = openFileForWrite(To, WriteFD, F_None)) {
896  close(ReadFD);
897  return EC;
898  }
899 
900  const size_t BufSize = 4096;
901  char *Buf = new char[BufSize];
902  int BytesRead = 0, BytesWritten = 0;
903  for (;;) {
904  BytesRead = read(ReadFD, Buf, BufSize);
905  if (BytesRead <= 0)
906  break;
907  while (BytesRead) {
908  BytesWritten = write(WriteFD, Buf, BytesRead);
909  if (BytesWritten < 0)
910  break;
911  BytesRead -= BytesWritten;
912  }
913  if (BytesWritten < 0)
914  break;
915  }
916  close(ReadFD);
917  close(WriteFD);
918  delete[] Buf;
919 
920  if (BytesRead < 0 || BytesWritten < 0)
921  return std::error_code(errno, std::generic_category());
922  return std::error_code();
923 }
924 
926  MD5 Hash;
927 
928  constexpr size_t BufSize = 4096;
929  std::vector<uint8_t> Buf(BufSize);
930  int BytesRead = 0;
931  for (;;) {
932  BytesRead = read(FD, Buf.data(), BufSize);
933  if (BytesRead <= 0)
934  break;
935  Hash.update(makeArrayRef(Buf.data(), BytesRead));
936  }
937 
938  if (BytesRead < 0)
939  return std::error_code(errno, std::generic_category());
940  MD5::MD5Result Result;
941  Hash.final(Result);
942  return Result;
943 }
944 
946  int FD;
947  if (auto EC = openFileForRead(Path, FD))
948  return EC;
949 
950  auto Result = md5_contents(FD);
951  close(FD);
952  return Result;
953 }
954 
956  return status_known(status) && status.type() != file_type::file_not_found;
957 }
958 
960  return s.type() != file_type::status_error;
961 }
962 
963 file_type get_file_type(const Twine &Path, bool Follow) {
964  file_status st;
965  if (status(Path, st, Follow))
966  return file_type::status_error;
967  return st.type();
968 }
969 
971  return status.type() == file_type::directory_file;
972 }
973 
974 std::error_code is_directory(const Twine &path, bool &result) {
975  file_status st;
976  if (std::error_code ec = status(path, st))
977  return ec;
978  result = is_directory(st);
979  return std::error_code();
980 }
981 
983  return status.type() == file_type::regular_file;
984 }
985 
986 std::error_code is_regular_file(const Twine &path, bool &result) {
987  file_status st;
988  if (std::error_code ec = status(path, st))
989  return ec;
990  result = is_regular_file(st);
991  return std::error_code();
992 }
993 
995  return status.type() == file_type::symlink_file;
996 }
997 
998 std::error_code is_symlink_file(const Twine &path, bool &result) {
999  file_status st;
1000  if (std::error_code ec = status(path, st, false))
1001  return ec;
1002  result = is_symlink_file(st);
1003  return std::error_code();
1004 }
1005 
1007  return exists(status) &&
1008  !is_regular_file(status) &&
1009  !is_directory(status);
1010 }
1011 
1012 std::error_code is_other(const Twine &Path, bool &Result) {
1013  file_status FileStatus;
1014  if (std::error_code EC = status(Path, FileStatus))
1015  return EC;
1016  Result = is_other(FileStatus);
1017  return std::error_code();
1018 }
1019 
1020 void directory_entry::replace_filename(const Twine &filename,
1021  basic_file_status st) {
1022  SmallString<128> path = path::parent_path(Path);
1023  path::append(path, filename);
1024  Path = path.str();
1025  Status = st;
1026 }
1027 
1029  file_status Status;
1030  if (std::error_code EC = status(Path, Status))
1031  return EC;
1032 
1033  return Status.permissions();
1034 }
1035 
1036 } // end namespace fs
1037 } // end namespace sys
1038 } // end namespace llvm
1039 
1040 // Include the truly platform-specific parts.
1041 #if defined(LLVM_ON_UNIX)
1042 #include "Unix/Path.inc"
1043 #endif
1044 #if defined(LLVM_ON_WIN32)
1045 #include "Windows/Path.inc"
1046 #endif
1047 
1048 namespace llvm {
1049 namespace sys {
1050 namespace path {
1051 
1053  const Twine &Path2, const Twine &Path3) {
1054  if (getUserCacheDir(Result)) {
1055  append(Result, Path1, Path2, Path3);
1056  return true;
1057  }
1058  return false;
1059 }
1060 
1061 } // end namespace path
1062 } // end namsspace sys
1063 } // end namespace llvm
uint64_t CallInst * C
void push_back(const T &Elt)
Definition: SmallVector.h:212
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:602
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:867
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:243
Represents either an error or a value T.
Definition: ErrorOr.h:69
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:483
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:228
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:234
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:508
SI Whole Quad Mode
bool status_known(const basic_file_status &s)
Is status available?
Definition: Path.cpp:959
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:136
std::error_code createUniqueFile(const Twine &Model, int &ResultFD, SmallVectorImpl< char > &ResultPath, unsigned Mode=all_read|all_write)
Create a uniquely named file.
Definition: Path.cpp:750
std::error_code openFileForRead(const Twine &Name, int &ResultFD, SmallVectorImpl< char > *RealPath=nullptr)
F_Excl - When opening a file, this flag makes raw_fd_ostream report an error if the file already exis...
Definition: FileSystem.h:721
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
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:408
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:463
static std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, llvm::SmallVectorImpl< char > &ResultPath, FSEntity Type)
Definition: Path.cpp:775
bool has_extension(const Twine &path, Style style=Style::native)
Has extension?
Definition: Path.cpp:665
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:421
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:970
std::string convert_to_slash(StringRef path, Style style=Style::native)
Replaces backslashes with slashes if Windows.
Definition: Path.cpp:565
bool is_regular_file(const basic_file_status &status)
Does status represent a regular file?
Definition: Path.cpp:982
StringRef remove_leading_dotslash(StringRef path, Style style=Style::native)
Remove redundant leading "./" pieces and consecutive separators.
Definition: Path.cpp:687
static std::error_code createUniqueEntity(const Twine &Model, int &ResultFD, SmallVectorImpl< char > &ResultPath, bool MakeAbsolute, unsigned Mode, FSEntity Type)
Definition: Path.cpp:162
Represents the result of a call to sys::fs::status().
Definition: FileSystem.h:218
Definition: Path.cpp:157
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
ErrorOr< MD5::MD5Result > md5_contents(int FD)
Compute an MD5 hash of a file&#39;s contents.
Definition: Path.cpp:925
ErrorOr< perms > getPermissions(const Twine &Path)
Get file permissions.
Definition: Path.cpp:1028
bool has_stem(const Twine &path, Style style=Style::native)
Has stem?
Definition: Path.cpp:658
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:464
reverse_iterator rbegin(StringRef path, Style style=Style::native)
Get reverse begin iterator over path.
Definition: Path.cpp:305
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
Definition: MD5.cpp:189
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition: Path.cpp:672
StringRef root_path(StringRef path, Style style=Style::native)
Get root path.
Definition: Path.cpp:355
StringRef getSingleStringRef() const
This returns the twine as a single StringRef.
Definition: Twine.h:447
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:267
bool isSingleStringRef() const
Return true if this twine can be dynamically accessed as a single StringRef value with getSingleStrin...
Definition: Twine.h:414
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
Open the file for read and write.
Definition: FileSystem.h:733
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:598
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition: Endian.h:100
void append(in_iter S, in_iter E)
Append from an iterator pair.
Definition: SmallString.h:75
Represents the result of a call to directory_iterator::status().
Definition: FileSystem.h:147
std::error_code copy_file(const Twine &From, const Twine &To)
Copy the contents of From to To.
Definition: Path.cpp:891
#define P(N)
static SmallString< 256 > remove_dots(StringRef path, bool remove_dot_dot, Style style)
Definition: Path.cpp:697
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
void swap(SmallVectorImpl &RHS)
Definition: SmallVector.h:696
bool has_parent_path(const Twine &path, Style style=Style::native)
Has parent path?
Definition: Path.cpp:651
UniqueID getUniqueID() const
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:116
bool has_filename(const Twine &path, Style style=Style::native)
Has filename?
Definition: Path.cpp:644
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
bool user_cache_directory(SmallVectorImpl< char > &Result, const Twine &Path1, const Twine &Path2="", const Twine &Path3="")
Get the user&#39;s cache directory.
Definition: Path.cpp:1052
std::error_code getUniqueID(const Twine Path, UniqueID &Result)
Definition: Path.cpp:741
file_type get_file_type(const Twine &Path, bool Follow=true)
Does status represent a directory?
Definition: Path.cpp:963
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
Definition: Path.cpp:475
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Definition: Twine.cpp:32
StringRef get_separator(Style style=Style::native)
Return the preferred separator for this platform.
Definition: Path.cpp:610
#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.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:864
StringRef root_directory(StringRef path, Style style=Style::native)
Get root directory.
Definition: Path.cpp:398
void replace_extension(SmallVectorImpl< char > &path, const Twine &extension, Style style=Style::native)
Replace the file extension of path with extension.
Definition: Path.cpp:489
StringRef root_name(StringRef path, Style style=Style::native)
Get root name.
Definition: Path.cpp:381
std::error_code createUniqueDirectory(const Twine &Prefix, SmallVectorImpl< char > &ResultPath)
Definition: Path.cpp:797
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:1006
bool has_root_directory(const Twine &path, Style style=Style::native)
Has root directory?
Definition: Path.cpp:623
Path iterator.
Definition: Path.h:52
iterator begin() const
Definition: StringRef.h:106
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:398
bool has_root_path(const Twine &path, Style style=Style::native)
Has root path?
Definition: Path.cpp:630
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:637
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:120
void set_size(size_type N)
Set the array size to N, which the current array must have enough capacity for.
Definition: SmallVector.h:689
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:143
static const size_t npos
Definition: StringRef.h:51
file_type
An enumeration for the file system&#39;s view of the type.
Definition: FileSystem.h:56
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:574
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:61
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
Definition: Endian.h:66
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:395
#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:683
APInt operator-(APInt)
Definition: APInt.h:2023
Definition: MD5.h:41
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:313
void system_temp_directory(bool erasedOnReboot, SmallVectorImpl< char > &result)
Get the typical temporary directory for the system, e.g., "/var/tmp" or "C:/TEMP".
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef relative_path(StringRef path, Style style=Style::native)
Get relative path.
Definition: Path.cpp:421
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:576
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static Expected< std::string > replace(StringRef S, StringRef From, StringRef To)
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:1946
bool has_root_name(const Twine &path, Style style=Style::native)
Has root name?
Definition: Path.cpp:616
bool is_symlink_file(const basic_file_status &status)
Does status represent a symlink file?
Definition: Path.cpp:994
bool exists(const basic_file_status &status)
Does file exist?
Definition: Path.cpp:955
iterator end() const
Definition: StringRef.h:108
FSEntity
Definition: Path.cpp:156
StringRef extension(StringRef path, Style style=Style::native)
Get extension.
Definition: Path.cpp:589
static std::error_code make_absolute(const Twine &current_directory, SmallVectorImpl< char > &path, bool use_current_directory)
Definition: Path.cpp:804
std::error_code openFileForWrite(const Twine &Name, int &ResultFD, OpenFlags Flags, unsigned Mode=0666)