blob: 5c28c55f26753c31b673329738d8f87fb31bc65f [file] [log] [blame]
Rafael Espindola7a231f52013-06-26 19:33:03 +00001//===-- Path.cpp - Implement OS Path Concept ------------------------------===//
Michael J. Spencerdffde992010-11-29 22:28:51 +00002//
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//
Rafael Espindola7a231f52013-06-26 19:33:03 +000010// This file implements the operating system Path API.
Michael J. Spencerdffde992010-11-29 22:28:51 +000011//
12//===----------------------------------------------------------------------===//
13
Rafael Espindolaa11c3e22013-06-11 22:21:28 +000014#include "llvm/Support/Path.h"
Michael J. Spencer5b082302011-12-13 23:17:12 +000015#include "llvm/Support/Endian.h"
Michael J. Spencerdffde992010-11-29 22:28:51 +000016#include "llvm/Support/ErrorHandling.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/Support/FileSystem.h"
Michael J. Spencerdffde992010-11-29 22:28:51 +000018#include <cctype>
Michael J. Spencer628467e2010-12-28 01:49:01 +000019#include <cstdio>
20#include <cstring>
Rafael Espindola8ee23f02013-06-18 17:01:00 +000021
22#if !defined(_MSC_VER) && !defined(__MINGW32__)
Douglas Gregorb0b5bde2013-03-21 21:46:10 +000023#include <unistd.h>
Rafael Espindola8ee23f02013-06-18 17:01:00 +000024#else
25#include <io.h>
Douglas Gregorb0b5bde2013-03-21 21:46:10 +000026#endif
Michael J. Spencerdffde992010-11-29 22:28:51 +000027
28namespace {
29 using llvm::StringRef;
Zhanyong Wan63cc3a82011-02-11 21:24:40 +000030 using llvm::sys::path::is_separator;
Michael J. Spencerdffde992010-11-29 22:28:51 +000031
32#ifdef LLVM_ON_WIN32
Benjamin Kramer80fd2a12012-02-08 13:13:47 +000033 const char *separators = "\\/";
34 const char prefered_separator = '\\';
Michael J. Spencerdffde992010-11-29 22:28:51 +000035#else
Benjamin Kramer80fd2a12012-02-08 13:13:47 +000036 const char separators = '/';
37 const char prefered_separator = '/';
Michael J. Spencerdffde992010-11-29 22:28:51 +000038#endif
39
Benjamin Kramer7fb86662010-12-17 18:59:09 +000040 StringRef find_first_component(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000041 // Look for this first component in the following order.
42 // * empty (in this case we return an empty string)
43 // * either C: or {//,\\}net.
44 // * {/,\}
45 // * {.,..}
46 // * {file,directory}name
47
48 if (path.empty())
49 return path;
50
Michael J. Spencerca3a3392010-12-07 03:57:48 +000051#ifdef LLVM_ON_WIN32
Michael J. Spencerdffde992010-11-29 22:28:51 +000052 // C:
Guy Benyei87d0b9e2013-02-12 21:21:59 +000053 if (path.size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) &&
54 path[1] == ':')
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000055 return path.substr(0, 2);
Michael J. Spencerca3a3392010-12-07 03:57:48 +000056#endif
Michael J. Spencerdffde992010-11-29 22:28:51 +000057
58 // //net
59 if ((path.size() > 2) &&
Michael J. Spencerca3a3392010-12-07 03:57:48 +000060 is_separator(path[0]) &&
61 path[0] == path[1] &&
62 !is_separator(path[2])) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000063 // Find the next directory separator.
Michael J. Spencerca3a3392010-12-07 03:57:48 +000064 size_t end = path.find_first_of(separators, 2);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000065 return path.substr(0, end);
Michael J. Spencerdffde992010-11-29 22:28:51 +000066 }
67
68 // {/,\}
Michael J. Spencerca3a3392010-12-07 03:57:48 +000069 if (is_separator(path[0]))
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000070 return path.substr(0, 1);
Michael J. Spencerdffde992010-11-29 22:28:51 +000071
72 if (path.startswith(".."))
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000073 return path.substr(0, 2);
Michael J. Spencerdffde992010-11-29 22:28:51 +000074
75 if (path[0] == '.')
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000076 return path.substr(0, 1);
Michael J. Spencerdffde992010-11-29 22:28:51 +000077
78 // * {file,directory}name
Michael J. Spencerca3a3392010-12-07 03:57:48 +000079 size_t end = path.find_first_of(separators, 2);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000080 return path.substr(0, end);
Michael J. Spencerdffde992010-11-29 22:28:51 +000081 }
Michael J. Spencera42cf732010-11-30 23:28:07 +000082
Benjamin Kramerd53f3c82010-12-17 18:19:06 +000083 size_t filename_pos(StringRef str) {
Michael J. Spencera42cf732010-11-30 23:28:07 +000084 if (str.size() == 2 &&
85 is_separator(str[0]) &&
Michael J. Spencerca3a3392010-12-07 03:57:48 +000086 str[0] == str[1])
Michael J. Spencera42cf732010-11-30 23:28:07 +000087 return 0;
88
89 if (str.size() > 0 && is_separator(str[str.size() - 1]))
90 return str.size() - 1;
91
92 size_t pos = str.find_last_of(separators, str.size() - 1);
93
94#ifdef LLVM_ON_WIN32
95 if (pos == StringRef::npos)
96 pos = str.find_last_of(':', str.size() - 2);
97#endif
98
99 if (pos == StringRef::npos ||
100 (pos == 1 && is_separator(str[0])))
101 return 0;
102
103 return pos + 1;
104 }
105
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000106 size_t root_dir_start(StringRef str) {
Michael J. Spencera42cf732010-11-30 23:28:07 +0000107 // case "c:/"
108#ifdef LLVM_ON_WIN32
109 if (str.size() > 2 &&
110 str[1] == ':' &&
111 is_separator(str[2]))
112 return 2;
113#endif
114
115 // case "//"
116 if (str.size() == 2 &&
117 is_separator(str[0]) &&
118 str[0] == str[1])
119 return StringRef::npos;
120
121 // case "//net"
122 if (str.size() > 3 &&
123 is_separator(str[0]) &&
124 str[0] == str[1] &&
125 !is_separator(str[2])) {
126 return str.find_first_of(separators, 2);
127 }
128
129 // case "/"
130 if (str.size() > 0 && is_separator(str[0]))
131 return 0;
132
133 return StringRef::npos;
134 }
135
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000136 size_t parent_path_end(StringRef path) {
Michael J. Spencera42cf732010-11-30 23:28:07 +0000137 size_t end_pos = filename_pos(path);
138
139 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
140
141 // Skip separators except for root dir.
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000142 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
Michael J. Spencera42cf732010-11-30 23:28:07 +0000143
144 while(end_pos > 0 &&
145 (end_pos - 1) != root_dir_pos &&
146 is_separator(path[end_pos - 1]))
147 --end_pos;
148
149 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
150 return StringRef::npos;
151
152 return end_pos;
153 }
Zhanyong Wan63cc3a82011-02-11 21:24:40 +0000154} // end unnamed namespace
Michael J. Spencerdffde992010-11-29 22:28:51 +0000155
Rafael Espindola8e7294f2013-06-28 03:48:47 +0000156enum FSEntity {
157 FS_Dir,
158 FS_File,
159 FS_Name
160};
161
162// Implemented in Unix/Path.inc and Windows/Path.inc.
163static llvm::error_code
164createUniqueEntity(const llvm::Twine &Model, int &ResultFD,
165 llvm::SmallVectorImpl<char> &ResultPath,
166 bool MakeAbsolute, unsigned Mode, FSEntity Type);
167
Michael J. Spencerdffde992010-11-29 22:28:51 +0000168namespace llvm {
169namespace sys {
170namespace path {
171
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000172const_iterator begin(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000173 const_iterator i;
174 i.Path = path;
175 i.Component = find_first_component(path);
176 i.Position = 0;
177 return i;
178}
179
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000180const_iterator end(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000181 const_iterator i;
182 i.Path = path;
183 i.Position = path.size();
184 return i;
185}
186
Michael J. Spencerdffde992010-11-29 22:28:51 +0000187const_iterator &const_iterator::operator++() {
188 assert(Position < Path.size() && "Tried to increment past end!");
189
190 // Increment Position to past the current component
191 Position += Component.size();
192
193 // Check for end.
194 if (Position == Path.size()) {
195 Component = StringRef();
196 return *this;
197 }
198
199 // Both POSIX and Windows treat paths that begin with exactly two separators
200 // specially.
201 bool was_net = Component.size() > 2 &&
202 is_separator(Component[0]) &&
203 Component[1] == Component[0] &&
204 !is_separator(Component[2]);
205
206 // Handle separators.
207 if (is_separator(Path[Position])) {
208 // Root dir.
209 if (was_net
210#ifdef LLVM_ON_WIN32
211 // c:/
212 || Component.endswith(":")
213#endif
214 ) {
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000215 Component = Path.substr(Position, 1);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000216 return *this;
217 }
218
219 // Skip extra separators.
220 while (Position != Path.size() &&
221 is_separator(Path[Position])) {
222 ++Position;
223 }
224
225 // Treat trailing '/' as a '.'.
226 if (Position == Path.size()) {
227 --Position;
228 Component = ".";
229 return *this;
230 }
231 }
232
233 // Find next component.
234 size_t end_pos = Path.find_first_of(separators, Position);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000235 Component = Path.slice(Position, end_pos);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000236
237 return *this;
238}
239
Michael J. Spencera42cf732010-11-30 23:28:07 +0000240const_iterator &const_iterator::operator--() {
241 // If we're at the end and the previous char was a '/', return '.'.
242 if (Position == Path.size() &&
243 Path.size() > 1 &&
244 is_separator(Path[Position - 1])
245#ifdef LLVM_ON_WIN32
246 && Path[Position - 2] != ':'
247#endif
248 ) {
249 --Position;
250 Component = ".";
251 return *this;
252 }
253
254 // Skip separators unless it's the root directory.
255 size_t root_dir_pos = root_dir_start(Path);
256 size_t end_pos = Position;
257
258 while(end_pos > 0 &&
259 (end_pos - 1) != root_dir_pos &&
260 is_separator(Path[end_pos - 1]))
261 --end_pos;
262
263 // Find next separator.
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000264 size_t start_pos = filename_pos(Path.substr(0, end_pos));
265 Component = Path.slice(start_pos, end_pos);
Michael J. Spencera42cf732010-11-30 23:28:07 +0000266 Position = start_pos;
267 return *this;
268}
269
Michael J. Spencerdffde992010-11-29 22:28:51 +0000270bool const_iterator::operator==(const const_iterator &RHS) const {
271 return Path.begin() == RHS.Path.begin() &&
272 Position == RHS.Position;
273}
274
275bool const_iterator::operator!=(const const_iterator &RHS) const {
276 return !(*this == RHS);
277}
278
Michael J. Spencera42cf732010-11-30 23:28:07 +0000279ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
280 return Position - RHS.Position;
281}
282
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000283const StringRef root_path(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000284 const_iterator b = begin(path),
285 pos = b,
286 e = end(path);
287 if (b != e) {
288 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
289 bool has_drive =
290#ifdef LLVM_ON_WIN32
291 b->endswith(":");
292#else
293 false;
294#endif
295
296 if (has_net || has_drive) {
297 if ((++pos != e) && is_separator((*pos)[0])) {
298 // {C:/,//net/}, so get the first two components.
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000299 return path.substr(0, b->size() + pos->size());
Michael J. Spencerdffde992010-11-29 22:28:51 +0000300 } else {
301 // just {C:,//net}, return the first component.
Michael J. Spencer50291592010-12-07 17:04:04 +0000302 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000303 }
304 }
305
306 // POSIX style root directory.
307 if (is_separator((*b)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000308 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000309 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000310 }
311
Michael J. Spencer50291592010-12-07 17:04:04 +0000312 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000313}
314
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000315const StringRef root_name(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000316 const_iterator b = begin(path),
317 e = end(path);
318 if (b != e) {
319 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
320 bool has_drive =
321#ifdef LLVM_ON_WIN32
322 b->endswith(":");
323#else
324 false;
325#endif
326
327 if (has_net || has_drive) {
328 // just {C:,//net}, return the first component.
Michael J. Spencer50291592010-12-07 17:04:04 +0000329 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000330 }
331 }
332
333 // No path or no name.
Michael J. Spencer50291592010-12-07 17:04:04 +0000334 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000335}
336
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000337const StringRef root_directory(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000338 const_iterator b = begin(path),
339 pos = b,
340 e = end(path);
341 if (b != e) {
342 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
343 bool has_drive =
344#ifdef LLVM_ON_WIN32
345 b->endswith(":");
346#else
347 false;
348#endif
349
350 if ((has_net || has_drive) &&
351 // {C:,//net}, skip to the next component.
352 (++pos != e) && is_separator((*pos)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000353 return *pos;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000354 }
355
356 // POSIX style root directory.
357 if (!has_net && is_separator((*b)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000358 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000359 }
360 }
361
362 // No path or no root.
Michael J. Spencer50291592010-12-07 17:04:04 +0000363 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000364}
365
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000366const StringRef relative_path(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000367 StringRef root = root_path(path);
Michael J. Spencerd0fff182012-02-29 00:06:24 +0000368 return path.substr(root.size());
Michael J. Spencerdffde992010-11-29 22:28:51 +0000369}
370
Michael J. Spencer936671b2010-12-07 03:57:37 +0000371void append(SmallVectorImpl<char> &path, const Twine &a,
372 const Twine &b,
373 const Twine &c,
374 const Twine &d) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000375 SmallString<32> a_storage;
376 SmallString<32> b_storage;
377 SmallString<32> c_storage;
378 SmallString<32> d_storage;
379
380 SmallVector<StringRef, 4> components;
381 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
382 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
383 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
384 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
385
386 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
387 e = components.end();
388 i != e; ++i) {
389 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
390 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
Michael J. Spencer50291592010-12-07 17:04:04 +0000391 bool is_root_name = has_root_name(*i);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000392
393 if (path_has_sep) {
394 // Strip separators from beginning of component.
395 size_t loc = i->find_first_not_of(separators);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000396 StringRef c = i->substr(loc);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000397
398 // Append it.
399 path.append(c.begin(), c.end());
400 continue;
401 }
402
Michael J. Spencerf150e762010-12-06 04:28:23 +0000403 if (!component_has_sep && !(path.empty() || is_root_name)) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000404 // Add a separator.
405 path.push_back(prefered_separator);
406 }
407
408 path.append(i->begin(), i->end());
409 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000410}
411
Argyrios Kyrtzidis6e31b9b2011-02-15 17:51:19 +0000412void append(SmallVectorImpl<char> &path,
413 const_iterator begin, const_iterator end) {
414 for (; begin != end; ++begin)
415 path::append(path, *begin);
416}
417
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000418const StringRef parent_path(StringRef path) {
Michael J. Spencera42cf732010-11-30 23:28:07 +0000419 size_t end_pos = parent_path_end(path);
420 if (end_pos == StringRef::npos)
Michael J. Spencer50291592010-12-07 17:04:04 +0000421 return StringRef();
Michael J. Spencera42cf732010-11-30 23:28:07 +0000422 else
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000423 return path.substr(0, end_pos);
Michael J. Spencera42cf732010-11-30 23:28:07 +0000424}
425
Michael J. Spencer936671b2010-12-07 03:57:37 +0000426void remove_filename(SmallVectorImpl<char> &path) {
Michael J. Spencerdbfb56b2010-12-01 00:52:28 +0000427 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
Michael J. Spencer936671b2010-12-07 03:57:37 +0000428 if (end_pos != StringRef::npos)
429 path.set_size(end_pos);
Michael J. Spencerdbfb56b2010-12-01 00:52:28 +0000430}
431
Michael J. Spencer50291592010-12-07 17:04:04 +0000432void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
Michael J. Spencer52ed8672010-12-01 00:52:55 +0000433 StringRef p(path.begin(), path.size());
434 SmallString<32> ext_storage;
435 StringRef ext = extension.toStringRef(ext_storage);
436
437 // Erase existing extension.
438 size_t pos = p.find_last_of('.');
439 if (pos != StringRef::npos && pos >= filename_pos(p))
440 path.set_size(pos);
441
442 // Append '.' if needed.
443 if (ext.size() > 0 && ext[0] != '.')
444 path.push_back('.');
445
446 // Append extension.
447 path.append(ext.begin(), ext.end());
Michael J. Spencer52ed8672010-12-01 00:52:55 +0000448}
449
Michael J. Spencer936671b2010-12-07 03:57:37 +0000450void native(const Twine &path, SmallVectorImpl<char> &result) {
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000451 // Clear result.
Michael J. Spencerfbd1bbd2010-12-07 01:23:19 +0000452 result.clear();
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000453#ifdef LLVM_ON_WIN32
454 SmallString<128> path_storage;
455 StringRef p = path.toStringRef(path_storage);
456 result.reserve(p.size());
457 for (StringRef::const_iterator i = p.begin(),
458 e = p.end();
459 i != e;
460 ++i) {
461 if (*i == '/')
462 result.push_back('\\');
463 else
464 result.push_back(*i);
465 }
466#else
467 path.toVector(result);
468#endif
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000469}
470
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000471const StringRef filename(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000472 return *(--end(path));
Michael J. Spencera9793552010-12-01 03:18:17 +0000473}
474
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000475const StringRef stem(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000476 StringRef fname = filename(path);
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000477 size_t pos = fname.find_last_of('.');
478 if (pos == StringRef::npos)
Michael J. Spencer50291592010-12-07 17:04:04 +0000479 return fname;
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000480 else
481 if ((fname.size() == 1 && fname == ".") ||
482 (fname.size() == 2 && fname == ".."))
Michael J. Spencer50291592010-12-07 17:04:04 +0000483 return fname;
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000484 else
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000485 return fname.substr(0, pos);
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000486}
487
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000488const StringRef extension(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000489 StringRef fname = filename(path);
Michael J. Spencer5265f222010-12-01 03:37:41 +0000490 size_t pos = fname.find_last_of('.');
491 if (pos == StringRef::npos)
Michael J. Spencer50291592010-12-07 17:04:04 +0000492 return StringRef();
Michael J. Spencer5265f222010-12-01 03:37:41 +0000493 else
494 if ((fname.size() == 1 && fname == ".") ||
495 (fname.size() == 2 && fname == ".."))
Michael J. Spencer50291592010-12-07 17:04:04 +0000496 return StringRef();
Michael J. Spencer5265f222010-12-01 03:37:41 +0000497 else
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000498 return fname.substr(pos);
Michael J. Spencer5265f222010-12-01 03:37:41 +0000499}
500
Zhanyong Wan63cc3a82011-02-11 21:24:40 +0000501bool is_separator(char value) {
502 switch(value) {
503#ifdef LLVM_ON_WIN32
504 case '\\': // fall through
505#endif
506 case '/': return true;
507 default: return false;
508 }
509}
510
Douglas Gregor55cf8152011-09-14 20:27:01 +0000511void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result) {
512 result.clear();
Michael J. Spencerfaebf112011-12-13 23:16:15 +0000513
Douglas Gregorb0b5bde2013-03-21 21:46:10 +0000514#ifdef __APPLE__
515 // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR.
516 int ConfName = erasedOnReboot? _CS_DARWIN_USER_TEMP_DIR
517 : _CS_DARWIN_USER_CACHE_DIR;
518 size_t ConfLen = confstr(ConfName, 0, 0);
519 if (ConfLen > 0) {
520 do {
521 result.resize(ConfLen);
522 ConfLen = confstr(ConfName, result.data(), result.size());
523 } while (ConfLen > 0 && ConfLen != result.size());
524
525 if (ConfLen > 0) {
526 assert(result.back() == 0);
527 result.pop_back();
528 return;
529 }
530
531 result.clear();
532 }
533#endif
534
Douglas Gregor55cf8152011-09-14 20:27:01 +0000535 // Check whether the temporary directory is specified by an environment
536 // variable.
537 const char *EnvironmentVariable;
538#ifdef LLVM_ON_WIN32
539 EnvironmentVariable = "TEMP";
540#else
541 EnvironmentVariable = "TMPDIR";
542#endif
543 if (char *RequestedDir = getenv(EnvironmentVariable)) {
544 result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
545 return;
546 }
Michael J. Spencerfaebf112011-12-13 23:16:15 +0000547
Douglas Gregor55cf8152011-09-14 20:27:01 +0000548 // Fall back to a system default.
549 const char *DefaultResult;
550#ifdef LLVM_ON_WIN32
551 (void)erasedOnReboot;
Douglas Gregord26d6b62011-09-14 23:21:47 +0000552 DefaultResult = "C:\\TEMP";
Douglas Gregor55cf8152011-09-14 20:27:01 +0000553#else
554 if (erasedOnReboot)
555 DefaultResult = "/tmp";
556 else
557 DefaultResult = "/var/tmp";
558#endif
559 result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
560}
Michael J. Spencerfaebf112011-12-13 23:16:15 +0000561
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000562bool has_root_name(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000563 SmallString<128> path_storage;
564 StringRef p = path.toStringRef(path_storage);
565
Michael J. Spencer50291592010-12-07 17:04:04 +0000566 return !root_name(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000567}
568
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000569bool has_root_directory(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000570 SmallString<128> path_storage;
571 StringRef p = path.toStringRef(path_storage);
572
Michael J. Spencer50291592010-12-07 17:04:04 +0000573 return !root_directory(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000574}
575
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000576bool has_root_path(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000577 SmallString<128> path_storage;
578 StringRef p = path.toStringRef(path_storage);
579
Michael J. Spencer50291592010-12-07 17:04:04 +0000580 return !root_path(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000581}
582
Michael J. Spencer2d484eb2010-12-20 13:30:28 +0000583bool has_relative_path(const Twine &path) {
584 SmallString<128> path_storage;
585 StringRef p = path.toStringRef(path_storage);
586
587 return !relative_path(p).empty();
588}
589
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000590bool has_filename(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000591 SmallString<128> path_storage;
592 StringRef p = path.toStringRef(path_storage);
593
Michael J. Spencer50291592010-12-07 17:04:04 +0000594 return !filename(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000595}
596
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000597bool has_parent_path(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000598 SmallString<128> path_storage;
599 StringRef p = path.toStringRef(path_storage);
600
Michael J. Spencer50291592010-12-07 17:04:04 +0000601 return !parent_path(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000602}
603
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000604bool has_stem(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000605 SmallString<128> path_storage;
606 StringRef p = path.toStringRef(path_storage);
607
Michael J. Spencer50291592010-12-07 17:04:04 +0000608 return !stem(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000609}
610
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000611bool has_extension(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000612 SmallString<128> path_storage;
613 StringRef p = path.toStringRef(path_storage);
614
Michael J. Spencer50291592010-12-07 17:04:04 +0000615 return !extension(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000616}
617
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000618bool is_absolute(const Twine &path) {
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000619 SmallString<128> path_storage;
620 StringRef p = path.toStringRef(path_storage);
621
Michael J. Spencer50291592010-12-07 17:04:04 +0000622 bool rootDir = has_root_directory(p),
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000623#ifdef LLVM_ON_WIN32
Michael J. Spencer50291592010-12-07 17:04:04 +0000624 rootName = has_root_name(p);
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000625#else
Michael J. Spencer50291592010-12-07 17:04:04 +0000626 rootName = true;
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000627#endif
628
Michael J. Spencer50291592010-12-07 17:04:04 +0000629 return rootDir && rootName;
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000630}
631
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000632bool is_relative(const Twine &path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000633 return !is_absolute(path);
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000634}
635
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000636} // end namespace path
637
638namespace fs {
639
Rafael Espindola8e7294f2013-06-28 03:48:47 +0000640// This is a mkostemps with a different pattern. Unfortunatelly OS X (ond *BSD)
Rafael Espindola41574662013-06-28 10:55:41 +0000641// don't have it. We should try using mkostemps on systems that have it.
Rafael Espindola8e7294f2013-06-28 03:48:47 +0000642error_code unique_file(const Twine &Model, int &ResultFD,
643 SmallVectorImpl<char> &ResultPath, bool MakeAbsolute,
644 unsigned Mode) {
645 return createUniqueEntity(Model, ResultFD, ResultPath, MakeAbsolute, Mode,
646 FS_File);
Rafael Espindola8ee23f02013-06-18 17:01:00 +0000647}
648
Rafael Espindola8e7294f2013-06-28 03:48:47 +0000649// This is a mktemp with a differet pattern. We use createUniqueEntity mostly
Rafael Espindola41574662013-06-28 10:55:41 +0000650// for consistency. We should try using mktemp.
Rafael Espindola8e7294f2013-06-28 03:48:47 +0000651error_code unique_file(const Twine &Model, SmallVectorImpl<char> &ResultPath,
652 bool MakeAbsolute) {
653 int Dummy;
654 return createUniqueEntity(Model, Dummy, ResultPath, MakeAbsolute, 0, FS_Name);
655}
656
657// This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
Rafael Espindola41574662013-06-28 10:55:41 +0000658// for consistency. We should try using mkdtemp.
Rafael Espindola08ddd122013-06-27 03:45:31 +0000659error_code createUniqueDirectory(const Twine &Prefix,
660 SmallVectorImpl<char> &ResultPath) {
Rafael Espindola8e7294f2013-06-28 03:48:47 +0000661 int Dummy;
662 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath,
663 true, 0, FS_Dir);
Rafael Espindola08ddd122013-06-27 03:45:31 +0000664}
665
Michael J. Spenceree271d82010-12-07 03:57:17 +0000666error_code make_absolute(SmallVectorImpl<char> &path) {
667 StringRef p(path.data(), path.size());
668
Daniel Dunbard1d72162012-02-29 00:20:37 +0000669 bool rootDirectory = path::has_root_directory(p),
670#ifdef LLVM_ON_WIN32
Daniel Dunbar95fe7b92012-02-29 00:46:46 +0000671 rootName = path::has_root_name(p);
Daniel Dunbard1d72162012-02-29 00:20:37 +0000672#else
673 rootName = true;
674#endif
Michael J. Spenceree271d82010-12-07 03:57:17 +0000675
676 // Already absolute.
677 if (rootName && rootDirectory)
David Blaikie58604cd2012-02-09 19:24:12 +0000678 return error_code::success();
Michael J. Spenceree271d82010-12-07 03:57:17 +0000679
680 // All of the following conditions will need the current directory.
681 SmallString<128> current_dir;
682 if (error_code ec = current_path(current_dir)) return ec;
683
684 // Relative path. Prepend the current directory.
685 if (!rootName && !rootDirectory) {
686 // Append path to the current directory.
Michael J. Spencer936671b2010-12-07 03:57:37 +0000687 path::append(current_dir, p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000688 // Set path to the result.
689 path.swap(current_dir);
David Blaikie58604cd2012-02-09 19:24:12 +0000690 return error_code::success();
Michael J. Spenceree271d82010-12-07 03:57:17 +0000691 }
692
693 if (!rootName && rootDirectory) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000694 StringRef cdrn = path::root_name(current_dir);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000695 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
Michael J. Spencer936671b2010-12-07 03:57:37 +0000696 path::append(curDirRootName, p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000697 // Set path to the result.
698 path.swap(curDirRootName);
David Blaikie58604cd2012-02-09 19:24:12 +0000699 return error_code::success();
Michael J. Spenceree271d82010-12-07 03:57:17 +0000700 }
701
702 if (rootName && !rootDirectory) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000703 StringRef pRootName = path::root_name(p);
704 StringRef bRootDirectory = path::root_directory(current_dir);
705 StringRef bRelativePath = path::relative_path(current_dir);
706 StringRef pRelativePath = path::relative_path(p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000707
708 SmallString<128> res;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000709 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000710 path.swap(res);
David Blaikie58604cd2012-02-09 19:24:12 +0000711 return error_code::success();
Michael J. Spenceree271d82010-12-07 03:57:17 +0000712 }
713
714 llvm_unreachable("All rootName and rootDirectory combinations should have "
715 "occurred above!");
716}
717
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000718error_code create_directories(const Twine &path, bool &existed) {
719 SmallString<128> path_storage;
720 StringRef p = path.toStringRef(path_storage);
721
Michael J. Spencer50291592010-12-07 17:04:04 +0000722 StringRef parent = path::parent_path(p);
Michael J. Spencer55f43d62012-03-21 23:09:14 +0000723 if (!parent.empty()) {
724 bool parent_exists;
725 if (error_code ec = fs::exists(parent, parent_exists)) return ec;
Michael J. Spencer50291592010-12-07 17:04:04 +0000726
Michael J. Spencer55f43d62012-03-21 23:09:14 +0000727 if (!parent_exists)
728 if (error_code ec = create_directories(parent, existed)) return ec;
729 }
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000730
731 return create_directory(p, existed);
732}
733
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000734bool exists(file_status status) {
735 return status_known(status) && status.type() != file_type::file_not_found;
736}
737
738bool status_known(file_status s) {
739 return s.type() != file_type::status_error;
740}
741
742bool is_directory(file_status status) {
743 return status.type() == file_type::directory_file;
744}
745
Michael J. Spencer218dc3e2011-01-11 01:21:55 +0000746error_code is_directory(const Twine &path, bool &result) {
747 file_status st;
748 if (error_code ec = status(path, st))
749 return ec;
750 result = is_directory(st);
David Blaikie58604cd2012-02-09 19:24:12 +0000751 return error_code::success();
Michael J. Spencer218dc3e2011-01-11 01:21:55 +0000752}
753
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000754bool is_regular_file(file_status status) {
755 return status.type() == file_type::regular_file;
756}
757
Michael J. Spencer218dc3e2011-01-11 01:21:55 +0000758error_code is_regular_file(const Twine &path, bool &result) {
759 file_status st;
760 if (error_code ec = status(path, st))
761 return ec;
762 result = is_regular_file(st);
David Blaikie58604cd2012-02-09 19:24:12 +0000763 return error_code::success();
Michael J. Spencer218dc3e2011-01-11 01:21:55 +0000764}
765
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000766bool is_symlink(file_status status) {
767 return status.type() == file_type::symlink_file;
768}
769
Michael J. Spencer9df536c2011-01-12 23:55:06 +0000770error_code is_symlink(const Twine &path, bool &result) {
771 file_status st;
772 if (error_code ec = status(path, st))
773 return ec;
774 result = is_symlink(st);
David Blaikie58604cd2012-02-09 19:24:12 +0000775 return error_code::success();
Michael J. Spencer9df536c2011-01-12 23:55:06 +0000776}
777
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000778bool is_other(file_status status) {
779 return exists(status) &&
780 !is_regular_file(status) &&
781 !is_directory(status) &&
782 !is_symlink(status);
783}
784
Benjamin Kramer357b5712011-09-14 01:14:36 +0000785void directory_entry::replace_filename(const Twine &filename, file_status st) {
Michael J. Spencer753cbbb2010-12-06 04:28:42 +0000786 SmallString<128> path(Path.begin(), Path.end());
787 path::remove_filename(path);
788 path::append(path, filename);
789 Path = path.str();
790 Status = st;
Michael J. Spencer753cbbb2010-12-06 04:28:42 +0000791}
792
Michael J. Spencer628467e2010-12-28 01:49:01 +0000793error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
Michael J. Spencer628467e2010-12-28 01:49:01 +0000794 SmallString<32> MagicStorage;
Michael J. Spencerb33594b2011-01-15 18:52:41 +0000795 StringRef Magic = magic.toStringRef(MagicStorage);
796 SmallString<32> Buffer;
Michael J. Spencer628467e2010-12-28 01:49:01 +0000797
Michael J. Spencerb33594b2011-01-15 18:52:41 +0000798 if (error_code ec = get_magic(path, Magic.size(), Buffer)) {
799 if (ec == errc::value_too_large) {
800 // Magic.size() > file_size(Path).
Michael J. Spencer628467e2010-12-28 01:49:01 +0000801 result = false;
David Blaikie58604cd2012-02-09 19:24:12 +0000802 return error_code::success();
Michael J. Spencer628467e2010-12-28 01:49:01 +0000803 }
Michael J. Spencerb33594b2011-01-15 18:52:41 +0000804 return ec;
Michael J. Spencer628467e2010-12-28 01:49:01 +0000805 }
Michael J. Spencer628467e2010-12-28 01:49:01 +0000806
Michael J. Spencerb33594b2011-01-15 18:52:41 +0000807 result = Magic == Buffer;
David Blaikie58604cd2012-02-09 19:24:12 +0000808 return error_code::success();
Michael J. Spencer628467e2010-12-28 01:49:01 +0000809}
810
Michael J. Spencer5b082302011-12-13 23:17:12 +0000811/// @brief Identify the magic in magic.
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000812 file_magic identify_magic(StringRef Magic) {
813 if (Magic.size() < 4)
Michael J. Spencerb51c8e92012-06-19 05:29:57 +0000814 return file_magic::unknown;
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000815 switch ((unsigned char)Magic[0]) {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000816 case 0xDE: // 0x0B17C0DE = BC wraper
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000817 if (Magic[1] == (char)0xC0 && Magic[2] == (char)0x17 &&
818 Magic[3] == (char)0x0B)
Michael J. Spencer5b082302011-12-13 23:17:12 +0000819 return file_magic::bitcode;
820 break;
821 case 'B':
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000822 if (Magic[1] == 'C' && Magic[2] == (char)0xC0 && Magic[3] == (char)0xDE)
Michael J. Spencer5b082302011-12-13 23:17:12 +0000823 return file_magic::bitcode;
824 break;
825 case '!':
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000826 if (Magic.size() >= 8)
827 if (memcmp(Magic.data(),"!<arch>\n",8) == 0)
Michael J. Spencer5b082302011-12-13 23:17:12 +0000828 return file_magic::archive;
829 break;
830
831 case '\177':
Rafael Espindola01797062013-06-11 17:25:45 +0000832 if (Magic.size() >= 18 && Magic[1] == 'E' && Magic[2] == 'L' &&
833 Magic[3] == 'F') {
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000834 bool Data2MSB = Magic[5] == 2;
Michael J. Spencerc0a74e22013-04-05 20:10:04 +0000835 unsigned high = Data2MSB ? 16 : 17;
836 unsigned low = Data2MSB ? 17 : 16;
Rafael Espindola01797062013-06-11 17:25:45 +0000837 if (Magic[high] == 0)
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000838 switch (Magic[low]) {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000839 default: break;
840 case 1: return file_magic::elf_relocatable;
841 case 2: return file_magic::elf_executable;
842 case 3: return file_magic::elf_shared_object;
843 case 4: return file_magic::elf_core;
844 }
845 }
846 break;
847
848 case 0xCA:
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000849 if (Magic[1] == char(0xFE) && Magic[2] == char(0xBA) &&
850 Magic[3] == char(0xBE)) {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000851 // This is complicated by an overlap with Java class files.
852 // See the Mach-O section in /usr/share/file/magic for details.
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000853 if (Magic.size() >= 8 && Magic[7] < 43)
Alexey Samsonov9c22f872013-06-18 15:03:28 +0000854 return file_magic::macho_universal_binary;
Michael J. Spencer5b082302011-12-13 23:17:12 +0000855 }
856 break;
857
858 // The two magic numbers for mach-o are:
859 // 0xfeedface - 32-bit mach-o
860 // 0xfeedfacf - 64-bit mach-o
861 case 0xFE:
862 case 0xCE:
863 case 0xCF: {
864 uint16_t type = 0;
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000865 if (Magic[0] == char(0xFE) && Magic[1] == char(0xED) &&
866 Magic[2] == char(0xFA) &&
867 (Magic[3] == char(0xCE) || Magic[3] == char(0xCF))) {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000868 /* Native endian */
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000869 if (Magic.size() >= 16) type = Magic[14] << 8 | Magic[15];
870 } else if ((Magic[0] == char(0xCE) || Magic[0] == char(0xCF)) &&
871 Magic[1] == char(0xFA) && Magic[2] == char(0xED) &&
872 Magic[3] == char(0xFE)) {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000873 /* Reverse endian */
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000874 if (Magic.size() >= 14) type = Magic[13] << 8 | Magic[12];
Michael J. Spencer5b082302011-12-13 23:17:12 +0000875 }
876 switch (type) {
877 default: break;
878 case 1: return file_magic::macho_object;
879 case 2: return file_magic::macho_executable;
880 case 3: return file_magic::macho_fixed_virtual_memory_shared_lib;
881 case 4: return file_magic::macho_core;
Rafael Espindola6d315c62013-06-10 20:32:27 +0000882 case 5: return file_magic::macho_preload_executable;
Michael J. Spencer5b082302011-12-13 23:17:12 +0000883 case 6: return file_magic::macho_dynamically_linked_shared_lib;
884 case 7: return file_magic::macho_dynamic_linker;
885 case 8: return file_magic::macho_bundle;
886 case 9: return file_magic::macho_dynamic_linker;
887 case 10: return file_magic::macho_dsym_companion;
888 }
889 break;
890 }
891 case 0xF0: // PowerPC Windows
892 case 0x83: // Alpha 32-bit
893 case 0x84: // Alpha 64-bit
894 case 0x66: // MPS R4000 Windows
895 case 0x50: // mc68K
896 case 0x4c: // 80386 Windows
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000897 if (Magic[1] == 0x01)
Michael J. Spencer5b082302011-12-13 23:17:12 +0000898 return file_magic::coff_object;
899
900 case 0x90: // PA-RISC Windows
901 case 0x68: // mc68K Windows
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000902 if (Magic[1] == 0x02)
Michael J. Spencer5b082302011-12-13 23:17:12 +0000903 return file_magic::coff_object;
904 break;
905
906 case 0x4d: // Possible MS-DOS stub on Windows PE file
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000907 if (Magic[1] == 0x5a) {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000908 uint32_t off =
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000909 *reinterpret_cast<const support::ulittle32_t*>(Magic.data() + 0x3c);
Michael J. Spencer5b082302011-12-13 23:17:12 +0000910 // PE/COFF file, either EXE or DLL.
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000911 if (off < Magic.size() && memcmp(Magic.data() + off, "PE\0\0",4) == 0)
Michael J. Spencer5b082302011-12-13 23:17:12 +0000912 return file_magic::pecoff_executable;
913 }
914 break;
915
916 case 0x64: // x86-64 Windows.
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000917 if (Magic[1] == char(0x86))
Michael J. Spencer5b082302011-12-13 23:17:12 +0000918 return file_magic::coff_object;
919 break;
920
921 default:
922 break;
923 }
924 return file_magic::unknown;
925}
926
927error_code identify_magic(const Twine &path, file_magic &result) {
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000928 SmallString<32> Magic;
929 error_code ec = get_magic(path, Magic.capacity(), Magic);
930 if (ec && ec != errc::value_too_large)
931 return ec;
932
Michael J. Spencer5b082302011-12-13 23:17:12 +0000933 result = identify_magic(Magic);
David Blaikie58604cd2012-02-09 19:24:12 +0000934 return error_code::success();
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000935}
936
Michael J. Spencer7fd682a2011-01-05 16:39:38 +0000937namespace {
938error_code remove_all_r(StringRef path, file_type ft, uint32_t &count) {
939 if (ft == file_type::directory_file) {
940 // This code would be a lot better with exceptions ;/.
941 error_code ec;
Michael J. Spencer1dd2ee72011-12-12 06:03:33 +0000942 directory_iterator i(path, ec);
943 if (ec) return ec;
944 for (directory_iterator e; i != e; i.increment(ec)) {
Michael J. Spencer7fd682a2011-01-05 16:39:38 +0000945 if (ec) return ec;
946 file_status st;
947 if (error_code ec = i->status(st)) return ec;
948 if (error_code ec = remove_all_r(i->path(), st.type(), count)) return ec;
949 }
950 bool obviously_this_exists;
951 if (error_code ec = remove(path, obviously_this_exists)) return ec;
952 assert(obviously_this_exists);
953 ++count; // Include the directory itself in the items removed.
954 } else {
955 bool obviously_this_exists;
956 if (error_code ec = remove(path, obviously_this_exists)) return ec;
957 assert(obviously_this_exists);
958 ++count;
959 }
960
David Blaikie58604cd2012-02-09 19:24:12 +0000961 return error_code::success();
Michael J. Spencer7fd682a2011-01-05 16:39:38 +0000962}
Zhanyong Wan63cc3a82011-02-11 21:24:40 +0000963} // end unnamed namespace
Michael J. Spencer7fd682a2011-01-05 16:39:38 +0000964
965error_code remove_all(const Twine &path, uint32_t &num_removed) {
966 SmallString<128> path_storage;
967 StringRef p = path.toStringRef(path_storage);
968
969 file_status fs;
970 if (error_code ec = status(path, fs))
971 return ec;
972 num_removed = 0;
973 return remove_all_r(p, fs.type(), num_removed);
974}
975
Michael J. Spencer5bcdc7f2011-01-05 16:39:13 +0000976error_code directory_entry::status(file_status &result) const {
977 return fs::status(Path, result);
978}
979
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000980} // end namespace fs
981} // end namespace sys
982} // end namespace llvm
Michael J. Spencerdffde992010-11-29 22:28:51 +0000983
984// Include the truly platform-specific parts.
985#if defined(LLVM_ON_UNIX)
Rafael Espindola7a231f52013-06-26 19:33:03 +0000986#include "Unix/Path.inc"
Michael J. Spencerdffde992010-11-29 22:28:51 +0000987#endif
988#if defined(LLVM_ON_WIN32)
Rafael Espindola7a231f52013-06-26 19:33:03 +0000989#include "Windows/Path.inc"
Michael J. Spencerdffde992010-11-29 22:28:51 +0000990#endif