blob: 535ff00bae031d162517ebabc0b1086403cf0dab [file] [log] [blame]
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001//===-- Path.cpp - Implement OS Path Concept ------------------------------===//
Michael J. Spencerebad2f92010-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 Espindolaf1fc3822013-06-26 19:33:03 +000010// This file implements the operating system Path API.
Michael J. Spencerebad2f92010-11-29 22:28:51 +000011//
12//===----------------------------------------------------------------------===//
13
Rafael Espindola3bc8e712013-06-11 22:21:28 +000014#include "llvm/Support/Path.h"
Michael J. Spencer4f8a8322011-12-13 23:17:12 +000015#include "llvm/Support/Endian.h"
Michael J. Spencerebad2f92010-11-29 22:28:51 +000016#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/Support/FileSystem.h"
Aaron Ballman07e76182014-02-11 03:40:14 +000018#include "llvm/Support/Process.h"
Michael J. Spencerebad2f92010-11-29 22:28:51 +000019#include <cctype>
Michael J. Spencer848f46b2010-12-28 01:49:01 +000020#include <cstdio>
21#include <cstring>
Rafael Espindola6d354812013-07-16 19:44:17 +000022#include <fcntl.h>
Rafael Espindola4526b1d2013-06-18 17:01:00 +000023
24#if !defined(_MSC_VER) && !defined(__MINGW32__)
Douglas Gregora86ddf02013-03-21 21:46:10 +000025#include <unistd.h>
Rafael Espindola4526b1d2013-06-18 17:01:00 +000026#else
27#include <io.h>
Douglas Gregora86ddf02013-03-21 21:46:10 +000028#endif
Michael J. Spencerebad2f92010-11-29 22:28:51 +000029
30namespace {
31 using llvm::StringRef;
Zhanyong Wan606bb1a2011-02-11 21:24:40 +000032 using llvm::sys::path::is_separator;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000033
34#ifdef LLVM_ON_WIN32
Benjamin Kramer5e4f0a42012-02-08 13:13:47 +000035 const char *separators = "\\/";
Alp Tokercb402912014-01-24 17:20:08 +000036 const char preferred_separator = '\\';
Michael J. Spencerebad2f92010-11-29 22:28:51 +000037#else
Benjamin Kramer5e4f0a42012-02-08 13:13:47 +000038 const char separators = '/';
Alp Tokercb402912014-01-24 17:20:08 +000039 const char preferred_separator = '/';
Michael J. Spencerebad2f92010-11-29 22:28:51 +000040#endif
41
Benjamin Kramercb520cd2010-12-17 18:59:09 +000042 StringRef find_first_component(StringRef path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000043 // Look for this first component in the following order.
44 // * empty (in this case we return an empty string)
45 // * either C: or {//,\\}net.
46 // * {/,\}
47 // * {.,..}
48 // * {file,directory}name
49
50 if (path.empty())
51 return path;
52
Michael J. Spencerbfea3c72010-12-07 03:57:48 +000053#ifdef LLVM_ON_WIN32
Michael J. Spencerebad2f92010-11-29 22:28:51 +000054 // C:
Guy Benyei83c74e92013-02-12 21:21:59 +000055 if (path.size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) &&
56 path[1] == ':')
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000057 return path.substr(0, 2);
Michael J. Spencerbfea3c72010-12-07 03:57:48 +000058#endif
Michael J. Spencerebad2f92010-11-29 22:28:51 +000059
60 // //net
61 if ((path.size() > 2) &&
Michael J. Spencerbfea3c72010-12-07 03:57:48 +000062 is_separator(path[0]) &&
63 path[0] == path[1] &&
64 !is_separator(path[2])) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000065 // Find the next directory separator.
Michael J. Spencerbfea3c72010-12-07 03:57:48 +000066 size_t end = path.find_first_of(separators, 2);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000067 return path.substr(0, end);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000068 }
69
70 // {/,\}
Michael J. Spencerbfea3c72010-12-07 03:57:48 +000071 if (is_separator(path[0]))
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000072 return path.substr(0, 1);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000073
74 if (path.startswith(".."))
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000075 return path.substr(0, 2);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000076
77 if (path[0] == '.')
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000078 return path.substr(0, 1);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000079
80 // * {file,directory}name
Tareq A. Siraj73537ea2013-08-12 17:10:49 +000081 size_t end = path.find_first_of(separators);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000082 return path.substr(0, end);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000083 }
Michael J. Spencer545cbdf2010-11-30 23:28:07 +000084
Benjamin Kramer292b44b2010-12-17 18:19:06 +000085 size_t filename_pos(StringRef str) {
Michael J. Spencer545cbdf2010-11-30 23:28:07 +000086 if (str.size() == 2 &&
87 is_separator(str[0]) &&
Michael J. Spencerbfea3c72010-12-07 03:57:48 +000088 str[0] == str[1])
Michael J. Spencer545cbdf2010-11-30 23:28:07 +000089 return 0;
90
91 if (str.size() > 0 && is_separator(str[str.size() - 1]))
92 return str.size() - 1;
93
94 size_t pos = str.find_last_of(separators, str.size() - 1);
95
96#ifdef LLVM_ON_WIN32
97 if (pos == StringRef::npos)
98 pos = str.find_last_of(':', str.size() - 2);
99#endif
100
101 if (pos == StringRef::npos ||
102 (pos == 1 && is_separator(str[0])))
103 return 0;
104
105 return pos + 1;
106 }
107
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000108 size_t root_dir_start(StringRef str) {
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000109 // case "c:/"
110#ifdef LLVM_ON_WIN32
111 if (str.size() > 2 &&
112 str[1] == ':' &&
113 is_separator(str[2]))
114 return 2;
115#endif
116
117 // case "//"
118 if (str.size() == 2 &&
119 is_separator(str[0]) &&
120 str[0] == str[1])
121 return StringRef::npos;
122
123 // case "//net"
124 if (str.size() > 3 &&
125 is_separator(str[0]) &&
126 str[0] == str[1] &&
127 !is_separator(str[2])) {
128 return str.find_first_of(separators, 2);
129 }
130
131 // case "/"
132 if (str.size() > 0 && is_separator(str[0]))
133 return 0;
134
135 return StringRef::npos;
136 }
137
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000138 size_t parent_path_end(StringRef path) {
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000139 size_t end_pos = filename_pos(path);
140
141 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
142
143 // Skip separators except for root dir.
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000144 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000145
146 while(end_pos > 0 &&
147 (end_pos - 1) != root_dir_pos &&
148 is_separator(path[end_pos - 1]))
149 --end_pos;
150
151 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
152 return StringRef::npos;
153
154 return end_pos;
155 }
Zhanyong Wan606bb1a2011-02-11 21:24:40 +0000156} // end unnamed namespace
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000157
Rafael Espindolae79a8722013-06-28 03:48:47 +0000158enum FSEntity {
159 FS_Dir,
160 FS_File,
161 FS_Name
162};
163
164// Implemented in Unix/Path.inc and Windows/Path.inc.
165static llvm::error_code
166createUniqueEntity(const llvm::Twine &Model, int &ResultFD,
167 llvm::SmallVectorImpl<char> &ResultPath,
168 bool MakeAbsolute, unsigned Mode, FSEntity Type);
169
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000170namespace llvm {
171namespace sys {
172namespace path {
173
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000174const_iterator begin(StringRef path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000175 const_iterator i;
176 i.Path = path;
177 i.Component = find_first_component(path);
178 i.Position = 0;
179 return i;
180}
181
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000182const_iterator end(StringRef path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000183 const_iterator i;
184 i.Path = path;
185 i.Position = path.size();
186 return i;
187}
188
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000189const_iterator &const_iterator::operator++() {
190 assert(Position < Path.size() && "Tried to increment past end!");
191
192 // Increment Position to past the current component
193 Position += Component.size();
194
195 // Check for end.
196 if (Position == Path.size()) {
197 Component = StringRef();
198 return *this;
199 }
200
201 // Both POSIX and Windows treat paths that begin with exactly two separators
202 // specially.
203 bool was_net = Component.size() > 2 &&
204 is_separator(Component[0]) &&
205 Component[1] == Component[0] &&
206 !is_separator(Component[2]);
207
208 // Handle separators.
209 if (is_separator(Path[Position])) {
210 // Root dir.
211 if (was_net
212#ifdef LLVM_ON_WIN32
213 // c:/
214 || Component.endswith(":")
215#endif
216 ) {
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000217 Component = Path.substr(Position, 1);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000218 return *this;
219 }
220
221 // Skip extra separators.
222 while (Position != Path.size() &&
223 is_separator(Path[Position])) {
224 ++Position;
225 }
226
227 // Treat trailing '/' as a '.'.
228 if (Position == Path.size()) {
229 --Position;
230 Component = ".";
231 return *this;
232 }
233 }
234
235 // Find next component.
236 size_t end_pos = Path.find_first_of(separators, Position);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000237 Component = Path.slice(Position, end_pos);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000238
239 return *this;
240}
241
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000242const_iterator &const_iterator::operator--() {
243 // If we're at the end and the previous char was a '/', return '.'.
244 if (Position == Path.size() &&
245 Path.size() > 1 &&
246 is_separator(Path[Position - 1])
247#ifdef LLVM_ON_WIN32
248 && Path[Position - 2] != ':'
249#endif
250 ) {
251 --Position;
252 Component = ".";
253 return *this;
254 }
255
256 // Skip separators unless it's the root directory.
257 size_t root_dir_pos = root_dir_start(Path);
258 size_t end_pos = Position;
259
260 while(end_pos > 0 &&
261 (end_pos - 1) != root_dir_pos &&
262 is_separator(Path[end_pos - 1]))
263 --end_pos;
264
265 // Find next separator.
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000266 size_t start_pos = filename_pos(Path.substr(0, end_pos));
267 Component = Path.slice(start_pos, end_pos);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000268 Position = start_pos;
269 return *this;
270}
271
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000272bool const_iterator::operator==(const const_iterator &RHS) const {
273 return Path.begin() == RHS.Path.begin() &&
274 Position == RHS.Position;
275}
276
277bool const_iterator::operator!=(const const_iterator &RHS) const {
278 return !(*this == RHS);
279}
280
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000281ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
282 return Position - RHS.Position;
283}
284
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000285const StringRef root_path(StringRef path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000286 const_iterator b = begin(path),
287 pos = b,
288 e = end(path);
289 if (b != e) {
290 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
291 bool has_drive =
292#ifdef LLVM_ON_WIN32
293 b->endswith(":");
294#else
295 false;
296#endif
297
298 if (has_net || has_drive) {
299 if ((++pos != e) && is_separator((*pos)[0])) {
300 // {C:/,//net/}, so get the first two components.
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000301 return path.substr(0, b->size() + pos->size());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000302 } else {
303 // just {C:,//net}, return the first component.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000304 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000305 }
306 }
307
308 // POSIX style root directory.
309 if (is_separator((*b)[0])) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000310 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000311 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000312 }
313
Michael J. Spencerf616b212010-12-07 17:04:04 +0000314 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000315}
316
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000317const StringRef root_name(StringRef path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000318 const_iterator b = begin(path),
319 e = end(path);
320 if (b != e) {
321 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
322 bool has_drive =
323#ifdef LLVM_ON_WIN32
324 b->endswith(":");
325#else
326 false;
327#endif
328
329 if (has_net || has_drive) {
330 // just {C:,//net}, return the first component.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000331 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000332 }
333 }
334
335 // No path or no name.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000336 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000337}
338
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000339const StringRef root_directory(StringRef path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000340 const_iterator b = begin(path),
341 pos = b,
342 e = end(path);
343 if (b != e) {
344 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
345 bool has_drive =
346#ifdef LLVM_ON_WIN32
347 b->endswith(":");
348#else
349 false;
350#endif
351
352 if ((has_net || has_drive) &&
353 // {C:,//net}, skip to the next component.
354 (++pos != e) && is_separator((*pos)[0])) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000355 return *pos;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000356 }
357
358 // POSIX style root directory.
359 if (!has_net && is_separator((*b)[0])) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000360 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000361 }
362 }
363
364 // No path or no root.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000365 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000366}
367
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000368const StringRef relative_path(StringRef path) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000369 StringRef root = root_path(path);
Michael J. Spencere6462392012-02-29 00:06:24 +0000370 return path.substr(root.size());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000371}
372
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000373void append(SmallVectorImpl<char> &path, const Twine &a,
374 const Twine &b,
375 const Twine &c,
376 const Twine &d) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000377 SmallString<32> a_storage;
378 SmallString<32> b_storage;
379 SmallString<32> c_storage;
380 SmallString<32> d_storage;
381
382 SmallVector<StringRef, 4> components;
383 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
384 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
385 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
386 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
387
388 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
389 e = components.end();
390 i != e; ++i) {
391 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
392 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
Michael J. Spencerf616b212010-12-07 17:04:04 +0000393 bool is_root_name = has_root_name(*i);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000394
395 if (path_has_sep) {
396 // Strip separators from beginning of component.
397 size_t loc = i->find_first_not_of(separators);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000398 StringRef c = i->substr(loc);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000399
400 // Append it.
401 path.append(c.begin(), c.end());
402 continue;
403 }
404
Michael J. Spencer95e4ac12010-12-06 04:28:23 +0000405 if (!component_has_sep && !(path.empty() || is_root_name)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000406 // Add a separator.
Alp Tokercb402912014-01-24 17:20:08 +0000407 path.push_back(preferred_separator);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000408 }
409
410 path.append(i->begin(), i->end());
411 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000412}
413
Argyrios Kyrtzidisa61736f2011-02-15 17:51:19 +0000414void append(SmallVectorImpl<char> &path,
415 const_iterator begin, const_iterator end) {
416 for (; begin != end; ++begin)
417 path::append(path, *begin);
418}
419
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000420const StringRef parent_path(StringRef path) {
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000421 size_t end_pos = parent_path_end(path);
422 if (end_pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000423 return StringRef();
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000424 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000425 return path.substr(0, end_pos);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000426}
427
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000428void remove_filename(SmallVectorImpl<char> &path) {
Michael J. Spencer9c594092010-12-01 00:52:28 +0000429 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000430 if (end_pos != StringRef::npos)
431 path.set_size(end_pos);
Michael J. Spencer9c594092010-12-01 00:52:28 +0000432}
433
Michael J. Spencerf616b212010-12-07 17:04:04 +0000434void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
Michael J. Spencerfb3a95d2010-12-01 00:52:55 +0000435 StringRef p(path.begin(), path.size());
436 SmallString<32> ext_storage;
437 StringRef ext = extension.toStringRef(ext_storage);
438
439 // Erase existing extension.
440 size_t pos = p.find_last_of('.');
441 if (pos != StringRef::npos && pos >= filename_pos(p))
442 path.set_size(pos);
443
444 // Append '.' if needed.
445 if (ext.size() > 0 && ext[0] != '.')
446 path.push_back('.');
447
448 // Append extension.
449 path.append(ext.begin(), ext.end());
Michael J. Spencerfb3a95d2010-12-01 00:52:55 +0000450}
451
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000452void native(const Twine &path, SmallVectorImpl<char> &result) {
Benjamin Kramerbd4ac9b2013-09-11 10:45:21 +0000453 assert((!path.isSingleStringRef() ||
454 path.getSingleStringRef().data() != result.data()) &&
455 "path and result are not allowed to overlap!");
Michael J. Spencer80025002010-12-01 02:48:27 +0000456 // Clear result.
Michael J. Spencer98c7a112010-12-07 01:23:19 +0000457 result.clear();
Michael J. Spencer80025002010-12-01 02:48:27 +0000458 path.toVector(result);
Benjamin Kramerbd4ac9b2013-09-11 10:45:21 +0000459 native(result);
460}
461
462void native(SmallVectorImpl<char> &path) {
463#ifdef LLVM_ON_WIN32
464 std::replace(path.begin(), path.end(), '/', '\\');
Michael J. Spencer80025002010-12-01 02:48:27 +0000465#endif
Michael J. Spencer80025002010-12-01 02:48:27 +0000466}
467
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000468const StringRef filename(StringRef path) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000469 return *(--end(path));
Michael J. Spencer14269202010-12-01 03:18:17 +0000470}
471
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000472const StringRef stem(StringRef path) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000473 StringRef fname = filename(path);
Michael J. Spencer956955e2010-12-01 03:18:33 +0000474 size_t pos = fname.find_last_of('.');
475 if (pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000476 return fname;
Michael J. Spencer956955e2010-12-01 03:18:33 +0000477 else
478 if ((fname.size() == 1 && fname == ".") ||
479 (fname.size() == 2 && fname == ".."))
Michael J. Spencerf616b212010-12-07 17:04:04 +0000480 return fname;
Michael J. Spencer956955e2010-12-01 03:18:33 +0000481 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000482 return fname.substr(0, pos);
Michael J. Spencer956955e2010-12-01 03:18:33 +0000483}
484
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000485const StringRef extension(StringRef path) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000486 StringRef fname = filename(path);
Michael J. Spencer87106c52010-12-01 03:37:41 +0000487 size_t pos = fname.find_last_of('.');
488 if (pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000489 return StringRef();
Michael J. Spencer87106c52010-12-01 03:37:41 +0000490 else
491 if ((fname.size() == 1 && fname == ".") ||
492 (fname.size() == 2 && fname == ".."))
Michael J. Spencerf616b212010-12-07 17:04:04 +0000493 return StringRef();
Michael J. Spencer87106c52010-12-01 03:37:41 +0000494 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000495 return fname.substr(pos);
Michael J. Spencer87106c52010-12-01 03:37:41 +0000496}
497
Zhanyong Wan606bb1a2011-02-11 21:24:40 +0000498bool is_separator(char value) {
499 switch(value) {
500#ifdef LLVM_ON_WIN32
501 case '\\': // fall through
502#endif
503 case '/': return true;
504 default: return false;
505 }
506}
507
Douglas Gregor123dc702011-09-14 20:27:01 +0000508void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result) {
509 result.clear();
Michael J. Spencer7dfbeda2011-12-13 23:16:15 +0000510
David Fang6860c812014-02-12 21:02:12 +0000511#if defined(_CS_DARWIN_USER_TEMP_DIR) && defined(_CS_DARWIN_USER_CACHE_DIR)
Douglas Gregora86ddf02013-03-21 21:46:10 +0000512 // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR.
David Fang6860c812014-02-12 21:02:12 +0000513 // macros defined in <unistd.h> on darwin >= 9
Douglas Gregora86ddf02013-03-21 21:46:10 +0000514 int ConfName = erasedOnReboot? _CS_DARWIN_USER_TEMP_DIR
515 : _CS_DARWIN_USER_CACHE_DIR;
516 size_t ConfLen = confstr(ConfName, 0, 0);
517 if (ConfLen > 0) {
518 do {
519 result.resize(ConfLen);
520 ConfLen = confstr(ConfName, result.data(), result.size());
521 } while (ConfLen > 0 && ConfLen != result.size());
522
523 if (ConfLen > 0) {
524 assert(result.back() == 0);
525 result.pop_back();
526 return;
527 }
528
529 result.clear();
530 }
531#endif
532
Douglas Gregor123dc702011-09-14 20:27:01 +0000533 // Check whether the temporary directory is specified by an environment
534 // variable.
535 const char *EnvironmentVariable;
536#ifdef LLVM_ON_WIN32
537 EnvironmentVariable = "TEMP";
538#else
539 EnvironmentVariable = "TMPDIR";
540#endif
541 if (char *RequestedDir = getenv(EnvironmentVariable)) {
542 result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
543 return;
544 }
Michael J. Spencer7dfbeda2011-12-13 23:16:15 +0000545
Douglas Gregor123dc702011-09-14 20:27:01 +0000546 // Fall back to a system default.
547 const char *DefaultResult;
548#ifdef LLVM_ON_WIN32
549 (void)erasedOnReboot;
Douglas Gregor8b744542011-09-14 23:21:47 +0000550 DefaultResult = "C:\\TEMP";
Douglas Gregor123dc702011-09-14 20:27:01 +0000551#else
552 if (erasedOnReboot)
553 DefaultResult = "/tmp";
554 else
555 DefaultResult = "/var/tmp";
556#endif
557 result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
558}
Michael J. Spencer7dfbeda2011-12-13 23:16:15 +0000559
Michael J. Spencera68282c2010-12-07 18:12:07 +0000560bool has_root_name(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000561 SmallString<128> path_storage;
562 StringRef p = path.toStringRef(path_storage);
563
Michael J. Spencerf616b212010-12-07 17:04:04 +0000564 return !root_name(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000565}
566
Michael J. Spencera68282c2010-12-07 18:12:07 +0000567bool has_root_directory(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000568 SmallString<128> path_storage;
569 StringRef p = path.toStringRef(path_storage);
570
Michael J. Spencerf616b212010-12-07 17:04:04 +0000571 return !root_directory(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000572}
573
Michael J. Spencera68282c2010-12-07 18:12:07 +0000574bool has_root_path(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000575 SmallString<128> path_storage;
576 StringRef p = path.toStringRef(path_storage);
577
Michael J. Spencerf616b212010-12-07 17:04:04 +0000578 return !root_path(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000579}
580
Michael J. Spencer6d4b7e72010-12-20 13:30:28 +0000581bool has_relative_path(const Twine &path) {
582 SmallString<128> path_storage;
583 StringRef p = path.toStringRef(path_storage);
584
585 return !relative_path(p).empty();
586}
587
Michael J. Spencera68282c2010-12-07 18:12:07 +0000588bool has_filename(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000589 SmallString<128> path_storage;
590 StringRef p = path.toStringRef(path_storage);
591
Michael J. Spencerf616b212010-12-07 17:04:04 +0000592 return !filename(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000593}
594
Michael J. Spencera68282c2010-12-07 18:12:07 +0000595bool has_parent_path(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000596 SmallString<128> path_storage;
597 StringRef p = path.toStringRef(path_storage);
598
Michael J. Spencerf616b212010-12-07 17:04:04 +0000599 return !parent_path(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000600}
601
Michael J. Spencera68282c2010-12-07 18:12:07 +0000602bool has_stem(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000603 SmallString<128> path_storage;
604 StringRef p = path.toStringRef(path_storage);
605
Michael J. Spencerf616b212010-12-07 17:04:04 +0000606 return !stem(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000607}
608
Michael J. Spencera68282c2010-12-07 18:12:07 +0000609bool has_extension(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000610 SmallString<128> path_storage;
611 StringRef p = path.toStringRef(path_storage);
612
Michael J. Spencerf616b212010-12-07 17:04:04 +0000613 return !extension(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000614}
615
Michael J. Spencera68282c2010-12-07 18:12:07 +0000616bool is_absolute(const Twine &path) {
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000617 SmallString<128> path_storage;
618 StringRef p = path.toStringRef(path_storage);
619
Michael J. Spencerf616b212010-12-07 17:04:04 +0000620 bool rootDir = has_root_directory(p),
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000621#ifdef LLVM_ON_WIN32
Michael J. Spencerf616b212010-12-07 17:04:04 +0000622 rootName = has_root_name(p);
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000623#else
Michael J. Spencerf616b212010-12-07 17:04:04 +0000624 rootName = true;
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000625#endif
626
Michael J. Spencerf616b212010-12-07 17:04:04 +0000627 return rootDir && rootName;
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000628}
629
Michael J. Spencera68282c2010-12-07 18:12:07 +0000630bool is_relative(const Twine &path) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000631 return !is_absolute(path);
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000632}
633
Michael J. Spencer9fc1d9d2010-12-01 19:32:01 +0000634} // end namespace path
635
636namespace fs {
637
Rafael Espindola7f822a92013-07-29 21:26:49 +0000638error_code getUniqueID(const Twine Path, UniqueID &Result) {
639 file_status Status;
640 error_code EC = status(Path, Status);
641 if (EC)
642 return EC;
643 Result = Status.getUniqueID();
644 return error_code::success();
645}
646
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000647error_code createUniqueFile(const Twine &Model, int &ResultFd,
648 SmallVectorImpl<char> &ResultPath, unsigned Mode) {
649 return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
650}
651
652error_code createUniqueFile(const Twine &Model,
653 SmallVectorImpl<char> &ResultPath) {
654 int Dummy;
655 return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
656}
657
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000658static error_code createTemporaryFile(const Twine &Model, int &ResultFD,
659 llvm::SmallVectorImpl<char> &ResultPath,
660 FSEntity Type) {
661 SmallString<128> Storage;
662 StringRef P = Model.toNullTerminatedStringRef(Storage);
663 assert(P.find_first_of(separators) == StringRef::npos &&
664 "Model must be a simple filename.");
665 // Use P.begin() so that createUniqueEntity doesn't need to recreate Storage.
666 return createUniqueEntity(P.begin(), ResultFD, ResultPath,
667 true, owner_read | owner_write, Type);
668}
669
670static error_code
671createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
672 llvm::SmallVectorImpl<char> &ResultPath,
673 FSEntity Type) {
Rafael Espindolad3c89042013-07-25 15:00:17 +0000674 const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
675 return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000676 Type);
677}
678
679
680error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
681 int &ResultFD,
682 SmallVectorImpl<char> &ResultPath) {
683 return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File);
684}
685
686error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
687 SmallVectorImpl<char> &ResultPath) {
688 int Dummy;
689 return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
690}
691
692
Rafael Espindolae79a8722013-06-28 03:48:47 +0000693// This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
Rafael Espindola31a24432013-06-28 10:55:41 +0000694// for consistency. We should try using mkdtemp.
Rafael Espindola7ffacc42013-06-27 03:45:31 +0000695error_code createUniqueDirectory(const Twine &Prefix,
696 SmallVectorImpl<char> &ResultPath) {
Rafael Espindolae79a8722013-06-28 03:48:47 +0000697 int Dummy;
698 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath,
699 true, 0, FS_Dir);
Rafael Espindola7ffacc42013-06-27 03:45:31 +0000700}
701
Michael J. Spencer92903a32010-12-07 03:57:17 +0000702error_code make_absolute(SmallVectorImpl<char> &path) {
703 StringRef p(path.data(), path.size());
704
Daniel Dunbarb23158c2012-02-29 00:20:37 +0000705 bool rootDirectory = path::has_root_directory(p),
706#ifdef LLVM_ON_WIN32
Daniel Dunbar39ea4582012-02-29 00:46:46 +0000707 rootName = path::has_root_name(p);
Daniel Dunbarb23158c2012-02-29 00:20:37 +0000708#else
709 rootName = true;
710#endif
Michael J. Spencer92903a32010-12-07 03:57:17 +0000711
712 // Already absolute.
713 if (rootName && rootDirectory)
David Blaikie18544b92012-02-09 19:24:12 +0000714 return error_code::success();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000715
716 // All of the following conditions will need the current directory.
717 SmallString<128> current_dir;
718 if (error_code ec = current_path(current_dir)) return ec;
719
720 // Relative path. Prepend the current directory.
721 if (!rootName && !rootDirectory) {
722 // Append path to the current directory.
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000723 path::append(current_dir, p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000724 // Set path to the result.
725 path.swap(current_dir);
David Blaikie18544b92012-02-09 19:24:12 +0000726 return error_code::success();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000727 }
728
729 if (!rootName && rootDirectory) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000730 StringRef cdrn = path::root_name(current_dir);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000731 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000732 path::append(curDirRootName, p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000733 // Set path to the result.
734 path.swap(curDirRootName);
David Blaikie18544b92012-02-09 19:24:12 +0000735 return error_code::success();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000736 }
737
738 if (rootName && !rootDirectory) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000739 StringRef pRootName = path::root_name(p);
740 StringRef bRootDirectory = path::root_directory(current_dir);
741 StringRef bRelativePath = path::relative_path(current_dir);
742 StringRef pRelativePath = path::relative_path(p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000743
744 SmallString<128> res;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000745 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000746 path.swap(res);
David Blaikie18544b92012-02-09 19:24:12 +0000747 return error_code::success();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000748 }
749
750 llvm_unreachable("All rootName and rootDirectory combinations should have "
751 "occurred above!");
752}
753
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000754error_code create_directories(const Twine &Path, bool IgnoreExisting) {
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000755 SmallString<128> PathStorage;
756 StringRef P = Path.toStringRef(PathStorage);
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000757
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000758 // Be optimistic and try to create the directory
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000759 error_code EC = create_directory(P, IgnoreExisting);
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000760 // If we succeeded, or had any error other than the parent not existing, just
761 // return it.
762 if (EC != errc::no_such_file_or_directory)
763 return EC;
Michael J. Spencerf616b212010-12-07 17:04:04 +0000764
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000765 // We failed because of a no_such_file_or_directory, try to create the
766 // parent.
767 StringRef Parent = path::parent_path(P);
768 if (Parent.empty())
769 return EC;
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000770
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000771 if ((EC = create_directories(Parent)))
772 return EC;
773
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000774 return create_directory(P, IgnoreExisting);
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000775}
776
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000777bool exists(file_status status) {
778 return status_known(status) && status.type() != file_type::file_not_found;
779}
780
781bool status_known(file_status s) {
782 return s.type() != file_type::status_error;
783}
784
785bool is_directory(file_status status) {
786 return status.type() == file_type::directory_file;
787}
788
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000789error_code is_directory(const Twine &path, bool &result) {
790 file_status st;
791 if (error_code ec = status(path, st))
792 return ec;
793 result = is_directory(st);
David Blaikie18544b92012-02-09 19:24:12 +0000794 return error_code::success();
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000795}
796
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000797bool is_regular_file(file_status status) {
798 return status.type() == file_type::regular_file;
799}
800
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000801error_code is_regular_file(const Twine &path, bool &result) {
802 file_status st;
803 if (error_code ec = status(path, st))
804 return ec;
805 result = is_regular_file(st);
David Blaikie18544b92012-02-09 19:24:12 +0000806 return error_code::success();
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000807}
808
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000809bool is_symlink(file_status status) {
810 return status.type() == file_type::symlink_file;
811}
812
Michael J. Spencerd9960c62011-01-12 23:55:06 +0000813error_code is_symlink(const Twine &path, bool &result) {
814 file_status st;
815 if (error_code ec = status(path, st))
816 return ec;
817 result = is_symlink(st);
David Blaikie18544b92012-02-09 19:24:12 +0000818 return error_code::success();
Michael J. Spencerd9960c62011-01-12 23:55:06 +0000819}
820
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000821bool is_other(file_status status) {
822 return exists(status) &&
823 !is_regular_file(status) &&
824 !is_directory(status) &&
825 !is_symlink(status);
826}
827
Benjamin Kramer91ead3c2011-09-14 01:14:36 +0000828void directory_entry::replace_filename(const Twine &filename, file_status st) {
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000829 SmallString<128> path(Path.begin(), Path.end());
830 path::remove_filename(path);
831 path::append(path, filename);
832 Path = path.str();
833 Status = st;
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000834}
835
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000836error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000837 SmallString<32> MagicStorage;
Michael J. Spencer78874662011-01-15 18:52:41 +0000838 StringRef Magic = magic.toStringRef(MagicStorage);
839 SmallString<32> Buffer;
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000840
Michael J. Spencer78874662011-01-15 18:52:41 +0000841 if (error_code ec = get_magic(path, Magic.size(), Buffer)) {
842 if (ec == errc::value_too_large) {
843 // Magic.size() > file_size(Path).
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000844 result = false;
David Blaikie18544b92012-02-09 19:24:12 +0000845 return error_code::success();
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000846 }
Michael J. Spencer78874662011-01-15 18:52:41 +0000847 return ec;
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000848 }
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000849
Michael J. Spencer78874662011-01-15 18:52:41 +0000850 result = Magic == Buffer;
David Blaikie18544b92012-02-09 19:24:12 +0000851 return error_code::success();
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000852}
853
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000854/// @brief Identify the magic in magic.
Rafael Espindola9b404292013-06-11 17:22:12 +0000855 file_magic identify_magic(StringRef Magic) {
856 if (Magic.size() < 4)
Michael J. Spencer96ebd912012-06-19 05:29:57 +0000857 return file_magic::unknown;
Rafael Espindola9b404292013-06-11 17:22:12 +0000858 switch ((unsigned char)Magic[0]) {
Rui Ueyamafc149a62013-10-15 22:45:38 +0000859 case 0x00: {
Rui Ueyamae448f9e2013-11-15 21:22:02 +0000860 // COFF short import library file
861 if (Magic[1] == (char)0x00 && Magic[2] == (char)0xff &&
862 Magic[3] == (char)0xff)
863 return file_magic::coff_import_library;
Rui Ueyamafc149a62013-10-15 22:45:38 +0000864 // Windows resource file
Rui Ueyama50a86e12013-10-16 03:29:49 +0000865 const char Expected[] = { 0, 0, 0, 0, '\x20', 0, 0, 0, '\xff' };
Rui Ueyamafc149a62013-10-15 22:45:38 +0000866 if (Magic.size() >= sizeof(Expected) &&
867 memcmp(Magic.data(), Expected, sizeof(Expected)) == 0)
868 return file_magic::windows_resource;
Rui Ueyama829c4392013-11-14 22:09:08 +0000869 // 0x0000 = COFF unknown machine type
870 if (Magic[1] == 0)
871 return file_magic::coff_object;
Rui Ueyamafc149a62013-10-15 22:45:38 +0000872 break;
873 }
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000874 case 0xDE: // 0x0B17C0DE = BC wraper
Rafael Espindola9b404292013-06-11 17:22:12 +0000875 if (Magic[1] == (char)0xC0 && Magic[2] == (char)0x17 &&
876 Magic[3] == (char)0x0B)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000877 return file_magic::bitcode;
878 break;
879 case 'B':
Rafael Espindola9b404292013-06-11 17:22:12 +0000880 if (Magic[1] == 'C' && Magic[2] == (char)0xC0 && Magic[3] == (char)0xDE)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000881 return file_magic::bitcode;
882 break;
883 case '!':
Rafael Espindola9b404292013-06-11 17:22:12 +0000884 if (Magic.size() >= 8)
885 if (memcmp(Magic.data(),"!<arch>\n",8) == 0)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000886 return file_magic::archive;
887 break;
888
889 case '\177':
Rafael Espindola823de7c2013-06-11 17:25:45 +0000890 if (Magic.size() >= 18 && Magic[1] == 'E' && Magic[2] == 'L' &&
891 Magic[3] == 'F') {
Rafael Espindola9b404292013-06-11 17:22:12 +0000892 bool Data2MSB = Magic[5] == 2;
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000893 unsigned high = Data2MSB ? 16 : 17;
894 unsigned low = Data2MSB ? 17 : 16;
Rafael Espindola823de7c2013-06-11 17:25:45 +0000895 if (Magic[high] == 0)
Rafael Espindola9b404292013-06-11 17:22:12 +0000896 switch (Magic[low]) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000897 default: break;
898 case 1: return file_magic::elf_relocatable;
899 case 2: return file_magic::elf_executable;
900 case 3: return file_magic::elf_shared_object;
901 case 4: return file_magic::elf_core;
902 }
903 }
904 break;
905
906 case 0xCA:
Rafael Espindola9b404292013-06-11 17:22:12 +0000907 if (Magic[1] == char(0xFE) && Magic[2] == char(0xBA) &&
908 Magic[3] == char(0xBE)) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000909 // This is complicated by an overlap with Java class files.
910 // See the Mach-O section in /usr/share/file/magic for details.
Rafael Espindola9b404292013-06-11 17:22:12 +0000911 if (Magic.size() >= 8 && Magic[7] < 43)
Alexey Samsonove6388e62013-06-18 15:03:28 +0000912 return file_magic::macho_universal_binary;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000913 }
914 break;
915
916 // The two magic numbers for mach-o are:
917 // 0xfeedface - 32-bit mach-o
918 // 0xfeedfacf - 64-bit mach-o
919 case 0xFE:
920 case 0xCE:
921 case 0xCF: {
922 uint16_t type = 0;
Rafael Espindola9b404292013-06-11 17:22:12 +0000923 if (Magic[0] == char(0xFE) && Magic[1] == char(0xED) &&
924 Magic[2] == char(0xFA) &&
925 (Magic[3] == char(0xCE) || Magic[3] == char(0xCF))) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000926 /* Native endian */
Rafael Espindola9b404292013-06-11 17:22:12 +0000927 if (Magic.size() >= 16) type = Magic[14] << 8 | Magic[15];
928 } else if ((Magic[0] == char(0xCE) || Magic[0] == char(0xCF)) &&
929 Magic[1] == char(0xFA) && Magic[2] == char(0xED) &&
930 Magic[3] == char(0xFE)) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000931 /* Reverse endian */
Rafael Espindola9b404292013-06-11 17:22:12 +0000932 if (Magic.size() >= 14) type = Magic[13] << 8 | Magic[12];
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000933 }
934 switch (type) {
935 default: break;
936 case 1: return file_magic::macho_object;
937 case 2: return file_magic::macho_executable;
938 case 3: return file_magic::macho_fixed_virtual_memory_shared_lib;
939 case 4: return file_magic::macho_core;
Rafael Espindola134cc992013-06-10 20:32:27 +0000940 case 5: return file_magic::macho_preload_executable;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000941 case 6: return file_magic::macho_dynamically_linked_shared_lib;
942 case 7: return file_magic::macho_dynamic_linker;
943 case 8: return file_magic::macho_bundle;
944 case 9: return file_magic::macho_dynamic_linker;
945 case 10: return file_magic::macho_dsym_companion;
946 }
947 break;
948 }
949 case 0xF0: // PowerPC Windows
950 case 0x83: // Alpha 32-bit
951 case 0x84: // Alpha 64-bit
952 case 0x66: // MPS R4000 Windows
953 case 0x50: // mc68K
954 case 0x4c: // 80386 Windows
Rafael Espindola9b404292013-06-11 17:22:12 +0000955 if (Magic[1] == 0x01)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000956 return file_magic::coff_object;
957
958 case 0x90: // PA-RISC Windows
959 case 0x68: // mc68K Windows
Rafael Espindola9b404292013-06-11 17:22:12 +0000960 if (Magic[1] == 0x02)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000961 return file_magic::coff_object;
962 break;
963
964 case 0x4d: // Possible MS-DOS stub on Windows PE file
Rafael Espindola9b404292013-06-11 17:22:12 +0000965 if (Magic[1] == 0x5a) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000966 uint32_t off =
Rafael Espindola9b404292013-06-11 17:22:12 +0000967 *reinterpret_cast<const support::ulittle32_t*>(Magic.data() + 0x3c);
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000968 // PE/COFF file, either EXE or DLL.
Rafael Espindola9b404292013-06-11 17:22:12 +0000969 if (off < Magic.size() && memcmp(Magic.data() + off, "PE\0\0",4) == 0)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000970 return file_magic::pecoff_executable;
971 }
972 break;
973
974 case 0x64: // x86-64 Windows.
Rafael Espindola9b404292013-06-11 17:22:12 +0000975 if (Magic[1] == char(0x86))
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000976 return file_magic::coff_object;
977 break;
978
979 default:
980 break;
981 }
982 return file_magic::unknown;
983}
984
985error_code identify_magic(const Twine &path, file_magic &result) {
Michael J. Spencer94b2ab32011-01-15 20:39:36 +0000986 SmallString<32> Magic;
987 error_code ec = get_magic(path, Magic.capacity(), Magic);
988 if (ec && ec != errc::value_too_large)
989 return ec;
990
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000991 result = identify_magic(Magic);
David Blaikie18544b92012-02-09 19:24:12 +0000992 return error_code::success();
Michael J. Spencer94b2ab32011-01-15 20:39:36 +0000993}
994
Michael J. Spencerf8dc1862011-01-05 16:39:13 +0000995error_code directory_entry::status(file_status &result) const {
996 return fs::status(Path, result);
997}
998
Michael J. Spencer9fc1d9d2010-12-01 19:32:01 +0000999} // end namespace fs
1000} // end namespace sys
1001} // end namespace llvm
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001002
1003// Include the truly platform-specific parts.
1004#if defined(LLVM_ON_UNIX)
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001005#include "Unix/Path.inc"
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001006#endif
1007#if defined(LLVM_ON_WIN32)
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001008#include "Windows/Path.inc"
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001009#endif