blob: 54522702d1efbcf440339f10ba88c71ac2870f71 [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>
18
19namespace {
20 using llvm::StringRef;
21
22 bool is_separator(const char value) {
23 switch(value) {
24#ifdef LLVM_ON_WIN32
25 case '\\': // fall through
26#endif
27 case '/': return true;
28 default: return false;
29 }
30 }
31
32#ifdef LLVM_ON_WIN32
33 const StringRef separators = "\\/";
Michael J. Spencerca3a3392010-12-07 03:57:48 +000034 const char prefered_separator = '\\';
Michael J. Spencerdffde992010-11-29 22:28:51 +000035#else
36 const StringRef separators = "/";
37 const char prefered_separator = '/';
38#endif
39
Michael J. Spencer470ae132010-12-04 00:32:40 +000040 const llvm::error_code success;
41
Michael J. Spencerdffde992010-11-29 22:28:51 +000042 StringRef find_first_component(const StringRef &path) {
43 // Look for this first component in the following order.
44 // * empty (in this case we return an empty string)
45 // * either C: or {//,\\}net.
46 // * {/,\}
47 // * {.,..}
48 // * {file,directory}name
49
50 if (path.empty())
51 return path;
52
Michael J. Spencerca3a3392010-12-07 03:57:48 +000053#ifdef LLVM_ON_WIN32
Michael J. Spencerdffde992010-11-29 22:28:51 +000054 // C:
55 if (path.size() >= 2 && std::isalpha(path[0]) && path[1] == ':')
56 return StringRef(path.begin(), 2);
Michael J. Spencerca3a3392010-12-07 03:57:48 +000057#endif
Michael J. Spencerdffde992010-11-29 22:28:51 +000058
59 // //net
60 if ((path.size() > 2) &&
Michael J. Spencerca3a3392010-12-07 03:57:48 +000061 is_separator(path[0]) &&
62 path[0] == path[1] &&
63 !is_separator(path[2])) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000064 // Find the next directory separator.
Michael J. Spencerca3a3392010-12-07 03:57:48 +000065 size_t end = path.find_first_of(separators, 2);
Michael J. Spencerdffde992010-11-29 22:28:51 +000066 if (end == StringRef::npos)
67 return path;
68 else
69 return StringRef(path.begin(), end);
70 }
71
72 // {/,\}
Michael J. Spencerca3a3392010-12-07 03:57:48 +000073 if (is_separator(path[0]))
Michael J. Spencerdffde992010-11-29 22:28:51 +000074 return StringRef(path.begin(), 1);
75
76 if (path.startswith(".."))
77 return StringRef(path.begin(), 2);
78
79 if (path[0] == '.')
80 return StringRef(path.begin(), 1);
81
82 // * {file,directory}name
Michael J. Spencerca3a3392010-12-07 03:57:48 +000083 size_t end = path.find_first_of(separators, 2);
Michael J. Spencerdffde992010-11-29 22:28:51 +000084 if (end == StringRef::npos)
85 return path;
86 else
87 return StringRef(path.begin(), end);
88
89 return StringRef();
90 }
Michael J. Spencera42cf732010-11-30 23:28:07 +000091
92 size_t filename_pos(const StringRef &str) {
93 if (str.size() == 2 &&
94 is_separator(str[0]) &&
Michael J. Spencerca3a3392010-12-07 03:57:48 +000095 str[0] == str[1])
Michael J. Spencera42cf732010-11-30 23:28:07 +000096 return 0;
97
98 if (str.size() > 0 && is_separator(str[str.size() - 1]))
99 return str.size() - 1;
100
101 size_t pos = str.find_last_of(separators, str.size() - 1);
102
103#ifdef LLVM_ON_WIN32
104 if (pos == StringRef::npos)
105 pos = str.find_last_of(':', str.size() - 2);
106#endif
107
108 if (pos == StringRef::npos ||
109 (pos == 1 && is_separator(str[0])))
110 return 0;
111
112 return pos + 1;
113 }
114
115 size_t root_dir_start(const StringRef &str) {
116 // case "c:/"
117#ifdef LLVM_ON_WIN32
118 if (str.size() > 2 &&
119 str[1] == ':' &&
120 is_separator(str[2]))
121 return 2;
122#endif
123
124 // case "//"
125 if (str.size() == 2 &&
126 is_separator(str[0]) &&
127 str[0] == str[1])
128 return StringRef::npos;
129
130 // case "//net"
131 if (str.size() > 3 &&
132 is_separator(str[0]) &&
133 str[0] == str[1] &&
134 !is_separator(str[2])) {
135 return str.find_first_of(separators, 2);
136 }
137
138 // case "/"
139 if (str.size() > 0 && is_separator(str[0]))
140 return 0;
141
142 return StringRef::npos;
143 }
144
145 size_t parent_path_end(const StringRef &path) {
146 size_t end_pos = filename_pos(path);
147
148 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
149
150 // Skip separators except for root dir.
151 size_t root_dir_pos = root_dir_start(StringRef(path.begin(), end_pos));
152
153 while(end_pos > 0 &&
154 (end_pos - 1) != root_dir_pos &&
155 is_separator(path[end_pos - 1]))
156 --end_pos;
157
158 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
159 return StringRef::npos;
160
161 return end_pos;
162 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000163}
164
165namespace llvm {
166namespace sys {
167namespace path {
168
169const_iterator begin(const StringRef &path) {
170 const_iterator i;
171 i.Path = path;
172 i.Component = find_first_component(path);
173 i.Position = 0;
174 return i;
175}
176
177const_iterator end(const StringRef &path) {
178 const_iterator i;
179 i.Path = path;
180 i.Position = path.size();
181 return i;
182}
183
Michael J. Spencerdffde992010-11-29 22:28:51 +0000184const_iterator &const_iterator::operator++() {
185 assert(Position < Path.size() && "Tried to increment past end!");
186
187 // Increment Position to past the current component
188 Position += Component.size();
189
190 // Check for end.
191 if (Position == Path.size()) {
192 Component = StringRef();
193 return *this;
194 }
195
196 // Both POSIX and Windows treat paths that begin with exactly two separators
197 // specially.
198 bool was_net = Component.size() > 2 &&
199 is_separator(Component[0]) &&
200 Component[1] == Component[0] &&
201 !is_separator(Component[2]);
202
203 // Handle separators.
204 if (is_separator(Path[Position])) {
205 // Root dir.
206 if (was_net
207#ifdef LLVM_ON_WIN32
208 // c:/
209 || Component.endswith(":")
210#endif
211 ) {
212 Component = StringRef(Path.begin() + Position, 1);
213 return *this;
214 }
215
216 // Skip extra separators.
217 while (Position != Path.size() &&
218 is_separator(Path[Position])) {
219 ++Position;
220 }
221
222 // Treat trailing '/' as a '.'.
223 if (Position == Path.size()) {
224 --Position;
225 Component = ".";
226 return *this;
227 }
228 }
229
230 // Find next component.
231 size_t end_pos = Path.find_first_of(separators, Position);
232 if (end_pos == StringRef::npos)
233 end_pos = Path.size();
234 Component = StringRef(Path.begin() + Position, end_pos - Position);
235
236 return *this;
237}
238
Michael J. Spencera42cf732010-11-30 23:28:07 +0000239const_iterator &const_iterator::operator--() {
240 // If we're at the end and the previous char was a '/', return '.'.
241 if (Position == Path.size() &&
242 Path.size() > 1 &&
243 is_separator(Path[Position - 1])
244#ifdef LLVM_ON_WIN32
245 && Path[Position - 2] != ':'
246#endif
247 ) {
248 --Position;
249 Component = ".";
250 return *this;
251 }
252
253 // Skip separators unless it's the root directory.
254 size_t root_dir_pos = root_dir_start(Path);
255 size_t end_pos = Position;
256
257 while(end_pos > 0 &&
258 (end_pos - 1) != root_dir_pos &&
259 is_separator(Path[end_pos - 1]))
260 --end_pos;
261
262 // Find next separator.
263 size_t start_pos = filename_pos(StringRef(Path.begin(), end_pos));
264 Component = StringRef(Path.begin() + start_pos, end_pos - start_pos);
265 Position = start_pos;
266 return *this;
267}
268
Michael J. Spencerdffde992010-11-29 22:28:51 +0000269bool const_iterator::operator==(const const_iterator &RHS) const {
270 return Path.begin() == RHS.Path.begin() &&
271 Position == RHS.Position;
272}
273
274bool const_iterator::operator!=(const const_iterator &RHS) const {
275 return !(*this == RHS);
276}
277
Michael J. Spencera42cf732010-11-30 23:28:07 +0000278ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
279 return Position - RHS.Position;
280}
281
Michael J. Spencer50291592010-12-07 17:04:04 +0000282const StringRef root_path(const StringRef &path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000283 const_iterator b = begin(path),
284 pos = b,
285 e = end(path);
286 if (b != e) {
287 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
288 bool has_drive =
289#ifdef LLVM_ON_WIN32
290 b->endswith(":");
291#else
292 false;
293#endif
294
295 if (has_net || has_drive) {
296 if ((++pos != e) && is_separator((*pos)[0])) {
297 // {C:/,//net/}, so get the first two components.
Michael J. Spencer50291592010-12-07 17:04:04 +0000298 return StringRef(path.begin(), b->size() + pos->size());
Michael J. Spencerdffde992010-11-29 22:28:51 +0000299 } else {
300 // just {C:,//net}, return the first component.
Michael J. Spencer50291592010-12-07 17:04:04 +0000301 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000302 }
303 }
304
305 // POSIX style root directory.
306 if (is_separator((*b)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000307 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000308 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000309 }
310
Michael J. Spencer50291592010-12-07 17:04:04 +0000311 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000312}
313
Michael J. Spencer50291592010-12-07 17:04:04 +0000314const StringRef root_name(const StringRef &path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000315 const_iterator b = begin(path),
316 e = end(path);
317 if (b != e) {
318 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
319 bool has_drive =
320#ifdef LLVM_ON_WIN32
321 b->endswith(":");
322#else
323 false;
324#endif
325
326 if (has_net || has_drive) {
327 // just {C:,//net}, return the first component.
Michael J. Spencer50291592010-12-07 17:04:04 +0000328 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000329 }
330 }
331
332 // No path or no name.
Michael J. Spencer50291592010-12-07 17:04:04 +0000333 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000334}
335
Michael J. Spencer50291592010-12-07 17:04:04 +0000336const StringRef root_directory(const StringRef &path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000337 const_iterator b = begin(path),
338 pos = b,
339 e = end(path);
340 if (b != e) {
341 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
342 bool has_drive =
343#ifdef LLVM_ON_WIN32
344 b->endswith(":");
345#else
346 false;
347#endif
348
349 if ((has_net || has_drive) &&
350 // {C:,//net}, skip to the next component.
351 (++pos != e) && is_separator((*pos)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000352 return *pos;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000353 }
354
355 // POSIX style root directory.
356 if (!has_net && is_separator((*b)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000357 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000358 }
359 }
360
361 // No path or no root.
Michael J. Spencer50291592010-12-07 17:04:04 +0000362 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000363}
364
Michael J. Spencer50291592010-12-07 17:04:04 +0000365const StringRef relative_path(const StringRef &path) {
366 StringRef root = root_path(path);
367 return StringRef(path.begin() + root.size(), path.size() - root.size());
Michael J. Spencerdffde992010-11-29 22:28:51 +0000368}
369
Michael J. Spencer936671b2010-12-07 03:57:37 +0000370void append(SmallVectorImpl<char> &path, const Twine &a,
371 const Twine &b,
372 const Twine &c,
373 const Twine &d) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000374 SmallString<32> a_storage;
375 SmallString<32> b_storage;
376 SmallString<32> c_storage;
377 SmallString<32> d_storage;
378
379 SmallVector<StringRef, 4> components;
380 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
381 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
382 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
383 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
384
385 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
386 e = components.end();
387 i != e; ++i) {
388 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
389 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
Michael J. Spencer50291592010-12-07 17:04:04 +0000390 bool is_root_name = has_root_name(*i);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000391
392 if (path_has_sep) {
393 // Strip separators from beginning of component.
394 size_t loc = i->find_first_not_of(separators);
395 StringRef c = StringRef(i->begin() + loc, i->size() - loc);
396
397 // Append it.
398 path.append(c.begin(), c.end());
399 continue;
400 }
401
Michael J. Spencerf150e762010-12-06 04:28:23 +0000402 if (!component_has_sep && !(path.empty() || is_root_name)) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000403 // Add a separator.
404 path.push_back(prefered_separator);
405 }
406
407 path.append(i->begin(), i->end());
408 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000409}
410
Michael J. Spencer50291592010-12-07 17:04:04 +0000411const StringRef parent_path(const StringRef &path) {
Michael J. Spencera42cf732010-11-30 23:28:07 +0000412 size_t end_pos = parent_path_end(path);
413 if (end_pos == StringRef::npos)
Michael J. Spencer50291592010-12-07 17:04:04 +0000414 return StringRef();
Michael J. Spencera42cf732010-11-30 23:28:07 +0000415 else
Michael J. Spencer50291592010-12-07 17:04:04 +0000416 return StringRef(path.data(), end_pos);
Michael J. Spencera42cf732010-11-30 23:28:07 +0000417}
418
Michael J. Spencer936671b2010-12-07 03:57:37 +0000419void remove_filename(SmallVectorImpl<char> &path) {
Michael J. Spencerdbfb56b2010-12-01 00:52:28 +0000420 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
Michael J. Spencer936671b2010-12-07 03:57:37 +0000421 if (end_pos != StringRef::npos)
422 path.set_size(end_pos);
Michael J. Spencerdbfb56b2010-12-01 00:52:28 +0000423}
424
Michael J. Spencer50291592010-12-07 17:04:04 +0000425void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
Michael J. Spencer52ed8672010-12-01 00:52:55 +0000426 StringRef p(path.begin(), path.size());
427 SmallString<32> ext_storage;
428 StringRef ext = extension.toStringRef(ext_storage);
429
430 // Erase existing extension.
431 size_t pos = p.find_last_of('.');
432 if (pos != StringRef::npos && pos >= filename_pos(p))
433 path.set_size(pos);
434
435 // Append '.' if needed.
436 if (ext.size() > 0 && ext[0] != '.')
437 path.push_back('.');
438
439 // Append extension.
440 path.append(ext.begin(), ext.end());
Michael J. Spencer52ed8672010-12-01 00:52:55 +0000441}
442
Michael J. Spencer936671b2010-12-07 03:57:37 +0000443void native(const Twine &path, SmallVectorImpl<char> &result) {
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000444 // Clear result.
Michael J. Spencerfbd1bbd2010-12-07 01:23:19 +0000445 result.clear();
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000446#ifdef LLVM_ON_WIN32
447 SmallString<128> path_storage;
448 StringRef p = path.toStringRef(path_storage);
449 result.reserve(p.size());
450 for (StringRef::const_iterator i = p.begin(),
451 e = p.end();
452 i != e;
453 ++i) {
454 if (*i == '/')
455 result.push_back('\\');
456 else
457 result.push_back(*i);
458 }
459#else
460 path.toVector(result);
461#endif
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000462}
463
Michael J. Spencer50291592010-12-07 17:04:04 +0000464const StringRef filename(const StringRef &path) {
465 return *(--end(path));
Michael J. Spencera9793552010-12-01 03:18:17 +0000466}
467
Michael J. Spencer50291592010-12-07 17:04:04 +0000468const StringRef stem(const StringRef &path) {
469 StringRef fname = filename(path);
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000470 size_t pos = fname.find_last_of('.');
471 if (pos == StringRef::npos)
Michael J. Spencer50291592010-12-07 17:04:04 +0000472 return fname;
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000473 else
474 if ((fname.size() == 1 && fname == ".") ||
475 (fname.size() == 2 && fname == ".."))
Michael J. Spencer50291592010-12-07 17:04:04 +0000476 return fname;
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000477 else
Michael J. Spencer50291592010-12-07 17:04:04 +0000478 return StringRef(fname.begin(), pos);
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000479}
480
Michael J. Spencer50291592010-12-07 17:04:04 +0000481const StringRef extension(const StringRef &path) {
482 StringRef fname = filename(path);
Michael J. Spencer5265f222010-12-01 03:37:41 +0000483 size_t pos = fname.find_last_of('.');
484 if (pos == StringRef::npos)
Michael J. Spencer50291592010-12-07 17:04:04 +0000485 return StringRef();
Michael J. Spencer5265f222010-12-01 03:37:41 +0000486 else
487 if ((fname.size() == 1 && fname == ".") ||
488 (fname.size() == 2 && fname == ".."))
Michael J. Spencer50291592010-12-07 17:04:04 +0000489 return StringRef();
Michael J. Spencer5265f222010-12-01 03:37:41 +0000490 else
Michael J. Spencer50291592010-12-07 17:04:04 +0000491 return StringRef(fname.begin() + pos, fname.size() - pos);
Michael J. Spencer5265f222010-12-01 03:37:41 +0000492}
493
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000494bool has_root_name(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000495 SmallString<128> path_storage;
496 StringRef p = path.toStringRef(path_storage);
497
Michael J. Spencer50291592010-12-07 17:04:04 +0000498 return !root_name(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000499}
500
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000501bool has_root_directory(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000502 SmallString<128> path_storage;
503 StringRef p = path.toStringRef(path_storage);
504
Michael J. Spencer50291592010-12-07 17:04:04 +0000505 return !root_directory(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000506}
507
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000508bool has_root_path(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000509 SmallString<128> path_storage;
510 StringRef p = path.toStringRef(path_storage);
511
Michael J. Spencer50291592010-12-07 17:04:04 +0000512 return !root_path(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000513}
514
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000515bool has_filename(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000516 SmallString<128> path_storage;
517 StringRef p = path.toStringRef(path_storage);
518
Michael J. Spencer50291592010-12-07 17:04:04 +0000519 return !filename(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000520}
521
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000522bool has_parent_path(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000523 SmallString<128> path_storage;
524 StringRef p = path.toStringRef(path_storage);
525
Michael J. Spencer50291592010-12-07 17:04:04 +0000526 return !parent_path(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000527}
528
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000529bool has_stem(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000530 SmallString<128> path_storage;
531 StringRef p = path.toStringRef(path_storage);
532
Michael J. Spencer50291592010-12-07 17:04:04 +0000533 return !stem(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000534}
535
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000536bool has_extension(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000537 SmallString<128> path_storage;
538 StringRef p = path.toStringRef(path_storage);
539
Michael J. Spencer50291592010-12-07 17:04:04 +0000540 return !extension(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000541}
542
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000543bool is_absolute(const Twine &path) {
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000544 SmallString<128> path_storage;
545 StringRef p = path.toStringRef(path_storage);
546
Michael J. Spencer50291592010-12-07 17:04:04 +0000547 bool rootDir = has_root_directory(p),
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000548#ifdef LLVM_ON_WIN32
Michael J. Spencer50291592010-12-07 17:04:04 +0000549 rootName = has_root_name(p);
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000550#else
Michael J. Spencer50291592010-12-07 17:04:04 +0000551 rootName = true;
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000552#endif
553
Michael J. Spencer50291592010-12-07 17:04:04 +0000554 return rootDir && rootName;
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000555}
556
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000557bool is_relative(const Twine &path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000558 return !is_absolute(path);
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000559}
560
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000561} // end namespace path
562
563namespace fs {
564
Michael J. Spenceree271d82010-12-07 03:57:17 +0000565error_code make_absolute(SmallVectorImpl<char> &path) {
566 StringRef p(path.data(), path.size());
567
Michael J. Spencer50291592010-12-07 17:04:04 +0000568 bool rootName = path::has_root_name(p),
569 rootDirectory = path::has_root_directory(p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000570
571 // Already absolute.
572 if (rootName && rootDirectory)
573 return success;
574
575 // All of the following conditions will need the current directory.
576 SmallString<128> current_dir;
577 if (error_code ec = current_path(current_dir)) return ec;
578
579 // Relative path. Prepend the current directory.
580 if (!rootName && !rootDirectory) {
581 // Append path to the current directory.
Michael J. Spencer936671b2010-12-07 03:57:37 +0000582 path::append(current_dir, p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000583 // Set path to the result.
584 path.swap(current_dir);
585 return success;
586 }
587
588 if (!rootName && rootDirectory) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000589 StringRef cdrn = path::root_name(current_dir);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000590 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
Michael J. Spencer936671b2010-12-07 03:57:37 +0000591 path::append(curDirRootName, p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000592 // Set path to the result.
593 path.swap(curDirRootName);
594 return success;
595 }
596
597 if (rootName && !rootDirectory) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000598 StringRef pRootName = path::root_name(p);
599 StringRef bRootDirectory = path::root_directory(current_dir);
600 StringRef bRelativePath = path::relative_path(current_dir);
601 StringRef pRelativePath = path::relative_path(p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000602
603 SmallString<128> res;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000604 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000605 path.swap(res);
606 return success;
607 }
608
609 llvm_unreachable("All rootName and rootDirectory combinations should have "
610 "occurred above!");
611}
612
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000613error_code create_directories(const Twine &path, bool &existed) {
614 SmallString<128> path_storage;
615 StringRef p = path.toStringRef(path_storage);
616
Michael J. Spencer50291592010-12-07 17:04:04 +0000617 StringRef parent = path::parent_path(p);
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000618 bool parent_exists;
Michael J. Spencer50291592010-12-07 17:04:04 +0000619
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000620 if (error_code ec = fs::exists(parent, parent_exists)) return ec;
621
622 if (!parent_exists)
623 return create_directories(parent, existed);
624
625 return create_directory(p, existed);
626}
627
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000628bool exists(file_status status) {
629 return status_known(status) && status.type() != file_type::file_not_found;
630}
631
632bool status_known(file_status s) {
633 return s.type() != file_type::status_error;
634}
635
636bool is_directory(file_status status) {
637 return status.type() == file_type::directory_file;
638}
639
640bool is_regular_file(file_status status) {
641 return status.type() == file_type::regular_file;
642}
643
644bool is_symlink(file_status status) {
645 return status.type() == file_type::symlink_file;
646}
647
648bool is_other(file_status status) {
649 return exists(status) &&
650 !is_regular_file(status) &&
651 !is_directory(status) &&
652 !is_symlink(status);
653}
654
Michael J. Spencer753cbbb2010-12-06 04:28:42 +0000655void directory_entry::replace_filename(const Twine &filename, file_status st,
656 file_status symlink_st) {
657 SmallString<128> path(Path.begin(), Path.end());
658 path::remove_filename(path);
659 path::append(path, filename);
660 Path = path.str();
661 Status = st;
662 SymlinkStatus = symlink_st;
663}
664
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000665} // end namespace fs
666} // end namespace sys
667} // end namespace llvm
Michael J. Spencerdffde992010-11-29 22:28:51 +0000668
669// Include the truly platform-specific parts.
670#if defined(LLVM_ON_UNIX)
671#include "Unix/PathV2.inc"
672#endif
673#if defined(LLVM_ON_WIN32)
674#include "Windows/PathV2.inc"
675#endif