blob: 6fdad22158d13b09032774c90df2014eff695606 [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: {
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000204 if (error_code EC =
205 sys::fs::openFileForWrite(Twine(ResultPath.begin()), ResultFD,
206 sys::fs::F_RW | sys::fs::F_Excl, Mode)) {
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000207 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--() {
Ben Langmuir8d116392014-03-05 19:56:30 +0000310 // If we're at the end and the previous char was a '/', return '.' unless
311 // we are the root path.
312 size_t root_dir_pos = root_dir_start(Path);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000313 if (Position == Path.size() &&
Ben Langmuir8d116392014-03-05 19:56:30 +0000314 Path.size() > root_dir_pos + 1 &&
315 is_separator(Path[Position - 1])) {
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000316 --Position;
317 Component = ".";
318 return *this;
319 }
320
321 // Skip separators unless it's the root directory.
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000322 size_t end_pos = Position;
323
324 while(end_pos > 0 &&
325 (end_pos - 1) != root_dir_pos &&
326 is_separator(Path[end_pos - 1]))
327 --end_pos;
328
329 // Find next separator.
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000330 size_t start_pos = filename_pos(Path.substr(0, end_pos));
331 Component = Path.slice(start_pos, end_pos);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000332 Position = start_pos;
333 return *this;
334}
335
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000336bool const_iterator::operator==(const const_iterator &RHS) const {
337 return Path.begin() == RHS.Path.begin() &&
338 Position == RHS.Position;
339}
340
341bool const_iterator::operator!=(const const_iterator &RHS) const {
342 return !(*this == RHS);
343}
344
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000345ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
346 return Position - RHS.Position;
347}
348
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000349const StringRef root_path(StringRef path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000350 const_iterator b = begin(path),
351 pos = b,
352 e = end(path);
353 if (b != e) {
354 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
355 bool has_drive =
356#ifdef LLVM_ON_WIN32
357 b->endswith(":");
358#else
359 false;
360#endif
361
362 if (has_net || has_drive) {
363 if ((++pos != e) && is_separator((*pos)[0])) {
364 // {C:/,//net/}, so get the first two components.
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000365 return path.substr(0, b->size() + pos->size());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000366 } else {
367 // just {C:,//net}, return the first component.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000368 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000369 }
370 }
371
372 // POSIX style root directory.
373 if (is_separator((*b)[0])) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000374 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000375 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000376 }
377
Michael J. Spencerf616b212010-12-07 17:04:04 +0000378 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000379}
380
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000381const StringRef root_name(StringRef path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000382 const_iterator b = begin(path),
383 e = end(path);
384 if (b != e) {
385 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
386 bool has_drive =
387#ifdef LLVM_ON_WIN32
388 b->endswith(":");
389#else
390 false;
391#endif
392
393 if (has_net || has_drive) {
394 // just {C:,//net}, return the first component.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000395 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000396 }
397 }
398
399 // No path or no name.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000400 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000401}
402
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000403const StringRef root_directory(StringRef path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000404 const_iterator b = begin(path),
405 pos = b,
406 e = end(path);
407 if (b != e) {
408 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
409 bool has_drive =
410#ifdef LLVM_ON_WIN32
411 b->endswith(":");
412#else
413 false;
414#endif
415
416 if ((has_net || has_drive) &&
417 // {C:,//net}, skip to the next component.
418 (++pos != e) && is_separator((*pos)[0])) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000419 return *pos;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000420 }
421
422 // POSIX style root directory.
423 if (!has_net && is_separator((*b)[0])) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000424 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000425 }
426 }
427
428 // No path or no root.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000429 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000430}
431
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000432const StringRef relative_path(StringRef path) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000433 StringRef root = root_path(path);
Michael J. Spencere6462392012-02-29 00:06:24 +0000434 return path.substr(root.size());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000435}
436
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000437void append(SmallVectorImpl<char> &path, const Twine &a,
438 const Twine &b,
439 const Twine &c,
440 const Twine &d) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000441 SmallString<32> a_storage;
442 SmallString<32> b_storage;
443 SmallString<32> c_storage;
444 SmallString<32> d_storage;
445
446 SmallVector<StringRef, 4> components;
447 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
448 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
449 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
450 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
451
452 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
453 e = components.end();
454 i != e; ++i) {
455 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
456 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
Michael J. Spencerf616b212010-12-07 17:04:04 +0000457 bool is_root_name = has_root_name(*i);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000458
459 if (path_has_sep) {
460 // Strip separators from beginning of component.
461 size_t loc = i->find_first_not_of(separators);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000462 StringRef c = i->substr(loc);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000463
464 // Append it.
465 path.append(c.begin(), c.end());
466 continue;
467 }
468
Michael J. Spencer95e4ac12010-12-06 04:28:23 +0000469 if (!component_has_sep && !(path.empty() || is_root_name)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000470 // Add a separator.
Alp Tokercb402912014-01-24 17:20:08 +0000471 path.push_back(preferred_separator);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000472 }
473
474 path.append(i->begin(), i->end());
475 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000476}
477
Argyrios Kyrtzidisa61736f2011-02-15 17:51:19 +0000478void append(SmallVectorImpl<char> &path,
479 const_iterator begin, const_iterator end) {
480 for (; begin != end; ++begin)
481 path::append(path, *begin);
482}
483
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000484const StringRef parent_path(StringRef path) {
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000485 size_t end_pos = parent_path_end(path);
486 if (end_pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000487 return StringRef();
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000488 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000489 return path.substr(0, end_pos);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000490}
491
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000492void remove_filename(SmallVectorImpl<char> &path) {
Michael J. Spencer9c594092010-12-01 00:52:28 +0000493 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000494 if (end_pos != StringRef::npos)
495 path.set_size(end_pos);
Michael J. Spencer9c594092010-12-01 00:52:28 +0000496}
497
Michael J. Spencerf616b212010-12-07 17:04:04 +0000498void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
Michael J. Spencerfb3a95d2010-12-01 00:52:55 +0000499 StringRef p(path.begin(), path.size());
500 SmallString<32> ext_storage;
501 StringRef ext = extension.toStringRef(ext_storage);
502
503 // Erase existing extension.
504 size_t pos = p.find_last_of('.');
505 if (pos != StringRef::npos && pos >= filename_pos(p))
506 path.set_size(pos);
507
508 // Append '.' if needed.
509 if (ext.size() > 0 && ext[0] != '.')
510 path.push_back('.');
511
512 // Append extension.
513 path.append(ext.begin(), ext.end());
Michael J. Spencerfb3a95d2010-12-01 00:52:55 +0000514}
515
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000516void native(const Twine &path, SmallVectorImpl<char> &result) {
Benjamin Kramerbd4ac9b2013-09-11 10:45:21 +0000517 assert((!path.isSingleStringRef() ||
518 path.getSingleStringRef().data() != result.data()) &&
519 "path and result are not allowed to overlap!");
Michael J. Spencer80025002010-12-01 02:48:27 +0000520 // Clear result.
Michael J. Spencer98c7a112010-12-07 01:23:19 +0000521 result.clear();
Michael J. Spencer80025002010-12-01 02:48:27 +0000522 path.toVector(result);
Benjamin Kramerbd4ac9b2013-09-11 10:45:21 +0000523 native(result);
524}
525
526void native(SmallVectorImpl<char> &path) {
527#ifdef LLVM_ON_WIN32
528 std::replace(path.begin(), path.end(), '/', '\\');
Michael J. Spencer80025002010-12-01 02:48:27 +0000529#endif
Michael J. Spencer80025002010-12-01 02:48:27 +0000530}
531
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000532const StringRef filename(StringRef path) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000533 return *(--end(path));
Michael J. Spencer14269202010-12-01 03:18:17 +0000534}
535
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000536const StringRef stem(StringRef path) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000537 StringRef fname = filename(path);
Michael J. Spencer956955e2010-12-01 03:18:33 +0000538 size_t pos = fname.find_last_of('.');
539 if (pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000540 return fname;
Michael J. Spencer956955e2010-12-01 03:18:33 +0000541 else
542 if ((fname.size() == 1 && fname == ".") ||
543 (fname.size() == 2 && fname == ".."))
Michael J. Spencerf616b212010-12-07 17:04:04 +0000544 return fname;
Michael J. Spencer956955e2010-12-01 03:18:33 +0000545 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000546 return fname.substr(0, pos);
Michael J. Spencer956955e2010-12-01 03:18:33 +0000547}
548
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000549const StringRef extension(StringRef path) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000550 StringRef fname = filename(path);
Michael J. Spencer87106c52010-12-01 03:37:41 +0000551 size_t pos = fname.find_last_of('.');
552 if (pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000553 return StringRef();
Michael J. Spencer87106c52010-12-01 03:37:41 +0000554 else
555 if ((fname.size() == 1 && fname == ".") ||
556 (fname.size() == 2 && fname == ".."))
Michael J. Spencerf616b212010-12-07 17:04:04 +0000557 return StringRef();
Michael J. Spencer87106c52010-12-01 03:37:41 +0000558 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000559 return fname.substr(pos);
Michael J. Spencer87106c52010-12-01 03:37:41 +0000560}
561
Zhanyong Wan606bb1a2011-02-11 21:24:40 +0000562bool is_separator(char value) {
563 switch(value) {
564#ifdef LLVM_ON_WIN32
565 case '\\': // fall through
566#endif
567 case '/': return true;
568 default: return false;
569 }
570}
571
Douglas Gregor123dc702011-09-14 20:27:01 +0000572void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result) {
573 result.clear();
Michael J. Spencer7dfbeda2011-12-13 23:16:15 +0000574
David Fang6860c812014-02-12 21:02:12 +0000575#if defined(_CS_DARWIN_USER_TEMP_DIR) && defined(_CS_DARWIN_USER_CACHE_DIR)
Douglas Gregora86ddf02013-03-21 21:46:10 +0000576 // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR.
David Fang6860c812014-02-12 21:02:12 +0000577 // macros defined in <unistd.h> on darwin >= 9
Douglas Gregora86ddf02013-03-21 21:46:10 +0000578 int ConfName = erasedOnReboot? _CS_DARWIN_USER_TEMP_DIR
579 : _CS_DARWIN_USER_CACHE_DIR;
580 size_t ConfLen = confstr(ConfName, 0, 0);
581 if (ConfLen > 0) {
582 do {
583 result.resize(ConfLen);
584 ConfLen = confstr(ConfName, result.data(), result.size());
585 } while (ConfLen > 0 && ConfLen != result.size());
586
587 if (ConfLen > 0) {
588 assert(result.back() == 0);
589 result.pop_back();
590 return;
591 }
592
593 result.clear();
594 }
595#endif
596
Douglas Gregor123dc702011-09-14 20:27:01 +0000597 // Check whether the temporary directory is specified by an environment
598 // variable.
599 const char *EnvironmentVariable;
600#ifdef LLVM_ON_WIN32
601 EnvironmentVariable = "TEMP";
602#else
603 EnvironmentVariable = "TMPDIR";
604#endif
605 if (char *RequestedDir = getenv(EnvironmentVariable)) {
606 result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
607 return;
608 }
Michael J. Spencer7dfbeda2011-12-13 23:16:15 +0000609
Douglas Gregor123dc702011-09-14 20:27:01 +0000610 // Fall back to a system default.
611 const char *DefaultResult;
612#ifdef LLVM_ON_WIN32
613 (void)erasedOnReboot;
Douglas Gregor8b744542011-09-14 23:21:47 +0000614 DefaultResult = "C:\\TEMP";
Douglas Gregor123dc702011-09-14 20:27:01 +0000615#else
616 if (erasedOnReboot)
617 DefaultResult = "/tmp";
618 else
619 DefaultResult = "/var/tmp";
620#endif
621 result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
622}
Michael J. Spencer7dfbeda2011-12-13 23:16:15 +0000623
Michael J. Spencera68282c2010-12-07 18:12:07 +0000624bool has_root_name(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000625 SmallString<128> path_storage;
626 StringRef p = path.toStringRef(path_storage);
627
Michael J. Spencerf616b212010-12-07 17:04:04 +0000628 return !root_name(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000629}
630
Michael J. Spencera68282c2010-12-07 18:12:07 +0000631bool has_root_directory(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000632 SmallString<128> path_storage;
633 StringRef p = path.toStringRef(path_storage);
634
Michael J. Spencerf616b212010-12-07 17:04:04 +0000635 return !root_directory(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000636}
637
Michael J. Spencera68282c2010-12-07 18:12:07 +0000638bool has_root_path(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000639 SmallString<128> path_storage;
640 StringRef p = path.toStringRef(path_storage);
641
Michael J. Spencerf616b212010-12-07 17:04:04 +0000642 return !root_path(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000643}
644
Michael J. Spencer6d4b7e72010-12-20 13:30:28 +0000645bool has_relative_path(const Twine &path) {
646 SmallString<128> path_storage;
647 StringRef p = path.toStringRef(path_storage);
648
649 return !relative_path(p).empty();
650}
651
Michael J. Spencera68282c2010-12-07 18:12:07 +0000652bool has_filename(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000653 SmallString<128> path_storage;
654 StringRef p = path.toStringRef(path_storage);
655
Michael J. Spencerf616b212010-12-07 17:04:04 +0000656 return !filename(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000657}
658
Michael J. Spencera68282c2010-12-07 18:12:07 +0000659bool has_parent_path(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000660 SmallString<128> path_storage;
661 StringRef p = path.toStringRef(path_storage);
662
Michael J. Spencerf616b212010-12-07 17:04:04 +0000663 return !parent_path(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000664}
665
Michael J. Spencera68282c2010-12-07 18:12:07 +0000666bool has_stem(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000667 SmallString<128> path_storage;
668 StringRef p = path.toStringRef(path_storage);
669
Michael J. Spencerf616b212010-12-07 17:04:04 +0000670 return !stem(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000671}
672
Michael J. Spencera68282c2010-12-07 18:12:07 +0000673bool has_extension(const Twine &path) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000674 SmallString<128> path_storage;
675 StringRef p = path.toStringRef(path_storage);
676
Michael J. Spencerf616b212010-12-07 17:04:04 +0000677 return !extension(p).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000678}
679
Michael J. Spencera68282c2010-12-07 18:12:07 +0000680bool is_absolute(const Twine &path) {
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000681 SmallString<128> path_storage;
682 StringRef p = path.toStringRef(path_storage);
683
Michael J. Spencerf616b212010-12-07 17:04:04 +0000684 bool rootDir = has_root_directory(p),
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000685#ifdef LLVM_ON_WIN32
Michael J. Spencerf616b212010-12-07 17:04:04 +0000686 rootName = has_root_name(p);
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000687#else
Michael J. Spencerf616b212010-12-07 17:04:04 +0000688 rootName = true;
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000689#endif
690
Michael J. Spencerf616b212010-12-07 17:04:04 +0000691 return rootDir && rootName;
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000692}
693
Michael J. Spencera68282c2010-12-07 18:12:07 +0000694bool is_relative(const Twine &path) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000695 return !is_absolute(path);
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000696}
697
Michael J. Spencer9fc1d9d2010-12-01 19:32:01 +0000698} // end namespace path
699
700namespace fs {
701
Rafael Espindola7f822a92013-07-29 21:26:49 +0000702error_code getUniqueID(const Twine Path, UniqueID &Result) {
703 file_status Status;
704 error_code EC = status(Path, Status);
705 if (EC)
706 return EC;
707 Result = Status.getUniqueID();
708 return error_code::success();
709}
710
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000711error_code createUniqueFile(const Twine &Model, int &ResultFd,
712 SmallVectorImpl<char> &ResultPath, unsigned Mode) {
713 return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
714}
715
716error_code createUniqueFile(const Twine &Model,
717 SmallVectorImpl<char> &ResultPath) {
718 int Dummy;
719 return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
720}
721
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000722static error_code createTemporaryFile(const Twine &Model, int &ResultFD,
723 llvm::SmallVectorImpl<char> &ResultPath,
724 FSEntity Type) {
725 SmallString<128> Storage;
726 StringRef P = Model.toNullTerminatedStringRef(Storage);
727 assert(P.find_first_of(separators) == StringRef::npos &&
728 "Model must be a simple filename.");
729 // Use P.begin() so that createUniqueEntity doesn't need to recreate Storage.
730 return createUniqueEntity(P.begin(), ResultFD, ResultPath,
731 true, owner_read | owner_write, Type);
732}
733
734static error_code
735createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
736 llvm::SmallVectorImpl<char> &ResultPath,
737 FSEntity Type) {
Rafael Espindolad3c89042013-07-25 15:00:17 +0000738 const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
739 return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000740 Type);
741}
742
743
744error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
745 int &ResultFD,
746 SmallVectorImpl<char> &ResultPath) {
747 return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File);
748}
749
750error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
751 SmallVectorImpl<char> &ResultPath) {
752 int Dummy;
753 return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
754}
755
756
Rafael Espindolae79a8722013-06-28 03:48:47 +0000757// This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
Rafael Espindola31a24432013-06-28 10:55:41 +0000758// for consistency. We should try using mkdtemp.
Rafael Espindola7ffacc42013-06-27 03:45:31 +0000759error_code createUniqueDirectory(const Twine &Prefix,
760 SmallVectorImpl<char> &ResultPath) {
Rafael Espindolae79a8722013-06-28 03:48:47 +0000761 int Dummy;
762 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath,
763 true, 0, FS_Dir);
Rafael Espindola7ffacc42013-06-27 03:45:31 +0000764}
765
Michael J. Spencer92903a32010-12-07 03:57:17 +0000766error_code make_absolute(SmallVectorImpl<char> &path) {
767 StringRef p(path.data(), path.size());
768
Daniel Dunbarb23158c2012-02-29 00:20:37 +0000769 bool rootDirectory = path::has_root_directory(p),
770#ifdef LLVM_ON_WIN32
Daniel Dunbar39ea4582012-02-29 00:46:46 +0000771 rootName = path::has_root_name(p);
Daniel Dunbarb23158c2012-02-29 00:20:37 +0000772#else
773 rootName = true;
774#endif
Michael J. Spencer92903a32010-12-07 03:57:17 +0000775
776 // Already absolute.
777 if (rootName && rootDirectory)
David Blaikie18544b92012-02-09 19:24:12 +0000778 return error_code::success();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000779
780 // All of the following conditions will need the current directory.
781 SmallString<128> current_dir;
782 if (error_code ec = current_path(current_dir)) return ec;
783
784 // Relative path. Prepend the current directory.
785 if (!rootName && !rootDirectory) {
786 // Append path to the current directory.
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000787 path::append(current_dir, p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000788 // Set path to the result.
789 path.swap(current_dir);
David Blaikie18544b92012-02-09 19:24:12 +0000790 return error_code::success();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000791 }
792
793 if (!rootName && rootDirectory) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000794 StringRef cdrn = path::root_name(current_dir);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000795 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000796 path::append(curDirRootName, p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000797 // Set path to the result.
798 path.swap(curDirRootName);
David Blaikie18544b92012-02-09 19:24:12 +0000799 return error_code::success();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000800 }
801
802 if (rootName && !rootDirectory) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000803 StringRef pRootName = path::root_name(p);
804 StringRef bRootDirectory = path::root_directory(current_dir);
805 StringRef bRelativePath = path::relative_path(current_dir);
806 StringRef pRelativePath = path::relative_path(p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000807
808 SmallString<128> res;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000809 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000810 path.swap(res);
David Blaikie18544b92012-02-09 19:24:12 +0000811 return error_code::success();
Michael J. Spencer92903a32010-12-07 03:57:17 +0000812 }
813
814 llvm_unreachable("All rootName and rootDirectory combinations should have "
815 "occurred above!");
816}
817
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000818error_code create_directories(const Twine &Path, bool IgnoreExisting) {
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000819 SmallString<128> PathStorage;
820 StringRef P = Path.toStringRef(PathStorage);
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000821
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000822 // Be optimistic and try to create the directory
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000823 error_code EC = create_directory(P, IgnoreExisting);
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000824 // If we succeeded, or had any error other than the parent not existing, just
825 // return it.
826 if (EC != errc::no_such_file_or_directory)
827 return EC;
Michael J. Spencerf616b212010-12-07 17:04:04 +0000828
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000829 // We failed because of a no_such_file_or_directory, try to create the
830 // parent.
831 StringRef Parent = path::parent_path(P);
832 if (Parent.empty())
833 return EC;
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000834
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000835 if ((EC = create_directories(Parent)))
836 return EC;
837
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000838 return create_directory(P, IgnoreExisting);
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000839}
840
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000841bool exists(file_status status) {
842 return status_known(status) && status.type() != file_type::file_not_found;
843}
844
845bool status_known(file_status s) {
846 return s.type() != file_type::status_error;
847}
848
849bool is_directory(file_status status) {
850 return status.type() == file_type::directory_file;
851}
852
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000853error_code is_directory(const Twine &path, bool &result) {
854 file_status st;
855 if (error_code ec = status(path, st))
856 return ec;
857 result = is_directory(st);
David Blaikie18544b92012-02-09 19:24:12 +0000858 return error_code::success();
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000859}
860
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000861bool is_regular_file(file_status status) {
862 return status.type() == file_type::regular_file;
863}
864
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000865error_code is_regular_file(const Twine &path, bool &result) {
866 file_status st;
867 if (error_code ec = status(path, st))
868 return ec;
869 result = is_regular_file(st);
David Blaikie18544b92012-02-09 19:24:12 +0000870 return error_code::success();
Michael J. Spencer0d771ed2011-01-11 01:21:55 +0000871}
872
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000873bool is_symlink(file_status status) {
874 return status.type() == file_type::symlink_file;
875}
876
Michael J. Spencerd9960c62011-01-12 23:55:06 +0000877error_code is_symlink(const Twine &path, bool &result) {
878 file_status st;
879 if (error_code ec = status(path, st))
880 return ec;
881 result = is_symlink(st);
David Blaikie18544b92012-02-09 19:24:12 +0000882 return error_code::success();
Michael J. Spencerd9960c62011-01-12 23:55:06 +0000883}
884
Michael J. Spencer730f51a2010-12-09 17:37:02 +0000885bool is_other(file_status status) {
886 return exists(status) &&
887 !is_regular_file(status) &&
888 !is_directory(status) &&
889 !is_symlink(status);
890}
891
Benjamin Kramer91ead3c2011-09-14 01:14:36 +0000892void directory_entry::replace_filename(const Twine &filename, file_status st) {
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000893 SmallString<128> path(Path.begin(), Path.end());
894 path::remove_filename(path);
895 path::append(path, filename);
896 Path = path.str();
897 Status = st;
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000898}
899
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000900error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000901 SmallString<32> MagicStorage;
Michael J. Spencer78874662011-01-15 18:52:41 +0000902 StringRef Magic = magic.toStringRef(MagicStorage);
903 SmallString<32> Buffer;
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000904
Michael J. Spencer78874662011-01-15 18:52:41 +0000905 if (error_code ec = get_magic(path, Magic.size(), Buffer)) {
906 if (ec == errc::value_too_large) {
907 // Magic.size() > file_size(Path).
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000908 result = false;
David Blaikie18544b92012-02-09 19:24:12 +0000909 return error_code::success();
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000910 }
Michael J. Spencer78874662011-01-15 18:52:41 +0000911 return ec;
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000912 }
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000913
Michael J. Spencer78874662011-01-15 18:52:41 +0000914 result = Magic == Buffer;
David Blaikie18544b92012-02-09 19:24:12 +0000915 return error_code::success();
Michael J. Spencer848f46b2010-12-28 01:49:01 +0000916}
917
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000918/// @brief Identify the magic in magic.
Rafael Espindola9b404292013-06-11 17:22:12 +0000919 file_magic identify_magic(StringRef Magic) {
920 if (Magic.size() < 4)
Michael J. Spencer96ebd912012-06-19 05:29:57 +0000921 return file_magic::unknown;
Rafael Espindola9b404292013-06-11 17:22:12 +0000922 switch ((unsigned char)Magic[0]) {
Rui Ueyamafc149a62013-10-15 22:45:38 +0000923 case 0x00: {
Rui Ueyamae448f9e2013-11-15 21:22:02 +0000924 // COFF short import library file
925 if (Magic[1] == (char)0x00 && Magic[2] == (char)0xff &&
926 Magic[3] == (char)0xff)
927 return file_magic::coff_import_library;
Rui Ueyamafc149a62013-10-15 22:45:38 +0000928 // Windows resource file
Rui Ueyama50a86e12013-10-16 03:29:49 +0000929 const char Expected[] = { 0, 0, 0, 0, '\x20', 0, 0, 0, '\xff' };
Rui Ueyamafc149a62013-10-15 22:45:38 +0000930 if (Magic.size() >= sizeof(Expected) &&
931 memcmp(Magic.data(), Expected, sizeof(Expected)) == 0)
932 return file_magic::windows_resource;
Rui Ueyama829c4392013-11-14 22:09:08 +0000933 // 0x0000 = COFF unknown machine type
934 if (Magic[1] == 0)
935 return file_magic::coff_object;
Rui Ueyamafc149a62013-10-15 22:45:38 +0000936 break;
937 }
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000938 case 0xDE: // 0x0B17C0DE = BC wraper
Rafael Espindola9b404292013-06-11 17:22:12 +0000939 if (Magic[1] == (char)0xC0 && Magic[2] == (char)0x17 &&
940 Magic[3] == (char)0x0B)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000941 return file_magic::bitcode;
942 break;
943 case 'B':
Rafael Espindola9b404292013-06-11 17:22:12 +0000944 if (Magic[1] == 'C' && Magic[2] == (char)0xC0 && Magic[3] == (char)0xDE)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000945 return file_magic::bitcode;
946 break;
947 case '!':
Rafael Espindola9b404292013-06-11 17:22:12 +0000948 if (Magic.size() >= 8)
949 if (memcmp(Magic.data(),"!<arch>\n",8) == 0)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000950 return file_magic::archive;
951 break;
952
953 case '\177':
Rafael Espindola823de7c2013-06-11 17:25:45 +0000954 if (Magic.size() >= 18 && Magic[1] == 'E' && Magic[2] == 'L' &&
955 Magic[3] == 'F') {
Rafael Espindola9b404292013-06-11 17:22:12 +0000956 bool Data2MSB = Magic[5] == 2;
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000957 unsigned high = Data2MSB ? 16 : 17;
958 unsigned low = Data2MSB ? 17 : 16;
Rafael Espindola823de7c2013-06-11 17:25:45 +0000959 if (Magic[high] == 0)
Rafael Espindola9b404292013-06-11 17:22:12 +0000960 switch (Magic[low]) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000961 default: break;
962 case 1: return file_magic::elf_relocatable;
963 case 2: return file_magic::elf_executable;
964 case 3: return file_magic::elf_shared_object;
965 case 4: return file_magic::elf_core;
966 }
967 }
968 break;
969
970 case 0xCA:
Rafael Espindola9b404292013-06-11 17:22:12 +0000971 if (Magic[1] == char(0xFE) && Magic[2] == char(0xBA) &&
972 Magic[3] == char(0xBE)) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000973 // This is complicated by an overlap with Java class files.
974 // See the Mach-O section in /usr/share/file/magic for details.
Rafael Espindola9b404292013-06-11 17:22:12 +0000975 if (Magic.size() >= 8 && Magic[7] < 43)
Alexey Samsonove6388e62013-06-18 15:03:28 +0000976 return file_magic::macho_universal_binary;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000977 }
978 break;
979
980 // The two magic numbers for mach-o are:
981 // 0xfeedface - 32-bit mach-o
982 // 0xfeedfacf - 64-bit mach-o
983 case 0xFE:
984 case 0xCE:
985 case 0xCF: {
986 uint16_t type = 0;
Rafael Espindola9b404292013-06-11 17:22:12 +0000987 if (Magic[0] == char(0xFE) && Magic[1] == char(0xED) &&
988 Magic[2] == char(0xFA) &&
989 (Magic[3] == char(0xCE) || Magic[3] == char(0xCF))) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000990 /* Native endian */
Rafael Espindola9b404292013-06-11 17:22:12 +0000991 if (Magic.size() >= 16) type = Magic[14] << 8 | Magic[15];
992 } else if ((Magic[0] == char(0xCE) || Magic[0] == char(0xCF)) &&
993 Magic[1] == char(0xFA) && Magic[2] == char(0xED) &&
994 Magic[3] == char(0xFE)) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000995 /* Reverse endian */
Rafael Espindola9b404292013-06-11 17:22:12 +0000996 if (Magic.size() >= 14) type = Magic[13] << 8 | Magic[12];
Michael J. Spencer4f8a8322011-12-13 23:17:12 +0000997 }
998 switch (type) {
999 default: break;
1000 case 1: return file_magic::macho_object;
1001 case 2: return file_magic::macho_executable;
1002 case 3: return file_magic::macho_fixed_virtual_memory_shared_lib;
1003 case 4: return file_magic::macho_core;
Rafael Espindola134cc992013-06-10 20:32:27 +00001004 case 5: return file_magic::macho_preload_executable;
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001005 case 6: return file_magic::macho_dynamically_linked_shared_lib;
1006 case 7: return file_magic::macho_dynamic_linker;
1007 case 8: return file_magic::macho_bundle;
1008 case 9: return file_magic::macho_dynamic_linker;
1009 case 10: return file_magic::macho_dsym_companion;
1010 }
1011 break;
1012 }
1013 case 0xF0: // PowerPC Windows
1014 case 0x83: // Alpha 32-bit
1015 case 0x84: // Alpha 64-bit
1016 case 0x66: // MPS R4000 Windows
1017 case 0x50: // mc68K
1018 case 0x4c: // 80386 Windows
Rafael Espindola9b404292013-06-11 17:22:12 +00001019 if (Magic[1] == 0x01)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001020 return file_magic::coff_object;
1021
1022 case 0x90: // PA-RISC Windows
1023 case 0x68: // mc68K Windows
Rafael Espindola9b404292013-06-11 17:22:12 +00001024 if (Magic[1] == 0x02)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001025 return file_magic::coff_object;
1026 break;
1027
1028 case 0x4d: // Possible MS-DOS stub on Windows PE file
Rafael Espindola9b404292013-06-11 17:22:12 +00001029 if (Magic[1] == 0x5a) {
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001030 uint32_t off =
Rafael Espindola9b404292013-06-11 17:22:12 +00001031 *reinterpret_cast<const support::ulittle32_t*>(Magic.data() + 0x3c);
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001032 // PE/COFF file, either EXE or DLL.
Rafael Espindola9b404292013-06-11 17:22:12 +00001033 if (off < Magic.size() && memcmp(Magic.data() + off, "PE\0\0",4) == 0)
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001034 return file_magic::pecoff_executable;
1035 }
1036 break;
1037
1038 case 0x64: // x86-64 Windows.
Rafael Espindola9b404292013-06-11 17:22:12 +00001039 if (Magic[1] == char(0x86))
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001040 return file_magic::coff_object;
1041 break;
1042
1043 default:
1044 break;
1045 }
1046 return file_magic::unknown;
1047}
1048
1049error_code identify_magic(const Twine &path, file_magic &result) {
Michael J. Spencer94b2ab32011-01-15 20:39:36 +00001050 SmallString<32> Magic;
1051 error_code ec = get_magic(path, Magic.capacity(), Magic);
1052 if (ec && ec != errc::value_too_large)
1053 return ec;
1054
Michael J. Spencer4f8a8322011-12-13 23:17:12 +00001055 result = identify_magic(Magic);
David Blaikie18544b92012-02-09 19:24:12 +00001056 return error_code::success();
Michael J. Spencer94b2ab32011-01-15 20:39:36 +00001057}
1058
Michael J. Spencerf8dc1862011-01-05 16:39:13 +00001059error_code directory_entry::status(file_status &result) const {
1060 return fs::status(Path, result);
1061}
1062
Michael J. Spencer9fc1d9d2010-12-01 19:32:01 +00001063} // end namespace fs
1064} // end namespace sys
1065} // end namespace llvm
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001066
1067// Include the truly platform-specific parts.
1068#if defined(LLVM_ON_UNIX)
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001069#include "Unix/Path.inc"
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001070#endif
1071#if defined(LLVM_ON_WIN32)
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001072#include "Windows/Path.inc"
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001073#endif