23 #if !defined(_MSC_VER) && !defined(__MINGW32__)
34 const char *separators =
"\\/";
35 const char prefered_separator =
'\\';
37 const char separators =
'/';
38 const char prefered_separator =
'/';
41 StringRef find_first_component(StringRef path) {
54 if (path.size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) &&
56 return path.substr(0, 2);
60 if ((path.size() > 2) &&
65 size_t end = path.find_first_of(separators, 2);
66 return path.substr(0, end);
71 return path.substr(0, 1);
73 if (path.startswith(
".."))
74 return path.substr(0, 2);
77 return path.substr(0, 1);
80 size_t end = path.find_first_of(separators);
81 return path.substr(0, end);
84 size_t filename_pos(StringRef str) {
85 if (str.size() == 2 &&
91 return str.size() - 1;
93 size_t pos = str.find_last_of(separators, str.size() - 1);
96 if (pos == StringRef::npos)
97 pos = str.find_last_of(
':', str.size() - 2);
100 if (pos == StringRef::npos ||
107 size_t root_dir_start(StringRef str) {
110 if (str.size() > 2 &&
117 if (str.size() == 2 &&
120 return StringRef::npos;
123 if (str.size() > 3 &&
127 return str.find_first_of(separators, 2);
134 return StringRef::npos;
137 size_t parent_path_end(StringRef path) {
138 size_t end_pos = filename_pos(path);
140 bool filename_was_sep = path.size() > 0 &&
is_separator(path[end_pos]);
143 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
146 (end_pos - 1) != root_dir_pos &&
150 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
151 return StringRef::npos;
176 i.Component = find_first_component(path);
184 i.Position = path.
size();
189 assert(Position < Path.
size() &&
"Tried to increment past end!");
192 Position += Component.
size();
195 if (Position == Path.
size()) {
202 bool was_net = Component.
size() > 2 &&
204 Component[1] == Component[0] &&
216 Component = Path.
substr(Position, 1);
221 while (Position != Path.
size() &&
227 if (Position == Path.
size()) {
236 Component = Path.
slice(Position, end_pos);
243 if (Position == Path.
size() &&
247 && Path[Position - 2] !=
':'
256 size_t root_dir_pos = root_dir_start(Path);
257 size_t end_pos = Position;
260 (end_pos - 1) != root_dir_pos &&
265 size_t start_pos = filename_pos(Path.
substr(0, end_pos));
266 Component = Path.
slice(start_pos, end_pos);
267 Position = start_pos;
273 Position == RHS.Position;
277 return !(*
this == RHS);
281 return Position - RHS.Position;
289 bool has_net = b->
size() > 2 &&
is_separator((*b)[0]) && (*b)[1] == (*b)[0];
297 if (has_net || has_drive) {
300 return path.
substr(0, b->
size() + pos->size());
320 bool has_net = b->
size() > 2 &&
is_separator((*b)[0]) && (*b)[1] == (*b)[0];
328 if (has_net || has_drive) {
343 bool has_net = b->
size() > 2 &&
is_separator((*b)[0]) && (*b)[1] == (*b)[0];
351 if ((has_net || has_drive) &&
388 e = components.
end();
391 bool component_has_sep = !i->empty() &&
is_separator((*i)[0]);
396 size_t loc = i->find_first_not_of(separators);
404 if (!component_has_sep && !(path.
empty() || is_root_name)) {
409 path.
append(i->begin(), i->end());
420 size_t end_pos = parent_path_end(path);
424 return path.
substr(0, end_pos);
444 if (ext.
size() > 0 && ext[0] !=
'.')
454 "path and result are not allowed to overlap!");
463 std::replace(path.
begin(), path.
end(),
'/',
'\\');
468 return *(--
end(path));
477 if ((fname.
size() == 1 && fname ==
".") ||
478 (fname.
size() == 2 && fname ==
".."))
481 return fname.
substr(0, pos);
490 if ((fname.
size() == 1 && fname ==
".") ||
491 (fname.
size() == 2 && fname ==
".."))
502 case '/':
return true;
503 default:
return false;
512 int ConfName = erasedOnReboot? _CS_DARWIN_USER_TEMP_DIR
513 : _CS_DARWIN_USER_CACHE_DIR;
514 size_t ConfLen = confstr(ConfName, 0, 0);
518 ConfLen = confstr(ConfName, result.
data(), result.
size());
519 }
while (ConfLen > 0 && ConfLen != result.
size());
522 assert(result.
back() == 0);
533 const char *EnvironmentVariable;
535 EnvironmentVariable =
"TEMP";
537 EnvironmentVariable =
"TMPDIR";
539 if (
char *RequestedDir =
getenv(EnvironmentVariable)) {
540 result.
append(RequestedDir, RequestedDir +
strlen(RequestedDir));
545 const char *DefaultResult;
547 (void)erasedOnReboot;
548 DefaultResult =
"C:\\TEMP";
551 DefaultResult =
"/tmp";
553 DefaultResult =
"/var/tmp";
555 result.
append(DefaultResult, DefaultResult +
strlen(DefaultResult));
625 return rootDir && rootName;
662 "Model must be a simple filename.");
672 const char *Middle = Suffix.
empty() ?
"-%%%%%%" :
"-%%%%%%.";
711 if (rootName && rootDirectory)
719 if (!rootName && !rootDirectory) {
723 path.
swap(current_dir);
727 if (!rootName && rootDirectory) {
732 path.
swap(curDirRootName);
736 if (rootName && !rootDirectory) {
743 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
757 if (!parent.
empty()) {
841 result = Magic == Buffer;
847 if (Magic.
size() < 4)
849 switch ((
unsigned char)Magic[0]) {
852 if (Magic[1] == (
char)0x00 && Magic[2] == (char)0xff &&
853 Magic[3] == (
char)0xff)
856 const char Expected[] = { 0, 0, 0, 0,
'\x20', 0, 0, 0,
'\xff' };
857 if (Magic.
size() >=
sizeof(Expected) &&
858 memcmp(Magic.
data(), Expected,
sizeof(Expected)) == 0)
866 if (Magic[1] == (
char)0xC0 && Magic[2] == (char)0x17 &&
867 Magic[3] == (
char)0x0B)
871 if (Magic[1] ==
'C' && Magic[2] == (
char)0xC0 && Magic[3] == (char)0xDE)
875 if (Magic.
size() >= 8)
881 if (Magic.
size() >= 18 && Magic[1] ==
'E' && Magic[2] ==
'L' &&
883 bool Data2MSB = Magic[5] == 2;
884 unsigned high = Data2MSB ? 16 : 17;
885 unsigned low = Data2MSB ? 17 : 16;
886 if (Magic[high] == 0)
887 switch (Magic[low]) {
898 if (Magic[1] ==
char(0xFE) && Magic[2] == char(0xBA) &&
899 Magic[3] == char(0xBE)) {
902 if (Magic.
size() >= 8 && Magic[7] < 43)
914 if (Magic[0] ==
char(0xFE) && Magic[1] == char(0xED) &&
915 Magic[2] == char(0xFA) &&
916 (Magic[3] == char(0xCE) || Magic[3] == char(0xCF))) {
918 if (Magic.
size() >= 16) type = Magic[14] << 8 | Magic[15];
919 }
else if ((Magic[0] ==
char(0xCE) || Magic[0] ==
char(0xCF)) &&
920 Magic[1] == char(0xFA) && Magic[2] == char(0xED) &&
921 Magic[3] == char(0xFE)) {
923 if (Magic.
size() >= 14) type = Magic[13] << 8 | Magic[12];
946 if (Magic[1] == 0x01)
951 if (Magic[1] == 0x02)
956 if (Magic[1] == 0x5a) {
960 if (off < Magic.
size() &&
memcmp(Magic.
data() + off,
"PE\0\0",4) == 0)
966 if (Magic[1] ==
char(0x86))
991 directory_iterator i(path, ec);
993 for (directory_iterator e; i != e; i.increment(ec)) {
997 if (
error_code ec = remove_all_r(i->path(), st.type(), count))
return ec;
999 bool obviously_this_exists;
1000 if (
error_code ec =
remove(path, obviously_this_exists))
return ec;
1001 assert(obviously_this_exists);
1004 bool obviously_this_exists;
1005 if (error_code ec =
remove(path, obviously_this_exists))
return ec;
1006 assert(obviously_this_exists);
1022 return remove_all_r(p, fs.
type(), num_removed);
1034 #if defined(LLVM_ON_UNIX)
1037 #if defined(LLVM_ON_WIN32)
void toVector(SmallVectorImpl< char > &Out) const
void set_size(unsigned N)
COFF::RelocationTypeX86 Type
void push_back(const T &Elt)
const_iterator end(StringRef path)
Get end iterator over path.
bool isSingleStringRef() const
size_t size() const
size - Get the string size.
error_code status(file_status &result) const
Mach-O dynlinked shared lib.
bool is_relative(const Twine &path)
Is path relative?
const StringRef parent_path(StringRef path)
Get parent path.
bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
void replace_filename(const Twine &filename, file_status st=file_status())
void replace_extension(SmallVectorImpl< char > &path, const Twine &extension)
Replace the file extension of path with extension.
UniqueID getUniqueID() const
bool has_stem(const Twine &path)
Has stem?
const StringRef extension(StringRef path)
Get extension.
StringRef substr(size_t Start, size_t N=npos) const
error_code current_path(SmallVectorImpl< char > &result)
Get the current path.
const_iterator begin(StringRef path)
Get begin iterator over path.
ELF Relocatable object file.
bool has_root_directory(const Twine &path)
Has root directory?
bool status_known(file_status s)
Is status available?
bool operator==(const const_iterator &RHS) const
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
void native(const Twine &path, SmallVectorImpl< char > &result)
void remove_filename(SmallVectorImpl< char > &path)
Remove the last component from path unless it is the root dir.
#define llvm_unreachable(msg)
error_code createUniqueDirectory(const Twine &Prefix, SmallVectorImpl< char > &ResultPath)
bool is_absolute(const Twine &path)
Is path absolute?
bool operator!=(const const_iterator &RHS) const
ELF dynamically linked shared lib.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
bool is_separator(char value)
Check whether the given char is a path separator on the host OS.
const char * data() const
const StringRef stem(StringRef path)
Get stem.
bool has_relative_path(const Twine &path)
Has relative path?
Windows compiled resource file (.rc)
StringRef getSingleStringRef() const
error_code getUniqueID(const Twine Path, UniqueID &Result)
static llvm::error_code createUniqueEntity(const llvm::Twine &Model, int &ResultFD, llvm::SmallVectorImpl< char > &ResultPath, bool MakeAbsolute, unsigned Mode, FSEntity Type)
int memcmp(const void *s1, const void *s2, size_t n);
error_code status(const Twine &path, file_status &result)
Get file status as if by POSIX stat().
void swap(SmallVectorImpl &RHS)
bool has_filename(const Twine &path)
Has filename?
Mach-O Preloaded Executable.
bool is_other(file_status status)
Does this status represent something that exists but is not a directory, regular file, or symlink?
error_code make_absolute(SmallVectorImpl< char > &path)
Make path an absolute path.
error_code remove_all(const Twine &path, uint32_t &num_removed)
Recursively remove all files below path, then path. Files are removed as if by POSIX remove()...
const StringRef root_directory(StringRef path)
Get root directory.
const_iterator & operator++()
ptrdiff_t operator-(const const_iterator &RHS) const
Difference in bytes between this and RHS.
bool is_symlink(file_status status)
Does status represent a symlink?
void append(in_iter in_start, in_iter in_end)
const std::string & path() const
error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, SmallVectorImpl< char > &ResultPath)
Create a file in the system temporary directory.
bool is_directory(file_status status)
Does status represent a directory?
error_code createUniqueFile(const Twine &Model, int &ResultFD, SmallVectorImpl< char > &ResultPath, unsigned Mode=all_read|all_write)
Create a uniquely named file.
static const char *const Magic
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
const StringRef root_path(StringRef path)
Get root path.
size_t find_last_of(char C, size_t From=npos) const
const_iterator & operator--()
StringRef toStringRef(SmallVectorImpl< char > &Out) const
error_code create_directories(const Twine &path, bool &existed)
Create all the non-existent directories in path.
size_t strlen(const char *s);
error_code has_magic(const Twine &path, const Twine &magic, bool &result)
Are path's first bytes magic?
bool is_regular_file(file_status status)
Does status represent a regular file?
pointer data()
data - Return a pointer to the vector's buffer, even if empty().
error_code create_directory(const Twine &path, bool &existed)
Create the directory in path.
static error_code success()
Mach-O dSYM companion file.
The Mach-O dynamic linker.
bool has_root_path(const Twine &path)
Has root path?
bool has_root_name(const Twine &path)
Has root name?
const StringRef relative_path(StringRef path)
Get relative path.
size_t find_first_of(char C, size_t From=0) const
void system_temp_directory(bool erasedOnReboot, SmallVectorImpl< char > &result)
Get the typical temporary directory for the system, e.g., "/var/tmp" or "C:/TEMP".
StringRef toNullTerminatedStringRef(SmallVectorImpl< char > &Out) const
bool isTriviallyEmpty() const
bool has_extension(const Twine &path)
Has extension?
const StringRef filename(StringRef path)
Get filename.
StringRef slice(size_t Start, size_t End) const
bool exists(file_status status)
Does file exist?
const StringRef root_name(StringRef path)
Get root name.
char *getenv(const char *name);
error_code get_magic(const Twine &path, uint32_t len, SmallVectorImpl< char > &result)
Get path's first len bytes.
bool has_parent_path(const Twine &path)
Has parent path?
bool empty() const
empty - Check if the string is empty.