blob: 5b8be18db3f7137d7ee015092ea2b0c1a9694c66 [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
Rafael Espindola7a0b6402014-02-24 03:07:41 +000030using namespace llvm;
31
Michael J. Spencerebad2f92010-11-29 22:28:51 +000032namespace {
33 using llvm::StringRef;
Zhanyong Wan606bb1a2011-02-11 21:24:40 +000034 using llvm::sys::path::is_separator;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000035
36#ifdef LLVM_ON_WIN32
Benjamin Kramer5e4f0a42012-02-08 13:13:47 +000037 const char *separators = "\\/";
Alp Tokercb402912014-01-24 17:20:08 +000038 const char preferred_separator = '\\';
Michael J. Spencerebad2f92010-11-29 22:28:51 +000039#else
Benjamin Kramer5e4f0a42012-02-08 13:13:47 +000040 const char separators = '/';
Alp Tokercb402912014-01-24 17:20:08 +000041 const char preferred_separator = '/';
Michael J. Spencerebad2f92010-11-29 22:28:51 +000042#endif
43
Benjamin Kramercb520cd2010-12-17 18:59:09 +000044 StringRef find_first_component(StringRef path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000045 // Look for this first component in the following order.
46 // * empty (in this case we return an empty string)
47 // * either C: or {//,\\}net.
48 // * {/,\}
49 // * {.,..}
50 // * {file,directory}name
51
52 if (path.empty())
53 return path;
54
Michael J. Spencerbfea3c72010-12-07 03:57:48 +000055#ifdef LLVM_ON_WIN32
Michael J. Spencerebad2f92010-11-29 22:28:51 +000056 // C:
Guy Benyei83c74e92013-02-12 21:21:59 +000057 if (path.size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) &&
58 path[1] == ':')
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000059 return path.substr(0, 2);
Michael J. Spencerbfea3c72010-12-07 03:57:48 +000060#endif
Michael J. Spencerebad2f92010-11-29 22:28:51 +000061
62 // //net
63 if ((path.size() > 2) &&
Michael J. Spencerbfea3c72010-12-07 03:57:48 +000064 is_separator(path[0]) &&
65 path[0] == path[1] &&
66 !is_separator(path[2])) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000067 // Find the next directory separator.
Michael J. Spencerbfea3c72010-12-07 03:57:48 +000068 size_t end = path.find_first_of(separators, 2);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000069 return path.substr(0, end);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000070 }
71
72 // {/,\}
Michael J. Spencerbfea3c72010-12-07 03:57:48 +000073 if (is_separator(path[0]))
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000074 return path.substr(0, 1);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000075
76 if (path.startswith(".."))
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000077 return path.substr(0, 2);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000078
79 if (path[0] == '.')
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000080 return path.substr(0, 1);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000081
82 // * {file,directory}name
Tareq A. Siraj73537ea2013-08-12 17:10:49 +000083 size_t end = path.find_first_of(separators);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000084 return path.substr(0, end);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000085 }
Michael J. Spencer545cbdf2010-11-30 23:28:07 +000086
Benjamin Kramer292b44b2010-12-17 18:19:06 +000087 size_t filename_pos(StringRef str) {
Michael J. Spencer545cbdf2010-11-30 23:28:07 +000088 if (str.size() == 2 &&
89 is_separator(str[0]) &&
Michael J. Spencerbfea3c72010-12-07 03:57:48 +000090 str[0] == str[1])
Michael J. Spencer545cbdf2010-11-30 23:28:07 +000091 return 0;
92
93 if (str.size() > 0 && is_separator(str[str.size() - 1]))
94 return str.size() - 1;
95
96 size_t pos = str.find_last_of(separators, str.size() - 1);
97
98#ifdef LLVM_ON_WIN32
99 if (pos == StringRef::npos)
100 pos = str.find_last_of(':', str.size() - 2);
101#endif
102
103 if (pos == StringRef::npos ||
104 (pos == 1 && is_separator(str[0])))
105 return 0;
106
107 return pos + 1;
108 }
109
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000110 size_t root_dir_start(StringRef str) {
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000111 // case "c:/"
112#ifdef LLVM_ON_WIN32
113 if (str.size() > 2 &&
114 str[1] == ':' &&
115 is_separator(str[2]))
116 return 2;
117#endif
118
119 // case "//"
120 if (str.size() == 2 &&
121 is_separator(str[0]) &&
122 str[0] == str[1])
123 return StringRef::npos;
124
125 // case "//net"
126 if (str.size() > 3 &&
127 is_separator(str[0]) &&
128 str[0] == str[1] &&
129 !is_separator(str[2])) {
130 return str.find_first_of(separators, 2);
131 }
132
133 // case "/"
134 if (str.size() > 0 && is_separator(str[0]))
135 return 0;
136
137 return StringRef::npos;
138 }
139
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000140 size_t parent_path_end(StringRef path) {
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000141 size_t end_pos = filename_pos(path);
142
143 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
144
145 // Skip separators except for root dir.
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000146 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000147
148 while(end_pos > 0 &&
149 (end_pos - 1) != root_dir_pos &&
150 is_separator(path[end_pos - 1]))
151 --end_pos;
152
153 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
154 return StringRef::npos;
155
156 return end_pos;
157 }
Zhanyong Wan606bb1a2011-02-11 21:24:40 +0000158} // end unnamed namespace
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000159
Rafael Espindolae79a8722013-06-28 03:48:47 +0000160enum FSEntity {
161 FS_Dir,
162 FS_File,
163 FS_Name
164};
165
166// Implemented in Unix/Path.inc and Windows/Path.inc.
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000167static error_code TempDir(SmallVectorImpl<char> &result);
168
169static error_code createUniqueEntity(const Twine &Model, int &ResultFD,
170 SmallVectorImpl<char> &ResultPath,
171 bool MakeAbsolute, unsigned Mode,
172 FSEntity Type) {
173 SmallString<128> ModelStorage;
174 Model.toVector(ModelStorage);
175
176 if (MakeAbsolute) {
177 // Make model absolute by prepending a temp directory if it's not already.
178 if (!sys::path::is_absolute(Twine(ModelStorage))) {
179 SmallString<128> TDir;
180 if (error_code EC = TempDir(TDir))
181 return EC;
182 sys::path::append(TDir, Twine(ModelStorage));
183 ModelStorage.swap(TDir);
184 }
185 }
186
187 // From here on, DO NOT modify model. It may be needed if the randomly chosen
188 // path already exists.
189 ResultPath = ModelStorage;
190 // Null terminate.
191 ResultPath.push_back(0);
192 ResultPath.pop_back();
193
194retry_random_path:
195 // Replace '%' with random chars.
196 for (unsigned i = 0, e = ModelStorage.size(); i != e; ++i) {
197 if (ModelStorage[i] == '%')
198 ResultPath[i] = "0123456789abcdef"[sys::Process::GetRandomNumber() & 15];
199 }
200
201 // Try to open + create the file.
202 switch (Type) {
203 case FS_File: {
204 if (error_code EC = sys::fs::openFileForWrite(
205 Twine(ResultPath.begin()), ResultFD,
206 sys::fs::F_RW | sys::fs::F_Excl | sys::fs::F_Binary, Mode)) {
207 if (EC == errc::file_exists)
208 goto retry_random_path;
209 return EC;
210 }
211
212 return error_code::success();
213 }
214
215 case FS_Name: {
216 bool Exists;
217 error_code EC = sys::fs::exists(ResultPath.begin(), Exists);
218 if (EC)
219 return EC;
220 if (Exists)
221 goto retry_random_path;
222 return error_code::success();
223 }
224
225 case FS_Dir: {
226 if (error_code EC = sys::fs::create_directory(ResultPath.begin(), false)) {
227 if (EC == errc::file_exists)
228 goto retry_random_path;
229 return EC;
230 }
231 return error_code::success();
232 }
233 }
234 llvm_unreachable("Invalid Type");
235}
Rafael Espindolae79a8722013-06-28 03:48:47 +0000236
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000237namespace llvm {
238namespace sys {
239namespace path {
240
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000241const_iterator begin(StringRef path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000242 const_iterator i;
243 i.Path = path;
244 i.Component = find_first_component(path);
245 i.Position = 0;
246 return i;
247}
248
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000249const_iterator end(StringRef path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000250 const_iterator i;
251 i.Path = path;
252 i.Position = path.size();
253 return i;
254}
255
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000256const_iterator &const_iterator::operator++() {
257 assert(Position < Path.size() && "Tried to increment past end!");
258
259 // Increment Position to past the current component
260 Position += Component.size();
261
262 // Check for end.
263 if (Position == Path.size()) {
264 Component = StringRef();
265 return *this;
266 }
267
268 // Both POSIX and Windows treat paths that begin with exactly two separators
269 // specially.
270 bool was_net = Component.size() > 2 &&
271 is_separator(Component[0]) &&
272 Component[1] == Component[0] &&
273 !is_separator(Component[2]);
274
275 // Handle separators.
276 if (is_separator(Path[Position])) {
277 // Root dir.
278 if (was_net
279#ifdef LLVM_ON_WIN32
280 // c:/
281 || Component.endswith(":")
282#endif
283 ) {
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000284 Component = Path.substr(Position, 1);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000285 return *this;
286 }
287
288 // Skip extra separators.
289 while (Position != Path.size() &&
290 is_separator(Path[Position])) {
291 ++Position;
292 }
293
294 // Treat trailing '/' as a '.'.
295 if (Position == Path.size()) {
296 --Position;
297 Component = ".";
298 return *this;
299 }
300 }
301
302 // Find next component.
303 size_t end_pos = Path.find_first_of(separators, Position);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000304 Component = Path.slice(Position, end_pos);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000305
306 return *this;
307}
308
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000309const_iterator &const_iterator::operator--() {
310 // If we're at the end and the previous char was a '/', return '.'.
311 if (Position == Path.size() &&
312 Path.size() > 1 &&
313 is_separator(Path[Position - 1])
314#ifdef LLVM_ON_WIN32
315 && Path[Position - 2] != ':'
316#endif
317 ) {
318 --Position;
319 Component = ".";
320 return *this;
321 }
322
323 // Skip separators unless it's the root directory.
324 size_t root_dir_pos = root_dir_start(Path);
325 size_t end_pos = Position;
326
327 while(end_pos > 0 &&
328 (end_pos - 1) != root_dir_pos &&
329 is_separator(Path[end_pos - 1]))
330 --end_pos;
331
332 // Find next separator.
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000333 size_t start_pos = filename_pos(Path.substr(0, end_pos));
334 Component = Path.slice(start_pos, end_pos);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000335 Position = start_pos;
336 return *this;
337}
338
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000339bool const_iterator::operator==(const const_iterator &RHS) const {
340 return Path.begin() == RHS.Path.begin() &&
341 Position == RHS.Position;
342}
343
344bool const_iterator::operator!=(const const_iterator &RHS) const {
345 return !(*this == RHS);
346}
347
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000348ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
349 return Position - RHS.Position;
350}
351
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000352const StringRef root_path(StringRef path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000353 const_iterator b = begin(path),
354 pos = b,
355 e = end(path);
356 if (b != e) {
357 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
358 bool has_drive =
359#ifdef LLVM_ON_WIN32
360 b->endswith(":");
361#else
362 false;
363#endif
364
365 if (has_net || has_drive) {
366 if ((++pos != e) && is_separator((*pos)[0])) {
367 // {C:/,//net/}, so get the first two components.
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000368 return path.substr(0, b->size() + pos->size());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000369 } else {
370 // just {C:,//net}, return the first component.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000371 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000372 }
373 }
374
375 // POSIX style root directory.
376 if (is_separator((*b)[0])) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000377 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000378 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000379 }
380
Michael J. Spencerf616b212010-12-07 17:04:04 +0000381 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000382}
383
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000384const StringRef root_name(StringRef path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000385 const_iterator b = begin(path),
386 e = end(path);
387 if (b != e) {
388 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
389 bool has_drive =
390#ifdef LLVM_ON_WIN32
391 b->endswith(":");
392#else
393 false;
394#endif
395
396 if (has_net || has_drive) {
397 // just {C:,//net}, return the first component.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000398 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000399 }
400 }
401
402 // No path or no name.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000403 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000404}
405
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000406const StringRef root_directory(StringRef path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000407 const_iterator b = begin(path),
408 pos = b,
409 e = end(path);
410 if (b != e) {
411 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
412 bool has_drive =
413#ifdef LLVM_ON_WIN32
414 b->endswith(":");
415#else
416 false;
417#endif
418
419 if ((has_net || has_drive) &&
420 // {C:,//net}, skip to the next component.
421 (++pos != e) && is_separator((*pos)[0])) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000422 return *pos;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000423 }
424
425 // POSIX style root directory.
426 if (!has_net && is_separator((*b)[0])) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000427 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000428 }
429 }
430
431 // No path or no root.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000432 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000433}
434
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000435const StringRef relative_path(StringRef path) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000436 StringRef root = root_path(path);
Michael J. Spencere6462392012-02-29 00:06:24 +0000437 return path.substr(root.size());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000438}
439
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000440void append(SmallVectorImpl<char> &path, const Twine &a,
441 const Twine &b,
442 const Twine &c,
443 const Twine &d) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000444 SmallString<32> a_storage;
445 SmallString<32> b_storage;
446 SmallString<32> c_storage;
447 SmallString<32> d_storage;
448
449 SmallVector<StringRef, 4> components;
450 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
451 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
452 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
453 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
454
455 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
456 e = components.end();
457 i != e; ++i) {
458 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
459 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
Michael J. Spencerf616b212010-12-07 17:04:04 +0000460 bool is_root_name = has_root_name(*i);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000461
462 if (path_has_sep) {
463 // Strip separators from beginning of component.
464 size_t loc = i->find_first_not_of(separators);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000465 StringRef c = i->substr(loc);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000466
467 // Append it.
468 path.append(c.begin(), c.end());
469 continue;
470 }
471
Michael J. Spencer95e4ac12010-12-06 04:28:23 +0000472 if (!component_has_sep && !(path.empty() || is_root_name)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000473 // Add a separator.
Alp Tokercb402912014-01-24 17:20:08 +0000474 path.push_back(preferred_separator);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000475 }
476
477 path.append(i->begin(), i->end());
478 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000479}
480
Argyrios Kyrtzidisa61736f2011-02-15 17:51:19 +0000481void append(SmallVectorImpl<char> &path,
482 const_iterator begin, const_iterator end) {
483 for (; begin != end; ++begin)
484 path::append(path, *begin);
485}
486
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000487const StringRef parent_path(StringRef path) {
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000488 size_t end_pos = parent_path_end(path);
489 if (end_pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000490 return StringRef();
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000491 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000492 return path.substr(0, end_pos);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000493}
494
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000495void remove_filename(SmallVectorImpl<char> &path) {
Michael J. Spencer9c594092010-12-01 00:52:28 +0000496 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000497 if (end_pos != StringRef::npos)
498 path.set_size(end_pos);
Michael J. Spencer9c594092010-12-01 00:52:28 +0000499}
500
Michael J. Spencerf616b212010-12-07 17:04:04 +0000501void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
Michael J. Spencerfb3a95d2010-12-01 00:52:55 +0000502 StringRef p(path.begin(), path.size());
503 SmallString<32> ext_storage;
504 StringRef ext = extension.toStringRef(ext_storage);
505
506 // Erase existing extension.
507 size_t pos = p.find_last_of('.');
508 if (pos != StringRef::npos && pos >= filename_pos(p))
509 path.set_size(pos);
510
511 // Append '.' if needed.
512 if (ext.size() > 0 && ext[0] != '.')
513 path.push_back('.');
514
515 // Append extension.
516 path.append(ext.begin(), ext.end());
Michael J. Spencerfb3a95d2010-12-01 00:52:55 +0000517}
518
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000519void native(const Twine &path, SmallVectorImpl<char> &result) {
Benjamin Kramerbd4ac9b2013-09-11 10:45:21 +0000520 assert((!path.isSingleStringRef() ||
521 path.getSingleStringRef().data() != result.data()) &&
522 "path and result are not allowed to overlap!");
Michael J. Spencer80025002010-12-01 02:48:27 +0000523 // Clear result.
Michael J. Spencer98c7a112010-12-07 01:23:19 +0000524 result.clear();
Michael J. Spencer80025002010-12-01 02:48:27 +0000525 path.toVector(result);
Benjamin Kramerbd4ac9b2013-09-11 10:45:21 +0000526 native(result);
527}
528
529void native(SmallVectorImpl<char> &path) {
530#ifdef LLVM_ON_WIN32
531 std::replace(path.begin(), path.end(), '/', '\\');
Michael J. Spencer80025002010-12-01 02:48:27 +0000532#endif
Michael J. Spencer80025002010-12-01 02:48:27 +0000533}
534
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000535const StringRef filename(StringRef path) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000536 return *(--end(path));
Michael J. Spencer14269202010-12-01 03:18:17 +0000537}
538
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000539const StringRef stem(StringRef path) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000540 StringRef fname = filename(path);
Michael J. Spencer956955e2010-12-01 03:18:33 +0000541 size_t pos = fname.find_last_of('.');
542 if (pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000543 return fname;
Michael J. Spencer956955e2010-12-01 03:18:33 +0000544 else
545 if ((fname.size() == 1 && fname == ".") ||
546 (fname.size() == 2 && fname == ".."))
Michael J. Spencerf616b212010-12-07 17:04:04 +0000547 return fname;
Michael J. Spencer956955e2010-12-01 03:18:33 +0000548 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000549 return fname.substr(0, pos);
Michael J. Spencer956955e2010-12-01 03:18:33 +0000550}
551
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000552const StringRef extension(StringRef path) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000553 StringRef fname = filename(path);
Michael J. Spencer87106c52010-12-01 03:37:41 +0000554 size_t pos = fname.find_last_of('.');
555 if (pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000556 return StringRef();
Michael J. Spencer87106c52010-12-01 03:37:41 +0000557 else
558 if ((fname.size() == 1 && fname == ".") ||
559 (fname.size() == 2 && fname == ".."))
Michael J. Spencerf616b212010-12-07 17:04:04 +0000560 return StringRef();
Michael J. Spencer87106c52010-12-01 03:37:41 +0000561 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000562 return fname.substr(pos);
Michael J. Spencer87106c52010-12-01 03:37:41 +0000563}
564
Zhanyong Wan606bb1a2011-02-11 21:24:40 +0000565bool is_separator(char value) {
566 switch(value) {
567#ifdef LLVM_ON_WIN32
568 case '\\': // fall through
569#endif
570 case '/': return true;
571 default: return false;
572 }
573}
574
Douglas Gregor123dc702011-09-14 20:27:01 +0000575void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result) {
576 result.clear();
Michael J. Spencer7dfbeda2011-12-13 23:16:15 +0000577
David Fang6860c812014-02-12 21:02:12 +0000578#if defined(_CS_DARWIN_USER_TEMP_DIR) && defined(_CS_DARWIN_USER_CACHE_DIR)
Douglas Gregora86ddf02013-03-21 21:46:10 +0000579 // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR.
David Fang6860c812014-02-12 21:02:12 +0000580 // macros defined in <unistd.h> on darwin >= 9
Douglas Gregora86ddf02013-03-21 21:46:10 +0000581 int ConfName = erasedOnReboot? _CS_DARWIN_USER_TEMP_DIR
582 : _CS_DARWIN_USER_CACHE_DIR;
583 size_t ConfLen = confstr(ConfName, 0, 0);
584 if (ConfLen > 0) {
585 do {
586 result.resize(ConfLen);
587 ConfLen = confstr(ConfName, result.data(), result.size());
588 } while (ConfLen > 0 && ConfLen != result.size());
589
590 if (ConfLen > 0) {
591 assert(result.back() == 0);
592 result.pop_back();
593 return;
594 }
595
596 result.clear();
597 }
598#endif
599
Douglas Gregor123dc702011-09-14 20:27:01 +0000600 // Check whether the temporary directory is specified by an environment
601 // variable.
602 const char *EnvironmentVariable;
603#ifdef LLVM_ON_WIN32
604 EnvironmentVariable = "TEMP";
605#else
606 EnvironmentVariable = "TMPDIR";
607#endif
608 if (char *RequestedDir = getenv(EnvironmentVariable)) {
609 result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
610 return;
611 }
Michael J. Spencer7dfbeda2011-12-13 23:16:15 +0000612
Douglas Gregor123dc702011-09-14 20:27:01 +0000613 // Fall back to a system default.
614 const char *DefaultResult;
615#ifdef LLVM_ON_WIN32
616 (void)erasedOnReboot;
Douglas Gregor8b744542011-09-14 23:21:47 +0000617 DefaultResult = "C:\\TEMP";
Douglas Gregor123dc702011-09-14 20:27:01 +0000618#else
619 if (erasedOnReboot)
620 DefaultResult = "/tmp";
621 else
622 DefaultResult = "/var/tmp";
623#endif
624 result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
625}
Michael J. Spencer7dfbeda2011-12-13 23:16:15 +0000626
Michael J. Spencera68282c2010-12-07 18:12:07 +0000627bool has_root_name(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000628 SmallString<128> path_storage;
629 StringRef p = path.toStringRef(path_storage);
630
Michael J. Spencerf616b212010-12-07 17:04:04 +0000631 return !root_name(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000632}
633
Michael J. Spencera68282c2010-12-07 18:12:07 +0000634bool has_root_directory(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000635 SmallString<128> path_storage;
636 StringRef p = path.toStringRef(path_storage);
637
Michael J. Spencerf616b212010-12-07 17:04:04 +0000638 return !root_directory(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000639}
640
Michael J. Spencera68282c2010-12-07 18:12:07 +0000641bool has_root_path(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000642 SmallString<128> path_storage;
643 StringRef p = path.toStringRef(path_storage);
644
Michael J. Spencerf616b212010-12-07 17:04:04 +0000645 return !root_path(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000646}
647
Michael J. Spencer6d4b7e72010-12-20 13:30:28 +0000648bool has_relative_path(const Twine &path) {
649 SmallString<128> path_storage;
650 StringRef p = path.toStringRef(path_storage);
651
652 return !relative_path(p).empty();
653}
654
Michael J. Spencera68282c2010-12-07 18:12:07 +0000655bool has_filename(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000656 SmallString<128> path_storage;
657 StringRef p = path.toStringRef(path_storage);
658
Michael J. Spencerf616b212010-12-07 17:04:04 +0000659 return !filename(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000660}
661
Michael J. Spencera68282c2010-12-07 18:12:07 +0000662bool has_parent_path(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000663 SmallString<128> path_storage;
664 StringRef p = path.toStringRef(path_storage);
665
Michael J. Spencerf616b212010-12-07 17:04:04 +0000666 return !parent_path(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000667}
668
Michael J. Spencera68282c2010-12-07 18:12:07 +0000669bool has_stem(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000670 SmallString<128> path_storage;
671 StringRef p = path.toStringRef(path_storage);
672
Michael J. Spencerf616b212010-12-07 17:04:04 +0000673 return !stem(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000674}
675
Michael J. Spencera68282c2010-12-07 18:12:07 +0000676bool has_extension(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000677 SmallString<128> path_storage;
678 StringRef p = path.toStringRef(path_storage);
679
Michael J. Spencerf616b212010-12-07 17:04:04 +0000680 return !extension(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000681}
682
Michael J. Spencera68282c2010-12-07 18:12:07 +0000683bool is_absolute(const Twine &path) {
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000684 SmallString<128> path_storage;
685 StringRef p = path.toStringRef(path_storage);
686
Michael J. Spencerf616b212010-12-07 17:04:04 +0000687 bool rootDir = has_root_directory(p),
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000688#ifdef LLVM_ON_WIN32
Michael J. Spencerf616b212010-12-07 17:04:04 +0000689 rootName = has_root_name(p);
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000690#else
Michael J. Spencerf616b212010-12-07 17:04:04 +0000691 rootName = true;
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000692#endif
693
Michael J. Spencerf616b212010-12-07 17:04:04 +0000694 return rootDir && rootName;
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000695}
696
Michael J. Spencera68282c2010-12-07 18:12:07 +0000697bool is_relative(const Twine &path) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000698 return !is_absolute(path);
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000699}
700
Michael J. Spencer9fc1d9d2010-12-01 19:32:01 +0000701} // end namespace path
702
703namespace fs {
704
Rafael Espindola7f822a92013-07-29 21:26:49 +0000705error_code getUniqueID(const Twine Path, UniqueID &Result) {
706 file_status Status;
707 error_code EC = status(Path, Status);
708 if (EC)
709 return EC;
710 Result = Status.getUniqueID();
711 return error_code::success();
712}
713
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000714error_code createUniqueFile(const Twine &Model, int &ResultFd,
715 SmallVectorImpl<char> &ResultPath, unsigned Mode) {
716 return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
717}
718
719error_code createUniqueFile(const Twine &Model,
720 SmallVectorImpl<char> &ResultPath) {
721 int Dummy;
722 return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
723}
724
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000725static error_code createTemporaryFile(const Twine &Model, int &ResultFD,
726 llvm::SmallVectorImpl<char> &ResultPath,
727 FSEntity Type) {
728 SmallString<128> Storage;
729 StringRef P = Model.toNullTerminatedStringRef(Storage);
730 assert(P.find_first_of(separators) == StringRef::npos &&
731 "Model must be a simple filename.");
732 // Use P.begin() so that createUniqueEntity doesn't need to recreate Storage.
733 return createUniqueEntity(P.begin(), ResultFD, ResultPath,
734 true, owner_read | owner_write, Type);
735}
736
737static error_code
738createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
739 llvm::SmallVectorImpl<char> &ResultPath,
740 FSEntity Type) {
Rafael Espindolad3c89042013-07-25 15:00:17 +0000741 const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
742 return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000743 Type);
744}
745
746
747error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
748 int &ResultFD,
749 SmallVectorImpl<char> &ResultPath) {
750 return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File);
751}
752
753error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
754 SmallVectorImpl<char> &ResultPath) {
755 int Dummy;
756 return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
757}
758
759
Rafael Espindolae79a8722013-06-28 03:48:47 +0000760// This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
Rafael Espindola31a24432013-06-28 10:55:41 +0000761// for consistency. We should try using mkdtemp.
Rafael Espindola7ffacc42013-06-27 03:45:31 +0000762error_code createUniqueDirectory(const Twine &Prefix,
763 SmallVectorImpl<char> &ResultPath) {
Rafael Espindolae79a8722013-06-28 03:48:47 +0000764 int Dummy;
765 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath,
766 true, 0, FS_Dir);
Rafael Espindola7ffacc42013-06-27 03:45:31 +0000767}
768
Michael J. Spencer92903a32010-12-07 03:57:17 +0000769error_code make_absolute(SmallVectorImpl<char> &path) {
770 StringRef p(path.data(), path.size());
771
Daniel Dunbarb23158c2012-02-29 00:20:37 +0000772 bool rootDirectory = path::has_root_directory(p),
773#ifdef LLVM_ON_WIN32
Daniel Dunbar39ea4582012-02-29 00:46:46 +0000774 rootName = path::has_root_name(p);
Daniel Dunbarb23158c2012-02-29 00:20:37 +0000775#else
776 rootName = true;
777#endif
Michael J. Spencer92903a32010-12-07 03:57:17 +0000778
779 // Already absolute.
780 if (rootName && rootDirectory)
David Blaikie18544b92012-02-09 19:24:12 +0000781 return error_code::success();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000782
783 // All of the following conditions will need the current directory.
784 SmallString<128> current_dir;
785 if (error_code ec = current_path(current_dir)) return ec;
786
787 // Relative path. Prepend the current directory.
788 if (!rootName && !rootDirectory) {
789 // Append path to the current directory.
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000790 path::append(current_dir, p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000791 // Set path to the result.
792 path.swap(current_dir);
David Blaikie18544b92012-02-09 19:24:12 +0000793 return error_code::success();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000794 }
795
796 if (!rootName && rootDirectory) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000797 StringRef cdrn = path::root_name(current_dir);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000798 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000799 path::append(curDirRootName, p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000800 // Set path to the result.
801 path.swap(curDirRootName);
David Blaikie18544b92012-02-09 19:24:12 +0000802 return error_code::success();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000803 }
804
805 if (rootName && !rootDirectory) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000806 StringRef pRootName = path::root_name(p);
807 StringRef bRootDirectory = path::root_directory(current_dir);
808 StringRef bRelativePath = path::relative_path(current_dir);
809 StringRef pRelativePath = path::relative_path(p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000810
811 SmallString<128> res;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000812 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000813 path.swap(res);
David Blaikie18544b92012-02-09 19:24:12 +0000814 return error_code::success();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000815 }
816
817 llvm_unreachable("All rootName and rootDirectory combinations should have "
818 "occurred above!");
819}
820
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000821error_code create_directories(const Twine &Path, bool IgnoreExisting) {
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000822 SmallString<128> PathStorage;
823 StringRef P = Path.toStringRef(PathStorage);
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000824
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000825 // Be optimistic and try to create the directory
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000826 error_code EC = create_directory(P, IgnoreExisting);
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000827 // If we succeeded, or had any error other than the parent not existing, just
828 // return it.
829 if (EC != errc::no_such_file_or_directory)
830 return EC;
Michael J. Spencerf616b212010-12-07 17:04:04 +0000831
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000832 // We failed because of a no_such_file_or_directory, try to create the
833 // parent.
834 StringRef Parent = path::parent_path(P);
835 if (Parent.empty())
836 return EC;
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000837
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000838 if ((EC = create_directories(Parent)))
839 return EC;
840
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000841 return create_directory(P, IgnoreExisting);
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000842}
843
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000844bool exists(file_status status) {
845 return status_known(status) && status.type() != file_type::file_not_found;
846}
847
848bool status_known(file_status s) {
849 return s.type() != file_type::status_error;
850}
851
852bool is_directory(file_status status) {
853 return status.type() == file_type::directory_file;
854}
855
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000856error_code is_directory(const Twine &path, bool &result) {
857 file_status st;
858 if (error_code ec = status(path, st))
859 return ec;
860 result = is_directory(st);
David Blaikie18544b92012-02-09 19:24:12 +0000861 return error_code::success();
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000862}
863
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000864bool is_regular_file(file_status status) {
865 return status.type() == file_type::regular_file;
866}
867
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000868error_code is_regular_file(const Twine &path, bool &result) {
869 file_status st;
870 if (error_code ec = status(path, st))
871 return ec;
872 result = is_regular_file(st);
David Blaikie18544b92012-02-09 19:24:12 +0000873 return error_code::success();
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000874}
875
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000876bool is_symlink(file_status status) {
877 return status.type() == file_type::symlink_file;
878}
879
Michael J. Spencerd9960c62011-01-12 23:55:06 +0000880error_code is_symlink(const Twine &path, bool &result) {
881 file_status st;
882 if (error_code ec = status(path, st))
883 return ec;
884 result = is_symlink(st);
David Blaikie18544b92012-02-09 19:24:12 +0000885 return error_code::success();
Michael J. Spencerd9960c62011-01-12 23:55:06 +0000886}
887
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000888bool is_other(file_status status) {
889 return exists(status) &&
890 !is_regular_file(status) &&
891 !is_directory(status) &&
892 !is_symlink(status);
893}
894
Benjamin Kramer91ead3c2011-09-14 01:14:36 +0000895void directory_entry::replace_filename(const Twine &filename, file_status st) {
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000896 SmallString<128> path(Path.begin(), Path.end());
897 path::remove_filename(path);
898 path::append(path, filename);
899 Path = path.str();
900 Status = st;
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000901}
902
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000903error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000904 SmallString<32> MagicStorage;
Michael J. Spencer78874662011-01-15 18:52:41 +0000905 StringRef Magic = magic.toStringRef(MagicStorage);
906 SmallString<32> Buffer;
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000907
Michael J. Spencer78874662011-01-15 18:52:41 +0000908 if (error_code ec = get_magic(path, Magic.size(), Buffer)) {
909 if (ec == errc::value_too_large) {
910 // Magic.size() > file_size(Path).
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000911 result = false;
David Blaikie18544b92012-02-09 19:24:12 +0000912 return error_code::success();
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000913 }
Michael J. Spencer78874662011-01-15 18:52:41 +0000914 return ec;
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000915 }
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000916
Michael J. Spencer78874662011-01-15 18:52:41 +0000917 result = Magic == Buffer;
David Blaikie18544b92012-02-09 19:24:12 +0000918 return error_code::success();
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000919}
920
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000921/// @brief Identify the magic in magic.
Rafael Espindola9b404292013-06-11 17:22:12 +0000922 file_magic identify_magic(StringRef Magic) {
923 if (Magic.size() < 4)
Michael J. Spencer96ebd912012-06-19 05:29:57 +0000924 return file_magic::unknown;
Rafael Espindola9b404292013-06-11 17:22:12 +0000925 switch ((unsigned char)Magic[0]) {
Rui Ueyamafc149a62013-10-15 22:45:38 +0000926 case 0x00: {
Rui Ueyamae448f9e2013-11-15 21:22:02 +0000927 // COFF short import library file
928 if (Magic[1] == (char)0x00 && Magic[2] == (char)0xff &&
929 Magic[3] == (char)0xff)
930 return file_magic::coff_import_library;
Rui Ueyamafc149a62013-10-15 22:45:38 +0000931 // Windows resource file
Rui Ueyama50a86e12013-10-16 03:29:49 +0000932 const char Expected[] = { 0, 0, 0, 0, '\x20', 0, 0, 0, '\xff' };
Rui Ueyamafc149a62013-10-15 22:45:38 +0000933 if (Magic.size() >= sizeof(Expected) &&
934 memcmp(Magic.data(), Expected, sizeof(Expected)) == 0)
935 return file_magic::windows_resource;
Rui Ueyama829c4392013-11-14 22:09:08 +0000936 // 0x0000 = COFF unknown machine type
937 if (Magic[1] == 0)
938 return file_magic::coff_object;
Rui Ueyamafc149a62013-10-15 22:45:38 +0000939 break;
940 }
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000941 case 0xDE: // 0x0B17C0DE = BC wraper
Rafael Espindola9b404292013-06-11 17:22:12 +0000942 if (Magic[1] == (char)0xC0 && Magic[2] == (char)0x17 &&
943 Magic[3] == (char)0x0B)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000944 return file_magic::bitcode;
945 break;
946 case 'B':
Rafael Espindola9b404292013-06-11 17:22:12 +0000947 if (Magic[1] == 'C' && Magic[2] == (char)0xC0 && Magic[3] == (char)0xDE)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000948 return file_magic::bitcode;
949 break;
950 case '!':
Rafael Espindola9b404292013-06-11 17:22:12 +0000951 if (Magic.size() >= 8)
952 if (memcmp(Magic.data(),"!<arch>\n",8) == 0)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000953 return file_magic::archive;
954 break;
955
956 case '\177':
Rafael Espindola823de7c2013-06-11 17:25:45 +0000957 if (Magic.size() >= 18 && Magic[1] == 'E' && Magic[2] == 'L' &&
958 Magic[3] == 'F') {
Rafael Espindola9b404292013-06-11 17:22:12 +0000959 bool Data2MSB = Magic[5] == 2;
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000960 unsigned high = Data2MSB ? 16 : 17;
961 unsigned low = Data2MSB ? 17 : 16;
Rafael Espindola823de7c2013-06-11 17:25:45 +0000962 if (Magic[high] == 0)
Rafael Espindola9b404292013-06-11 17:22:12 +0000963 switch (Magic[low]) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000964 default: break;
965 case 1: return file_magic::elf_relocatable;
966 case 2: return file_magic::elf_executable;
967 case 3: return file_magic::elf_shared_object;
968 case 4: return file_magic::elf_core;
969 }
970 }
971 break;
972
973 case 0xCA:
Rafael Espindola9b404292013-06-11 17:22:12 +0000974 if (Magic[1] == char(0xFE) && Magic[2] == char(0xBA) &&
975 Magic[3] == char(0xBE)) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000976 // This is complicated by an overlap with Java class files.
977 // See the Mach-O section in /usr/share/file/magic for details.
Rafael Espindola9b404292013-06-11 17:22:12 +0000978 if (Magic.size() >= 8 && Magic[7] < 43)
Alexey Samsonove6388e62013-06-18 15:03:28 +0000979 return file_magic::macho_universal_binary;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000980 }
981 break;
982
983 // The two magic numbers for mach-o are:
984 // 0xfeedface - 32-bit mach-o
985 // 0xfeedfacf - 64-bit mach-o
986 case 0xFE:
987 case 0xCE:
988 case 0xCF: {
989 uint16_t type = 0;
Rafael Espindola9b404292013-06-11 17:22:12 +0000990 if (Magic[0] == char(0xFE) && Magic[1] == char(0xED) &&
991 Magic[2] == char(0xFA) &&
992 (Magic[3] == char(0xCE) || Magic[3] == char(0xCF))) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000993 /* Native endian */
Rafael Espindola9b404292013-06-11 17:22:12 +0000994 if (Magic.size() >= 16) type = Magic[14] << 8 | Magic[15];
995 } else if ((Magic[0] == char(0xCE) || Magic[0] == char(0xCF)) &&
996 Magic[1] == char(0xFA) && Magic[2] == char(0xED) &&
997 Magic[3] == char(0xFE)) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000998 /* Reverse endian */
Rafael Espindola9b404292013-06-11 17:22:12 +0000999 if (Magic.size() >= 14) type = Magic[13] << 8 | Magic[12];
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001000 }
1001 switch (type) {
1002 default: break;
1003 case 1: return file_magic::macho_object;
1004 case 2: return file_magic::macho_executable;
1005 case 3: return file_magic::macho_fixed_virtual_memory_shared_lib;
1006 case 4: return file_magic::macho_core;
Rafael Espindola134cc992013-06-10 20:32:27 +00001007 case 5: return file_magic::macho_preload_executable;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001008 case 6: return file_magic::macho_dynamically_linked_shared_lib;
1009 case 7: return file_magic::macho_dynamic_linker;
1010 case 8: return file_magic::macho_bundle;
1011 case 9: return file_magic::macho_dynamic_linker;
1012 case 10: return file_magic::macho_dsym_companion;
1013 }
1014 break;
1015 }
1016 case 0xF0: // PowerPC Windows
1017 case 0x83: // Alpha 32-bit
1018 case 0x84: // Alpha 64-bit
1019 case 0x66: // MPS R4000 Windows
1020 case 0x50: // mc68K
1021 case 0x4c: // 80386 Windows
Rafael Espindola9b404292013-06-11 17:22:12 +00001022 if (Magic[1] == 0x01)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001023 return file_magic::coff_object;
1024
1025 case 0x90: // PA-RISC Windows
1026 case 0x68: // mc68K Windows
Rafael Espindola9b404292013-06-11 17:22:12 +00001027 if (Magic[1] == 0x02)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001028 return file_magic::coff_object;
1029 break;
1030
1031 case 0x4d: // Possible MS-DOS stub on Windows PE file
Rafael Espindola9b404292013-06-11 17:22:12 +00001032 if (Magic[1] == 0x5a) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001033 uint32_t off =
Rafael Espindola9b404292013-06-11 17:22:12 +00001034 *reinterpret_cast<const support::ulittle32_t*>(Magic.data() + 0x3c);
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001035 // PE/COFF file, either EXE or DLL.
Rafael Espindola9b404292013-06-11 17:22:12 +00001036 if (off < Magic.size() && memcmp(Magic.data() + off, "PE\0\0",4) == 0)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001037 return file_magic::pecoff_executable;
1038 }
1039 break;
1040
1041 case 0x64: // x86-64 Windows.
Rafael Espindola9b404292013-06-11 17:22:12 +00001042 if (Magic[1] == char(0x86))
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001043 return file_magic::coff_object;
1044 break;
1045
1046 default:
1047 break;
1048 }
1049 return file_magic::unknown;
1050}
1051
1052error_code identify_magic(const Twine &path, file_magic &result) {
Michael J. Spencer94b2ab32011-01-15 20:39:36 +00001053 SmallString<32> Magic;
1054 error_code ec = get_magic(path, Magic.capacity(), Magic);
1055 if (ec && ec != errc::value_too_large)
1056 return ec;
1057
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001058 result = identify_magic(Magic);
David Blaikie18544b92012-02-09 19:24:12 +00001059 return error_code::success();
Michael J. Spencer94b2ab32011-01-15 20:39:36 +00001060}
1061
Michael J. Spencerf8dc1862011-01-05 16:39:13 +00001062error_code directory_entry::status(file_status &result) const {
1063 return fs::status(Path, result);
1064}
1065
Michael J. Spencer9fc1d9d2010-12-01 19:32:01 +00001066} // end namespace fs
1067} // end namespace sys
1068} // end namespace llvm
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001069
1070// Include the truly platform-specific parts.
1071#if defined(LLVM_ON_UNIX)
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001072#include "Unix/Path.inc"
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001073#endif
1074#if defined(LLVM_ON_WIN32)
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001075#include "Windows/Path.inc"
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001076#endif