blob: 1872af55ed6b9b40a8202c810707fdef50f2f691 [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
Douglas Gregora86ddf02013-03-21 21:46:10 +0000511#ifdef __APPLE__
512 // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR.
513 int ConfName = erasedOnReboot? _CS_DARWIN_USER_TEMP_DIR
514 : _CS_DARWIN_USER_CACHE_DIR;
515 size_t ConfLen = confstr(ConfName, 0, 0);
516 if (ConfLen > 0) {
517 do {
518 result.resize(ConfLen);
519 ConfLen = confstr(ConfName, result.data(), result.size());
520 } while (ConfLen > 0 && ConfLen != result.size());
521
522 if (ConfLen > 0) {
523 assert(result.back() == 0);
524 result.pop_back();
525 return;
526 }
527
528 result.clear();
529 }
530#endif
531
Douglas Gregor123dc702011-09-14 20:27:01 +0000532 // Check whether the temporary directory is specified by an environment
533 // variable.
534 const char *EnvironmentVariable;
535#ifdef LLVM_ON_WIN32
536 EnvironmentVariable = "TEMP";
537#else
538 EnvironmentVariable = "TMPDIR";
539#endif
540 if (char *RequestedDir = getenv(EnvironmentVariable)) {
541 result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
542 return;
543 }
Michael J. Spencer7dfbeda2011-12-13 23:16:15 +0000544
Douglas Gregor123dc702011-09-14 20:27:01 +0000545 // Fall back to a system default.
546 const char *DefaultResult;
547#ifdef LLVM_ON_WIN32
548 (void)erasedOnReboot;
Douglas Gregor8b744542011-09-14 23:21:47 +0000549 DefaultResult = "C:\\TEMP";
Douglas Gregor123dc702011-09-14 20:27:01 +0000550#else
551 if (erasedOnReboot)
552 DefaultResult = "/tmp";
553 else
554 DefaultResult = "/var/tmp";
555#endif
556 result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
557}
Michael J. Spencer7dfbeda2011-12-13 23:16:15 +0000558
Michael J. Spencera68282c2010-12-07 18:12:07 +0000559bool has_root_name(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000560 SmallString<128> path_storage;
561 StringRef p = path.toStringRef(path_storage);
562
Michael J. Spencerf616b212010-12-07 17:04:04 +0000563 return !root_name(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000564}
565
Michael J. Spencera68282c2010-12-07 18:12:07 +0000566bool has_root_directory(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000567 SmallString<128> path_storage;
568 StringRef p = path.toStringRef(path_storage);
569
Michael J. Spencerf616b212010-12-07 17:04:04 +0000570 return !root_directory(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000571}
572
Michael J. Spencera68282c2010-12-07 18:12:07 +0000573bool has_root_path(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000574 SmallString<128> path_storage;
575 StringRef p = path.toStringRef(path_storage);
576
Michael J. Spencerf616b212010-12-07 17:04:04 +0000577 return !root_path(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000578}
579
Michael J. Spencer6d4b7e72010-12-20 13:30:28 +0000580bool has_relative_path(const Twine &path) {
581 SmallString<128> path_storage;
582 StringRef p = path.toStringRef(path_storage);
583
584 return !relative_path(p).empty();
585}
586
Michael J. Spencera68282c2010-12-07 18:12:07 +0000587bool has_filename(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000588 SmallString<128> path_storage;
589 StringRef p = path.toStringRef(path_storage);
590
Michael J. Spencerf616b212010-12-07 17:04:04 +0000591 return !filename(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000592}
593
Michael J. Spencera68282c2010-12-07 18:12:07 +0000594bool has_parent_path(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000595 SmallString<128> path_storage;
596 StringRef p = path.toStringRef(path_storage);
597
Michael J. Spencerf616b212010-12-07 17:04:04 +0000598 return !parent_path(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000599}
600
Michael J. Spencera68282c2010-12-07 18:12:07 +0000601bool has_stem(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000602 SmallString<128> path_storage;
603 StringRef p = path.toStringRef(path_storage);
604
Michael J. Spencerf616b212010-12-07 17:04:04 +0000605 return !stem(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000606}
607
Michael J. Spencera68282c2010-12-07 18:12:07 +0000608bool has_extension(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000609 SmallString<128> path_storage;
610 StringRef p = path.toStringRef(path_storage);
611
Michael J. Spencerf616b212010-12-07 17:04:04 +0000612 return !extension(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000613}
614
Michael J. Spencera68282c2010-12-07 18:12:07 +0000615bool is_absolute(const Twine &path) {
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000616 SmallString<128> path_storage;
617 StringRef p = path.toStringRef(path_storage);
618
Michael J. Spencerf616b212010-12-07 17:04:04 +0000619 bool rootDir = has_root_directory(p),
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000620#ifdef LLVM_ON_WIN32
Michael J. Spencerf616b212010-12-07 17:04:04 +0000621 rootName = has_root_name(p);
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000622#else
Michael J. Spencerf616b212010-12-07 17:04:04 +0000623 rootName = true;
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000624#endif
625
Michael J. Spencerf616b212010-12-07 17:04:04 +0000626 return rootDir && rootName;
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000627}
628
Michael J. Spencera68282c2010-12-07 18:12:07 +0000629bool is_relative(const Twine &path) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000630 return !is_absolute(path);
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000631}
632
Michael J. Spencer9fc1d9d2010-12-01 19:32:01 +0000633} // end namespace path
634
635namespace fs {
636
Rafael Espindola7f822a92013-07-29 21:26:49 +0000637error_code getUniqueID(const Twine Path, UniqueID &Result) {
638 file_status Status;
639 error_code EC = status(Path, Status);
640 if (EC)
641 return EC;
642 Result = Status.getUniqueID();
643 return error_code::success();
644}
645
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000646error_code createUniqueFile(const Twine &Model, int &ResultFd,
647 SmallVectorImpl<char> &ResultPath, unsigned Mode) {
648 return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
649}
650
651error_code createUniqueFile(const Twine &Model,
652 SmallVectorImpl<char> &ResultPath) {
653 int Dummy;
654 return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
655}
656
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000657static error_code createTemporaryFile(const Twine &Model, int &ResultFD,
658 llvm::SmallVectorImpl<char> &ResultPath,
659 FSEntity Type) {
660 SmallString<128> Storage;
661 StringRef P = Model.toNullTerminatedStringRef(Storage);
662 assert(P.find_first_of(separators) == StringRef::npos &&
663 "Model must be a simple filename.");
664 // Use P.begin() so that createUniqueEntity doesn't need to recreate Storage.
665 return createUniqueEntity(P.begin(), ResultFD, ResultPath,
666 true, owner_read | owner_write, Type);
667}
668
669static error_code
670createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
671 llvm::SmallVectorImpl<char> &ResultPath,
672 FSEntity Type) {
Rafael Espindolad3c89042013-07-25 15:00:17 +0000673 const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
674 return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000675 Type);
676}
677
678
679error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
680 int &ResultFD,
681 SmallVectorImpl<char> &ResultPath) {
682 return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File);
683}
684
685error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
686 SmallVectorImpl<char> &ResultPath) {
687 int Dummy;
688 return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
689}
690
691
Rafael Espindolae79a8722013-06-28 03:48:47 +0000692// This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
Rafael Espindola31a24432013-06-28 10:55:41 +0000693// for consistency. We should try using mkdtemp.
Rafael Espindola7ffacc42013-06-27 03:45:31 +0000694error_code createUniqueDirectory(const Twine &Prefix,
695 SmallVectorImpl<char> &ResultPath) {
Rafael Espindolae79a8722013-06-28 03:48:47 +0000696 int Dummy;
697 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath,
698 true, 0, FS_Dir);
Rafael Espindola7ffacc42013-06-27 03:45:31 +0000699}
700
Michael J. Spencer92903a32010-12-07 03:57:17 +0000701error_code make_absolute(SmallVectorImpl<char> &path) {
702 StringRef p(path.data(), path.size());
703
Daniel Dunbarb23158c2012-02-29 00:20:37 +0000704 bool rootDirectory = path::has_root_directory(p),
705#ifdef LLVM_ON_WIN32
Daniel Dunbar39ea4582012-02-29 00:46:46 +0000706 rootName = path::has_root_name(p);
Daniel Dunbarb23158c2012-02-29 00:20:37 +0000707#else
708 rootName = true;
709#endif
Michael J. Spencer92903a32010-12-07 03:57:17 +0000710
711 // Already absolute.
712 if (rootName && rootDirectory)
David Blaikie18544b92012-02-09 19:24:12 +0000713 return error_code::success();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000714
715 // All of the following conditions will need the current directory.
716 SmallString<128> current_dir;
717 if (error_code ec = current_path(current_dir)) return ec;
718
719 // Relative path. Prepend the current directory.
720 if (!rootName && !rootDirectory) {
721 // Append path to the current directory.
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000722 path::append(current_dir, p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000723 // Set path to the result.
724 path.swap(current_dir);
David Blaikie18544b92012-02-09 19:24:12 +0000725 return error_code::success();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000726 }
727
728 if (!rootName && rootDirectory) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000729 StringRef cdrn = path::root_name(current_dir);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000730 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000731 path::append(curDirRootName, p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000732 // Set path to the result.
733 path.swap(curDirRootName);
David Blaikie18544b92012-02-09 19:24:12 +0000734 return error_code::success();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000735 }
736
737 if (rootName && !rootDirectory) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000738 StringRef pRootName = path::root_name(p);
739 StringRef bRootDirectory = path::root_directory(current_dir);
740 StringRef bRelativePath = path::relative_path(current_dir);
741 StringRef pRelativePath = path::relative_path(p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000742
743 SmallString<128> res;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000744 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000745 path.swap(res);
David Blaikie18544b92012-02-09 19:24:12 +0000746 return error_code::success();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000747 }
748
749 llvm_unreachable("All rootName and rootDirectory combinations should have "
750 "occurred above!");
751}
752
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000753error_code create_directories(const Twine &path, bool &existed) {
754 SmallString<128> path_storage;
755 StringRef p = path.toStringRef(path_storage);
756
Michael J. Spencerf616b212010-12-07 17:04:04 +0000757 StringRef parent = path::parent_path(p);
Michael J. Spencer39fb4082012-03-21 23:09:14 +0000758 if (!parent.empty()) {
759 bool parent_exists;
760 if (error_code ec = fs::exists(parent, parent_exists)) return ec;
Michael J. Spencerf616b212010-12-07 17:04:04 +0000761
Michael J. Spencer39fb4082012-03-21 23:09:14 +0000762 if (!parent_exists)
763 if (error_code ec = create_directories(parent, existed)) return ec;
764 }
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000765
766 return create_directory(p, existed);
767}
768
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000769bool exists(file_status status) {
770 return status_known(status) && status.type() != file_type::file_not_found;
771}
772
773bool status_known(file_status s) {
774 return s.type() != file_type::status_error;
775}
776
777bool is_directory(file_status status) {
778 return status.type() == file_type::directory_file;
779}
780
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000781error_code is_directory(const Twine &path, bool &result) {
782 file_status st;
783 if (error_code ec = status(path, st))
784 return ec;
785 result = is_directory(st);
David Blaikie18544b92012-02-09 19:24:12 +0000786 return error_code::success();
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000787}
788
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000789bool is_regular_file(file_status status) {
790 return status.type() == file_type::regular_file;
791}
792
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000793error_code is_regular_file(const Twine &path, bool &result) {
794 file_status st;
795 if (error_code ec = status(path, st))
796 return ec;
797 result = is_regular_file(st);
David Blaikie18544b92012-02-09 19:24:12 +0000798 return error_code::success();
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000799}
800
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000801bool is_symlink(file_status status) {
802 return status.type() == file_type::symlink_file;
803}
804
Michael J. Spencerd9960c62011-01-12 23:55:06 +0000805error_code is_symlink(const Twine &path, bool &result) {
806 file_status st;
807 if (error_code ec = status(path, st))
808 return ec;
809 result = is_symlink(st);
David Blaikie18544b92012-02-09 19:24:12 +0000810 return error_code::success();
Michael J. Spencerd9960c62011-01-12 23:55:06 +0000811}
812
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000813bool is_other(file_status status) {
814 return exists(status) &&
815 !is_regular_file(status) &&
816 !is_directory(status) &&
817 !is_symlink(status);
818}
819
Benjamin Kramer91ead3c2011-09-14 01:14:36 +0000820void directory_entry::replace_filename(const Twine &filename, file_status st) {
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000821 SmallString<128> path(Path.begin(), Path.end());
822 path::remove_filename(path);
823 path::append(path, filename);
824 Path = path.str();
825 Status = st;
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000826}
827
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000828error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000829 SmallString<32> MagicStorage;
Michael J. Spencer78874662011-01-15 18:52:41 +0000830 StringRef Magic = magic.toStringRef(MagicStorage);
831 SmallString<32> Buffer;
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000832
Michael J. Spencer78874662011-01-15 18:52:41 +0000833 if (error_code ec = get_magic(path, Magic.size(), Buffer)) {
834 if (ec == errc::value_too_large) {
835 // Magic.size() > file_size(Path).
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000836 result = false;
David Blaikie18544b92012-02-09 19:24:12 +0000837 return error_code::success();
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000838 }
Michael J. Spencer78874662011-01-15 18:52:41 +0000839 return ec;
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000840 }
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000841
Michael J. Spencer78874662011-01-15 18:52:41 +0000842 result = Magic == Buffer;
David Blaikie18544b92012-02-09 19:24:12 +0000843 return error_code::success();
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000844}
845
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000846/// @brief Identify the magic in magic.
Rafael Espindola9b404292013-06-11 17:22:12 +0000847 file_magic identify_magic(StringRef Magic) {
848 if (Magic.size() < 4)
Michael J. Spencer96ebd912012-06-19 05:29:57 +0000849 return file_magic::unknown;
Rafael Espindola9b404292013-06-11 17:22:12 +0000850 switch ((unsigned char)Magic[0]) {
Rui Ueyamafc149a62013-10-15 22:45:38 +0000851 case 0x00: {
Rui Ueyamae448f9e2013-11-15 21:22:02 +0000852 // COFF short import library file
853 if (Magic[1] == (char)0x00 && Magic[2] == (char)0xff &&
854 Magic[3] == (char)0xff)
855 return file_magic::coff_import_library;
Rui Ueyamafc149a62013-10-15 22:45:38 +0000856 // Windows resource file
Rui Ueyama50a86e12013-10-16 03:29:49 +0000857 const char Expected[] = { 0, 0, 0, 0, '\x20', 0, 0, 0, '\xff' };
Rui Ueyamafc149a62013-10-15 22:45:38 +0000858 if (Magic.size() >= sizeof(Expected) &&
859 memcmp(Magic.data(), Expected, sizeof(Expected)) == 0)
860 return file_magic::windows_resource;
Rui Ueyama829c4392013-11-14 22:09:08 +0000861 // 0x0000 = COFF unknown machine type
862 if (Magic[1] == 0)
863 return file_magic::coff_object;
Rui Ueyamafc149a62013-10-15 22:45:38 +0000864 break;
865 }
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000866 case 0xDE: // 0x0B17C0DE = BC wraper
Rafael Espindola9b404292013-06-11 17:22:12 +0000867 if (Magic[1] == (char)0xC0 && Magic[2] == (char)0x17 &&
868 Magic[3] == (char)0x0B)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000869 return file_magic::bitcode;
870 break;
871 case 'B':
Rafael Espindola9b404292013-06-11 17:22:12 +0000872 if (Magic[1] == 'C' && Magic[2] == (char)0xC0 && Magic[3] == (char)0xDE)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000873 return file_magic::bitcode;
874 break;
875 case '!':
Rafael Espindola9b404292013-06-11 17:22:12 +0000876 if (Magic.size() >= 8)
877 if (memcmp(Magic.data(),"!<arch>\n",8) == 0)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000878 return file_magic::archive;
879 break;
880
881 case '\177':
Rafael Espindola823de7c2013-06-11 17:25:45 +0000882 if (Magic.size() >= 18 && Magic[1] == 'E' && Magic[2] == 'L' &&
883 Magic[3] == 'F') {
Rafael Espindola9b404292013-06-11 17:22:12 +0000884 bool Data2MSB = Magic[5] == 2;
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000885 unsigned high = Data2MSB ? 16 : 17;
886 unsigned low = Data2MSB ? 17 : 16;
Rafael Espindola823de7c2013-06-11 17:25:45 +0000887 if (Magic[high] == 0)
Rafael Espindola9b404292013-06-11 17:22:12 +0000888 switch (Magic[low]) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000889 default: break;
890 case 1: return file_magic::elf_relocatable;
891 case 2: return file_magic::elf_executable;
892 case 3: return file_magic::elf_shared_object;
893 case 4: return file_magic::elf_core;
894 }
895 }
896 break;
897
898 case 0xCA:
Rafael Espindola9b404292013-06-11 17:22:12 +0000899 if (Magic[1] == char(0xFE) && Magic[2] == char(0xBA) &&
900 Magic[3] == char(0xBE)) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000901 // This is complicated by an overlap with Java class files.
902 // See the Mach-O section in /usr/share/file/magic for details.
Rafael Espindola9b404292013-06-11 17:22:12 +0000903 if (Magic.size() >= 8 && Magic[7] < 43)
Alexey Samsonove6388e62013-06-18 15:03:28 +0000904 return file_magic::macho_universal_binary;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000905 }
906 break;
907
908 // The two magic numbers for mach-o are:
909 // 0xfeedface - 32-bit mach-o
910 // 0xfeedfacf - 64-bit mach-o
911 case 0xFE:
912 case 0xCE:
913 case 0xCF: {
914 uint16_t type = 0;
Rafael Espindola9b404292013-06-11 17:22:12 +0000915 if (Magic[0] == char(0xFE) && Magic[1] == char(0xED) &&
916 Magic[2] == char(0xFA) &&
917 (Magic[3] == char(0xCE) || Magic[3] == char(0xCF))) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000918 /* Native endian */
Rafael Espindola9b404292013-06-11 17:22:12 +0000919 if (Magic.size() >= 16) type = Magic[14] << 8 | Magic[15];
920 } else if ((Magic[0] == char(0xCE) || Magic[0] == char(0xCF)) &&
921 Magic[1] == char(0xFA) && Magic[2] == char(0xED) &&
922 Magic[3] == char(0xFE)) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000923 /* Reverse endian */
Rafael Espindola9b404292013-06-11 17:22:12 +0000924 if (Magic.size() >= 14) type = Magic[13] << 8 | Magic[12];
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000925 }
926 switch (type) {
927 default: break;
928 case 1: return file_magic::macho_object;
929 case 2: return file_magic::macho_executable;
930 case 3: return file_magic::macho_fixed_virtual_memory_shared_lib;
931 case 4: return file_magic::macho_core;
Rafael Espindola134cc992013-06-10 20:32:27 +0000932 case 5: return file_magic::macho_preload_executable;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000933 case 6: return file_magic::macho_dynamically_linked_shared_lib;
934 case 7: return file_magic::macho_dynamic_linker;
935 case 8: return file_magic::macho_bundle;
936 case 9: return file_magic::macho_dynamic_linker;
937 case 10: return file_magic::macho_dsym_companion;
938 }
939 break;
940 }
941 case 0xF0: // PowerPC Windows
942 case 0x83: // Alpha 32-bit
943 case 0x84: // Alpha 64-bit
944 case 0x66: // MPS R4000 Windows
945 case 0x50: // mc68K
946 case 0x4c: // 80386 Windows
Rafael Espindola9b404292013-06-11 17:22:12 +0000947 if (Magic[1] == 0x01)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000948 return file_magic::coff_object;
949
950 case 0x90: // PA-RISC Windows
951 case 0x68: // mc68K Windows
Rafael Espindola9b404292013-06-11 17:22:12 +0000952 if (Magic[1] == 0x02)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000953 return file_magic::coff_object;
954 break;
955
956 case 0x4d: // Possible MS-DOS stub on Windows PE file
Rafael Espindola9b404292013-06-11 17:22:12 +0000957 if (Magic[1] == 0x5a) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000958 uint32_t off =
Rafael Espindola9b404292013-06-11 17:22:12 +0000959 *reinterpret_cast<const support::ulittle32_t*>(Magic.data() + 0x3c);
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000960 // PE/COFF file, either EXE or DLL.
Rafael Espindola9b404292013-06-11 17:22:12 +0000961 if (off < Magic.size() && memcmp(Magic.data() + off, "PE\0\0",4) == 0)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000962 return file_magic::pecoff_executable;
963 }
964 break;
965
966 case 0x64: // x86-64 Windows.
Rafael Espindola9b404292013-06-11 17:22:12 +0000967 if (Magic[1] == char(0x86))
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000968 return file_magic::coff_object;
969 break;
970
971 default:
972 break;
973 }
974 return file_magic::unknown;
975}
976
977error_code identify_magic(const Twine &path, file_magic &result) {
Michael J. Spencer94b2ab32011-01-15 20:39:36 +0000978 SmallString<32> Magic;
979 error_code ec = get_magic(path, Magic.capacity(), Magic);
980 if (ec && ec != errc::value_too_large)
981 return ec;
982
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000983 result = identify_magic(Magic);
David Blaikie18544b92012-02-09 19:24:12 +0000984 return error_code::success();
Michael J. Spencer94b2ab32011-01-15 20:39:36 +0000985}
986
Michael J. Spencerf8dc1862011-01-05 16:39:13 +0000987error_code directory_entry::status(file_status &result) const {
988 return fs::status(Path, result);
989}
990
Michael J. Spencer9fc1d9d2010-12-01 19:32:01 +0000991} // end namespace fs
992} // end namespace sys
993} // end namespace llvm
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000994
995// Include the truly platform-specific parts.
996#if defined(LLVM_ON_UNIX)
Rafael Espindolaf1fc3822013-06-26 19:33:03 +0000997#include "Unix/Path.inc"
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000998#endif
999#if defined(LLVM_ON_WIN32)
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001000#include "Windows/Path.inc"
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001001#endif