blob: 0c145ab4f1c521f14535aaf2f776580d30ecd2d1 [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
14#include "llvm/Support/PathV2.h"
Michael J. Spencerbee0c382010-12-01 19:32:01 +000015#include "llvm/Support/FileSystem.h"
Michael J. Spencerdffde992010-11-29 22:28:51 +000016#include "llvm/Support/ErrorHandling.h"
17#include <cctype>
Michael J. Spencer628467e2010-12-28 01:49:01 +000018#include <cstdio>
19#include <cstring>
Michael J. Spencerdffde992010-11-29 22:28:51 +000020
21namespace {
22 using llvm::StringRef;
Zhanyong Wan63cc3a82011-02-11 21:24:40 +000023 using llvm::sys::path::is_separator;
Michael J. Spencerdffde992010-11-29 22:28:51 +000024
25#ifdef LLVM_ON_WIN32
26 const StringRef separators = "\\/";
Michael J. Spencerca3a3392010-12-07 03:57:48 +000027 const char prefered_separator = '\\';
Michael J. Spencerdffde992010-11-29 22:28:51 +000028#else
29 const StringRef separators = "/";
30 const char prefered_separator = '/';
31#endif
32
Michael J. Spencer470ae132010-12-04 00:32:40 +000033 const llvm::error_code success;
34
Benjamin Kramer7fb86662010-12-17 18:59:09 +000035 StringRef find_first_component(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000036 // Look for this first component in the following order.
37 // * empty (in this case we return an empty string)
38 // * either C: or {//,\\}net.
39 // * {/,\}
40 // * {.,..}
41 // * {file,directory}name
42
43 if (path.empty())
44 return path;
45
Michael J. Spencerca3a3392010-12-07 03:57:48 +000046#ifdef LLVM_ON_WIN32
Michael J. Spencerdffde992010-11-29 22:28:51 +000047 // C:
48 if (path.size() >= 2 && std::isalpha(path[0]) && path[1] == ':')
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000049 return path.substr(0, 2);
Michael J. Spencerca3a3392010-12-07 03:57:48 +000050#endif
Michael J. Spencerdffde992010-11-29 22:28:51 +000051
52 // //net
53 if ((path.size() > 2) &&
Michael J. Spencerca3a3392010-12-07 03:57:48 +000054 is_separator(path[0]) &&
55 path[0] == path[1] &&
56 !is_separator(path[2])) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000057 // Find the next directory separator.
Michael J. Spencerca3a3392010-12-07 03:57:48 +000058 size_t end = path.find_first_of(separators, 2);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000059 return path.substr(0, end);
Michael J. Spencerdffde992010-11-29 22:28:51 +000060 }
61
62 // {/,\}
Michael J. Spencerca3a3392010-12-07 03:57:48 +000063 if (is_separator(path[0]))
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000064 return path.substr(0, 1);
Michael J. Spencerdffde992010-11-29 22:28:51 +000065
66 if (path.startswith(".."))
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000067 return path.substr(0, 2);
Michael J. Spencerdffde992010-11-29 22:28:51 +000068
69 if (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 // * {file,directory}name
Michael J. Spencerca3a3392010-12-07 03:57:48 +000073 size_t end = path.find_first_of(separators, 2);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000074 return path.substr(0, end);
Michael J. Spencerdffde992010-11-29 22:28:51 +000075 }
Michael J. Spencera42cf732010-11-30 23:28:07 +000076
Benjamin Kramerd53f3c82010-12-17 18:19:06 +000077 size_t filename_pos(StringRef str) {
Michael J. Spencera42cf732010-11-30 23:28:07 +000078 if (str.size() == 2 &&
79 is_separator(str[0]) &&
Michael J. Spencerca3a3392010-12-07 03:57:48 +000080 str[0] == str[1])
Michael J. Spencera42cf732010-11-30 23:28:07 +000081 return 0;
82
83 if (str.size() > 0 && is_separator(str[str.size() - 1]))
84 return str.size() - 1;
85
86 size_t pos = str.find_last_of(separators, str.size() - 1);
87
88#ifdef LLVM_ON_WIN32
89 if (pos == StringRef::npos)
90 pos = str.find_last_of(':', str.size() - 2);
91#endif
92
93 if (pos == StringRef::npos ||
94 (pos == 1 && is_separator(str[0])))
95 return 0;
96
97 return pos + 1;
98 }
99
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000100 size_t root_dir_start(StringRef str) {
Michael J. Spencera42cf732010-11-30 23:28:07 +0000101 // case "c:/"
102#ifdef LLVM_ON_WIN32
103 if (str.size() > 2 &&
104 str[1] == ':' &&
105 is_separator(str[2]))
106 return 2;
107#endif
108
109 // case "//"
110 if (str.size() == 2 &&
111 is_separator(str[0]) &&
112 str[0] == str[1])
113 return StringRef::npos;
114
115 // case "//net"
116 if (str.size() > 3 &&
117 is_separator(str[0]) &&
118 str[0] == str[1] &&
119 !is_separator(str[2])) {
120 return str.find_first_of(separators, 2);
121 }
122
123 // case "/"
124 if (str.size() > 0 && is_separator(str[0]))
125 return 0;
126
127 return StringRef::npos;
128 }
129
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000130 size_t parent_path_end(StringRef path) {
Michael J. Spencera42cf732010-11-30 23:28:07 +0000131 size_t end_pos = filename_pos(path);
132
133 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
134
135 // Skip separators except for root dir.
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000136 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
Michael J. Spencera42cf732010-11-30 23:28:07 +0000137
138 while(end_pos > 0 &&
139 (end_pos - 1) != root_dir_pos &&
140 is_separator(path[end_pos - 1]))
141 --end_pos;
142
143 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
144 return StringRef::npos;
145
146 return end_pos;
147 }
Zhanyong Wan63cc3a82011-02-11 21:24:40 +0000148} // end unnamed namespace
Michael J. Spencerdffde992010-11-29 22:28:51 +0000149
150namespace llvm {
151namespace sys {
152namespace path {
153
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000154const_iterator begin(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000155 const_iterator i;
156 i.Path = path;
157 i.Component = find_first_component(path);
158 i.Position = 0;
159 return i;
160}
161
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000162const_iterator end(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000163 const_iterator i;
164 i.Path = path;
165 i.Position = path.size();
166 return i;
167}
168
Michael J. Spencerdffde992010-11-29 22:28:51 +0000169const_iterator &const_iterator::operator++() {
170 assert(Position < Path.size() && "Tried to increment past end!");
171
172 // Increment Position to past the current component
173 Position += Component.size();
174
175 // Check for end.
176 if (Position == Path.size()) {
177 Component = StringRef();
178 return *this;
179 }
180
181 // Both POSIX and Windows treat paths that begin with exactly two separators
182 // specially.
183 bool was_net = Component.size() > 2 &&
184 is_separator(Component[0]) &&
185 Component[1] == Component[0] &&
186 !is_separator(Component[2]);
187
188 // Handle separators.
189 if (is_separator(Path[Position])) {
190 // Root dir.
191 if (was_net
192#ifdef LLVM_ON_WIN32
193 // c:/
194 || Component.endswith(":")
195#endif
196 ) {
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000197 Component = Path.substr(Position, 1);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000198 return *this;
199 }
200
201 // Skip extra separators.
202 while (Position != Path.size() &&
203 is_separator(Path[Position])) {
204 ++Position;
205 }
206
207 // Treat trailing '/' as a '.'.
208 if (Position == Path.size()) {
209 --Position;
210 Component = ".";
211 return *this;
212 }
213 }
214
215 // Find next component.
216 size_t end_pos = Path.find_first_of(separators, Position);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000217 Component = Path.slice(Position, end_pos);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000218
219 return *this;
220}
221
Michael J. Spencera42cf732010-11-30 23:28:07 +0000222const_iterator &const_iterator::operator--() {
223 // If we're at the end and the previous char was a '/', return '.'.
224 if (Position == Path.size() &&
225 Path.size() > 1 &&
226 is_separator(Path[Position - 1])
227#ifdef LLVM_ON_WIN32
228 && Path[Position - 2] != ':'
229#endif
230 ) {
231 --Position;
232 Component = ".";
233 return *this;
234 }
235
236 // Skip separators unless it's the root directory.
237 size_t root_dir_pos = root_dir_start(Path);
238 size_t end_pos = Position;
239
240 while(end_pos > 0 &&
241 (end_pos - 1) != root_dir_pos &&
242 is_separator(Path[end_pos - 1]))
243 --end_pos;
244
245 // Find next separator.
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000246 size_t start_pos = filename_pos(Path.substr(0, end_pos));
247 Component = Path.slice(start_pos, end_pos);
Michael J. Spencera42cf732010-11-30 23:28:07 +0000248 Position = start_pos;
249 return *this;
250}
251
Michael J. Spencerdffde992010-11-29 22:28:51 +0000252bool const_iterator::operator==(const const_iterator &RHS) const {
253 return Path.begin() == RHS.Path.begin() &&
254 Position == RHS.Position;
255}
256
257bool const_iterator::operator!=(const const_iterator &RHS) const {
258 return !(*this == RHS);
259}
260
Michael J. Spencera42cf732010-11-30 23:28:07 +0000261ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
262 return Position - RHS.Position;
263}
264
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000265const StringRef root_path(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000266 const_iterator b = begin(path),
267 pos = b,
268 e = end(path);
269 if (b != e) {
270 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
271 bool has_drive =
272#ifdef LLVM_ON_WIN32
273 b->endswith(":");
274#else
275 false;
276#endif
277
278 if (has_net || has_drive) {
279 if ((++pos != e) && is_separator((*pos)[0])) {
280 // {C:/,//net/}, so get the first two components.
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000281 return path.substr(0, b->size() + pos->size());
Michael J. Spencerdffde992010-11-29 22:28:51 +0000282 } else {
283 // just {C:,//net}, return the first component.
Michael J. Spencer50291592010-12-07 17:04:04 +0000284 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000285 }
286 }
287
288 // POSIX style root directory.
289 if (is_separator((*b)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000290 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000291 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000292 }
293
Michael J. Spencer50291592010-12-07 17:04:04 +0000294 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000295}
296
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000297const StringRef root_name(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000298 const_iterator b = begin(path),
299 e = end(path);
300 if (b != e) {
301 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
302 bool has_drive =
303#ifdef LLVM_ON_WIN32
304 b->endswith(":");
305#else
306 false;
307#endif
308
309 if (has_net || has_drive) {
310 // just {C:,//net}, return the first component.
Michael J. Spencer50291592010-12-07 17:04:04 +0000311 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000312 }
313 }
314
315 // No path or no name.
Michael J. Spencer50291592010-12-07 17:04:04 +0000316 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000317}
318
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000319const StringRef root_directory(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000320 const_iterator b = begin(path),
321 pos = b,
322 e = end(path);
323 if (b != e) {
324 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
325 bool has_drive =
326#ifdef LLVM_ON_WIN32
327 b->endswith(":");
328#else
329 false;
330#endif
331
332 if ((has_net || has_drive) &&
333 // {C:,//net}, skip to the next component.
334 (++pos != e) && is_separator((*pos)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000335 return *pos;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000336 }
337
338 // POSIX style root directory.
339 if (!has_net && is_separator((*b)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000340 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000341 }
342 }
343
344 // No path or no root.
Michael J. Spencer50291592010-12-07 17:04:04 +0000345 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000346}
347
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000348const StringRef relative_path(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000349 StringRef root = root_path(path);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000350 return root.substr(root.size());
Michael J. Spencerdffde992010-11-29 22:28:51 +0000351}
352
Michael J. Spencer936671b2010-12-07 03:57:37 +0000353void append(SmallVectorImpl<char> &path, const Twine &a,
354 const Twine &b,
355 const Twine &c,
356 const Twine &d) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000357 SmallString<32> a_storage;
358 SmallString<32> b_storage;
359 SmallString<32> c_storage;
360 SmallString<32> d_storage;
361
362 SmallVector<StringRef, 4> components;
363 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
364 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
365 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
366 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
367
368 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
369 e = components.end();
370 i != e; ++i) {
371 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
372 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
Michael J. Spencer50291592010-12-07 17:04:04 +0000373 bool is_root_name = has_root_name(*i);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000374
375 if (path_has_sep) {
376 // Strip separators from beginning of component.
377 size_t loc = i->find_first_not_of(separators);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000378 StringRef c = i->substr(loc);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000379
380 // Append it.
381 path.append(c.begin(), c.end());
382 continue;
383 }
384
Michael J. Spencerf150e762010-12-06 04:28:23 +0000385 if (!component_has_sep && !(path.empty() || is_root_name)) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000386 // Add a separator.
387 path.push_back(prefered_separator);
388 }
389
390 path.append(i->begin(), i->end());
391 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000392}
393
Argyrios Kyrtzidis6e31b9b2011-02-15 17:51:19 +0000394void append(SmallVectorImpl<char> &path,
395 const_iterator begin, const_iterator end) {
396 for (; begin != end; ++begin)
397 path::append(path, *begin);
398}
399
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000400const StringRef parent_path(StringRef path) {
Michael J. Spencera42cf732010-11-30 23:28:07 +0000401 size_t end_pos = parent_path_end(path);
402 if (end_pos == StringRef::npos)
Michael J. Spencer50291592010-12-07 17:04:04 +0000403 return StringRef();
Michael J. Spencera42cf732010-11-30 23:28:07 +0000404 else
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000405 return path.substr(0, end_pos);
Michael J. Spencera42cf732010-11-30 23:28:07 +0000406}
407
Michael J. Spencer936671b2010-12-07 03:57:37 +0000408void remove_filename(SmallVectorImpl<char> &path) {
Michael J. Spencerdbfb56b2010-12-01 00:52:28 +0000409 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
Michael J. Spencer936671b2010-12-07 03:57:37 +0000410 if (end_pos != StringRef::npos)
411 path.set_size(end_pos);
Michael J. Spencerdbfb56b2010-12-01 00:52:28 +0000412}
413
Michael J. Spencer50291592010-12-07 17:04:04 +0000414void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
Michael J. Spencer52ed8672010-12-01 00:52:55 +0000415 StringRef p(path.begin(), path.size());
416 SmallString<32> ext_storage;
417 StringRef ext = extension.toStringRef(ext_storage);
418
419 // Erase existing extension.
420 size_t pos = p.find_last_of('.');
421 if (pos != StringRef::npos && pos >= filename_pos(p))
422 path.set_size(pos);
423
424 // Append '.' if needed.
425 if (ext.size() > 0 && ext[0] != '.')
426 path.push_back('.');
427
428 // Append extension.
429 path.append(ext.begin(), ext.end());
Michael J. Spencer52ed8672010-12-01 00:52:55 +0000430}
431
Michael J. Spencer936671b2010-12-07 03:57:37 +0000432void native(const Twine &path, SmallVectorImpl<char> &result) {
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000433 // Clear result.
Michael J. Spencerfbd1bbd2010-12-07 01:23:19 +0000434 result.clear();
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000435#ifdef LLVM_ON_WIN32
436 SmallString<128> path_storage;
437 StringRef p = path.toStringRef(path_storage);
438 result.reserve(p.size());
439 for (StringRef::const_iterator i = p.begin(),
440 e = p.end();
441 i != e;
442 ++i) {
443 if (*i == '/')
444 result.push_back('\\');
445 else
446 result.push_back(*i);
447 }
448#else
449 path.toVector(result);
450#endif
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000451}
452
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000453const StringRef filename(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000454 return *(--end(path));
Michael J. Spencera9793552010-12-01 03:18:17 +0000455}
456
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000457const StringRef stem(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000458 StringRef fname = filename(path);
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000459 size_t pos = fname.find_last_of('.');
460 if (pos == StringRef::npos)
Michael J. Spencer50291592010-12-07 17:04:04 +0000461 return fname;
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000462 else
463 if ((fname.size() == 1 && fname == ".") ||
464 (fname.size() == 2 && fname == ".."))
Michael J. Spencer50291592010-12-07 17:04:04 +0000465 return fname;
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000466 else
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000467 return fname.substr(0, pos);
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000468}
469
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000470const StringRef extension(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000471 StringRef fname = filename(path);
Michael J. Spencer5265f222010-12-01 03:37:41 +0000472 size_t pos = fname.find_last_of('.');
473 if (pos == StringRef::npos)
Michael J. Spencer50291592010-12-07 17:04:04 +0000474 return StringRef();
Michael J. Spencer5265f222010-12-01 03:37:41 +0000475 else
476 if ((fname.size() == 1 && fname == ".") ||
477 (fname.size() == 2 && fname == ".."))
Michael J. Spencer50291592010-12-07 17:04:04 +0000478 return StringRef();
Michael J. Spencer5265f222010-12-01 03:37:41 +0000479 else
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000480 return fname.substr(pos);
Michael J. Spencer5265f222010-12-01 03:37:41 +0000481}
482
Zhanyong Wan63cc3a82011-02-11 21:24:40 +0000483bool is_separator(char value) {
484 switch(value) {
485#ifdef LLVM_ON_WIN32
486 case '\\': // fall through
487#endif
488 case '/': return true;
489 default: return false;
490 }
491}
492
Douglas Gregor55cf8152011-09-14 20:27:01 +0000493void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result) {
494 result.clear();
495
496 // Check whether the temporary directory is specified by an environment
497 // variable.
498 const char *EnvironmentVariable;
499#ifdef LLVM_ON_WIN32
500 EnvironmentVariable = "TEMP";
501#else
502 EnvironmentVariable = "TMPDIR";
503#endif
504 if (char *RequestedDir = getenv(EnvironmentVariable)) {
505 result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
506 return;
507 }
508
509 // Fall back to a system default.
510 const char *DefaultResult;
511#ifdef LLVM_ON_WIN32
512 (void)erasedOnReboot;
Douglas Gregord26d6b62011-09-14 23:21:47 +0000513 DefaultResult = "C:\\TEMP";
Douglas Gregor55cf8152011-09-14 20:27:01 +0000514#else
515 if (erasedOnReboot)
516 DefaultResult = "/tmp";
517 else
518 DefaultResult = "/var/tmp";
519#endif
520 result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
521}
522
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000523bool has_root_name(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000524 SmallString<128> path_storage;
525 StringRef p = path.toStringRef(path_storage);
526
Michael J. Spencer50291592010-12-07 17:04:04 +0000527 return !root_name(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000528}
529
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000530bool has_root_directory(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000531 SmallString<128> path_storage;
532 StringRef p = path.toStringRef(path_storage);
533
Michael J. Spencer50291592010-12-07 17:04:04 +0000534 return !root_directory(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000535}
536
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000537bool has_root_path(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000538 SmallString<128> path_storage;
539 StringRef p = path.toStringRef(path_storage);
540
Michael J. Spencer50291592010-12-07 17:04:04 +0000541 return !root_path(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000542}
543
Michael J. Spencer2d484eb2010-12-20 13:30:28 +0000544bool has_relative_path(const Twine &path) {
545 SmallString<128> path_storage;
546 StringRef p = path.toStringRef(path_storage);
547
548 return !relative_path(p).empty();
549}
550
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000551bool has_filename(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000552 SmallString<128> path_storage;
553 StringRef p = path.toStringRef(path_storage);
554
Michael J. Spencer50291592010-12-07 17:04:04 +0000555 return !filename(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000556}
557
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000558bool has_parent_path(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000559 SmallString<128> path_storage;
560 StringRef p = path.toStringRef(path_storage);
561
Michael J. Spencer50291592010-12-07 17:04:04 +0000562 return !parent_path(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000563}
564
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000565bool has_stem(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000566 SmallString<128> path_storage;
567 StringRef p = path.toStringRef(path_storage);
568
Michael J. Spencer50291592010-12-07 17:04:04 +0000569 return !stem(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000570}
571
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000572bool has_extension(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000573 SmallString<128> path_storage;
574 StringRef p = path.toStringRef(path_storage);
575
Michael J. Spencer50291592010-12-07 17:04:04 +0000576 return !extension(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000577}
578
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000579bool is_absolute(const Twine &path) {
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000580 SmallString<128> path_storage;
581 StringRef p = path.toStringRef(path_storage);
582
Michael J. Spencer50291592010-12-07 17:04:04 +0000583 bool rootDir = has_root_directory(p),
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000584#ifdef LLVM_ON_WIN32
Michael J. Spencer50291592010-12-07 17:04:04 +0000585 rootName = has_root_name(p);
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000586#else
Michael J. Spencer50291592010-12-07 17:04:04 +0000587 rootName = true;
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000588#endif
589
Michael J. Spencer50291592010-12-07 17:04:04 +0000590 return rootDir && rootName;
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000591}
592
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000593bool is_relative(const Twine &path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000594 return !is_absolute(path);
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000595}
596
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000597} // end namespace path
598
599namespace fs {
600
Michael J. Spenceree271d82010-12-07 03:57:17 +0000601error_code make_absolute(SmallVectorImpl<char> &path) {
602 StringRef p(path.data(), path.size());
603
Michael J. Spencer50291592010-12-07 17:04:04 +0000604 bool rootName = path::has_root_name(p),
605 rootDirectory = path::has_root_directory(p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000606
607 // Already absolute.
608 if (rootName && rootDirectory)
609 return success;
610
611 // All of the following conditions will need the current directory.
612 SmallString<128> current_dir;
613 if (error_code ec = current_path(current_dir)) return ec;
614
615 // Relative path. Prepend the current directory.
616 if (!rootName && !rootDirectory) {
617 // Append path to the current directory.
Michael J. Spencer936671b2010-12-07 03:57:37 +0000618 path::append(current_dir, p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000619 // Set path to the result.
620 path.swap(current_dir);
621 return success;
622 }
623
624 if (!rootName && rootDirectory) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000625 StringRef cdrn = path::root_name(current_dir);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000626 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
Michael J. Spencer936671b2010-12-07 03:57:37 +0000627 path::append(curDirRootName, p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000628 // Set path to the result.
629 path.swap(curDirRootName);
630 return success;
631 }
632
633 if (rootName && !rootDirectory) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000634 StringRef pRootName = path::root_name(p);
635 StringRef bRootDirectory = path::root_directory(current_dir);
636 StringRef bRelativePath = path::relative_path(current_dir);
637 StringRef pRelativePath = path::relative_path(p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000638
639 SmallString<128> res;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000640 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000641 path.swap(res);
642 return success;
643 }
644
645 llvm_unreachable("All rootName and rootDirectory combinations should have "
646 "occurred above!");
647}
648
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000649error_code create_directories(const Twine &path, bool &existed) {
650 SmallString<128> path_storage;
651 StringRef p = path.toStringRef(path_storage);
652
Michael J. Spencer50291592010-12-07 17:04:04 +0000653 StringRef parent = path::parent_path(p);
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000654 bool parent_exists;
Michael J. Spencer50291592010-12-07 17:04:04 +0000655
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000656 if (error_code ec = fs::exists(parent, parent_exists)) return ec;
657
658 if (!parent_exists)
Eli Friedmanecc9b5e2011-09-16 18:36:31 +0000659 if (error_code ec = create_directories(parent, existed)) return ec;
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000660
661 return create_directory(p, existed);
662}
663
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000664bool exists(file_status status) {
665 return status_known(status) && status.type() != file_type::file_not_found;
666}
667
668bool status_known(file_status s) {
669 return s.type() != file_type::status_error;
670}
671
672bool is_directory(file_status status) {
673 return status.type() == file_type::directory_file;
674}
675
Michael J. Spencer218dc3e2011-01-11 01:21:55 +0000676error_code is_directory(const Twine &path, bool &result) {
677 file_status st;
678 if (error_code ec = status(path, st))
679 return ec;
680 result = is_directory(st);
681 return success;
682}
683
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000684bool is_regular_file(file_status status) {
685 return status.type() == file_type::regular_file;
686}
687
Michael J. Spencer218dc3e2011-01-11 01:21:55 +0000688error_code is_regular_file(const Twine &path, bool &result) {
689 file_status st;
690 if (error_code ec = status(path, st))
691 return ec;
692 result = is_regular_file(st);
693 return success;
694}
695
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000696bool is_symlink(file_status status) {
697 return status.type() == file_type::symlink_file;
698}
699
Michael J. Spencer9df536c2011-01-12 23:55:06 +0000700error_code is_symlink(const Twine &path, bool &result) {
701 file_status st;
702 if (error_code ec = status(path, st))
703 return ec;
704 result = is_symlink(st);
705 return success;
706}
707
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000708bool is_other(file_status status) {
709 return exists(status) &&
710 !is_regular_file(status) &&
711 !is_directory(status) &&
712 !is_symlink(status);
713}
714
Benjamin Kramer357b5712011-09-14 01:14:36 +0000715void directory_entry::replace_filename(const Twine &filename, file_status st) {
Michael J. Spencer753cbbb2010-12-06 04:28:42 +0000716 SmallString<128> path(Path.begin(), Path.end());
717 path::remove_filename(path);
718 path::append(path, filename);
719 Path = path.str();
720 Status = st;
Michael J. Spencer753cbbb2010-12-06 04:28:42 +0000721}
722
Michael J. Spencer628467e2010-12-28 01:49:01 +0000723error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
Michael J. Spencer628467e2010-12-28 01:49:01 +0000724 SmallString<32> MagicStorage;
Michael J. Spencerb33594b2011-01-15 18:52:41 +0000725 StringRef Magic = magic.toStringRef(MagicStorage);
726 SmallString<32> Buffer;
Michael J. Spencer628467e2010-12-28 01:49:01 +0000727
Michael J. Spencerb33594b2011-01-15 18:52:41 +0000728 if (error_code ec = get_magic(path, Magic.size(), Buffer)) {
729 if (ec == errc::value_too_large) {
730 // Magic.size() > file_size(Path).
Michael J. Spencer628467e2010-12-28 01:49:01 +0000731 result = false;
732 return success;
733 }
Michael J. Spencerb33594b2011-01-15 18:52:41 +0000734 return ec;
Michael J. Spencer628467e2010-12-28 01:49:01 +0000735 }
Michael J. Spencer628467e2010-12-28 01:49:01 +0000736
Michael J. Spencerb33594b2011-01-15 18:52:41 +0000737 result = Magic == Buffer;
Michael J. Spencer628467e2010-12-28 01:49:01 +0000738 return success;
739}
740
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000741error_code identify_magic(const Twine &path, LLVMFileType &result) {
742 SmallString<32> Magic;
743 error_code ec = get_magic(path, Magic.capacity(), Magic);
744 if (ec && ec != errc::value_too_large)
745 return ec;
746
747 result = IdentifyFileType(Magic.data(), Magic.size());
748 return success;
749}
750
Michael J. Spencer7fd682a2011-01-05 16:39:38 +0000751namespace {
752error_code remove_all_r(StringRef path, file_type ft, uint32_t &count) {
753 if (ft == file_type::directory_file) {
754 // This code would be a lot better with exceptions ;/.
755 error_code ec;
Michael J. Spencer1dd2ee72011-12-12 06:03:33 +0000756 directory_iterator i(path, ec);
757 if (ec) return ec;
758 for (directory_iterator e; i != e; i.increment(ec)) {
Michael J. Spencer7fd682a2011-01-05 16:39:38 +0000759 if (ec) return ec;
760 file_status st;
761 if (error_code ec = i->status(st)) return ec;
762 if (error_code ec = remove_all_r(i->path(), st.type(), count)) return ec;
763 }
764 bool obviously_this_exists;
765 if (error_code ec = remove(path, obviously_this_exists)) return ec;
766 assert(obviously_this_exists);
767 ++count; // Include the directory itself in the items removed.
768 } else {
769 bool obviously_this_exists;
770 if (error_code ec = remove(path, obviously_this_exists)) return ec;
771 assert(obviously_this_exists);
772 ++count;
773 }
774
775 return success;
776}
Zhanyong Wan63cc3a82011-02-11 21:24:40 +0000777} // end unnamed namespace
Michael J. Spencer7fd682a2011-01-05 16:39:38 +0000778
779error_code remove_all(const Twine &path, uint32_t &num_removed) {
780 SmallString<128> path_storage;
781 StringRef p = path.toStringRef(path_storage);
782
783 file_status fs;
784 if (error_code ec = status(path, fs))
785 return ec;
786 num_removed = 0;
787 return remove_all_r(p, fs.type(), num_removed);
788}
789
Michael J. Spencer5bcdc7f2011-01-05 16:39:13 +0000790error_code directory_entry::status(file_status &result) const {
791 return fs::status(Path, result);
792}
793
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000794} // end namespace fs
795} // end namespace sys
796} // end namespace llvm
Michael J. Spencerdffde992010-11-29 22:28:51 +0000797
798// Include the truly platform-specific parts.
799#if defined(LLVM_ON_UNIX)
800#include "Unix/PathV2.inc"
801#endif
802#if defined(LLVM_ON_WIN32)
803#include "Windows/PathV2.inc"
804#endif