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