blob: 9fd6652ce4b8c5fbddca3ee3bdb1cf111ec198f9 [file] [log] [blame]
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001//===-- Path.cpp - Implement OS Path Concept ------------------------------===//
Michael J. Spencerebad2f92010-11-29 22:28:51 +00002//
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//
Rafael Espindolaf1fc3822013-06-26 19:33:03 +000010// This file implements the operating system Path API.
Michael J. Spencerebad2f92010-11-29 22:28:51 +000011//
12//===----------------------------------------------------------------------===//
13
Zachary Turner82a0c972017-03-20 23:33:18 +000014#include "llvm/Support/Path.h"
15#include "llvm/ADT/ArrayRef.h"
Rui Ueyama5c69ff52014-09-11 22:34:32 +000016#include "llvm/Support/COFF.h"
17#include "llvm/Support/Endian.h"
Rafael Espindola2a826e42014-06-13 17:20:48 +000018#include "llvm/Support/Errc.h"
Michael J. Spencerebad2f92010-11-29 22:28:51 +000019#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/Support/FileSystem.h"
Zachary Turner82a0c972017-03-20 23:33:18 +000021#include "llvm/Support/MachO.h"
Aaron Ballman07e76182014-02-11 03:40:14 +000022#include "llvm/Support/Process.h"
Michael J. Spencerebad2f92010-11-29 22:28:51 +000023#include <cctype>
Michael J. Spencer848f46b2010-12-28 01:49:01 +000024#include <cstring>
Rafael Espindola4526b1d2013-06-18 17:01:00 +000025
26#if !defined(_MSC_VER) && !defined(__MINGW32__)
Douglas Gregora86ddf02013-03-21 21:46:10 +000027#include <unistd.h>
Rafael Espindola4526b1d2013-06-18 17:01:00 +000028#else
29#include <io.h>
Douglas Gregora86ddf02013-03-21 21:46:10 +000030#endif
Michael J. Spencerebad2f92010-11-29 22:28:51 +000031
Rafael Espindola7a0b6402014-02-24 03:07:41 +000032using namespace llvm;
Rui Ueyama3206b792015-03-02 21:19:12 +000033using namespace llvm::support::endian;
Rafael Espindola7a0b6402014-02-24 03:07:41 +000034
Michael J. Spencerebad2f92010-11-29 22:28:51 +000035namespace {
36 using llvm::StringRef;
Zhanyong Wan606bb1a2011-02-11 21:24:40 +000037 using llvm::sys::path::is_separator;
Zachary Turner5c5091f2017-03-16 22:28:04 +000038 using llvm::sys::path::Style;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000039
Zachary Turner5c5091f2017-03-16 22:28:04 +000040 inline Style real_style(Style style) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000041#ifdef LLVM_ON_WIN32
Zachary Turner5c5091f2017-03-16 22:28:04 +000042 return (style == Style::posix) ? Style::posix : Style::windows;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000043#else
Zachary Turner5c5091f2017-03-16 22:28:04 +000044 return (style == Style::windows) ? Style::windows : Style::posix;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000045#endif
Zachary Turner5c5091f2017-03-16 22:28:04 +000046 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +000047
Zachary Turner5c5091f2017-03-16 22:28:04 +000048 inline const char *separators(Style style) {
49 if (real_style(style) == Style::windows)
50 return "\\/";
51 return "/";
52 }
53
54 inline char preferred_separator(Style style) {
55 if (real_style(style) == Style::windows)
56 return '\\';
57 return '/';
58 }
59
60 StringRef find_first_component(StringRef path, Style style) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000061 // Look for this first component in the following order.
62 // * empty (in this case we return an empty string)
63 // * either C: or {//,\\}net.
64 // * {/,\}
Michael J. Spencerebad2f92010-11-29 22:28:51 +000065 // * {file,directory}name
66
67 if (path.empty())
68 return path;
69
Zachary Turner5c5091f2017-03-16 22:28:04 +000070 if (real_style(style) == Style::windows) {
71 // C:
72 if (path.size() >= 2 &&
73 std::isalpha(static_cast<unsigned char>(path[0])) && path[1] == ':')
74 return path.substr(0, 2);
75 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +000076
77 // //net
Zachary Turner5c5091f2017-03-16 22:28:04 +000078 if ((path.size() > 2) && is_separator(path[0], style) &&
79 path[0] == path[1] && !is_separator(path[2], style)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000080 // Find the next directory separator.
Zachary Turner5c5091f2017-03-16 22:28:04 +000081 size_t end = path.find_first_of(separators(style), 2);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000082 return path.substr(0, end);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000083 }
84
85 // {/,\}
Zachary Turner5c5091f2017-03-16 22:28:04 +000086 if (is_separator(path[0], style))
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000087 return path.substr(0, 1);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000088
Michael J. Spencerebad2f92010-11-29 22:28:51 +000089 // * {file,directory}name
Zachary Turner5c5091f2017-03-16 22:28:04 +000090 size_t end = path.find_first_of(separators(style));
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000091 return path.substr(0, end);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000092 }
Michael J. Spencer545cbdf2010-11-30 23:28:07 +000093
Zachary Turner5c5091f2017-03-16 22:28:04 +000094 size_t filename_pos(StringRef str, Style style) {
95 if (str.size() == 2 && is_separator(str[0], style) && str[0] == str[1])
Michael J. Spencer545cbdf2010-11-30 23:28:07 +000096 return 0;
97
Zachary Turner5c5091f2017-03-16 22:28:04 +000098 if (str.size() > 0 && is_separator(str[str.size() - 1], style))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +000099 return str.size() - 1;
100
Zachary Turner5c5091f2017-03-16 22:28:04 +0000101 size_t pos = str.find_last_of(separators(style), str.size() - 1);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000102
Zachary Turner5c5091f2017-03-16 22:28:04 +0000103 if (real_style(style) == Style::windows) {
104 if (pos == StringRef::npos)
105 pos = str.find_last_of(':', str.size() - 2);
106 }
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000107
Zachary Turner5c5091f2017-03-16 22:28:04 +0000108 if (pos == StringRef::npos || (pos == 1 && is_separator(str[0], style)))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000109 return 0;
110
111 return pos + 1;
112 }
113
Zachary Turner5c5091f2017-03-16 22:28:04 +0000114 size_t root_dir_start(StringRef str, Style style) {
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000115 // case "c:/"
Zachary Turner5c5091f2017-03-16 22:28:04 +0000116 if (real_style(style) == Style::windows) {
117 if (str.size() > 2 && str[1] == ':' && is_separator(str[2], style))
118 return 2;
119 }
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000120
121 // case "//"
Zachary Turner5c5091f2017-03-16 22:28:04 +0000122 if (str.size() == 2 && is_separator(str[0], style) && str[0] == str[1])
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000123 return StringRef::npos;
124
125 // case "//net"
Zachary Turner5c5091f2017-03-16 22:28:04 +0000126 if (str.size() > 3 && is_separator(str[0], style) && str[0] == str[1] &&
127 !is_separator(str[2], style)) {
128 return str.find_first_of(separators(style), 2);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000129 }
130
131 // case "/"
Zachary Turner5c5091f2017-03-16 22:28:04 +0000132 if (str.size() > 0 && is_separator(str[0], style))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000133 return 0;
134
135 return StringRef::npos;
136 }
137
Zachary Turner5c5091f2017-03-16 22:28:04 +0000138 size_t parent_path_end(StringRef path, Style style) {
139 size_t end_pos = filename_pos(path, style);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000140
Zachary Turner5c5091f2017-03-16 22:28:04 +0000141 bool filename_was_sep =
142 path.size() > 0 && is_separator(path[end_pos], style);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000143
144 // Skip separators except for root dir.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000145 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos), style);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000146
Zachary Turner5c5091f2017-03-16 22:28:04 +0000147 while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
148 is_separator(path[end_pos - 1], style))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000149 --end_pos;
150
151 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
152 return StringRef::npos;
153
154 return end_pos;
155 }
Zhanyong Wan606bb1a2011-02-11 21:24:40 +0000156} // end unnamed namespace
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000157
Rafael Espindolae79a8722013-06-28 03:48:47 +0000158enum FSEntity {
159 FS_Dir,
160 FS_File,
161 FS_Name
162};
163
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000164static std::error_code createUniqueEntity(const Twine &Model, int &ResultFD,
165 SmallVectorImpl<char> &ResultPath,
166 bool MakeAbsolute, unsigned Mode,
167 FSEntity Type) {
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000168 SmallString<128> ModelStorage;
169 Model.toVector(ModelStorage);
170
171 if (MakeAbsolute) {
172 // Make model absolute by prepending a temp directory if it's not already.
173 if (!sys::path::is_absolute(Twine(ModelStorage))) {
174 SmallString<128> TDir;
Rafael Espindola016a6d52014-08-26 14:47:52 +0000175 sys::path::system_temp_directory(true, TDir);
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000176 sys::path::append(TDir, Twine(ModelStorage));
177 ModelStorage.swap(TDir);
178 }
179 }
180
181 // From here on, DO NOT modify model. It may be needed if the randomly chosen
182 // path already exists.
183 ResultPath = ModelStorage;
184 // Null terminate.
185 ResultPath.push_back(0);
186 ResultPath.pop_back();
187
188retry_random_path:
189 // Replace '%' with random chars.
190 for (unsigned i = 0, e = ModelStorage.size(); i != e; ++i) {
191 if (ModelStorage[i] == '%')
192 ResultPath[i] = "0123456789abcdef"[sys::Process::GetRandomNumber() & 15];
193 }
194
195 // Try to open + create the file.
196 switch (Type) {
197 case FS_File: {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000198 if (std::error_code EC =
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000199 sys::fs::openFileForWrite(Twine(ResultPath.begin()), ResultFD,
200 sys::fs::F_RW | sys::fs::F_Excl, Mode)) {
Rafael Espindola2a826e42014-06-13 17:20:48 +0000201 if (EC == errc::file_exists)
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000202 goto retry_random_path;
203 return EC;
204 }
205
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000206 return std::error_code();
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000207 }
208
209 case FS_Name: {
Rafael Espindola281f23a2014-09-11 20:30:02 +0000210 std::error_code EC =
211 sys::fs::access(ResultPath.begin(), sys::fs::AccessMode::Exist);
212 if (EC == errc::no_such_file_or_directory)
213 return std::error_code();
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000214 if (EC)
215 return EC;
Rafael Espindola281f23a2014-09-11 20:30:02 +0000216 goto retry_random_path;
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000217 }
218
219 case FS_Dir: {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000220 if (std::error_code EC =
221 sys::fs::create_directory(ResultPath.begin(), false)) {
Rafael Espindola2a826e42014-06-13 17:20:48 +0000222 if (EC == errc::file_exists)
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000223 goto retry_random_path;
224 return EC;
225 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000226 return std::error_code();
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000227 }
228 }
229 llvm_unreachable("Invalid Type");
230}
Rafael Espindolae79a8722013-06-28 03:48:47 +0000231
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000232namespace llvm {
233namespace sys {
234namespace path {
235
Zachary Turner5c5091f2017-03-16 22:28:04 +0000236const_iterator begin(StringRef path, Style style) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000237 const_iterator i;
238 i.Path = path;
Zachary Turner5c5091f2017-03-16 22:28:04 +0000239 i.Component = find_first_component(path, style);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000240 i.Position = 0;
Zachary Turner5c5091f2017-03-16 22:28:04 +0000241 i.S = style;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000242 return i;
243}
244
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000245const_iterator end(StringRef path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000246 const_iterator i;
247 i.Path = path;
248 i.Position = path.size();
249 return i;
250}
251
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000252const_iterator &const_iterator::operator++() {
253 assert(Position < Path.size() && "Tried to increment past end!");
254
255 // Increment Position to past the current component
256 Position += Component.size();
257
258 // Check for end.
259 if (Position == Path.size()) {
260 Component = StringRef();
261 return *this;
262 }
263
264 // Both POSIX and Windows treat paths that begin with exactly two separators
265 // specially.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000266 bool was_net = Component.size() > 2 && is_separator(Component[0], S) &&
267 Component[1] == Component[0] && !is_separator(Component[2], S);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000268
269 // Handle separators.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000270 if (is_separator(Path[Position], S)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000271 // Root dir.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000272 if (was_net ||
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000273 // c:/
Zachary Turner5c5091f2017-03-16 22:28:04 +0000274 (real_style(S) == Style::windows && Component.endswith(":"))) {
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000275 Component = Path.substr(Position, 1);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000276 return *this;
277 }
278
279 // Skip extra separators.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000280 while (Position != Path.size() && is_separator(Path[Position], S)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000281 ++Position;
282 }
283
284 // Treat trailing '/' as a '.'.
285 if (Position == Path.size()) {
286 --Position;
287 Component = ".";
288 return *this;
289 }
290 }
291
292 // Find next component.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000293 size_t end_pos = Path.find_first_of(separators(S), Position);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000294 Component = Path.slice(Position, end_pos);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000295
296 return *this;
297}
298
Justin Bogner487e7642014-08-04 17:36:41 +0000299bool const_iterator::operator==(const const_iterator &RHS) const {
300 return Path.begin() == RHS.Path.begin() && Position == RHS.Position;
301}
302
303ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
304 return Position - RHS.Position;
305}
306
Zachary Turner5c5091f2017-03-16 22:28:04 +0000307reverse_iterator rbegin(StringRef Path, Style style) {
Justin Bogner487e7642014-08-04 17:36:41 +0000308 reverse_iterator I;
309 I.Path = Path;
310 I.Position = Path.size();
Zachary Turner5c5091f2017-03-16 22:28:04 +0000311 I.S = style;
Justin Bogner487e7642014-08-04 17:36:41 +0000312 return ++I;
313}
314
315reverse_iterator rend(StringRef Path) {
316 reverse_iterator I;
317 I.Path = Path;
318 I.Component = Path.substr(0, 0);
319 I.Position = 0;
320 return I;
321}
322
323reverse_iterator &reverse_iterator::operator++() {
Ben Langmuir8d116392014-03-05 19:56:30 +0000324 // If we're at the end and the previous char was a '/', return '.' unless
325 // we are the root path.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000326 size_t root_dir_pos = root_dir_start(Path, S);
327 if (Position == Path.size() && Path.size() > root_dir_pos + 1 &&
328 is_separator(Path[Position - 1], S)) {
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000329 --Position;
330 Component = ".";
331 return *this;
332 }
333
334 // Skip separators unless it's the root directory.
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000335 size_t end_pos = Position;
336
Zachary Turner5c5091f2017-03-16 22:28:04 +0000337 while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
338 is_separator(Path[end_pos - 1], S))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000339 --end_pos;
340
341 // Find next separator.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000342 size_t start_pos = filename_pos(Path.substr(0, end_pos), S);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000343 Component = Path.slice(start_pos, end_pos);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000344 Position = start_pos;
345 return *this;
346}
347
Justin Bogner487e7642014-08-04 17:36:41 +0000348bool reverse_iterator::operator==(const reverse_iterator &RHS) const {
349 return Path.begin() == RHS.Path.begin() && Component == RHS.Component &&
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000350 Position == RHS.Position;
351}
352
Filipe Cabecinhas78949382016-04-29 16:48:07 +0000353ptrdiff_t reverse_iterator::operator-(const reverse_iterator &RHS) const {
354 return Position - RHS.Position;
355}
356
Zachary Turner5c5091f2017-03-16 22:28:04 +0000357StringRef root_path(StringRef path, Style style) {
358 const_iterator b = begin(path, style), pos = b, e = end(path);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000359 if (b != e) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000360 bool has_net =
361 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
362 bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000363
364 if (has_net || has_drive) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000365 if ((++pos != e) && is_separator((*pos)[0], style)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000366 // {C:/,//net/}, so get the first two components.
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000367 return path.substr(0, b->size() + pos->size());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000368 } else {
369 // just {C:,//net}, return the first component.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000370 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000371 }
372 }
373
374 // POSIX style root directory.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000375 if (is_separator((*b)[0], style)) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000376 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000377 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000378 }
379
Michael J. Spencerf616b212010-12-07 17:04:04 +0000380 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000381}
382
Zachary Turner5c5091f2017-03-16 22:28:04 +0000383StringRef root_name(StringRef path, Style style) {
384 const_iterator b = begin(path, style), e = end(path);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000385 if (b != e) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000386 bool has_net =
387 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
388 bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000389
390 if (has_net || has_drive) {
391 // just {C:,//net}, return the first component.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000392 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000393 }
394 }
395
396 // No path or no name.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000397 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000398}
399
Zachary Turner5c5091f2017-03-16 22:28:04 +0000400StringRef root_directory(StringRef path, Style style) {
401 const_iterator b = begin(path, style), pos = b, e = end(path);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000402 if (b != e) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000403 bool has_net =
404 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
405 bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000406
407 if ((has_net || has_drive) &&
408 // {C:,//net}, skip to the next component.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000409 (++pos != e) && is_separator((*pos)[0], style)) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000410 return *pos;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000411 }
412
413 // POSIX style root directory.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000414 if (!has_net && is_separator((*b)[0], style)) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000415 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000416 }
417 }
418
419 // No path or no root.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000420 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000421}
422
Zachary Turner5c5091f2017-03-16 22:28:04 +0000423StringRef relative_path(StringRef path, Style style) {
424 StringRef root = root_path(path, style);
Michael J. Spencere6462392012-02-29 00:06:24 +0000425 return path.substr(root.size());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000426}
427
Zachary Turner5c5091f2017-03-16 22:28:04 +0000428void append(SmallVectorImpl<char> &path, Style style, const Twine &a,
429 const Twine &b, const Twine &c, const Twine &d) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000430 SmallString<32> a_storage;
431 SmallString<32> b_storage;
432 SmallString<32> c_storage;
433 SmallString<32> d_storage;
434
435 SmallVector<StringRef, 4> components;
436 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
437 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
438 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
439 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
440
Pawel Bylica64d08ff2015-10-22 08:12:15 +0000441 for (auto &component : components) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000442 bool path_has_sep =
443 !path.empty() && is_separator(path[path.size() - 1], style);
444 bool component_has_sep =
445 !component.empty() && is_separator(component[0], style);
446 bool is_root_name = has_root_name(component, style);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000447
448 if (path_has_sep) {
449 // Strip separators from beginning of component.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000450 size_t loc = component.find_first_not_of(separators(style));
Pawel Bylica64d08ff2015-10-22 08:12:15 +0000451 StringRef c = component.substr(loc);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000452
453 // Append it.
454 path.append(c.begin(), c.end());
455 continue;
456 }
457
Michael J. Spencer95e4ac12010-12-06 04:28:23 +0000458 if (!component_has_sep && !(path.empty() || is_root_name)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000459 // Add a separator.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000460 path.push_back(preferred_separator(style));
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000461 }
462
Pawel Bylica64d08ff2015-10-22 08:12:15 +0000463 path.append(component.begin(), component.end());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000464 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000465}
466
Zachary Turner5c5091f2017-03-16 22:28:04 +0000467void append(SmallVectorImpl<char> &path, const Twine &a, const Twine &b,
468 const Twine &c, const Twine &d) {
469 append(path, Style::native, a, b, c, d);
Argyrios Kyrtzidisa61736f2011-02-15 17:51:19 +0000470}
471
Zachary Turner5c5091f2017-03-16 22:28:04 +0000472void append(SmallVectorImpl<char> &path, const_iterator begin,
473 const_iterator end, Style style) {
474 for (; begin != end; ++begin)
475 path::append(path, style, *begin);
476}
477
478StringRef parent_path(StringRef path, Style style) {
479 size_t end_pos = parent_path_end(path, style);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000480 if (end_pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000481 return StringRef();
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000482 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000483 return path.substr(0, end_pos);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000484}
485
Zachary Turner5c5091f2017-03-16 22:28:04 +0000486void remove_filename(SmallVectorImpl<char> &path, Style style) {
487 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()), style);
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000488 if (end_pos != StringRef::npos)
489 path.set_size(end_pos);
Michael J. Spencer9c594092010-12-01 00:52:28 +0000490}
491
Zachary Turner5c5091f2017-03-16 22:28:04 +0000492void replace_extension(SmallVectorImpl<char> &path, const Twine &extension,
493 Style style) {
Michael J. Spencerfb3a95d2010-12-01 00:52:55 +0000494 StringRef p(path.begin(), path.size());
495 SmallString<32> ext_storage;
496 StringRef ext = extension.toStringRef(ext_storage);
497
498 // Erase existing extension.
499 size_t pos = p.find_last_of('.');
Zachary Turner5c5091f2017-03-16 22:28:04 +0000500 if (pos != StringRef::npos && pos >= filename_pos(p, style))
Michael J. Spencerfb3a95d2010-12-01 00:52:55 +0000501 path.set_size(pos);
502
503 // Append '.' if needed.
504 if (ext.size() > 0 && ext[0] != '.')
505 path.push_back('.');
506
507 // Append extension.
508 path.append(ext.begin(), ext.end());
Michael J. Spencerfb3a95d2010-12-01 00:52:55 +0000509}
510
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000511void replace_path_prefix(SmallVectorImpl<char> &Path,
Zachary Turner5c5091f2017-03-16 22:28:04 +0000512 const StringRef &OldPrefix, const StringRef &NewPrefix,
513 Style style) {
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000514 if (OldPrefix.empty() && NewPrefix.empty())
515 return;
516
517 StringRef OrigPath(Path.begin(), Path.size());
518 if (!OrigPath.startswith(OldPrefix))
519 return;
520
521 // If prefixes have the same size we can simply copy the new one over.
522 if (OldPrefix.size() == NewPrefix.size()) {
523 std::copy(NewPrefix.begin(), NewPrefix.end(), Path.begin());
524 return;
525 }
526
527 StringRef RelPath = OrigPath.substr(OldPrefix.size());
528 SmallString<256> NewPath;
Zachary Turner5c5091f2017-03-16 22:28:04 +0000529 path::append(NewPath, style, NewPrefix);
530 path::append(NewPath, style, RelPath);
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000531 Path.swap(NewPath);
532}
533
Zachary Turner5c5091f2017-03-16 22:28:04 +0000534void native(const Twine &path, SmallVectorImpl<char> &result, Style style) {
Benjamin Kramerbd4ac9b2013-09-11 10:45:21 +0000535 assert((!path.isSingleStringRef() ||
536 path.getSingleStringRef().data() != result.data()) &&
537 "path and result are not allowed to overlap!");
Michael J. Spencer80025002010-12-01 02:48:27 +0000538 // Clear result.
Michael J. Spencer98c7a112010-12-07 01:23:19 +0000539 result.clear();
Michael J. Spencer80025002010-12-01 02:48:27 +0000540 path.toVector(result);
Zachary Turner5c5091f2017-03-16 22:28:04 +0000541 native(result, style);
Benjamin Kramerbd4ac9b2013-09-11 10:45:21 +0000542}
543
Zachary Turner5c5091f2017-03-16 22:28:04 +0000544void native(SmallVectorImpl<char> &Path, Style style) {
Serge Pavlov9c761a32017-03-01 09:38:15 +0000545 if (Path.empty())
546 return;
Zachary Turner5c5091f2017-03-16 22:28:04 +0000547 if (real_style(style) == Style::windows) {
548 std::replace(Path.begin(), Path.end(), '/', '\\');
549 if (Path[0] == '~' && (Path.size() == 1 || is_separator(Path[1], style))) {
550 SmallString<128> PathHome;
551 home_directory(PathHome);
552 PathHome.append(Path.begin() + 1, Path.end());
553 Path = PathHome;
554 }
555 } else {
556 for (auto PI = Path.begin(), PE = Path.end(); PI < PE; ++PI) {
557 if (*PI == '\\') {
558 auto PN = PI + 1;
559 if (PN < PE && *PN == '\\')
560 ++PI; // increment once, the for loop will move over the escaped slash
561 else
562 *PI = '/';
563 }
Rafael Espindolad649b9d2014-08-08 21:29:34 +0000564 }
565 }
Michael J. Spencer80025002010-12-01 02:48:27 +0000566}
567
Zachary Turner5c5091f2017-03-16 22:28:04 +0000568std::string convert_to_slash(StringRef path, Style style) {
569 if (real_style(style) != Style::windows)
570 return path;
571
Rui Ueyama3e649032017-01-09 01:47:15 +0000572 std::string s = path.str();
573 std::replace(s.begin(), s.end(), '\\', '/');
574 return s;
Rui Ueyama3e649032017-01-09 01:47:15 +0000575}
576
Zachary Turner5c5091f2017-03-16 22:28:04 +0000577StringRef filename(StringRef path, Style style) { return *rbegin(path, style); }
Michael J. Spencer14269202010-12-01 03:18:17 +0000578
Zachary Turner5c5091f2017-03-16 22:28:04 +0000579StringRef stem(StringRef path, Style style) {
580 StringRef fname = filename(path, style);
Michael J. Spencer956955e2010-12-01 03:18:33 +0000581 size_t pos = fname.find_last_of('.');
582 if (pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000583 return fname;
Michael J. Spencer956955e2010-12-01 03:18:33 +0000584 else
585 if ((fname.size() == 1 && fname == ".") ||
586 (fname.size() == 2 && fname == ".."))
Michael J. Spencerf616b212010-12-07 17:04:04 +0000587 return fname;
Michael J. Spencer956955e2010-12-01 03:18:33 +0000588 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000589 return fname.substr(0, pos);
Michael J. Spencer956955e2010-12-01 03:18:33 +0000590}
591
Zachary Turner5c5091f2017-03-16 22:28:04 +0000592StringRef extension(StringRef path, Style style) {
593 StringRef fname = filename(path, style);
Michael J. Spencer87106c52010-12-01 03:37:41 +0000594 size_t pos = fname.find_last_of('.');
595 if (pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000596 return StringRef();
Michael J. Spencer87106c52010-12-01 03:37:41 +0000597 else
598 if ((fname.size() == 1 && fname == ".") ||
599 (fname.size() == 2 && fname == ".."))
Michael J. Spencerf616b212010-12-07 17:04:04 +0000600 return StringRef();
Michael J. Spencer87106c52010-12-01 03:37:41 +0000601 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000602 return fname.substr(pos);
Michael J. Spencer87106c52010-12-01 03:37:41 +0000603}
604
Zachary Turner5c5091f2017-03-16 22:28:04 +0000605bool is_separator(char value, Style style) {
606 if (value == '/')
607 return true;
608 if (real_style(style) == Style::windows)
609 return value == '\\';
610 return false;
Zhanyong Wan606bb1a2011-02-11 21:24:40 +0000611}
612
Zachary Turner5c5091f2017-03-16 22:28:04 +0000613StringRef get_separator(Style style) {
614 if (real_style(style) == Style::windows)
615 return "\\";
616 return "/";
Yaron Keren15217202014-05-16 13:16:30 +0000617}
618
Zachary Turner5c5091f2017-03-16 22:28:04 +0000619bool has_root_name(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000620 SmallString<128> path_storage;
621 StringRef p = path.toStringRef(path_storage);
622
Zachary Turner5c5091f2017-03-16 22:28:04 +0000623 return !root_name(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000624}
625
Zachary Turner5c5091f2017-03-16 22:28:04 +0000626bool has_root_directory(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000627 SmallString<128> path_storage;
628 StringRef p = path.toStringRef(path_storage);
629
Zachary Turner5c5091f2017-03-16 22:28:04 +0000630 return !root_directory(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000631}
632
Zachary Turner5c5091f2017-03-16 22:28:04 +0000633bool has_root_path(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000634 SmallString<128> path_storage;
635 StringRef p = path.toStringRef(path_storage);
636
Zachary Turner5c5091f2017-03-16 22:28:04 +0000637 return !root_path(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000638}
639
Zachary Turner5c5091f2017-03-16 22:28:04 +0000640bool has_relative_path(const Twine &path, Style style) {
Michael J. Spencer6d4b7e72010-12-20 13:30:28 +0000641 SmallString<128> path_storage;
642 StringRef p = path.toStringRef(path_storage);
643
Zachary Turner5c5091f2017-03-16 22:28:04 +0000644 return !relative_path(p, style).empty();
Michael J. Spencer6d4b7e72010-12-20 13:30:28 +0000645}
646
Zachary Turner5c5091f2017-03-16 22:28:04 +0000647bool has_filename(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000648 SmallString<128> path_storage;
649 StringRef p = path.toStringRef(path_storage);
650
Zachary Turner5c5091f2017-03-16 22:28:04 +0000651 return !filename(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000652}
653
Zachary Turner5c5091f2017-03-16 22:28:04 +0000654bool has_parent_path(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000655 SmallString<128> path_storage;
656 StringRef p = path.toStringRef(path_storage);
657
Zachary Turner5c5091f2017-03-16 22:28:04 +0000658 return !parent_path(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000659}
660
Zachary Turner5c5091f2017-03-16 22:28:04 +0000661bool has_stem(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000662 SmallString<128> path_storage;
663 StringRef p = path.toStringRef(path_storage);
664
Zachary Turner5c5091f2017-03-16 22:28:04 +0000665 return !stem(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000666}
667
Zachary Turner5c5091f2017-03-16 22:28:04 +0000668bool has_extension(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000669 SmallString<128> path_storage;
670 StringRef p = path.toStringRef(path_storage);
671
Zachary Turner5c5091f2017-03-16 22:28:04 +0000672 return !extension(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000673}
674
Zachary Turner5c5091f2017-03-16 22:28:04 +0000675bool is_absolute(const Twine &path, Style style) {
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000676 SmallString<128> path_storage;
677 StringRef p = path.toStringRef(path_storage);
678
Zachary Turner5c5091f2017-03-16 22:28:04 +0000679 bool rootDir = has_root_directory(p, style);
680 bool rootName =
681 (real_style(style) != Style::windows) || has_root_name(p, style);
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000682
Michael J. Spencerf616b212010-12-07 17:04:04 +0000683 return rootDir && rootName;
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000684}
685
Zachary Turner5c5091f2017-03-16 22:28:04 +0000686bool is_relative(const Twine &path, Style style) {
687 return !is_absolute(path, style);
688}
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000689
Zachary Turner5c5091f2017-03-16 22:28:04 +0000690StringRef remove_leading_dotslash(StringRef Path, Style style) {
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000691 // Remove leading "./" (or ".//" or "././" etc.)
Zachary Turner5c5091f2017-03-16 22:28:04 +0000692 while (Path.size() > 2 && Path[0] == '.' && is_separator(Path[1], style)) {
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000693 Path = Path.substr(2);
Zachary Turner5c5091f2017-03-16 22:28:04 +0000694 while (Path.size() > 0 && is_separator(Path[0], style))
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000695 Path = Path.substr(1);
696 }
697 return Path;
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000698}
699
Zachary Turner5c5091f2017-03-16 22:28:04 +0000700static SmallString<256> remove_dots(StringRef path, bool remove_dot_dot,
701 Style style) {
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000702 SmallVector<StringRef, 16> components;
703
704 // Skip the root path, then look for traversal in the components.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000705 StringRef rel = path::relative_path(path, style);
706 for (StringRef C :
707 llvm::make_range(path::begin(rel, style), path::end(rel))) {
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000708 if (C == ".")
709 continue;
Benjamin Kramer937dd7a2016-10-17 13:28:21 +0000710 // Leading ".." will remain in the path unless it's at the root.
711 if (remove_dot_dot && C == "..") {
712 if (!components.empty() && components.back() != "..") {
713 components.pop_back();
714 continue;
715 }
Zachary Turner5c5091f2017-03-16 22:28:04 +0000716 if (path::is_absolute(path, style))
Benjamin Kramer937dd7a2016-10-17 13:28:21 +0000717 continue;
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000718 }
719 components.push_back(C);
720 }
721
Zachary Turner5c5091f2017-03-16 22:28:04 +0000722 SmallString<256> buffer = path::root_path(path, style);
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000723 for (StringRef C : components)
Zachary Turner5c5091f2017-03-16 22:28:04 +0000724 path::append(buffer, style, C);
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000725 return buffer;
726}
727
Zachary Turner5c5091f2017-03-16 22:28:04 +0000728bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot,
729 Style style) {
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000730 StringRef p(path.data(), path.size());
731
Zachary Turner5c5091f2017-03-16 22:28:04 +0000732 SmallString<256> result = remove_dots(p, remove_dot_dot, style);
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000733 if (result == path)
734 return false;
735
736 path.swap(result);
737 return true;
738}
739
Michael J. Spencer9fc1d9d2010-12-01 19:32:01 +0000740} // end namespace path
741
742namespace fs {
743
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000744std::error_code getUniqueID(const Twine Path, UniqueID &Result) {
Rafael Espindola7f822a92013-07-29 21:26:49 +0000745 file_status Status;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000746 std::error_code EC = status(Path, Status);
Rafael Espindola7f822a92013-07-29 21:26:49 +0000747 if (EC)
748 return EC;
749 Result = Status.getUniqueID();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000750 return std::error_code();
Rafael Espindola7f822a92013-07-29 21:26:49 +0000751}
752
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000753std::error_code createUniqueFile(const Twine &Model, int &ResultFd,
754 SmallVectorImpl<char> &ResultPath,
755 unsigned Mode) {
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000756 return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
757}
758
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000759std::error_code createUniqueFile(const Twine &Model,
760 SmallVectorImpl<char> &ResultPath) {
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000761 int Dummy;
762 return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
763}
764
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000765static std::error_code
766createTemporaryFile(const Twine &Model, int &ResultFD,
767 llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) {
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000768 SmallString<128> Storage;
769 StringRef P = Model.toNullTerminatedStringRef(Storage);
Zachary Turner5c5091f2017-03-16 22:28:04 +0000770 assert(P.find_first_of(separators(Style::native)) == StringRef::npos &&
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000771 "Model must be a simple filename.");
772 // Use P.begin() so that createUniqueEntity doesn't need to recreate Storage.
773 return createUniqueEntity(P.begin(), ResultFD, ResultPath,
774 true, owner_read | owner_write, Type);
775}
776
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000777static std::error_code
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000778createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000779 llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) {
Rafael Espindolad3c89042013-07-25 15:00:17 +0000780 const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
781 return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000782 Type);
783}
784
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000785std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
786 int &ResultFD,
787 SmallVectorImpl<char> &ResultPath) {
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000788 return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File);
789}
790
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000791std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
792 SmallVectorImpl<char> &ResultPath) {
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000793 int Dummy;
794 return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
795}
796
797
Rafael Espindolae79a8722013-06-28 03:48:47 +0000798// This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
Rafael Espindola31a24432013-06-28 10:55:41 +0000799// for consistency. We should try using mkdtemp.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000800std::error_code createUniqueDirectory(const Twine &Prefix,
801 SmallVectorImpl<char> &ResultPath) {
Rafael Espindolae79a8722013-06-28 03:48:47 +0000802 int Dummy;
803 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath,
804 true, 0, FS_Dir);
Rafael Espindola7ffacc42013-06-27 03:45:31 +0000805}
806
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000807static std::error_code make_absolute(const Twine &current_directory,
808 SmallVectorImpl<char> &path,
809 bool use_current_directory) {
Michael J. Spencer92903a32010-12-07 03:57:17 +0000810 StringRef p(path.data(), path.size());
811
Zachary Turner5c5091f2017-03-16 22:28:04 +0000812 bool rootDirectory = path::has_root_directory(p);
813 bool rootName =
814 (real_style(Style::native) != Style::windows) || path::has_root_name(p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000815
816 // Already absolute.
817 if (rootName && rootDirectory)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000818 return std::error_code();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000819
820 // All of the following conditions will need the current directory.
821 SmallString<128> current_dir;
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000822 if (use_current_directory)
823 current_directory.toVector(current_dir);
824 else if (std::error_code ec = current_path(current_dir))
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000825 return ec;
Michael J. Spencer92903a32010-12-07 03:57:17 +0000826
827 // Relative path. Prepend the current directory.
828 if (!rootName && !rootDirectory) {
829 // Append path to the current directory.
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000830 path::append(current_dir, p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000831 // Set path to the result.
832 path.swap(current_dir);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000833 return std::error_code();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000834 }
835
836 if (!rootName && rootDirectory) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000837 StringRef cdrn = path::root_name(current_dir);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000838 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000839 path::append(curDirRootName, p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000840 // Set path to the result.
841 path.swap(curDirRootName);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000842 return std::error_code();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000843 }
844
845 if (rootName && !rootDirectory) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000846 StringRef pRootName = path::root_name(p);
847 StringRef bRootDirectory = path::root_directory(current_dir);
848 StringRef bRelativePath = path::relative_path(current_dir);
849 StringRef pRelativePath = path::relative_path(p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000850
851 SmallString<128> res;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000852 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000853 path.swap(res);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000854 return std::error_code();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000855 }
856
857 llvm_unreachable("All rootName and rootDirectory combinations should have "
858 "occurred above!");
859}
860
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000861std::error_code make_absolute(const Twine &current_directory,
862 SmallVectorImpl<char> &path) {
863 return make_absolute(current_directory, path, true);
864}
865
866std::error_code make_absolute(SmallVectorImpl<char> &path) {
867 return make_absolute(Twine(), path, false);
868}
869
Frederic Riss6b9396c2015-08-06 21:04:55 +0000870std::error_code create_directories(const Twine &Path, bool IgnoreExisting,
871 perms Perms) {
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000872 SmallString<128> PathStorage;
873 StringRef P = Path.toStringRef(PathStorage);
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000874
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000875 // Be optimistic and try to create the directory
Frederic Riss6b9396c2015-08-06 21:04:55 +0000876 std::error_code EC = create_directory(P, IgnoreExisting, Perms);
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000877 // If we succeeded, or had any error other than the parent not existing, just
878 // return it.
Rafael Espindola2a826e42014-06-13 17:20:48 +0000879 if (EC != errc::no_such_file_or_directory)
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000880 return EC;
Michael J. Spencerf616b212010-12-07 17:04:04 +0000881
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000882 // We failed because of a no_such_file_or_directory, try to create the
883 // parent.
884 StringRef Parent = path::parent_path(P);
885 if (Parent.empty())
886 return EC;
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000887
Frederic Riss6b9396c2015-08-06 21:04:55 +0000888 if ((EC = create_directories(Parent, IgnoreExisting, Perms)))
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000889 return EC;
890
Frederic Riss6b9396c2015-08-06 21:04:55 +0000891 return create_directory(P, IgnoreExisting, Perms);
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000892}
893
Justin Bognercd45f962014-06-19 19:35:39 +0000894std::error_code copy_file(const Twine &From, const Twine &To) {
895 int ReadFD, WriteFD;
896 if (std::error_code EC = openFileForRead(From, ReadFD))
897 return EC;
898 if (std::error_code EC = openFileForWrite(To, WriteFD, F_None)) {
899 close(ReadFD);
900 return EC;
901 }
902
903 const size_t BufSize = 4096;
Dylan Noblesmith42836d92014-08-26 02:03:30 +0000904 char *Buf = new char[BufSize];
Justin Bognercd45f962014-06-19 19:35:39 +0000905 int BytesRead = 0, BytesWritten = 0;
906 for (;;) {
907 BytesRead = read(ReadFD, Buf, BufSize);
908 if (BytesRead <= 0)
909 break;
910 while (BytesRead) {
911 BytesWritten = write(WriteFD, Buf, BytesRead);
912 if (BytesWritten < 0)
913 break;
914 BytesRead -= BytesWritten;
915 }
916 if (BytesWritten < 0)
917 break;
918 }
919 close(ReadFD);
920 close(WriteFD);
Dylan Noblesmith42836d92014-08-26 02:03:30 +0000921 delete[] Buf;
Justin Bognercd45f962014-06-19 19:35:39 +0000922
923 if (BytesRead < 0 || BytesWritten < 0)
924 return std::error_code(errno, std::generic_category());
925 return std::error_code();
926}
927
Zachary Turner82a0c972017-03-20 23:33:18 +0000928ErrorOr<MD5::MD5Result> md5_contents(int FD) {
929 MD5 Hash;
930
931 constexpr size_t BufSize = 4096;
932 std::vector<uint8_t> Buf(BufSize);
933 int BytesRead = 0;
934 for (;;) {
935 BytesRead = read(FD, Buf.data(), BufSize);
936 if (BytesRead <= 0)
937 break;
938 Hash.update(makeArrayRef(Buf.data(), BytesRead));
939 }
940
941 if (BytesRead < 0)
942 return std::error_code(errno, std::generic_category());
943 MD5::MD5Result Result;
944 Hash.final(Result);
945 return Result;
946}
947
948ErrorOr<MD5::MD5Result> md5_contents(const Twine &Path) {
949 int FD;
950 if (auto EC = openFileForRead(Path, FD))
951 return EC;
952
953 auto Result = md5_contents(FD);
954 close(FD);
955 return Result;
956}
957
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000958bool exists(file_status status) {
959 return status_known(status) && status.type() != file_type::file_not_found;
960}
961
962bool status_known(file_status s) {
963 return s.type() != file_type::status_error;
964}
965
Zachary Turner82dd5422017-03-07 16:10:10 +0000966file_type get_file_type(const Twine &Path, bool Follow) {
Zachary Turner990e3cd2017-03-07 03:43:17 +0000967 file_status st;
Zachary Turner82dd5422017-03-07 16:10:10 +0000968 if (status(Path, st, Follow))
Zachary Turner990e3cd2017-03-07 03:43:17 +0000969 return file_type::status_error;
970 return st.type();
971}
972
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000973bool is_directory(file_status status) {
974 return status.type() == file_type::directory_file;
975}
976
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000977std::error_code is_directory(const Twine &path, bool &result) {
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000978 file_status st;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000979 if (std::error_code ec = status(path, st))
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000980 return ec;
981 result = is_directory(st);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000982 return std::error_code();
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000983}
984
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000985bool is_regular_file(file_status status) {
986 return status.type() == file_type::regular_file;
987}
988
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000989std::error_code is_regular_file(const Twine &path, bool &result) {
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000990 file_status st;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000991 if (std::error_code ec = status(path, st))
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000992 return ec;
993 result = is_regular_file(st);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000994 return std::error_code();
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000995}
996
Zachary Turner7d86ee52017-03-08 17:56:08 +0000997bool is_symlink_file(file_status status) {
998 return status.type() == file_type::symlink_file;
999}
1000
1001std::error_code is_symlink_file(const Twine &path, bool &result) {
1002 file_status st;
1003 if (std::error_code ec = status(path, st, false))
1004 return ec;
1005 result = is_symlink_file(st);
1006 return std::error_code();
1007}
1008
Michael J. Spencer730f51a2010-12-09 17:37:02 +00001009bool is_other(file_status status) {
1010 return exists(status) &&
1011 !is_regular_file(status) &&
Rafael Espindola20063062014-03-20 17:39:04 +00001012 !is_directory(status);
Michael J. Spencer730f51a2010-12-09 17:37:02 +00001013}
1014
Juergen Ributzka84ba3422014-12-18 18:19:47 +00001015std::error_code is_other(const Twine &Path, bool &Result) {
1016 file_status FileStatus;
1017 if (std::error_code EC = status(Path, FileStatus))
1018 return EC;
1019 Result = is_other(FileStatus);
1020 return std::error_code();
1021}
1022
Benjamin Kramer91ead3c2011-09-14 01:14:36 +00001023void directory_entry::replace_filename(const Twine &filename, file_status st) {
Rafael Espindolaf662e002015-07-15 21:24:07 +00001024 SmallString<128> path = path::parent_path(Path);
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +00001025 path::append(path, filename);
1026 Path = path.str();
1027 Status = st;
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +00001028}
1029
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001030template <size_t N>
1031static bool startswith(StringRef Magic, const char (&S)[N]) {
1032 return Magic.startswith(StringRef(S, N - 1));
1033}
1034
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001035/// @brief Identify the magic in magic.
Logan Chienc0f691d2014-06-25 13:46:17 +00001036file_magic identify_magic(StringRef Magic) {
Rafael Espindola9b404292013-06-11 17:22:12 +00001037 if (Magic.size() < 4)
Michael J. Spencer96ebd912012-06-19 05:29:57 +00001038 return file_magic::unknown;
Rafael Espindola9b404292013-06-11 17:22:12 +00001039 switch ((unsigned char)Magic[0]) {
Rui Ueyamafc149a62013-10-15 22:45:38 +00001040 case 0x00: {
Rui Ueyama2d021662016-11-15 00:54:54 +00001041 // COFF bigobj, CL.exe's LTO object file, or short import library file
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001042 if (startswith(Magic, "\0\0\xFF\xFF")) {
Rui Ueyama5c69ff52014-09-11 22:34:32 +00001043 size_t MinSize = offsetof(COFF::BigObjHeader, UUID) + sizeof(COFF::BigObjMagic);
1044 if (Magic.size() < MinSize)
1045 return file_magic::coff_import_library;
1046
Rui Ueyama5c69ff52014-09-11 22:34:32 +00001047 const char *Start = Magic.data() + offsetof(COFF::BigObjHeader, UUID);
Rui Ueyama2d021662016-11-15 00:54:54 +00001048 if (memcmp(Start, COFF::BigObjMagic, sizeof(COFF::BigObjMagic)) == 0)
1049 return file_magic::coff_object;
1050 if (memcmp(Start, COFF::ClGlObjMagic, sizeof(COFF::BigObjMagic)) == 0)
1051 return file_magic::coff_cl_gl_object;
1052 return file_magic::coff_import_library;
Rui Ueyama2acb0582014-09-11 21:09:57 +00001053 }
Rui Ueyamafc149a62013-10-15 22:45:38 +00001054 // Windows resource file
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001055 if (startswith(Magic, "\0\0\0\0\x20\0\0\0\xFF"))
Rui Ueyamafc149a62013-10-15 22:45:38 +00001056 return file_magic::windows_resource;
Rui Ueyama829c4392013-11-14 22:09:08 +00001057 // 0x0000 = COFF unknown machine type
1058 if (Magic[1] == 0)
1059 return file_magic::coff_object;
Derek Schuff2c6f75d2016-11-30 16:49:11 +00001060 if (startswith(Magic, "\0asm"))
1061 return file_magic::wasm_object;
Rui Ueyamafc149a62013-10-15 22:45:38 +00001062 break;
1063 }
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001064 case 0xDE: // 0x0B17C0DE = BC wraper
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001065 if (startswith(Magic, "\xDE\xC0\x17\x0B"))
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001066 return file_magic::bitcode;
1067 break;
1068 case 'B':
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001069 if (startswith(Magic, "BC\xC0\xDE"))
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001070 return file_magic::bitcode;
1071 break;
1072 case '!':
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001073 if (startswith(Magic, "!<arch>\n") || startswith(Magic, "!<thin>\n"))
1074 return file_magic::archive;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001075 break;
1076
1077 case '\177':
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001078 if (startswith(Magic, "\177ELF") && Magic.size() >= 18) {
Rafael Espindola9b404292013-06-11 17:22:12 +00001079 bool Data2MSB = Magic[5] == 2;
Michael J. Spencerb8055cb2013-04-05 20:10:04 +00001080 unsigned high = Data2MSB ? 16 : 17;
1081 unsigned low = Data2MSB ? 17 : 16;
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001082 if (Magic[high] == 0) {
Rafael Espindola9b404292013-06-11 17:22:12 +00001083 switch (Magic[low]) {
Michael J. Spencere368a622015-01-23 21:58:09 +00001084 default: return file_magic::elf;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001085 case 1: return file_magic::elf_relocatable;
1086 case 2: return file_magic::elf_executable;
1087 case 3: return file_magic::elf_shared_object;
1088 case 4: return file_magic::elf_core;
1089 }
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001090 }
1091 // It's still some type of ELF file.
1092 return file_magic::elf;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001093 }
1094 break;
1095
1096 case 0xCA:
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001097 if (startswith(Magic, "\xCA\xFE\xBA\xBE") ||
1098 startswith(Magic, "\xCA\xFE\xBA\xBF")) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001099 // This is complicated by an overlap with Java class files.
1100 // See the Mach-O section in /usr/share/file/magic for details.
Rafael Espindola9b404292013-06-11 17:22:12 +00001101 if (Magic.size() >= 8 && Magic[7] < 43)
Alexey Samsonove6388e62013-06-18 15:03:28 +00001102 return file_magic::macho_universal_binary;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001103 }
1104 break;
1105
1106 // The two magic numbers for mach-o are:
1107 // 0xfeedface - 32-bit mach-o
1108 // 0xfeedfacf - 64-bit mach-o
1109 case 0xFE:
1110 case 0xCE:
1111 case 0xCF: {
1112 uint16_t type = 0;
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001113 if (startswith(Magic, "\xFE\xED\xFA\xCE") ||
1114 startswith(Magic, "\xFE\xED\xFA\xCF")) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001115 /* Native endian */
Kevin Enderby87c85b72016-01-26 23:43:37 +00001116 size_t MinSize;
1117 if (Magic[3] == char(0xCE))
1118 MinSize = sizeof(MachO::mach_header);
1119 else
1120 MinSize = sizeof(MachO::mach_header_64);
1121 if (Magic.size() >= MinSize)
1122 type = Magic[12] << 24 | Magic[13] << 12 | Magic[14] << 8 | Magic[15];
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001123 } else if (startswith(Magic, "\xCE\xFA\xED\xFE") ||
1124 startswith(Magic, "\xCF\xFA\xED\xFE")) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001125 /* Reverse endian */
Kevin Enderby87c85b72016-01-26 23:43:37 +00001126 size_t MinSize;
1127 if (Magic[0] == char(0xCE))
1128 MinSize = sizeof(MachO::mach_header);
1129 else
1130 MinSize = sizeof(MachO::mach_header_64);
1131 if (Magic.size() >= MinSize)
1132 type = Magic[15] << 24 | Magic[14] << 12 |Magic[13] << 8 | Magic[12];
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001133 }
1134 switch (type) {
1135 default: break;
1136 case 1: return file_magic::macho_object;
1137 case 2: return file_magic::macho_executable;
1138 case 3: return file_magic::macho_fixed_virtual_memory_shared_lib;
1139 case 4: return file_magic::macho_core;
Rafael Espindola134cc992013-06-10 20:32:27 +00001140 case 5: return file_magic::macho_preload_executable;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001141 case 6: return file_magic::macho_dynamically_linked_shared_lib;
1142 case 7: return file_magic::macho_dynamic_linker;
1143 case 8: return file_magic::macho_bundle;
Nick Kledzik2d2b2542014-09-17 00:53:44 +00001144 case 9: return file_magic::macho_dynamically_linked_shared_lib_stub;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001145 case 10: return file_magic::macho_dsym_companion;
Justin Bognera7ad4b32015-02-25 22:59:20 +00001146 case 11: return file_magic::macho_kext_bundle;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001147 }
1148 break;
1149 }
1150 case 0xF0: // PowerPC Windows
1151 case 0x83: // Alpha 32-bit
1152 case 0x84: // Alpha 64-bit
1153 case 0x66: // MPS R4000 Windows
1154 case 0x50: // mc68K
1155 case 0x4c: // 80386 Windows
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +00001156 case 0xc4: // ARMNT Windows
Rafael Espindola9b404292013-06-11 17:22:12 +00001157 if (Magic[1] == 0x01)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001158 return file_magic::coff_object;
1159
1160 case 0x90: // PA-RISC Windows
1161 case 0x68: // mc68K Windows
Rafael Espindola9b404292013-06-11 17:22:12 +00001162 if (Magic[1] == 0x02)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001163 return file_magic::coff_object;
1164 break;
1165
David Majnemer50267222014-11-05 06:24:35 +00001166 case 'M': // Possible MS-DOS stub on Windows PE file
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001167 if (startswith(Magic, "MZ")) {
Rui Ueyama3206b792015-03-02 21:19:12 +00001168 uint32_t off = read32le(Magic.data() + 0x3c);
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001169 // PE/COFF file, either EXE or DLL.
David Majnemer50267222014-11-05 06:24:35 +00001170 if (off < Magic.size() &&
1171 memcmp(Magic.data()+off, COFF::PEMagic, sizeof(COFF::PEMagic)) == 0)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001172 return file_magic::pecoff_executable;
1173 }
1174 break;
1175
1176 case 0x64: // x86-64 Windows.
Rafael Espindola9b404292013-06-11 17:22:12 +00001177 if (Magic[1] == char(0x86))
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001178 return file_magic::coff_object;
1179 break;
1180
1181 default:
1182 break;
1183 }
1184 return file_magic::unknown;
1185}
1186
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001187std::error_code identify_magic(const Twine &Path, file_magic &Result) {
Rafael Espindolada70bfd2014-06-11 22:53:00 +00001188 int FD;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001189 if (std::error_code EC = openFileForRead(Path, FD))
Rafael Espindolada70bfd2014-06-11 22:53:00 +00001190 return EC;
Michael J. Spencer94b2ab32011-01-15 20:39:36 +00001191
Rafael Espindolada70bfd2014-06-11 22:53:00 +00001192 char Buffer[32];
1193 int Length = read(FD, Buffer, sizeof(Buffer));
Rafael Espindolaa8acef62014-06-25 14:35:59 +00001194 if (close(FD) != 0 || Length < 0)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001195 return std::error_code(errno, std::generic_category());
Rafael Espindolada70bfd2014-06-11 22:53:00 +00001196
1197 Result = identify_magic(StringRef(Buffer, Length));
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001198 return std::error_code();
Michael J. Spencer94b2ab32011-01-15 20:39:36 +00001199}
1200
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001201std::error_code directory_entry::status(file_status &result) const {
Aaron Ballman345012d2017-03-13 12:24:51 +00001202 return fs::status(Path, result, FollowSymlinks);
1203}
1204
James Henderson566fdf42017-03-16 11:22:09 +00001205ErrorOr<perms> getPermissions(const Twine &Path) {
1206 file_status Status;
1207 if (std::error_code EC = status(Path, Status))
1208 return EC;
1209
1210 return Status.permissions();
1211}
1212
Aaron Ballman345012d2017-03-13 12:24:51 +00001213} // end namespace fs
1214} // end namespace sys
1215} // end namespace llvm
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001216
1217// Include the truly platform-specific parts.
1218#if defined(LLVM_ON_UNIX)
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001219#include "Unix/Path.inc"
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001220#endif
1221#if defined(LLVM_ON_WIN32)
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001222#include "Windows/Path.inc"
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001223#endif
Pawel Bylica0e97e5c2015-11-02 09:49:17 +00001224
1225namespace llvm {
1226namespace sys {
1227namespace path {
1228
1229bool user_cache_directory(SmallVectorImpl<char> &Result, const Twine &Path1,
1230 const Twine &Path2, const Twine &Path3) {
1231 if (getUserCacheDir(Result)) {
1232 append(Result, Path1, Path2, Path3);
1233 return true;
1234 }
1235 return false;
1236}
1237
1238} // end namespace path
1239} // end namsspace sys
1240} // end namespace llvm