2013-06-26 19:33:03 +00:00
|
|
|
//===-- Path.cpp - Implement OS Path Concept ------------------------------===//
|
2010-11-29 22:28:51 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2013-06-26 19:33:03 +00:00
|
|
|
// This file implements the operating system Path API.
|
2010-11-29 22:28:51 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-06-11 22:21:28 +00:00
|
|
|
#include "llvm/Support/Path.h"
|
2011-12-13 23:17:12 +00:00
|
|
|
#include "llvm/Support/Endian.h"
|
2010-11-29 22:28:51 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2014-02-11 03:40:14 +00:00
|
|
|
#include "llvm/Support/Process.h"
|
2010-11-29 22:28:51 +00:00
|
|
|
#include <cctype>
|
2010-12-28 01:49:01 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2013-07-16 19:44:17 +00:00
|
|
|
#include <fcntl.h>
|
2013-06-18 17:01:00 +00:00
|
|
|
|
|
|
|
#if !defined(_MSC_VER) && !defined(__MINGW32__)
|
2013-03-21 21:46:10 +00:00
|
|
|
#include <unistd.h>
|
2013-06-18 17:01:00 +00:00
|
|
|
#else
|
|
|
|
#include <io.h>
|
2013-03-21 21:46:10 +00:00
|
|
|
#endif
|
2010-11-29 22:28:51 +00:00
|
|
|
|
2014-02-24 03:07:41 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2010-11-29 22:28:51 +00:00
|
|
|
namespace {
|
|
|
|
using llvm::StringRef;
|
2011-02-11 21:24:40 +00:00
|
|
|
using llvm::sys::path::is_separator;
|
2010-11-29 22:28:51 +00:00
|
|
|
|
|
|
|
#ifdef LLVM_ON_WIN32
|
2012-02-08 13:13:47 +00:00
|
|
|
const char *separators = "\\/";
|
2014-01-24 17:20:08 +00:00
|
|
|
const char preferred_separator = '\\';
|
2010-11-29 22:28:51 +00:00
|
|
|
#else
|
2012-02-08 13:13:47 +00:00
|
|
|
const char separators = '/';
|
2014-01-24 17:20:08 +00:00
|
|
|
const char preferred_separator = '/';
|
2010-11-29 22:28:51 +00:00
|
|
|
#endif
|
|
|
|
|
2010-12-17 18:59:09 +00:00
|
|
|
StringRef find_first_component(StringRef path) {
|
2010-11-29 22:28:51 +00:00
|
|
|
// 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;
|
|
|
|
|
2010-12-07 03:57:48 +00:00
|
|
|
#ifdef LLVM_ON_WIN32
|
2010-11-29 22:28:51 +00:00
|
|
|
// C:
|
2013-02-12 21:21:59 +00:00
|
|
|
if (path.size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) &&
|
|
|
|
path[1] == ':')
|
2010-12-17 20:27:37 +00:00
|
|
|
return path.substr(0, 2);
|
2010-12-07 03:57:48 +00:00
|
|
|
#endif
|
2010-11-29 22:28:51 +00:00
|
|
|
|
|
|
|
// //net
|
|
|
|
if ((path.size() > 2) &&
|
2010-12-07 03:57:48 +00:00
|
|
|
is_separator(path[0]) &&
|
|
|
|
path[0] == path[1] &&
|
|
|
|
!is_separator(path[2])) {
|
2010-11-29 22:28:51 +00:00
|
|
|
// Find the next directory separator.
|
2010-12-07 03:57:48 +00:00
|
|
|
size_t end = path.find_first_of(separators, 2);
|
2010-12-17 20:27:37 +00:00
|
|
|
return path.substr(0, end);
|
2010-11-29 22:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// {/,\}
|
2010-12-07 03:57:48 +00:00
|
|
|
if (is_separator(path[0]))
|
2010-12-17 20:27:37 +00:00
|
|
|
return path.substr(0, 1);
|
2010-11-29 22:28:51 +00:00
|
|
|
|
|
|
|
if (path.startswith(".."))
|
2010-12-17 20:27:37 +00:00
|
|
|
return path.substr(0, 2);
|
2010-11-29 22:28:51 +00:00
|
|
|
|
|
|
|
if (path[0] == '.')
|
2010-12-17 20:27:37 +00:00
|
|
|
return path.substr(0, 1);
|
2010-11-29 22:28:51 +00:00
|
|
|
|
|
|
|
// * {file,directory}name
|
2013-08-12 17:10:49 +00:00
|
|
|
size_t end = path.find_first_of(separators);
|
2010-12-17 20:27:37 +00:00
|
|
|
return path.substr(0, end);
|
2010-11-29 22:28:51 +00:00
|
|
|
}
|
2010-11-30 23:28:07 +00:00
|
|
|
|
2010-12-17 18:19:06 +00:00
|
|
|
size_t filename_pos(StringRef str) {
|
2010-11-30 23:28:07 +00:00
|
|
|
if (str.size() == 2 &&
|
|
|
|
is_separator(str[0]) &&
|
2010-12-07 03:57:48 +00:00
|
|
|
str[0] == str[1])
|
2010-11-30 23:28:07 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-12-17 18:19:06 +00:00
|
|
|
size_t root_dir_start(StringRef str) {
|
2010-11-30 23:28:07 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2010-12-17 18:19:06 +00:00
|
|
|
size_t parent_path_end(StringRef path) {
|
2010-11-30 23:28:07 +00:00
|
|
|
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.
|
2010-12-17 20:27:37 +00:00
|
|
|
size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
|
2010-11-30 23:28:07 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2011-02-11 21:24:40 +00:00
|
|
|
} // end unnamed namespace
|
2010-11-29 22:28:51 +00:00
|
|
|
|
2013-06-28 03:48:47 +00:00
|
|
|
enum FSEntity {
|
|
|
|
FS_Dir,
|
|
|
|
FS_File,
|
|
|
|
FS_Name
|
|
|
|
};
|
|
|
|
|
|
|
|
// Implemented in Unix/Path.inc and Windows/Path.inc.
|
2014-02-24 03:07:41 +00:00
|
|
|
static error_code TempDir(SmallVectorImpl<char> &result);
|
|
|
|
|
|
|
|
static error_code createUniqueEntity(const Twine &Model, int &ResultFD,
|
|
|
|
SmallVectorImpl<char> &ResultPath,
|
|
|
|
bool MakeAbsolute, unsigned Mode,
|
|
|
|
FSEntity Type) {
|
|
|
|
SmallString<128> ModelStorage;
|
|
|
|
Model.toVector(ModelStorage);
|
|
|
|
|
|
|
|
if (MakeAbsolute) {
|
|
|
|
// Make model absolute by prepending a temp directory if it's not already.
|
|
|
|
if (!sys::path::is_absolute(Twine(ModelStorage))) {
|
|
|
|
SmallString<128> TDir;
|
|
|
|
if (error_code EC = TempDir(TDir))
|
|
|
|
return EC;
|
|
|
|
sys::path::append(TDir, Twine(ModelStorage));
|
|
|
|
ModelStorage.swap(TDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// From here on, DO NOT modify model. It may be needed if the randomly chosen
|
|
|
|
// path already exists.
|
|
|
|
ResultPath = ModelStorage;
|
|
|
|
// Null terminate.
|
|
|
|
ResultPath.push_back(0);
|
|
|
|
ResultPath.pop_back();
|
|
|
|
|
|
|
|
retry_random_path:
|
|
|
|
// Replace '%' with random chars.
|
|
|
|
for (unsigned i = 0, e = ModelStorage.size(); i != e; ++i) {
|
|
|
|
if (ModelStorage[i] == '%')
|
|
|
|
ResultPath[i] = "0123456789abcdef"[sys::Process::GetRandomNumber() & 15];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to open + create the file.
|
|
|
|
switch (Type) {
|
|
|
|
case FS_File: {
|
2014-02-24 18:20:12 +00:00
|
|
|
if (error_code EC =
|
|
|
|
sys::fs::openFileForWrite(Twine(ResultPath.begin()), ResultFD,
|
|
|
|
sys::fs::F_RW | sys::fs::F_Excl, Mode)) {
|
2014-02-24 03:07:41 +00:00
|
|
|
if (EC == errc::file_exists)
|
|
|
|
goto retry_random_path;
|
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
|
|
|
|
return error_code::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
case FS_Name: {
|
|
|
|
bool Exists;
|
|
|
|
error_code EC = sys::fs::exists(ResultPath.begin(), Exists);
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
|
|
|
if (Exists)
|
|
|
|
goto retry_random_path;
|
|
|
|
return error_code::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
case FS_Dir: {
|
|
|
|
if (error_code EC = sys::fs::create_directory(ResultPath.begin(), false)) {
|
|
|
|
if (EC == errc::file_exists)
|
|
|
|
goto retry_random_path;
|
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
return error_code::success();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid Type");
|
|
|
|
}
|
2013-06-28 03:48:47 +00:00
|
|
|
|
2010-11-29 22:28:51 +00:00
|
|
|
namespace llvm {
|
|
|
|
namespace sys {
|
|
|
|
namespace path {
|
|
|
|
|
2010-12-17 18:19:06 +00:00
|
|
|
const_iterator begin(StringRef path) {
|
2010-11-29 22:28:51 +00:00
|
|
|
const_iterator i;
|
|
|
|
i.Path = path;
|
|
|
|
i.Component = find_first_component(path);
|
|
|
|
i.Position = 0;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2010-12-17 18:19:06 +00:00
|
|
|
const_iterator end(StringRef path) {
|
2010-11-29 22:28:51 +00:00
|
|
|
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
|
|
|
|
) {
|
2010-12-17 20:27:37 +00:00
|
|
|
Component = Path.substr(Position, 1);
|
2010-11-29 22:28:51 +00:00
|
|
|
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);
|
2010-12-17 20:27:37 +00:00
|
|
|
Component = Path.slice(Position, end_pos);
|
2010-11-29 22:28:51 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-11-30 23:28:07 +00:00
|
|
|
const_iterator &const_iterator::operator--() {
|
2014-03-05 19:56:30 +00:00
|
|
|
// If we're at the end and the previous char was a '/', return '.' unless
|
|
|
|
// we are the root path.
|
|
|
|
size_t root_dir_pos = root_dir_start(Path);
|
2010-11-30 23:28:07 +00:00
|
|
|
if (Position == Path.size() &&
|
2014-03-05 19:56:30 +00:00
|
|
|
Path.size() > root_dir_pos + 1 &&
|
|
|
|
is_separator(Path[Position - 1])) {
|
2010-11-30 23:28:07 +00:00
|
|
|
--Position;
|
|
|
|
Component = ".";
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip separators unless it's the root directory.
|
|
|
|
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.
|
2010-12-17 20:27:37 +00:00
|
|
|
size_t start_pos = filename_pos(Path.substr(0, end_pos));
|
|
|
|
Component = Path.slice(start_pos, end_pos);
|
2010-11-30 23:28:07 +00:00
|
|
|
Position = start_pos;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-11-29 22:28:51 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-11-30 23:28:07 +00:00
|
|
|
ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
|
|
|
|
return Position - RHS.Position;
|
|
|
|
}
|
|
|
|
|
2010-12-17 18:19:06 +00:00
|
|
|
const StringRef root_path(StringRef path) {
|
2010-11-29 22:28:51 +00:00
|
|
|
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.
|
2010-12-17 20:27:37 +00:00
|
|
|
return path.substr(0, b->size() + pos->size());
|
2010-11-29 22:28:51 +00:00
|
|
|
} else {
|
|
|
|
// just {C:,//net}, return the first component.
|
2010-12-07 17:04:04 +00:00
|
|
|
return *b;
|
2010-11-29 22:28:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// POSIX style root directory.
|
|
|
|
if (is_separator((*b)[0])) {
|
2010-12-07 17:04:04 +00:00
|
|
|
return *b;
|
2010-11-29 22:28:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-07 17:04:04 +00:00
|
|
|
return StringRef();
|
2010-11-29 22:28:51 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 18:19:06 +00:00
|
|
|
const StringRef root_name(StringRef path) {
|
2010-11-29 22:28:51 +00:00
|
|
|
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.
|
2010-12-07 17:04:04 +00:00
|
|
|
return *b;
|
2010-11-29 22:28:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No path or no name.
|
2010-12-07 17:04:04 +00:00
|
|
|
return StringRef();
|
2010-11-29 22:28:51 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 18:19:06 +00:00
|
|
|
const StringRef root_directory(StringRef path) {
|
2010-11-29 22:28:51 +00:00
|
|
|
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])) {
|
2010-12-07 17:04:04 +00:00
|
|
|
return *pos;
|
2010-11-29 22:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// POSIX style root directory.
|
|
|
|
if (!has_net && is_separator((*b)[0])) {
|
2010-12-07 17:04:04 +00:00
|
|
|
return *b;
|
2010-11-29 22:28:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No path or no root.
|
2010-12-07 17:04:04 +00:00
|
|
|
return StringRef();
|
2010-11-29 22:28:51 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 18:19:06 +00:00
|
|
|
const StringRef relative_path(StringRef path) {
|
2010-12-07 17:04:04 +00:00
|
|
|
StringRef root = root_path(path);
|
2012-02-29 00:06:24 +00:00
|
|
|
return path.substr(root.size());
|
2010-11-29 22:28:51 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 03:57:37 +00:00
|
|
|
void append(SmallVectorImpl<char> &path, const Twine &a,
|
|
|
|
const Twine &b,
|
|
|
|
const Twine &c,
|
|
|
|
const Twine &d) {
|
2010-11-29 22:28:51 +00:00
|
|
|
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]);
|
2010-12-07 17:04:04 +00:00
|
|
|
bool is_root_name = has_root_name(*i);
|
2010-11-29 22:28:51 +00:00
|
|
|
|
|
|
|
if (path_has_sep) {
|
|
|
|
// Strip separators from beginning of component.
|
|
|
|
size_t loc = i->find_first_not_of(separators);
|
2010-12-17 20:27:37 +00:00
|
|
|
StringRef c = i->substr(loc);
|
2010-11-29 22:28:51 +00:00
|
|
|
|
|
|
|
// Append it.
|
|
|
|
path.append(c.begin(), c.end());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-12-06 04:28:23 +00:00
|
|
|
if (!component_has_sep && !(path.empty() || is_root_name)) {
|
2010-11-29 22:28:51 +00:00
|
|
|
// Add a separator.
|
2014-01-24 17:20:08 +00:00
|
|
|
path.push_back(preferred_separator);
|
2010-11-29 22:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
path.append(i->begin(), i->end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-15 17:51:19 +00:00
|
|
|
void append(SmallVectorImpl<char> &path,
|
|
|
|
const_iterator begin, const_iterator end) {
|
|
|
|
for (; begin != end; ++begin)
|
|
|
|
path::append(path, *begin);
|
|
|
|
}
|
|
|
|
|
2010-12-17 18:19:06 +00:00
|
|
|
const StringRef parent_path(StringRef path) {
|
2010-11-30 23:28:07 +00:00
|
|
|
size_t end_pos = parent_path_end(path);
|
|
|
|
if (end_pos == StringRef::npos)
|
2010-12-07 17:04:04 +00:00
|
|
|
return StringRef();
|
2010-11-30 23:28:07 +00:00
|
|
|
else
|
2010-12-17 20:27:37 +00:00
|
|
|
return path.substr(0, end_pos);
|
2010-11-30 23:28:07 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 03:57:37 +00:00
|
|
|
void remove_filename(SmallVectorImpl<char> &path) {
|
2010-12-01 00:52:28 +00:00
|
|
|
size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
|
2010-12-07 03:57:37 +00:00
|
|
|
if (end_pos != StringRef::npos)
|
|
|
|
path.set_size(end_pos);
|
2010-12-01 00:52:28 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 17:04:04 +00:00
|
|
|
void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
|
2010-12-01 00:52:55 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2010-12-07 03:57:37 +00:00
|
|
|
void native(const Twine &path, SmallVectorImpl<char> &result) {
|
2013-09-11 10:45:21 +00:00
|
|
|
assert((!path.isSingleStringRef() ||
|
|
|
|
path.getSingleStringRef().data() != result.data()) &&
|
|
|
|
"path and result are not allowed to overlap!");
|
2010-12-01 02:48:27 +00:00
|
|
|
// Clear result.
|
2010-12-07 01:23:19 +00:00
|
|
|
result.clear();
|
2010-12-01 02:48:27 +00:00
|
|
|
path.toVector(result);
|
2013-09-11 10:45:21 +00:00
|
|
|
native(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void native(SmallVectorImpl<char> &path) {
|
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
std::replace(path.begin(), path.end(), '/', '\\');
|
2010-12-01 02:48:27 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-12-17 18:19:06 +00:00
|
|
|
const StringRef filename(StringRef path) {
|
2010-12-07 17:04:04 +00:00
|
|
|
return *(--end(path));
|
2010-12-01 03:18:17 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 18:19:06 +00:00
|
|
|
const StringRef stem(StringRef path) {
|
2010-12-07 17:04:04 +00:00
|
|
|
StringRef fname = filename(path);
|
2010-12-01 03:18:33 +00:00
|
|
|
size_t pos = fname.find_last_of('.');
|
|
|
|
if (pos == StringRef::npos)
|
2010-12-07 17:04:04 +00:00
|
|
|
return fname;
|
2010-12-01 03:18:33 +00:00
|
|
|
else
|
|
|
|
if ((fname.size() == 1 && fname == ".") ||
|
|
|
|
(fname.size() == 2 && fname == ".."))
|
2010-12-07 17:04:04 +00:00
|
|
|
return fname;
|
2010-12-01 03:18:33 +00:00
|
|
|
else
|
2010-12-17 20:27:37 +00:00
|
|
|
return fname.substr(0, pos);
|
2010-12-01 03:18:33 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 18:19:06 +00:00
|
|
|
const StringRef extension(StringRef path) {
|
2010-12-07 17:04:04 +00:00
|
|
|
StringRef fname = filename(path);
|
2010-12-01 03:37:41 +00:00
|
|
|
size_t pos = fname.find_last_of('.');
|
|
|
|
if (pos == StringRef::npos)
|
2010-12-07 17:04:04 +00:00
|
|
|
return StringRef();
|
2010-12-01 03:37:41 +00:00
|
|
|
else
|
|
|
|
if ((fname.size() == 1 && fname == ".") ||
|
|
|
|
(fname.size() == 2 && fname == ".."))
|
2010-12-07 17:04:04 +00:00
|
|
|
return StringRef();
|
2010-12-01 03:37:41 +00:00
|
|
|
else
|
2010-12-17 20:27:37 +00:00
|
|
|
return fname.substr(pos);
|
2010-12-01 03:37:41 +00:00
|
|
|
}
|
|
|
|
|
2011-02-11 21:24:40 +00:00
|
|
|
bool is_separator(char value) {
|
|
|
|
switch(value) {
|
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
case '\\': // fall through
|
|
|
|
#endif
|
|
|
|
case '/': return true;
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-14 20:27:01 +00:00
|
|
|
void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result) {
|
|
|
|
result.clear();
|
2011-12-13 23:16:15 +00:00
|
|
|
|
2014-02-12 21:02:12 +00:00
|
|
|
#if defined(_CS_DARWIN_USER_TEMP_DIR) && defined(_CS_DARWIN_USER_CACHE_DIR)
|
2013-03-21 21:46:10 +00:00
|
|
|
// On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR.
|
2014-02-12 21:02:12 +00:00
|
|
|
// macros defined in <unistd.h> on darwin >= 9
|
2013-03-21 21:46:10 +00:00
|
|
|
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
|
|
|
|
|
2011-09-14 20:27:01 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2011-12-13 23:16:15 +00:00
|
|
|
|
2011-09-14 20:27:01 +00:00
|
|
|
// Fall back to a system default.
|
|
|
|
const char *DefaultResult;
|
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
(void)erasedOnReboot;
|
2011-09-14 23:21:47 +00:00
|
|
|
DefaultResult = "C:\\TEMP";
|
2011-09-14 20:27:01 +00:00
|
|
|
#else
|
|
|
|
if (erasedOnReboot)
|
|
|
|
DefaultResult = "/tmp";
|
|
|
|
else
|
|
|
|
DefaultResult = "/var/tmp";
|
|
|
|
#endif
|
|
|
|
result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
|
|
|
|
}
|
2011-12-13 23:16:15 +00:00
|
|
|
|
2010-12-07 18:12:07 +00:00
|
|
|
bool has_root_name(const Twine &path) {
|
2010-12-01 06:03:50 +00:00
|
|
|
SmallString<128> path_storage;
|
|
|
|
StringRef p = path.toStringRef(path_storage);
|
|
|
|
|
2010-12-07 17:04:04 +00:00
|
|
|
return !root_name(p).empty();
|
2010-12-01 06:03:50 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 18:12:07 +00:00
|
|
|
bool has_root_directory(const Twine &path) {
|
2010-12-01 06:03:50 +00:00
|
|
|
SmallString<128> path_storage;
|
|
|
|
StringRef p = path.toStringRef(path_storage);
|
|
|
|
|
2010-12-07 17:04:04 +00:00
|
|
|
return !root_directory(p).empty();
|
2010-12-01 06:03:50 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 18:12:07 +00:00
|
|
|
bool has_root_path(const Twine &path) {
|
2010-12-01 06:03:50 +00:00
|
|
|
SmallString<128> path_storage;
|
|
|
|
StringRef p = path.toStringRef(path_storage);
|
|
|
|
|
2010-12-07 17:04:04 +00:00
|
|
|
return !root_path(p).empty();
|
2010-12-01 06:03:50 +00:00
|
|
|
}
|
|
|
|
|
2010-12-20 13:30:28 +00:00
|
|
|
bool has_relative_path(const Twine &path) {
|
|
|
|
SmallString<128> path_storage;
|
|
|
|
StringRef p = path.toStringRef(path_storage);
|
|
|
|
|
|
|
|
return !relative_path(p).empty();
|
|
|
|
}
|
|
|
|
|
2010-12-07 18:12:07 +00:00
|
|
|
bool has_filename(const Twine &path) {
|
2010-12-01 06:03:50 +00:00
|
|
|
SmallString<128> path_storage;
|
|
|
|
StringRef p = path.toStringRef(path_storage);
|
|
|
|
|
2010-12-07 17:04:04 +00:00
|
|
|
return !filename(p).empty();
|
2010-12-01 06:03:50 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 18:12:07 +00:00
|
|
|
bool has_parent_path(const Twine &path) {
|
2010-12-01 06:03:50 +00:00
|
|
|
SmallString<128> path_storage;
|
|
|
|
StringRef p = path.toStringRef(path_storage);
|
|
|
|
|
2010-12-07 17:04:04 +00:00
|
|
|
return !parent_path(p).empty();
|
2010-12-01 06:03:50 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 18:12:07 +00:00
|
|
|
bool has_stem(const Twine &path) {
|
2010-12-01 06:03:50 +00:00
|
|
|
SmallString<128> path_storage;
|
|
|
|
StringRef p = path.toStringRef(path_storage);
|
|
|
|
|
2010-12-07 17:04:04 +00:00
|
|
|
return !stem(p).empty();
|
2010-12-01 06:03:50 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 18:12:07 +00:00
|
|
|
bool has_extension(const Twine &path) {
|
2010-12-01 06:03:50 +00:00
|
|
|
SmallString<128> path_storage;
|
|
|
|
StringRef p = path.toStringRef(path_storage);
|
|
|
|
|
2010-12-07 17:04:04 +00:00
|
|
|
return !extension(p).empty();
|
2010-12-01 06:03:50 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 18:12:07 +00:00
|
|
|
bool is_absolute(const Twine &path) {
|
2010-12-01 06:21:53 +00:00
|
|
|
SmallString<128> path_storage;
|
|
|
|
StringRef p = path.toStringRef(path_storage);
|
|
|
|
|
2010-12-07 17:04:04 +00:00
|
|
|
bool rootDir = has_root_directory(p),
|
2010-12-01 06:21:53 +00:00
|
|
|
#ifdef LLVM_ON_WIN32
|
2010-12-07 17:04:04 +00:00
|
|
|
rootName = has_root_name(p);
|
2010-12-01 06:21:53 +00:00
|
|
|
#else
|
2010-12-07 17:04:04 +00:00
|
|
|
rootName = true;
|
2010-12-01 06:21:53 +00:00
|
|
|
#endif
|
|
|
|
|
2010-12-07 17:04:04 +00:00
|
|
|
return rootDir && rootName;
|
2010-12-01 06:21:53 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 18:12:07 +00:00
|
|
|
bool is_relative(const Twine &path) {
|
2010-12-07 17:04:04 +00:00
|
|
|
return !is_absolute(path);
|
2010-12-01 06:21:53 +00:00
|
|
|
}
|
|
|
|
|
2010-12-01 19:32:01 +00:00
|
|
|
} // end namespace path
|
|
|
|
|
|
|
|
namespace fs {
|
|
|
|
|
2013-07-29 21:26:49 +00:00
|
|
|
error_code getUniqueID(const Twine Path, UniqueID &Result) {
|
|
|
|
file_status Status;
|
|
|
|
error_code EC = status(Path, Status);
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
|
|
|
Result = Status.getUniqueID();
|
|
|
|
return error_code::success();
|
|
|
|
}
|
|
|
|
|
2013-07-05 21:01:08 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-07-05 19:56:49 +00:00
|
|
|
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) {
|
2013-07-25 15:00:17 +00:00
|
|
|
const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
|
|
|
|
return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
|
2013-07-05 19:56:49 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-28 03:48:47 +00:00
|
|
|
// This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
|
2013-06-28 10:55:41 +00:00
|
|
|
// for consistency. We should try using mkdtemp.
|
2013-06-27 03:45:31 +00:00
|
|
|
error_code createUniqueDirectory(const Twine &Prefix,
|
|
|
|
SmallVectorImpl<char> &ResultPath) {
|
2013-06-28 03:48:47 +00:00
|
|
|
int Dummy;
|
|
|
|
return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath,
|
|
|
|
true, 0, FS_Dir);
|
2013-06-27 03:45:31 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 03:57:17 +00:00
|
|
|
error_code make_absolute(SmallVectorImpl<char> &path) {
|
|
|
|
StringRef p(path.data(), path.size());
|
|
|
|
|
2012-02-29 00:20:37 +00:00
|
|
|
bool rootDirectory = path::has_root_directory(p),
|
|
|
|
#ifdef LLVM_ON_WIN32
|
2012-02-29 00:46:46 +00:00
|
|
|
rootName = path::has_root_name(p);
|
2012-02-29 00:20:37 +00:00
|
|
|
#else
|
|
|
|
rootName = true;
|
|
|
|
#endif
|
2010-12-07 03:57:17 +00:00
|
|
|
|
|
|
|
// Already absolute.
|
|
|
|
if (rootName && rootDirectory)
|
2012-02-09 19:24:12 +00:00
|
|
|
return error_code::success();
|
2010-12-07 03:57:17 +00:00
|
|
|
|
|
|
|
// 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.
|
2010-12-07 03:57:37 +00:00
|
|
|
path::append(current_dir, p);
|
2010-12-07 03:57:17 +00:00
|
|
|
// Set path to the result.
|
|
|
|
path.swap(current_dir);
|
2012-02-09 19:24:12 +00:00
|
|
|
return error_code::success();
|
2010-12-07 03:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!rootName && rootDirectory) {
|
2010-12-07 17:04:04 +00:00
|
|
|
StringRef cdrn = path::root_name(current_dir);
|
2010-12-07 03:57:17 +00:00
|
|
|
SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
|
2010-12-07 03:57:37 +00:00
|
|
|
path::append(curDirRootName, p);
|
2010-12-07 03:57:17 +00:00
|
|
|
// Set path to the result.
|
|
|
|
path.swap(curDirRootName);
|
2012-02-09 19:24:12 +00:00
|
|
|
return error_code::success();
|
2010-12-07 03:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rootName && !rootDirectory) {
|
2010-12-07 17:04:04 +00:00
|
|
|
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);
|
2010-12-07 03:57:17 +00:00
|
|
|
|
|
|
|
SmallString<128> res;
|
2010-12-07 03:57:37 +00:00
|
|
|
path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
|
2010-12-07 03:57:17 +00:00
|
|
|
path.swap(res);
|
2012-02-09 19:24:12 +00:00
|
|
|
return error_code::success();
|
2010-12-07 03:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("All rootName and rootDirectory combinations should have "
|
|
|
|
"occurred above!");
|
|
|
|
}
|
|
|
|
|
2014-02-23 13:56:14 +00:00
|
|
|
error_code create_directories(const Twine &Path, bool IgnoreExisting) {
|
2014-02-13 16:58:19 +00:00
|
|
|
SmallString<128> PathStorage;
|
|
|
|
StringRef P = Path.toStringRef(PathStorage);
|
|
|
|
|
|
|
|
// Be optimistic and try to create the directory
|
2014-02-23 13:56:14 +00:00
|
|
|
error_code EC = create_directory(P, IgnoreExisting);
|
2014-02-13 16:58:19 +00:00
|
|
|
// If we succeeded, or had any error other than the parent not existing, just
|
|
|
|
// return it.
|
|
|
|
if (EC != errc::no_such_file_or_directory)
|
|
|
|
return EC;
|
2010-12-03 05:42:11 +00:00
|
|
|
|
2014-02-13 16:58:19 +00:00
|
|
|
// We failed because of a no_such_file_or_directory, try to create the
|
|
|
|
// parent.
|
|
|
|
StringRef Parent = path::parent_path(P);
|
|
|
|
if (Parent.empty())
|
|
|
|
return EC;
|
2010-12-07 17:04:04 +00:00
|
|
|
|
2014-02-13 16:58:19 +00:00
|
|
|
if ((EC = create_directories(Parent)))
|
|
|
|
return EC;
|
2010-12-03 05:42:11 +00:00
|
|
|
|
2014-02-23 13:56:14 +00:00
|
|
|
return create_directory(P, IgnoreExisting);
|
2010-12-03 05:42:11 +00:00
|
|
|
}
|
|
|
|
|
2010-12-09 17:37:02 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-01-11 01:21:55 +00:00
|
|
|
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);
|
2012-02-09 19:24:12 +00:00
|
|
|
return error_code::success();
|
2011-01-11 01:21:55 +00:00
|
|
|
}
|
|
|
|
|
2010-12-09 17:37:02 +00:00
|
|
|
bool is_regular_file(file_status status) {
|
|
|
|
return status.type() == file_type::regular_file;
|
|
|
|
}
|
|
|
|
|
2011-01-11 01:21:55 +00:00
|
|
|
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);
|
2012-02-09 19:24:12 +00:00
|
|
|
return error_code::success();
|
2011-01-11 01:21:55 +00:00
|
|
|
}
|
|
|
|
|
2010-12-09 17:37:02 +00:00
|
|
|
bool is_symlink(file_status status) {
|
|
|
|
return status.type() == file_type::symlink_file;
|
|
|
|
}
|
|
|
|
|
2011-01-12 23:55:06 +00:00
|
|
|
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);
|
2012-02-09 19:24:12 +00:00
|
|
|
return error_code::success();
|
2011-01-12 23:55:06 +00:00
|
|
|
}
|
|
|
|
|
2010-12-09 17:37:02 +00:00
|
|
|
bool is_other(file_status status) {
|
|
|
|
return exists(status) &&
|
|
|
|
!is_regular_file(status) &&
|
|
|
|
!is_directory(status) &&
|
|
|
|
!is_symlink(status);
|
|
|
|
}
|
|
|
|
|
2011-09-14 01:14:36 +00:00
|
|
|
void directory_entry::replace_filename(const Twine &filename, file_status st) {
|
2010-12-06 04:28:42 +00:00
|
|
|
SmallString<128> path(Path.begin(), Path.end());
|
|
|
|
path::remove_filename(path);
|
|
|
|
path::append(path, filename);
|
|
|
|
Path = path.str();
|
|
|
|
Status = st;
|
|
|
|
}
|
|
|
|
|
2010-12-28 01:49:01 +00:00
|
|
|
error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
|
|
|
|
SmallString<32> MagicStorage;
|
2011-01-15 18:52:41 +00:00
|
|
|
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).
|
2010-12-28 01:49:01 +00:00
|
|
|
result = false;
|
2012-02-09 19:24:12 +00:00
|
|
|
return error_code::success();
|
2010-12-28 01:49:01 +00:00
|
|
|
}
|
2011-01-15 18:52:41 +00:00
|
|
|
return ec;
|
2010-12-28 01:49:01 +00:00
|
|
|
}
|
|
|
|
|
2011-01-15 18:52:41 +00:00
|
|
|
result = Magic == Buffer;
|
2012-02-09 19:24:12 +00:00
|
|
|
return error_code::success();
|
2010-12-28 01:49:01 +00:00
|
|
|
}
|
|
|
|
|
2011-12-13 23:17:12 +00:00
|
|
|
/// @brief Identify the magic in magic.
|
2013-06-11 17:22:12 +00:00
|
|
|
file_magic identify_magic(StringRef Magic) {
|
|
|
|
if (Magic.size() < 4)
|
2012-06-19 05:29:57 +00:00
|
|
|
return file_magic::unknown;
|
2013-06-11 17:22:12 +00:00
|
|
|
switch ((unsigned char)Magic[0]) {
|
2013-10-15 22:45:38 +00:00
|
|
|
case 0x00: {
|
2013-11-15 21:22:02 +00:00
|
|
|
// COFF short import library file
|
|
|
|
if (Magic[1] == (char)0x00 && Magic[2] == (char)0xff &&
|
|
|
|
Magic[3] == (char)0xff)
|
|
|
|
return file_magic::coff_import_library;
|
2013-10-15 22:45:38 +00:00
|
|
|
// Windows resource file
|
2013-10-16 03:29:49 +00:00
|
|
|
const char Expected[] = { 0, 0, 0, 0, '\x20', 0, 0, 0, '\xff' };
|
2013-10-15 22:45:38 +00:00
|
|
|
if (Magic.size() >= sizeof(Expected) &&
|
|
|
|
memcmp(Magic.data(), Expected, sizeof(Expected)) == 0)
|
|
|
|
return file_magic::windows_resource;
|
2013-11-14 22:09:08 +00:00
|
|
|
// 0x0000 = COFF unknown machine type
|
|
|
|
if (Magic[1] == 0)
|
|
|
|
return file_magic::coff_object;
|
2013-10-15 22:45:38 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-12-13 23:17:12 +00:00
|
|
|
case 0xDE: // 0x0B17C0DE = BC wraper
|
2013-06-11 17:22:12 +00:00
|
|
|
if (Magic[1] == (char)0xC0 && Magic[2] == (char)0x17 &&
|
|
|
|
Magic[3] == (char)0x0B)
|
2011-12-13 23:17:12 +00:00
|
|
|
return file_magic::bitcode;
|
|
|
|
break;
|
|
|
|
case 'B':
|
2013-06-11 17:22:12 +00:00
|
|
|
if (Magic[1] == 'C' && Magic[2] == (char)0xC0 && Magic[3] == (char)0xDE)
|
2011-12-13 23:17:12 +00:00
|
|
|
return file_magic::bitcode;
|
|
|
|
break;
|
|
|
|
case '!':
|
2013-06-11 17:22:12 +00:00
|
|
|
if (Magic.size() >= 8)
|
|
|
|
if (memcmp(Magic.data(),"!<arch>\n",8) == 0)
|
2011-12-13 23:17:12 +00:00
|
|
|
return file_magic::archive;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\177':
|
2013-06-11 17:25:45 +00:00
|
|
|
if (Magic.size() >= 18 && Magic[1] == 'E' && Magic[2] == 'L' &&
|
|
|
|
Magic[3] == 'F') {
|
2013-06-11 17:22:12 +00:00
|
|
|
bool Data2MSB = Magic[5] == 2;
|
2013-04-05 20:10:04 +00:00
|
|
|
unsigned high = Data2MSB ? 16 : 17;
|
|
|
|
unsigned low = Data2MSB ? 17 : 16;
|
2013-06-11 17:25:45 +00:00
|
|
|
if (Magic[high] == 0)
|
2013-06-11 17:22:12 +00:00
|
|
|
switch (Magic[low]) {
|
2011-12-13 23:17:12 +00:00
|
|
|
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:
|
2013-06-11 17:22:12 +00:00
|
|
|
if (Magic[1] == char(0xFE) && Magic[2] == char(0xBA) &&
|
|
|
|
Magic[3] == char(0xBE)) {
|
2011-12-13 23:17:12 +00:00
|
|
|
// This is complicated by an overlap with Java class files.
|
|
|
|
// See the Mach-O section in /usr/share/file/magic for details.
|
2013-06-11 17:22:12 +00:00
|
|
|
if (Magic.size() >= 8 && Magic[7] < 43)
|
2013-06-18 15:03:28 +00:00
|
|
|
return file_magic::macho_universal_binary;
|
2011-12-13 23:17:12 +00:00
|
|
|
}
|
|
|
|
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;
|
2013-06-11 17:22:12 +00:00
|
|
|
if (Magic[0] == char(0xFE) && Magic[1] == char(0xED) &&
|
|
|
|
Magic[2] == char(0xFA) &&
|
|
|
|
(Magic[3] == char(0xCE) || Magic[3] == char(0xCF))) {
|
2011-12-13 23:17:12 +00:00
|
|
|
/* Native endian */
|
2013-06-11 17:22:12 +00:00
|
|
|
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)) {
|
2011-12-13 23:17:12 +00:00
|
|
|
/* Reverse endian */
|
2013-06-11 17:22:12 +00:00
|
|
|
if (Magic.size() >= 14) type = Magic[13] << 8 | Magic[12];
|
2011-12-13 23:17:12 +00:00
|
|
|
}
|
|
|
|
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;
|
2013-06-10 20:32:27 +00:00
|
|
|
case 5: return file_magic::macho_preload_executable;
|
2011-12-13 23:17:12 +00:00
|
|
|
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
|
2014-03-13 07:02:35 +00:00
|
|
|
case 0xc4: // ARMNT Windows
|
2013-06-11 17:22:12 +00:00
|
|
|
if (Magic[1] == 0x01)
|
2011-12-13 23:17:12 +00:00
|
|
|
return file_magic::coff_object;
|
|
|
|
|
|
|
|
case 0x90: // PA-RISC Windows
|
|
|
|
case 0x68: // mc68K Windows
|
2013-06-11 17:22:12 +00:00
|
|
|
if (Magic[1] == 0x02)
|
2011-12-13 23:17:12 +00:00
|
|
|
return file_magic::coff_object;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x4d: // Possible MS-DOS stub on Windows PE file
|
2013-06-11 17:22:12 +00:00
|
|
|
if (Magic[1] == 0x5a) {
|
2011-12-13 23:17:12 +00:00
|
|
|
uint32_t off =
|
2013-06-11 17:22:12 +00:00
|
|
|
*reinterpret_cast<const support::ulittle32_t*>(Magic.data() + 0x3c);
|
2011-12-13 23:17:12 +00:00
|
|
|
// PE/COFF file, either EXE or DLL.
|
2013-06-11 17:22:12 +00:00
|
|
|
if (off < Magic.size() && memcmp(Magic.data() + off, "PE\0\0",4) == 0)
|
2011-12-13 23:17:12 +00:00
|
|
|
return file_magic::pecoff_executable;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x64: // x86-64 Windows.
|
2013-06-11 17:22:12 +00:00
|
|
|
if (Magic[1] == char(0x86))
|
2011-12-13 23:17:12 +00:00
|
|
|
return file_magic::coff_object;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return file_magic::unknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
error_code identify_magic(const Twine &path, file_magic &result) {
|
2011-01-15 20:39:36 +00:00
|
|
|
SmallString<32> Magic;
|
|
|
|
error_code ec = get_magic(path, Magic.capacity(), Magic);
|
|
|
|
if (ec && ec != errc::value_too_large)
|
|
|
|
return ec;
|
|
|
|
|
2011-12-13 23:17:12 +00:00
|
|
|
result = identify_magic(Magic);
|
2012-02-09 19:24:12 +00:00
|
|
|
return error_code::success();
|
2011-01-15 20:39:36 +00:00
|
|
|
}
|
|
|
|
|
2011-01-05 16:39:13 +00:00
|
|
|
error_code directory_entry::status(file_status &result) const {
|
|
|
|
return fs::status(Path, result);
|
|
|
|
}
|
|
|
|
|
2010-12-01 19:32:01 +00:00
|
|
|
} // end namespace fs
|
|
|
|
} // end namespace sys
|
|
|
|
} // end namespace llvm
|
2010-11-29 22:28:51 +00:00
|
|
|
|
|
|
|
// Include the truly platform-specific parts.
|
|
|
|
#if defined(LLVM_ON_UNIX)
|
2013-06-26 19:33:03 +00:00
|
|
|
#include "Unix/Path.inc"
|
2010-11-29 22:28:51 +00:00
|
|
|
#endif
|
|
|
|
#if defined(LLVM_ON_WIN32)
|
2013-06-26 19:33:03 +00:00
|
|
|
#include "Windows/Path.inc"
|
2010-11-29 22:28:51 +00:00
|
|
|
#endif
|