2013-06-27 03:33:03 +08:00
|
|
|
//===-- Path.cpp - Implement OS Path Concept ------------------------------===//
|
2010-11-30 06:28:51 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2013-06-27 03:33:03 +08:00
|
|
|
// This file implements the operating system Path API.
|
2010-11-30 06:28:51 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-03-21 07:33:18 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2014-09-12 06:34:32 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
2014-06-14 01:20:48 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2010-11-30 06:28:51 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2014-02-11 11:40:14 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2017-11-14 02:33:44 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2010-11-30 06:28:51 +08:00
|
|
|
#include <cctype>
|
2010-12-28 09:49:01 +08:00
|
|
|
#include <cstring>
|
2013-06-19 01:01:00 +08:00
|
|
|
|
|
|
|
#if !defined(_MSC_VER) && !defined(__MINGW32__)
|
2013-03-22 05:46:10 +08:00
|
|
|
#include <unistd.h>
|
2013-06-19 01:01:00 +08:00
|
|
|
#else
|
|
|
|
#include <io.h>
|
2013-03-22 05:46:10 +08:00
|
|
|
#endif
|
2010-11-30 06:28:51 +08:00
|
|
|
|
2014-02-24 11:07:41 +08:00
|
|
|
using namespace llvm;
|
2015-03-03 05:19:12 +08:00
|
|
|
using namespace llvm::support::endian;
|
2014-02-24 11:07:41 +08:00
|
|
|
|
2010-11-30 06:28:51 +08:00
|
|
|
namespace {
|
|
|
|
using llvm::StringRef;
|
2011-02-12 05:24:40 +08:00
|
|
|
using llvm::sys::path::is_separator;
|
2017-03-17 06:28:04 +08:00
|
|
|
using llvm::sys::path::Style;
|
2010-11-30 06:28:51 +08:00
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
inline Style real_style(Style style) {
|
2010-11-30 06:28:51 +08:00
|
|
|
#ifdef LLVM_ON_WIN32
|
2017-03-17 06:28:04 +08:00
|
|
|
return (style == Style::posix) ? Style::posix : Style::windows;
|
2010-11-30 06:28:51 +08:00
|
|
|
#else
|
2017-03-17 06:28:04 +08:00
|
|
|
return (style == Style::windows) ? Style::windows : Style::posix;
|
2010-11-30 06:28:51 +08:00
|
|
|
#endif
|
2017-03-17 06:28:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline const char *separators(Style style) {
|
|
|
|
if (real_style(style) == Style::windows)
|
|
|
|
return "\\/";
|
|
|
|
return "/";
|
|
|
|
}
|
2010-11-30 06:28:51 +08:00
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
inline char preferred_separator(Style style) {
|
|
|
|
if (real_style(style) == Style::windows)
|
|
|
|
return '\\';
|
|
|
|
return '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef find_first_component(StringRef path, Style style) {
|
2010-11-30 06:28:51 +08: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;
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
if (real_style(style) == Style::windows) {
|
|
|
|
// C:
|
|
|
|
if (path.size() >= 2 &&
|
|
|
|
std::isalpha(static_cast<unsigned char>(path[0])) && path[1] == ':')
|
|
|
|
return path.substr(0, 2);
|
|
|
|
}
|
2010-11-30 06:28:51 +08:00
|
|
|
|
|
|
|
// //net
|
2017-03-17 06:28:04 +08:00
|
|
|
if ((path.size() > 2) && is_separator(path[0], style) &&
|
|
|
|
path[0] == path[1] && !is_separator(path[2], style)) {
|
2010-11-30 06:28:51 +08:00
|
|
|
// Find the next directory separator.
|
2017-03-17 06:28:04 +08:00
|
|
|
size_t end = path.find_first_of(separators(style), 2);
|
2010-12-18 04:27:37 +08:00
|
|
|
return path.substr(0, end);
|
2010-11-30 06:28:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// {/,\}
|
2017-03-17 06:28:04 +08:00
|
|
|
if (is_separator(path[0], style))
|
2010-12-18 04:27:37 +08:00
|
|
|
return path.substr(0, 1);
|
2010-11-30 06:28:51 +08:00
|
|
|
|
|
|
|
// * {file,directory}name
|
2017-03-17 06:28:04 +08:00
|
|
|
size_t end = path.find_first_of(separators(style));
|
2010-12-18 04:27:37 +08:00
|
|
|
return path.substr(0, end);
|
2010-11-30 06:28:51 +08:00
|
|
|
}
|
2010-12-01 07:28:07 +08:00
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
size_t filename_pos(StringRef str, Style style) {
|
|
|
|
if (str.size() == 2 && is_separator(str[0], style) && str[0] == str[1])
|
2010-12-01 07:28:07 +08:00
|
|
|
return 0;
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
if (str.size() > 0 && is_separator(str[str.size() - 1], style))
|
2010-12-01 07:28:07 +08:00
|
|
|
return str.size() - 1;
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
size_t pos = str.find_last_of(separators(style), str.size() - 1);
|
2010-12-01 07:28:07 +08:00
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
if (real_style(style) == Style::windows) {
|
|
|
|
if (pos == StringRef::npos)
|
|
|
|
pos = str.find_last_of(':', str.size() - 2);
|
|
|
|
}
|
2010-12-01 07:28:07 +08:00
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
if (pos == StringRef::npos || (pos == 1 && is_separator(str[0], style)))
|
2010-12-01 07:28:07 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return pos + 1;
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
size_t root_dir_start(StringRef str, Style style) {
|
2010-12-01 07:28:07 +08:00
|
|
|
// case "c:/"
|
2017-03-17 06:28:04 +08:00
|
|
|
if (real_style(style) == Style::windows) {
|
|
|
|
if (str.size() > 2 && str[1] == ':' && is_separator(str[2], style))
|
|
|
|
return 2;
|
|
|
|
}
|
2010-12-01 07:28:07 +08:00
|
|
|
|
|
|
|
// case "//"
|
2017-03-17 06:28:04 +08:00
|
|
|
if (str.size() == 2 && is_separator(str[0], style) && str[0] == str[1])
|
2010-12-01 07:28:07 +08:00
|
|
|
return StringRef::npos;
|
|
|
|
|
|
|
|
// case "//net"
|
2017-03-17 06:28:04 +08:00
|
|
|
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);
|
2010-12-01 07:28:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// case "/"
|
2017-03-17 06:28:04 +08:00
|
|
|
if (str.size() > 0 && is_separator(str[0], style))
|
2010-12-01 07:28:07 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return StringRef::npos;
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
size_t parent_path_end(StringRef path, Style style) {
|
|
|
|
size_t end_pos = filename_pos(path, style);
|
2010-12-01 07:28:07 +08:00
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
bool filename_was_sep =
|
|
|
|
path.size() > 0 && is_separator(path[end_pos], style);
|
2010-12-01 07:28:07 +08:00
|
|
|
|
|
|
|
// Skip separators except for root dir.
|
2017-03-17 06:28:04 +08:00
|
|
|
size_t root_dir_pos = root_dir_start(path.substr(0, end_pos), style);
|
2010-12-01 07:28:07 +08:00
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
|
|
|
|
is_separator(path[end_pos - 1], style))
|
2010-12-01 07:28:07 +08:00
|
|
|
--end_pos;
|
|
|
|
|
|
|
|
if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
|
|
|
|
return StringRef::npos;
|
|
|
|
|
|
|
|
return end_pos;
|
|
|
|
}
|
2011-02-12 05:24:40 +08:00
|
|
|
} // end unnamed namespace
|
2010-11-30 06:28:51 +08:00
|
|
|
|
2013-06-28 11:48:47 +08:00
|
|
|
enum FSEntity {
|
|
|
|
FS_Dir,
|
|
|
|
FS_File,
|
|
|
|
FS_Name
|
|
|
|
};
|
|
|
|
|
2017-11-28 07:44:11 +08:00
|
|
|
static std::error_code
|
|
|
|
createUniqueEntity(const Twine &Model, int &ResultFD,
|
|
|
|
SmallVectorImpl<char> &ResultPath, bool MakeAbsolute,
|
|
|
|
unsigned Mode, FSEntity Type,
|
|
|
|
sys::fs::OpenFlags Flags = sys::fs::F_None) {
|
2014-02-24 11:07:41 +08:00
|
|
|
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;
|
2014-08-26 22:47:52 +08:00
|
|
|
sys::path::system_temp_directory(true, TDir);
|
2014-02-24 11:07:41 +08:00
|
|
|
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-06-13 10:24:39 +08:00
|
|
|
if (std::error_code EC =
|
2014-02-25 02:20:12 +08:00
|
|
|
sys::fs::openFileForWrite(Twine(ResultPath.begin()), ResultFD,
|
2017-11-28 07:44:11 +08:00
|
|
|
Flags | sys::fs::F_Excl, Mode)) {
|
2014-06-14 01:20:48 +08:00
|
|
|
if (EC == errc::file_exists)
|
2014-02-24 11:07:41 +08:00
|
|
|
goto retry_random_path;
|
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
return std::error_code();
|
2014-02-24 11:07:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case FS_Name: {
|
2014-09-12 04:30:02 +08:00
|
|
|
std::error_code EC =
|
|
|
|
sys::fs::access(ResultPath.begin(), sys::fs::AccessMode::Exist);
|
|
|
|
if (EC == errc::no_such_file_or_directory)
|
|
|
|
return std::error_code();
|
2014-02-24 11:07:41 +08:00
|
|
|
if (EC)
|
|
|
|
return EC;
|
2014-09-12 04:30:02 +08:00
|
|
|
goto retry_random_path;
|
2014-02-24 11:07:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case FS_Dir: {
|
2014-06-13 10:24:39 +08:00
|
|
|
if (std::error_code EC =
|
|
|
|
sys::fs::create_directory(ResultPath.begin(), false)) {
|
2014-06-14 01:20:48 +08:00
|
|
|
if (EC == errc::file_exists)
|
2014-02-24 11:07:41 +08:00
|
|
|
goto retry_random_path;
|
|
|
|
return EC;
|
|
|
|
}
|
2014-06-13 10:24:39 +08:00
|
|
|
return std::error_code();
|
2014-02-24 11:07:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid Type");
|
|
|
|
}
|
2013-06-28 11:48:47 +08:00
|
|
|
|
2010-11-30 06:28:51 +08:00
|
|
|
namespace llvm {
|
|
|
|
namespace sys {
|
|
|
|
namespace path {
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
const_iterator begin(StringRef path, Style style) {
|
2010-11-30 06:28:51 +08:00
|
|
|
const_iterator i;
|
|
|
|
i.Path = path;
|
2017-03-17 06:28:04 +08:00
|
|
|
i.Component = find_first_component(path, style);
|
2010-11-30 06:28:51 +08:00
|
|
|
i.Position = 0;
|
2017-03-17 06:28:04 +08:00
|
|
|
i.S = style;
|
2010-11-30 06:28:51 +08:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2010-12-18 02:19:06 +08:00
|
|
|
const_iterator end(StringRef path) {
|
2010-11-30 06:28:51 +08: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.
|
2017-03-17 06:28:04 +08:00
|
|
|
bool was_net = Component.size() > 2 && is_separator(Component[0], S) &&
|
|
|
|
Component[1] == Component[0] && !is_separator(Component[2], S);
|
2010-11-30 06:28:51 +08:00
|
|
|
|
|
|
|
// Handle separators.
|
2017-03-17 06:28:04 +08:00
|
|
|
if (is_separator(Path[Position], S)) {
|
2010-11-30 06:28:51 +08:00
|
|
|
// Root dir.
|
2017-03-17 06:28:04 +08:00
|
|
|
if (was_net ||
|
2010-11-30 06:28:51 +08:00
|
|
|
// c:/
|
2017-03-17 06:28:04 +08:00
|
|
|
(real_style(S) == Style::windows && Component.endswith(":"))) {
|
2010-12-18 04:27:37 +08:00
|
|
|
Component = Path.substr(Position, 1);
|
2010-11-30 06:28:51 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip extra separators.
|
2017-03-17 06:28:04 +08:00
|
|
|
while (Position != Path.size() && is_separator(Path[Position], S)) {
|
2010-11-30 06:28:51 +08:00
|
|
|
++Position;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Treat trailing '/' as a '.'.
|
|
|
|
if (Position == Path.size()) {
|
|
|
|
--Position;
|
|
|
|
Component = ".";
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find next component.
|
2017-03-17 06:28:04 +08:00
|
|
|
size_t end_pos = Path.find_first_of(separators(S), Position);
|
2010-12-18 04:27:37 +08:00
|
|
|
Component = Path.slice(Position, end_pos);
|
2010-11-30 06:28:51 +08:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-08-05 01:36:41 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
reverse_iterator rbegin(StringRef Path, Style style) {
|
2014-08-05 01:36:41 +08:00
|
|
|
reverse_iterator I;
|
|
|
|
I.Path = Path;
|
|
|
|
I.Position = Path.size();
|
2017-03-17 06:28:04 +08:00
|
|
|
I.S = style;
|
2014-08-05 01:36:41 +08:00
|
|
|
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++() {
|
2014-03-06 03:56:30 +08:00
|
|
|
// If we're at the end and the previous char was a '/', return '.' unless
|
|
|
|
// we are the root path.
|
2017-03-17 06:28:04 +08:00
|
|
|
size_t root_dir_pos = root_dir_start(Path, S);
|
|
|
|
if (Position == Path.size() && Path.size() > root_dir_pos + 1 &&
|
|
|
|
is_separator(Path[Position - 1], S)) {
|
2010-12-01 07:28:07 +08:00
|
|
|
--Position;
|
|
|
|
Component = ".";
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip separators unless it's the root directory.
|
|
|
|
size_t end_pos = Position;
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
|
|
|
|
is_separator(Path[end_pos - 1], S))
|
2010-12-01 07:28:07 +08:00
|
|
|
--end_pos;
|
|
|
|
|
|
|
|
// Find next separator.
|
2017-03-17 06:28:04 +08:00
|
|
|
size_t start_pos = filename_pos(Path.substr(0, end_pos), S);
|
2010-12-18 04:27:37 +08:00
|
|
|
Component = Path.slice(start_pos, end_pos);
|
2010-12-01 07:28:07 +08:00
|
|
|
Position = start_pos;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-08-05 01:36:41 +08:00
|
|
|
bool reverse_iterator::operator==(const reverse_iterator &RHS) const {
|
|
|
|
return Path.begin() == RHS.Path.begin() && Component == RHS.Component &&
|
2010-11-30 06:28:51 +08:00
|
|
|
Position == RHS.Position;
|
|
|
|
}
|
|
|
|
|
2016-04-30 00:48:07 +08:00
|
|
|
ptrdiff_t reverse_iterator::operator-(const reverse_iterator &RHS) const {
|
|
|
|
return Position - RHS.Position;
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
StringRef root_path(StringRef path, Style style) {
|
|
|
|
const_iterator b = begin(path, style), pos = b, e = end(path);
|
2010-11-30 06:28:51 +08:00
|
|
|
if (b != e) {
|
2017-03-17 06:28:04 +08:00
|
|
|
bool has_net =
|
|
|
|
b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
|
|
|
|
bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
|
2010-11-30 06:28:51 +08:00
|
|
|
|
|
|
|
if (has_net || has_drive) {
|
2017-03-17 06:28:04 +08:00
|
|
|
if ((++pos != e) && is_separator((*pos)[0], style)) {
|
2010-11-30 06:28:51 +08:00
|
|
|
// {C:/,//net/}, so get the first two components.
|
2010-12-18 04:27:37 +08:00
|
|
|
return path.substr(0, b->size() + pos->size());
|
2010-11-30 06:28:51 +08:00
|
|
|
} else {
|
|
|
|
// just {C:,//net}, return the first component.
|
2010-12-08 01:04:04 +08:00
|
|
|
return *b;
|
2010-11-30 06:28:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// POSIX style root directory.
|
2017-03-17 06:28:04 +08:00
|
|
|
if (is_separator((*b)[0], style)) {
|
2010-12-08 01:04:04 +08:00
|
|
|
return *b;
|
2010-11-30 06:28:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-08 01:04:04 +08:00
|
|
|
return StringRef();
|
2010-11-30 06:28:51 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
StringRef root_name(StringRef path, Style style) {
|
|
|
|
const_iterator b = begin(path, style), e = end(path);
|
2010-11-30 06:28:51 +08:00
|
|
|
if (b != e) {
|
2017-03-17 06:28:04 +08:00
|
|
|
bool has_net =
|
|
|
|
b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
|
|
|
|
bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
|
2010-11-30 06:28:51 +08:00
|
|
|
|
|
|
|
if (has_net || has_drive) {
|
|
|
|
// just {C:,//net}, return the first component.
|
2010-12-08 01:04:04 +08:00
|
|
|
return *b;
|
2010-11-30 06:28:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No path or no name.
|
2010-12-08 01:04:04 +08:00
|
|
|
return StringRef();
|
2010-11-30 06:28:51 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
StringRef root_directory(StringRef path, Style style) {
|
|
|
|
const_iterator b = begin(path, style), pos = b, e = end(path);
|
2010-11-30 06:28:51 +08:00
|
|
|
if (b != e) {
|
2017-03-17 06:28:04 +08:00
|
|
|
bool has_net =
|
|
|
|
b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
|
|
|
|
bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
|
2010-11-30 06:28:51 +08:00
|
|
|
|
|
|
|
if ((has_net || has_drive) &&
|
|
|
|
// {C:,//net}, skip to the next component.
|
2017-03-17 06:28:04 +08:00
|
|
|
(++pos != e) && is_separator((*pos)[0], style)) {
|
2010-12-08 01:04:04 +08:00
|
|
|
return *pos;
|
2010-11-30 06:28:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// POSIX style root directory.
|
2017-03-17 06:28:04 +08:00
|
|
|
if (!has_net && is_separator((*b)[0], style)) {
|
2010-12-08 01:04:04 +08:00
|
|
|
return *b;
|
2010-11-30 06:28:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No path or no root.
|
2010-12-08 01:04:04 +08:00
|
|
|
return StringRef();
|
2010-11-30 06:28:51 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
StringRef relative_path(StringRef path, Style style) {
|
|
|
|
StringRef root = root_path(path, style);
|
2012-02-29 08:06:24 +08:00
|
|
|
return path.substr(root.size());
|
2010-11-30 06:28:51 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
void append(SmallVectorImpl<char> &path, Style style, const Twine &a,
|
|
|
|
const Twine &b, const Twine &c, const Twine &d) {
|
2010-11-30 06:28:51 +08: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));
|
|
|
|
|
2015-10-22 16:12:15 +08:00
|
|
|
for (auto &component : components) {
|
2017-03-17 06:28:04 +08:00
|
|
|
bool path_has_sep =
|
|
|
|
!path.empty() && is_separator(path[path.size() - 1], style);
|
2010-11-30 06:28:51 +08:00
|
|
|
if (path_has_sep) {
|
|
|
|
// Strip separators from beginning of component.
|
2017-03-17 06:28:04 +08:00
|
|
|
size_t loc = component.find_first_not_of(separators(style));
|
2015-10-22 16:12:15 +08:00
|
|
|
StringRef c = component.substr(loc);
|
2010-11-30 06:28:51 +08:00
|
|
|
|
|
|
|
// Append it.
|
|
|
|
path.append(c.begin(), c.end());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-08-10 06:06:32 +08:00
|
|
|
bool component_has_sep =
|
|
|
|
!component.empty() && is_separator(component[0], style);
|
|
|
|
if (!component_has_sep &&
|
|
|
|
!(path.empty() || has_root_name(component, style))) {
|
2010-11-30 06:28:51 +08:00
|
|
|
// Add a separator.
|
2017-03-17 06:28:04 +08:00
|
|
|
path.push_back(preferred_separator(style));
|
2010-11-30 06:28:51 +08:00
|
|
|
}
|
|
|
|
|
2015-10-22 16:12:15 +08:00
|
|
|
path.append(component.begin(), component.end());
|
2010-11-30 06:28:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
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) {
|
2011-02-16 01:51:19 +08:00
|
|
|
for (; begin != end; ++begin)
|
2017-03-17 06:28:04 +08:00
|
|
|
path::append(path, style, *begin);
|
2011-02-16 01:51:19 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
StringRef parent_path(StringRef path, Style style) {
|
|
|
|
size_t end_pos = parent_path_end(path, style);
|
2010-12-01 07:28:07 +08:00
|
|
|
if (end_pos == StringRef::npos)
|
2010-12-08 01:04:04 +08:00
|
|
|
return StringRef();
|
2010-12-01 07:28:07 +08:00
|
|
|
else
|
2010-12-18 04:27:37 +08:00
|
|
|
return path.substr(0, end_pos);
|
2010-12-01 07:28:07 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
void remove_filename(SmallVectorImpl<char> &path, Style style) {
|
|
|
|
size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()), style);
|
2010-12-07 11:57:37 +08:00
|
|
|
if (end_pos != StringRef::npos)
|
|
|
|
path.set_size(end_pos);
|
2010-12-01 08:52:28 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
void replace_extension(SmallVectorImpl<char> &path, const Twine &extension,
|
|
|
|
Style style) {
|
2010-12-01 08:52:55 +08: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('.');
|
2017-03-17 06:28:04 +08:00
|
|
|
if (pos != StringRef::npos && pos >= filename_pos(p, style))
|
2010-12-01 08:52:55 +08:00
|
|
|
path.set_size(pos);
|
|
|
|
|
|
|
|
// Append '.' if needed.
|
|
|
|
if (ext.size() > 0 && ext[0] != '.')
|
|
|
|
path.push_back('.');
|
|
|
|
|
|
|
|
// Append extension.
|
|
|
|
path.append(ext.begin(), ext.end());
|
|
|
|
}
|
|
|
|
|
2016-05-17 22:45:30 +08:00
|
|
|
void replace_path_prefix(SmallVectorImpl<char> &Path,
|
2017-03-17 06:28:04 +08:00
|
|
|
const StringRef &OldPrefix, const StringRef &NewPrefix,
|
|
|
|
Style style) {
|
2016-05-17 22:45:30 +08:00
|
|
|
if (OldPrefix.empty() && NewPrefix.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
StringRef OrigPath(Path.begin(), Path.size());
|
|
|
|
if (!OrigPath.startswith(OldPrefix))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If prefixes have the same size we can simply copy the new one over.
|
|
|
|
if (OldPrefix.size() == NewPrefix.size()) {
|
|
|
|
std::copy(NewPrefix.begin(), NewPrefix.end(), Path.begin());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef RelPath = OrigPath.substr(OldPrefix.size());
|
|
|
|
SmallString<256> NewPath;
|
2017-03-17 06:28:04 +08:00
|
|
|
path::append(NewPath, style, NewPrefix);
|
|
|
|
path::append(NewPath, style, RelPath);
|
2016-05-17 22:45:30 +08:00
|
|
|
Path.swap(NewPath);
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
void native(const Twine &path, SmallVectorImpl<char> &result, Style style) {
|
2013-09-11 18:45:21 +08:00
|
|
|
assert((!path.isSingleStringRef() ||
|
|
|
|
path.getSingleStringRef().data() != result.data()) &&
|
|
|
|
"path and result are not allowed to overlap!");
|
2010-12-01 10:48:27 +08:00
|
|
|
// Clear result.
|
2010-12-07 09:23:19 +08:00
|
|
|
result.clear();
|
2010-12-01 10:48:27 +08:00
|
|
|
path.toVector(result);
|
2017-03-17 06:28:04 +08:00
|
|
|
native(result, style);
|
2013-09-11 18:45:21 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
void native(SmallVectorImpl<char> &Path, Style style) {
|
2017-03-01 17:38:15 +08:00
|
|
|
if (Path.empty())
|
|
|
|
return;
|
2017-03-17 06:28:04 +08:00
|
|
|
if (real_style(style) == Style::windows) {
|
|
|
|
std::replace(Path.begin(), Path.end(), '/', '\\');
|
|
|
|
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 {
|
|
|
|
for (auto PI = Path.begin(), PE = Path.end(); PI < PE; ++PI) {
|
|
|
|
if (*PI == '\\') {
|
|
|
|
auto PN = PI + 1;
|
|
|
|
if (PN < PE && *PN == '\\')
|
|
|
|
++PI; // increment once, the for loop will move over the escaped slash
|
|
|
|
else
|
|
|
|
*PI = '/';
|
|
|
|
}
|
2014-08-09 05:29:34 +08:00
|
|
|
}
|
|
|
|
}
|
2010-12-01 10:48:27 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
std::string convert_to_slash(StringRef path, Style style) {
|
|
|
|
if (real_style(style) != Style::windows)
|
|
|
|
return path;
|
|
|
|
|
2017-01-09 09:47:15 +08:00
|
|
|
std::string s = path.str();
|
|
|
|
std::replace(s.begin(), s.end(), '\\', '/');
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
StringRef filename(StringRef path, Style style) { return *rbegin(path, style); }
|
2010-12-01 11:18:17 +08:00
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
StringRef stem(StringRef path, Style style) {
|
|
|
|
StringRef fname = filename(path, style);
|
2010-12-01 11:18:33 +08:00
|
|
|
size_t pos = fname.find_last_of('.');
|
|
|
|
if (pos == StringRef::npos)
|
2010-12-08 01:04:04 +08:00
|
|
|
return fname;
|
2010-12-01 11:18:33 +08:00
|
|
|
else
|
|
|
|
if ((fname.size() == 1 && fname == ".") ||
|
|
|
|
(fname.size() == 2 && fname == ".."))
|
2010-12-08 01:04:04 +08:00
|
|
|
return fname;
|
2010-12-01 11:18:33 +08:00
|
|
|
else
|
2010-12-18 04:27:37 +08:00
|
|
|
return fname.substr(0, pos);
|
2010-12-01 11:18:33 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
StringRef extension(StringRef path, Style style) {
|
|
|
|
StringRef fname = filename(path, style);
|
2010-12-01 11:37:41 +08:00
|
|
|
size_t pos = fname.find_last_of('.');
|
|
|
|
if (pos == StringRef::npos)
|
2010-12-08 01:04:04 +08:00
|
|
|
return StringRef();
|
2010-12-01 11:37:41 +08:00
|
|
|
else
|
|
|
|
if ((fname.size() == 1 && fname == ".") ||
|
|
|
|
(fname.size() == 2 && fname == ".."))
|
2010-12-08 01:04:04 +08:00
|
|
|
return StringRef();
|
2010-12-01 11:37:41 +08:00
|
|
|
else
|
2010-12-18 04:27:37 +08:00
|
|
|
return fname.substr(pos);
|
2010-12-01 11:37:41 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
bool is_separator(char value, Style style) {
|
|
|
|
if (value == '/')
|
|
|
|
return true;
|
|
|
|
if (real_style(style) == Style::windows)
|
|
|
|
return value == '\\';
|
|
|
|
return false;
|
2011-02-12 05:24:40 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
StringRef get_separator(Style style) {
|
|
|
|
if (real_style(style) == Style::windows)
|
|
|
|
return "\\";
|
|
|
|
return "/";
|
2014-05-16 21:16:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
bool has_root_name(const Twine &path, Style style) {
|
2010-12-01 14:03:50 +08:00
|
|
|
SmallString<128> path_storage;
|
|
|
|
StringRef p = path.toStringRef(path_storage);
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
return !root_name(p, style).empty();
|
2010-12-01 14:03:50 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
bool has_root_directory(const Twine &path, Style style) {
|
2010-12-01 14:03:50 +08:00
|
|
|
SmallString<128> path_storage;
|
|
|
|
StringRef p = path.toStringRef(path_storage);
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
return !root_directory(p, style).empty();
|
2010-12-01 14:03:50 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
bool has_root_path(const Twine &path, Style style) {
|
2010-12-01 14:03:50 +08:00
|
|
|
SmallString<128> path_storage;
|
|
|
|
StringRef p = path.toStringRef(path_storage);
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
return !root_path(p, style).empty();
|
2010-12-01 14:03:50 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
bool has_relative_path(const Twine &path, Style style) {
|
2010-12-20 21:30:28 +08:00
|
|
|
SmallString<128> path_storage;
|
|
|
|
StringRef p = path.toStringRef(path_storage);
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
return !relative_path(p, style).empty();
|
2010-12-20 21:30:28 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
bool has_filename(const Twine &path, Style style) {
|
2010-12-01 14:03:50 +08:00
|
|
|
SmallString<128> path_storage;
|
|
|
|
StringRef p = path.toStringRef(path_storage);
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
return !filename(p, style).empty();
|
2010-12-01 14:03:50 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
bool has_parent_path(const Twine &path, Style style) {
|
2010-12-01 14:03:50 +08:00
|
|
|
SmallString<128> path_storage;
|
|
|
|
StringRef p = path.toStringRef(path_storage);
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
return !parent_path(p, style).empty();
|
2010-12-01 14:03:50 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
bool has_stem(const Twine &path, Style style) {
|
2010-12-01 14:03:50 +08:00
|
|
|
SmallString<128> path_storage;
|
|
|
|
StringRef p = path.toStringRef(path_storage);
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
return !stem(p, style).empty();
|
2010-12-01 14:03:50 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
bool has_extension(const Twine &path, Style style) {
|
2010-12-01 14:03:50 +08:00
|
|
|
SmallString<128> path_storage;
|
|
|
|
StringRef p = path.toStringRef(path_storage);
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
return !extension(p, style).empty();
|
2010-12-01 14:03:50 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
bool is_absolute(const Twine &path, Style style) {
|
2010-12-01 14:21:53 +08:00
|
|
|
SmallString<128> path_storage;
|
|
|
|
StringRef p = path.toStringRef(path_storage);
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
bool rootDir = has_root_directory(p, style);
|
|
|
|
bool rootName =
|
|
|
|
(real_style(style) != Style::windows) || has_root_name(p, style);
|
2010-12-01 14:21:53 +08:00
|
|
|
|
2010-12-08 01:04:04 +08:00
|
|
|
return rootDir && rootName;
|
2010-12-01 14:21:53 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
bool is_relative(const Twine &path, Style style) {
|
|
|
|
return !is_absolute(path, style);
|
|
|
|
}
|
2015-09-03 05:02:10 +08:00
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
StringRef remove_leading_dotslash(StringRef Path, Style style) {
|
2015-09-03 05:02:10 +08:00
|
|
|
// Remove leading "./" (or ".//" or "././" etc.)
|
2017-03-17 06:28:04 +08:00
|
|
|
while (Path.size() > 2 && Path[0] == '.' && is_separator(Path[1], style)) {
|
2015-09-03 05:02:10 +08:00
|
|
|
Path = Path.substr(2);
|
2017-03-17 06:28:04 +08:00
|
|
|
while (Path.size() > 0 && is_separator(Path[0], style))
|
2015-09-03 05:02:10 +08:00
|
|
|
Path = Path.substr(1);
|
|
|
|
}
|
|
|
|
return Path;
|
2010-12-01 14:21:53 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
static SmallString<256> remove_dots(StringRef path, bool remove_dot_dot,
|
|
|
|
Style style) {
|
2015-11-10 02:56:31 +08:00
|
|
|
SmallVector<StringRef, 16> components;
|
|
|
|
|
|
|
|
// Skip the root path, then look for traversal in the components.
|
2017-03-17 06:28:04 +08:00
|
|
|
StringRef rel = path::relative_path(path, style);
|
|
|
|
for (StringRef C :
|
|
|
|
llvm::make_range(path::begin(rel, style), path::end(rel))) {
|
2015-11-10 02:56:31 +08:00
|
|
|
if (C == ".")
|
|
|
|
continue;
|
2016-10-17 21:28:21 +08:00
|
|
|
// Leading ".." will remain in the path unless it's at the root.
|
|
|
|
if (remove_dot_dot && C == "..") {
|
|
|
|
if (!components.empty() && components.back() != "..") {
|
|
|
|
components.pop_back();
|
|
|
|
continue;
|
|
|
|
}
|
2017-03-17 06:28:04 +08:00
|
|
|
if (path::is_absolute(path, style))
|
2016-10-17 21:28:21 +08:00
|
|
|
continue;
|
2015-11-10 02:56:31 +08:00
|
|
|
}
|
|
|
|
components.push_back(C);
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
SmallString<256> buffer = path::root_path(path, style);
|
2015-11-10 02:56:31 +08:00
|
|
|
for (StringRef C : components)
|
2017-03-17 06:28:04 +08:00
|
|
|
path::append(buffer, style, C);
|
2015-11-10 02:56:31 +08:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot,
|
|
|
|
Style style) {
|
2015-11-10 02:56:31 +08:00
|
|
|
StringRef p(path.data(), path.size());
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
SmallString<256> result = remove_dots(p, remove_dot_dot, style);
|
2015-11-10 02:56:31 +08:00
|
|
|
if (result == path)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
path.swap(result);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-12-02 03:32:01 +08:00
|
|
|
} // end namespace path
|
|
|
|
|
|
|
|
namespace fs {
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code getUniqueID(const Twine Path, UniqueID &Result) {
|
2013-07-30 05:26:49 +08:00
|
|
|
file_status Status;
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code EC = status(Path, Status);
|
2013-07-30 05:26:49 +08:00
|
|
|
if (EC)
|
|
|
|
return EC;
|
|
|
|
Result = Status.getUniqueID();
|
2014-06-13 10:24:39 +08:00
|
|
|
return std::error_code();
|
2013-07-30 05:26:49 +08:00
|
|
|
}
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code createUniqueFile(const Twine &Model, int &ResultFd,
|
|
|
|
SmallVectorImpl<char> &ResultPath,
|
2017-11-28 07:44:11 +08:00
|
|
|
unsigned Mode, sys::fs::OpenFlags Flags) {
|
|
|
|
return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File,
|
|
|
|
Flags);
|
2013-07-06 05:01:08 +08:00
|
|
|
}
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code createUniqueFile(const Twine &Model,
|
|
|
|
SmallVectorImpl<char> &ResultPath) {
|
2013-07-06 05:01:08 +08:00
|
|
|
int Dummy;
|
|
|
|
return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
|
|
|
|
}
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
static std::error_code
|
|
|
|
createTemporaryFile(const Twine &Model, int &ResultFD,
|
2017-11-28 07:44:11 +08:00
|
|
|
llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type,
|
|
|
|
sys::fs::OpenFlags Flags) {
|
2013-07-06 03:56:49 +08:00
|
|
|
SmallString<128> Storage;
|
|
|
|
StringRef P = Model.toNullTerminatedStringRef(Storage);
|
2017-03-17 06:28:04 +08:00
|
|
|
assert(P.find_first_of(separators(Style::native)) == StringRef::npos &&
|
2013-07-06 03:56:49 +08:00
|
|
|
"Model must be a simple filename.");
|
|
|
|
// Use P.begin() so that createUniqueEntity doesn't need to recreate Storage.
|
2017-11-28 07:44:11 +08:00
|
|
|
return createUniqueEntity(P.begin(), ResultFD, ResultPath, true,
|
|
|
|
owner_read | owner_write, Type, Flags);
|
2013-07-06 03:56:49 +08:00
|
|
|
}
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
static std::error_code
|
2013-07-06 03:56:49 +08:00
|
|
|
createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
|
2017-11-28 07:44:11 +08:00
|
|
|
llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type,
|
|
|
|
sys::fs::OpenFlags Flags = sys::fs::F_None) {
|
2013-07-25 23:00:17 +08:00
|
|
|
const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
|
|
|
|
return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
|
2017-11-28 07:44:11 +08:00
|
|
|
Type, Flags);
|
2013-07-06 03:56:49 +08:00
|
|
|
}
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
|
|
|
|
int &ResultFD,
|
2017-11-28 07:44:11 +08:00
|
|
|
SmallVectorImpl<char> &ResultPath,
|
|
|
|
sys::fs::OpenFlags Flags) {
|
|
|
|
return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File,
|
|
|
|
Flags);
|
2013-07-06 03:56:49 +08:00
|
|
|
}
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
|
|
|
|
SmallVectorImpl<char> &ResultPath) {
|
2013-07-06 03:56:49 +08:00
|
|
|
int Dummy;
|
|
|
|
return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-28 11:48:47 +08:00
|
|
|
// This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
|
2013-06-28 18:55:41 +08:00
|
|
|
// for consistency. We should try using mkdtemp.
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code createUniqueDirectory(const Twine &Prefix,
|
|
|
|
SmallVectorImpl<char> &ResultPath) {
|
2013-06-28 11:48:47 +08:00
|
|
|
int Dummy;
|
|
|
|
return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath,
|
|
|
|
true, 0, FS_Dir);
|
2013-06-27 11:45:31 +08:00
|
|
|
}
|
|
|
|
|
2015-10-05 21:02:43 +08:00
|
|
|
static std::error_code make_absolute(const Twine ¤t_directory,
|
|
|
|
SmallVectorImpl<char> &path,
|
|
|
|
bool use_current_directory) {
|
2010-12-07 11:57:17 +08:00
|
|
|
StringRef p(path.data(), path.size());
|
|
|
|
|
2017-03-17 06:28:04 +08:00
|
|
|
bool rootDirectory = path::has_root_directory(p);
|
|
|
|
bool rootName =
|
|
|
|
(real_style(Style::native) != Style::windows) || path::has_root_name(p);
|
2010-12-07 11:57:17 +08:00
|
|
|
|
|
|
|
// Already absolute.
|
|
|
|
if (rootName && rootDirectory)
|
2014-06-13 10:24:39 +08:00
|
|
|
return std::error_code();
|
2010-12-07 11:57:17 +08:00
|
|
|
|
|
|
|
// All of the following conditions will need the current directory.
|
|
|
|
SmallString<128> current_dir;
|
2015-10-05 21:02:43 +08:00
|
|
|
if (use_current_directory)
|
|
|
|
current_directory.toVector(current_dir);
|
|
|
|
else if (std::error_code ec = current_path(current_dir))
|
2014-06-13 10:24:39 +08:00
|
|
|
return ec;
|
2010-12-07 11:57:17 +08:00
|
|
|
|
|
|
|
// Relative path. Prepend the current directory.
|
|
|
|
if (!rootName && !rootDirectory) {
|
|
|
|
// Append path to the current directory.
|
2010-12-07 11:57:37 +08:00
|
|
|
path::append(current_dir, p);
|
2010-12-07 11:57:17 +08:00
|
|
|
// Set path to the result.
|
|
|
|
path.swap(current_dir);
|
2014-06-13 10:24:39 +08:00
|
|
|
return std::error_code();
|
2010-12-07 11:57:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!rootName && rootDirectory) {
|
2010-12-08 01:04:04 +08:00
|
|
|
StringRef cdrn = path::root_name(current_dir);
|
2010-12-07 11:57:17 +08:00
|
|
|
SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
|
2010-12-07 11:57:37 +08:00
|
|
|
path::append(curDirRootName, p);
|
2010-12-07 11:57:17 +08:00
|
|
|
// Set path to the result.
|
|
|
|
path.swap(curDirRootName);
|
2014-06-13 10:24:39 +08:00
|
|
|
return std::error_code();
|
2010-12-07 11:57:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rootName && !rootDirectory) {
|
2010-12-08 01:04:04 +08: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 11:57:17 +08:00
|
|
|
|
|
|
|
SmallString<128> res;
|
2010-12-07 11:57:37 +08:00
|
|
|
path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
|
2010-12-07 11:57:17 +08:00
|
|
|
path.swap(res);
|
2014-06-13 10:24:39 +08:00
|
|
|
return std::error_code();
|
2010-12-07 11:57:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("All rootName and rootDirectory combinations should have "
|
|
|
|
"occurred above!");
|
|
|
|
}
|
|
|
|
|
2015-10-05 21:02:43 +08:00
|
|
|
std::error_code make_absolute(const Twine ¤t_directory,
|
|
|
|
SmallVectorImpl<char> &path) {
|
|
|
|
return make_absolute(current_directory, path, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code make_absolute(SmallVectorImpl<char> &path) {
|
|
|
|
return make_absolute(Twine(), path, false);
|
|
|
|
}
|
|
|
|
|
2015-08-07 05:04:55 +08:00
|
|
|
std::error_code create_directories(const Twine &Path, bool IgnoreExisting,
|
|
|
|
perms Perms) {
|
2014-02-14 00:58:19 +08:00
|
|
|
SmallString<128> PathStorage;
|
|
|
|
StringRef P = Path.toStringRef(PathStorage);
|
|
|
|
|
|
|
|
// Be optimistic and try to create the directory
|
2015-08-07 05:04:55 +08:00
|
|
|
std::error_code EC = create_directory(P, IgnoreExisting, Perms);
|
2014-02-14 00:58:19 +08:00
|
|
|
// If we succeeded, or had any error other than the parent not existing, just
|
|
|
|
// return it.
|
2014-06-14 01:20:48 +08:00
|
|
|
if (EC != errc::no_such_file_or_directory)
|
2014-02-14 00:58:19 +08:00
|
|
|
return EC;
|
2010-12-03 13:42:11 +08:00
|
|
|
|
2014-02-14 00:58:19 +08: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-08 01:04:04 +08:00
|
|
|
|
2015-08-07 05:04:55 +08:00
|
|
|
if ((EC = create_directories(Parent, IgnoreExisting, Perms)))
|
2014-02-14 00:58:19 +08:00
|
|
|
return EC;
|
2010-12-03 13:42:11 +08:00
|
|
|
|
2015-08-07 05:04:55 +08:00
|
|
|
return create_directory(P, IgnoreExisting, Perms);
|
2010-12-03 13:42:11 +08:00
|
|
|
}
|
|
|
|
|
2014-06-20 03:35:39 +08:00
|
|
|
std::error_code copy_file(const Twine &From, const Twine &To) {
|
|
|
|
int ReadFD, WriteFD;
|
|
|
|
if (std::error_code EC = openFileForRead(From, ReadFD))
|
|
|
|
return EC;
|
|
|
|
if (std::error_code EC = openFileForWrite(To, WriteFD, F_None)) {
|
|
|
|
close(ReadFD);
|
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t BufSize = 4096;
|
2014-08-26 10:03:30 +08:00
|
|
|
char *Buf = new char[BufSize];
|
2014-06-20 03:35:39 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
close(ReadFD);
|
|
|
|
close(WriteFD);
|
2014-08-26 10:03:30 +08:00
|
|
|
delete[] Buf;
|
2014-06-20 03:35:39 +08:00
|
|
|
|
|
|
|
if (BytesRead < 0 || BytesWritten < 0)
|
|
|
|
return std::error_code(errno, std::generic_category());
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
2017-03-21 07:33:18 +08:00
|
|
|
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))
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
auto Result = md5_contents(FD);
|
|
|
|
close(FD);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2017-10-11 06:19:46 +08:00
|
|
|
bool exists(const basic_file_status &status) {
|
2010-12-10 01:37:02 +08:00
|
|
|
return status_known(status) && status.type() != file_type::file_not_found;
|
|
|
|
}
|
|
|
|
|
2017-10-11 06:19:46 +08:00
|
|
|
bool status_known(const basic_file_status &s) {
|
2010-12-10 01:37:02 +08:00
|
|
|
return s.type() != file_type::status_error;
|
|
|
|
}
|
|
|
|
|
2017-03-08 00:10:10 +08:00
|
|
|
file_type get_file_type(const Twine &Path, bool Follow) {
|
2017-03-07 11:43:17 +08:00
|
|
|
file_status st;
|
2017-03-08 00:10:10 +08:00
|
|
|
if (status(Path, st, Follow))
|
2017-03-07 11:43:17 +08:00
|
|
|
return file_type::status_error;
|
|
|
|
return st.type();
|
|
|
|
}
|
|
|
|
|
2017-10-11 06:19:46 +08:00
|
|
|
bool is_directory(const basic_file_status &status) {
|
2010-12-10 01:37:02 +08:00
|
|
|
return status.type() == file_type::directory_file;
|
|
|
|
}
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code is_directory(const Twine &path, bool &result) {
|
2011-01-11 09:21:55 +08:00
|
|
|
file_status st;
|
2014-06-13 10:24:39 +08:00
|
|
|
if (std::error_code ec = status(path, st))
|
2011-01-11 09:21:55 +08:00
|
|
|
return ec;
|
|
|
|
result = is_directory(st);
|
2014-06-13 10:24:39 +08:00
|
|
|
return std::error_code();
|
2011-01-11 09:21:55 +08:00
|
|
|
}
|
|
|
|
|
2017-10-11 06:19:46 +08:00
|
|
|
bool is_regular_file(const basic_file_status &status) {
|
2010-12-10 01:37:02 +08:00
|
|
|
return status.type() == file_type::regular_file;
|
|
|
|
}
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code is_regular_file(const Twine &path, bool &result) {
|
2011-01-11 09:21:55 +08:00
|
|
|
file_status st;
|
2014-06-13 10:24:39 +08:00
|
|
|
if (std::error_code ec = status(path, st))
|
2011-01-11 09:21:55 +08:00
|
|
|
return ec;
|
|
|
|
result = is_regular_file(st);
|
2014-06-13 10:24:39 +08:00
|
|
|
return std::error_code();
|
2011-01-11 09:21:55 +08:00
|
|
|
}
|
|
|
|
|
2017-10-11 06:19:46 +08:00
|
|
|
bool is_symlink_file(const basic_file_status &status) {
|
2017-03-09 01:56:08 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-10-11 06:19:46 +08:00
|
|
|
bool is_other(const basic_file_status &status) {
|
2010-12-10 01:37:02 +08:00
|
|
|
return exists(status) &&
|
|
|
|
!is_regular_file(status) &&
|
2014-03-21 01:39:04 +08:00
|
|
|
!is_directory(status);
|
2010-12-10 01:37:02 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 02:19:47 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-10-11 06:19:46 +08:00
|
|
|
void directory_entry::replace_filename(const Twine &filename,
|
|
|
|
basic_file_status st) {
|
2015-07-16 05:24:07 +08:00
|
|
|
SmallString<128> path = path::parent_path(Path);
|
2010-12-06 12:28:42 +08:00
|
|
|
path::append(path, filename);
|
|
|
|
Path = path.str();
|
|
|
|
Status = st;
|
|
|
|
}
|
|
|
|
|
[Support] Add support for getting file system permissions on Windows and implement sys::fs::set/getPermissions to work with them
This change adds support for functions to set and get file permissions, in a similar manner to the C++17 permissions() function in <filesystem>. The setter uses chmod on Unix systems and SetFileAttributes on Windows, setting the permissions as passed in. The getter simply uses the existing status() function.
Prior to this change, status() would always return an unknown value for the permissions on a Windows file, making it impossible to test the new function on Windows. I have therefore added support for this as well. On Linux, prior to this change, the permissions included the file type, which should actually be accessed via a different member of the file_status class.
Note that on Windows, only the *_write permission bits have any affect - if any are set, the file is writable, and if not, the file is read-only. This is in common with what MSDN describes for their behaviour of std::filesystem::permissions(), and also what boost::filesystem does.
The motivation behind this change is so that we can easily test behaviour on read-only files in LLVM unit tests, but I am sure that others may find it useful in some situations.
Reviewers: zturner, amccarth, aaron.ballman
Differential Revision: https://reviews.llvm.org/D30736
llvm-svn: 297945
2017-03-16 19:22:09 +08:00
|
|
|
ErrorOr<perms> getPermissions(const Twine &Path) {
|
|
|
|
file_status Status;
|
|
|
|
if (std::error_code EC = status(Path, Status))
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
return Status.permissions();
|
|
|
|
}
|
|
|
|
|
2017-03-13 20:24:51 +08:00
|
|
|
} // end namespace fs
|
|
|
|
} // end namespace sys
|
|
|
|
} // end namespace llvm
|
2010-11-30 06:28:51 +08:00
|
|
|
|
|
|
|
// Include the truly platform-specific parts.
|
|
|
|
#if defined(LLVM_ON_UNIX)
|
2013-06-27 03:33:03 +08:00
|
|
|
#include "Unix/Path.inc"
|
2010-11-30 06:28:51 +08:00
|
|
|
#endif
|
|
|
|
#if defined(LLVM_ON_WIN32)
|
2013-06-27 03:33:03 +08:00
|
|
|
#include "Windows/Path.inc"
|
2010-11-30 06:28:51 +08:00
|
|
|
#endif
|
2015-11-02 17:49:17 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace sys {
|
2017-11-28 09:34:20 +08:00
|
|
|
namespace fs {
|
|
|
|
TempFile::TempFile(StringRef Name, int FD) : TmpName(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;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
TempFile::~TempFile() { assert(Done); }
|
|
|
|
|
|
|
|
Error TempFile::discard() {
|
|
|
|
Done = true;
|
|
|
|
// Always try to close and remove.
|
|
|
|
std::error_code RemoveEC;
|
|
|
|
if (!TmpName.empty()) {
|
|
|
|
RemoveEC = fs::remove(TmpName);
|
|
|
|
sys::DontRemoveFileOnSignal(TmpName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!RemoveEC)
|
|
|
|
TmpName = "";
|
|
|
|
|
|
|
|
if (FD != -1 && close(FD) == -1) {
|
|
|
|
std::error_code EC = std::error_code(errno, std::generic_category());
|
|
|
|
return errorCodeToError(EC);
|
|
|
|
}
|
|
|
|
FD = -1;
|
|
|
|
|
|
|
|
return errorCodeToError(RemoveEC);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error TempFile::keep(const Twine &Name) {
|
|
|
|
assert(!Done);
|
|
|
|
Done = true;
|
|
|
|
// Always try to close and rename.
|
|
|
|
std::error_code RenameEC = fs::rename(TmpName, Name);
|
|
|
|
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;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
int FD;
|
|
|
|
SmallString<128> ResultPath;
|
|
|
|
if (std::error_code EC = createUniqueFile(Model, FD, ResultPath, Mode))
|
|
|
|
return errorCodeToError(EC);
|
|
|
|
|
|
|
|
// Make sure we delete the file when RemoveFileOnSignal fails.
|
|
|
|
TempFile Ret(ResultPath, FD);
|
|
|
|
if (sys::RemoveFileOnSignal(ResultPath)) {
|
|
|
|
consumeError(Ret.discard());
|
|
|
|
std::error_code EC(errc::operation_not_permitted);
|
|
|
|
return errorCodeToError(EC);
|
|
|
|
}
|
|
|
|
return std::move(Ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-02 17:49:17 +08:00
|
|
|
namespace path {
|
|
|
|
|
|
|
|
bool user_cache_directory(SmallVectorImpl<char> &Result, const Twine &Path1,
|
|
|
|
const Twine &Path2, const Twine &Path3) {
|
|
|
|
if (getUserCacheDir(Result)) {
|
|
|
|
append(Result, Path1, Path2, Path3);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace path
|
|
|
|
} // end namsspace sys
|
|
|
|
} // end namespace llvm
|