#include "llvm/Support/Path.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/Config/config.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/Signals.h"
#include <cctype>
#if !defined(_MSC_VER) && !defined(__MINGW32__)
#include <unistd.h>
#else
#include <io.h>
#endif
using namespace llvm;
using namespace llvm::support::endian;
namespace {
using llvm::StringRef;
using llvm::sys::path::is_separator;
using llvm::sys::path::Style;
inline Style real_style(Style style) {
if (style != Style::native)
return style;
if (is_style_posix(style))
return Style::posix;
return LLVM_WINDOWS_PREFER_FORWARD_SLASH ? Style::windows_slash
: Style::windows_backslash;
}
inline const char *separators(Style style) {
if (is_style_windows(style))
return "\\/";
return "/";
}
inline char preferred_separator(Style style) {
if (real_style(style) == Style::windows)
return '\\';
return '/';
}
StringRef find_first_component(StringRef path, Style style) {
if (path.empty())
return path;
if (is_style_windows(style)) {
if (path.size() >= 2 &&
std::isalpha(static_cast<unsigned char>(path[0])) && path[1] == ':')
return path.substr(0, 2);
}
if ((path.size() > 2) && is_separator(path[0], style) &&
path[0] == path[1] && !is_separator(path[2], style)) {
size_t end = path.find_first_of(separators(style), 2);
return path.substr(0, end);
}
if (is_separator(path[0], style))
return path.substr(0, 1);
size_t end = path.find_first_of(separators(style));
return path.substr(0, end);
}
size_t filename_pos(StringRef str, Style style) {
if (str.size() > 0 && is_separator(str[str.size() - 1], style))
return str.size() - 1;
size_t pos = str.find_last_of(separators(style), str.size() - 1);
if (is_style_windows(style)) {
if (pos == StringRef::npos)
pos = str.find_last_of(':', str.size() - 2);
}
if (pos == StringRef::npos || (pos == 1 && is_separator(str[0], style)))
return 0;
return pos + 1;
}
size_t root_dir_start(StringRef str, Style style) {
if (is_style_windows(style)) {
if (str.size() > 2 && str[1] == ':' && is_separator(str[2], style))
return 2;
}
if (str.size() > 3 && is_separator(str[0], style) && str[0] == str[1] &&
!is_separator(str[2], style)) {
return str.find_first_of(separators(style), 2);
}
if (str.size() > 0 && is_separator(str[0], style))
return 0;
return StringRef::npos;
}
size_t parent_path_end(StringRef path, Style style) {
size_t end_pos = filename_pos(path, style);
bool filename_was_sep =
path.size() > 0 && is_separator(path[end_pos], style);
size_t root_dir_pos = root_dir_start(path, style);
while (end_pos > 0 &&
(root_dir_pos == StringRef::npos || end_pos > root_dir_pos) &&
is_separator(path[end_pos - 1], style))
--end_pos;
if (end_pos == root_dir_pos && !filename_was_sep) {
return root_dir_pos + 1;
}
return end_pos;
}
}
enum FSEntity {
FS_Dir,
FS_File,
FS_Name
};
static std::error_code
createUniqueEntity(const Twine &Model, int &ResultFD,
SmallVectorImpl<char> &ResultPath, bool MakeAbsolute,
FSEntity Type, sys::fs::OpenFlags Flags = sys::fs::OF_None,
unsigned Mode = 0) {
std::error_code EC;
for (int Retries = 128; Retries > 0; --Retries) {
sys::fs::createUniquePath(Model, ResultPath, MakeAbsolute);
switch (Type) {
case FS_File: {
EC = sys::fs::openFileForReadWrite(Twine(ResultPath.begin()), ResultFD,
sys::fs::CD_CreateNew, Flags, Mode);
if (EC) {
if (EC == errc::file_exists || EC == errc::permission_denied)
continue;
return EC;
}
return std::error_code();
}
case FS_Name: {
EC = sys::fs::access(ResultPath.begin(), sys::fs::AccessMode::Exist);
if (EC == errc::no_such_file_or_directory)
return std::error_code();
if (EC)
return EC;
continue;
}
case FS_Dir: {
EC = sys::fs::create_directory(ResultPath.begin(), false);
if (EC) {
if (EC == errc::file_exists)
continue;
return EC;
}
return std::error_code();
}
}
llvm_unreachable("Invalid Type");
}
return EC;
}
namespace llvm {
namespace sys {
namespace path {
const_iterator begin(StringRef path, Style style) {
const_iterator i;
i.Path = path;
i.Component = find_first_component(path, style);
i.Position = 0;
i.S = style;
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!");
Position += Component.size();
if (Position == Path.size()) {
Component = StringRef();
return *this;
}
bool was_net = Component.size() > 2 && is_separator(Component[0], S) &&
Component[1] == Component[0] && !is_separator(Component[2], S);
if (is_separator(Path[Position], S)) {
if (was_net ||
(is_style_windows(S) && Component.endswith(":"))) {
Component = Path.substr(Position, 1);
return *this;
}
while (Position != Path.size() && is_separator(Path[Position], S)) {
++Position;
}
if (Position == Path.size() && Component != "/") {
--Position;
Component = ".";
return *this;
}
}
size_t end_pos = Path.find_first_of(separators(S), Position);
Component = Path.slice(Position, end_pos);
return *this;
}
bool const_iterator::operator==(const const_iterator &RHS) const {
return Path.begin() == RHS.Path.begin() && Position == RHS.Position;
}
ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
return Position - RHS.Position;
}
reverse_iterator rbegin(StringRef Path, Style style) {
reverse_iterator I;
I.Path = Path;
I.Position = Path.size();
I.S = style;
++I;
return I;
}
reverse_iterator rend(StringRef Path) {
reverse_iterator I;
I.Path = Path;
I.Component = Path.substr(0, 0);
I.Position = 0;
return I;
}
reverse_iterator &reverse_iterator::operator++() {
size_t root_dir_pos = root_dir_start(Path, S);
size_t end_pos = Position;
while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
is_separator(Path[end_pos - 1], S))
--end_pos;
if (Position == Path.size() && !Path.empty() &&
is_separator(Path.back(), S) &&
(root_dir_pos == StringRef::npos || end_pos - 1 > root_dir_pos)) {
--Position;
Component = ".";
return *this;
}
size_t start_pos = filename_pos(Path.substr(0, end_pos), S);
Component = Path.slice(start_pos, end_pos);
Position = start_pos;
return *this;
}
bool reverse_iterator::operator==(const reverse_iterator &RHS) const {
return Path.begin() == RHS.Path.begin() && Component == RHS.Component &&
Position == RHS.Position;
}
ptrdiff_t reverse_iterator::operator-(const reverse_iterator &RHS) const {
return Position - RHS.Position;
}
StringRef root_path(StringRef path, Style style) {
const_iterator b = begin(path, style), pos = b, e = end(path);
if (b != e) {
bool has_net =
b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
bool has_drive = is_style_windows(style) && b->endswith(":");
if (has_net || has_drive) {
if ((++pos != e) && is_separator((*pos)[0], style)) {
return path.substr(0, b->size() + pos->size());
}
return *b;
}
if (is_separator((*b)[0], style)) {
return *b;
}
}
return StringRef();
}
StringRef root_name(StringRef path, Style style) {
const_iterator b = begin(path, style), e = end(path);
if (b != e) {
bool has_net =
b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
bool has_drive = is_style_windows(style) && b->endswith(":");
if (has_net || has_drive) {
return *b;
}
}
return StringRef();
}
StringRef root_directory(StringRef path, Style style) {
const_iterator b = begin(path, style), pos = b, e = end(path);
if (b != e) {
bool has_net =
b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
bool has_drive = is_style_windows(style) && b->endswith(":");
if ((has_net || has_drive) &&
(++pos != e) && is_separator((*pos)[0], style)) {
return *pos;
}
if (!has_net && is_separator((*b)[0], style)) {
return *b;
}
}
return StringRef();
}
StringRef relative_path(StringRef path, Style style) {
StringRef root = root_path(path, style);
return path.substr(root.size());
}
void append(SmallVectorImpl<char> &path, Style style, 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 (auto &component : components) {
bool path_has_sep =
!path.empty() && is_separator(path[path.size() - 1], style);
if (path_has_sep) {
size_t loc = component.find_first_not_of(separators(style));
StringRef c = component.substr(loc);
path.append(c.begin(), c.end());
continue;
}
bool component_has_sep =
!component.empty() && is_separator(component[0], style);
if (!component_has_sep &&
!(path.empty() || has_root_name(component, style))) {
path.push_back(preferred_separator(style));
}
path.append(component.begin(), component.end());
}
}
void append(SmallVectorImpl<char> &path, const Twine &a, const Twine &b,
const Twine &c, const Twine &d) {
append(path, Style::native, a, b, c, d);
}
void append(SmallVectorImpl<char> &path, const_iterator begin,
const_iterator end, Style style) {
for (; begin != end; ++begin)
path::append(path, style, *begin);
}
StringRef parent_path(StringRef path, Style style) {
size_t end_pos = parent_path_end(path, style);
if (end_pos == StringRef::npos)
return StringRef();
return path.substr(0, end_pos);
}
void remove_filename(SmallVectorImpl<char> &path, Style style) {
size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()), style);
if (end_pos != StringRef::npos)
path.truncate(end_pos);
}
void replace_extension(SmallVectorImpl<char> &path, const Twine &extension,
Style style) {
StringRef p(path.begin(), path.size());
SmallString<32> ext_storage;
StringRef ext = extension.toStringRef(ext_storage);
size_t pos = p.find_last_of('.');
if (pos != StringRef::npos && pos >= filename_pos(p, style))
path.truncate(pos);
if (ext.size() > 0 && ext[0] != '.')
path.push_back('.');
path.append(ext.begin(), ext.end());
}
static bool starts_with(StringRef Path, StringRef Prefix,
Style style = Style::native) {
if (is_style_windows(style)) {
if (Path.size() < Prefix.size())
return false;
for (size_t I = 0, E = Prefix.size(); I != E; ++I) {
bool SepPath = is_separator(Path[I], style);
bool SepPrefix = is_separator(Prefix[I], style);
if (SepPath != SepPrefix)
return false;
if (!SepPath && toLower(Path[I]) != toLower(Prefix[I]))
return false;
}
return true;
}
return Path.startswith(Prefix);
}
bool replace_path_prefix(SmallVectorImpl<char> &Path, StringRef OldPrefix,
StringRef NewPrefix, Style style) {
if (OldPrefix.empty() && NewPrefix.empty())
return false;
StringRef OrigPath(Path.begin(), Path.size());
if (!starts_with(OrigPath, OldPrefix, style))
return false;
if (OldPrefix.size() == NewPrefix.size()) {
llvm::copy(NewPrefix, Path.begin());
return true;
}
StringRef RelPath = OrigPath.substr(OldPrefix.size());
SmallString<256> NewPath;
(Twine(NewPrefix) + RelPath).toVector(NewPath);
Path.swap(NewPath);
return true;
}
void native(const Twine &path, SmallVectorImpl<char> &result, Style style) {
assert((!path.isSingleStringRef() ||
path.getSingleStringRef().data() != result.data()) &&
"path and result are not allowed to overlap!");
result.clear();
path.toVector(result);
native(result, style);
}
void native(SmallVectorImpl<char> &Path, Style style) {
if (Path.empty())
return;
if (is_style_windows(style)) {
for (char &Ch : Path)
if (is_separator(Ch, style))
Ch = preferred_separator(style);
if (Path[0] == '~' && (Path.size() == 1 || is_separator(Path[1], style))) {
SmallString<128> PathHome;
home_directory(PathHome);
PathHome.append(Path.begin() + 1, Path.end());
Path = PathHome;
}
} else {
std::replace(Path.begin(), Path.end(), '\\', '/');
}
}
std::string convert_to_slash(StringRef path, Style style) {
if (is_style_posix(style))
return std::string(path);
std::string s = path.str();
std::replace(s.begin(), s.end(), '\\', '/');
return s;
}
StringRef filename(StringRef path, Style style) { return *rbegin(path, style); }
StringRef stem(StringRef path, Style style) {
StringRef fname = filename(path, style);
size_t pos = fname.find_last_of('.');
if (pos == StringRef::npos)
return fname;
if ((fname.size() == 1 && fname == ".") ||
(fname.size() == 2 && fname == ".."))
return fname;
return fname.substr(0, pos);
}
StringRef extension(StringRef path, Style style) {
StringRef fname = filename(path, style);
size_t pos = fname.find_last_of('.');
if (pos == StringRef::npos)
return StringRef();
if ((fname.size() == 1 && fname == ".") ||
(fname.size() == 2 && fname == ".."))
return StringRef();
return fname.substr(pos);
}
bool is_separator(char value, Style style) {
if (value == '/')
return true;
if (is_style_windows(style))
return value == '\\';
return false;
}
StringRef get_separator(Style style) {
if (real_style(style) == Style::windows)
return "\\";
return "/";
}
bool has_root_name(const Twine &path, Style style) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
return !root_name(p, style).empty();
}
bool has_root_directory(const Twine &path, Style style) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
return !root_directory(p, style).empty();
}
bool has_root_path(const Twine &path, Style style) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
return !root_path(p, style).empty();
}
bool has_relative_path(const Twine &path, Style style) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
return !relative_path(p, style).empty();
}
bool has_filename(const Twine &path, Style style) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
return !filename(p, style).empty();
}
bool has_parent_path(const Twine &path, Style style) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
return !parent_path(p, style).empty();
}
bool has_stem(const Twine &path, Style style) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
return !stem(p, style).empty();
}
bool has_extension(const Twine &path, Style style) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
return !extension(p, style).empty();
}
bool is_absolute(const Twine &path, Style style) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
bool rootDir = has_root_directory(p, style);
bool rootName = is_style_posix(style) || has_root_name(p, style);
return rootDir && rootName;
}
bool is_absolute_gnu(const Twine &path, Style style) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
if (!p.empty() && is_separator(p.front(), style))
return true;
if (is_style_windows(style)) {
if (p.size() >= 2 && (p[0] && p[1] == ':'))
return true;
}
return false;
}
bool is_relative(const Twine &path, Style style) {
return !is_absolute(path, style);
}
StringRef remove_leading_dotslash(StringRef Path, Style style) {
while (Path.size() > 2 && Path[0] == '.' && is_separator(Path[1], style)) {
Path = Path.substr(2);
while (Path.size() > 0 && is_separator(Path[0], style))
Path = Path.substr(1);
}
return Path;
}
bool remove_dots(SmallVectorImpl<char> &the_path, bool remove_dot_dot,
Style style) {
style = real_style(style);
StringRef remaining(the_path.data(), the_path.size());
bool needs_change = false;
SmallVector<StringRef, 16> components;
StringRef root = path::root_path(remaining, style);
bool absolute = !root.empty();
if (absolute)
remaining = remaining.drop_front(root.size());
while (!remaining.empty()) {
size_t next_slash = remaining.find_first_of(separators(style));
if (next_slash == StringRef::npos)
next_slash = remaining.size();
StringRef component = remaining.take_front(next_slash);
remaining = remaining.drop_front(next_slash);
if (!remaining.empty()) {
needs_change |= remaining.front() != preferred_separator(style);
remaining = remaining.drop_front();
needs_change |= remaining.empty();
}
if (component.empty() || component == ".") {
needs_change = true;
} else if (remove_dot_dot && component == "..") {
needs_change = true;
if (!components.empty() && components.back() != "..") {
components.pop_back();
} else if (!absolute) {
components.push_back(component);
}
} else {
components.push_back(component);
}
}
SmallString<256> buffer = root;
make_preferred(buffer, style);
needs_change |= root != buffer;
if (!needs_change)
return false;
if (!components.empty()) {
buffer += components[0];
for (StringRef C : makeArrayRef(components).drop_front()) {
buffer += preferred_separator(style);
buffer += C;
}
}
the_path.swap(buffer);
return true;
}
}
namespace fs {
std::error_code getUniqueID(const Twine Path, UniqueID &Result) {
file_status Status;
std::error_code EC = status(Path, Status);
if (EC)
return EC;
Result = Status.getUniqueID();
return std::error_code();
}
void createUniquePath(const Twine &Model, SmallVectorImpl<char> &ResultPath,
bool MakeAbsolute) {
SmallString<128> ModelStorage;
Model.toVector(ModelStorage);
if (MakeAbsolute) {
if (!sys::path::is_absolute(Twine(ModelStorage))) {
SmallString<128> TDir;
sys::path::system_temp_directory(true, TDir);
sys::path::append(TDir, Twine(ModelStorage));
ModelStorage.swap(TDir);
}
}
ResultPath = ModelStorage;
ResultPath.push_back(0);
ResultPath.pop_back();
for (unsigned i = 0, e = ModelStorage.size(); i != e; ++i) {
if (ModelStorage[i] == '%')
ResultPath[i] = "0123456789abcdef"[sys::Process::GetRandomNumber() & 15];
}
}
std::error_code createUniqueFile(const Twine &Model, int &ResultFd,
SmallVectorImpl<char> &ResultPath,
OpenFlags Flags, unsigned Mode) {
return createUniqueEntity(Model, ResultFd, ResultPath, false, FS_File, Flags,
Mode);
}
std::error_code createUniqueFile(const Twine &Model,
SmallVectorImpl<char> &ResultPath,
unsigned Mode) {
int FD;
auto EC = createUniqueFile(Model, FD, ResultPath, OF_None, Mode);
if (EC)
return EC;
close(FD);
return EC;
}
static std::error_code
createTemporaryFile(const Twine &Model, int &ResultFD,
llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type,
sys::fs::OpenFlags Flags = sys::fs::OF_None) {
SmallString<128> Storage;
StringRef P = Model.toNullTerminatedStringRef(Storage);
assert(P.find_first_of(separators(Style::native)) == StringRef::npos &&
"Model must be a simple filename.");
return createUniqueEntity(P.begin(), ResultFD, ResultPath, true, Type, Flags,
owner_read | owner_write);
}
static std::error_code
createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type,
sys::fs::OpenFlags Flags = sys::fs::OF_None) {
const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
Type, Flags);
}
std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
int &ResultFD,
SmallVectorImpl<char> &ResultPath,
sys::fs::OpenFlags Flags) {
return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File,
Flags);
}
std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
SmallVectorImpl<char> &ResultPath,
sys::fs::OpenFlags Flags) {
int FD;
auto EC = createTemporaryFile(Prefix, Suffix, FD, ResultPath, Flags);
if (EC)
return EC;
close(FD);
return EC;
}
std::error_code createUniqueDirectory(const Twine &Prefix,
SmallVectorImpl<char> &ResultPath) {
int Dummy;
return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath, true,
FS_Dir);
}
std::error_code
getPotentiallyUniqueFileName(const Twine &Model,
SmallVectorImpl<char> &ResultPath) {
int Dummy;
return createUniqueEntity(Model, Dummy, ResultPath, false, FS_Name);
}
std::error_code
getPotentiallyUniqueTempFileName(const Twine &Prefix, StringRef Suffix,
SmallVectorImpl<char> &ResultPath) {
int Dummy;
return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
}
void make_absolute(const Twine ¤t_directory,
SmallVectorImpl<char> &path) {
StringRef p(path.data(), path.size());
bool rootDirectory = path::has_root_directory(p);
bool rootName = path::has_root_name(p);
if ((rootName || is_style_posix(Style::native)) && rootDirectory)
return;
SmallString<128> current_dir;
current_directory.toVector(current_dir);
if (!rootName && !rootDirectory) {
path::append(current_dir, p);
path.swap(current_dir);
return;
}
if (!rootName && rootDirectory) {
StringRef cdrn = path::root_name(current_dir);
SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
path::append(curDirRootName, p);
path.swap(curDirRootName);
return;
}
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;
}
llvm_unreachable("All rootName and rootDirectory combinations should have "
"occurred above!");
}
std::error_code make_absolute(SmallVectorImpl<char> &path) {
if (path::is_absolute(path))
return {};
SmallString<128> current_dir;
if (std::error_code ec = current_path(current_dir))
return ec;
make_absolute(current_dir, path);
return {};
}
std::error_code create_directories(const Twine &Path, bool IgnoreExisting,
perms Perms) {
SmallString<128> PathStorage;
StringRef P = Path.toStringRef(PathStorage);
std::error_code EC = create_directory(P, IgnoreExisting, Perms);
if (EC != errc::no_such_file_or_directory)
return EC;
StringRef Parent = path::parent_path(P);
if (Parent.empty())
return EC;
if ((EC = create_directories(Parent, IgnoreExisting, Perms)))
return EC;
return create_directory(P, IgnoreExisting, Perms);
}
static std::error_code copy_file_internal(int ReadFD, int WriteFD) {
const size_t BufSize = 4096;
char *Buf = new char[BufSize];
int BytesRead = 0, BytesWritten = 0;
for (;;) {
BytesRead = read(ReadFD, Buf, BufSize);
if (BytesRead <= 0)
break;
while (BytesRead) {
BytesWritten = write(WriteFD, Buf, BytesRead);
if (BytesWritten < 0)
break;
BytesRead -= BytesWritten;
}
if (BytesWritten < 0)
break;
}
delete[] Buf;
if (BytesRead < 0 || BytesWritten < 0)
return std::error_code(errno, std::generic_category());
return std::error_code();
}
#ifndef __APPLE__
std::error_code copy_file(const Twine &From, const Twine &To) {
int ReadFD, WriteFD;
if (std::error_code EC = openFileForRead(From, ReadFD, OF_None))
return EC;
if (std::error_code EC =
openFileForWrite(To, WriteFD, CD_CreateAlways, OF_None)) {
close(ReadFD);
return EC;
}
std::error_code EC = copy_file_internal(ReadFD, WriteFD);
close(ReadFD);
close(WriteFD);
return EC;
}
#endif
std::error_code copy_file(const Twine &From, int ToFD) {
int ReadFD;
if (std::error_code EC = openFileForRead(From, ReadFD, OF_None))
return EC;
std::error_code EC = copy_file_internal(ReadFD, ToFD);
close(ReadFD);
return EC;
}
ErrorOr<MD5::MD5Result> md5_contents(int FD) {
MD5 Hash;
constexpr size_t BufSize = 4096;
std::vector<uint8_t> Buf(BufSize);
int BytesRead = 0;
for (;;) {
BytesRead = read(FD, Buf.data(), BufSize);
if (BytesRead <= 0)
break;
Hash.update(makeArrayRef(Buf.data(), BytesRead));
}
if (BytesRead < 0)
return std::error_code(errno, std::generic_category());
MD5::MD5Result Result;
Hash.final(Result);
return Result;
}
ErrorOr<MD5::MD5Result> md5_contents(const Twine &Path) {
int FD;
if (auto EC = openFileForRead(Path, FD, OF_None))
return EC;
auto Result = md5_contents(FD);
close(FD);
return Result;
}
bool exists(const basic_file_status &status) {
return status_known(status) && status.type() != file_type::file_not_found;
}
bool status_known(const basic_file_status &s) {
return s.type() != file_type::status_error;
}
file_type get_file_type(const Twine &Path, bool Follow) {
file_status st;
if (status(Path, st, Follow))
return file_type::status_error;
return st.type();
}
bool is_directory(const basic_file_status &status) {
return status.type() == file_type::directory_file;
}
std::error_code is_directory(const Twine &path, bool &result) {
file_status st;
if (std::error_code ec = status(path, st))
return ec;
result = is_directory(st);
return std::error_code();
}
bool is_regular_file(const basic_file_status &status) {
return status.type() == file_type::regular_file;
}
std::error_code is_regular_file(const Twine &path, bool &result) {
file_status st;
if (std::error_code ec = status(path, st))
return ec;
result = is_regular_file(st);
return std::error_code();
}
bool is_symlink_file(const basic_file_status &status) {
return status.type() == file_type::symlink_file;
}
std::error_code is_symlink_file(const Twine &path, bool &result) {
file_status st;
if (std::error_code ec = status(path, st, false))
return ec;
result = is_symlink_file(st);
return std::error_code();
}
bool is_other(const basic_file_status &status) {
return exists(status) &&
!is_regular_file(status) &&
!is_directory(status);
}
std::error_code is_other(const Twine &Path, bool &Result) {
file_status FileStatus;
if (std::error_code EC = status(Path, FileStatus))
return EC;
Result = is_other(FileStatus);
return std::error_code();
}
void directory_entry::replace_filename(const Twine &Filename, file_type Type,
basic_file_status Status) {
SmallString<128> PathStr = path::parent_path(Path);
path::append(PathStr, Filename);
this->Path = std::string(PathStr.str());
this->Type = Type;
this->Status = Status;
}
ErrorOr<perms> getPermissions(const Twine &Path) {
file_status Status;
if (std::error_code EC = status(Path, Status))
return EC;
return Status.permissions();
}
size_t mapped_file_region::size() const {
assert(Mapping && "Mapping failed but used anyway!");
return Size;
}
char *mapped_file_region::data() const {
assert(Mapping && "Mapping failed but used anyway!");
return reinterpret_cast<char *>(Mapping);
}
const char *mapped_file_region::const_data() const {
assert(Mapping && "Mapping failed but used anyway!");
return reinterpret_cast<const char *>(Mapping);
}
Error readNativeFileToEOF(file_t FileHandle, SmallVectorImpl<char> &Buffer,
ssize_t ChunkSize) {
size_t Size = Buffer.size();
auto TruncateOnExit = make_scope_exit([&]() { Buffer.truncate(Size); });
for (;;) {
Buffer.resize_for_overwrite(Size + ChunkSize);
Expected<size_t> ReadBytes = readNativeFile(
FileHandle, makeMutableArrayRef(Buffer.begin() + Size, ChunkSize));
if (!ReadBytes)
return ReadBytes.takeError();
if (*ReadBytes == 0)
return Error::success();
Size += *ReadBytes;
}
}
} } }
#if defined(LLVM_ON_UNIX)
#include "Unix/Path.inc"
#endif
#if defined(_WIN32)
#include "Windows/Path.inc"
#endif
bool IsLLVMDriver = false;
namespace llvm {
namespace sys {
namespace fs {
std::string getMainExecutable(const char *Argv0, void *MainAddr) {
if (IsLLVMDriver)
return sys::path::stem(Argv0).str();
return getMainExecutableImpl(Argv0, MainAddr);
}
TempFile::TempFile(StringRef Name, int FD)
: TmpName(std::string(Name)), FD(FD) {}
TempFile::TempFile(TempFile &&Other) { *this = std::move(Other); }
TempFile &TempFile::operator=(TempFile &&Other) {
TmpName = std::move(Other.TmpName);
FD = Other.FD;
Other.Done = true;
Other.FD = -1;
#ifdef _WIN32
RemoveOnClose = Other.RemoveOnClose;
Other.RemoveOnClose = false;
#endif
return *this;
}
TempFile::~TempFile() { assert(Done); }
Error TempFile::discard() {
Done = true;
if (FD != -1 && close(FD) == -1) {
std::error_code EC = std::error_code(errno, std::generic_category());
return errorCodeToError(EC);
}
FD = -1;
#ifdef _WIN32
bool Remove = RemoveOnClose;
#else
bool Remove = true;
#endif
std::error_code RemoveEC;
if (Remove && !TmpName.empty()) {
RemoveEC = fs::remove(TmpName);
sys::DontRemoveFileOnSignal(TmpName);
if (!RemoveEC)
TmpName = "";
} else {
TmpName = "";
}
return errorCodeToError(RemoveEC);
}
Error TempFile::keep(const Twine &Name) {
assert(!Done);
Done = true;
#ifdef _WIN32
auto H = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
std::error_code RenameEC =
RemoveOnClose ? std::error_code() : setDeleteDisposition(H, false);
bool ShouldDelete = false;
if (!RenameEC) {
RenameEC = rename_handle(H, Name);
if (RenameEC ==
std::error_code(ERROR_NOT_SAME_DEVICE, std::system_category())) {
RenameEC = copy_file(TmpName, Name);
ShouldDelete = true;
}
}
if (RenameEC)
ShouldDelete = true;
if (ShouldDelete) {
if (!RemoveOnClose)
setDeleteDisposition(H, true);
else
remove(TmpName);
}
#else
std::error_code RenameEC = fs::rename(TmpName, Name);
if (RenameEC) {
RenameEC = sys::fs::copy_file(TmpName, Name);
if (RenameEC)
remove(TmpName);
}
#endif
sys::DontRemoveFileOnSignal(TmpName);
if (!RenameEC)
TmpName = "";
if (close(FD) == -1) {
std::error_code EC(errno, std::generic_category());
return errorCodeToError(EC);
}
FD = -1;
return errorCodeToError(RenameEC);
}
Error TempFile::keep() {
assert(!Done);
Done = true;
#ifdef _WIN32
auto H = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
if (std::error_code EC = setDeleteDisposition(H, false))
return errorCodeToError(EC);
#endif
sys::DontRemoveFileOnSignal(TmpName);
TmpName = "";
if (close(FD) == -1) {
std::error_code EC(errno, std::generic_category());
return errorCodeToError(EC);
}
FD = -1;
return Error::success();
}
Expected<TempFile> TempFile::create(const Twine &Model, unsigned Mode,
OpenFlags ExtraFlags) {
int FD;
SmallString<128> ResultPath;
if (std::error_code EC =
createUniqueFile(Model, FD, ResultPath, OF_Delete | ExtraFlags, Mode))
return errorCodeToError(EC);
TempFile Ret(ResultPath, FD);
#ifdef _WIN32
auto H = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
bool SetSignalHandler = false;
if (std::error_code EC = setDeleteDisposition(H, true)) {
Ret.RemoveOnClose = true;
SetSignalHandler = true;
}
#else
bool SetSignalHandler = true;
#endif
if (SetSignalHandler && sys::RemoveFileOnSignal(ResultPath)) {
consumeError(Ret.discard());
std::error_code EC(errc::operation_not_permitted);
return errorCodeToError(EC);
}
return std::move(Ret);
}
}
} }