]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/lib/Support/PathV2.cpp
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / llvm / lib / Support / PathV2.cpp
1 //===-- PathV2.cpp - Implement OS Path Concept ------------------*- C++ -*-===//
2 //
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 //
10 //  This file implements the operating system PathV2 API.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/PathV2.h"
15 #include "llvm/Support/Endian.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/FileSystem.h"
18 #include <cctype>
19 #include <cstdio>
20 #include <cstring>
21 #ifdef __APPLE__
22 #include <unistd.h>
23 #endif
24
25 namespace {
26   using llvm::StringRef;
27   using llvm::sys::path::is_separator;
28
29 #ifdef LLVM_ON_WIN32
30   const char *separators = "\\/";
31   const char  prefered_separator = '\\';
32 #else
33   const char  separators = '/';
34   const char  prefered_separator = '/';
35 #endif
36
37   StringRef find_first_component(StringRef path) {
38     // Look for this first component in the following order.
39     // * empty (in this case we return an empty string)
40     // * either C: or {//,\\}net.
41     // * {/,\}
42     // * {.,..}
43     // * {file,directory}name
44
45     if (path.empty())
46       return path;
47
48 #ifdef LLVM_ON_WIN32
49     // C:
50     if (path.size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) &&
51         path[1] == ':')
52       return path.substr(0, 2);
53 #endif
54
55     // //net
56     if ((path.size() > 2) &&
57         is_separator(path[0]) &&
58         path[0] == path[1] &&
59         !is_separator(path[2])) {
60       // Find the next directory separator.
61       size_t end = path.find_first_of(separators, 2);
62       return path.substr(0, end);
63     }
64
65     // {/,\}
66     if (is_separator(path[0]))
67       return path.substr(0, 1);
68
69     if (path.startswith(".."))
70       return path.substr(0, 2);
71
72     if (path[0] == '.')
73       return path.substr(0, 1);
74
75     // * {file,directory}name
76     size_t end = path.find_first_of(separators, 2);
77     return path.substr(0, end);
78   }
79
80   size_t filename_pos(StringRef str) {
81     if (str.size() == 2 &&
82         is_separator(str[0]) &&
83         str[0] == str[1])
84       return 0;
85
86     if (str.size() > 0 && is_separator(str[str.size() - 1]))
87       return str.size() - 1;
88
89     size_t pos = str.find_last_of(separators, str.size() - 1);
90
91 #ifdef LLVM_ON_WIN32
92     if (pos == StringRef::npos)
93       pos = str.find_last_of(':', str.size() - 2);
94 #endif
95
96     if (pos == StringRef::npos ||
97         (pos == 1 && is_separator(str[0])))
98       return 0;
99
100     return pos + 1;
101   }
102
103   size_t root_dir_start(StringRef str) {
104     // case "c:/"
105 #ifdef LLVM_ON_WIN32
106     if (str.size() > 2 &&
107         str[1] == ':' &&
108         is_separator(str[2]))
109       return 2;
110 #endif
111
112     // case "//"
113     if (str.size() == 2 &&
114         is_separator(str[0]) &&
115         str[0] == str[1])
116       return StringRef::npos;
117
118     // case "//net"
119     if (str.size() > 3 &&
120         is_separator(str[0]) &&
121         str[0] == str[1] &&
122         !is_separator(str[2])) {
123       return str.find_first_of(separators, 2);
124     }
125
126     // case "/"
127     if (str.size() > 0 && is_separator(str[0]))
128       return 0;
129
130     return StringRef::npos;
131   }
132
133   size_t parent_path_end(StringRef path) {
134     size_t end_pos = filename_pos(path);
135
136     bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
137
138     // Skip separators except for root dir.
139     size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
140
141     while(end_pos > 0 &&
142           (end_pos - 1) != root_dir_pos &&
143           is_separator(path[end_pos - 1]))
144       --end_pos;
145
146     if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
147       return StringRef::npos;
148
149     return end_pos;
150   }
151 } // end unnamed namespace
152
153 namespace llvm {
154 namespace sys  {
155 namespace path {
156
157 const_iterator begin(StringRef path) {
158   const_iterator i;
159   i.Path      = path;
160   i.Component = find_first_component(path);
161   i.Position  = 0;
162   return i;
163 }
164
165 const_iterator end(StringRef path) {
166   const_iterator i;
167   i.Path      = path;
168   i.Position  = path.size();
169   return i;
170 }
171
172 const_iterator &const_iterator::operator++() {
173   assert(Position < Path.size() && "Tried to increment past end!");
174
175   // Increment Position to past the current component
176   Position += Component.size();
177
178   // Check for end.
179   if (Position == Path.size()) {
180     Component = StringRef();
181     return *this;
182   }
183
184   // Both POSIX and Windows treat paths that begin with exactly two separators
185   // specially.
186   bool was_net = Component.size() > 2 &&
187     is_separator(Component[0]) &&
188     Component[1] == Component[0] &&
189     !is_separator(Component[2]);
190
191   // Handle separators.
192   if (is_separator(Path[Position])) {
193     // Root dir.
194     if (was_net
195 #ifdef LLVM_ON_WIN32
196         // c:/
197         || Component.endswith(":")
198 #endif
199         ) {
200       Component = Path.substr(Position, 1);
201       return *this;
202     }
203
204     // Skip extra separators.
205     while (Position != Path.size() &&
206            is_separator(Path[Position])) {
207       ++Position;
208     }
209
210     // Treat trailing '/' as a '.'.
211     if (Position == Path.size()) {
212       --Position;
213       Component = ".";
214       return *this;
215     }
216   }
217
218   // Find next component.
219   size_t end_pos = Path.find_first_of(separators, Position);
220   Component = Path.slice(Position, end_pos);
221
222   return *this;
223 }
224
225 const_iterator &const_iterator::operator--() {
226   // If we're at the end and the previous char was a '/', return '.'.
227   if (Position == Path.size() &&
228       Path.size() > 1 &&
229       is_separator(Path[Position - 1])
230 #ifdef LLVM_ON_WIN32
231       && Path[Position - 2] != ':'
232 #endif
233       ) {
234     --Position;
235     Component = ".";
236     return *this;
237   }
238
239   // Skip separators unless it's the root directory.
240   size_t root_dir_pos = root_dir_start(Path);
241   size_t end_pos = Position;
242
243   while(end_pos > 0 &&
244         (end_pos - 1) != root_dir_pos &&
245         is_separator(Path[end_pos - 1]))
246     --end_pos;
247
248   // Find next separator.
249   size_t start_pos = filename_pos(Path.substr(0, end_pos));
250   Component = Path.slice(start_pos, end_pos);
251   Position = start_pos;
252   return *this;
253 }
254
255 bool const_iterator::operator==(const const_iterator &RHS) const {
256   return Path.begin() == RHS.Path.begin() &&
257          Position == RHS.Position;
258 }
259
260 bool const_iterator::operator!=(const const_iterator &RHS) const {
261   return !(*this == RHS);
262 }
263
264 ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
265   return Position - RHS.Position;
266 }
267
268 const StringRef root_path(StringRef path) {
269   const_iterator b = begin(path),
270                  pos = b,
271                  e = end(path);
272   if (b != e) {
273     bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
274     bool has_drive =
275 #ifdef LLVM_ON_WIN32
276       b->endswith(":");
277 #else
278       false;
279 #endif
280
281     if (has_net || has_drive) {
282       if ((++pos != e) && is_separator((*pos)[0])) {
283         // {C:/,//net/}, so get the first two components.
284         return path.substr(0, b->size() + pos->size());
285       } else {
286         // just {C:,//net}, return the first component.
287         return *b;
288       }
289     }
290
291     // POSIX style root directory.
292     if (is_separator((*b)[0])) {
293       return *b;
294     }
295   }
296
297   return StringRef();
298 }
299
300 const StringRef root_name(StringRef path) {
301   const_iterator b = begin(path),
302                  e = end(path);
303   if (b != e) {
304     bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
305     bool has_drive =
306 #ifdef LLVM_ON_WIN32
307       b->endswith(":");
308 #else
309       false;
310 #endif
311
312     if (has_net || has_drive) {
313       // just {C:,//net}, return the first component.
314       return *b;
315     }
316   }
317
318   // No path or no name.
319   return StringRef();
320 }
321
322 const StringRef root_directory(StringRef path) {
323   const_iterator b = begin(path),
324                  pos = b,
325                  e = end(path);
326   if (b != e) {
327     bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
328     bool has_drive =
329 #ifdef LLVM_ON_WIN32
330       b->endswith(":");
331 #else
332       false;
333 #endif
334
335     if ((has_net || has_drive) &&
336         // {C:,//net}, skip to the next component.
337         (++pos != e) && is_separator((*pos)[0])) {
338       return *pos;
339     }
340
341     // POSIX style root directory.
342     if (!has_net && is_separator((*b)[0])) {
343       return *b;
344     }
345   }
346
347   // No path or no root.
348   return StringRef();
349 }
350
351 const StringRef relative_path(StringRef path) {
352   StringRef root = root_path(path);
353   return path.substr(root.size());
354 }
355
356 void append(SmallVectorImpl<char> &path, const Twine &a,
357                                          const Twine &b,
358                                          const Twine &c,
359                                          const Twine &d) {
360   SmallString<32> a_storage;
361   SmallString<32> b_storage;
362   SmallString<32> c_storage;
363   SmallString<32> d_storage;
364
365   SmallVector<StringRef, 4> components;
366   if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
367   if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
368   if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
369   if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
370
371   for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
372                                                   e = components.end();
373                                                   i != e; ++i) {
374     bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
375     bool component_has_sep = !i->empty() && is_separator((*i)[0]);
376     bool is_root_name = has_root_name(*i);
377
378     if (path_has_sep) {
379       // Strip separators from beginning of component.
380       size_t loc = i->find_first_not_of(separators);
381       StringRef c = i->substr(loc);
382
383       // Append it.
384       path.append(c.begin(), c.end());
385       continue;
386     }
387
388     if (!component_has_sep && !(path.empty() || is_root_name)) {
389       // Add a separator.
390       path.push_back(prefered_separator);
391     }
392
393     path.append(i->begin(), i->end());
394   }
395 }
396
397 void append(SmallVectorImpl<char> &path,
398             const_iterator begin, const_iterator end) {
399   for (; begin != end; ++begin)
400     path::append(path, *begin);
401 }
402
403 const StringRef parent_path(StringRef path) {
404   size_t end_pos = parent_path_end(path);
405   if (end_pos == StringRef::npos)
406     return StringRef();
407   else
408     return path.substr(0, end_pos);
409 }
410
411 void remove_filename(SmallVectorImpl<char> &path) {
412   size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
413   if (end_pos != StringRef::npos)
414     path.set_size(end_pos);
415 }
416
417 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
418   StringRef p(path.begin(), path.size());
419   SmallString<32> ext_storage;
420   StringRef ext = extension.toStringRef(ext_storage);
421
422   // Erase existing extension.
423   size_t pos = p.find_last_of('.');
424   if (pos != StringRef::npos && pos >= filename_pos(p))
425     path.set_size(pos);
426
427   // Append '.' if needed.
428   if (ext.size() > 0 && ext[0] != '.')
429     path.push_back('.');
430
431   // Append extension.
432   path.append(ext.begin(), ext.end());
433 }
434
435 void native(const Twine &path, SmallVectorImpl<char> &result) {
436   // Clear result.
437   result.clear();
438 #ifdef LLVM_ON_WIN32
439   SmallString<128> path_storage;
440   StringRef p = path.toStringRef(path_storage);
441   result.reserve(p.size());
442   for (StringRef::const_iterator i = p.begin(),
443                                  e = p.end();
444                                  i != e;
445                                  ++i) {
446     if (*i == '/')
447       result.push_back('\\');
448     else
449       result.push_back(*i);
450   }
451 #else
452   path.toVector(result);
453 #endif
454 }
455
456 const StringRef filename(StringRef path) {
457   return *(--end(path));
458 }
459
460 const StringRef stem(StringRef path) {
461   StringRef fname = filename(path);
462   size_t pos = fname.find_last_of('.');
463   if (pos == StringRef::npos)
464     return fname;
465   else
466     if ((fname.size() == 1 && fname == ".") ||
467         (fname.size() == 2 && fname == ".."))
468       return fname;
469     else
470       return fname.substr(0, pos);
471 }
472
473 const StringRef extension(StringRef path) {
474   StringRef fname = filename(path);
475   size_t pos = fname.find_last_of('.');
476   if (pos == StringRef::npos)
477     return StringRef();
478   else
479     if ((fname.size() == 1 && fname == ".") ||
480         (fname.size() == 2 && fname == ".."))
481       return StringRef();
482     else
483       return fname.substr(pos);
484 }
485
486 bool is_separator(char value) {
487   switch(value) {
488 #ifdef LLVM_ON_WIN32
489     case '\\': // fall through
490 #endif
491     case '/': return true;
492     default: return false;
493   }
494 }
495
496 void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result) {
497   result.clear();
498
499 #ifdef __APPLE__
500   // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR.
501   int ConfName = erasedOnReboot? _CS_DARWIN_USER_TEMP_DIR
502                                : _CS_DARWIN_USER_CACHE_DIR;
503   size_t ConfLen = confstr(ConfName, 0, 0);
504   if (ConfLen > 0) {
505     do {
506       result.resize(ConfLen);
507       ConfLen = confstr(ConfName, result.data(), result.size());
508     } while (ConfLen > 0 && ConfLen != result.size());
509
510     if (ConfLen > 0) {
511       assert(result.back() == 0);
512       result.pop_back();
513       return;
514     }
515
516     result.clear();
517   }
518 #endif
519
520   // Check whether the temporary directory is specified by an environment
521   // variable.
522   const char *EnvironmentVariable;
523 #ifdef LLVM_ON_WIN32
524   EnvironmentVariable = "TEMP";
525 #else
526   EnvironmentVariable = "TMPDIR";
527 #endif
528   if (char *RequestedDir = getenv(EnvironmentVariable)) {
529     result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
530     return;
531   }
532
533   // Fall back to a system default.
534   const char *DefaultResult;
535 #ifdef LLVM_ON_WIN32
536   (void)erasedOnReboot;
537   DefaultResult = "C:\\TEMP";
538 #else
539   if (erasedOnReboot)
540     DefaultResult = "/tmp";
541   else
542     DefaultResult = "/var/tmp";
543 #endif
544   result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
545 }
546
547 bool has_root_name(const Twine &path) {
548   SmallString<128> path_storage;
549   StringRef p = path.toStringRef(path_storage);
550
551   return !root_name(p).empty();
552 }
553
554 bool has_root_directory(const Twine &path) {
555   SmallString<128> path_storage;
556   StringRef p = path.toStringRef(path_storage);
557
558   return !root_directory(p).empty();
559 }
560
561 bool has_root_path(const Twine &path) {
562   SmallString<128> path_storage;
563   StringRef p = path.toStringRef(path_storage);
564
565   return !root_path(p).empty();
566 }
567
568 bool has_relative_path(const Twine &path) {
569   SmallString<128> path_storage;
570   StringRef p = path.toStringRef(path_storage);
571
572   return !relative_path(p).empty();
573 }
574
575 bool has_filename(const Twine &path) {
576   SmallString<128> path_storage;
577   StringRef p = path.toStringRef(path_storage);
578
579   return !filename(p).empty();
580 }
581
582 bool has_parent_path(const Twine &path) {
583   SmallString<128> path_storage;
584   StringRef p = path.toStringRef(path_storage);
585
586   return !parent_path(p).empty();
587 }
588
589 bool has_stem(const Twine &path) {
590   SmallString<128> path_storage;
591   StringRef p = path.toStringRef(path_storage);
592
593   return !stem(p).empty();
594 }
595
596 bool has_extension(const Twine &path) {
597   SmallString<128> path_storage;
598   StringRef p = path.toStringRef(path_storage);
599
600   return !extension(p).empty();
601 }
602
603 bool is_absolute(const Twine &path) {
604   SmallString<128> path_storage;
605   StringRef p = path.toStringRef(path_storage);
606
607   bool rootDir = has_root_directory(p),
608 #ifdef LLVM_ON_WIN32
609        rootName = has_root_name(p);
610 #else
611        rootName = true;
612 #endif
613
614   return rootDir && rootName;
615 }
616
617 bool is_relative(const Twine &path) {
618   return !is_absolute(path);
619 }
620
621 } // end namespace path
622
623 namespace fs {
624
625 error_code make_absolute(SmallVectorImpl<char> &path) {
626   StringRef p(path.data(), path.size());
627
628   bool rootDirectory = path::has_root_directory(p),
629 #ifdef LLVM_ON_WIN32
630        rootName = path::has_root_name(p);
631 #else
632        rootName = true;
633 #endif
634
635   // Already absolute.
636   if (rootName && rootDirectory)
637     return error_code::success();
638
639   // All of the following conditions will need the current directory.
640   SmallString<128> current_dir;
641   if (error_code ec = current_path(current_dir)) return ec;
642
643   // Relative path. Prepend the current directory.
644   if (!rootName && !rootDirectory) {
645     // Append path to the current directory.
646     path::append(current_dir, p);
647     // Set path to the result.
648     path.swap(current_dir);
649     return error_code::success();
650   }
651
652   if (!rootName && rootDirectory) {
653     StringRef cdrn = path::root_name(current_dir);
654     SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
655     path::append(curDirRootName, p);
656     // Set path to the result.
657     path.swap(curDirRootName);
658     return error_code::success();
659   }
660
661   if (rootName && !rootDirectory) {
662     StringRef pRootName      = path::root_name(p);
663     StringRef bRootDirectory = path::root_directory(current_dir);
664     StringRef bRelativePath  = path::relative_path(current_dir);
665     StringRef pRelativePath  = path::relative_path(p);
666
667     SmallString<128> res;
668     path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
669     path.swap(res);
670     return error_code::success();
671   }
672
673   llvm_unreachable("All rootName and rootDirectory combinations should have "
674                    "occurred above!");
675 }
676
677 error_code create_directories(const Twine &path, bool &existed) {
678   SmallString<128> path_storage;
679   StringRef p = path.toStringRef(path_storage);
680
681   StringRef parent = path::parent_path(p);
682   if (!parent.empty()) {
683     bool parent_exists;
684     if (error_code ec = fs::exists(parent, parent_exists)) return ec;
685
686     if (!parent_exists)
687       if (error_code ec = create_directories(parent, existed)) return ec;
688   }
689
690   return create_directory(p, existed);
691 }
692
693 bool exists(file_status status) {
694   return status_known(status) && status.type() != file_type::file_not_found;
695 }
696
697 bool status_known(file_status s) {
698   return s.type() != file_type::status_error;
699 }
700
701 bool is_directory(file_status status) {
702   return status.type() == file_type::directory_file;
703 }
704
705 error_code is_directory(const Twine &path, bool &result) {
706   file_status st;
707   if (error_code ec = status(path, st))
708     return ec;
709   result = is_directory(st);
710   return error_code::success();
711 }
712
713 bool is_regular_file(file_status status) {
714   return status.type() == file_type::regular_file;
715 }
716
717 error_code is_regular_file(const Twine &path, bool &result) {
718   file_status st;
719   if (error_code ec = status(path, st))
720     return ec;
721   result = is_regular_file(st);
722   return error_code::success();
723 }
724
725 bool is_symlink(file_status status) {
726   return status.type() == file_type::symlink_file;
727 }
728
729 error_code is_symlink(const Twine &path, bool &result) {
730   file_status st;
731   if (error_code ec = status(path, st))
732     return ec;
733   result = is_symlink(st);
734   return error_code::success();
735 }
736
737 bool is_other(file_status status) {
738   return exists(status) &&
739          !is_regular_file(status) &&
740          !is_directory(status) &&
741          !is_symlink(status);
742 }
743
744 void directory_entry::replace_filename(const Twine &filename, file_status st) {
745   SmallString<128> path(Path.begin(), Path.end());
746   path::remove_filename(path);
747   path::append(path, filename);
748   Path = path.str();
749   Status = st;
750 }
751
752 error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
753   SmallString<32>  MagicStorage;
754   StringRef Magic = magic.toStringRef(MagicStorage);
755   SmallString<32> Buffer;
756
757   if (error_code ec = get_magic(path, Magic.size(), Buffer)) {
758     if (ec == errc::value_too_large) {
759       // Magic.size() > file_size(Path).
760       result = false;
761       return error_code::success();
762     }
763     return ec;
764   }
765
766   result = Magic == Buffer;
767   return error_code::success();
768 }
769
770 /// @brief Identify the magic in magic.
771 file_magic identify_magic(StringRef magic) {
772   if (magic.size() < 4)
773     return file_magic::unknown;
774   switch ((unsigned char)magic[0]) {
775     case 0xDE:  // 0x0B17C0DE = BC wraper
776       if (magic[1] == (char)0xC0 && magic[2] == (char)0x17 &&
777           magic[3] == (char)0x0B)
778         return file_magic::bitcode;
779       break;
780     case 'B':
781       if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
782         return file_magic::bitcode;
783       break;
784     case '!':
785       if (magic.size() >= 8)
786         if (memcmp(magic.data(),"!<arch>\n",8) == 0)
787           return file_magic::archive;
788       break;
789
790     case '\177':
791       if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
792         bool Data2MSB = magic[5] == 2;
793         unsigned high = Data2MSB ? 16 : 17;
794         unsigned low  = Data2MSB ? 17 : 16;
795         if (magic.size() >= 18 && magic[high] == 0)
796           switch (magic[low]) {
797             default: break;
798             case 1: return file_magic::elf_relocatable;
799             case 2: return file_magic::elf_executable;
800             case 3: return file_magic::elf_shared_object;
801             case 4: return file_magic::elf_core;
802           }
803       }
804       break;
805
806     case 0xCA:
807       if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
808           magic[3] == char(0xBE)) {
809         // This is complicated by an overlap with Java class files.
810         // See the Mach-O section in /usr/share/file/magic for details.
811         if (magic.size() >= 8 && magic[7] < 43)
812           // FIXME: Universal Binary of any type.
813           return file_magic::macho_dynamically_linked_shared_lib;
814       }
815       break;
816
817       // The two magic numbers for mach-o are:
818       // 0xfeedface - 32-bit mach-o
819       // 0xfeedfacf - 64-bit mach-o
820     case 0xFE:
821     case 0xCE:
822     case 0xCF: {
823       uint16_t type = 0;
824       if (magic[0] == char(0xFE) && magic[1] == char(0xED) &&
825           magic[2] == char(0xFA) &&
826           (magic[3] == char(0xCE) || magic[3] == char(0xCF))) {
827         /* Native endian */
828         if (magic.size() >= 16) type = magic[14] << 8 | magic[15];
829       } else if ((magic[0] == char(0xCE) || magic[0] == char(0xCF)) &&
830                  magic[1] == char(0xFA) && magic[2] == char(0xED) &&
831                  magic[3] == char(0xFE)) {
832         /* Reverse endian */
833         if (magic.size() >= 14) type = magic[13] << 8 | magic[12];
834       }
835       switch (type) {
836         default: break;
837         case 1: return file_magic::macho_object;
838         case 2: return file_magic::macho_executable;
839         case 3: return file_magic::macho_fixed_virtual_memory_shared_lib;
840         case 4: return file_magic::macho_core;
841         case 5: return file_magic::macho_preload_executabl;
842         case 6: return file_magic::macho_dynamically_linked_shared_lib;
843         case 7: return file_magic::macho_dynamic_linker;
844         case 8: return file_magic::macho_bundle;
845         case 9: return file_magic::macho_dynamic_linker;
846         case 10: return file_magic::macho_dsym_companion;
847       }
848       break;
849     }
850     case 0xF0: // PowerPC Windows
851     case 0x83: // Alpha 32-bit
852     case 0x84: // Alpha 64-bit
853     case 0x66: // MPS R4000 Windows
854     case 0x50: // mc68K
855     case 0x4c: // 80386 Windows
856       if (magic[1] == 0x01)
857         return file_magic::coff_object;
858
859     case 0x90: // PA-RISC Windows
860     case 0x68: // mc68K Windows
861       if (magic[1] == 0x02)
862         return file_magic::coff_object;
863       break;
864
865     case 0x4d: // Possible MS-DOS stub on Windows PE file
866       if (magic[1] == 0x5a) {
867         uint32_t off =
868           *reinterpret_cast<const support::ulittle32_t*>(magic.data() + 0x3c);
869         // PE/COFF file, either EXE or DLL.
870         if (off < magic.size() && memcmp(magic.data() + off, "PE\0\0",4) == 0)
871           return file_magic::pecoff_executable;
872       }
873       break;
874
875     case 0x64: // x86-64 Windows.
876       if (magic[1] == char(0x86))
877         return file_magic::coff_object;
878       break;
879
880     default:
881       break;
882   }
883   return file_magic::unknown;
884 }
885
886 error_code identify_magic(const Twine &path, file_magic &result) {
887   SmallString<32> Magic;
888   error_code ec = get_magic(path, Magic.capacity(), Magic);
889   if (ec && ec != errc::value_too_large)
890     return ec;
891
892   result = identify_magic(Magic);
893   return error_code::success();
894 }
895
896 namespace {
897 error_code remove_all_r(StringRef path, file_type ft, uint32_t &count) {
898   if (ft == file_type::directory_file) {
899     // This code would be a lot better with exceptions ;/.
900     error_code ec;
901     directory_iterator i(path, ec);
902     if (ec) return ec;
903     for (directory_iterator e; i != e; i.increment(ec)) {
904       if (ec) return ec;
905       file_status st;
906       if (error_code ec = i->status(st)) return ec;
907       if (error_code ec = remove_all_r(i->path(), st.type(), count)) return ec;
908     }
909     bool obviously_this_exists;
910     if (error_code ec = remove(path, obviously_this_exists)) return ec;
911     assert(obviously_this_exists);
912     ++count; // Include the directory itself in the items removed.
913   } else {
914     bool obviously_this_exists;
915     if (error_code ec = remove(path, obviously_this_exists)) return ec;
916     assert(obviously_this_exists);
917     ++count;
918   }
919
920   return error_code::success();
921 }
922 } // end unnamed namespace
923
924 error_code remove_all(const Twine &path, uint32_t &num_removed) {
925   SmallString<128> path_storage;
926   StringRef p = path.toStringRef(path_storage);
927
928   file_status fs;
929   if (error_code ec = status(path, fs))
930     return ec;
931   num_removed = 0;
932   return remove_all_r(p, fs.type(), num_removed);
933 }
934
935 error_code directory_entry::status(file_status &result) const {
936   return fs::status(Path, result);
937 }
938
939 } // end namespace fs
940 } // end namespace sys
941 } // end namespace llvm
942
943 // Include the truly platform-specific parts.
944 #if defined(LLVM_ON_UNIX)
945 #include "Unix/PathV2.inc"
946 #endif
947 #if defined(LLVM_ON_WIN32)
948 #include "Windows/PathV2.inc"
949 #endif