blob: ffb8ab220881cfa28488753beb40591a6deb2345 [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
Rui Ueyama5c69ff52014-09-11 22:34:32 +000014#include "llvm/Support/COFF.h"
Kevin Enderby87c85b72016-01-26 23:43:37 +000015#include "llvm/Support/MachO.h"
Rui Ueyama5c69ff52014-09-11 22:34:32 +000016#include "llvm/Support/Endian.h"
Rafael Espindola2a826e42014-06-13 17:20:48 +000017#include "llvm/Support/Errc.h"
Michael J. Spencerebad2f92010-11-29 22:28:51 +000018#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Support/FileSystem.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000020#include "llvm/Support/Path.h"
Aaron Ballman07e76182014-02-11 03:40:14 +000021#include "llvm/Support/Process.h"
Michael J. Spencerebad2f92010-11-29 22:28:51 +000022#include <cctype>
Michael J. Spencer848f46b2010-12-28 01:49:01 +000023#include <cstring>
Rafael Espindola4526b1d2013-06-18 17:01:00 +000024
25#if !defined(_MSC_VER) && !defined(__MINGW32__)
Douglas Gregora86ddf02013-03-21 21:46:10 +000026#include <unistd.h>
Rafael Espindola4526b1d2013-06-18 17:01:00 +000027#else
28#include <io.h>
Douglas Gregora86ddf02013-03-21 21:46:10 +000029#endif
Michael J. Spencerebad2f92010-11-29 22:28:51 +000030
Rafael Espindola7a0b6402014-02-24 03:07:41 +000031using namespace llvm;
Rui Ueyama3206b792015-03-02 21:19:12 +000032using namespace llvm::support::endian;
Rafael Espindola7a0b6402014-02-24 03:07:41 +000033
Michael J. Spencerebad2f92010-11-29 22:28:51 +000034namespace {
35 using llvm::StringRef;
Zhanyong Wan606bb1a2011-02-11 21:24:40 +000036 using llvm::sys::path::is_separator;
Zachary Turner5c5091f2017-03-16 22:28:04 +000037 using llvm::sys::path::Style;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000038
Zachary Turner5c5091f2017-03-16 22:28:04 +000039 inline Style real_style(Style style) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000040#ifdef LLVM_ON_WIN32
Zachary Turner5c5091f2017-03-16 22:28:04 +000041 return (style == Style::posix) ? Style::posix : Style::windows;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000042#else
Zachary Turner5c5091f2017-03-16 22:28:04 +000043 return (style == Style::windows) ? Style::windows : Style::posix;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000044#endif
Zachary Turner5c5091f2017-03-16 22:28:04 +000045 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +000046
Zachary Turner5c5091f2017-03-16 22:28:04 +000047 inline const char *separators(Style style) {
48 if (real_style(style) == Style::windows)
49 return "\\/";
50 return "/";
51 }
52
53 inline char preferred_separator(Style style) {
54 if (real_style(style) == Style::windows)
55 return '\\';
56 return '/';
57 }
58
59 StringRef find_first_component(StringRef path, Style style) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000060 // Look for this first component in the following order.
61 // * empty (in this case we return an empty string)
62 // * either C: or {//,\\}net.
63 // * {/,\}
Michael J. Spencerebad2f92010-11-29 22:28:51 +000064 // * {file,directory}name
65
66 if (path.empty())
67 return path;
68
Zachary Turner5c5091f2017-03-16 22:28:04 +000069 if (real_style(style) == Style::windows) {
70 // C:
71 if (path.size() >= 2 &&
72 std::isalpha(static_cast<unsigned char>(path[0])) && path[1] == ':')
73 return path.substr(0, 2);
74 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +000075
76 // //net
Zachary Turner5c5091f2017-03-16 22:28:04 +000077 if ((path.size() > 2) && is_separator(path[0], style) &&
78 path[0] == path[1] && !is_separator(path[2], style)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000079 // Find the next directory separator.
Zachary Turner5c5091f2017-03-16 22:28:04 +000080 size_t end = path.find_first_of(separators(style), 2);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000081 return path.substr(0, end);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000082 }
83
84 // {/,\}
Zachary Turner5c5091f2017-03-16 22:28:04 +000085 if (is_separator(path[0], style))
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000086 return path.substr(0, 1);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000087
Michael J. Spencerebad2f92010-11-29 22:28:51 +000088 // * {file,directory}name
Zachary Turner5c5091f2017-03-16 22:28:04 +000089 size_t end = path.find_first_of(separators(style));
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000090 return path.substr(0, end);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000091 }
Michael J. Spencer545cbdf2010-11-30 23:28:07 +000092
Zachary Turner5c5091f2017-03-16 22:28:04 +000093 size_t filename_pos(StringRef str, Style style) {
94 if (str.size() == 2 && is_separator(str[0], style) && str[0] == str[1])
Michael J. Spencer545cbdf2010-11-30 23:28:07 +000095 return 0;
96
Zachary Turner5c5091f2017-03-16 22:28:04 +000097 if (str.size() > 0 && is_separator(str[str.size() - 1], style))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +000098 return str.size() - 1;
99
Zachary Turner5c5091f2017-03-16 22:28:04 +0000100 size_t pos = str.find_last_of(separators(style), str.size() - 1);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000101
Zachary Turner5c5091f2017-03-16 22:28:04 +0000102 if (real_style(style) == Style::windows) {
103 if (pos == StringRef::npos)
104 pos = str.find_last_of(':', str.size() - 2);
105 }
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000106
Zachary Turner5c5091f2017-03-16 22:28:04 +0000107 if (pos == StringRef::npos || (pos == 1 && is_separator(str[0], style)))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000108 return 0;
109
110 return pos + 1;
111 }
112
Zachary Turner5c5091f2017-03-16 22:28:04 +0000113 size_t root_dir_start(StringRef str, Style style) {
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000114 // case "c:/"
Zachary Turner5c5091f2017-03-16 22:28:04 +0000115 if (real_style(style) == Style::windows) {
116 if (str.size() > 2 && str[1] == ':' && is_separator(str[2], style))
117 return 2;
118 }
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000119
120 // case "//"
Zachary Turner5c5091f2017-03-16 22:28:04 +0000121 if (str.size() == 2 && is_separator(str[0], style) && str[0] == str[1])
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000122 return StringRef::npos;
123
124 // case "//net"
Zachary Turner5c5091f2017-03-16 22:28:04 +0000125 if (str.size() > 3 && is_separator(str[0], style) && str[0] == str[1] &&
126 !is_separator(str[2], style)) {
127 return str.find_first_of(separators(style), 2);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000128 }
129
130 // case "/"
Zachary Turner5c5091f2017-03-16 22:28:04 +0000131 if (str.size() > 0 && is_separator(str[0], style))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000132 return 0;
133
134 return StringRef::npos;
135 }
136
Zachary Turner5c5091f2017-03-16 22:28:04 +0000137 size_t parent_path_end(StringRef path, Style style) {
138 size_t end_pos = filename_pos(path, style);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000139
Zachary Turner5c5091f2017-03-16 22:28:04 +0000140 bool filename_was_sep =
141 path.size() > 0 && is_separator(path[end_pos], style);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000142
143 // Skip separators except for root dir.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000144 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos), style);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000145
Zachary Turner5c5091f2017-03-16 22:28:04 +0000146 while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
147 is_separator(path[end_pos - 1], style))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000148 --end_pos;
149
150 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
151 return StringRef::npos;
152
153 return end_pos;
154 }
Zhanyong Wan606bb1a2011-02-11 21:24:40 +0000155} // end unnamed namespace
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000156
Rafael Espindolae79a8722013-06-28 03:48:47 +0000157enum FSEntity {
158 FS_Dir,
159 FS_File,
160 FS_Name
161};
162
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000163static std::error_code createUniqueEntity(const Twine &Model, int &ResultFD,
164 SmallVectorImpl<char> &ResultPath,
165 bool MakeAbsolute, unsigned Mode,
166 FSEntity Type) {
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000167 SmallString<128> ModelStorage;
168 Model.toVector(ModelStorage);
169
170 if (MakeAbsolute) {
171 // Make model absolute by prepending a temp directory if it's not already.
172 if (!sys::path::is_absolute(Twine(ModelStorage))) {
173 SmallString<128> TDir;
Rafael Espindola016a6d52014-08-26 14:47:52 +0000174 sys::path::system_temp_directory(true, TDir);
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000175 sys::path::append(TDir, Twine(ModelStorage));
176 ModelStorage.swap(TDir);
177 }
178 }
179
180 // From here on, DO NOT modify model. It may be needed if the randomly chosen
181 // path already exists.
182 ResultPath = ModelStorage;
183 // Null terminate.
184 ResultPath.push_back(0);
185 ResultPath.pop_back();
186
187retry_random_path:
188 // Replace '%' with random chars.
189 for (unsigned i = 0, e = ModelStorage.size(); i != e; ++i) {
190 if (ModelStorage[i] == '%')
191 ResultPath[i] = "0123456789abcdef"[sys::Process::GetRandomNumber() & 15];
192 }
193
194 // Try to open + create the file.
195 switch (Type) {
196 case FS_File: {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000197 if (std::error_code EC =
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000198 sys::fs::openFileForWrite(Twine(ResultPath.begin()), ResultFD,
199 sys::fs::F_RW | sys::fs::F_Excl, Mode)) {
Rafael Espindola2a826e42014-06-13 17:20:48 +0000200 if (EC == errc::file_exists)
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000201 goto retry_random_path;
202 return EC;
203 }
204
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000205 return std::error_code();
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000206 }
207
208 case FS_Name: {
Rafael Espindola281f23a2014-09-11 20:30:02 +0000209 std::error_code EC =
210 sys::fs::access(ResultPath.begin(), sys::fs::AccessMode::Exist);
211 if (EC == errc::no_such_file_or_directory)
212 return std::error_code();
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000213 if (EC)
214 return EC;
Rafael Espindola281f23a2014-09-11 20:30:02 +0000215 goto retry_random_path;
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000216 }
217
218 case FS_Dir: {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000219 if (std::error_code EC =
220 sys::fs::create_directory(ResultPath.begin(), false)) {
Rafael Espindola2a826e42014-06-13 17:20:48 +0000221 if (EC == errc::file_exists)
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000222 goto retry_random_path;
223 return EC;
224 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000225 return std::error_code();
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000226 }
227 }
228 llvm_unreachable("Invalid Type");
229}
Rafael Espindolae79a8722013-06-28 03:48:47 +0000230
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000231namespace llvm {
232namespace sys {
233namespace path {
234
Zachary Turner5c5091f2017-03-16 22:28:04 +0000235const_iterator begin(StringRef path, Style style) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000236 const_iterator i;
237 i.Path = path;
Zachary Turner5c5091f2017-03-16 22:28:04 +0000238 i.Component = find_first_component(path, style);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000239 i.Position = 0;
Zachary Turner5c5091f2017-03-16 22:28:04 +0000240 i.S = style;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000241 return i;
242}
243
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000244const_iterator end(StringRef path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000245 const_iterator i;
246 i.Path = path;
247 i.Position = path.size();
248 return i;
249}
250
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000251const_iterator &const_iterator::operator++() {
252 assert(Position < Path.size() && "Tried to increment past end!");
253
254 // Increment Position to past the current component
255 Position += Component.size();
256
257 // Check for end.
258 if (Position == Path.size()) {
259 Component = StringRef();
260 return *this;
261 }
262
263 // Both POSIX and Windows treat paths that begin with exactly two separators
264 // specially.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000265 bool was_net = Component.size() > 2 && is_separator(Component[0], S) &&
266 Component[1] == Component[0] && !is_separator(Component[2], S);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000267
268 // Handle separators.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000269 if (is_separator(Path[Position], S)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000270 // Root dir.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000271 if (was_net ||
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000272 // c:/
Zachary Turner5c5091f2017-03-16 22:28:04 +0000273 (real_style(S) == Style::windows && Component.endswith(":"))) {
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000274 Component = Path.substr(Position, 1);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000275 return *this;
276 }
277
278 // Skip extra separators.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000279 while (Position != Path.size() && is_separator(Path[Position], S)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000280 ++Position;
281 }
282
283 // Treat trailing '/' as a '.'.
284 if (Position == Path.size()) {
285 --Position;
286 Component = ".";
287 return *this;
288 }
289 }
290
291 // Find next component.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000292 size_t end_pos = Path.find_first_of(separators(S), Position);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000293 Component = Path.slice(Position, end_pos);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000294
295 return *this;
296}
297
Justin Bogner487e7642014-08-04 17:36:41 +0000298bool const_iterator::operator==(const const_iterator &RHS) const {
299 return Path.begin() == RHS.Path.begin() && Position == RHS.Position;
300}
301
302ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
303 return Position - RHS.Position;
304}
305
Zachary Turner5c5091f2017-03-16 22:28:04 +0000306reverse_iterator rbegin(StringRef Path, Style style) {
Justin Bogner487e7642014-08-04 17:36:41 +0000307 reverse_iterator I;
308 I.Path = Path;
309 I.Position = Path.size();
Zachary Turner5c5091f2017-03-16 22:28:04 +0000310 I.S = style;
Justin Bogner487e7642014-08-04 17:36:41 +0000311 return ++I;
312}
313
314reverse_iterator rend(StringRef Path) {
315 reverse_iterator I;
316 I.Path = Path;
317 I.Component = Path.substr(0, 0);
318 I.Position = 0;
319 return I;
320}
321
322reverse_iterator &reverse_iterator::operator++() {
Ben Langmuir8d116392014-03-05 19:56:30 +0000323 // If we're at the end and the previous char was a '/', return '.' unless
324 // we are the root path.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000325 size_t root_dir_pos = root_dir_start(Path, S);
326 if (Position == Path.size() && Path.size() > root_dir_pos + 1 &&
327 is_separator(Path[Position - 1], S)) {
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000328 --Position;
329 Component = ".";
330 return *this;
331 }
332
333 // Skip separators unless it's the root directory.
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000334 size_t end_pos = Position;
335
Zachary Turner5c5091f2017-03-16 22:28:04 +0000336 while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
337 is_separator(Path[end_pos - 1], S))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000338 --end_pos;
339
340 // Find next separator.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000341 size_t start_pos = filename_pos(Path.substr(0, end_pos), S);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000342 Component = Path.slice(start_pos, end_pos);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000343 Position = start_pos;
344 return *this;
345}
346
Justin Bogner487e7642014-08-04 17:36:41 +0000347bool reverse_iterator::operator==(const reverse_iterator &RHS) const {
348 return Path.begin() == RHS.Path.begin() && Component == RHS.Component &&
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000349 Position == RHS.Position;
350}
351
Filipe Cabecinhas78949382016-04-29 16:48:07 +0000352ptrdiff_t reverse_iterator::operator-(const reverse_iterator &RHS) const {
353 return Position - RHS.Position;
354}
355
Zachary Turner5c5091f2017-03-16 22:28:04 +0000356StringRef root_path(StringRef path, Style style) {
357 const_iterator b = begin(path, style), pos = b, e = end(path);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000358 if (b != e) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000359 bool has_net =
360 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
361 bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000362
363 if (has_net || has_drive) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000364 if ((++pos != e) && is_separator((*pos)[0], style)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000365 // {C:/,//net/}, so get the first two components.
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000366 return path.substr(0, b->size() + pos->size());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000367 } else {
368 // just {C:,//net}, return the first component.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000369 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000370 }
371 }
372
373 // POSIX style root directory.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000374 if (is_separator((*b)[0], style)) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000375 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000376 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000377 }
378
Michael J. Spencerf616b212010-12-07 17:04:04 +0000379 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000380}
381
Zachary Turner5c5091f2017-03-16 22:28:04 +0000382StringRef root_name(StringRef path, Style style) {
383 const_iterator b = begin(path, style), e = end(path);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000384 if (b != e) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000385 bool has_net =
386 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
387 bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000388
389 if (has_net || has_drive) {
390 // just {C:,//net}, return the first component.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000391 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000392 }
393 }
394
395 // No path or no name.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000396 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000397}
398
Zachary Turner5c5091f2017-03-16 22:28:04 +0000399StringRef root_directory(StringRef path, Style style) {
400 const_iterator b = begin(path, style), pos = b, e = end(path);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000401 if (b != e) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000402 bool has_net =
403 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
404 bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000405
406 if ((has_net || has_drive) &&
407 // {C:,//net}, skip to the next component.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000408 (++pos != e) && is_separator((*pos)[0], style)) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000409 return *pos;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000410 }
411
412 // POSIX style root directory.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000413 if (!has_net && is_separator((*b)[0], style)) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000414 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000415 }
416 }
417
418 // No path or no root.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000419 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000420}
421
Zachary Turner5c5091f2017-03-16 22:28:04 +0000422StringRef relative_path(StringRef path, Style style) {
423 StringRef root = root_path(path, style);
Michael J. Spencere6462392012-02-29 00:06:24 +0000424 return path.substr(root.size());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000425}
426
Zachary Turner5c5091f2017-03-16 22:28:04 +0000427void append(SmallVectorImpl<char> &path, Style style, const Twine &a,
428 const Twine &b, const Twine &c, const Twine &d) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000429 SmallString<32> a_storage;
430 SmallString<32> b_storage;
431 SmallString<32> c_storage;
432 SmallString<32> d_storage;
433
434 SmallVector<StringRef, 4> components;
435 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
436 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
437 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
438 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
439
Pawel Bylica64d08ff2015-10-22 08:12:15 +0000440 for (auto &component : components) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000441 bool path_has_sep =
442 !path.empty() && is_separator(path[path.size() - 1], style);
443 bool component_has_sep =
444 !component.empty() && is_separator(component[0], style);
445 bool is_root_name = has_root_name(component, style);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000446
447 if (path_has_sep) {
448 // Strip separators from beginning of component.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000449 size_t loc = component.find_first_not_of(separators(style));
Pawel Bylica64d08ff2015-10-22 08:12:15 +0000450 StringRef c = component.substr(loc);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000451
452 // Append it.
453 path.append(c.begin(), c.end());
454 continue;
455 }
456
Michael J. Spencer95e4ac12010-12-06 04:28:23 +0000457 if (!component_has_sep && !(path.empty() || is_root_name)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000458 // Add a separator.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000459 path.push_back(preferred_separator(style));
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000460 }
461
Pawel Bylica64d08ff2015-10-22 08:12:15 +0000462 path.append(component.begin(), component.end());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000463 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000464}
465
Zachary Turner5c5091f2017-03-16 22:28:04 +0000466void append(SmallVectorImpl<char> &path, const Twine &a, const Twine &b,
467 const Twine &c, const Twine &d) {
468 append(path, Style::native, a, b, c, d);
Argyrios Kyrtzidisa61736f2011-02-15 17:51:19 +0000469}
470
Zachary Turner5c5091f2017-03-16 22:28:04 +0000471void append(SmallVectorImpl<char> &path, const_iterator begin,
472 const_iterator end, Style style) {
473 for (; begin != end; ++begin)
474 path::append(path, style, *begin);
475}
476
477StringRef parent_path(StringRef path, Style style) {
478 size_t end_pos = parent_path_end(path, style);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000479 if (end_pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000480 return StringRef();
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000481 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000482 return path.substr(0, end_pos);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000483}
484
Zachary Turner5c5091f2017-03-16 22:28:04 +0000485void remove_filename(SmallVectorImpl<char> &path, Style style) {
486 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()), style);
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000487 if (end_pos != StringRef::npos)
488 path.set_size(end_pos);
Michael J. Spencer9c594092010-12-01 00:52:28 +0000489}
490
Zachary Turner5c5091f2017-03-16 22:28:04 +0000491void replace_extension(SmallVectorImpl<char> &path, const Twine &extension,
492 Style style) {
Michael J. Spencerfb3a95d2010-12-01 00:52:55 +0000493 StringRef p(path.begin(), path.size());
494 SmallString<32> ext_storage;
495 StringRef ext = extension.toStringRef(ext_storage);
496
497 // Erase existing extension.
498 size_t pos = p.find_last_of('.');
Zachary Turner5c5091f2017-03-16 22:28:04 +0000499 if (pos != StringRef::npos && pos >= filename_pos(p, style))
Michael J. Spencerfb3a95d2010-12-01 00:52:55 +0000500 path.set_size(pos);
501
502 // Append '.' if needed.
503 if (ext.size() > 0 && ext[0] != '.')
504 path.push_back('.');
505
506 // Append extension.
507 path.append(ext.begin(), ext.end());
Michael J. Spencerfb3a95d2010-12-01 00:52:55 +0000508}
509
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000510void replace_path_prefix(SmallVectorImpl<char> &Path,
Zachary Turner5c5091f2017-03-16 22:28:04 +0000511 const StringRef &OldPrefix, const StringRef &NewPrefix,
512 Style style) {
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000513 if (OldPrefix.empty() && NewPrefix.empty())
514 return;
515
516 StringRef OrigPath(Path.begin(), Path.size());
517 if (!OrigPath.startswith(OldPrefix))
518 return;
519
520 // If prefixes have the same size we can simply copy the new one over.
521 if (OldPrefix.size() == NewPrefix.size()) {
522 std::copy(NewPrefix.begin(), NewPrefix.end(), Path.begin());
523 return;
524 }
525
526 StringRef RelPath = OrigPath.substr(OldPrefix.size());
527 SmallString<256> NewPath;
Zachary Turner5c5091f2017-03-16 22:28:04 +0000528 path::append(NewPath, style, NewPrefix);
529 path::append(NewPath, style, RelPath);
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000530 Path.swap(NewPath);
531}
532
Zachary Turner5c5091f2017-03-16 22:28:04 +0000533void native(const Twine &path, SmallVectorImpl<char> &result, Style style) {
Benjamin Kramerbd4ac9b2013-09-11 10:45:21 +0000534 assert((!path.isSingleStringRef() ||
535 path.getSingleStringRef().data() != result.data()) &&
536 "path and result are not allowed to overlap!");
Michael J. Spencer80025002010-12-01 02:48:27 +0000537 // Clear result.
Michael J. Spencer98c7a112010-12-07 01:23:19 +0000538 result.clear();
Michael J. Spencer80025002010-12-01 02:48:27 +0000539 path.toVector(result);
Zachary Turner5c5091f2017-03-16 22:28:04 +0000540 native(result, style);
Benjamin Kramerbd4ac9b2013-09-11 10:45:21 +0000541}
542
Zachary Turner5c5091f2017-03-16 22:28:04 +0000543void native(SmallVectorImpl<char> &Path, Style style) {
Serge Pavlov9c761a32017-03-01 09:38:15 +0000544 if (Path.empty())
545 return;
Zachary Turner5c5091f2017-03-16 22:28:04 +0000546 if (real_style(style) == Style::windows) {
547 std::replace(Path.begin(), Path.end(), '/', '\\');
548 if (Path[0] == '~' && (Path.size() == 1 || is_separator(Path[1], style))) {
549 SmallString<128> PathHome;
550 home_directory(PathHome);
551 PathHome.append(Path.begin() + 1, Path.end());
552 Path = PathHome;
553 }
554 } else {
555 for (auto PI = Path.begin(), PE = Path.end(); PI < PE; ++PI) {
556 if (*PI == '\\') {
557 auto PN = PI + 1;
558 if (PN < PE && *PN == '\\')
559 ++PI; // increment once, the for loop will move over the escaped slash
560 else
561 *PI = '/';
562 }
Rafael Espindolad649b9d2014-08-08 21:29:34 +0000563 }
564 }
Michael J. Spencer80025002010-12-01 02:48:27 +0000565}
566
Zachary Turner5c5091f2017-03-16 22:28:04 +0000567std::string convert_to_slash(StringRef path, Style style) {
568 if (real_style(style) != Style::windows)
569 return path;
570
Rui Ueyama3e649032017-01-09 01:47:15 +0000571 std::string s = path.str();
572 std::replace(s.begin(), s.end(), '\\', '/');
573 return s;
Rui Ueyama3e649032017-01-09 01:47:15 +0000574}
575
Zachary Turner5c5091f2017-03-16 22:28:04 +0000576StringRef filename(StringRef path, Style style) { return *rbegin(path, style); }
Michael J. Spencer14269202010-12-01 03:18:17 +0000577
Zachary Turner5c5091f2017-03-16 22:28:04 +0000578StringRef stem(StringRef path, Style style) {
579 StringRef fname = filename(path, style);
Michael J. Spencer956955e2010-12-01 03:18:33 +0000580 size_t pos = fname.find_last_of('.');
581 if (pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000582 return fname;
Michael J. Spencer956955e2010-12-01 03:18:33 +0000583 else
584 if ((fname.size() == 1 && fname == ".") ||
585 (fname.size() == 2 && fname == ".."))
Michael J. Spencerf616b212010-12-07 17:04:04 +0000586 return fname;
Michael J. Spencer956955e2010-12-01 03:18:33 +0000587 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000588 return fname.substr(0, pos);
Michael J. Spencer956955e2010-12-01 03:18:33 +0000589}
590
Zachary Turner5c5091f2017-03-16 22:28:04 +0000591StringRef extension(StringRef path, Style style) {
592 StringRef fname = filename(path, style);
Michael J. Spencer87106c52010-12-01 03:37:41 +0000593 size_t pos = fname.find_last_of('.');
594 if (pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000595 return StringRef();
Michael J. Spencer87106c52010-12-01 03:37:41 +0000596 else
597 if ((fname.size() == 1 && fname == ".") ||
598 (fname.size() == 2 && fname == ".."))
Michael J. Spencerf616b212010-12-07 17:04:04 +0000599 return StringRef();
Michael J. Spencer87106c52010-12-01 03:37:41 +0000600 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000601 return fname.substr(pos);
Michael J. Spencer87106c52010-12-01 03:37:41 +0000602}
603
Zachary Turner5c5091f2017-03-16 22:28:04 +0000604bool is_separator(char value, Style style) {
605 if (value == '/')
606 return true;
607 if (real_style(style) == Style::windows)
608 return value == '\\';
609 return false;
Zhanyong Wan606bb1a2011-02-11 21:24:40 +0000610}
611
Zachary Turner5c5091f2017-03-16 22:28:04 +0000612StringRef get_separator(Style style) {
613 if (real_style(style) == Style::windows)
614 return "\\";
615 return "/";
Yaron Keren15217202014-05-16 13:16:30 +0000616}
617
Zachary Turner5c5091f2017-03-16 22:28:04 +0000618bool has_root_name(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000619 SmallString<128> path_storage;
620 StringRef p = path.toStringRef(path_storage);
621
Zachary Turner5c5091f2017-03-16 22:28:04 +0000622 return !root_name(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000623}
624
Zachary Turner5c5091f2017-03-16 22:28:04 +0000625bool has_root_directory(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000626 SmallString<128> path_storage;
627 StringRef p = path.toStringRef(path_storage);
628
Zachary Turner5c5091f2017-03-16 22:28:04 +0000629 return !root_directory(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000630}
631
Zachary Turner5c5091f2017-03-16 22:28:04 +0000632bool has_root_path(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000633 SmallString<128> path_storage;
634 StringRef p = path.toStringRef(path_storage);
635
Zachary Turner5c5091f2017-03-16 22:28:04 +0000636 return !root_path(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000637}
638
Zachary Turner5c5091f2017-03-16 22:28:04 +0000639bool has_relative_path(const Twine &path, Style style) {
Michael J. Spencer6d4b7e72010-12-20 13:30:28 +0000640 SmallString<128> path_storage;
641 StringRef p = path.toStringRef(path_storage);
642
Zachary Turner5c5091f2017-03-16 22:28:04 +0000643 return !relative_path(p, style).empty();
Michael J. Spencer6d4b7e72010-12-20 13:30:28 +0000644}
645
Zachary Turner5c5091f2017-03-16 22:28:04 +0000646bool has_filename(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000647 SmallString<128> path_storage;
648 StringRef p = path.toStringRef(path_storage);
649
Zachary Turner5c5091f2017-03-16 22:28:04 +0000650 return !filename(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000651}
652
Zachary Turner5c5091f2017-03-16 22:28:04 +0000653bool has_parent_path(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000654 SmallString<128> path_storage;
655 StringRef p = path.toStringRef(path_storage);
656
Zachary Turner5c5091f2017-03-16 22:28:04 +0000657 return !parent_path(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000658}
659
Zachary Turner5c5091f2017-03-16 22:28:04 +0000660bool has_stem(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000661 SmallString<128> path_storage;
662 StringRef p = path.toStringRef(path_storage);
663
Zachary Turner5c5091f2017-03-16 22:28:04 +0000664 return !stem(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000665}
666
Zachary Turner5c5091f2017-03-16 22:28:04 +0000667bool has_extension(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000668 SmallString<128> path_storage;
669 StringRef p = path.toStringRef(path_storage);
670
Zachary Turner5c5091f2017-03-16 22:28:04 +0000671 return !extension(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000672}
673
Zachary Turner5c5091f2017-03-16 22:28:04 +0000674bool is_absolute(const Twine &path, Style style) {
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000675 SmallString<128> path_storage;
676 StringRef p = path.toStringRef(path_storage);
677
Zachary Turner5c5091f2017-03-16 22:28:04 +0000678 bool rootDir = has_root_directory(p, style);
679 bool rootName =
680 (real_style(style) != Style::windows) || has_root_name(p, style);
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000681
Michael J. Spencerf616b212010-12-07 17:04:04 +0000682 return rootDir && rootName;
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000683}
684
Zachary Turner5c5091f2017-03-16 22:28:04 +0000685bool is_relative(const Twine &path, Style style) {
686 return !is_absolute(path, style);
687}
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000688
Zachary Turner5c5091f2017-03-16 22:28:04 +0000689StringRef remove_leading_dotslash(StringRef Path, Style style) {
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000690 // Remove leading "./" (or ".//" or "././" etc.)
Zachary Turner5c5091f2017-03-16 22:28:04 +0000691 while (Path.size() > 2 && Path[0] == '.' && is_separator(Path[1], style)) {
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000692 Path = Path.substr(2);
Zachary Turner5c5091f2017-03-16 22:28:04 +0000693 while (Path.size() > 0 && is_separator(Path[0], style))
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000694 Path = Path.substr(1);
695 }
696 return Path;
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000697}
698
Zachary Turner5c5091f2017-03-16 22:28:04 +0000699static SmallString<256> remove_dots(StringRef path, bool remove_dot_dot,
700 Style style) {
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000701 SmallVector<StringRef, 16> components;
702
703 // Skip the root path, then look for traversal in the components.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000704 StringRef rel = path::relative_path(path, style);
705 for (StringRef C :
706 llvm::make_range(path::begin(rel, style), path::end(rel))) {
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000707 if (C == ".")
708 continue;
Benjamin Kramer937dd7a2016-10-17 13:28:21 +0000709 // Leading ".." will remain in the path unless it's at the root.
710 if (remove_dot_dot && C == "..") {
711 if (!components.empty() && components.back() != "..") {
712 components.pop_back();
713 continue;
714 }
Zachary Turner5c5091f2017-03-16 22:28:04 +0000715 if (path::is_absolute(path, style))
Benjamin Kramer937dd7a2016-10-17 13:28:21 +0000716 continue;
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000717 }
718 components.push_back(C);
719 }
720
Zachary Turner5c5091f2017-03-16 22:28:04 +0000721 SmallString<256> buffer = path::root_path(path, style);
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000722 for (StringRef C : components)
Zachary Turner5c5091f2017-03-16 22:28:04 +0000723 path::append(buffer, style, C);
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000724 return buffer;
725}
726
Zachary Turner5c5091f2017-03-16 22:28:04 +0000727bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot,
728 Style style) {
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000729 StringRef p(path.data(), path.size());
730
Zachary Turner5c5091f2017-03-16 22:28:04 +0000731 SmallString<256> result = remove_dots(p, remove_dot_dot, style);
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000732 if (result == path)
733 return false;
734
735 path.swap(result);
736 return true;
737}
738
Michael J. Spencer9fc1d9d2010-12-01 19:32:01 +0000739} // end namespace path
740
741namespace fs {
742
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000743std::error_code getUniqueID(const Twine Path, UniqueID &Result) {
Rafael Espindola7f822a92013-07-29 21:26:49 +0000744 file_status Status;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000745 std::error_code EC = status(Path, Status);
Rafael Espindola7f822a92013-07-29 21:26:49 +0000746 if (EC)
747 return EC;
748 Result = Status.getUniqueID();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000749 return std::error_code();
Rafael Espindola7f822a92013-07-29 21:26:49 +0000750}
751
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000752std::error_code createUniqueFile(const Twine &Model, int &ResultFd,
753 SmallVectorImpl<char> &ResultPath,
754 unsigned Mode) {
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000755 return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
756}
757
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000758std::error_code createUniqueFile(const Twine &Model,
759 SmallVectorImpl<char> &ResultPath) {
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000760 int Dummy;
761 return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
762}
763
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000764static std::error_code
765createTemporaryFile(const Twine &Model, int &ResultFD,
766 llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) {
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000767 SmallString<128> Storage;
768 StringRef P = Model.toNullTerminatedStringRef(Storage);
Zachary Turner5c5091f2017-03-16 22:28:04 +0000769 assert(P.find_first_of(separators(Style::native)) == StringRef::npos &&
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000770 "Model must be a simple filename.");
771 // Use P.begin() so that createUniqueEntity doesn't need to recreate Storage.
772 return createUniqueEntity(P.begin(), ResultFD, ResultPath,
773 true, owner_read | owner_write, Type);
774}
775
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000776static std::error_code
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000777createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000778 llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) {
Rafael Espindolad3c89042013-07-25 15:00:17 +0000779 const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
780 return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000781 Type);
782}
783
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000784std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
785 int &ResultFD,
786 SmallVectorImpl<char> &ResultPath) {
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000787 return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File);
788}
789
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000790std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
791 SmallVectorImpl<char> &ResultPath) {
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000792 int Dummy;
793 return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
794}
795
796
Rafael Espindolae79a8722013-06-28 03:48:47 +0000797// This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
Rafael Espindola31a24432013-06-28 10:55:41 +0000798// for consistency. We should try using mkdtemp.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000799std::error_code createUniqueDirectory(const Twine &Prefix,
800 SmallVectorImpl<char> &ResultPath) {
Rafael Espindolae79a8722013-06-28 03:48:47 +0000801 int Dummy;
802 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath,
803 true, 0, FS_Dir);
Rafael Espindola7ffacc42013-06-27 03:45:31 +0000804}
805
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000806static std::error_code make_absolute(const Twine &current_directory,
807 SmallVectorImpl<char> &path,
808 bool use_current_directory) {
Michael J. Spencer92903a32010-12-07 03:57:17 +0000809 StringRef p(path.data(), path.size());
810
Zachary Turner5c5091f2017-03-16 22:28:04 +0000811 bool rootDirectory = path::has_root_directory(p);
812 bool rootName =
813 (real_style(Style::native) != Style::windows) || path::has_root_name(p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000814
815 // Already absolute.
816 if (rootName && rootDirectory)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000817 return std::error_code();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000818
819 // All of the following conditions will need the current directory.
820 SmallString<128> current_dir;
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000821 if (use_current_directory)
822 current_directory.toVector(current_dir);
823 else if (std::error_code ec = current_path(current_dir))
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000824 return ec;
Michael J. Spencer92903a32010-12-07 03:57:17 +0000825
826 // Relative path. Prepend the current directory.
827 if (!rootName && !rootDirectory) {
828 // Append path to the current directory.
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000829 path::append(current_dir, p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000830 // Set path to the result.
831 path.swap(current_dir);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000832 return std::error_code();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000833 }
834
835 if (!rootName && rootDirectory) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000836 StringRef cdrn = path::root_name(current_dir);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000837 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000838 path::append(curDirRootName, p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000839 // Set path to the result.
840 path.swap(curDirRootName);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000841 return std::error_code();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000842 }
843
844 if (rootName && !rootDirectory) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000845 StringRef pRootName = path::root_name(p);
846 StringRef bRootDirectory = path::root_directory(current_dir);
847 StringRef bRelativePath = path::relative_path(current_dir);
848 StringRef pRelativePath = path::relative_path(p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000849
850 SmallString<128> res;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000851 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000852 path.swap(res);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000853 return std::error_code();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000854 }
855
856 llvm_unreachable("All rootName and rootDirectory combinations should have "
857 "occurred above!");
858}
859
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000860std::error_code make_absolute(const Twine &current_directory,
861 SmallVectorImpl<char> &path) {
862 return make_absolute(current_directory, path, true);
863}
864
865std::error_code make_absolute(SmallVectorImpl<char> &path) {
866 return make_absolute(Twine(), path, false);
867}
868
Frederic Riss6b9396c2015-08-06 21:04:55 +0000869std::error_code create_directories(const Twine &Path, bool IgnoreExisting,
870 perms Perms) {
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000871 SmallString<128> PathStorage;
872 StringRef P = Path.toStringRef(PathStorage);
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000873
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000874 // Be optimistic and try to create the directory
Frederic Riss6b9396c2015-08-06 21:04:55 +0000875 std::error_code EC = create_directory(P, IgnoreExisting, Perms);
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000876 // If we succeeded, or had any error other than the parent not existing, just
877 // return it.
Rafael Espindola2a826e42014-06-13 17:20:48 +0000878 if (EC != errc::no_such_file_or_directory)
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000879 return EC;
Michael J. Spencerf616b212010-12-07 17:04:04 +0000880
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000881 // We failed because of a no_such_file_or_directory, try to create the
882 // parent.
883 StringRef Parent = path::parent_path(P);
884 if (Parent.empty())
885 return EC;
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000886
Frederic Riss6b9396c2015-08-06 21:04:55 +0000887 if ((EC = create_directories(Parent, IgnoreExisting, Perms)))
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000888 return EC;
889
Frederic Riss6b9396c2015-08-06 21:04:55 +0000890 return create_directory(P, IgnoreExisting, Perms);
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000891}
892
Justin Bognercd45f962014-06-19 19:35:39 +0000893std::error_code copy_file(const Twine &From, const Twine &To) {
894 int ReadFD, WriteFD;
895 if (std::error_code EC = openFileForRead(From, ReadFD))
896 return EC;
897 if (std::error_code EC = openFileForWrite(To, WriteFD, F_None)) {
898 close(ReadFD);
899 return EC;
900 }
901
902 const size_t BufSize = 4096;
Dylan Noblesmith42836d92014-08-26 02:03:30 +0000903 char *Buf = new char[BufSize];
Justin Bognercd45f962014-06-19 19:35:39 +0000904 int BytesRead = 0, BytesWritten = 0;
905 for (;;) {
906 BytesRead = read(ReadFD, Buf, BufSize);
907 if (BytesRead <= 0)
908 break;
909 while (BytesRead) {
910 BytesWritten = write(WriteFD, Buf, BytesRead);
911 if (BytesWritten < 0)
912 break;
913 BytesRead -= BytesWritten;
914 }
915 if (BytesWritten < 0)
916 break;
917 }
918 close(ReadFD);
919 close(WriteFD);
Dylan Noblesmith42836d92014-08-26 02:03:30 +0000920 delete[] Buf;
Justin Bognercd45f962014-06-19 19:35:39 +0000921
922 if (BytesRead < 0 || BytesWritten < 0)
923 return std::error_code(errno, std::generic_category());
924 return std::error_code();
925}
926
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000927bool exists(file_status status) {
928 return status_known(status) && status.type() != file_type::file_not_found;
929}
930
931bool status_known(file_status s) {
932 return s.type() != file_type::status_error;
933}
934
Zachary Turner82dd5422017-03-07 16:10:10 +0000935file_type get_file_type(const Twine &Path, bool Follow) {
Zachary Turner990e3cd2017-03-07 03:43:17 +0000936 file_status st;
Zachary Turner82dd5422017-03-07 16:10:10 +0000937 if (status(Path, st, Follow))
Zachary Turner990e3cd2017-03-07 03:43:17 +0000938 return file_type::status_error;
939 return st.type();
940}
941
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000942bool is_directory(file_status status) {
943 return status.type() == file_type::directory_file;
944}
945
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000946std::error_code is_directory(const Twine &path, bool &result) {
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000947 file_status st;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000948 if (std::error_code ec = status(path, st))
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000949 return ec;
950 result = is_directory(st);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000951 return std::error_code();
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000952}
953
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000954bool is_regular_file(file_status status) {
955 return status.type() == file_type::regular_file;
956}
957
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000958std::error_code is_regular_file(const Twine &path, bool &result) {
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000959 file_status st;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000960 if (std::error_code ec = status(path, st))
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000961 return ec;
962 result = is_regular_file(st);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000963 return std::error_code();
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000964}
965
Zachary Turner7d86ee52017-03-08 17:56:08 +0000966bool is_symlink_file(file_status status) {
967 return status.type() == file_type::symlink_file;
968}
969
970std::error_code is_symlink_file(const Twine &path, bool &result) {
971 file_status st;
972 if (std::error_code ec = status(path, st, false))
973 return ec;
974 result = is_symlink_file(st);
975 return std::error_code();
976}
977
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000978bool is_other(file_status status) {
979 return exists(status) &&
980 !is_regular_file(status) &&
Rafael Espindola20063062014-03-20 17:39:04 +0000981 !is_directory(status);
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000982}
983
Juergen Ributzka84ba3422014-12-18 18:19:47 +0000984std::error_code is_other(const Twine &Path, bool &Result) {
985 file_status FileStatus;
986 if (std::error_code EC = status(Path, FileStatus))
987 return EC;
988 Result = is_other(FileStatus);
989 return std::error_code();
990}
991
Benjamin Kramer91ead3c2011-09-14 01:14:36 +0000992void directory_entry::replace_filename(const Twine &filename, file_status st) {
Rafael Espindolaf662e002015-07-15 21:24:07 +0000993 SmallString<128> path = path::parent_path(Path);
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000994 path::append(path, filename);
995 Path = path.str();
996 Status = st;
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000997}
998
Rui Ueyama6b77ad32016-11-15 01:57:05 +0000999template <size_t N>
1000static bool startswith(StringRef Magic, const char (&S)[N]) {
1001 return Magic.startswith(StringRef(S, N - 1));
1002}
1003
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001004/// @brief Identify the magic in magic.
Logan Chienc0f691d2014-06-25 13:46:17 +00001005file_magic identify_magic(StringRef Magic) {
Rafael Espindola9b404292013-06-11 17:22:12 +00001006 if (Magic.size() < 4)
Michael J. Spencer96ebd912012-06-19 05:29:57 +00001007 return file_magic::unknown;
Rafael Espindola9b404292013-06-11 17:22:12 +00001008 switch ((unsigned char)Magic[0]) {
Rui Ueyamafc149a62013-10-15 22:45:38 +00001009 case 0x00: {
Rui Ueyama2d021662016-11-15 00:54:54 +00001010 // COFF bigobj, CL.exe's LTO object file, or short import library file
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001011 if (startswith(Magic, "\0\0\xFF\xFF")) {
Rui Ueyama5c69ff52014-09-11 22:34:32 +00001012 size_t MinSize = offsetof(COFF::BigObjHeader, UUID) + sizeof(COFF::BigObjMagic);
1013 if (Magic.size() < MinSize)
1014 return file_magic::coff_import_library;
1015
Rui Ueyama5c69ff52014-09-11 22:34:32 +00001016 const char *Start = Magic.data() + offsetof(COFF::BigObjHeader, UUID);
Rui Ueyama2d021662016-11-15 00:54:54 +00001017 if (memcmp(Start, COFF::BigObjMagic, sizeof(COFF::BigObjMagic)) == 0)
1018 return file_magic::coff_object;
1019 if (memcmp(Start, COFF::ClGlObjMagic, sizeof(COFF::BigObjMagic)) == 0)
1020 return file_magic::coff_cl_gl_object;
1021 return file_magic::coff_import_library;
Rui Ueyama2acb0582014-09-11 21:09:57 +00001022 }
Rui Ueyamafc149a62013-10-15 22:45:38 +00001023 // Windows resource file
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001024 if (startswith(Magic, "\0\0\0\0\x20\0\0\0\xFF"))
Rui Ueyamafc149a62013-10-15 22:45:38 +00001025 return file_magic::windows_resource;
Rui Ueyama829c4392013-11-14 22:09:08 +00001026 // 0x0000 = COFF unknown machine type
1027 if (Magic[1] == 0)
1028 return file_magic::coff_object;
Derek Schuff2c6f75d2016-11-30 16:49:11 +00001029 if (startswith(Magic, "\0asm"))
1030 return file_magic::wasm_object;
Rui Ueyamafc149a62013-10-15 22:45:38 +00001031 break;
1032 }
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001033 case 0xDE: // 0x0B17C0DE = BC wraper
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001034 if (startswith(Magic, "\xDE\xC0\x17\x0B"))
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001035 return file_magic::bitcode;
1036 break;
1037 case 'B':
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001038 if (startswith(Magic, "BC\xC0\xDE"))
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001039 return file_magic::bitcode;
1040 break;
1041 case '!':
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001042 if (startswith(Magic, "!<arch>\n") || startswith(Magic, "!<thin>\n"))
1043 return file_magic::archive;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001044 break;
1045
1046 case '\177':
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001047 if (startswith(Magic, "\177ELF") && Magic.size() >= 18) {
Rafael Espindola9b404292013-06-11 17:22:12 +00001048 bool Data2MSB = Magic[5] == 2;
Michael J. Spencerb8055cb2013-04-05 20:10:04 +00001049 unsigned high = Data2MSB ? 16 : 17;
1050 unsigned low = Data2MSB ? 17 : 16;
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001051 if (Magic[high] == 0) {
Rafael Espindola9b404292013-06-11 17:22:12 +00001052 switch (Magic[low]) {
Michael J. Spencere368a622015-01-23 21:58:09 +00001053 default: return file_magic::elf;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001054 case 1: return file_magic::elf_relocatable;
1055 case 2: return file_magic::elf_executable;
1056 case 3: return file_magic::elf_shared_object;
1057 case 4: return file_magic::elf_core;
1058 }
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001059 }
1060 // It's still some type of ELF file.
1061 return file_magic::elf;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001062 }
1063 break;
1064
1065 case 0xCA:
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001066 if (startswith(Magic, "\xCA\xFE\xBA\xBE") ||
1067 startswith(Magic, "\xCA\xFE\xBA\xBF")) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001068 // This is complicated by an overlap with Java class files.
1069 // See the Mach-O section in /usr/share/file/magic for details.
Rafael Espindola9b404292013-06-11 17:22:12 +00001070 if (Magic.size() >= 8 && Magic[7] < 43)
Alexey Samsonove6388e62013-06-18 15:03:28 +00001071 return file_magic::macho_universal_binary;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001072 }
1073 break;
1074
1075 // The two magic numbers for mach-o are:
1076 // 0xfeedface - 32-bit mach-o
1077 // 0xfeedfacf - 64-bit mach-o
1078 case 0xFE:
1079 case 0xCE:
1080 case 0xCF: {
1081 uint16_t type = 0;
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001082 if (startswith(Magic, "\xFE\xED\xFA\xCE") ||
1083 startswith(Magic, "\xFE\xED\xFA\xCF")) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001084 /* Native endian */
Kevin Enderby87c85b72016-01-26 23:43:37 +00001085 size_t MinSize;
1086 if (Magic[3] == char(0xCE))
1087 MinSize = sizeof(MachO::mach_header);
1088 else
1089 MinSize = sizeof(MachO::mach_header_64);
1090 if (Magic.size() >= MinSize)
1091 type = Magic[12] << 24 | Magic[13] << 12 | Magic[14] << 8 | Magic[15];
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001092 } else if (startswith(Magic, "\xCE\xFA\xED\xFE") ||
1093 startswith(Magic, "\xCF\xFA\xED\xFE")) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001094 /* Reverse endian */
Kevin Enderby87c85b72016-01-26 23:43:37 +00001095 size_t MinSize;
1096 if (Magic[0] == char(0xCE))
1097 MinSize = sizeof(MachO::mach_header);
1098 else
1099 MinSize = sizeof(MachO::mach_header_64);
1100 if (Magic.size() >= MinSize)
1101 type = Magic[15] << 24 | Magic[14] << 12 |Magic[13] << 8 | Magic[12];
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001102 }
1103 switch (type) {
1104 default: break;
1105 case 1: return file_magic::macho_object;
1106 case 2: return file_magic::macho_executable;
1107 case 3: return file_magic::macho_fixed_virtual_memory_shared_lib;
1108 case 4: return file_magic::macho_core;
Rafael Espindola134cc992013-06-10 20:32:27 +00001109 case 5: return file_magic::macho_preload_executable;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001110 case 6: return file_magic::macho_dynamically_linked_shared_lib;
1111 case 7: return file_magic::macho_dynamic_linker;
1112 case 8: return file_magic::macho_bundle;
Nick Kledzik2d2b2542014-09-17 00:53:44 +00001113 case 9: return file_magic::macho_dynamically_linked_shared_lib_stub;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001114 case 10: return file_magic::macho_dsym_companion;
Justin Bognera7ad4b32015-02-25 22:59:20 +00001115 case 11: return file_magic::macho_kext_bundle;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001116 }
1117 break;
1118 }
1119 case 0xF0: // PowerPC Windows
1120 case 0x83: // Alpha 32-bit
1121 case 0x84: // Alpha 64-bit
1122 case 0x66: // MPS R4000 Windows
1123 case 0x50: // mc68K
1124 case 0x4c: // 80386 Windows
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +00001125 case 0xc4: // ARMNT Windows
Rafael Espindola9b404292013-06-11 17:22:12 +00001126 if (Magic[1] == 0x01)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001127 return file_magic::coff_object;
1128
1129 case 0x90: // PA-RISC Windows
1130 case 0x68: // mc68K Windows
Rafael Espindola9b404292013-06-11 17:22:12 +00001131 if (Magic[1] == 0x02)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001132 return file_magic::coff_object;
1133 break;
1134
David Majnemer50267222014-11-05 06:24:35 +00001135 case 'M': // Possible MS-DOS stub on Windows PE file
Rui Ueyama6b77ad32016-11-15 01:57:05 +00001136 if (startswith(Magic, "MZ")) {
Rui Ueyama3206b792015-03-02 21:19:12 +00001137 uint32_t off = read32le(Magic.data() + 0x3c);
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001138 // PE/COFF file, either EXE or DLL.
David Majnemer50267222014-11-05 06:24:35 +00001139 if (off < Magic.size() &&
1140 memcmp(Magic.data()+off, COFF::PEMagic, sizeof(COFF::PEMagic)) == 0)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001141 return file_magic::pecoff_executable;
1142 }
1143 break;
1144
1145 case 0x64: // x86-64 Windows.
Rafael Espindola9b404292013-06-11 17:22:12 +00001146 if (Magic[1] == char(0x86))
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001147 return file_magic::coff_object;
1148 break;
1149
1150 default:
1151 break;
1152 }
1153 return file_magic::unknown;
1154}
1155
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001156std::error_code identify_magic(const Twine &Path, file_magic &Result) {
Rafael Espindolada70bfd2014-06-11 22:53:00 +00001157 int FD;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001158 if (std::error_code EC = openFileForRead(Path, FD))
Rafael Espindolada70bfd2014-06-11 22:53:00 +00001159 return EC;
Michael J. Spencer94b2ab32011-01-15 20:39:36 +00001160
Rafael Espindolada70bfd2014-06-11 22:53:00 +00001161 char Buffer[32];
1162 int Length = read(FD, Buffer, sizeof(Buffer));
Rafael Espindolaa8acef62014-06-25 14:35:59 +00001163 if (close(FD) != 0 || Length < 0)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001164 return std::error_code(errno, std::generic_category());
Rafael Espindolada70bfd2014-06-11 22:53:00 +00001165
1166 Result = identify_magic(StringRef(Buffer, Length));
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001167 return std::error_code();
Michael J. Spencer94b2ab32011-01-15 20:39:36 +00001168}
1169
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001170std::error_code directory_entry::status(file_status &result) const {
Aaron Ballman345012d2017-03-13 12:24:51 +00001171 return fs::status(Path, result, FollowSymlinks);
1172}
1173
James Henderson566fdf42017-03-16 11:22:09 +00001174ErrorOr<perms> getPermissions(const Twine &Path) {
1175 file_status Status;
1176 if (std::error_code EC = status(Path, Status))
1177 return EC;
1178
1179 return Status.permissions();
1180}
1181
Aaron Ballman345012d2017-03-13 12:24:51 +00001182} // end namespace fs
1183} // end namespace sys
1184} // end namespace llvm
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001185
1186// Include the truly platform-specific parts.
1187#if defined(LLVM_ON_UNIX)
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001188#include "Unix/Path.inc"
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001189#endif
1190#if defined(LLVM_ON_WIN32)
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001191#include "Windows/Path.inc"
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001192#endif
Pawel Bylica0e97e5c2015-11-02 09:49:17 +00001193
1194namespace llvm {
1195namespace sys {
1196namespace path {
1197
1198bool user_cache_directory(SmallVectorImpl<char> &Result, const Twine &Path1,
1199 const Twine &Path2, const Twine &Path3) {
1200 if (getUserCacheDir(Result)) {
1201 append(Result, Path1, Path2, Path3);
1202 return true;
1203 }
1204 return false;
1205}
1206
1207} // end namespace path
1208} // end namsspace sys
1209} // end namespace llvm