blob: 0e02953c549a14bd40dac71fd0923820b19b8f0b [file] [log] [blame]
Michael J. Spencerdffde992010-11-29 22:28:51 +00001//===-- PathV2.cpp - Implement OS Path Concept ------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the operating system PathV2 API.
11//
12//===----------------------------------------------------------------------===//
13
Rafael Espindolaa11c3e22013-06-11 22:21:28 +000014#include "llvm/Support/Path.h"
Michael J. Spencer5b082302011-12-13 23:17:12 +000015#include "llvm/Support/Endian.h"
Michael J. Spencerdffde992010-11-29 22:28:51 +000016#include "llvm/Support/ErrorHandling.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/Support/FileSystem.h"
Michael J. Spencerdffde992010-11-29 22:28:51 +000018#include <cctype>
Michael J. Spencer628467e2010-12-28 01:49:01 +000019#include <cstdio>
20#include <cstring>
Rafael Espindola8ee23f02013-06-18 17:01:00 +000021
22#if !defined(_MSC_VER) && !defined(__MINGW32__)
Douglas Gregorb0b5bde2013-03-21 21:46:10 +000023#include <unistd.h>
Rafael Espindola8ee23f02013-06-18 17:01:00 +000024#else
25#include <io.h>
Douglas Gregorb0b5bde2013-03-21 21:46:10 +000026#endif
Michael J. Spencerdffde992010-11-29 22:28:51 +000027
28namespace {
29 using llvm::StringRef;
Zhanyong Wan63cc3a82011-02-11 21:24:40 +000030 using llvm::sys::path::is_separator;
Michael J. Spencerdffde992010-11-29 22:28:51 +000031
32#ifdef LLVM_ON_WIN32
Benjamin Kramer80fd2a12012-02-08 13:13:47 +000033 const char *separators = "\\/";
34 const char prefered_separator = '\\';
Michael J. Spencerdffde992010-11-29 22:28:51 +000035#else
Benjamin Kramer80fd2a12012-02-08 13:13:47 +000036 const char separators = '/';
37 const char prefered_separator = '/';
Michael J. Spencerdffde992010-11-29 22:28:51 +000038#endif
39
Benjamin Kramer7fb86662010-12-17 18:59:09 +000040 StringRef find_first_component(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000041 // Look for this first component in the following order.
42 // * empty (in this case we return an empty string)
43 // * either C: or {//,\\}net.
44 // * {/,\}
45 // * {.,..}
46 // * {file,directory}name
47
48 if (path.empty())
49 return path;
50
Michael J. Spencerca3a3392010-12-07 03:57:48 +000051#ifdef LLVM_ON_WIN32
Michael J. Spencerdffde992010-11-29 22:28:51 +000052 // C:
Guy Benyei87d0b9e2013-02-12 21:21:59 +000053 if (path.size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) &&
54 path[1] == ':')
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000055 return path.substr(0, 2);
Michael J. Spencerca3a3392010-12-07 03:57:48 +000056#endif
Michael J. Spencerdffde992010-11-29 22:28:51 +000057
58 // //net
59 if ((path.size() > 2) &&
Michael J. Spencerca3a3392010-12-07 03:57:48 +000060 is_separator(path[0]) &&
61 path[0] == path[1] &&
62 !is_separator(path[2])) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000063 // Find the next directory separator.
Michael J. Spencerca3a3392010-12-07 03:57:48 +000064 size_t end = path.find_first_of(separators, 2);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000065 return path.substr(0, end);
Michael J. Spencerdffde992010-11-29 22:28:51 +000066 }
67
68 // {/,\}
Michael J. Spencerca3a3392010-12-07 03:57:48 +000069 if (is_separator(path[0]))
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000070 return path.substr(0, 1);
Michael J. Spencerdffde992010-11-29 22:28:51 +000071
72 if (path.startswith(".."))
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000073 return path.substr(0, 2);
Michael J. Spencerdffde992010-11-29 22:28:51 +000074
75 if (path[0] == '.')
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000076 return path.substr(0, 1);
Michael J. Spencerdffde992010-11-29 22:28:51 +000077
78 // * {file,directory}name
Michael J. Spencerca3a3392010-12-07 03:57:48 +000079 size_t end = path.find_first_of(separators, 2);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000080 return path.substr(0, end);
Michael J. Spencerdffde992010-11-29 22:28:51 +000081 }
Michael J. Spencera42cf732010-11-30 23:28:07 +000082
Benjamin Kramerd53f3c82010-12-17 18:19:06 +000083 size_t filename_pos(StringRef str) {
Michael J. Spencera42cf732010-11-30 23:28:07 +000084 if (str.size() == 2 &&
85 is_separator(str[0]) &&
Michael J. Spencerca3a3392010-12-07 03:57:48 +000086 str[0] == str[1])
Michael J. Spencera42cf732010-11-30 23:28:07 +000087 return 0;
88
89 if (str.size() > 0 && is_separator(str[str.size() - 1]))
90 return str.size() - 1;
91
92 size_t pos = str.find_last_of(separators, str.size() - 1);
93
94#ifdef LLVM_ON_WIN32
95 if (pos == StringRef::npos)
96 pos = str.find_last_of(':', str.size() - 2);
97#endif
98
99 if (pos == StringRef::npos ||
100 (pos == 1 && is_separator(str[0])))
101 return 0;
102
103 return pos + 1;
104 }
105
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000106 size_t root_dir_start(StringRef str) {
Michael J. Spencera42cf732010-11-30 23:28:07 +0000107 // case "c:/"
108#ifdef LLVM_ON_WIN32
109 if (str.size() > 2 &&
110 str[1] == ':' &&
111 is_separator(str[2]))
112 return 2;
113#endif
114
115 // case "//"
116 if (str.size() == 2 &&
117 is_separator(str[0]) &&
118 str[0] == str[1])
119 return StringRef::npos;
120
121 // case "//net"
122 if (str.size() > 3 &&
123 is_separator(str[0]) &&
124 str[0] == str[1] &&
125 !is_separator(str[2])) {
126 return str.find_first_of(separators, 2);
127 }
128
129 // case "/"
130 if (str.size() > 0 && is_separator(str[0]))
131 return 0;
132
133 return StringRef::npos;
134 }
135
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000136 size_t parent_path_end(StringRef path) {
Michael J. Spencera42cf732010-11-30 23:28:07 +0000137 size_t end_pos = filename_pos(path);
138
139 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
140
141 // Skip separators except for root dir.
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000142 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
Michael J. Spencera42cf732010-11-30 23:28:07 +0000143
144 while(end_pos > 0 &&
145 (end_pos - 1) != root_dir_pos &&
146 is_separator(path[end_pos - 1]))
147 --end_pos;
148
149 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
150 return StringRef::npos;
151
152 return end_pos;
153 }
Zhanyong Wan63cc3a82011-02-11 21:24:40 +0000154} // end unnamed namespace
Michael J. Spencerdffde992010-11-29 22:28:51 +0000155
156namespace llvm {
157namespace sys {
158namespace path {
159
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000160const_iterator begin(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000161 const_iterator i;
162 i.Path = path;
163 i.Component = find_first_component(path);
164 i.Position = 0;
165 return i;
166}
167
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000168const_iterator end(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000169 const_iterator i;
170 i.Path = path;
171 i.Position = path.size();
172 return i;
173}
174
Michael J. Spencerdffde992010-11-29 22:28:51 +0000175const_iterator &const_iterator::operator++() {
176 assert(Position < Path.size() && "Tried to increment past end!");
177
178 // Increment Position to past the current component
179 Position += Component.size();
180
181 // Check for end.
182 if (Position == Path.size()) {
183 Component = StringRef();
184 return *this;
185 }
186
187 // Both POSIX and Windows treat paths that begin with exactly two separators
188 // specially.
189 bool was_net = Component.size() > 2 &&
190 is_separator(Component[0]) &&
191 Component[1] == Component[0] &&
192 !is_separator(Component[2]);
193
194 // Handle separators.
195 if (is_separator(Path[Position])) {
196 // Root dir.
197 if (was_net
198#ifdef LLVM_ON_WIN32
199 // c:/
200 || Component.endswith(":")
201#endif
202 ) {
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000203 Component = Path.substr(Position, 1);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000204 return *this;
205 }
206
207 // Skip extra separators.
208 while (Position != Path.size() &&
209 is_separator(Path[Position])) {
210 ++Position;
211 }
212
213 // Treat trailing '/' as a '.'.
214 if (Position == Path.size()) {
215 --Position;
216 Component = ".";
217 return *this;
218 }
219 }
220
221 // Find next component.
222 size_t end_pos = Path.find_first_of(separators, Position);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000223 Component = Path.slice(Position, end_pos);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000224
225 return *this;
226}
227
Michael J. Spencera42cf732010-11-30 23:28:07 +0000228const_iterator &const_iterator::operator--() {
229 // If we're at the end and the previous char was a '/', return '.'.
230 if (Position == Path.size() &&
231 Path.size() > 1 &&
232 is_separator(Path[Position - 1])
233#ifdef LLVM_ON_WIN32
234 && Path[Position - 2] != ':'
235#endif
236 ) {
237 --Position;
238 Component = ".";
239 return *this;
240 }
241
242 // Skip separators unless it's the root directory.
243 size_t root_dir_pos = root_dir_start(Path);
244 size_t end_pos = Position;
245
246 while(end_pos > 0 &&
247 (end_pos - 1) != root_dir_pos &&
248 is_separator(Path[end_pos - 1]))
249 --end_pos;
250
251 // Find next separator.
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000252 size_t start_pos = filename_pos(Path.substr(0, end_pos));
253 Component = Path.slice(start_pos, end_pos);
Michael J. Spencera42cf732010-11-30 23:28:07 +0000254 Position = start_pos;
255 return *this;
256}
257
Michael J. Spencerdffde992010-11-29 22:28:51 +0000258bool const_iterator::operator==(const const_iterator &RHS) const {
259 return Path.begin() == RHS.Path.begin() &&
260 Position == RHS.Position;
261}
262
263bool const_iterator::operator!=(const const_iterator &RHS) const {
264 return !(*this == RHS);
265}
266
Michael J. Spencera42cf732010-11-30 23:28:07 +0000267ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
268 return Position - RHS.Position;
269}
270
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000271const StringRef root_path(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000272 const_iterator b = begin(path),
273 pos = b,
274 e = end(path);
275 if (b != e) {
276 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
277 bool has_drive =
278#ifdef LLVM_ON_WIN32
279 b->endswith(":");
280#else
281 false;
282#endif
283
284 if (has_net || has_drive) {
285 if ((++pos != e) && is_separator((*pos)[0])) {
286 // {C:/,//net/}, so get the first two components.
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000287 return path.substr(0, b->size() + pos->size());
Michael J. Spencerdffde992010-11-29 22:28:51 +0000288 } else {
289 // just {C:,//net}, return the first component.
Michael J. Spencer50291592010-12-07 17:04:04 +0000290 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000291 }
292 }
293
294 // POSIX style root directory.
295 if (is_separator((*b)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000296 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000297 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000298 }
299
Michael J. Spencer50291592010-12-07 17:04:04 +0000300 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000301}
302
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000303const StringRef root_name(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000304 const_iterator b = begin(path),
305 e = end(path);
306 if (b != e) {
307 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
308 bool has_drive =
309#ifdef LLVM_ON_WIN32
310 b->endswith(":");
311#else
312 false;
313#endif
314
315 if (has_net || has_drive) {
316 // just {C:,//net}, return the first component.
Michael J. Spencer50291592010-12-07 17:04:04 +0000317 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000318 }
319 }
320
321 // No path or no name.
Michael J. Spencer50291592010-12-07 17:04:04 +0000322 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000323}
324
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000325const StringRef root_directory(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000326 const_iterator b = begin(path),
327 pos = b,
328 e = end(path);
329 if (b != e) {
330 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
331 bool has_drive =
332#ifdef LLVM_ON_WIN32
333 b->endswith(":");
334#else
335 false;
336#endif
337
338 if ((has_net || has_drive) &&
339 // {C:,//net}, skip to the next component.
340 (++pos != e) && is_separator((*pos)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000341 return *pos;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000342 }
343
344 // POSIX style root directory.
345 if (!has_net && is_separator((*b)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000346 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000347 }
348 }
349
350 // No path or no root.
Michael J. Spencer50291592010-12-07 17:04:04 +0000351 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000352}
353
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000354const StringRef relative_path(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000355 StringRef root = root_path(path);
Michael J. Spencerd0fff182012-02-29 00:06:24 +0000356 return path.substr(root.size());
Michael J. Spencerdffde992010-11-29 22:28:51 +0000357}
358
Michael J. Spencer936671b2010-12-07 03:57:37 +0000359void append(SmallVectorImpl<char> &path, const Twine &a,
360 const Twine &b,
361 const Twine &c,
362 const Twine &d) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000363 SmallString<32> a_storage;
364 SmallString<32> b_storage;
365 SmallString<32> c_storage;
366 SmallString<32> d_storage;
367
368 SmallVector<StringRef, 4> components;
369 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
370 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
371 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
372 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
373
374 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
375 e = components.end();
376 i != e; ++i) {
377 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
378 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
Michael J. Spencer50291592010-12-07 17:04:04 +0000379 bool is_root_name = has_root_name(*i);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000380
381 if (path_has_sep) {
382 // Strip separators from beginning of component.
383 size_t loc = i->find_first_not_of(separators);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000384 StringRef c = i->substr(loc);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000385
386 // Append it.
387 path.append(c.begin(), c.end());
388 continue;
389 }
390
Michael J. Spencerf150e762010-12-06 04:28:23 +0000391 if (!component_has_sep && !(path.empty() || is_root_name)) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000392 // Add a separator.
393 path.push_back(prefered_separator);
394 }
395
396 path.append(i->begin(), i->end());
397 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000398}
399
Argyrios Kyrtzidis6e31b9b2011-02-15 17:51:19 +0000400void append(SmallVectorImpl<char> &path,
401 const_iterator begin, const_iterator end) {
402 for (; begin != end; ++begin)
403 path::append(path, *begin);
404}
405
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000406const StringRef parent_path(StringRef path) {
Michael J. Spencera42cf732010-11-30 23:28:07 +0000407 size_t end_pos = parent_path_end(path);
408 if (end_pos == StringRef::npos)
Michael J. Spencer50291592010-12-07 17:04:04 +0000409 return StringRef();
Michael J. Spencera42cf732010-11-30 23:28:07 +0000410 else
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000411 return path.substr(0, end_pos);
Michael J. Spencera42cf732010-11-30 23:28:07 +0000412}
413
Michael J. Spencer936671b2010-12-07 03:57:37 +0000414void remove_filename(SmallVectorImpl<char> &path) {
Michael J. Spencerdbfb56b2010-12-01 00:52:28 +0000415 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
Michael J. Spencer936671b2010-12-07 03:57:37 +0000416 if (end_pos != StringRef::npos)
417 path.set_size(end_pos);
Michael J. Spencerdbfb56b2010-12-01 00:52:28 +0000418}
419
Michael J. Spencer50291592010-12-07 17:04:04 +0000420void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
Michael J. Spencer52ed8672010-12-01 00:52:55 +0000421 StringRef p(path.begin(), path.size());
422 SmallString<32> ext_storage;
423 StringRef ext = extension.toStringRef(ext_storage);
424
425 // Erase existing extension.
426 size_t pos = p.find_last_of('.');
427 if (pos != StringRef::npos && pos >= filename_pos(p))
428 path.set_size(pos);
429
430 // Append '.' if needed.
431 if (ext.size() > 0 && ext[0] != '.')
432 path.push_back('.');
433
434 // Append extension.
435 path.append(ext.begin(), ext.end());
Michael J. Spencer52ed8672010-12-01 00:52:55 +0000436}
437
Michael J. Spencer936671b2010-12-07 03:57:37 +0000438void native(const Twine &path, SmallVectorImpl<char> &result) {
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000439 // Clear result.
Michael J. Spencerfbd1bbd2010-12-07 01:23:19 +0000440 result.clear();
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000441#ifdef LLVM_ON_WIN32
442 SmallString<128> path_storage;
443 StringRef p = path.toStringRef(path_storage);
444 result.reserve(p.size());
445 for (StringRef::const_iterator i = p.begin(),
446 e = p.end();
447 i != e;
448 ++i) {
449 if (*i == '/')
450 result.push_back('\\');
451 else
452 result.push_back(*i);
453 }
454#else
455 path.toVector(result);
456#endif
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000457}
458
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000459const StringRef filename(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000460 return *(--end(path));
Michael J. Spencera9793552010-12-01 03:18:17 +0000461}
462
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000463const StringRef stem(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000464 StringRef fname = filename(path);
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000465 size_t pos = fname.find_last_of('.');
466 if (pos == StringRef::npos)
Michael J. Spencer50291592010-12-07 17:04:04 +0000467 return fname;
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000468 else
469 if ((fname.size() == 1 && fname == ".") ||
470 (fname.size() == 2 && fname == ".."))
Michael J. Spencer50291592010-12-07 17:04:04 +0000471 return fname;
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000472 else
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000473 return fname.substr(0, pos);
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000474}
475
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000476const StringRef extension(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000477 StringRef fname = filename(path);
Michael J. Spencer5265f222010-12-01 03:37:41 +0000478 size_t pos = fname.find_last_of('.');
479 if (pos == StringRef::npos)
Michael J. Spencer50291592010-12-07 17:04:04 +0000480 return StringRef();
Michael J. Spencer5265f222010-12-01 03:37:41 +0000481 else
482 if ((fname.size() == 1 && fname == ".") ||
483 (fname.size() == 2 && fname == ".."))
Michael J. Spencer50291592010-12-07 17:04:04 +0000484 return StringRef();
Michael J. Spencer5265f222010-12-01 03:37:41 +0000485 else
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000486 return fname.substr(pos);
Michael J. Spencer5265f222010-12-01 03:37:41 +0000487}
488
Zhanyong Wan63cc3a82011-02-11 21:24:40 +0000489bool is_separator(char value) {
490 switch(value) {
491#ifdef LLVM_ON_WIN32
492 case '\\': // fall through
493#endif
494 case '/': return true;
495 default: return false;
496 }
497}
498
Douglas Gregor55cf8152011-09-14 20:27:01 +0000499void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result) {
500 result.clear();
Michael J. Spencerfaebf112011-12-13 23:16:15 +0000501
Douglas Gregorb0b5bde2013-03-21 21:46:10 +0000502#ifdef __APPLE__
503 // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR.
504 int ConfName = erasedOnReboot? _CS_DARWIN_USER_TEMP_DIR
505 : _CS_DARWIN_USER_CACHE_DIR;
506 size_t ConfLen = confstr(ConfName, 0, 0);
507 if (ConfLen > 0) {
508 do {
509 result.resize(ConfLen);
510 ConfLen = confstr(ConfName, result.data(), result.size());
511 } while (ConfLen > 0 && ConfLen != result.size());
512
513 if (ConfLen > 0) {
514 assert(result.back() == 0);
515 result.pop_back();
516 return;
517 }
518
519 result.clear();
520 }
521#endif
522
Douglas Gregor55cf8152011-09-14 20:27:01 +0000523 // Check whether the temporary directory is specified by an environment
524 // variable.
525 const char *EnvironmentVariable;
526#ifdef LLVM_ON_WIN32
527 EnvironmentVariable = "TEMP";
528#else
529 EnvironmentVariable = "TMPDIR";
530#endif
531 if (char *RequestedDir = getenv(EnvironmentVariable)) {
532 result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
533 return;
534 }
Michael J. Spencerfaebf112011-12-13 23:16:15 +0000535
Douglas Gregor55cf8152011-09-14 20:27:01 +0000536 // Fall back to a system default.
537 const char *DefaultResult;
538#ifdef LLVM_ON_WIN32
539 (void)erasedOnReboot;
Douglas Gregord26d6b62011-09-14 23:21:47 +0000540 DefaultResult = "C:\\TEMP";
Douglas Gregor55cf8152011-09-14 20:27:01 +0000541#else
542 if (erasedOnReboot)
543 DefaultResult = "/tmp";
544 else
545 DefaultResult = "/var/tmp";
546#endif
547 result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
548}
Michael J. Spencerfaebf112011-12-13 23:16:15 +0000549
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000550bool has_root_name(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000551 SmallString<128> path_storage;
552 StringRef p = path.toStringRef(path_storage);
553
Michael J. Spencer50291592010-12-07 17:04:04 +0000554 return !root_name(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000555}
556
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000557bool has_root_directory(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000558 SmallString<128> path_storage;
559 StringRef p = path.toStringRef(path_storage);
560
Michael J. Spencer50291592010-12-07 17:04:04 +0000561 return !root_directory(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000562}
563
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000564bool has_root_path(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000565 SmallString<128> path_storage;
566 StringRef p = path.toStringRef(path_storage);
567
Michael J. Spencer50291592010-12-07 17:04:04 +0000568 return !root_path(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000569}
570
Michael J. Spencer2d484eb2010-12-20 13:30:28 +0000571bool has_relative_path(const Twine &path) {
572 SmallString<128> path_storage;
573 StringRef p = path.toStringRef(path_storage);
574
575 return !relative_path(p).empty();
576}
577
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000578bool has_filename(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000579 SmallString<128> path_storage;
580 StringRef p = path.toStringRef(path_storage);
581
Michael J. Spencer50291592010-12-07 17:04:04 +0000582 return !filename(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000583}
584
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000585bool has_parent_path(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000586 SmallString<128> path_storage;
587 StringRef p = path.toStringRef(path_storage);
588
Michael J. Spencer50291592010-12-07 17:04:04 +0000589 return !parent_path(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000590}
591
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000592bool has_stem(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000593 SmallString<128> path_storage;
594 StringRef p = path.toStringRef(path_storage);
595
Michael J. Spencer50291592010-12-07 17:04:04 +0000596 return !stem(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000597}
598
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000599bool has_extension(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000600 SmallString<128> path_storage;
601 StringRef p = path.toStringRef(path_storage);
602
Michael J. Spencer50291592010-12-07 17:04:04 +0000603 return !extension(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000604}
605
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000606bool is_absolute(const Twine &path) {
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000607 SmallString<128> path_storage;
608 StringRef p = path.toStringRef(path_storage);
609
Michael J. Spencer50291592010-12-07 17:04:04 +0000610 bool rootDir = has_root_directory(p),
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000611#ifdef LLVM_ON_WIN32
Michael J. Spencer50291592010-12-07 17:04:04 +0000612 rootName = has_root_name(p);
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000613#else
Michael J. Spencer50291592010-12-07 17:04:04 +0000614 rootName = true;
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000615#endif
616
Michael J. Spencer50291592010-12-07 17:04:04 +0000617 return rootDir && rootName;
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000618}
619
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000620bool is_relative(const Twine &path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000621 return !is_absolute(path);
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000622}
623
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000624} // end namespace path
625
626namespace fs {
627
Rafael Espindola8ee23f02013-06-18 17:01:00 +0000628error_code unique_file(const Twine &Model, SmallVectorImpl<char> &ResultPath,
629 bool MakeAbsolute, unsigned Mode) {
630 int FD;
631 error_code Ret = unique_file(Model, FD, ResultPath, MakeAbsolute, Mode);
632 close(FD);
633 return Ret;
634}
635
Michael J. Spenceree271d82010-12-07 03:57:17 +0000636error_code make_absolute(SmallVectorImpl<char> &path) {
637 StringRef p(path.data(), path.size());
638
Daniel Dunbard1d72162012-02-29 00:20:37 +0000639 bool rootDirectory = path::has_root_directory(p),
640#ifdef LLVM_ON_WIN32
Daniel Dunbar95fe7b92012-02-29 00:46:46 +0000641 rootName = path::has_root_name(p);
Daniel Dunbard1d72162012-02-29 00:20:37 +0000642#else
643 rootName = true;
644#endif
Michael J. Spenceree271d82010-12-07 03:57:17 +0000645
646 // Already absolute.
647 if (rootName && rootDirectory)
David Blaikie58604cd2012-02-09 19:24:12 +0000648 return error_code::success();
Michael J. Spenceree271d82010-12-07 03:57:17 +0000649
650 // All of the following conditions will need the current directory.
651 SmallString<128> current_dir;
652 if (error_code ec = current_path(current_dir)) return ec;
653
654 // Relative path. Prepend the current directory.
655 if (!rootName && !rootDirectory) {
656 // Append path to the current directory.
Michael J. Spencer936671b2010-12-07 03:57:37 +0000657 path::append(current_dir, p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000658 // Set path to the result.
659 path.swap(current_dir);
David Blaikie58604cd2012-02-09 19:24:12 +0000660 return error_code::success();
Michael J. Spenceree271d82010-12-07 03:57:17 +0000661 }
662
663 if (!rootName && rootDirectory) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000664 StringRef cdrn = path::root_name(current_dir);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000665 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
Michael J. Spencer936671b2010-12-07 03:57:37 +0000666 path::append(curDirRootName, p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000667 // Set path to the result.
668 path.swap(curDirRootName);
David Blaikie58604cd2012-02-09 19:24:12 +0000669 return error_code::success();
Michael J. Spenceree271d82010-12-07 03:57:17 +0000670 }
671
672 if (rootName && !rootDirectory) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000673 StringRef pRootName = path::root_name(p);
674 StringRef bRootDirectory = path::root_directory(current_dir);
675 StringRef bRelativePath = path::relative_path(current_dir);
676 StringRef pRelativePath = path::relative_path(p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000677
678 SmallString<128> res;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000679 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000680 path.swap(res);
David Blaikie58604cd2012-02-09 19:24:12 +0000681 return error_code::success();
Michael J. Spenceree271d82010-12-07 03:57:17 +0000682 }
683
684 llvm_unreachable("All rootName and rootDirectory combinations should have "
685 "occurred above!");
686}
687
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000688error_code create_directories(const Twine &path, bool &existed) {
689 SmallString<128> path_storage;
690 StringRef p = path.toStringRef(path_storage);
691
Michael J. Spencer50291592010-12-07 17:04:04 +0000692 StringRef parent = path::parent_path(p);
Michael J. Spencer55f43d62012-03-21 23:09:14 +0000693 if (!parent.empty()) {
694 bool parent_exists;
695 if (error_code ec = fs::exists(parent, parent_exists)) return ec;
Michael J. Spencer50291592010-12-07 17:04:04 +0000696
Michael J. Spencer55f43d62012-03-21 23:09:14 +0000697 if (!parent_exists)
698 if (error_code ec = create_directories(parent, existed)) return ec;
699 }
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000700
701 return create_directory(p, existed);
702}
703
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000704bool exists(file_status status) {
705 return status_known(status) && status.type() != file_type::file_not_found;
706}
707
708bool status_known(file_status s) {
709 return s.type() != file_type::status_error;
710}
711
712bool is_directory(file_status status) {
713 return status.type() == file_type::directory_file;
714}
715
Michael J. Spencer218dc3e2011-01-11 01:21:55 +0000716error_code is_directory(const Twine &path, bool &result) {
717 file_status st;
718 if (error_code ec = status(path, st))
719 return ec;
720 result = is_directory(st);
David Blaikie58604cd2012-02-09 19:24:12 +0000721 return error_code::success();
Michael J. Spencer218dc3e2011-01-11 01:21:55 +0000722}
723
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000724bool is_regular_file(file_status status) {
725 return status.type() == file_type::regular_file;
726}
727
Michael J. Spencer218dc3e2011-01-11 01:21:55 +0000728error_code is_regular_file(const Twine &path, bool &result) {
729 file_status st;
730 if (error_code ec = status(path, st))
731 return ec;
732 result = is_regular_file(st);
David Blaikie58604cd2012-02-09 19:24:12 +0000733 return error_code::success();
Michael J. Spencer218dc3e2011-01-11 01:21:55 +0000734}
735
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000736bool is_symlink(file_status status) {
737 return status.type() == file_type::symlink_file;
738}
739
Michael J. Spencer9df536c2011-01-12 23:55:06 +0000740error_code is_symlink(const Twine &path, bool &result) {
741 file_status st;
742 if (error_code ec = status(path, st))
743 return ec;
744 result = is_symlink(st);
David Blaikie58604cd2012-02-09 19:24:12 +0000745 return error_code::success();
Michael J. Spencer9df536c2011-01-12 23:55:06 +0000746}
747
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000748bool is_other(file_status status) {
749 return exists(status) &&
750 !is_regular_file(status) &&
751 !is_directory(status) &&
752 !is_symlink(status);
753}
754
Benjamin Kramer357b5712011-09-14 01:14:36 +0000755void directory_entry::replace_filename(const Twine &filename, file_status st) {
Michael J. Spencer753cbbb2010-12-06 04:28:42 +0000756 SmallString<128> path(Path.begin(), Path.end());
757 path::remove_filename(path);
758 path::append(path, filename);
759 Path = path.str();
760 Status = st;
Michael J. Spencer753cbbb2010-12-06 04:28:42 +0000761}
762
Michael J. Spencer628467e2010-12-28 01:49:01 +0000763error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
Michael J. Spencer628467e2010-12-28 01:49:01 +0000764 SmallString<32> MagicStorage;
Michael J. Spencerb33594b2011-01-15 18:52:41 +0000765 StringRef Magic = magic.toStringRef(MagicStorage);
766 SmallString<32> Buffer;
Michael J. Spencer628467e2010-12-28 01:49:01 +0000767
Michael J. Spencerb33594b2011-01-15 18:52:41 +0000768 if (error_code ec = get_magic(path, Magic.size(), Buffer)) {
769 if (ec == errc::value_too_large) {
770 // Magic.size() > file_size(Path).
Michael J. Spencer628467e2010-12-28 01:49:01 +0000771 result = false;
David Blaikie58604cd2012-02-09 19:24:12 +0000772 return error_code::success();
Michael J. Spencer628467e2010-12-28 01:49:01 +0000773 }
Michael J. Spencerb33594b2011-01-15 18:52:41 +0000774 return ec;
Michael J. Spencer628467e2010-12-28 01:49:01 +0000775 }
Michael J. Spencer628467e2010-12-28 01:49:01 +0000776
Michael J. Spencerb33594b2011-01-15 18:52:41 +0000777 result = Magic == Buffer;
David Blaikie58604cd2012-02-09 19:24:12 +0000778 return error_code::success();
Michael J. Spencer628467e2010-12-28 01:49:01 +0000779}
780
Michael J. Spencer5b082302011-12-13 23:17:12 +0000781/// @brief Identify the magic in magic.
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000782 file_magic identify_magic(StringRef Magic) {
783 if (Magic.size() < 4)
Michael J. Spencerb51c8e92012-06-19 05:29:57 +0000784 return file_magic::unknown;
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000785 switch ((unsigned char)Magic[0]) {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000786 case 0xDE: // 0x0B17C0DE = BC wraper
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000787 if (Magic[1] == (char)0xC0 && Magic[2] == (char)0x17 &&
788 Magic[3] == (char)0x0B)
Michael J. Spencer5b082302011-12-13 23:17:12 +0000789 return file_magic::bitcode;
790 break;
791 case 'B':
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000792 if (Magic[1] == 'C' && Magic[2] == (char)0xC0 && Magic[3] == (char)0xDE)
Michael J. Spencer5b082302011-12-13 23:17:12 +0000793 return file_magic::bitcode;
794 break;
795 case '!':
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000796 if (Magic.size() >= 8)
797 if (memcmp(Magic.data(),"!<arch>\n",8) == 0)
Michael J. Spencer5b082302011-12-13 23:17:12 +0000798 return file_magic::archive;
799 break;
800
801 case '\177':
Rafael Espindola01797062013-06-11 17:25:45 +0000802 if (Magic.size() >= 18 && Magic[1] == 'E' && Magic[2] == 'L' &&
803 Magic[3] == 'F') {
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000804 bool Data2MSB = Magic[5] == 2;
Michael J. Spencerc0a74e22013-04-05 20:10:04 +0000805 unsigned high = Data2MSB ? 16 : 17;
806 unsigned low = Data2MSB ? 17 : 16;
Rafael Espindola01797062013-06-11 17:25:45 +0000807 if (Magic[high] == 0)
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000808 switch (Magic[low]) {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000809 default: break;
810 case 1: return file_magic::elf_relocatable;
811 case 2: return file_magic::elf_executable;
812 case 3: return file_magic::elf_shared_object;
813 case 4: return file_magic::elf_core;
814 }
815 }
816 break;
817
818 case 0xCA:
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000819 if (Magic[1] == char(0xFE) && Magic[2] == char(0xBA) &&
820 Magic[3] == char(0xBE)) {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000821 // This is complicated by an overlap with Java class files.
822 // See the Mach-O section in /usr/share/file/magic for details.
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000823 if (Magic.size() >= 8 && Magic[7] < 43)
Alexey Samsonov9c22f872013-06-18 15:03:28 +0000824 return file_magic::macho_universal_binary;
Michael J. Spencer5b082302011-12-13 23:17:12 +0000825 }
826 break;
827
828 // The two magic numbers for mach-o are:
829 // 0xfeedface - 32-bit mach-o
830 // 0xfeedfacf - 64-bit mach-o
831 case 0xFE:
832 case 0xCE:
833 case 0xCF: {
834 uint16_t type = 0;
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000835 if (Magic[0] == char(0xFE) && Magic[1] == char(0xED) &&
836 Magic[2] == char(0xFA) &&
837 (Magic[3] == char(0xCE) || Magic[3] == char(0xCF))) {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000838 /* Native endian */
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000839 if (Magic.size() >= 16) type = Magic[14] << 8 | Magic[15];
840 } else if ((Magic[0] == char(0xCE) || Magic[0] == char(0xCF)) &&
841 Magic[1] == char(0xFA) && Magic[2] == char(0xED) &&
842 Magic[3] == char(0xFE)) {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000843 /* Reverse endian */
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000844 if (Magic.size() >= 14) type = Magic[13] << 8 | Magic[12];
Michael J. Spencer5b082302011-12-13 23:17:12 +0000845 }
846 switch (type) {
847 default: break;
848 case 1: return file_magic::macho_object;
849 case 2: return file_magic::macho_executable;
850 case 3: return file_magic::macho_fixed_virtual_memory_shared_lib;
851 case 4: return file_magic::macho_core;
Rafael Espindola6d315c62013-06-10 20:32:27 +0000852 case 5: return file_magic::macho_preload_executable;
Michael J. Spencer5b082302011-12-13 23:17:12 +0000853 case 6: return file_magic::macho_dynamically_linked_shared_lib;
854 case 7: return file_magic::macho_dynamic_linker;
855 case 8: return file_magic::macho_bundle;
856 case 9: return file_magic::macho_dynamic_linker;
857 case 10: return file_magic::macho_dsym_companion;
858 }
859 break;
860 }
861 case 0xF0: // PowerPC Windows
862 case 0x83: // Alpha 32-bit
863 case 0x84: // Alpha 64-bit
864 case 0x66: // MPS R4000 Windows
865 case 0x50: // mc68K
866 case 0x4c: // 80386 Windows
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000867 if (Magic[1] == 0x01)
Michael J. Spencer5b082302011-12-13 23:17:12 +0000868 return file_magic::coff_object;
869
870 case 0x90: // PA-RISC Windows
871 case 0x68: // mc68K Windows
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000872 if (Magic[1] == 0x02)
Michael J. Spencer5b082302011-12-13 23:17:12 +0000873 return file_magic::coff_object;
874 break;
875
876 case 0x4d: // Possible MS-DOS stub on Windows PE file
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000877 if (Magic[1] == 0x5a) {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000878 uint32_t off =
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000879 *reinterpret_cast<const support::ulittle32_t*>(Magic.data() + 0x3c);
Michael J. Spencer5b082302011-12-13 23:17:12 +0000880 // PE/COFF file, either EXE or DLL.
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000881 if (off < Magic.size() && memcmp(Magic.data() + off, "PE\0\0",4) == 0)
Michael J. Spencer5b082302011-12-13 23:17:12 +0000882 return file_magic::pecoff_executable;
883 }
884 break;
885
886 case 0x64: // x86-64 Windows.
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000887 if (Magic[1] == char(0x86))
Michael J. Spencer5b082302011-12-13 23:17:12 +0000888 return file_magic::coff_object;
889 break;
890
891 default:
892 break;
893 }
894 return file_magic::unknown;
895}
896
897error_code identify_magic(const Twine &path, file_magic &result) {
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000898 SmallString<32> Magic;
899 error_code ec = get_magic(path, Magic.capacity(), Magic);
900 if (ec && ec != errc::value_too_large)
901 return ec;
902
Michael J. Spencer5b082302011-12-13 23:17:12 +0000903 result = identify_magic(Magic);
David Blaikie58604cd2012-02-09 19:24:12 +0000904 return error_code::success();
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000905}
906
Michael J. Spencer7fd682a2011-01-05 16:39:38 +0000907namespace {
908error_code remove_all_r(StringRef path, file_type ft, uint32_t &count) {
909 if (ft == file_type::directory_file) {
910 // This code would be a lot better with exceptions ;/.
911 error_code ec;
Michael J. Spencer1dd2ee72011-12-12 06:03:33 +0000912 directory_iterator i(path, ec);
913 if (ec) return ec;
914 for (directory_iterator e; i != e; i.increment(ec)) {
Michael J. Spencer7fd682a2011-01-05 16:39:38 +0000915 if (ec) return ec;
916 file_status st;
917 if (error_code ec = i->status(st)) return ec;
918 if (error_code ec = remove_all_r(i->path(), st.type(), count)) return ec;
919 }
920 bool obviously_this_exists;
921 if (error_code ec = remove(path, obviously_this_exists)) return ec;
922 assert(obviously_this_exists);
923 ++count; // Include the directory itself in the items removed.
924 } else {
925 bool obviously_this_exists;
926 if (error_code ec = remove(path, obviously_this_exists)) return ec;
927 assert(obviously_this_exists);
928 ++count;
929 }
930
David Blaikie58604cd2012-02-09 19:24:12 +0000931 return error_code::success();
Michael J. Spencer7fd682a2011-01-05 16:39:38 +0000932}
Zhanyong Wan63cc3a82011-02-11 21:24:40 +0000933} // end unnamed namespace
Michael J. Spencer7fd682a2011-01-05 16:39:38 +0000934
935error_code remove_all(const Twine &path, uint32_t &num_removed) {
936 SmallString<128> path_storage;
937 StringRef p = path.toStringRef(path_storage);
938
939 file_status fs;
940 if (error_code ec = status(path, fs))
941 return ec;
942 num_removed = 0;
943 return remove_all_r(p, fs.type(), num_removed);
944}
945
Michael J. Spencer5bcdc7f2011-01-05 16:39:13 +0000946error_code directory_entry::status(file_status &result) const {
947 return fs::status(Path, result);
948}
949
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000950} // end namespace fs
951} // end namespace sys
952} // end namespace llvm
Michael J. Spencerdffde992010-11-29 22:28:51 +0000953
954// Include the truly platform-specific parts.
955#if defined(LLVM_ON_UNIX)
956#include "Unix/PathV2.inc"
957#endif
958#if defined(LLVM_ON_WIN32)
959#include "Windows/PathV2.inc"
960#endif