blob: 4cfb650a6cc8984c96e78450c7f0eaf40486ea94 [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;
23
24 bool is_separator(const char value) {
25 switch(value) {
26#ifdef LLVM_ON_WIN32
27 case '\\': // fall through
28#endif
29 case '/': return true;
30 default: return false;
31 }
32 }
33
34#ifdef LLVM_ON_WIN32
35 const StringRef separators = "\\/";
Michael J. Spencerca3a3392010-12-07 03:57:48 +000036 const char prefered_separator = '\\';
Michael J. Spencerdffde992010-11-29 22:28:51 +000037#else
38 const StringRef separators = "/";
39 const char prefered_separator = '/';
40#endif
41
Michael J. Spencer470ae132010-12-04 00:32:40 +000042 const llvm::error_code success;
43
Benjamin Kramer7fb86662010-12-17 18:59:09 +000044 StringRef find_first_component(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000045 // Look for this first component in the following order.
46 // * empty (in this case we return an empty string)
47 // * either C: or {//,\\}net.
48 // * {/,\}
49 // * {.,..}
50 // * {file,directory}name
51
52 if (path.empty())
53 return path;
54
Michael J. Spencerca3a3392010-12-07 03:57:48 +000055#ifdef LLVM_ON_WIN32
Michael J. Spencerdffde992010-11-29 22:28:51 +000056 // C:
57 if (path.size() >= 2 && std::isalpha(path[0]) && path[1] == ':')
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000058 return path.substr(0, 2);
Michael J. Spencerca3a3392010-12-07 03:57:48 +000059#endif
Michael J. Spencerdffde992010-11-29 22:28:51 +000060
61 // //net
62 if ((path.size() > 2) &&
Michael J. Spencerca3a3392010-12-07 03:57:48 +000063 is_separator(path[0]) &&
64 path[0] == path[1] &&
65 !is_separator(path[2])) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000066 // Find the next directory separator.
Michael J. Spencerca3a3392010-12-07 03:57:48 +000067 size_t end = path.find_first_of(separators, 2);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000068 return path.substr(0, end);
Michael J. Spencerdffde992010-11-29 22:28:51 +000069 }
70
71 // {/,\}
Michael J. Spencerca3a3392010-12-07 03:57:48 +000072 if (is_separator(path[0]))
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000073 return path.substr(0, 1);
Michael J. Spencerdffde992010-11-29 22:28:51 +000074
75 if (path.startswith(".."))
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000076 return path.substr(0, 2);
Michael J. Spencerdffde992010-11-29 22:28:51 +000077
78 if (path[0] == '.')
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000079 return path.substr(0, 1);
Michael J. Spencerdffde992010-11-29 22:28:51 +000080
81 // * {file,directory}name
Michael J. Spencerca3a3392010-12-07 03:57:48 +000082 size_t end = path.find_first_of(separators, 2);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000083 return path.substr(0, end);
Michael J. Spencerdffde992010-11-29 22:28:51 +000084 }
Michael J. Spencera42cf732010-11-30 23:28:07 +000085
Benjamin Kramerd53f3c82010-12-17 18:19:06 +000086 size_t filename_pos(StringRef str) {
Michael J. Spencera42cf732010-11-30 23:28:07 +000087 if (str.size() == 2 &&
88 is_separator(str[0]) &&
Michael J. Spencerca3a3392010-12-07 03:57:48 +000089 str[0] == str[1])
Michael J. Spencera42cf732010-11-30 23:28:07 +000090 return 0;
91
92 if (str.size() > 0 && is_separator(str[str.size() - 1]))
93 return str.size() - 1;
94
95 size_t pos = str.find_last_of(separators, str.size() - 1);
96
97#ifdef LLVM_ON_WIN32
98 if (pos == StringRef::npos)
99 pos = str.find_last_of(':', str.size() - 2);
100#endif
101
102 if (pos == StringRef::npos ||
103 (pos == 1 && is_separator(str[0])))
104 return 0;
105
106 return pos + 1;
107 }
108
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000109 size_t root_dir_start(StringRef str) {
Michael J. Spencera42cf732010-11-30 23:28:07 +0000110 // case "c:/"
111#ifdef LLVM_ON_WIN32
112 if (str.size() > 2 &&
113 str[1] == ':' &&
114 is_separator(str[2]))
115 return 2;
116#endif
117
118 // case "//"
119 if (str.size() == 2 &&
120 is_separator(str[0]) &&
121 str[0] == str[1])
122 return StringRef::npos;
123
124 // case "//net"
125 if (str.size() > 3 &&
126 is_separator(str[0]) &&
127 str[0] == str[1] &&
128 !is_separator(str[2])) {
129 return str.find_first_of(separators, 2);
130 }
131
132 // case "/"
133 if (str.size() > 0 && is_separator(str[0]))
134 return 0;
135
136 return StringRef::npos;
137 }
138
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000139 size_t parent_path_end(StringRef path) {
Michael J. Spencera42cf732010-11-30 23:28:07 +0000140 size_t end_pos = filename_pos(path);
141
142 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
143
144 // Skip separators except for root dir.
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000145 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
Michael J. Spencera42cf732010-11-30 23:28:07 +0000146
147 while(end_pos > 0 &&
148 (end_pos - 1) != root_dir_pos &&
149 is_separator(path[end_pos - 1]))
150 --end_pos;
151
152 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
153 return StringRef::npos;
154
155 return end_pos;
156 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000157}
158
159namespace llvm {
160namespace sys {
161namespace path {
162
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000163const_iterator begin(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000164 const_iterator i;
165 i.Path = path;
166 i.Component = find_first_component(path);
167 i.Position = 0;
168 return i;
169}
170
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000171const_iterator end(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000172 const_iterator i;
173 i.Path = path;
174 i.Position = path.size();
175 return i;
176}
177
Michael J. Spencerdffde992010-11-29 22:28:51 +0000178const_iterator &const_iterator::operator++() {
179 assert(Position < Path.size() && "Tried to increment past end!");
180
181 // Increment Position to past the current component
182 Position += Component.size();
183
184 // Check for end.
185 if (Position == Path.size()) {
186 Component = StringRef();
187 return *this;
188 }
189
190 // Both POSIX and Windows treat paths that begin with exactly two separators
191 // specially.
192 bool was_net = Component.size() > 2 &&
193 is_separator(Component[0]) &&
194 Component[1] == Component[0] &&
195 !is_separator(Component[2]);
196
197 // Handle separators.
198 if (is_separator(Path[Position])) {
199 // Root dir.
200 if (was_net
201#ifdef LLVM_ON_WIN32
202 // c:/
203 || Component.endswith(":")
204#endif
205 ) {
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000206 Component = Path.substr(Position, 1);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000207 return *this;
208 }
209
210 // Skip extra separators.
211 while (Position != Path.size() &&
212 is_separator(Path[Position])) {
213 ++Position;
214 }
215
216 // Treat trailing '/' as a '.'.
217 if (Position == Path.size()) {
218 --Position;
219 Component = ".";
220 return *this;
221 }
222 }
223
224 // Find next component.
225 size_t end_pos = Path.find_first_of(separators, Position);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000226 Component = Path.slice(Position, end_pos);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000227
228 return *this;
229}
230
Michael J. Spencera42cf732010-11-30 23:28:07 +0000231const_iterator &const_iterator::operator--() {
232 // If we're at the end and the previous char was a '/', return '.'.
233 if (Position == Path.size() &&
234 Path.size() > 1 &&
235 is_separator(Path[Position - 1])
236#ifdef LLVM_ON_WIN32
237 && Path[Position - 2] != ':'
238#endif
239 ) {
240 --Position;
241 Component = ".";
242 return *this;
243 }
244
245 // Skip separators unless it's the root directory.
246 size_t root_dir_pos = root_dir_start(Path);
247 size_t end_pos = Position;
248
249 while(end_pos > 0 &&
250 (end_pos - 1) != root_dir_pos &&
251 is_separator(Path[end_pos - 1]))
252 --end_pos;
253
254 // Find next separator.
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000255 size_t start_pos = filename_pos(Path.substr(0, end_pos));
256 Component = Path.slice(start_pos, end_pos);
Michael J. Spencera42cf732010-11-30 23:28:07 +0000257 Position = start_pos;
258 return *this;
259}
260
Michael J. Spencerdffde992010-11-29 22:28:51 +0000261bool const_iterator::operator==(const const_iterator &RHS) const {
262 return Path.begin() == RHS.Path.begin() &&
263 Position == RHS.Position;
264}
265
266bool const_iterator::operator!=(const const_iterator &RHS) const {
267 return !(*this == RHS);
268}
269
Michael J. Spencera42cf732010-11-30 23:28:07 +0000270ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
271 return Position - RHS.Position;
272}
273
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000274const StringRef root_path(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000275 const_iterator b = begin(path),
276 pos = b,
277 e = end(path);
278 if (b != e) {
279 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
280 bool has_drive =
281#ifdef LLVM_ON_WIN32
282 b->endswith(":");
283#else
284 false;
285#endif
286
287 if (has_net || has_drive) {
288 if ((++pos != e) && is_separator((*pos)[0])) {
289 // {C:/,//net/}, so get the first two components.
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000290 return path.substr(0, b->size() + pos->size());
Michael J. Spencerdffde992010-11-29 22:28:51 +0000291 } else {
292 // just {C:,//net}, return the first component.
Michael J. Spencer50291592010-12-07 17:04:04 +0000293 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000294 }
295 }
296
297 // POSIX style root directory.
298 if (is_separator((*b)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000299 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000300 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000301 }
302
Michael J. Spencer50291592010-12-07 17:04:04 +0000303 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000304}
305
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000306const StringRef root_name(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000307 const_iterator b = begin(path),
308 e = end(path);
309 if (b != e) {
310 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
311 bool has_drive =
312#ifdef LLVM_ON_WIN32
313 b->endswith(":");
314#else
315 false;
316#endif
317
318 if (has_net || has_drive) {
319 // just {C:,//net}, return the first component.
Michael J. Spencer50291592010-12-07 17:04:04 +0000320 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000321 }
322 }
323
324 // No path or no name.
Michael J. Spencer50291592010-12-07 17:04:04 +0000325 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000326}
327
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000328const StringRef root_directory(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000329 const_iterator b = begin(path),
330 pos = b,
331 e = end(path);
332 if (b != e) {
333 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
334 bool has_drive =
335#ifdef LLVM_ON_WIN32
336 b->endswith(":");
337#else
338 false;
339#endif
340
341 if ((has_net || has_drive) &&
342 // {C:,//net}, skip to the next component.
343 (++pos != e) && is_separator((*pos)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000344 return *pos;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000345 }
346
347 // POSIX style root directory.
348 if (!has_net && is_separator((*b)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000349 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000350 }
351 }
352
353 // No path or no root.
Michael J. Spencer50291592010-12-07 17:04:04 +0000354 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000355}
356
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000357const StringRef relative_path(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000358 StringRef root = root_path(path);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000359 return root.substr(root.size());
Michael J. Spencerdffde992010-11-29 22:28:51 +0000360}
361
Michael J. Spencer936671b2010-12-07 03:57:37 +0000362void append(SmallVectorImpl<char> &path, const Twine &a,
363 const Twine &b,
364 const Twine &c,
365 const Twine &d) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000366 SmallString<32> a_storage;
367 SmallString<32> b_storage;
368 SmallString<32> c_storage;
369 SmallString<32> d_storage;
370
371 SmallVector<StringRef, 4> components;
372 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
373 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
374 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
375 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
376
377 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
378 e = components.end();
379 i != e; ++i) {
380 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
381 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
Michael J. Spencer50291592010-12-07 17:04:04 +0000382 bool is_root_name = has_root_name(*i);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000383
384 if (path_has_sep) {
385 // Strip separators from beginning of component.
386 size_t loc = i->find_first_not_of(separators);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000387 StringRef c = i->substr(loc);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000388
389 // Append it.
390 path.append(c.begin(), c.end());
391 continue;
392 }
393
Michael J. Spencerf150e762010-12-06 04:28:23 +0000394 if (!component_has_sep && !(path.empty() || is_root_name)) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000395 // Add a separator.
396 path.push_back(prefered_separator);
397 }
398
399 path.append(i->begin(), i->end());
400 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000401}
402
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000403const StringRef parent_path(StringRef path) {
Michael J. Spencera42cf732010-11-30 23:28:07 +0000404 size_t end_pos = parent_path_end(path);
405 if (end_pos == StringRef::npos)
Michael J. Spencer50291592010-12-07 17:04:04 +0000406 return StringRef();
Michael J. Spencera42cf732010-11-30 23:28:07 +0000407 else
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000408 return path.substr(0, end_pos);
Michael J. Spencera42cf732010-11-30 23:28:07 +0000409}
410
Michael J. Spencer936671b2010-12-07 03:57:37 +0000411void remove_filename(SmallVectorImpl<char> &path) {
Michael J. Spencerdbfb56b2010-12-01 00:52:28 +0000412 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
Michael J. Spencer936671b2010-12-07 03:57:37 +0000413 if (end_pos != StringRef::npos)
414 path.set_size(end_pos);
Michael J. Spencerdbfb56b2010-12-01 00:52:28 +0000415}
416
Michael J. Spencer50291592010-12-07 17:04:04 +0000417void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
Michael J. Spencer52ed8672010-12-01 00:52:55 +0000418 StringRef p(path.begin(), path.size());
419 SmallString<32> ext_storage;
420 StringRef ext = extension.toStringRef(ext_storage);
421
422 // Erase existing extension.
423 size_t pos = p.find_last_of('.');
424 if (pos != StringRef::npos && pos >= filename_pos(p))
425 path.set_size(pos);
426
427 // Append '.' if needed.
428 if (ext.size() > 0 && ext[0] != '.')
429 path.push_back('.');
430
431 // Append extension.
432 path.append(ext.begin(), ext.end());
Michael J. Spencer52ed8672010-12-01 00:52:55 +0000433}
434
Michael J. Spencer936671b2010-12-07 03:57:37 +0000435void native(const Twine &path, SmallVectorImpl<char> &result) {
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000436 // Clear result.
Michael J. Spencerfbd1bbd2010-12-07 01:23:19 +0000437 result.clear();
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000438#ifdef LLVM_ON_WIN32
439 SmallString<128> path_storage;
440 StringRef p = path.toStringRef(path_storage);
441 result.reserve(p.size());
442 for (StringRef::const_iterator i = p.begin(),
443 e = p.end();
444 i != e;
445 ++i) {
446 if (*i == '/')
447 result.push_back('\\');
448 else
449 result.push_back(*i);
450 }
451#else
452 path.toVector(result);
453#endif
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000454}
455
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000456const StringRef filename(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000457 return *(--end(path));
Michael J. Spencera9793552010-12-01 03:18:17 +0000458}
459
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000460const StringRef stem(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000461 StringRef fname = filename(path);
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000462 size_t pos = fname.find_last_of('.');
463 if (pos == StringRef::npos)
Michael J. Spencer50291592010-12-07 17:04:04 +0000464 return fname;
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000465 else
466 if ((fname.size() == 1 && fname == ".") ||
467 (fname.size() == 2 && fname == ".."))
Michael J. Spencer50291592010-12-07 17:04:04 +0000468 return fname;
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000469 else
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000470 return fname.substr(0, pos);
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000471}
472
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000473const StringRef extension(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000474 StringRef fname = filename(path);
Michael J. Spencer5265f222010-12-01 03:37:41 +0000475 size_t pos = fname.find_last_of('.');
476 if (pos == StringRef::npos)
Michael J. Spencer50291592010-12-07 17:04:04 +0000477 return StringRef();
Michael J. Spencer5265f222010-12-01 03:37:41 +0000478 else
479 if ((fname.size() == 1 && fname == ".") ||
480 (fname.size() == 2 && fname == ".."))
Michael J. Spencer50291592010-12-07 17:04:04 +0000481 return StringRef();
Michael J. Spencer5265f222010-12-01 03:37:41 +0000482 else
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000483 return fname.substr(pos);
Michael J. Spencer5265f222010-12-01 03:37:41 +0000484}
485
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000486bool has_root_name(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000487 SmallString<128> path_storage;
488 StringRef p = path.toStringRef(path_storage);
489
Michael J. Spencer50291592010-12-07 17:04:04 +0000490 return !root_name(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000491}
492
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000493bool has_root_directory(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000494 SmallString<128> path_storage;
495 StringRef p = path.toStringRef(path_storage);
496
Michael J. Spencer50291592010-12-07 17:04:04 +0000497 return !root_directory(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000498}
499
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000500bool has_root_path(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000501 SmallString<128> path_storage;
502 StringRef p = path.toStringRef(path_storage);
503
Michael J. Spencer50291592010-12-07 17:04:04 +0000504 return !root_path(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000505}
506
Michael J. Spencer2d484eb2010-12-20 13:30:28 +0000507bool has_relative_path(const Twine &path) {
508 SmallString<128> path_storage;
509 StringRef p = path.toStringRef(path_storage);
510
511 return !relative_path(p).empty();
512}
513
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000514bool has_filename(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000515 SmallString<128> path_storage;
516 StringRef p = path.toStringRef(path_storage);
517
Michael J. Spencer50291592010-12-07 17:04:04 +0000518 return !filename(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000519}
520
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000521bool has_parent_path(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000522 SmallString<128> path_storage;
523 StringRef p = path.toStringRef(path_storage);
524
Michael J. Spencer50291592010-12-07 17:04:04 +0000525 return !parent_path(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000526}
527
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000528bool has_stem(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000529 SmallString<128> path_storage;
530 StringRef p = path.toStringRef(path_storage);
531
Michael J. Spencer50291592010-12-07 17:04:04 +0000532 return !stem(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000533}
534
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000535bool has_extension(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000536 SmallString<128> path_storage;
537 StringRef p = path.toStringRef(path_storage);
538
Michael J. Spencer50291592010-12-07 17:04:04 +0000539 return !extension(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000540}
541
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000542bool is_absolute(const Twine &path) {
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000543 SmallString<128> path_storage;
544 StringRef p = path.toStringRef(path_storage);
545
Michael J. Spencer50291592010-12-07 17:04:04 +0000546 bool rootDir = has_root_directory(p),
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000547#ifdef LLVM_ON_WIN32
Michael J. Spencer50291592010-12-07 17:04:04 +0000548 rootName = has_root_name(p);
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000549#else
Michael J. Spencer50291592010-12-07 17:04:04 +0000550 rootName = true;
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000551#endif
552
Michael J. Spencer50291592010-12-07 17:04:04 +0000553 return rootDir && rootName;
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000554}
555
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000556bool is_relative(const Twine &path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000557 return !is_absolute(path);
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000558}
559
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000560} // end namespace path
561
562namespace fs {
563
Michael J. Spenceree271d82010-12-07 03:57:17 +0000564error_code make_absolute(SmallVectorImpl<char> &path) {
565 StringRef p(path.data(), path.size());
566
Michael J. Spencer50291592010-12-07 17:04:04 +0000567 bool rootName = path::has_root_name(p),
568 rootDirectory = path::has_root_directory(p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000569
570 // Already absolute.
571 if (rootName && rootDirectory)
572 return success;
573
574 // All of the following conditions will need the current directory.
575 SmallString<128> current_dir;
576 if (error_code ec = current_path(current_dir)) return ec;
577
578 // Relative path. Prepend the current directory.
579 if (!rootName && !rootDirectory) {
580 // Append path to the current directory.
Michael J. Spencer936671b2010-12-07 03:57:37 +0000581 path::append(current_dir, p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000582 // Set path to the result.
583 path.swap(current_dir);
584 return success;
585 }
586
587 if (!rootName && rootDirectory) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000588 StringRef cdrn = path::root_name(current_dir);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000589 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
Michael J. Spencer936671b2010-12-07 03:57:37 +0000590 path::append(curDirRootName, p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000591 // Set path to the result.
592 path.swap(curDirRootName);
593 return success;
594 }
595
596 if (rootName && !rootDirectory) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000597 StringRef pRootName = path::root_name(p);
598 StringRef bRootDirectory = path::root_directory(current_dir);
599 StringRef bRelativePath = path::relative_path(current_dir);
600 StringRef pRelativePath = path::relative_path(p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000601
602 SmallString<128> res;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000603 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000604 path.swap(res);
605 return success;
606 }
607
608 llvm_unreachable("All rootName and rootDirectory combinations should have "
609 "occurred above!");
610}
611
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000612error_code create_directories(const Twine &path, bool &existed) {
613 SmallString<128> path_storage;
614 StringRef p = path.toStringRef(path_storage);
615
Michael J. Spencer50291592010-12-07 17:04:04 +0000616 StringRef parent = path::parent_path(p);
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000617 bool parent_exists;
Michael J. Spencer50291592010-12-07 17:04:04 +0000618
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000619 if (error_code ec = fs::exists(parent, parent_exists)) return ec;
620
621 if (!parent_exists)
622 return create_directories(parent, existed);
623
624 return create_directory(p, existed);
625}
626
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000627bool exists(file_status status) {
628 return status_known(status) && status.type() != file_type::file_not_found;
629}
630
631bool status_known(file_status s) {
632 return s.type() != file_type::status_error;
633}
634
635bool is_directory(file_status status) {
636 return status.type() == file_type::directory_file;
637}
638
639bool is_regular_file(file_status status) {
640 return status.type() == file_type::regular_file;
641}
642
643bool is_symlink(file_status status) {
644 return status.type() == file_type::symlink_file;
645}
646
647bool is_other(file_status status) {
648 return exists(status) &&
649 !is_regular_file(status) &&
650 !is_directory(status) &&
651 !is_symlink(status);
652}
653
Michael J. Spencer753cbbb2010-12-06 04:28:42 +0000654void directory_entry::replace_filename(const Twine &filename, file_status st,
655 file_status symlink_st) {
656 SmallString<128> path(Path.begin(), Path.end());
657 path::remove_filename(path);
658 path::append(path, filename);
659 Path = path.str();
660 Status = st;
661 SymlinkStatus = symlink_st;
662}
663
Michael J. Spencer628467e2010-12-28 01:49:01 +0000664error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
665 SmallString<128> PathStorage;
666 SmallString<32> MagicStorage;
667 StringRef Path = path.toNullTerminatedStringRef(PathStorage);
668 StringRef Magic = magic.toNullTerminatedStringRef(MagicStorage);
669
670 assert(Magic.size() > 0 && "magic must be non-empty!");
671
672 SmallString<32> BufferStorage;
673 BufferStorage.reserve(Magic.size());
674
675 // Open file.
676 std::FILE *file = std::fopen(Path.data(), "rb");
677 if (file == 0)
678 return error_code(errno, posix_category());
679 int size = ::fread(BufferStorage.data(), 1, Magic.size(), file);
680 if (size != Magic.size()) {
681 int error = errno;
682 bool eof = std::feof(file) != 0;
683 std::fclose(file);
684 if (eof) {
685 // EOF, return false.
686 result = false;
687 return success;
688 }
689 return error_code(error, posix_category());
690 }
691 std::fclose(file);
692
693 if (std::memcmp(BufferStorage.data(), Magic.data(), Magic.size()) != 0)
694 result = false;
695 else
696 result = true;
697 return success;
698}
699
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000700} // end namespace fs
701} // end namespace sys
702} // end namespace llvm
Michael J. Spencerdffde992010-11-29 22:28:51 +0000703
704// Include the truly platform-specific parts.
705#if defined(LLVM_ON_UNIX)
706#include "Unix/PathV2.inc"
707#endif
708#if defined(LLVM_ON_WIN32)
709#include "Windows/PathV2.inc"
710#endif