blob: ac53a9e9e6b41ec985281e3ed94314f1c6115085 [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. 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>
Douglas Gregorb0b5bde2013-03-21 21:46:10 +000021#ifdef __APPLE__
22#include <unistd.h>
23#endif
Michael J. Spencerdffde992010-11-29 22:28:51 +000024
25namespace {
26 using llvm::StringRef;
Zhanyong Wan63cc3a82011-02-11 21:24:40 +000027 using llvm::sys::path::is_separator;
Michael J. Spencerdffde992010-11-29 22:28:51 +000028
29#ifdef LLVM_ON_WIN32
Benjamin Kramer80fd2a12012-02-08 13:13:47 +000030 const char *separators = "\\/";
31 const char prefered_separator = '\\';
Michael J. Spencerdffde992010-11-29 22:28:51 +000032#else
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#endif
36
Benjamin Kramer7fb86662010-12-17 18:59:09 +000037 StringRef find_first_component(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000038 // Look for this first component in the following order.
39 // * empty (in this case we return an empty string)
40 // * either C: or {//,\\}net.
41 // * {/,\}
42 // * {.,..}
43 // * {file,directory}name
44
45 if (path.empty())
46 return path;
47
Michael J. Spencerca3a3392010-12-07 03:57:48 +000048#ifdef LLVM_ON_WIN32
Michael J. Spencerdffde992010-11-29 22:28:51 +000049 // C:
Guy Benyei87d0b9e2013-02-12 21:21:59 +000050 if (path.size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) &&
51 path[1] == ':')
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000052 return path.substr(0, 2);
Michael J. Spencerca3a3392010-12-07 03:57:48 +000053#endif
Michael J. Spencerdffde992010-11-29 22:28:51 +000054
55 // //net
56 if ((path.size() > 2) &&
Michael J. Spencerca3a3392010-12-07 03:57:48 +000057 is_separator(path[0]) &&
58 path[0] == path[1] &&
59 !is_separator(path[2])) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000060 // Find the next directory separator.
Michael J. Spencerca3a3392010-12-07 03:57:48 +000061 size_t end = path.find_first_of(separators, 2);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000062 return path.substr(0, end);
Michael J. Spencerdffde992010-11-29 22:28:51 +000063 }
64
65 // {/,\}
Michael J. Spencerca3a3392010-12-07 03:57:48 +000066 if (is_separator(path[0]))
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000067 return path.substr(0, 1);
Michael J. Spencerdffde992010-11-29 22:28:51 +000068
69 if (path.startswith(".."))
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000070 return path.substr(0, 2);
Michael J. Spencerdffde992010-11-29 22:28:51 +000071
72 if (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 // * {file,directory}name
Michael J. Spencerca3a3392010-12-07 03:57:48 +000076 size_t end = path.find_first_of(separators, 2);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000077 return path.substr(0, end);
Michael J. Spencerdffde992010-11-29 22:28:51 +000078 }
Michael J. Spencera42cf732010-11-30 23:28:07 +000079
Benjamin Kramerd53f3c82010-12-17 18:19:06 +000080 size_t filename_pos(StringRef str) {
Michael J. Spencera42cf732010-11-30 23:28:07 +000081 if (str.size() == 2 &&
82 is_separator(str[0]) &&
Michael J. Spencerca3a3392010-12-07 03:57:48 +000083 str[0] == str[1])
Michael J. Spencera42cf732010-11-30 23:28:07 +000084 return 0;
85
86 if (str.size() > 0 && is_separator(str[str.size() - 1]))
87 return str.size() - 1;
88
89 size_t pos = str.find_last_of(separators, str.size() - 1);
90
91#ifdef LLVM_ON_WIN32
92 if (pos == StringRef::npos)
93 pos = str.find_last_of(':', str.size() - 2);
94#endif
95
96 if (pos == StringRef::npos ||
97 (pos == 1 && is_separator(str[0])))
98 return 0;
99
100 return pos + 1;
101 }
102
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000103 size_t root_dir_start(StringRef str) {
Michael J. Spencera42cf732010-11-30 23:28:07 +0000104 // case "c:/"
105#ifdef LLVM_ON_WIN32
106 if (str.size() > 2 &&
107 str[1] == ':' &&
108 is_separator(str[2]))
109 return 2;
110#endif
111
112 // case "//"
113 if (str.size() == 2 &&
114 is_separator(str[0]) &&
115 str[0] == str[1])
116 return StringRef::npos;
117
118 // case "//net"
119 if (str.size() > 3 &&
120 is_separator(str[0]) &&
121 str[0] == str[1] &&
122 !is_separator(str[2])) {
123 return str.find_first_of(separators, 2);
124 }
125
126 // case "/"
127 if (str.size() > 0 && is_separator(str[0]))
128 return 0;
129
130 return StringRef::npos;
131 }
132
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000133 size_t parent_path_end(StringRef path) {
Michael J. Spencera42cf732010-11-30 23:28:07 +0000134 size_t end_pos = filename_pos(path);
135
136 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
137
138 // Skip separators except for root dir.
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000139 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
Michael J. Spencera42cf732010-11-30 23:28:07 +0000140
141 while(end_pos > 0 &&
142 (end_pos - 1) != root_dir_pos &&
143 is_separator(path[end_pos - 1]))
144 --end_pos;
145
146 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
147 return StringRef::npos;
148
149 return end_pos;
150 }
Zhanyong Wan63cc3a82011-02-11 21:24:40 +0000151} // end unnamed namespace
Michael J. Spencerdffde992010-11-29 22:28:51 +0000152
153namespace llvm {
154namespace sys {
155namespace path {
156
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000157const_iterator begin(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000158 const_iterator i;
159 i.Path = path;
160 i.Component = find_first_component(path);
161 i.Position = 0;
162 return i;
163}
164
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000165const_iterator end(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000166 const_iterator i;
167 i.Path = path;
168 i.Position = path.size();
169 return i;
170}
171
Michael J. Spencerdffde992010-11-29 22:28:51 +0000172const_iterator &const_iterator::operator++() {
173 assert(Position < Path.size() && "Tried to increment past end!");
174
175 // Increment Position to past the current component
176 Position += Component.size();
177
178 // Check for end.
179 if (Position == Path.size()) {
180 Component = StringRef();
181 return *this;
182 }
183
184 // Both POSIX and Windows treat paths that begin with exactly two separators
185 // specially.
186 bool was_net = Component.size() > 2 &&
187 is_separator(Component[0]) &&
188 Component[1] == Component[0] &&
189 !is_separator(Component[2]);
190
191 // Handle separators.
192 if (is_separator(Path[Position])) {
193 // Root dir.
194 if (was_net
195#ifdef LLVM_ON_WIN32
196 // c:/
197 || Component.endswith(":")
198#endif
199 ) {
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000200 Component = Path.substr(Position, 1);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000201 return *this;
202 }
203
204 // Skip extra separators.
205 while (Position != Path.size() &&
206 is_separator(Path[Position])) {
207 ++Position;
208 }
209
210 // Treat trailing '/' as a '.'.
211 if (Position == Path.size()) {
212 --Position;
213 Component = ".";
214 return *this;
215 }
216 }
217
218 // Find next component.
219 size_t end_pos = Path.find_first_of(separators, Position);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000220 Component = Path.slice(Position, end_pos);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000221
222 return *this;
223}
224
Michael J. Spencera42cf732010-11-30 23:28:07 +0000225const_iterator &const_iterator::operator--() {
226 // If we're at the end and the previous char was a '/', return '.'.
227 if (Position == Path.size() &&
228 Path.size() > 1 &&
229 is_separator(Path[Position - 1])
230#ifdef LLVM_ON_WIN32
231 && Path[Position - 2] != ':'
232#endif
233 ) {
234 --Position;
235 Component = ".";
236 return *this;
237 }
238
239 // Skip separators unless it's the root directory.
240 size_t root_dir_pos = root_dir_start(Path);
241 size_t end_pos = Position;
242
243 while(end_pos > 0 &&
244 (end_pos - 1) != root_dir_pos &&
245 is_separator(Path[end_pos - 1]))
246 --end_pos;
247
248 // Find next separator.
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000249 size_t start_pos = filename_pos(Path.substr(0, end_pos));
250 Component = Path.slice(start_pos, end_pos);
Michael J. Spencera42cf732010-11-30 23:28:07 +0000251 Position = start_pos;
252 return *this;
253}
254
Michael J. Spencerdffde992010-11-29 22:28:51 +0000255bool const_iterator::operator==(const const_iterator &RHS) const {
256 return Path.begin() == RHS.Path.begin() &&
257 Position == RHS.Position;
258}
259
260bool const_iterator::operator!=(const const_iterator &RHS) const {
261 return !(*this == RHS);
262}
263
Michael J. Spencera42cf732010-11-30 23:28:07 +0000264ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
265 return Position - RHS.Position;
266}
267
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000268const StringRef root_path(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000269 const_iterator b = begin(path),
270 pos = b,
271 e = end(path);
272 if (b != e) {
273 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
274 bool has_drive =
275#ifdef LLVM_ON_WIN32
276 b->endswith(":");
277#else
278 false;
279#endif
280
281 if (has_net || has_drive) {
282 if ((++pos != e) && is_separator((*pos)[0])) {
283 // {C:/,//net/}, so get the first two components.
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000284 return path.substr(0, b->size() + pos->size());
Michael J. Spencerdffde992010-11-29 22:28:51 +0000285 } else {
286 // just {C:,//net}, return the first component.
Michael J. Spencer50291592010-12-07 17:04:04 +0000287 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000288 }
289 }
290
291 // POSIX style root directory.
292 if (is_separator((*b)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000293 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000294 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000295 }
296
Michael J. Spencer50291592010-12-07 17:04:04 +0000297 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000298}
299
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000300const StringRef root_name(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000301 const_iterator b = begin(path),
302 e = end(path);
303 if (b != e) {
304 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
305 bool has_drive =
306#ifdef LLVM_ON_WIN32
307 b->endswith(":");
308#else
309 false;
310#endif
311
312 if (has_net || has_drive) {
313 // just {C:,//net}, return the first component.
Michael J. Spencer50291592010-12-07 17:04:04 +0000314 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000315 }
316 }
317
318 // No path or no name.
Michael J. Spencer50291592010-12-07 17:04:04 +0000319 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000320}
321
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000322const StringRef root_directory(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000323 const_iterator b = begin(path),
324 pos = b,
325 e = end(path);
326 if (b != e) {
327 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
328 bool has_drive =
329#ifdef LLVM_ON_WIN32
330 b->endswith(":");
331#else
332 false;
333#endif
334
335 if ((has_net || has_drive) &&
336 // {C:,//net}, skip to the next component.
337 (++pos != e) && is_separator((*pos)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000338 return *pos;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000339 }
340
341 // POSIX style root directory.
342 if (!has_net && is_separator((*b)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000343 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000344 }
345 }
346
347 // No path or no root.
Michael J. Spencer50291592010-12-07 17:04:04 +0000348 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000349}
350
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000351const StringRef relative_path(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000352 StringRef root = root_path(path);
Michael J. Spencerd0fff182012-02-29 00:06:24 +0000353 return path.substr(root.size());
Michael J. Spencerdffde992010-11-29 22:28:51 +0000354}
355
Michael J. Spencer936671b2010-12-07 03:57:37 +0000356void append(SmallVectorImpl<char> &path, const Twine &a,
357 const Twine &b,
358 const Twine &c,
359 const Twine &d) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000360 SmallString<32> a_storage;
361 SmallString<32> b_storage;
362 SmallString<32> c_storage;
363 SmallString<32> d_storage;
364
365 SmallVector<StringRef, 4> components;
366 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
367 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
368 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
369 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
370
371 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
372 e = components.end();
373 i != e; ++i) {
374 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
375 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
Michael J. Spencer50291592010-12-07 17:04:04 +0000376 bool is_root_name = has_root_name(*i);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000377
378 if (path_has_sep) {
379 // Strip separators from beginning of component.
380 size_t loc = i->find_first_not_of(separators);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000381 StringRef c = i->substr(loc);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000382
383 // Append it.
384 path.append(c.begin(), c.end());
385 continue;
386 }
387
Michael J. Spencerf150e762010-12-06 04:28:23 +0000388 if (!component_has_sep && !(path.empty() || is_root_name)) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000389 // Add a separator.
390 path.push_back(prefered_separator);
391 }
392
393 path.append(i->begin(), i->end());
394 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000395}
396
Argyrios Kyrtzidis6e31b9b2011-02-15 17:51:19 +0000397void append(SmallVectorImpl<char> &path,
398 const_iterator begin, const_iterator end) {
399 for (; begin != end; ++begin)
400 path::append(path, *begin);
401}
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
Zhanyong Wan63cc3a82011-02-11 21:24:40 +0000486bool is_separator(char value) {
487 switch(value) {
488#ifdef LLVM_ON_WIN32
489 case '\\': // fall through
490#endif
491 case '/': return true;
492 default: return false;
493 }
494}
495
Douglas Gregor55cf8152011-09-14 20:27:01 +0000496void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result) {
497 result.clear();
Michael J. Spencerfaebf112011-12-13 23:16:15 +0000498
Douglas Gregorb0b5bde2013-03-21 21:46:10 +0000499#ifdef __APPLE__
500 // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR.
501 int ConfName = erasedOnReboot? _CS_DARWIN_USER_TEMP_DIR
502 : _CS_DARWIN_USER_CACHE_DIR;
503 size_t ConfLen = confstr(ConfName, 0, 0);
504 if (ConfLen > 0) {
505 do {
506 result.resize(ConfLen);
507 ConfLen = confstr(ConfName, result.data(), result.size());
508 } while (ConfLen > 0 && ConfLen != result.size());
509
510 if (ConfLen > 0) {
511 assert(result.back() == 0);
512 result.pop_back();
513 return;
514 }
515
516 result.clear();
517 }
518#endif
519
Douglas Gregor55cf8152011-09-14 20:27:01 +0000520 // Check whether the temporary directory is specified by an environment
521 // variable.
522 const char *EnvironmentVariable;
523#ifdef LLVM_ON_WIN32
524 EnvironmentVariable = "TEMP";
525#else
526 EnvironmentVariable = "TMPDIR";
527#endif
528 if (char *RequestedDir = getenv(EnvironmentVariable)) {
529 result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
530 return;
531 }
Michael J. Spencerfaebf112011-12-13 23:16:15 +0000532
Douglas Gregor55cf8152011-09-14 20:27:01 +0000533 // Fall back to a system default.
534 const char *DefaultResult;
535#ifdef LLVM_ON_WIN32
536 (void)erasedOnReboot;
Douglas Gregord26d6b62011-09-14 23:21:47 +0000537 DefaultResult = "C:\\TEMP";
Douglas Gregor55cf8152011-09-14 20:27:01 +0000538#else
539 if (erasedOnReboot)
540 DefaultResult = "/tmp";
541 else
542 DefaultResult = "/var/tmp";
543#endif
544 result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
545}
Michael J. Spencerfaebf112011-12-13 23:16:15 +0000546
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000547bool has_root_name(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000548 SmallString<128> path_storage;
549 StringRef p = path.toStringRef(path_storage);
550
Michael J. Spencer50291592010-12-07 17:04:04 +0000551 return !root_name(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000552}
553
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000554bool has_root_directory(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000555 SmallString<128> path_storage;
556 StringRef p = path.toStringRef(path_storage);
557
Michael J. Spencer50291592010-12-07 17:04:04 +0000558 return !root_directory(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000559}
560
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000561bool has_root_path(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000562 SmallString<128> path_storage;
563 StringRef p = path.toStringRef(path_storage);
564
Michael J. Spencer50291592010-12-07 17:04:04 +0000565 return !root_path(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000566}
567
Michael J. Spencer2d484eb2010-12-20 13:30:28 +0000568bool has_relative_path(const Twine &path) {
569 SmallString<128> path_storage;
570 StringRef p = path.toStringRef(path_storage);
571
572 return !relative_path(p).empty();
573}
574
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000575bool has_filename(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000576 SmallString<128> path_storage;
577 StringRef p = path.toStringRef(path_storage);
578
Michael J. Spencer50291592010-12-07 17:04:04 +0000579 return !filename(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000580}
581
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000582bool has_parent_path(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000583 SmallString<128> path_storage;
584 StringRef p = path.toStringRef(path_storage);
585
Michael J. Spencer50291592010-12-07 17:04:04 +0000586 return !parent_path(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000587}
588
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000589bool has_stem(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000590 SmallString<128> path_storage;
591 StringRef p = path.toStringRef(path_storage);
592
Michael J. Spencer50291592010-12-07 17:04:04 +0000593 return !stem(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000594}
595
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000596bool has_extension(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000597 SmallString<128> path_storage;
598 StringRef p = path.toStringRef(path_storage);
599
Michael J. Spencer50291592010-12-07 17:04:04 +0000600 return !extension(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000601}
602
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000603bool is_absolute(const Twine &path) {
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000604 SmallString<128> path_storage;
605 StringRef p = path.toStringRef(path_storage);
606
Michael J. Spencer50291592010-12-07 17:04:04 +0000607 bool rootDir = has_root_directory(p),
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000608#ifdef LLVM_ON_WIN32
Michael J. Spencer50291592010-12-07 17:04:04 +0000609 rootName = has_root_name(p);
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000610#else
Michael J. Spencer50291592010-12-07 17:04:04 +0000611 rootName = true;
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000612#endif
613
Michael J. Spencer50291592010-12-07 17:04:04 +0000614 return rootDir && rootName;
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000615}
616
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000617bool is_relative(const Twine &path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000618 return !is_absolute(path);
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000619}
620
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000621} // end namespace path
622
623namespace fs {
624
Michael J. Spenceree271d82010-12-07 03:57:17 +0000625error_code make_absolute(SmallVectorImpl<char> &path) {
626 StringRef p(path.data(), path.size());
627
Daniel Dunbard1d72162012-02-29 00:20:37 +0000628 bool rootDirectory = path::has_root_directory(p),
629#ifdef LLVM_ON_WIN32
Daniel Dunbar95fe7b92012-02-29 00:46:46 +0000630 rootName = path::has_root_name(p);
Daniel Dunbard1d72162012-02-29 00:20:37 +0000631#else
632 rootName = true;
633#endif
Michael J. Spenceree271d82010-12-07 03:57:17 +0000634
635 // Already absolute.
636 if (rootName && rootDirectory)
David Blaikie58604cd2012-02-09 19:24:12 +0000637 return error_code::success();
Michael J. Spenceree271d82010-12-07 03:57:17 +0000638
639 // All of the following conditions will need the current directory.
640 SmallString<128> current_dir;
641 if (error_code ec = current_path(current_dir)) return ec;
642
643 // Relative path. Prepend the current directory.
644 if (!rootName && !rootDirectory) {
645 // Append path to the current directory.
Michael J. Spencer936671b2010-12-07 03:57:37 +0000646 path::append(current_dir, p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000647 // Set path to the result.
648 path.swap(current_dir);
David Blaikie58604cd2012-02-09 19:24:12 +0000649 return error_code::success();
Michael J. Spenceree271d82010-12-07 03:57:17 +0000650 }
651
652 if (!rootName && rootDirectory) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000653 StringRef cdrn = path::root_name(current_dir);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000654 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
Michael J. Spencer936671b2010-12-07 03:57:37 +0000655 path::append(curDirRootName, p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000656 // Set path to the result.
657 path.swap(curDirRootName);
David Blaikie58604cd2012-02-09 19:24:12 +0000658 return error_code::success();
Michael J. Spenceree271d82010-12-07 03:57:17 +0000659 }
660
661 if (rootName && !rootDirectory) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000662 StringRef pRootName = path::root_name(p);
663 StringRef bRootDirectory = path::root_directory(current_dir);
664 StringRef bRelativePath = path::relative_path(current_dir);
665 StringRef pRelativePath = path::relative_path(p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000666
667 SmallString<128> res;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000668 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000669 path.swap(res);
David Blaikie58604cd2012-02-09 19:24:12 +0000670 return error_code::success();
Michael J. Spenceree271d82010-12-07 03:57:17 +0000671 }
672
673 llvm_unreachable("All rootName and rootDirectory combinations should have "
674 "occurred above!");
675}
676
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000677error_code create_directories(const Twine &path, bool &existed) {
678 SmallString<128> path_storage;
679 StringRef p = path.toStringRef(path_storage);
680
Michael J. Spencer50291592010-12-07 17:04:04 +0000681 StringRef parent = path::parent_path(p);
Michael J. Spencer55f43d62012-03-21 23:09:14 +0000682 if (!parent.empty()) {
683 bool parent_exists;
684 if (error_code ec = fs::exists(parent, parent_exists)) return ec;
Michael J. Spencer50291592010-12-07 17:04:04 +0000685
Michael J. Spencer55f43d62012-03-21 23:09:14 +0000686 if (!parent_exists)
687 if (error_code ec = create_directories(parent, existed)) return ec;
688 }
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000689
690 return create_directory(p, existed);
691}
692
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000693bool exists(file_status status) {
694 return status_known(status) && status.type() != file_type::file_not_found;
695}
696
697bool status_known(file_status s) {
698 return s.type() != file_type::status_error;
699}
700
701bool is_directory(file_status status) {
702 return status.type() == file_type::directory_file;
703}
704
Michael J. Spencer218dc3e2011-01-11 01:21:55 +0000705error_code is_directory(const Twine &path, bool &result) {
706 file_status st;
707 if (error_code ec = status(path, st))
708 return ec;
709 result = is_directory(st);
David Blaikie58604cd2012-02-09 19:24:12 +0000710 return error_code::success();
Michael J. Spencer218dc3e2011-01-11 01:21:55 +0000711}
712
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000713bool is_regular_file(file_status status) {
714 return status.type() == file_type::regular_file;
715}
716
Michael J. Spencer218dc3e2011-01-11 01:21:55 +0000717error_code is_regular_file(const Twine &path, bool &result) {
718 file_status st;
719 if (error_code ec = status(path, st))
720 return ec;
721 result = is_regular_file(st);
David Blaikie58604cd2012-02-09 19:24:12 +0000722 return error_code::success();
Michael J. Spencer218dc3e2011-01-11 01:21:55 +0000723}
724
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000725bool is_symlink(file_status status) {
726 return status.type() == file_type::symlink_file;
727}
728
Michael J. Spencer9df536c2011-01-12 23:55:06 +0000729error_code is_symlink(const Twine &path, bool &result) {
730 file_status st;
731 if (error_code ec = status(path, st))
732 return ec;
733 result = is_symlink(st);
David Blaikie58604cd2012-02-09 19:24:12 +0000734 return error_code::success();
Michael J. Spencer9df536c2011-01-12 23:55:06 +0000735}
736
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000737bool is_other(file_status status) {
738 return exists(status) &&
739 !is_regular_file(status) &&
740 !is_directory(status) &&
741 !is_symlink(status);
742}
743
Benjamin Kramer357b5712011-09-14 01:14:36 +0000744void directory_entry::replace_filename(const Twine &filename, file_status st) {
Michael J. Spencer753cbbb2010-12-06 04:28:42 +0000745 SmallString<128> path(Path.begin(), Path.end());
746 path::remove_filename(path);
747 path::append(path, filename);
748 Path = path.str();
749 Status = st;
Michael J. Spencer753cbbb2010-12-06 04:28:42 +0000750}
751
Michael J. Spencer628467e2010-12-28 01:49:01 +0000752error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
Michael J. Spencer628467e2010-12-28 01:49:01 +0000753 SmallString<32> MagicStorage;
Michael J. Spencerb33594b2011-01-15 18:52:41 +0000754 StringRef Magic = magic.toStringRef(MagicStorage);
755 SmallString<32> Buffer;
Michael J. Spencer628467e2010-12-28 01:49:01 +0000756
Michael J. Spencerb33594b2011-01-15 18:52:41 +0000757 if (error_code ec = get_magic(path, Magic.size(), Buffer)) {
758 if (ec == errc::value_too_large) {
759 // Magic.size() > file_size(Path).
Michael J. Spencer628467e2010-12-28 01:49:01 +0000760 result = false;
David Blaikie58604cd2012-02-09 19:24:12 +0000761 return error_code::success();
Michael J. Spencer628467e2010-12-28 01:49:01 +0000762 }
Michael J. Spencerb33594b2011-01-15 18:52:41 +0000763 return ec;
Michael J. Spencer628467e2010-12-28 01:49:01 +0000764 }
Michael J. Spencer628467e2010-12-28 01:49:01 +0000765
Michael J. Spencerb33594b2011-01-15 18:52:41 +0000766 result = Magic == Buffer;
David Blaikie58604cd2012-02-09 19:24:12 +0000767 return error_code::success();
Michael J. Spencer628467e2010-12-28 01:49:01 +0000768}
769
Michael J. Spencer5b082302011-12-13 23:17:12 +0000770/// @brief Identify the magic in magic.
771file_magic identify_magic(StringRef magic) {
Michael J. Spencerb51c8e92012-06-19 05:29:57 +0000772 if (magic.size() < 4)
773 return file_magic::unknown;
Michael J. Spencer5b082302011-12-13 23:17:12 +0000774 switch ((unsigned char)magic[0]) {
775 case 0xDE: // 0x0B17C0DE = BC wraper
776 if (magic[1] == (char)0xC0 && magic[2] == (char)0x17 &&
777 magic[3] == (char)0x0B)
778 return file_magic::bitcode;
779 break;
780 case 'B':
781 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
782 return file_magic::bitcode;
783 break;
784 case '!':
785 if (magic.size() >= 8)
786 if (memcmp(magic.data(),"!<arch>\n",8) == 0)
787 return file_magic::archive;
788 break;
789
790 case '\177':
791 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
Michael J. Spencerc0a74e22013-04-05 20:10:04 +0000792 bool Data2MSB = magic[5] == 2;
793 unsigned high = Data2MSB ? 16 : 17;
794 unsigned low = Data2MSB ? 17 : 16;
795 if (magic.size() >= 18 && magic[high] == 0)
796 switch (magic[low]) {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000797 default: break;
798 case 1: return file_magic::elf_relocatable;
799 case 2: return file_magic::elf_executable;
800 case 3: return file_magic::elf_shared_object;
801 case 4: return file_magic::elf_core;
802 }
803 }
804 break;
805
806 case 0xCA:
807 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
808 magic[3] == char(0xBE)) {
809 // This is complicated by an overlap with Java class files.
810 // See the Mach-O section in /usr/share/file/magic for details.
811 if (magic.size() >= 8 && magic[7] < 43)
812 // FIXME: Universal Binary of any type.
813 return file_magic::macho_dynamically_linked_shared_lib;
814 }
815 break;
816
817 // The two magic numbers for mach-o are:
818 // 0xfeedface - 32-bit mach-o
819 // 0xfeedfacf - 64-bit mach-o
820 case 0xFE:
821 case 0xCE:
822 case 0xCF: {
823 uint16_t type = 0;
824 if (magic[0] == char(0xFE) && magic[1] == char(0xED) &&
825 magic[2] == char(0xFA) &&
826 (magic[3] == char(0xCE) || magic[3] == char(0xCF))) {
827 /* Native endian */
828 if (magic.size() >= 16) type = magic[14] << 8 | magic[15];
829 } else if ((magic[0] == char(0xCE) || magic[0] == char(0xCF)) &&
830 magic[1] == char(0xFA) && magic[2] == char(0xED) &&
831 magic[3] == char(0xFE)) {
832 /* Reverse endian */
833 if (magic.size() >= 14) type = magic[13] << 8 | magic[12];
834 }
835 switch (type) {
836 default: break;
837 case 1: return file_magic::macho_object;
838 case 2: return file_magic::macho_executable;
839 case 3: return file_magic::macho_fixed_virtual_memory_shared_lib;
840 case 4: return file_magic::macho_core;
841 case 5: return file_magic::macho_preload_executabl;
842 case 6: return file_magic::macho_dynamically_linked_shared_lib;
843 case 7: return file_magic::macho_dynamic_linker;
844 case 8: return file_magic::macho_bundle;
845 case 9: return file_magic::macho_dynamic_linker;
846 case 10: return file_magic::macho_dsym_companion;
847 }
848 break;
849 }
850 case 0xF0: // PowerPC Windows
851 case 0x83: // Alpha 32-bit
852 case 0x84: // Alpha 64-bit
853 case 0x66: // MPS R4000 Windows
854 case 0x50: // mc68K
855 case 0x4c: // 80386 Windows
856 if (magic[1] == 0x01)
857 return file_magic::coff_object;
858
859 case 0x90: // PA-RISC Windows
860 case 0x68: // mc68K Windows
861 if (magic[1] == 0x02)
862 return file_magic::coff_object;
863 break;
864
865 case 0x4d: // Possible MS-DOS stub on Windows PE file
866 if (magic[1] == 0x5a) {
867 uint32_t off =
868 *reinterpret_cast<const support::ulittle32_t*>(magic.data() + 0x3c);
869 // PE/COFF file, either EXE or DLL.
870 if (off < magic.size() && memcmp(magic.data() + off, "PE\0\0",4) == 0)
871 return file_magic::pecoff_executable;
872 }
873 break;
874
875 case 0x64: // x86-64 Windows.
876 if (magic[1] == char(0x86))
877 return file_magic::coff_object;
878 break;
879
880 default:
881 break;
882 }
883 return file_magic::unknown;
884}
885
886error_code identify_magic(const Twine &path, file_magic &result) {
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000887 SmallString<32> Magic;
888 error_code ec = get_magic(path, Magic.capacity(), Magic);
889 if (ec && ec != errc::value_too_large)
890 return ec;
891
Michael J. Spencer5b082302011-12-13 23:17:12 +0000892 result = identify_magic(Magic);
David Blaikie58604cd2012-02-09 19:24:12 +0000893 return error_code::success();
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000894}
895
Michael J. Spencer7fd682a2011-01-05 16:39:38 +0000896namespace {
897error_code remove_all_r(StringRef path, file_type ft, uint32_t &count) {
898 if (ft == file_type::directory_file) {
899 // This code would be a lot better with exceptions ;/.
900 error_code ec;
Michael J. Spencer1dd2ee72011-12-12 06:03:33 +0000901 directory_iterator i(path, ec);
902 if (ec) return ec;
903 for (directory_iterator e; i != e; i.increment(ec)) {
Michael J. Spencer7fd682a2011-01-05 16:39:38 +0000904 if (ec) return ec;
905 file_status st;
906 if (error_code ec = i->status(st)) return ec;
907 if (error_code ec = remove_all_r(i->path(), st.type(), count)) return ec;
908 }
909 bool obviously_this_exists;
910 if (error_code ec = remove(path, obviously_this_exists)) return ec;
911 assert(obviously_this_exists);
912 ++count; // Include the directory itself in the items removed.
913 } else {
914 bool obviously_this_exists;
915 if (error_code ec = remove(path, obviously_this_exists)) return ec;
916 assert(obviously_this_exists);
917 ++count;
918 }
919
David Blaikie58604cd2012-02-09 19:24:12 +0000920 return error_code::success();
Michael J. Spencer7fd682a2011-01-05 16:39:38 +0000921}
Zhanyong Wan63cc3a82011-02-11 21:24:40 +0000922} // end unnamed namespace
Michael J. Spencer7fd682a2011-01-05 16:39:38 +0000923
924error_code remove_all(const Twine &path, uint32_t &num_removed) {
925 SmallString<128> path_storage;
926 StringRef p = path.toStringRef(path_storage);
927
928 file_status fs;
929 if (error_code ec = status(path, fs))
930 return ec;
931 num_removed = 0;
932 return remove_all_r(p, fs.type(), num_removed);
933}
934
Michael J. Spencer5bcdc7f2011-01-05 16:39:13 +0000935error_code directory_entry::status(file_status &result) const {
936 return fs::status(Path, result);
937}
938
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000939} // end namespace fs
940} // end namespace sys
941} // end namespace llvm
Michael J. Spencerdffde992010-11-29 22:28:51 +0000942
943// Include the truly platform-specific parts.
944#if defined(LLVM_ON_UNIX)
945#include "Unix/PathV2.inc"
946#endif
947#if defined(LLVM_ON_WIN32)
948#include "Windows/PathV2.inc"
949#endif