blob: 0fb7666aeba749c8f95e9695850758ad44cd0c56 [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 Espindolac1b49b52013-07-16 19:44:17 +000021#include <fcntl.h>
Rafael Espindola8ee23f02013-06-18 17:01:00 +000022
23#if !defined(_MSC_VER) && !defined(__MINGW32__)
Douglas Gregorb0b5bde2013-03-21 21:46:10 +000024#include <unistd.h>
Rafael Espindola8ee23f02013-06-18 17:01:00 +000025#else
26#include <io.h>
Douglas Gregorb0b5bde2013-03-21 21:46:10 +000027#endif
Michael J. Spencerdffde992010-11-29 22:28:51 +000028
29namespace {
30 using llvm::StringRef;
Zhanyong Wan63cc3a82011-02-11 21:24:40 +000031 using llvm::sys::path::is_separator;
Michael J. Spencerdffde992010-11-29 22:28:51 +000032
33#ifdef LLVM_ON_WIN32
Benjamin Kramer80fd2a12012-02-08 13:13:47 +000034 const char *separators = "\\/";
35 const char prefered_separator = '\\';
Michael J. Spencerdffde992010-11-29 22:28:51 +000036#else
Benjamin Kramer80fd2a12012-02-08 13:13:47 +000037 const char separators = '/';
38 const char prefered_separator = '/';
Michael J. Spencerdffde992010-11-29 22:28:51 +000039#endif
40
Benjamin Kramer7fb86662010-12-17 18:59:09 +000041 StringRef find_first_component(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000042 // Look for this first component in the following order.
43 // * empty (in this case we return an empty string)
44 // * either C: or {//,\\}net.
45 // * {/,\}
46 // * {.,..}
47 // * {file,directory}name
48
49 if (path.empty())
50 return path;
51
Michael J. Spencerca3a3392010-12-07 03:57:48 +000052#ifdef LLVM_ON_WIN32
Michael J. Spencerdffde992010-11-29 22:28:51 +000053 // C:
Guy Benyei87d0b9e2013-02-12 21:21:59 +000054 if (path.size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) &&
55 path[1] == ':')
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000056 return path.substr(0, 2);
Michael J. Spencerca3a3392010-12-07 03:57:48 +000057#endif
Michael J. Spencerdffde992010-11-29 22:28:51 +000058
59 // //net
60 if ((path.size() > 2) &&
Michael J. Spencerca3a3392010-12-07 03:57:48 +000061 is_separator(path[0]) &&
62 path[0] == path[1] &&
63 !is_separator(path[2])) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000064 // Find the next directory separator.
Michael J. Spencerca3a3392010-12-07 03:57:48 +000065 size_t end = path.find_first_of(separators, 2);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000066 return path.substr(0, end);
Michael J. Spencerdffde992010-11-29 22:28:51 +000067 }
68
69 // {/,\}
Michael J. Spencerca3a3392010-12-07 03:57:48 +000070 if (is_separator(path[0]))
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000071 return path.substr(0, 1);
Michael J. Spencerdffde992010-11-29 22:28:51 +000072
73 if (path.startswith(".."))
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000074 return path.substr(0, 2);
Michael J. Spencerdffde992010-11-29 22:28:51 +000075
76 if (path[0] == '.')
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000077 return path.substr(0, 1);
Michael J. Spencerdffde992010-11-29 22:28:51 +000078
79 // * {file,directory}name
Michael J. Spencerca3a3392010-12-07 03:57:48 +000080 size_t end = path.find_first_of(separators, 2);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +000081 return path.substr(0, end);
Michael J. Spencerdffde992010-11-29 22:28:51 +000082 }
Michael J. Spencera42cf732010-11-30 23:28:07 +000083
Benjamin Kramerd53f3c82010-12-17 18:19:06 +000084 size_t filename_pos(StringRef str) {
Michael J. Spencera42cf732010-11-30 23:28:07 +000085 if (str.size() == 2 &&
86 is_separator(str[0]) &&
Michael J. Spencerca3a3392010-12-07 03:57:48 +000087 str[0] == str[1])
Michael J. Spencera42cf732010-11-30 23:28:07 +000088 return 0;
89
90 if (str.size() > 0 && is_separator(str[str.size() - 1]))
91 return str.size() - 1;
92
93 size_t pos = str.find_last_of(separators, str.size() - 1);
94
95#ifdef LLVM_ON_WIN32
96 if (pos == StringRef::npos)
97 pos = str.find_last_of(':', str.size() - 2);
98#endif
99
100 if (pos == StringRef::npos ||
101 (pos == 1 && is_separator(str[0])))
102 return 0;
103
104 return pos + 1;
105 }
106
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000107 size_t root_dir_start(StringRef str) {
Michael J. Spencera42cf732010-11-30 23:28:07 +0000108 // case "c:/"
109#ifdef LLVM_ON_WIN32
110 if (str.size() > 2 &&
111 str[1] == ':' &&
112 is_separator(str[2]))
113 return 2;
114#endif
115
116 // case "//"
117 if (str.size() == 2 &&
118 is_separator(str[0]) &&
119 str[0] == str[1])
120 return StringRef::npos;
121
122 // case "//net"
123 if (str.size() > 3 &&
124 is_separator(str[0]) &&
125 str[0] == str[1] &&
126 !is_separator(str[2])) {
127 return str.find_first_of(separators, 2);
128 }
129
130 // case "/"
131 if (str.size() > 0 && is_separator(str[0]))
132 return 0;
133
134 return StringRef::npos;
135 }
136
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000137 size_t parent_path_end(StringRef path) {
Michael J. Spencera42cf732010-11-30 23:28:07 +0000138 size_t end_pos = filename_pos(path);
139
140 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
141
142 // Skip separators except for root dir.
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000143 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
Michael J. Spencera42cf732010-11-30 23:28:07 +0000144
145 while(end_pos > 0 &&
146 (end_pos - 1) != root_dir_pos &&
147 is_separator(path[end_pos - 1]))
148 --end_pos;
149
150 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
151 return StringRef::npos;
152
153 return end_pos;
154 }
Zhanyong Wan63cc3a82011-02-11 21:24:40 +0000155} // end unnamed namespace
Michael J. Spencerdffde992010-11-29 22:28:51 +0000156
Rafael Espindola8e7294f2013-06-28 03:48:47 +0000157enum FSEntity {
158 FS_Dir,
159 FS_File,
160 FS_Name
161};
162
163// Implemented in Unix/Path.inc and Windows/Path.inc.
164static llvm::error_code
165createUniqueEntity(const llvm::Twine &Model, int &ResultFD,
166 llvm::SmallVectorImpl<char> &ResultPath,
167 bool MakeAbsolute, unsigned Mode, FSEntity Type);
168
Michael J. Spencerdffde992010-11-29 22:28:51 +0000169namespace llvm {
170namespace sys {
171namespace path {
172
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000173const_iterator begin(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000174 const_iterator i;
175 i.Path = path;
176 i.Component = find_first_component(path);
177 i.Position = 0;
178 return i;
179}
180
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000181const_iterator end(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000182 const_iterator i;
183 i.Path = path;
184 i.Position = path.size();
185 return i;
186}
187
Michael J. Spencerdffde992010-11-29 22:28:51 +0000188const_iterator &const_iterator::operator++() {
189 assert(Position < Path.size() && "Tried to increment past end!");
190
191 // Increment Position to past the current component
192 Position += Component.size();
193
194 // Check for end.
195 if (Position == Path.size()) {
196 Component = StringRef();
197 return *this;
198 }
199
200 // Both POSIX and Windows treat paths that begin with exactly two separators
201 // specially.
202 bool was_net = Component.size() > 2 &&
203 is_separator(Component[0]) &&
204 Component[1] == Component[0] &&
205 !is_separator(Component[2]);
206
207 // Handle separators.
208 if (is_separator(Path[Position])) {
209 // Root dir.
210 if (was_net
211#ifdef LLVM_ON_WIN32
212 // c:/
213 || Component.endswith(":")
214#endif
215 ) {
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000216 Component = Path.substr(Position, 1);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000217 return *this;
218 }
219
220 // Skip extra separators.
221 while (Position != Path.size() &&
222 is_separator(Path[Position])) {
223 ++Position;
224 }
225
226 // Treat trailing '/' as a '.'.
227 if (Position == Path.size()) {
228 --Position;
229 Component = ".";
230 return *this;
231 }
232 }
233
234 // Find next component.
235 size_t end_pos = Path.find_first_of(separators, Position);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000236 Component = Path.slice(Position, end_pos);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000237
238 return *this;
239}
240
Michael J. Spencera42cf732010-11-30 23:28:07 +0000241const_iterator &const_iterator::operator--() {
242 // If we're at the end and the previous char was a '/', return '.'.
243 if (Position == Path.size() &&
244 Path.size() > 1 &&
245 is_separator(Path[Position - 1])
246#ifdef LLVM_ON_WIN32
247 && Path[Position - 2] != ':'
248#endif
249 ) {
250 --Position;
251 Component = ".";
252 return *this;
253 }
254
255 // Skip separators unless it's the root directory.
256 size_t root_dir_pos = root_dir_start(Path);
257 size_t end_pos = Position;
258
259 while(end_pos > 0 &&
260 (end_pos - 1) != root_dir_pos &&
261 is_separator(Path[end_pos - 1]))
262 --end_pos;
263
264 // Find next separator.
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000265 size_t start_pos = filename_pos(Path.substr(0, end_pos));
266 Component = Path.slice(start_pos, end_pos);
Michael J. Spencera42cf732010-11-30 23:28:07 +0000267 Position = start_pos;
268 return *this;
269}
270
Michael J. Spencerdffde992010-11-29 22:28:51 +0000271bool const_iterator::operator==(const const_iterator &RHS) const {
272 return Path.begin() == RHS.Path.begin() &&
273 Position == RHS.Position;
274}
275
276bool const_iterator::operator!=(const const_iterator &RHS) const {
277 return !(*this == RHS);
278}
279
Michael J. Spencera42cf732010-11-30 23:28:07 +0000280ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
281 return Position - RHS.Position;
282}
283
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000284const StringRef root_path(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000285 const_iterator b = begin(path),
286 pos = b,
287 e = end(path);
288 if (b != e) {
289 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
290 bool has_drive =
291#ifdef LLVM_ON_WIN32
292 b->endswith(":");
293#else
294 false;
295#endif
296
297 if (has_net || has_drive) {
298 if ((++pos != e) && is_separator((*pos)[0])) {
299 // {C:/,//net/}, so get the first two components.
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000300 return path.substr(0, b->size() + pos->size());
Michael J. Spencerdffde992010-11-29 22:28:51 +0000301 } else {
302 // just {C:,//net}, return the first component.
Michael J. Spencer50291592010-12-07 17:04:04 +0000303 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000304 }
305 }
306
307 // POSIX style root directory.
308 if (is_separator((*b)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000309 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000310 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000311 }
312
Michael J. Spencer50291592010-12-07 17:04:04 +0000313 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000314}
315
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000316const StringRef root_name(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000317 const_iterator b = begin(path),
318 e = end(path);
319 if (b != e) {
320 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
321 bool has_drive =
322#ifdef LLVM_ON_WIN32
323 b->endswith(":");
324#else
325 false;
326#endif
327
328 if (has_net || has_drive) {
329 // just {C:,//net}, return the first component.
Michael J. Spencer50291592010-12-07 17:04:04 +0000330 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000331 }
332 }
333
334 // No path or no name.
Michael J. Spencer50291592010-12-07 17:04:04 +0000335 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000336}
337
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000338const StringRef root_directory(StringRef path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000339 const_iterator b = begin(path),
340 pos = b,
341 e = end(path);
342 if (b != e) {
343 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
344 bool has_drive =
345#ifdef LLVM_ON_WIN32
346 b->endswith(":");
347#else
348 false;
349#endif
350
351 if ((has_net || has_drive) &&
352 // {C:,//net}, skip to the next component.
353 (++pos != e) && is_separator((*pos)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000354 return *pos;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000355 }
356
357 // POSIX style root directory.
358 if (!has_net && is_separator((*b)[0])) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000359 return *b;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000360 }
361 }
362
363 // No path or no root.
Michael J. Spencer50291592010-12-07 17:04:04 +0000364 return StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000365}
366
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000367const StringRef relative_path(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000368 StringRef root = root_path(path);
Michael J. Spencerd0fff182012-02-29 00:06:24 +0000369 return path.substr(root.size());
Michael J. Spencerdffde992010-11-29 22:28:51 +0000370}
371
Michael J. Spencer936671b2010-12-07 03:57:37 +0000372void append(SmallVectorImpl<char> &path, const Twine &a,
373 const Twine &b,
374 const Twine &c,
375 const Twine &d) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000376 SmallString<32> a_storage;
377 SmallString<32> b_storage;
378 SmallString<32> c_storage;
379 SmallString<32> d_storage;
380
381 SmallVector<StringRef, 4> components;
382 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
383 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
384 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
385 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
386
387 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
388 e = components.end();
389 i != e; ++i) {
390 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
391 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
Michael J. Spencer50291592010-12-07 17:04:04 +0000392 bool is_root_name = has_root_name(*i);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000393
394 if (path_has_sep) {
395 // Strip separators from beginning of component.
396 size_t loc = i->find_first_not_of(separators);
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000397 StringRef c = i->substr(loc);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000398
399 // Append it.
400 path.append(c.begin(), c.end());
401 continue;
402 }
403
Michael J. Spencerf150e762010-12-06 04:28:23 +0000404 if (!component_has_sep && !(path.empty() || is_root_name)) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000405 // Add a separator.
406 path.push_back(prefered_separator);
407 }
408
409 path.append(i->begin(), i->end());
410 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000411}
412
Argyrios Kyrtzidis6e31b9b2011-02-15 17:51:19 +0000413void append(SmallVectorImpl<char> &path,
414 const_iterator begin, const_iterator end) {
415 for (; begin != end; ++begin)
416 path::append(path, *begin);
417}
418
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000419const StringRef parent_path(StringRef path) {
Michael J. Spencera42cf732010-11-30 23:28:07 +0000420 size_t end_pos = parent_path_end(path);
421 if (end_pos == StringRef::npos)
Michael J. Spencer50291592010-12-07 17:04:04 +0000422 return StringRef();
Michael J. Spencera42cf732010-11-30 23:28:07 +0000423 else
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000424 return path.substr(0, end_pos);
Michael J. Spencera42cf732010-11-30 23:28:07 +0000425}
426
Michael J. Spencer936671b2010-12-07 03:57:37 +0000427void remove_filename(SmallVectorImpl<char> &path) {
Michael J. Spencerdbfb56b2010-12-01 00:52:28 +0000428 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
Michael J. Spencer936671b2010-12-07 03:57:37 +0000429 if (end_pos != StringRef::npos)
430 path.set_size(end_pos);
Michael J. Spencerdbfb56b2010-12-01 00:52:28 +0000431}
432
Michael J. Spencer50291592010-12-07 17:04:04 +0000433void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
Michael J. Spencer52ed8672010-12-01 00:52:55 +0000434 StringRef p(path.begin(), path.size());
435 SmallString<32> ext_storage;
436 StringRef ext = extension.toStringRef(ext_storage);
437
438 // Erase existing extension.
439 size_t pos = p.find_last_of('.');
440 if (pos != StringRef::npos && pos >= filename_pos(p))
441 path.set_size(pos);
442
443 // Append '.' if needed.
444 if (ext.size() > 0 && ext[0] != '.')
445 path.push_back('.');
446
447 // Append extension.
448 path.append(ext.begin(), ext.end());
Michael J. Spencer52ed8672010-12-01 00:52:55 +0000449}
450
Michael J. Spencer936671b2010-12-07 03:57:37 +0000451void native(const Twine &path, SmallVectorImpl<char> &result) {
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000452 // Clear result.
Michael J. Spencerfbd1bbd2010-12-07 01:23:19 +0000453 result.clear();
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000454#ifdef LLVM_ON_WIN32
455 SmallString<128> path_storage;
456 StringRef p = path.toStringRef(path_storage);
457 result.reserve(p.size());
458 for (StringRef::const_iterator i = p.begin(),
459 e = p.end();
460 i != e;
461 ++i) {
462 if (*i == '/')
463 result.push_back('\\');
464 else
465 result.push_back(*i);
466 }
467#else
468 path.toVector(result);
469#endif
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000470}
471
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000472const StringRef filename(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000473 return *(--end(path));
Michael J. Spencera9793552010-12-01 03:18:17 +0000474}
475
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000476const StringRef stem(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000477 StringRef fname = filename(path);
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000478 size_t pos = fname.find_last_of('.');
479 if (pos == StringRef::npos)
Michael J. Spencer50291592010-12-07 17:04:04 +0000480 return fname;
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000481 else
482 if ((fname.size() == 1 && fname == ".") ||
483 (fname.size() == 2 && fname == ".."))
Michael J. Spencer50291592010-12-07 17:04:04 +0000484 return fname;
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000485 else
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000486 return fname.substr(0, pos);
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000487}
488
Benjamin Kramerd53f3c82010-12-17 18:19:06 +0000489const StringRef extension(StringRef path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000490 StringRef fname = filename(path);
Michael J. Spencer5265f222010-12-01 03:37:41 +0000491 size_t pos = fname.find_last_of('.');
492 if (pos == StringRef::npos)
Michael J. Spencer50291592010-12-07 17:04:04 +0000493 return StringRef();
Michael J. Spencer5265f222010-12-01 03:37:41 +0000494 else
495 if ((fname.size() == 1 && fname == ".") ||
496 (fname.size() == 2 && fname == ".."))
Michael J. Spencer50291592010-12-07 17:04:04 +0000497 return StringRef();
Michael J. Spencer5265f222010-12-01 03:37:41 +0000498 else
Benjamin Kramer3ce88c92010-12-17 20:27:37 +0000499 return fname.substr(pos);
Michael J. Spencer5265f222010-12-01 03:37:41 +0000500}
501
Zhanyong Wan63cc3a82011-02-11 21:24:40 +0000502bool is_separator(char value) {
503 switch(value) {
504#ifdef LLVM_ON_WIN32
505 case '\\': // fall through
506#endif
507 case '/': return true;
508 default: return false;
509 }
510}
511
Douglas Gregor55cf8152011-09-14 20:27:01 +0000512void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result) {
513 result.clear();
Michael J. Spencerfaebf112011-12-13 23:16:15 +0000514
Douglas Gregorb0b5bde2013-03-21 21:46:10 +0000515#ifdef __APPLE__
516 // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR.
517 int ConfName = erasedOnReboot? _CS_DARWIN_USER_TEMP_DIR
518 : _CS_DARWIN_USER_CACHE_DIR;
519 size_t ConfLen = confstr(ConfName, 0, 0);
520 if (ConfLen > 0) {
521 do {
522 result.resize(ConfLen);
523 ConfLen = confstr(ConfName, result.data(), result.size());
524 } while (ConfLen > 0 && ConfLen != result.size());
525
526 if (ConfLen > 0) {
527 assert(result.back() == 0);
528 result.pop_back();
529 return;
530 }
531
532 result.clear();
533 }
534#endif
535
Douglas Gregor55cf8152011-09-14 20:27:01 +0000536 // Check whether the temporary directory is specified by an environment
537 // variable.
538 const char *EnvironmentVariable;
539#ifdef LLVM_ON_WIN32
540 EnvironmentVariable = "TEMP";
541#else
542 EnvironmentVariable = "TMPDIR";
543#endif
544 if (char *RequestedDir = getenv(EnvironmentVariable)) {
545 result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
546 return;
547 }
Michael J. Spencerfaebf112011-12-13 23:16:15 +0000548
Douglas Gregor55cf8152011-09-14 20:27:01 +0000549 // Fall back to a system default.
550 const char *DefaultResult;
551#ifdef LLVM_ON_WIN32
552 (void)erasedOnReboot;
Douglas Gregord26d6b62011-09-14 23:21:47 +0000553 DefaultResult = "C:\\TEMP";
Douglas Gregor55cf8152011-09-14 20:27:01 +0000554#else
555 if (erasedOnReboot)
556 DefaultResult = "/tmp";
557 else
558 DefaultResult = "/var/tmp";
559#endif
560 result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
561}
Michael J. Spencerfaebf112011-12-13 23:16:15 +0000562
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000563bool has_root_name(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000564 SmallString<128> path_storage;
565 StringRef p = path.toStringRef(path_storage);
566
Michael J. Spencer50291592010-12-07 17:04:04 +0000567 return !root_name(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000568}
569
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000570bool has_root_directory(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000571 SmallString<128> path_storage;
572 StringRef p = path.toStringRef(path_storage);
573
Michael J. Spencer50291592010-12-07 17:04:04 +0000574 return !root_directory(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000575}
576
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000577bool has_root_path(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000578 SmallString<128> path_storage;
579 StringRef p = path.toStringRef(path_storage);
580
Michael J. Spencer50291592010-12-07 17:04:04 +0000581 return !root_path(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000582}
583
Michael J. Spencer2d484eb2010-12-20 13:30:28 +0000584bool has_relative_path(const Twine &path) {
585 SmallString<128> path_storage;
586 StringRef p = path.toStringRef(path_storage);
587
588 return !relative_path(p).empty();
589}
590
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000591bool has_filename(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000592 SmallString<128> path_storage;
593 StringRef p = path.toStringRef(path_storage);
594
Michael J. Spencer50291592010-12-07 17:04:04 +0000595 return !filename(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000596}
597
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000598bool has_parent_path(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000599 SmallString<128> path_storage;
600 StringRef p = path.toStringRef(path_storage);
601
Michael J. Spencer50291592010-12-07 17:04:04 +0000602 return !parent_path(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000603}
604
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000605bool has_stem(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000606 SmallString<128> path_storage;
607 StringRef p = path.toStringRef(path_storage);
608
Michael J. Spencer50291592010-12-07 17:04:04 +0000609 return !stem(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000610}
611
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000612bool has_extension(const Twine &path) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000613 SmallString<128> path_storage;
614 StringRef p = path.toStringRef(path_storage);
615
Michael J. Spencer50291592010-12-07 17:04:04 +0000616 return !extension(p).empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000617}
618
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000619bool is_absolute(const Twine &path) {
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000620 SmallString<128> path_storage;
621 StringRef p = path.toStringRef(path_storage);
622
Michael J. Spencer50291592010-12-07 17:04:04 +0000623 bool rootDir = has_root_directory(p),
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000624#ifdef LLVM_ON_WIN32
Michael J. Spencer50291592010-12-07 17:04:04 +0000625 rootName = has_root_name(p);
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000626#else
Michael J. Spencer50291592010-12-07 17:04:04 +0000627 rootName = true;
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000628#endif
629
Michael J. Spencer50291592010-12-07 17:04:04 +0000630 return rootDir && rootName;
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000631}
632
Michael J. Spencer12ccf672010-12-07 18:12:07 +0000633bool is_relative(const Twine &path) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000634 return !is_absolute(path);
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000635}
636
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000637} // end namespace path
638
639namespace fs {
640
Rafael Espindola200c7482013-07-05 21:01:08 +0000641error_code createUniqueFile(const Twine &Model, int &ResultFd,
642 SmallVectorImpl<char> &ResultPath, unsigned Mode) {
643 return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
644}
645
646error_code createUniqueFile(const Twine &Model,
647 SmallVectorImpl<char> &ResultPath) {
648 int Dummy;
649 return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
650}
651
Rafael Espindolae11874f2013-07-05 19:56:49 +0000652static error_code createTemporaryFile(const Twine &Model, int &ResultFD,
653 llvm::SmallVectorImpl<char> &ResultPath,
654 FSEntity Type) {
655 SmallString<128> Storage;
656 StringRef P = Model.toNullTerminatedStringRef(Storage);
657 assert(P.find_first_of(separators) == StringRef::npos &&
658 "Model must be a simple filename.");
659 // Use P.begin() so that createUniqueEntity doesn't need to recreate Storage.
660 return createUniqueEntity(P.begin(), ResultFD, ResultPath,
661 true, owner_read | owner_write, Type);
662}
663
664static error_code
665createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
666 llvm::SmallVectorImpl<char> &ResultPath,
667 FSEntity Type) {
668 return createTemporaryFile(Prefix + "-%%%%%%." + Suffix, ResultFD, ResultPath,
669 Type);
670}
671
672
673error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
674 int &ResultFD,
675 SmallVectorImpl<char> &ResultPath) {
676 return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File);
677}
678
679error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
680 SmallVectorImpl<char> &ResultPath) {
681 int Dummy;
682 return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
683}
684
685
Rafael Espindola8e7294f2013-06-28 03:48:47 +0000686// This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
Rafael Espindola41574662013-06-28 10:55:41 +0000687// for consistency. We should try using mkdtemp.
Rafael Espindola08ddd122013-06-27 03:45:31 +0000688error_code createUniqueDirectory(const Twine &Prefix,
689 SmallVectorImpl<char> &ResultPath) {
Rafael Espindola8e7294f2013-06-28 03:48:47 +0000690 int Dummy;
691 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath,
692 true, 0, FS_Dir);
Rafael Espindola08ddd122013-06-27 03:45:31 +0000693}
694
Rafael Espindolac1b49b52013-07-16 19:44:17 +0000695error_code openFileForWrite(const Twine &Name, int &ResultFD,
696 sys::fs::OpenFlags Flags, unsigned Mode) {
697 // Verify that we don't have both "append" and "excl".
698 assert((!(Flags & sys::fs::F_Excl) || !(Flags & sys::fs::F_Append)) &&
699 "Cannot specify both 'excl' and 'append' file creation flags!");
700
701 int OpenFlags = O_WRONLY | O_CREAT;
702
703#ifdef O_BINARY
704 if (Flags & F_Binary)
705 OpenFlags |= O_BINARY;
706#endif
707
708 if (Flags & F_Append)
709 OpenFlags |= O_APPEND;
710 else
711 OpenFlags |= O_TRUNC;
712
713 if (Flags & F_Excl)
714 OpenFlags |= O_EXCL;
715
716 SmallString<128> Storage;
717 StringRef P = Name.toNullTerminatedStringRef(Storage);
718 while ((ResultFD = open(P.begin(), OpenFlags, Mode)) < 0) {
719 if (errno != EINTR)
720 return error_code(errno, system_category());
721 }
722 return error_code::success();
723}
724
725error_code openFileForRead(const Twine &Name, int &ResultFD) {
726 int OpenFlags = O_RDONLY;
727#ifdef O_BINARY
728 OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
729#endif
730
731 SmallString<128> Storage;
732 StringRef P = Name.toNullTerminatedStringRef(Storage);
733 while ((ResultFD = open(P.begin(), OpenFlags)) < 0) {
734 if (errno != EINTR)
735 return error_code(errno, system_category());
736 }
737 return error_code::success();
738}
739
Michael J. Spenceree271d82010-12-07 03:57:17 +0000740error_code make_absolute(SmallVectorImpl<char> &path) {
741 StringRef p(path.data(), path.size());
742
Daniel Dunbard1d72162012-02-29 00:20:37 +0000743 bool rootDirectory = path::has_root_directory(p),
744#ifdef LLVM_ON_WIN32
Daniel Dunbar95fe7b92012-02-29 00:46:46 +0000745 rootName = path::has_root_name(p);
Daniel Dunbard1d72162012-02-29 00:20:37 +0000746#else
747 rootName = true;
748#endif
Michael J. Spenceree271d82010-12-07 03:57:17 +0000749
750 // Already absolute.
751 if (rootName && rootDirectory)
David Blaikie58604cd2012-02-09 19:24:12 +0000752 return error_code::success();
Michael J. Spenceree271d82010-12-07 03:57:17 +0000753
754 // All of the following conditions will need the current directory.
755 SmallString<128> current_dir;
756 if (error_code ec = current_path(current_dir)) return ec;
757
758 // Relative path. Prepend the current directory.
759 if (!rootName && !rootDirectory) {
760 // Append path to the current directory.
Michael J. Spencer936671b2010-12-07 03:57:37 +0000761 path::append(current_dir, p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000762 // Set path to the result.
763 path.swap(current_dir);
David Blaikie58604cd2012-02-09 19:24:12 +0000764 return error_code::success();
Michael J. Spenceree271d82010-12-07 03:57:17 +0000765 }
766
767 if (!rootName && rootDirectory) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000768 StringRef cdrn = path::root_name(current_dir);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000769 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
Michael J. Spencer936671b2010-12-07 03:57:37 +0000770 path::append(curDirRootName, p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000771 // Set path to the result.
772 path.swap(curDirRootName);
David Blaikie58604cd2012-02-09 19:24:12 +0000773 return error_code::success();
Michael J. Spenceree271d82010-12-07 03:57:17 +0000774 }
775
776 if (rootName && !rootDirectory) {
Michael J. Spencer50291592010-12-07 17:04:04 +0000777 StringRef pRootName = path::root_name(p);
778 StringRef bRootDirectory = path::root_directory(current_dir);
779 StringRef bRelativePath = path::relative_path(current_dir);
780 StringRef pRelativePath = path::relative_path(p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000781
782 SmallString<128> res;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000783 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000784 path.swap(res);
David Blaikie58604cd2012-02-09 19:24:12 +0000785 return error_code::success();
Michael J. Spenceree271d82010-12-07 03:57:17 +0000786 }
787
788 llvm_unreachable("All rootName and rootDirectory combinations should have "
789 "occurred above!");
790}
791
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000792error_code create_directories(const Twine &path, bool &existed) {
793 SmallString<128> path_storage;
794 StringRef p = path.toStringRef(path_storage);
795
Michael J. Spencer50291592010-12-07 17:04:04 +0000796 StringRef parent = path::parent_path(p);
Michael J. Spencer55f43d62012-03-21 23:09:14 +0000797 if (!parent.empty()) {
798 bool parent_exists;
799 if (error_code ec = fs::exists(parent, parent_exists)) return ec;
Michael J. Spencer50291592010-12-07 17:04:04 +0000800
Michael J. Spencer55f43d62012-03-21 23:09:14 +0000801 if (!parent_exists)
802 if (error_code ec = create_directories(parent, existed)) return ec;
803 }
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000804
805 return create_directory(p, existed);
806}
807
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000808bool exists(file_status status) {
809 return status_known(status) && status.type() != file_type::file_not_found;
810}
811
812bool status_known(file_status s) {
813 return s.type() != file_type::status_error;
814}
815
816bool is_directory(file_status status) {
817 return status.type() == file_type::directory_file;
818}
819
Michael J. Spencer218dc3e2011-01-11 01:21:55 +0000820error_code is_directory(const Twine &path, bool &result) {
821 file_status st;
822 if (error_code ec = status(path, st))
823 return ec;
824 result = is_directory(st);
David Blaikie58604cd2012-02-09 19:24:12 +0000825 return error_code::success();
Michael J. Spencer218dc3e2011-01-11 01:21:55 +0000826}
827
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000828bool is_regular_file(file_status status) {
829 return status.type() == file_type::regular_file;
830}
831
Michael J. Spencer218dc3e2011-01-11 01:21:55 +0000832error_code is_regular_file(const Twine &path, bool &result) {
833 file_status st;
834 if (error_code ec = status(path, st))
835 return ec;
836 result = is_regular_file(st);
David Blaikie58604cd2012-02-09 19:24:12 +0000837 return error_code::success();
Michael J. Spencer218dc3e2011-01-11 01:21:55 +0000838}
839
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000840bool is_symlink(file_status status) {
841 return status.type() == file_type::symlink_file;
842}
843
Michael J. Spencer9df536c2011-01-12 23:55:06 +0000844error_code is_symlink(const Twine &path, bool &result) {
845 file_status st;
846 if (error_code ec = status(path, st))
847 return ec;
848 result = is_symlink(st);
David Blaikie58604cd2012-02-09 19:24:12 +0000849 return error_code::success();
Michael J. Spencer9df536c2011-01-12 23:55:06 +0000850}
851
Michael J. Spencer61187dd2010-12-09 17:37:02 +0000852bool is_other(file_status status) {
853 return exists(status) &&
854 !is_regular_file(status) &&
855 !is_directory(status) &&
856 !is_symlink(status);
857}
858
Benjamin Kramer357b5712011-09-14 01:14:36 +0000859void directory_entry::replace_filename(const Twine &filename, file_status st) {
Michael J. Spencer753cbbb2010-12-06 04:28:42 +0000860 SmallString<128> path(Path.begin(), Path.end());
861 path::remove_filename(path);
862 path::append(path, filename);
863 Path = path.str();
864 Status = st;
Michael J. Spencer753cbbb2010-12-06 04:28:42 +0000865}
866
Michael J. Spencer628467e2010-12-28 01:49:01 +0000867error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
Michael J. Spencer628467e2010-12-28 01:49:01 +0000868 SmallString<32> MagicStorage;
Michael J. Spencerb33594b2011-01-15 18:52:41 +0000869 StringRef Magic = magic.toStringRef(MagicStorage);
870 SmallString<32> Buffer;
Michael J. Spencer628467e2010-12-28 01:49:01 +0000871
Michael J. Spencerb33594b2011-01-15 18:52:41 +0000872 if (error_code ec = get_magic(path, Magic.size(), Buffer)) {
873 if (ec == errc::value_too_large) {
874 // Magic.size() > file_size(Path).
Michael J. Spencer628467e2010-12-28 01:49:01 +0000875 result = false;
David Blaikie58604cd2012-02-09 19:24:12 +0000876 return error_code::success();
Michael J. Spencer628467e2010-12-28 01:49:01 +0000877 }
Michael J. Spencerb33594b2011-01-15 18:52:41 +0000878 return ec;
Michael J. Spencer628467e2010-12-28 01:49:01 +0000879 }
Michael J. Spencer628467e2010-12-28 01:49:01 +0000880
Michael J. Spencerb33594b2011-01-15 18:52:41 +0000881 result = Magic == Buffer;
David Blaikie58604cd2012-02-09 19:24:12 +0000882 return error_code::success();
Michael J. Spencer628467e2010-12-28 01:49:01 +0000883}
884
Michael J. Spencer5b082302011-12-13 23:17:12 +0000885/// @brief Identify the magic in magic.
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000886 file_magic identify_magic(StringRef Magic) {
887 if (Magic.size() < 4)
Michael J. Spencerb51c8e92012-06-19 05:29:57 +0000888 return file_magic::unknown;
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000889 switch ((unsigned char)Magic[0]) {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000890 case 0xDE: // 0x0B17C0DE = BC wraper
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000891 if (Magic[1] == (char)0xC0 && Magic[2] == (char)0x17 &&
892 Magic[3] == (char)0x0B)
Michael J. Spencer5b082302011-12-13 23:17:12 +0000893 return file_magic::bitcode;
894 break;
895 case 'B':
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000896 if (Magic[1] == 'C' && Magic[2] == (char)0xC0 && Magic[3] == (char)0xDE)
Michael J. Spencer5b082302011-12-13 23:17:12 +0000897 return file_magic::bitcode;
898 break;
899 case '!':
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000900 if (Magic.size() >= 8)
901 if (memcmp(Magic.data(),"!<arch>\n",8) == 0)
Michael J. Spencer5b082302011-12-13 23:17:12 +0000902 return file_magic::archive;
903 break;
904
905 case '\177':
Rafael Espindola01797062013-06-11 17:25:45 +0000906 if (Magic.size() >= 18 && Magic[1] == 'E' && Magic[2] == 'L' &&
907 Magic[3] == 'F') {
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000908 bool Data2MSB = Magic[5] == 2;
Michael J. Spencerc0a74e22013-04-05 20:10:04 +0000909 unsigned high = Data2MSB ? 16 : 17;
910 unsigned low = Data2MSB ? 17 : 16;
Rafael Espindola01797062013-06-11 17:25:45 +0000911 if (Magic[high] == 0)
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000912 switch (Magic[low]) {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000913 default: break;
914 case 1: return file_magic::elf_relocatable;
915 case 2: return file_magic::elf_executable;
916 case 3: return file_magic::elf_shared_object;
917 case 4: return file_magic::elf_core;
918 }
919 }
920 break;
921
922 case 0xCA:
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000923 if (Magic[1] == char(0xFE) && Magic[2] == char(0xBA) &&
924 Magic[3] == char(0xBE)) {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000925 // This is complicated by an overlap with Java class files.
926 // See the Mach-O section in /usr/share/file/magic for details.
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000927 if (Magic.size() >= 8 && Magic[7] < 43)
Alexey Samsonov9c22f872013-06-18 15:03:28 +0000928 return file_magic::macho_universal_binary;
Michael J. Spencer5b082302011-12-13 23:17:12 +0000929 }
930 break;
931
932 // The two magic numbers for mach-o are:
933 // 0xfeedface - 32-bit mach-o
934 // 0xfeedfacf - 64-bit mach-o
935 case 0xFE:
936 case 0xCE:
937 case 0xCF: {
938 uint16_t type = 0;
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000939 if (Magic[0] == char(0xFE) && Magic[1] == char(0xED) &&
940 Magic[2] == char(0xFA) &&
941 (Magic[3] == char(0xCE) || Magic[3] == char(0xCF))) {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000942 /* Native endian */
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000943 if (Magic.size() >= 16) type = Magic[14] << 8 | Magic[15];
944 } else if ((Magic[0] == char(0xCE) || Magic[0] == char(0xCF)) &&
945 Magic[1] == char(0xFA) && Magic[2] == char(0xED) &&
946 Magic[3] == char(0xFE)) {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000947 /* Reverse endian */
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000948 if (Magic.size() >= 14) type = Magic[13] << 8 | Magic[12];
Michael J. Spencer5b082302011-12-13 23:17:12 +0000949 }
950 switch (type) {
951 default: break;
952 case 1: return file_magic::macho_object;
953 case 2: return file_magic::macho_executable;
954 case 3: return file_magic::macho_fixed_virtual_memory_shared_lib;
955 case 4: return file_magic::macho_core;
Rafael Espindola6d315c62013-06-10 20:32:27 +0000956 case 5: return file_magic::macho_preload_executable;
Michael J. Spencer5b082302011-12-13 23:17:12 +0000957 case 6: return file_magic::macho_dynamically_linked_shared_lib;
958 case 7: return file_magic::macho_dynamic_linker;
959 case 8: return file_magic::macho_bundle;
960 case 9: return file_magic::macho_dynamic_linker;
961 case 10: return file_magic::macho_dsym_companion;
962 }
963 break;
964 }
965 case 0xF0: // PowerPC Windows
966 case 0x83: // Alpha 32-bit
967 case 0x84: // Alpha 64-bit
968 case 0x66: // MPS R4000 Windows
969 case 0x50: // mc68K
970 case 0x4c: // 80386 Windows
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000971 if (Magic[1] == 0x01)
Michael J. Spencer5b082302011-12-13 23:17:12 +0000972 return file_magic::coff_object;
973
974 case 0x90: // PA-RISC Windows
975 case 0x68: // mc68K Windows
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000976 if (Magic[1] == 0x02)
Michael J. Spencer5b082302011-12-13 23:17:12 +0000977 return file_magic::coff_object;
978 break;
979
980 case 0x4d: // Possible MS-DOS stub on Windows PE file
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000981 if (Magic[1] == 0x5a) {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000982 uint32_t off =
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000983 *reinterpret_cast<const support::ulittle32_t*>(Magic.data() + 0x3c);
Michael J. Spencer5b082302011-12-13 23:17:12 +0000984 // PE/COFF file, either EXE or DLL.
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000985 if (off < Magic.size() && memcmp(Magic.data() + off, "PE\0\0",4) == 0)
Michael J. Spencer5b082302011-12-13 23:17:12 +0000986 return file_magic::pecoff_executable;
987 }
988 break;
989
990 case 0x64: // x86-64 Windows.
Rafael Espindola9bd9f8e2013-06-11 17:22:12 +0000991 if (Magic[1] == char(0x86))
Michael J. Spencer5b082302011-12-13 23:17:12 +0000992 return file_magic::coff_object;
993 break;
994
995 default:
996 break;
997 }
998 return file_magic::unknown;
999}
1000
1001error_code identify_magic(const Twine &path, file_magic &result) {
Michael J. Spencer28f0ed52011-01-15 20:39:36 +00001002 SmallString<32> Magic;
1003 error_code ec = get_magic(path, Magic.capacity(), Magic);
1004 if (ec && ec != errc::value_too_large)
1005 return ec;
1006
Michael J. Spencer5b082302011-12-13 23:17:12 +00001007 result = identify_magic(Magic);
David Blaikie58604cd2012-02-09 19:24:12 +00001008 return error_code::success();
Michael J. Spencer28f0ed52011-01-15 20:39:36 +00001009}
1010
Michael J. Spencer7fd682a2011-01-05 16:39:38 +00001011namespace {
1012error_code remove_all_r(StringRef path, file_type ft, uint32_t &count) {
1013 if (ft == file_type::directory_file) {
1014 // This code would be a lot better with exceptions ;/.
1015 error_code ec;
Michael J. Spencer1dd2ee72011-12-12 06:03:33 +00001016 directory_iterator i(path, ec);
1017 if (ec) return ec;
1018 for (directory_iterator e; i != e; i.increment(ec)) {
Michael J. Spencer7fd682a2011-01-05 16:39:38 +00001019 if (ec) return ec;
1020 file_status st;
1021 if (error_code ec = i->status(st)) return ec;
1022 if (error_code ec = remove_all_r(i->path(), st.type(), count)) return ec;
1023 }
1024 bool obviously_this_exists;
1025 if (error_code ec = remove(path, obviously_this_exists)) return ec;
1026 assert(obviously_this_exists);
1027 ++count; // Include the directory itself in the items removed.
1028 } else {
1029 bool obviously_this_exists;
1030 if (error_code ec = remove(path, obviously_this_exists)) return ec;
1031 assert(obviously_this_exists);
1032 ++count;
1033 }
1034
David Blaikie58604cd2012-02-09 19:24:12 +00001035 return error_code::success();
Michael J. Spencer7fd682a2011-01-05 16:39:38 +00001036}
Zhanyong Wan63cc3a82011-02-11 21:24:40 +00001037} // end unnamed namespace
Michael J. Spencer7fd682a2011-01-05 16:39:38 +00001038
1039error_code remove_all(const Twine &path, uint32_t &num_removed) {
1040 SmallString<128> path_storage;
1041 StringRef p = path.toStringRef(path_storage);
1042
1043 file_status fs;
1044 if (error_code ec = status(path, fs))
1045 return ec;
1046 num_removed = 0;
1047 return remove_all_r(p, fs.type(), num_removed);
1048}
1049
Michael J. Spencer5bcdc7f2011-01-05 16:39:13 +00001050error_code directory_entry::status(file_status &result) const {
1051 return fs::status(Path, result);
1052}
1053
Michael J. Spencerbee0c382010-12-01 19:32:01 +00001054} // end namespace fs
1055} // end namespace sys
1056} // end namespace llvm
Michael J. Spencerdffde992010-11-29 22:28:51 +00001057
1058// Include the truly platform-specific parts.
1059#if defined(LLVM_ON_UNIX)
Rafael Espindola7a231f52013-06-26 19:33:03 +00001060#include "Unix/Path.inc"
Michael J. Spencerdffde992010-11-29 22:28:51 +00001061#endif
1062#if defined(LLVM_ON_WIN32)
Rafael Espindola7a231f52013-06-26 19:33:03 +00001063#include "Windows/Path.inc"
Michael J. Spencerdffde992010-11-29 22:28:51 +00001064#endif