2018-10-10 15:27:25 +02:00
|
|
|
//===- VirtualFileSystem.cpp - Virtual File System Layer ------------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-10-10 15:27:25 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the VirtualFileSystem interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/VirtualFileSystem.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
|
|
|
#include "llvm/ADT/None.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/StringSet.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/ADT/iterator_range.h"
|
|
|
|
#include "llvm/Config/llvm-config.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/Chrono.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/Errc.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Process.h"
|
|
|
|
#include "llvm/Support/SMLoc.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/YAMLParser.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <atomic>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iterator>
|
|
|
|
#include <limits>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
|
|
|
#include <system_error>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::vfs;
|
|
|
|
|
[Support] Move llvm::MemoryBuffer to sys::fs::file_t
Summary:
On Windows, Posix integer file descriptors are a compatibility layer
over native file handles provided by the C runtime. There is a hard
limit on the maximum number of file descriptors that a process can open,
and the limit is 8192. LLD typically doesn't run into this limit because
it opens input files, maps them into memory, and then immediately closes
the file descriptor. This prevents it from running out of FDs.
For various reasons, I'd like to open handles to every input file and
keep them open during linking. That requires migrating MemoryBuffer over
to taking open native file handles instead of integer FDs.
Reviewers: aganea, Bigcheese
Reviewed By: aganea
Subscribers: smeenai, silvas, mehdi_amini, hiraditya, steven_wu, dexonsmith, dang, llvm-commits, zturner
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D63453
llvm-svn: 365588
2019-07-10 02:34:13 +02:00
|
|
|
using llvm::sys::fs::file_t;
|
2018-10-10 15:27:25 +02:00
|
|
|
using llvm::sys::fs::file_status;
|
|
|
|
using llvm::sys::fs::file_type;
|
[Support] Move llvm::MemoryBuffer to sys::fs::file_t
Summary:
On Windows, Posix integer file descriptors are a compatibility layer
over native file handles provided by the C runtime. There is a hard
limit on the maximum number of file descriptors that a process can open,
and the limit is 8192. LLD typically doesn't run into this limit because
it opens input files, maps them into memory, and then immediately closes
the file descriptor. This prevents it from running out of FDs.
For various reasons, I'd like to open handles to every input file and
keep them open during linking. That requires migrating MemoryBuffer over
to taking open native file handles instead of integer FDs.
Reviewers: aganea, Bigcheese
Reviewed By: aganea
Subscribers: smeenai, silvas, mehdi_amini, hiraditya, steven_wu, dexonsmith, dang, llvm-commits, zturner
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D63453
llvm-svn: 365588
2019-07-10 02:34:13 +02:00
|
|
|
using llvm::sys::fs::kInvalidFile;
|
2018-10-10 15:27:25 +02:00
|
|
|
using llvm::sys::fs::perms;
|
|
|
|
using llvm::sys::fs::UniqueID;
|
|
|
|
|
|
|
|
Status::Status(const file_status &Status)
|
|
|
|
: UID(Status.getUniqueID()), MTime(Status.getLastModificationTime()),
|
|
|
|
User(Status.getUser()), Group(Status.getGroup()), Size(Status.getSize()),
|
|
|
|
Type(Status.type()), Perms(Status.permissions()) {}
|
|
|
|
|
2019-02-24 00:48:47 +01:00
|
|
|
Status::Status(const Twine &Name, UniqueID UID, sys::TimePoint<> MTime,
|
2018-10-10 15:27:25 +02:00
|
|
|
uint32_t User, uint32_t Group, uint64_t Size, file_type Type,
|
|
|
|
perms Perms)
|
2019-02-24 00:48:47 +01:00
|
|
|
: Name(Name.str()), UID(UID), MTime(MTime), User(User), Group(Group),
|
|
|
|
Size(Size), Type(Type), Perms(Perms) {}
|
2018-10-10 15:27:25 +02:00
|
|
|
|
2019-02-24 00:48:47 +01:00
|
|
|
Status Status::copyWithNewName(const Status &In, const Twine &NewName) {
|
2018-10-10 15:27:25 +02:00
|
|
|
return Status(NewName, In.getUniqueID(), In.getLastModificationTime(),
|
|
|
|
In.getUser(), In.getGroup(), In.getSize(), In.getType(),
|
|
|
|
In.getPermissions());
|
|
|
|
}
|
|
|
|
|
2019-02-24 00:48:47 +01:00
|
|
|
Status Status::copyWithNewName(const file_status &In, const Twine &NewName) {
|
2018-10-10 15:27:25 +02:00
|
|
|
return Status(NewName, In.getUniqueID(), In.getLastModificationTime(),
|
|
|
|
In.getUser(), In.getGroup(), In.getSize(), In.type(),
|
|
|
|
In.permissions());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Status::equivalent(const Status &Other) const {
|
|
|
|
assert(isStatusKnown() && Other.isStatusKnown());
|
|
|
|
return getUniqueID() == Other.getUniqueID();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Status::isDirectory() const { return Type == file_type::directory_file; }
|
|
|
|
|
|
|
|
bool Status::isRegularFile() const { return Type == file_type::regular_file; }
|
|
|
|
|
|
|
|
bool Status::isOther() const {
|
|
|
|
return exists() && !isRegularFile() && !isDirectory() && !isSymlink();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Status::isSymlink() const { return Type == file_type::symlink_file; }
|
|
|
|
|
|
|
|
bool Status::isStatusKnown() const { return Type != file_type::status_error; }
|
|
|
|
|
|
|
|
bool Status::exists() const {
|
|
|
|
return isStatusKnown() && Type != file_type::file_not_found;
|
|
|
|
}
|
|
|
|
|
|
|
|
File::~File() = default;
|
|
|
|
|
|
|
|
FileSystem::~FileSystem() = default;
|
|
|
|
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>>
|
|
|
|
FileSystem::getBufferForFile(const llvm::Twine &Name, int64_t FileSize,
|
|
|
|
bool RequiresNullTerminator, bool IsVolatile) {
|
|
|
|
auto F = openFileForRead(Name);
|
|
|
|
if (!F)
|
|
|
|
return F.getError();
|
|
|
|
|
|
|
|
return (*F)->getBuffer(Name, FileSize, RequiresNullTerminator, IsVolatile);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code FileSystem::makeAbsolute(SmallVectorImpl<char> &Path) const {
|
|
|
|
if (llvm::sys::path::is_absolute(Path))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto WorkingDir = getCurrentWorkingDirectory();
|
|
|
|
if (!WorkingDir)
|
|
|
|
return WorkingDir.getError();
|
|
|
|
|
2019-01-16 10:55:32 +01:00
|
|
|
llvm::sys::fs::make_absolute(WorkingDir.get(), Path);
|
|
|
|
return {};
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code FileSystem::getRealPath(const Twine &Path,
|
2018-11-09 16:11:34 +01:00
|
|
|
SmallVectorImpl<char> &Output) const {
|
2018-10-10 15:27:25 +02:00
|
|
|
return errc::operation_not_permitted;
|
|
|
|
}
|
|
|
|
|
2018-11-08 01:01:32 +01:00
|
|
|
std::error_code FileSystem::isLocal(const Twine &Path, bool &Result) {
|
|
|
|
return errc::operation_not_permitted;
|
|
|
|
}
|
|
|
|
|
2018-10-10 15:27:25 +02:00
|
|
|
bool FileSystem::exists(const Twine &Path) {
|
|
|
|
auto Status = status(Path);
|
|
|
|
return Status && Status->exists();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
static bool isTraversalComponent(StringRef Component) {
|
|
|
|
return Component.equals("..") || Component.equals(".");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool pathHasTraversal(StringRef Path) {
|
|
|
|
using namespace llvm::sys;
|
|
|
|
|
|
|
|
for (StringRef Comp : llvm::make_range(path::begin(Path), path::end(Path)))
|
|
|
|
if (isTraversalComponent(Comp))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//===-----------------------------------------------------------------------===/
|
|
|
|
// RealFileSystem implementation
|
|
|
|
//===-----------------------------------------------------------------------===/
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// Wrapper around a raw file descriptor.
|
|
|
|
class RealFile : public File {
|
|
|
|
friend class RealFileSystem;
|
|
|
|
|
[Support] Move llvm::MemoryBuffer to sys::fs::file_t
Summary:
On Windows, Posix integer file descriptors are a compatibility layer
over native file handles provided by the C runtime. There is a hard
limit on the maximum number of file descriptors that a process can open,
and the limit is 8192. LLD typically doesn't run into this limit because
it opens input files, maps them into memory, and then immediately closes
the file descriptor. This prevents it from running out of FDs.
For various reasons, I'd like to open handles to every input file and
keep them open during linking. That requires migrating MemoryBuffer over
to taking open native file handles instead of integer FDs.
Reviewers: aganea, Bigcheese
Reviewed By: aganea
Subscribers: smeenai, silvas, mehdi_amini, hiraditya, steven_wu, dexonsmith, dang, llvm-commits, zturner
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D63453
llvm-svn: 365588
2019-07-10 02:34:13 +02:00
|
|
|
file_t FD;
|
2018-10-10 15:27:25 +02:00
|
|
|
Status S;
|
|
|
|
std::string RealName;
|
|
|
|
|
2019-10-16 13:16:59 +02:00
|
|
|
RealFile(file_t RawFD, StringRef NewName, StringRef NewRealPathName)
|
|
|
|
: FD(RawFD), S(NewName, {}, {}, {}, {}, {},
|
|
|
|
llvm::sys::fs::file_type::status_error, {}),
|
2018-10-10 15:27:25 +02:00
|
|
|
RealName(NewRealPathName.str()) {
|
[Support] Move llvm::MemoryBuffer to sys::fs::file_t
Summary:
On Windows, Posix integer file descriptors are a compatibility layer
over native file handles provided by the C runtime. There is a hard
limit on the maximum number of file descriptors that a process can open,
and the limit is 8192. LLD typically doesn't run into this limit because
it opens input files, maps them into memory, and then immediately closes
the file descriptor. This prevents it from running out of FDs.
For various reasons, I'd like to open handles to every input file and
keep them open during linking. That requires migrating MemoryBuffer over
to taking open native file handles instead of integer FDs.
Reviewers: aganea, Bigcheese
Reviewed By: aganea
Subscribers: smeenai, silvas, mehdi_amini, hiraditya, steven_wu, dexonsmith, dang, llvm-commits, zturner
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D63453
llvm-svn: 365588
2019-07-10 02:34:13 +02:00
|
|
|
assert(FD != kInvalidFile && "Invalid or inactive file descriptor");
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
~RealFile() override;
|
|
|
|
|
|
|
|
ErrorOr<Status> status() override;
|
|
|
|
ErrorOr<std::string> getName() override;
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> getBuffer(const Twine &Name,
|
|
|
|
int64_t FileSize,
|
|
|
|
bool RequiresNullTerminator,
|
|
|
|
bool IsVolatile) override;
|
|
|
|
std::error_code close() override;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
RealFile::~RealFile() { close(); }
|
|
|
|
|
|
|
|
ErrorOr<Status> RealFile::status() {
|
[Support] Move llvm::MemoryBuffer to sys::fs::file_t
Summary:
On Windows, Posix integer file descriptors are a compatibility layer
over native file handles provided by the C runtime. There is a hard
limit on the maximum number of file descriptors that a process can open,
and the limit is 8192. LLD typically doesn't run into this limit because
it opens input files, maps them into memory, and then immediately closes
the file descriptor. This prevents it from running out of FDs.
For various reasons, I'd like to open handles to every input file and
keep them open during linking. That requires migrating MemoryBuffer over
to taking open native file handles instead of integer FDs.
Reviewers: aganea, Bigcheese
Reviewed By: aganea
Subscribers: smeenai, silvas, mehdi_amini, hiraditya, steven_wu, dexonsmith, dang, llvm-commits, zturner
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D63453
llvm-svn: 365588
2019-07-10 02:34:13 +02:00
|
|
|
assert(FD != kInvalidFile && "cannot stat closed file");
|
2018-10-10 15:27:25 +02:00
|
|
|
if (!S.isStatusKnown()) {
|
|
|
|
file_status RealStatus;
|
|
|
|
if (std::error_code EC = sys::fs::status(FD, RealStatus))
|
|
|
|
return EC;
|
|
|
|
S = Status::copyWithNewName(RealStatus, S.getName());
|
|
|
|
}
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<std::string> RealFile::getName() {
|
|
|
|
return RealName.empty() ? S.getName().str() : RealName;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>>
|
|
|
|
RealFile::getBuffer(const Twine &Name, int64_t FileSize,
|
|
|
|
bool RequiresNullTerminator, bool IsVolatile) {
|
[Support] Move llvm::MemoryBuffer to sys::fs::file_t
Summary:
On Windows, Posix integer file descriptors are a compatibility layer
over native file handles provided by the C runtime. There is a hard
limit on the maximum number of file descriptors that a process can open,
and the limit is 8192. LLD typically doesn't run into this limit because
it opens input files, maps them into memory, and then immediately closes
the file descriptor. This prevents it from running out of FDs.
For various reasons, I'd like to open handles to every input file and
keep them open during linking. That requires migrating MemoryBuffer over
to taking open native file handles instead of integer FDs.
Reviewers: aganea, Bigcheese
Reviewed By: aganea
Subscribers: smeenai, silvas, mehdi_amini, hiraditya, steven_wu, dexonsmith, dang, llvm-commits, zturner
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D63453
llvm-svn: 365588
2019-07-10 02:34:13 +02:00
|
|
|
assert(FD != kInvalidFile && "cannot get buffer for closed file");
|
2018-10-10 15:27:25 +02:00
|
|
|
return MemoryBuffer::getOpenFile(FD, Name, FileSize, RequiresNullTerminator,
|
|
|
|
IsVolatile);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code RealFile::close() {
|
[Support] Move llvm::MemoryBuffer to sys::fs::file_t
Summary:
On Windows, Posix integer file descriptors are a compatibility layer
over native file handles provided by the C runtime. There is a hard
limit on the maximum number of file descriptors that a process can open,
and the limit is 8192. LLD typically doesn't run into this limit because
it opens input files, maps them into memory, and then immediately closes
the file descriptor. This prevents it from running out of FDs.
For various reasons, I'd like to open handles to every input file and
keep them open during linking. That requires migrating MemoryBuffer over
to taking open native file handles instead of integer FDs.
Reviewers: aganea, Bigcheese
Reviewed By: aganea
Subscribers: smeenai, silvas, mehdi_amini, hiraditya, steven_wu, dexonsmith, dang, llvm-commits, zturner
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D63453
llvm-svn: 365588
2019-07-10 02:34:13 +02:00
|
|
|
std::error_code EC = sys::fs::closeFile(FD);
|
|
|
|
FD = kInvalidFile;
|
2018-10-10 15:27:25 +02:00
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2019-02-14 13:57:01 +01:00
|
|
|
/// A file system according to your operating system.
|
|
|
|
/// This may be linked to the process's working directory, or maintain its own.
|
|
|
|
///
|
|
|
|
/// Currently, its own working directory is emulated by storing the path and
|
|
|
|
/// sending absolute paths to llvm::sys::fs:: functions.
|
|
|
|
/// A more principled approach would be to push this down a level, modelling
|
|
|
|
/// the working dir as an llvm::sys::fs::WorkingDir or similar.
|
|
|
|
/// This would enable the use of openat()-style functions on some platforms.
|
2018-10-10 15:27:25 +02:00
|
|
|
class RealFileSystem : public FileSystem {
|
|
|
|
public:
|
2019-02-14 13:57:01 +01:00
|
|
|
explicit RealFileSystem(bool LinkCWDToProcess) {
|
|
|
|
if (!LinkCWDToProcess) {
|
|
|
|
SmallString<128> PWD, RealPWD;
|
|
|
|
if (llvm::sys::fs::current_path(PWD))
|
|
|
|
return; // Awful, but nothing to do here.
|
|
|
|
if (llvm::sys::fs::real_path(PWD, RealPWD))
|
|
|
|
WD = {PWD, PWD};
|
|
|
|
else
|
|
|
|
WD = {PWD, RealPWD};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 15:27:25 +02:00
|
|
|
ErrorOr<Status> status(const Twine &Path) override;
|
|
|
|
ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
|
|
|
|
directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
|
|
|
|
|
|
|
|
llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override;
|
|
|
|
std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
|
2018-11-08 01:01:32 +01:00
|
|
|
std::error_code isLocal(const Twine &Path, bool &Result) override;
|
2018-11-09 16:11:34 +01:00
|
|
|
std::error_code getRealPath(const Twine &Path,
|
|
|
|
SmallVectorImpl<char> &Output) const override;
|
2018-10-10 15:27:25 +02:00
|
|
|
|
|
|
|
private:
|
2019-02-14 13:57:01 +01:00
|
|
|
// If this FS has its own working dir, use it to make Path absolute.
|
|
|
|
// The returned twine is safe to use as long as both Storage and Path live.
|
|
|
|
Twine adjustPath(const Twine &Path, SmallVectorImpl<char> &Storage) const {
|
|
|
|
if (!WD)
|
|
|
|
return Path;
|
|
|
|
Path.toVector(Storage);
|
|
|
|
sys::fs::make_absolute(WD->Resolved, Storage);
|
|
|
|
return Storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct WorkingDirectory {
|
|
|
|
// The current working directory, without symlinks resolved. (echo $PWD).
|
|
|
|
SmallString<128> Specified;
|
|
|
|
// The current working directory, with links resolved. (readlink .).
|
|
|
|
SmallString<128> Resolved;
|
|
|
|
};
|
|
|
|
Optional<WorkingDirectory> WD;
|
2018-10-10 15:27:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
ErrorOr<Status> RealFileSystem::status(const Twine &Path) {
|
2019-02-14 13:57:01 +01:00
|
|
|
SmallString<256> Storage;
|
2018-10-10 15:27:25 +02:00
|
|
|
sys::fs::file_status RealStatus;
|
2019-02-14 13:57:01 +01:00
|
|
|
if (std::error_code EC =
|
|
|
|
sys::fs::status(adjustPath(Path, Storage), RealStatus))
|
2018-10-10 15:27:25 +02:00
|
|
|
return EC;
|
2019-02-24 00:48:47 +01:00
|
|
|
return Status::copyWithNewName(RealStatus, Path);
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<std::unique_ptr<File>>
|
|
|
|
RealFileSystem::openFileForRead(const Twine &Name) {
|
2019-02-14 13:57:01 +01:00
|
|
|
SmallString<256> RealName, Storage;
|
[Support] Move llvm::MemoryBuffer to sys::fs::file_t
Summary:
On Windows, Posix integer file descriptors are a compatibility layer
over native file handles provided by the C runtime. There is a hard
limit on the maximum number of file descriptors that a process can open,
and the limit is 8192. LLD typically doesn't run into this limit because
it opens input files, maps them into memory, and then immediately closes
the file descriptor. This prevents it from running out of FDs.
For various reasons, I'd like to open handles to every input file and
keep them open during linking. That requires migrating MemoryBuffer over
to taking open native file handles instead of integer FDs.
Reviewers: aganea, Bigcheese
Reviewed By: aganea
Subscribers: smeenai, silvas, mehdi_amini, hiraditya, steven_wu, dexonsmith, dang, llvm-commits, zturner
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D63453
llvm-svn: 365588
2019-07-10 02:34:13 +02:00
|
|
|
Expected<file_t> FDOrErr = sys::fs::openNativeFileForRead(
|
|
|
|
adjustPath(Name, Storage), sys::fs::OF_None, &RealName);
|
|
|
|
if (!FDOrErr)
|
|
|
|
return errorToErrorCode(FDOrErr.takeError());
|
|
|
|
return std::unique_ptr<File>(
|
|
|
|
new RealFile(*FDOrErr, Name.str(), RealName.str()));
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::ErrorOr<std::string> RealFileSystem::getCurrentWorkingDirectory() const {
|
2019-02-14 13:57:01 +01:00
|
|
|
if (WD)
|
2020-01-28 20:23:46 +01:00
|
|
|
return std::string(WD->Specified.str());
|
2019-02-14 13:57:01 +01:00
|
|
|
|
|
|
|
SmallString<128> Dir;
|
2018-10-10 15:27:25 +02:00
|
|
|
if (std::error_code EC = llvm::sys::fs::current_path(Dir))
|
|
|
|
return EC;
|
2020-01-28 20:23:46 +01:00
|
|
|
return std::string(Dir.str());
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code RealFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
|
2019-02-14 13:57:01 +01:00
|
|
|
if (!WD)
|
|
|
|
return llvm::sys::fs::set_current_path(Path);
|
|
|
|
|
|
|
|
SmallString<128> Absolute, Resolved, Storage;
|
|
|
|
adjustPath(Path, Storage).toVector(Absolute);
|
|
|
|
bool IsDir;
|
|
|
|
if (auto Err = llvm::sys::fs::is_directory(Absolute, IsDir))
|
|
|
|
return Err;
|
|
|
|
if (!IsDir)
|
|
|
|
return std::make_error_code(std::errc::not_a_directory);
|
|
|
|
if (auto Err = llvm::sys::fs::real_path(Absolute, Resolved))
|
|
|
|
return Err;
|
|
|
|
WD = {Absolute, Resolved};
|
2018-10-10 15:27:25 +02:00
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
2018-11-08 01:01:32 +01:00
|
|
|
std::error_code RealFileSystem::isLocal(const Twine &Path, bool &Result) {
|
2019-02-14 13:57:01 +01:00
|
|
|
SmallString<256> Storage;
|
|
|
|
return llvm::sys::fs::is_local(adjustPath(Path, Storage), Result);
|
2018-11-08 01:01:32 +01:00
|
|
|
}
|
|
|
|
|
2018-11-09 16:11:34 +01:00
|
|
|
std::error_code
|
|
|
|
RealFileSystem::getRealPath(const Twine &Path,
|
|
|
|
SmallVectorImpl<char> &Output) const {
|
2019-02-14 13:57:01 +01:00
|
|
|
SmallString<256> Storage;
|
|
|
|
return llvm::sys::fs::real_path(adjustPath(Path, Storage), Output);
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() {
|
2019-02-14 13:57:01 +01:00
|
|
|
static IntrusiveRefCntPtr<FileSystem> FS(new RealFileSystem(true));
|
2018-10-10 15:27:25 +02:00
|
|
|
return FS;
|
|
|
|
}
|
|
|
|
|
2019-02-14 13:57:01 +01:00
|
|
|
std::unique_ptr<FileSystem> vfs::createPhysicalFileSystem() {
|
2019-08-15 17:54:37 +02:00
|
|
|
return std::make_unique<RealFileSystem>(false);
|
2019-02-14 13:57:01 +01:00
|
|
|
}
|
|
|
|
|
2018-10-10 15:27:25 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class RealFSDirIter : public llvm::vfs::detail::DirIterImpl {
|
|
|
|
llvm::sys::fs::directory_iterator Iter;
|
|
|
|
|
|
|
|
public:
|
|
|
|
RealFSDirIter(const Twine &Path, std::error_code &EC) : Iter(Path, EC) {
|
|
|
|
if (Iter != llvm::sys::fs::directory_iterator())
|
|
|
|
CurrentEntry = directory_entry(Iter->path(), Iter->type());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code increment() override {
|
|
|
|
std::error_code EC;
|
|
|
|
Iter.increment(EC);
|
|
|
|
CurrentEntry = (Iter == llvm::sys::fs::directory_iterator())
|
|
|
|
? directory_entry()
|
|
|
|
: directory_entry(Iter->path(), Iter->type());
|
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
directory_iterator RealFileSystem::dir_begin(const Twine &Dir,
|
|
|
|
std::error_code &EC) {
|
2019-02-14 13:57:01 +01:00
|
|
|
SmallString<128> Storage;
|
|
|
|
return directory_iterator(
|
|
|
|
std::make_shared<RealFSDirIter>(adjustPath(Dir, Storage), EC));
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//===-----------------------------------------------------------------------===/
|
|
|
|
// OverlayFileSystem implementation
|
|
|
|
//===-----------------------------------------------------------------------===/
|
|
|
|
|
|
|
|
OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> BaseFS) {
|
|
|
|
FSList.push_back(std::move(BaseFS));
|
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr<FileSystem> FS) {
|
|
|
|
FSList.push_back(FS);
|
|
|
|
// Synchronize added file systems by duplicating the working directory from
|
|
|
|
// the first one in the list.
|
|
|
|
FS->setCurrentWorkingDirectory(getCurrentWorkingDirectory().get());
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<Status> OverlayFileSystem::status(const Twine &Path) {
|
|
|
|
// FIXME: handle symlinks that cross file systems
|
|
|
|
for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
|
|
|
|
ErrorOr<Status> Status = (*I)->status(Path);
|
|
|
|
if (Status || Status.getError() != llvm::errc::no_such_file_or_directory)
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
return make_error_code(llvm::errc::no_such_file_or_directory);
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<std::unique_ptr<File>>
|
|
|
|
OverlayFileSystem::openFileForRead(const llvm::Twine &Path) {
|
|
|
|
// FIXME: handle symlinks that cross file systems
|
|
|
|
for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
|
|
|
|
auto Result = (*I)->openFileForRead(Path);
|
|
|
|
if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
return make_error_code(llvm::errc::no_such_file_or_directory);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::ErrorOr<std::string>
|
|
|
|
OverlayFileSystem::getCurrentWorkingDirectory() const {
|
|
|
|
// All file systems are synchronized, just take the first working directory.
|
|
|
|
return FSList.front()->getCurrentWorkingDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code
|
|
|
|
OverlayFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
|
|
|
|
for (auto &FS : FSList)
|
|
|
|
if (std::error_code EC = FS->setCurrentWorkingDirectory(Path))
|
|
|
|
return EC;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-11-08 01:01:32 +01:00
|
|
|
std::error_code OverlayFileSystem::isLocal(const Twine &Path, bool &Result) {
|
|
|
|
for (auto &FS : FSList)
|
|
|
|
if (FS->exists(Path))
|
|
|
|
return FS->isLocal(Path, Result);
|
|
|
|
return errc::no_such_file_or_directory;
|
|
|
|
}
|
|
|
|
|
2018-11-09 16:11:34 +01:00
|
|
|
std::error_code
|
|
|
|
OverlayFileSystem::getRealPath(const Twine &Path,
|
|
|
|
SmallVectorImpl<char> &Output) const {
|
2018-10-10 15:27:25 +02:00
|
|
|
for (auto &FS : FSList)
|
|
|
|
if (FS->exists(Path))
|
2018-11-09 16:11:34 +01:00
|
|
|
return FS->getRealPath(Path, Output);
|
2018-10-10 15:27:25 +02:00
|
|
|
return errc::no_such_file_or_directory;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::vfs::detail::DirIterImpl::~DirIterImpl() = default;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2021-01-16 02:44:14 +01:00
|
|
|
/// Combines and deduplicates directory entries across multiple file systems.
|
|
|
|
class CombiningDirIterImpl : public llvm::vfs::detail::DirIterImpl {
|
|
|
|
using FileSystemPtr = llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>;
|
|
|
|
|
|
|
|
/// File systems to check for entries in. Processed in reverse order.
|
|
|
|
SmallVector<FileSystemPtr, 8> FSList;
|
|
|
|
/// The directory iterator for the current filesystem.
|
2018-10-10 15:27:25 +02:00
|
|
|
directory_iterator CurrentDirIter;
|
2021-01-16 02:44:14 +01:00
|
|
|
/// The path of the directory to iterate the entries of.
|
|
|
|
std::string DirPath;
|
|
|
|
/// The set of names already returned as entries.
|
2018-10-10 15:27:25 +02:00
|
|
|
llvm::StringSet<> SeenNames;
|
|
|
|
|
2021-01-16 02:44:14 +01:00
|
|
|
/// Sets \c CurrentDirIter to an iterator of \c DirPath in the next file
|
|
|
|
/// system in the list, or leaves it as is (at its end position) if we've
|
|
|
|
/// already gone through them all.
|
2018-10-10 15:27:25 +02:00
|
|
|
std::error_code incrementFS() {
|
2021-01-16 02:44:14 +01:00
|
|
|
while (!FSList.empty()) {
|
2018-10-10 15:27:25 +02:00
|
|
|
std::error_code EC;
|
2021-01-16 02:44:14 +01:00
|
|
|
CurrentDirIter = FSList.back()->dir_begin(DirPath, EC);
|
|
|
|
FSList.pop_back();
|
2018-10-10 15:27:25 +02:00
|
|
|
if (EC && EC != errc::no_such_file_or_directory)
|
|
|
|
return EC;
|
|
|
|
if (CurrentDirIter != directory_iterator())
|
|
|
|
break; // found
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code incrementDirIter(bool IsFirstTime) {
|
|
|
|
assert((IsFirstTime || CurrentDirIter != directory_iterator()) &&
|
|
|
|
"incrementing past end");
|
|
|
|
std::error_code EC;
|
|
|
|
if (!IsFirstTime)
|
|
|
|
CurrentDirIter.increment(EC);
|
|
|
|
if (!EC && CurrentDirIter == directory_iterator())
|
|
|
|
EC = incrementFS();
|
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code incrementImpl(bool IsFirstTime) {
|
|
|
|
while (true) {
|
|
|
|
std::error_code EC = incrementDirIter(IsFirstTime);
|
|
|
|
if (EC || CurrentDirIter == directory_iterator()) {
|
|
|
|
CurrentEntry = directory_entry();
|
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
CurrentEntry = *CurrentDirIter;
|
|
|
|
StringRef Name = llvm::sys::path::filename(CurrentEntry.path());
|
|
|
|
if (SeenNames.insert(Name).second)
|
|
|
|
return EC; // name not seen before
|
|
|
|
}
|
|
|
|
llvm_unreachable("returned above");
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2021-01-16 02:44:14 +01:00
|
|
|
CombiningDirIterImpl(ArrayRef<FileSystemPtr> FileSystems, std::string Dir,
|
2018-10-10 15:27:25 +02:00
|
|
|
std::error_code &EC)
|
2021-01-16 02:44:14 +01:00
|
|
|
: FSList(FileSystems.begin(), FileSystems.end()),
|
|
|
|
DirPath(std::move(Dir)) {
|
|
|
|
if (!FSList.empty()) {
|
|
|
|
CurrentDirIter = FSList.back()->dir_begin(DirPath, EC);
|
|
|
|
FSList.pop_back();
|
|
|
|
if (!EC || EC == errc::no_such_file_or_directory)
|
|
|
|
EC = incrementImpl(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CombiningDirIterImpl(directory_iterator FirstIter, FileSystemPtr Fallback,
|
|
|
|
std::string FallbackDir, std::error_code &EC)
|
|
|
|
: FSList({Fallback}), CurrentDirIter(FirstIter),
|
|
|
|
DirPath(std::move(FallbackDir)) {
|
|
|
|
if (!EC || EC == errc::no_such_file_or_directory)
|
|
|
|
EC = incrementImpl(true);
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code increment() override { return incrementImpl(false); }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
directory_iterator OverlayFileSystem::dir_begin(const Twine &Dir,
|
|
|
|
std::error_code &EC) {
|
|
|
|
return directory_iterator(
|
2021-01-16 02:44:14 +01:00
|
|
|
std::make_shared<CombiningDirIterImpl>(FSList, Dir.str(), EC));
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 03:02:13 +01:00
|
|
|
void ProxyFileSystem::anchor() {}
|
|
|
|
|
2018-10-10 15:27:25 +02:00
|
|
|
namespace llvm {
|
|
|
|
namespace vfs {
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
enum InMemoryNodeKind { IME_File, IME_Directory, IME_HardLink };
|
|
|
|
|
|
|
|
/// The in memory file system is a tree of Nodes. Every node can either be a
|
|
|
|
/// file , hardlink or a directory.
|
|
|
|
class InMemoryNode {
|
|
|
|
InMemoryNodeKind Kind;
|
|
|
|
std::string FileName;
|
|
|
|
|
|
|
|
public:
|
|
|
|
InMemoryNode(llvm::StringRef FileName, InMemoryNodeKind Kind)
|
2020-01-28 20:23:46 +01:00
|
|
|
: Kind(Kind), FileName(std::string(llvm::sys::path::filename(FileName))) {
|
|
|
|
}
|
2018-10-10 15:27:25 +02:00
|
|
|
virtual ~InMemoryNode() = default;
|
|
|
|
|
|
|
|
/// Get the filename of this node (the name without the directory part).
|
|
|
|
StringRef getFileName() const { return FileName; }
|
|
|
|
InMemoryNodeKind getKind() const { return Kind; }
|
|
|
|
virtual std::string toString(unsigned Indent) const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class InMemoryFile : public InMemoryNode {
|
|
|
|
Status Stat;
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buffer;
|
|
|
|
|
|
|
|
public:
|
|
|
|
InMemoryFile(Status Stat, std::unique_ptr<llvm::MemoryBuffer> Buffer)
|
|
|
|
: InMemoryNode(Stat.getName(), IME_File), Stat(std::move(Stat)),
|
|
|
|
Buffer(std::move(Buffer)) {}
|
|
|
|
|
|
|
|
/// Return the \p Status for this node. \p RequestedName should be the name
|
|
|
|
/// through which the caller referred to this node. It will override
|
|
|
|
/// \p Status::Name in the return value, to mimic the behavior of \p RealFile.
|
2019-02-24 00:48:47 +01:00
|
|
|
Status getStatus(const Twine &RequestedName) const {
|
2018-10-10 15:27:25 +02:00
|
|
|
return Status::copyWithNewName(Stat, RequestedName);
|
|
|
|
}
|
|
|
|
llvm::MemoryBuffer *getBuffer() const { return Buffer.get(); }
|
|
|
|
|
|
|
|
std::string toString(unsigned Indent) const override {
|
|
|
|
return (std::string(Indent, ' ') + Stat.getName() + "\n").str();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool classof(const InMemoryNode *N) {
|
|
|
|
return N->getKind() == IME_File;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class InMemoryHardLink : public InMemoryNode {
|
|
|
|
const InMemoryFile &ResolvedFile;
|
|
|
|
|
|
|
|
public:
|
|
|
|
InMemoryHardLink(StringRef Path, const InMemoryFile &ResolvedFile)
|
|
|
|
: InMemoryNode(Path, IME_HardLink), ResolvedFile(ResolvedFile) {}
|
|
|
|
const InMemoryFile &getResolvedFile() const { return ResolvedFile; }
|
|
|
|
|
|
|
|
std::string toString(unsigned Indent) const override {
|
|
|
|
return std::string(Indent, ' ') + "HardLink to -> " +
|
|
|
|
ResolvedFile.toString(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool classof(const InMemoryNode *N) {
|
|
|
|
return N->getKind() == IME_HardLink;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Adapt a InMemoryFile for VFS' File interface. The goal is to make
|
|
|
|
/// \p InMemoryFileAdaptor mimic as much as possible the behavior of
|
|
|
|
/// \p RealFile.
|
|
|
|
class InMemoryFileAdaptor : public File {
|
|
|
|
const InMemoryFile &Node;
|
|
|
|
/// The name to use when returning a Status for this file.
|
|
|
|
std::string RequestedName;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit InMemoryFileAdaptor(const InMemoryFile &Node,
|
|
|
|
std::string RequestedName)
|
|
|
|
: Node(Node), RequestedName(std::move(RequestedName)) {}
|
|
|
|
|
|
|
|
llvm::ErrorOr<Status> status() override {
|
|
|
|
return Node.getStatus(RequestedName);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
|
|
|
|
getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
|
|
|
|
bool IsVolatile) override {
|
|
|
|
llvm::MemoryBuffer *Buf = Node.getBuffer();
|
|
|
|
return llvm::MemoryBuffer::getMemBuffer(
|
|
|
|
Buf->getBuffer(), Buf->getBufferIdentifier(), RequiresNullTerminator);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code close() override { return {}; }
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
class InMemoryDirectory : public InMemoryNode {
|
|
|
|
Status Stat;
|
|
|
|
llvm::StringMap<std::unique_ptr<InMemoryNode>> Entries;
|
|
|
|
|
|
|
|
public:
|
|
|
|
InMemoryDirectory(Status Stat)
|
|
|
|
: InMemoryNode(Stat.getName(), IME_Directory), Stat(std::move(Stat)) {}
|
|
|
|
|
|
|
|
/// Return the \p Status for this node. \p RequestedName should be the name
|
|
|
|
/// through which the caller referred to this node. It will override
|
|
|
|
/// \p Status::Name in the return value, to mimic the behavior of \p RealFile.
|
2019-02-24 00:48:47 +01:00
|
|
|
Status getStatus(const Twine &RequestedName) const {
|
2018-10-10 15:27:25 +02:00
|
|
|
return Status::copyWithNewName(Stat, RequestedName);
|
|
|
|
}
|
|
|
|
InMemoryNode *getChild(StringRef Name) {
|
|
|
|
auto I = Entries.find(Name);
|
|
|
|
if (I != Entries.end())
|
|
|
|
return I->second.get();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
InMemoryNode *addChild(StringRef Name, std::unique_ptr<InMemoryNode> Child) {
|
|
|
|
return Entries.insert(make_pair(Name, std::move(Child)))
|
|
|
|
.first->second.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
using const_iterator = decltype(Entries)::const_iterator;
|
|
|
|
|
|
|
|
const_iterator begin() const { return Entries.begin(); }
|
|
|
|
const_iterator end() const { return Entries.end(); }
|
|
|
|
|
|
|
|
std::string toString(unsigned Indent) const override {
|
|
|
|
std::string Result =
|
|
|
|
(std::string(Indent, ' ') + Stat.getName() + "\n").str();
|
|
|
|
for (const auto &Entry : Entries)
|
|
|
|
Result += Entry.second->toString(Indent + 2);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool classof(const InMemoryNode *N) {
|
|
|
|
return N->getKind() == IME_Directory;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
2019-02-24 00:48:47 +01:00
|
|
|
Status getNodeStatus(const InMemoryNode *Node, const Twine &RequestedName) {
|
2018-10-10 15:27:25 +02:00
|
|
|
if (auto Dir = dyn_cast<detail::InMemoryDirectory>(Node))
|
|
|
|
return Dir->getStatus(RequestedName);
|
|
|
|
if (auto File = dyn_cast<detail::InMemoryFile>(Node))
|
|
|
|
return File->getStatus(RequestedName);
|
|
|
|
if (auto Link = dyn_cast<detail::InMemoryHardLink>(Node))
|
|
|
|
return Link->getResolvedFile().getStatus(RequestedName);
|
|
|
|
llvm_unreachable("Unknown node type");
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
InMemoryFileSystem::InMemoryFileSystem(bool UseNormalizedPaths)
|
|
|
|
: Root(new detail::InMemoryDirectory(
|
|
|
|
Status("", getNextVirtualUniqueID(), llvm::sys::TimePoint<>(), 0, 0,
|
|
|
|
0, llvm::sys::fs::file_type::directory_file,
|
|
|
|
llvm::sys::fs::perms::all_all))),
|
|
|
|
UseNormalizedPaths(UseNormalizedPaths) {}
|
|
|
|
|
|
|
|
InMemoryFileSystem::~InMemoryFileSystem() = default;
|
|
|
|
|
|
|
|
std::string InMemoryFileSystem::toString() const {
|
|
|
|
return Root->toString(/*Indent=*/0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime,
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buffer,
|
|
|
|
Optional<uint32_t> User,
|
|
|
|
Optional<uint32_t> Group,
|
|
|
|
Optional<llvm::sys::fs::file_type> Type,
|
|
|
|
Optional<llvm::sys::fs::perms> Perms,
|
|
|
|
const detail::InMemoryFile *HardLinkTarget) {
|
|
|
|
SmallString<128> Path;
|
|
|
|
P.toVector(Path);
|
|
|
|
|
|
|
|
// Fix up relative paths. This just prepends the current working directory.
|
|
|
|
std::error_code EC = makeAbsolute(Path);
|
|
|
|
assert(!EC);
|
|
|
|
(void)EC;
|
|
|
|
|
|
|
|
if (useNormalizedPaths())
|
|
|
|
llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
|
|
|
|
|
|
|
|
if (Path.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
detail::InMemoryDirectory *Dir = Root.get();
|
|
|
|
auto I = llvm::sys::path::begin(Path), E = sys::path::end(Path);
|
|
|
|
const auto ResolvedUser = User.getValueOr(0);
|
|
|
|
const auto ResolvedGroup = Group.getValueOr(0);
|
|
|
|
const auto ResolvedType = Type.getValueOr(sys::fs::file_type::regular_file);
|
|
|
|
const auto ResolvedPerms = Perms.getValueOr(sys::fs::all_all);
|
|
|
|
assert(!(HardLinkTarget && Buffer) && "HardLink cannot have a buffer");
|
|
|
|
// Any intermediate directories we create should be accessible by
|
|
|
|
// the owner, even if Perms says otherwise for the final path.
|
|
|
|
const auto NewDirectoryPerms = ResolvedPerms | sys::fs::owner_all;
|
|
|
|
while (true) {
|
|
|
|
StringRef Name = *I;
|
|
|
|
detail::InMemoryNode *Node = Dir->getChild(Name);
|
|
|
|
++I;
|
|
|
|
if (!Node) {
|
|
|
|
if (I == E) {
|
|
|
|
// End of the path.
|
|
|
|
std::unique_ptr<detail::InMemoryNode> Child;
|
|
|
|
if (HardLinkTarget)
|
|
|
|
Child.reset(new detail::InMemoryHardLink(P.str(), *HardLinkTarget));
|
|
|
|
else {
|
|
|
|
// Create a new file or directory.
|
|
|
|
Status Stat(P.str(), getNextVirtualUniqueID(),
|
|
|
|
llvm::sys::toTimePoint(ModificationTime), ResolvedUser,
|
|
|
|
ResolvedGroup, Buffer->getBufferSize(), ResolvedType,
|
|
|
|
ResolvedPerms);
|
|
|
|
if (ResolvedType == sys::fs::file_type::directory_file) {
|
|
|
|
Child.reset(new detail::InMemoryDirectory(std::move(Stat)));
|
|
|
|
} else {
|
|
|
|
Child.reset(
|
|
|
|
new detail::InMemoryFile(std::move(Stat), std::move(Buffer)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Dir->addChild(Name, std::move(Child));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new directory. Use the path up to here.
|
|
|
|
Status Stat(
|
|
|
|
StringRef(Path.str().begin(), Name.end() - Path.str().begin()),
|
|
|
|
getNextVirtualUniqueID(), llvm::sys::toTimePoint(ModificationTime),
|
|
|
|
ResolvedUser, ResolvedGroup, 0, sys::fs::file_type::directory_file,
|
|
|
|
NewDirectoryPerms);
|
|
|
|
Dir = cast<detail::InMemoryDirectory>(Dir->addChild(
|
2019-08-15 17:54:37 +02:00
|
|
|
Name, std::make_unique<detail::InMemoryDirectory>(std::move(Stat))));
|
2018-10-10 15:27:25 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto *NewDir = dyn_cast<detail::InMemoryDirectory>(Node)) {
|
|
|
|
Dir = NewDir;
|
|
|
|
} else {
|
|
|
|
assert((isa<detail::InMemoryFile>(Node) ||
|
|
|
|
isa<detail::InMemoryHardLink>(Node)) &&
|
|
|
|
"Must be either file, hardlink or directory!");
|
|
|
|
|
|
|
|
// Trying to insert a directory in place of a file.
|
|
|
|
if (I != E)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Return false only if the new file is different from the existing one.
|
|
|
|
if (auto Link = dyn_cast<detail::InMemoryHardLink>(Node)) {
|
|
|
|
return Link->getResolvedFile().getBuffer()->getBuffer() ==
|
|
|
|
Buffer->getBuffer();
|
|
|
|
}
|
|
|
|
return cast<detail::InMemoryFile>(Node)->getBuffer()->getBuffer() ==
|
|
|
|
Buffer->getBuffer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime,
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buffer,
|
|
|
|
Optional<uint32_t> User,
|
|
|
|
Optional<uint32_t> Group,
|
|
|
|
Optional<llvm::sys::fs::file_type> Type,
|
|
|
|
Optional<llvm::sys::fs::perms> Perms) {
|
|
|
|
return addFile(P, ModificationTime, std::move(Buffer), User, Group, Type,
|
|
|
|
Perms, /*HardLinkTarget=*/nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InMemoryFileSystem::addFileNoOwn(const Twine &P, time_t ModificationTime,
|
2020-11-06 00:23:19 +01:00
|
|
|
const llvm::MemoryBufferRef &Buffer,
|
2018-10-10 15:27:25 +02:00
|
|
|
Optional<uint32_t> User,
|
|
|
|
Optional<uint32_t> Group,
|
|
|
|
Optional<llvm::sys::fs::file_type> Type,
|
|
|
|
Optional<llvm::sys::fs::perms> Perms) {
|
2020-11-06 00:23:19 +01:00
|
|
|
return addFile(P, ModificationTime, llvm::MemoryBuffer::getMemBuffer(Buffer),
|
2018-10-10 15:27:25 +02:00
|
|
|
std::move(User), std::move(Group), std::move(Type),
|
|
|
|
std::move(Perms));
|
|
|
|
}
|
|
|
|
|
|
|
|
static ErrorOr<const detail::InMemoryNode *>
|
|
|
|
lookupInMemoryNode(const InMemoryFileSystem &FS, detail::InMemoryDirectory *Dir,
|
|
|
|
const Twine &P) {
|
|
|
|
SmallString<128> Path;
|
|
|
|
P.toVector(Path);
|
|
|
|
|
|
|
|
// Fix up relative paths. This just prepends the current working directory.
|
|
|
|
std::error_code EC = FS.makeAbsolute(Path);
|
|
|
|
assert(!EC);
|
|
|
|
(void)EC;
|
|
|
|
|
|
|
|
if (FS.useNormalizedPaths())
|
|
|
|
llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
|
|
|
|
|
|
|
|
if (Path.empty())
|
|
|
|
return Dir;
|
|
|
|
|
|
|
|
auto I = llvm::sys::path::begin(Path), E = llvm::sys::path::end(Path);
|
|
|
|
while (true) {
|
|
|
|
detail::InMemoryNode *Node = Dir->getChild(*I);
|
|
|
|
++I;
|
|
|
|
if (!Node)
|
|
|
|
return errc::no_such_file_or_directory;
|
|
|
|
|
|
|
|
// Return the file if it's at the end of the path.
|
|
|
|
if (auto File = dyn_cast<detail::InMemoryFile>(Node)) {
|
|
|
|
if (I == E)
|
|
|
|
return File;
|
|
|
|
return errc::no_such_file_or_directory;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If Node is HardLink then return the resolved file.
|
|
|
|
if (auto File = dyn_cast<detail::InMemoryHardLink>(Node)) {
|
|
|
|
if (I == E)
|
|
|
|
return &File->getResolvedFile();
|
|
|
|
return errc::no_such_file_or_directory;
|
|
|
|
}
|
|
|
|
// Traverse directories.
|
|
|
|
Dir = cast<detail::InMemoryDirectory>(Node);
|
|
|
|
if (I == E)
|
|
|
|
return Dir;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InMemoryFileSystem::addHardLink(const Twine &FromPath,
|
|
|
|
const Twine &ToPath) {
|
|
|
|
auto FromNode = lookupInMemoryNode(*this, Root.get(), FromPath);
|
|
|
|
auto ToNode = lookupInMemoryNode(*this, Root.get(), ToPath);
|
|
|
|
// FromPath must not have been added before. ToPath must have been added
|
|
|
|
// before. Resolved ToPath must be a File.
|
|
|
|
if (!ToNode || FromNode || !isa<detail::InMemoryFile>(*ToNode))
|
|
|
|
return false;
|
|
|
|
return this->addFile(FromPath, 0, nullptr, None, None, None, None,
|
|
|
|
cast<detail::InMemoryFile>(*ToNode));
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::ErrorOr<Status> InMemoryFileSystem::status(const Twine &Path) {
|
|
|
|
auto Node = lookupInMemoryNode(*this, Root.get(), Path);
|
|
|
|
if (Node)
|
2019-02-24 00:48:47 +01:00
|
|
|
return detail::getNodeStatus(*Node, Path);
|
2018-10-10 15:27:25 +02:00
|
|
|
return Node.getError();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::ErrorOr<std::unique_ptr<File>>
|
|
|
|
InMemoryFileSystem::openFileForRead(const Twine &Path) {
|
|
|
|
auto Node = lookupInMemoryNode(*this, Root.get(), Path);
|
|
|
|
if (!Node)
|
|
|
|
return Node.getError();
|
|
|
|
|
|
|
|
// When we have a file provide a heap-allocated wrapper for the memory buffer
|
|
|
|
// to match the ownership semantics for File.
|
|
|
|
if (auto *F = dyn_cast<detail::InMemoryFile>(*Node))
|
|
|
|
return std::unique_ptr<File>(
|
|
|
|
new detail::InMemoryFileAdaptor(*F, Path.str()));
|
|
|
|
|
|
|
|
// FIXME: errc::not_a_file?
|
|
|
|
return make_error_code(llvm::errc::invalid_argument);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// Adaptor from InMemoryDir::iterator to directory_iterator.
|
|
|
|
class InMemoryDirIterator : public llvm::vfs::detail::DirIterImpl {
|
|
|
|
detail::InMemoryDirectory::const_iterator I;
|
|
|
|
detail::InMemoryDirectory::const_iterator E;
|
|
|
|
std::string RequestedDirName;
|
|
|
|
|
|
|
|
void setCurrentEntry() {
|
|
|
|
if (I != E) {
|
|
|
|
SmallString<256> Path(RequestedDirName);
|
|
|
|
llvm::sys::path::append(Path, I->second->getFileName());
|
2019-11-04 18:23:07 +01:00
|
|
|
sys::fs::file_type Type = sys::fs::file_type::type_unknown;
|
2018-10-10 15:27:25 +02:00
|
|
|
switch (I->second->getKind()) {
|
|
|
|
case detail::IME_File:
|
|
|
|
case detail::IME_HardLink:
|
|
|
|
Type = sys::fs::file_type::regular_file;
|
|
|
|
break;
|
|
|
|
case detail::IME_Directory:
|
|
|
|
Type = sys::fs::file_type::directory_file;
|
|
|
|
break;
|
|
|
|
}
|
2020-01-28 20:23:46 +01:00
|
|
|
CurrentEntry = directory_entry(std::string(Path.str()), Type);
|
2018-10-10 15:27:25 +02:00
|
|
|
} else {
|
|
|
|
// When we're at the end, make CurrentEntry invalid and DirIterImpl will
|
|
|
|
// do the rest.
|
|
|
|
CurrentEntry = directory_entry();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
InMemoryDirIterator() = default;
|
|
|
|
|
|
|
|
explicit InMemoryDirIterator(const detail::InMemoryDirectory &Dir,
|
|
|
|
std::string RequestedDirName)
|
|
|
|
: I(Dir.begin()), E(Dir.end()),
|
|
|
|
RequestedDirName(std::move(RequestedDirName)) {
|
|
|
|
setCurrentEntry();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code increment() override {
|
|
|
|
++I;
|
|
|
|
setCurrentEntry();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
directory_iterator InMemoryFileSystem::dir_begin(const Twine &Dir,
|
|
|
|
std::error_code &EC) {
|
|
|
|
auto Node = lookupInMemoryNode(*this, Root.get(), Dir);
|
|
|
|
if (!Node) {
|
|
|
|
EC = Node.getError();
|
|
|
|
return directory_iterator(std::make_shared<InMemoryDirIterator>());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto *DirNode = dyn_cast<detail::InMemoryDirectory>(*Node))
|
|
|
|
return directory_iterator(
|
|
|
|
std::make_shared<InMemoryDirIterator>(*DirNode, Dir.str()));
|
|
|
|
|
|
|
|
EC = make_error_code(llvm::errc::not_a_directory);
|
|
|
|
return directory_iterator(std::make_shared<InMemoryDirIterator>());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code InMemoryFileSystem::setCurrentWorkingDirectory(const Twine &P) {
|
|
|
|
SmallString<128> Path;
|
|
|
|
P.toVector(Path);
|
|
|
|
|
|
|
|
// Fix up relative paths. This just prepends the current working directory.
|
|
|
|
std::error_code EC = makeAbsolute(Path);
|
|
|
|
assert(!EC);
|
|
|
|
(void)EC;
|
|
|
|
|
|
|
|
if (useNormalizedPaths())
|
|
|
|
llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
|
|
|
|
|
|
|
|
if (!Path.empty())
|
2020-01-28 20:23:46 +01:00
|
|
|
WorkingDirectory = std::string(Path.str());
|
2018-10-10 15:27:25 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-11-09 16:11:34 +01:00
|
|
|
std::error_code
|
|
|
|
InMemoryFileSystem::getRealPath(const Twine &Path,
|
|
|
|
SmallVectorImpl<char> &Output) const {
|
2018-10-10 15:27:25 +02:00
|
|
|
auto CWD = getCurrentWorkingDirectory();
|
|
|
|
if (!CWD || CWD->empty())
|
|
|
|
return errc::operation_not_permitted;
|
|
|
|
Path.toVector(Output);
|
|
|
|
if (auto EC = makeAbsolute(Output))
|
|
|
|
return EC;
|
|
|
|
llvm::sys::path::remove_dots(Output, /*remove_dot_dot=*/true);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-11-08 01:01:32 +01:00
|
|
|
std::error_code InMemoryFileSystem::isLocal(const Twine &Path, bool &Result) {
|
|
|
|
Result = false;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-10-10 15:27:25 +02:00
|
|
|
} // namespace vfs
|
|
|
|
} // namespace llvm
|
|
|
|
|
|
|
|
//===-----------------------------------------------------------------------===/
|
|
|
|
// RedirectingFileSystem implementation
|
|
|
|
//===-----------------------------------------------------------------------===/
|
|
|
|
|
2020-01-22 01:45:51 +01:00
|
|
|
namespace {
|
|
|
|
|
2021-01-15 08:33:52 +01:00
|
|
|
static llvm::sys::path::Style getExistingStyle(llvm::StringRef Path) {
|
|
|
|
// Detect the path style in use by checking the first separator.
|
2020-01-22 01:45:51 +01:00
|
|
|
llvm::sys::path::Style style = llvm::sys::path::Style::native;
|
|
|
|
const size_t n = Path.find_first_of("/\\");
|
|
|
|
if (n != static_cast<size_t>(-1))
|
|
|
|
style = (Path[n] == '/') ? llvm::sys::path::Style::posix
|
|
|
|
: llvm::sys::path::Style::windows;
|
2021-01-15 08:33:52 +01:00
|
|
|
return style;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes leading "./" as well as path components like ".." and ".".
|
|
|
|
static llvm::SmallString<256> canonicalize(llvm::StringRef Path) {
|
|
|
|
// First detect the path style in use by checking the first separator.
|
|
|
|
llvm::sys::path::Style style = getExistingStyle(Path);
|
2020-01-22 01:45:51 +01:00
|
|
|
|
|
|
|
// Now remove the dots. Explicitly specifying the path style prevents the
|
|
|
|
// direction of the slashes from changing.
|
|
|
|
llvm::SmallString<256> result =
|
|
|
|
llvm::sys::path::remove_leading_dotslash(Path, style);
|
|
|
|
llvm::sys::path::remove_dots(result, /*remove_dot_dot=*/true, style);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
|
2019-10-16 01:08:57 +02:00
|
|
|
RedirectingFileSystem::RedirectingFileSystem(IntrusiveRefCntPtr<FileSystem> FS)
|
|
|
|
: ExternalFS(std::move(FS)) {
|
|
|
|
if (ExternalFS)
|
|
|
|
if (auto ExternalWorkingDirectory =
|
|
|
|
ExternalFS->getCurrentWorkingDirectory()) {
|
|
|
|
WorkingDirectory = *ExternalWorkingDirectory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 02:44:14 +01:00
|
|
|
/// Directory iterator implementation for \c RedirectingFileSystem's
|
|
|
|
/// directory entries.
|
|
|
|
class llvm::vfs::RedirectingFSDirIterImpl
|
2019-01-15 23:36:41 +01:00
|
|
|
: public llvm::vfs::detail::DirIterImpl {
|
2018-10-10 15:27:25 +02:00
|
|
|
std::string Dir;
|
2021-01-16 02:44:14 +01:00
|
|
|
RedirectingFileSystem::DirectoryEntry::iterator Current, End;
|
2018-10-27 00:14:33 +02:00
|
|
|
|
2021-01-16 02:44:14 +01:00
|
|
|
std::error_code incrementImpl(bool IsFirstTime) {
|
|
|
|
assert((IsFirstTime || Current != End) && "cannot iterate past end");
|
|
|
|
if (!IsFirstTime)
|
|
|
|
++Current;
|
|
|
|
if (Current != End) {
|
|
|
|
SmallString<128> PathStr(Dir);
|
|
|
|
llvm::sys::path::append(PathStr, (*Current)->getName());
|
|
|
|
sys::fs::file_type Type = sys::fs::file_type::type_unknown;
|
|
|
|
switch ((*Current)->getKind()) {
|
|
|
|
case RedirectingFileSystem::EK_Directory:
|
2021-01-15 08:33:52 +01:00
|
|
|
LLVM_FALLTHROUGH;
|
|
|
|
case RedirectingFileSystem::EK_DirectoryRemap:
|
2021-01-16 02:44:14 +01:00
|
|
|
Type = sys::fs::file_type::directory_file;
|
|
|
|
break;
|
|
|
|
case RedirectingFileSystem::EK_File:
|
|
|
|
Type = sys::fs::file_type::regular_file;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
CurrentEntry = directory_entry(std::string(PathStr.str()), Type);
|
|
|
|
} else {
|
|
|
|
CurrentEntry = directory_entry();
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
};
|
2018-10-10 15:27:25 +02:00
|
|
|
|
|
|
|
public:
|
2021-01-16 02:44:14 +01:00
|
|
|
RedirectingFSDirIterImpl(
|
|
|
|
const Twine &Path, RedirectingFileSystem::DirectoryEntry::iterator Begin,
|
|
|
|
RedirectingFileSystem::DirectoryEntry::iterator End, std::error_code &EC)
|
|
|
|
: Dir(Path.str()), Current(Begin), End(End) {
|
|
|
|
EC = incrementImpl(/*IsFirstTime=*/true);
|
|
|
|
}
|
2018-10-10 15:27:25 +02:00
|
|
|
|
2021-01-16 02:44:14 +01:00
|
|
|
std::error_code increment() override {
|
|
|
|
return incrementImpl(/*IsFirstTime=*/false);
|
|
|
|
}
|
2018-10-10 15:27:25 +02:00
|
|
|
};
|
|
|
|
|
2021-01-15 08:33:52 +01:00
|
|
|
/// Directory iterator implementation for \c RedirectingFileSystem's
|
|
|
|
/// directory remap entries that maps the paths reported by the external
|
|
|
|
/// file system's directory iterator back to the virtual directory's path.
|
|
|
|
class RedirectingFSDirRemapIterImpl : public llvm::vfs::detail::DirIterImpl {
|
|
|
|
std::string Dir;
|
|
|
|
llvm::sys::path::Style DirStyle;
|
|
|
|
llvm::vfs::directory_iterator ExternalIter;
|
|
|
|
|
|
|
|
public:
|
|
|
|
RedirectingFSDirRemapIterImpl(std::string DirPath,
|
|
|
|
llvm::vfs::directory_iterator ExtIter)
|
|
|
|
: Dir(std::move(DirPath)), DirStyle(getExistingStyle(Dir)),
|
|
|
|
ExternalIter(ExtIter) {
|
|
|
|
if (ExternalIter != llvm::vfs::directory_iterator())
|
|
|
|
setCurrentEntry();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setCurrentEntry() {
|
|
|
|
StringRef ExternalPath = ExternalIter->path();
|
|
|
|
llvm::sys::path::Style ExternalStyle = getExistingStyle(ExternalPath);
|
|
|
|
StringRef File = llvm::sys::path::filename(ExternalPath, ExternalStyle);
|
|
|
|
|
|
|
|
SmallString<128> NewPath(Dir);
|
|
|
|
llvm::sys::path::append(NewPath, DirStyle, File);
|
|
|
|
|
|
|
|
CurrentEntry = directory_entry(std::string(NewPath), ExternalIter->type());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code increment() override {
|
|
|
|
std::error_code EC;
|
|
|
|
ExternalIter.increment(EC);
|
|
|
|
if (!EC && ExternalIter != llvm::vfs::directory_iterator())
|
|
|
|
setCurrentEntry();
|
|
|
|
else
|
|
|
|
CurrentEntry = directory_entry();
|
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-15 23:36:41 +01:00
|
|
|
llvm::ErrorOr<std::string>
|
|
|
|
RedirectingFileSystem::getCurrentWorkingDirectory() const {
|
2019-10-16 01:08:57 +02:00
|
|
|
return WorkingDirectory;
|
2019-01-15 23:36:41 +01:00
|
|
|
}
|
2018-10-10 15:27:25 +02:00
|
|
|
|
2019-01-15 23:36:41 +01:00
|
|
|
std::error_code
|
|
|
|
RedirectingFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
|
2019-10-16 01:08:57 +02:00
|
|
|
// Don't change the working directory if the path doesn't exist.
|
|
|
|
if (!exists(Path))
|
|
|
|
return errc::no_such_file_or_directory;
|
|
|
|
|
|
|
|
SmallString<128> AbsolutePath;
|
|
|
|
Path.toVector(AbsolutePath);
|
|
|
|
if (std::error_code EC = makeAbsolute(AbsolutePath))
|
|
|
|
return EC;
|
2020-01-28 20:23:46 +01:00
|
|
|
WorkingDirectory = std::string(AbsolutePath.str());
|
2019-10-16 01:08:57 +02:00
|
|
|
return {};
|
2019-01-15 23:36:41 +01:00
|
|
|
}
|
2018-10-10 15:27:25 +02:00
|
|
|
|
2021-01-22 22:51:59 +01:00
|
|
|
std::error_code RedirectingFileSystem::isLocal(const Twine &Path_,
|
2019-01-15 23:36:41 +01:00
|
|
|
bool &Result) {
|
2021-01-22 22:51:59 +01:00
|
|
|
SmallString<256> Path;
|
|
|
|
Path_.toVector(Path);
|
|
|
|
|
|
|
|
if (std::error_code EC = makeCanonical(Path))
|
|
|
|
return {};
|
|
|
|
|
2019-01-15 23:36:41 +01:00
|
|
|
return ExternalFS->isLocal(Path, Result);
|
|
|
|
}
|
2018-11-16 02:15:54 +01:00
|
|
|
|
2019-11-26 00:57:21 +01:00
|
|
|
std::error_code RedirectingFileSystem::makeAbsolute(SmallVectorImpl<char> &Path) const {
|
|
|
|
if (llvm::sys::path::is_absolute(Path, llvm::sys::path::Style::posix) ||
|
|
|
|
llvm::sys::path::is_absolute(Path, llvm::sys::path::Style::windows))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto WorkingDir = getCurrentWorkingDirectory();
|
|
|
|
if (!WorkingDir)
|
|
|
|
return WorkingDir.getError();
|
|
|
|
|
2020-01-22 01:45:51 +01:00
|
|
|
// We can't use sys::fs::make_absolute because that assumes the path style
|
|
|
|
// is native and there is no way to override that. Since we know WorkingDir
|
|
|
|
// is absolute, we can use it to determine which style we actually have and
|
|
|
|
// append Path ourselves.
|
|
|
|
sys::path::Style style = sys::path::Style::windows;
|
|
|
|
if (sys::path::is_absolute(WorkingDir.get(), sys::path::Style::posix)) {
|
|
|
|
style = sys::path::Style::posix;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Result = WorkingDir.get();
|
|
|
|
StringRef Dir(Result);
|
|
|
|
if (!Dir.endswith(sys::path::get_separator(style))) {
|
|
|
|
Result += sys::path::get_separator(style);
|
|
|
|
}
|
|
|
|
Result.append(Path.data(), Path.size());
|
|
|
|
Path.assign(Result.begin(), Result.end());
|
|
|
|
|
2019-11-26 00:57:21 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-01-15 23:36:41 +01:00
|
|
|
directory_iterator RedirectingFileSystem::dir_begin(const Twine &Dir,
|
|
|
|
std::error_code &EC) {
|
2021-01-22 22:51:59 +01:00
|
|
|
SmallString<256> Path;
|
|
|
|
Dir.toVector(Path);
|
|
|
|
|
|
|
|
EC = makeCanonical(Path);
|
|
|
|
if (EC)
|
|
|
|
return {};
|
|
|
|
|
2021-01-15 08:33:52 +01:00
|
|
|
ErrorOr<RedirectingFileSystem::LookupResult> Result = lookupPath(Path);
|
|
|
|
if (!Result) {
|
|
|
|
EC = Result.getError();
|
2021-01-16 02:44:14 +01:00
|
|
|
if (shouldFallBackToExternalFS(EC))
|
2021-01-22 22:51:59 +01:00
|
|
|
return ExternalFS->dir_begin(Path, EC);
|
2019-01-15 23:36:41 +01:00
|
|
|
return {};
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
2021-01-15 08:33:52 +01:00
|
|
|
|
|
|
|
// Use status to make sure the path exists and refers to a directory.
|
|
|
|
ErrorOr<Status> S = status(Path, *Result);
|
2019-01-15 23:36:41 +01:00
|
|
|
if (!S) {
|
2021-01-15 08:33:52 +01:00
|
|
|
if (shouldFallBackToExternalFS(S.getError(), Result->E))
|
|
|
|
return ExternalFS->dir_begin(Dir, EC);
|
2019-01-15 23:36:41 +01:00
|
|
|
EC = S.getError();
|
|
|
|
return {};
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
2019-01-15 23:36:41 +01:00
|
|
|
if (!S->isDirectory()) {
|
|
|
|
EC = std::error_code(static_cast<int>(errc::not_a_directory),
|
|
|
|
std::system_category());
|
|
|
|
return {};
|
2018-11-08 01:01:32 +01:00
|
|
|
}
|
|
|
|
|
2021-01-15 08:33:52 +01:00
|
|
|
// Create the appropriate directory iterator based on whether we found a
|
|
|
|
// DirectoryRemapEntry or DirectoryEntry.
|
|
|
|
directory_iterator DirIter;
|
|
|
|
if (auto ExtRedirect = Result->getExternalRedirect()) {
|
|
|
|
auto RE = cast<RedirectingFileSystem::RemapEntry>(Result->E);
|
|
|
|
DirIter = ExternalFS->dir_begin(*ExtRedirect, EC);
|
|
|
|
|
|
|
|
if (!RE->useExternalName(UseExternalNames)) {
|
|
|
|
// Update the paths in the results to use the virtual directory's path.
|
|
|
|
DirIter =
|
|
|
|
directory_iterator(std::make_shared<RedirectingFSDirRemapIterImpl>(
|
|
|
|
std::string(Path), DirIter));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auto DE = cast<DirectoryEntry>(Result->E);
|
|
|
|
DirIter = directory_iterator(std::make_shared<RedirectingFSDirIterImpl>(
|
|
|
|
Path, DE->contents_begin(), DE->contents_end(), EC));
|
|
|
|
}
|
2021-01-16 02:44:14 +01:00
|
|
|
|
|
|
|
if (!shouldUseExternalFS())
|
|
|
|
return DirIter;
|
|
|
|
return directory_iterator(std::make_shared<CombiningDirIterImpl>(
|
|
|
|
DirIter, ExternalFS, std::string(Path), EC));
|
2019-01-15 23:36:41 +01:00
|
|
|
}
|
2018-10-10 15:27:25 +02:00
|
|
|
|
2019-01-15 23:36:41 +01:00
|
|
|
void RedirectingFileSystem::setExternalContentsPrefixDir(StringRef PrefixDir) {
|
|
|
|
ExternalContentsPrefixDir = PrefixDir.str();
|
|
|
|
}
|
2018-10-10 15:27:25 +02:00
|
|
|
|
2019-01-15 23:36:41 +01:00
|
|
|
StringRef RedirectingFileSystem::getExternalContentsPrefixDir() const {
|
|
|
|
return ExternalContentsPrefixDir;
|
|
|
|
}
|
2018-10-10 15:27:25 +02:00
|
|
|
|
2020-09-01 00:13:49 +02:00
|
|
|
void RedirectingFileSystem::setFallthrough(bool Fallthrough) {
|
|
|
|
IsFallthrough = Fallthrough;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<StringRef> RedirectingFileSystem::getRoots() const {
|
|
|
|
std::vector<StringRef> R;
|
|
|
|
for (const auto &Root : Roots)
|
|
|
|
R.push_back(Root->getName());
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2019-09-14 01:27:31 +02:00
|
|
|
void RedirectingFileSystem::dump(raw_ostream &OS) const {
|
2019-01-15 23:36:41 +01:00
|
|
|
for (const auto &Root : Roots)
|
2019-09-14 01:27:31 +02:00
|
|
|
dumpEntry(OS, Root.get());
|
2019-01-15 23:36:41 +01:00
|
|
|
}
|
2018-10-10 15:27:25 +02:00
|
|
|
|
2019-09-14 01:27:31 +02:00
|
|
|
void RedirectingFileSystem::dumpEntry(raw_ostream &OS,
|
|
|
|
RedirectingFileSystem::Entry *E,
|
|
|
|
int NumSpaces) const {
|
2019-01-15 23:36:41 +01:00
|
|
|
StringRef Name = E->getName();
|
|
|
|
for (int i = 0, e = NumSpaces; i < e; ++i)
|
2019-09-14 01:27:31 +02:00
|
|
|
OS << " ";
|
|
|
|
OS << "'" << Name.str().c_str() << "'"
|
|
|
|
<< "\n";
|
2018-10-10 15:27:25 +02:00
|
|
|
|
2019-01-15 23:36:41 +01:00
|
|
|
if (E->getKind() == RedirectingFileSystem::EK_Directory) {
|
2021-01-16 02:44:14 +01:00
|
|
|
auto *DE = dyn_cast<RedirectingFileSystem::DirectoryEntry>(E);
|
2019-01-15 23:36:41 +01:00
|
|
|
assert(DE && "Should be a directory");
|
2018-10-10 15:27:25 +02:00
|
|
|
|
2019-01-15 23:36:41 +01:00
|
|
|
for (std::unique_ptr<Entry> &SubEntry :
|
|
|
|
llvm::make_range(DE->contents_begin(), DE->contents_end()))
|
2019-09-14 01:27:31 +02:00
|
|
|
dumpEntry(OS, SubEntry.get(), NumSpaces + 2);
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
2019-01-15 23:36:41 +01:00
|
|
|
}
|
2019-09-14 01:27:31 +02:00
|
|
|
|
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
|
|
|
LLVM_DUMP_METHOD void RedirectingFileSystem::dump() const { dump(dbgs()); }
|
2018-10-10 15:27:25 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/// A helper class to hold the common YAML parsing state.
|
2019-01-15 23:36:41 +01:00
|
|
|
class llvm::vfs::RedirectingFileSystemParser {
|
2018-10-10 15:27:25 +02:00
|
|
|
yaml::Stream &Stream;
|
|
|
|
|
|
|
|
void error(yaml::Node *N, const Twine &Msg) { Stream.printError(N, Msg); }
|
|
|
|
|
|
|
|
// false on error
|
|
|
|
bool parseScalarString(yaml::Node *N, StringRef &Result,
|
|
|
|
SmallVectorImpl<char> &Storage) {
|
|
|
|
const auto *S = dyn_cast<yaml::ScalarNode>(N);
|
|
|
|
|
|
|
|
if (!S) {
|
|
|
|
error(N, "expected string");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Result = S->getValue(Storage);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// false on error
|
|
|
|
bool parseScalarBool(yaml::Node *N, bool &Result) {
|
|
|
|
SmallString<5> Storage;
|
|
|
|
StringRef Value;
|
|
|
|
if (!parseScalarString(N, Value, Storage))
|
|
|
|
return false;
|
|
|
|
|
2021-06-23 13:52:36 +02:00
|
|
|
if (Value.equals_insensitive("true") || Value.equals_insensitive("on") ||
|
|
|
|
Value.equals_insensitive("yes") || Value == "1") {
|
2018-10-10 15:27:25 +02:00
|
|
|
Result = true;
|
|
|
|
return true;
|
2021-06-23 13:52:36 +02:00
|
|
|
} else if (Value.equals_insensitive("false") ||
|
|
|
|
Value.equals_insensitive("off") ||
|
|
|
|
Value.equals_insensitive("no") || Value == "0") {
|
2018-10-10 15:27:25 +02:00
|
|
|
Result = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
error(N, "expected boolean value");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct KeyStatus {
|
|
|
|
bool Required;
|
|
|
|
bool Seen = false;
|
|
|
|
|
|
|
|
KeyStatus(bool Required = false) : Required(Required) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
using KeyStatusPair = std::pair<StringRef, KeyStatus>;
|
|
|
|
|
|
|
|
// false on error
|
|
|
|
bool checkDuplicateOrUnknownKey(yaml::Node *KeyNode, StringRef Key,
|
|
|
|
DenseMap<StringRef, KeyStatus> &Keys) {
|
|
|
|
if (!Keys.count(Key)) {
|
|
|
|
error(KeyNode, "unknown key");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
KeyStatus &S = Keys[Key];
|
|
|
|
if (S.Seen) {
|
|
|
|
error(KeyNode, Twine("duplicate key '") + Key + "'");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
S.Seen = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// false on error
|
|
|
|
bool checkMissingKeys(yaml::Node *Obj, DenseMap<StringRef, KeyStatus> &Keys) {
|
|
|
|
for (const auto &I : Keys) {
|
|
|
|
if (I.second.Required && !I.second.Seen) {
|
|
|
|
error(Obj, Twine("missing key '") + I.first + "'");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-12 05:03:38 +01:00
|
|
|
public:
|
|
|
|
static RedirectingFileSystem::Entry *
|
2019-01-15 23:36:41 +01:00
|
|
|
lookupOrCreateEntry(RedirectingFileSystem *FS, StringRef Name,
|
|
|
|
RedirectingFileSystem::Entry *ParentEntry = nullptr) {
|
2018-10-10 15:27:25 +02:00
|
|
|
if (!ParentEntry) { // Look for a existent root
|
|
|
|
for (const auto &Root : FS->Roots) {
|
|
|
|
if (Name.equals(Root->getName())) {
|
|
|
|
ParentEntry = Root.get();
|
|
|
|
return ParentEntry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // Advance to the next component
|
2021-01-16 02:44:14 +01:00
|
|
|
auto *DE = dyn_cast<RedirectingFileSystem::DirectoryEntry>(ParentEntry);
|
2019-01-15 23:36:41 +01:00
|
|
|
for (std::unique_ptr<RedirectingFileSystem::Entry> &Content :
|
2018-10-10 15:27:25 +02:00
|
|
|
llvm::make_range(DE->contents_begin(), DE->contents_end())) {
|
2019-01-15 23:36:41 +01:00
|
|
|
auto *DirContent =
|
2021-01-16 02:44:14 +01:00
|
|
|
dyn_cast<RedirectingFileSystem::DirectoryEntry>(Content.get());
|
2018-10-10 15:27:25 +02:00
|
|
|
if (DirContent && Name.equals(Content->getName()))
|
|
|
|
return DirContent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ... or create a new one
|
2019-01-15 23:36:41 +01:00
|
|
|
std::unique_ptr<RedirectingFileSystem::Entry> E =
|
2021-01-16 02:44:14 +01:00
|
|
|
std::make_unique<RedirectingFileSystem::DirectoryEntry>(
|
2019-01-15 23:36:41 +01:00
|
|
|
Name, Status("", getNextVirtualUniqueID(),
|
|
|
|
std::chrono::system_clock::now(), 0, 0, 0,
|
|
|
|
file_type::directory_file, sys::fs::all_all));
|
2018-10-10 15:27:25 +02:00
|
|
|
|
|
|
|
if (!ParentEntry) { // Add a new root to the overlay
|
|
|
|
FS->Roots.push_back(std::move(E));
|
|
|
|
ParentEntry = FS->Roots.back().get();
|
|
|
|
return ParentEntry;
|
|
|
|
}
|
|
|
|
|
2021-01-16 02:44:14 +01:00
|
|
|
auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(ParentEntry);
|
2018-10-10 15:27:25 +02:00
|
|
|
DE->addContent(std::move(E));
|
|
|
|
return DE->getLastContent();
|
|
|
|
}
|
|
|
|
|
2020-11-12 05:03:38 +01:00
|
|
|
private:
|
2019-01-15 23:36:41 +01:00
|
|
|
void uniqueOverlayTree(RedirectingFileSystem *FS,
|
|
|
|
RedirectingFileSystem::Entry *SrcE,
|
|
|
|
RedirectingFileSystem::Entry *NewParentE = nullptr) {
|
2018-10-10 15:27:25 +02:00
|
|
|
StringRef Name = SrcE->getName();
|
|
|
|
switch (SrcE->getKind()) {
|
2019-01-15 23:36:41 +01:00
|
|
|
case RedirectingFileSystem::EK_Directory: {
|
2021-01-16 02:44:14 +01:00
|
|
|
auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(SrcE);
|
2018-10-10 15:27:25 +02:00
|
|
|
// Empty directories could be present in the YAML as a way to
|
|
|
|
// describe a file for a current directory after some of its subdir
|
|
|
|
// is parsed. This only leads to redundant walks, ignore it.
|
|
|
|
if (!Name.empty())
|
|
|
|
NewParentE = lookupOrCreateEntry(FS, Name, NewParentE);
|
2019-01-15 23:36:41 +01:00
|
|
|
for (std::unique_ptr<RedirectingFileSystem::Entry> &SubEntry :
|
2018-10-10 15:27:25 +02:00
|
|
|
llvm::make_range(DE->contents_begin(), DE->contents_end()))
|
|
|
|
uniqueOverlayTree(FS, SubEntry.get(), NewParentE);
|
|
|
|
break;
|
|
|
|
}
|
2021-01-15 08:33:52 +01:00
|
|
|
case RedirectingFileSystem::EK_DirectoryRemap: {
|
|
|
|
assert(NewParentE && "Parent entry must exist");
|
|
|
|
auto *DR = cast<RedirectingFileSystem::DirectoryRemapEntry>(SrcE);
|
|
|
|
auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(NewParentE);
|
|
|
|
DE->addContent(
|
|
|
|
std::make_unique<RedirectingFileSystem::DirectoryRemapEntry>(
|
|
|
|
Name, DR->getExternalContentsPath(), DR->getUseName()));
|
|
|
|
break;
|
|
|
|
}
|
2019-01-15 23:36:41 +01:00
|
|
|
case RedirectingFileSystem::EK_File: {
|
2018-10-10 15:27:25 +02:00
|
|
|
assert(NewParentE && "Parent entry must exist");
|
2021-01-16 02:44:14 +01:00
|
|
|
auto *FE = cast<RedirectingFileSystem::FileEntry>(SrcE);
|
|
|
|
auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(NewParentE);
|
|
|
|
DE->addContent(std::make_unique<RedirectingFileSystem::FileEntry>(
|
|
|
|
Name, FE->getExternalContentsPath(), FE->getUseName()));
|
2018-10-10 15:27:25 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 23:36:41 +01:00
|
|
|
std::unique_ptr<RedirectingFileSystem::Entry>
|
|
|
|
parseEntry(yaml::Node *N, RedirectingFileSystem *FS, bool IsRootEntry) {
|
2018-10-10 15:27:25 +02:00
|
|
|
auto *M = dyn_cast<yaml::MappingNode>(N);
|
|
|
|
if (!M) {
|
|
|
|
error(N, "expected mapping node for file or directory entry");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyStatusPair Fields[] = {
|
|
|
|
KeyStatusPair("name", true),
|
|
|
|
KeyStatusPair("type", true),
|
|
|
|
KeyStatusPair("contents", false),
|
|
|
|
KeyStatusPair("external-contents", false),
|
|
|
|
KeyStatusPair("use-external-name", false),
|
|
|
|
};
|
|
|
|
|
|
|
|
DenseMap<StringRef, KeyStatus> Keys(std::begin(Fields), std::end(Fields));
|
|
|
|
|
2021-01-15 08:33:52 +01:00
|
|
|
enum { CF_NotSet, CF_List, CF_External } ContentsField = CF_NotSet;
|
2019-01-15 23:36:41 +01:00
|
|
|
std::vector<std::unique_ptr<RedirectingFileSystem::Entry>>
|
|
|
|
EntryArrayContents;
|
2020-01-22 01:45:51 +01:00
|
|
|
SmallString<256> ExternalContentsPath;
|
|
|
|
SmallString<256> Name;
|
2019-05-22 13:20:52 +02:00
|
|
|
yaml::Node *NameValueNode = nullptr;
|
2021-01-15 08:33:52 +01:00
|
|
|
auto UseExternalName = RedirectingFileSystem::NK_NotSet;
|
2019-01-15 23:36:41 +01:00
|
|
|
RedirectingFileSystem::EntryKind Kind;
|
2018-10-10 15:27:25 +02:00
|
|
|
|
|
|
|
for (auto &I : *M) {
|
|
|
|
StringRef Key;
|
|
|
|
// Reuse the buffer for key and value, since we don't look at key after
|
|
|
|
// parsing value.
|
|
|
|
SmallString<256> Buffer;
|
|
|
|
if (!parseScalarString(I.getKey(), Key, Buffer))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (!checkDuplicateOrUnknownKey(I.getKey(), Key, Keys))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
StringRef Value;
|
|
|
|
if (Key == "name") {
|
|
|
|
if (!parseScalarString(I.getValue(), Value, Buffer))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
NameValueNode = I.getValue();
|
2020-01-22 01:45:51 +01:00
|
|
|
// Guarantee that old YAML files containing paths with ".." and "."
|
|
|
|
// are properly canonicalized before read into the VFS.
|
|
|
|
Name = canonicalize(Value).str();
|
2018-10-10 15:27:25 +02:00
|
|
|
} else if (Key == "type") {
|
|
|
|
if (!parseScalarString(I.getValue(), Value, Buffer))
|
|
|
|
return nullptr;
|
|
|
|
if (Value == "file")
|
2019-01-15 23:36:41 +01:00
|
|
|
Kind = RedirectingFileSystem::EK_File;
|
2018-10-10 15:27:25 +02:00
|
|
|
else if (Value == "directory")
|
2019-01-15 23:36:41 +01:00
|
|
|
Kind = RedirectingFileSystem::EK_Directory;
|
2021-01-15 08:33:52 +01:00
|
|
|
else if (Value == "directory-remap")
|
|
|
|
Kind = RedirectingFileSystem::EK_DirectoryRemap;
|
2018-10-10 15:27:25 +02:00
|
|
|
else {
|
|
|
|
error(I.getValue(), "unknown value for 'type'");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else if (Key == "contents") {
|
2021-01-15 08:33:52 +01:00
|
|
|
if (ContentsField != CF_NotSet) {
|
2018-10-10 15:27:25 +02:00
|
|
|
error(I.getKey(),
|
|
|
|
"entry already has 'contents' or 'external-contents'");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-01-15 08:33:52 +01:00
|
|
|
ContentsField = CF_List;
|
2018-10-10 15:27:25 +02:00
|
|
|
auto *Contents = dyn_cast<yaml::SequenceNode>(I.getValue());
|
|
|
|
if (!Contents) {
|
|
|
|
// FIXME: this is only for directories, what about files?
|
|
|
|
error(I.getValue(), "expected array");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &I : *Contents) {
|
2019-01-15 23:36:41 +01:00
|
|
|
if (std::unique_ptr<RedirectingFileSystem::Entry> E =
|
2018-10-10 15:27:25 +02:00
|
|
|
parseEntry(&I, FS, /*IsRootEntry*/ false))
|
|
|
|
EntryArrayContents.push_back(std::move(E));
|
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else if (Key == "external-contents") {
|
2021-01-15 08:33:52 +01:00
|
|
|
if (ContentsField != CF_NotSet) {
|
2018-10-10 15:27:25 +02:00
|
|
|
error(I.getKey(),
|
|
|
|
"entry already has 'contents' or 'external-contents'");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-01-15 08:33:52 +01:00
|
|
|
ContentsField = CF_External;
|
2018-10-10 15:27:25 +02:00
|
|
|
if (!parseScalarString(I.getValue(), Value, Buffer))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
SmallString<256> FullPath;
|
|
|
|
if (FS->IsRelativeOverlay) {
|
|
|
|
FullPath = FS->getExternalContentsPrefixDir();
|
|
|
|
assert(!FullPath.empty() &&
|
|
|
|
"External contents prefix directory must exist");
|
|
|
|
llvm::sys::path::append(FullPath, Value);
|
|
|
|
} else {
|
|
|
|
FullPath = Value;
|
|
|
|
}
|
|
|
|
|
2020-01-22 01:45:51 +01:00
|
|
|
// Guarantee that old YAML files containing paths with ".." and "."
|
|
|
|
// are properly canonicalized before read into the VFS.
|
|
|
|
FullPath = canonicalize(FullPath);
|
|
|
|
ExternalContentsPath = FullPath.str();
|
2018-10-10 15:27:25 +02:00
|
|
|
} else if (Key == "use-external-name") {
|
|
|
|
bool Val;
|
|
|
|
if (!parseScalarBool(I.getValue(), Val))
|
|
|
|
return nullptr;
|
2021-01-15 08:33:52 +01:00
|
|
|
UseExternalName = Val ? RedirectingFileSystem::NK_External
|
|
|
|
: RedirectingFileSystem::NK_Virtual;
|
2018-10-10 15:27:25 +02:00
|
|
|
} else {
|
|
|
|
llvm_unreachable("key missing from Keys");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Stream.failed())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// check for missing keys
|
2021-01-15 08:33:52 +01:00
|
|
|
if (ContentsField == CF_NotSet) {
|
2018-10-10 15:27:25 +02:00
|
|
|
error(N, "missing key 'contents' or 'external-contents'");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!checkMissingKeys(N, Keys))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// check invalid configuration
|
2019-01-15 23:36:41 +01:00
|
|
|
if (Kind == RedirectingFileSystem::EK_Directory &&
|
2021-01-15 08:33:52 +01:00
|
|
|
UseExternalName != RedirectingFileSystem::NK_NotSet) {
|
|
|
|
error(N, "'use-external-name' is not supported for 'directory' entries");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Kind == RedirectingFileSystem::EK_DirectoryRemap &&
|
|
|
|
ContentsField == CF_List) {
|
|
|
|
error(N, "'contents' is not supported for 'directory-remap' entries");
|
2018-10-10 15:27:25 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-11-26 00:57:21 +01:00
|
|
|
sys::path::Style path_style = sys::path::Style::native;
|
|
|
|
if (IsRootEntry) {
|
|
|
|
// VFS root entries may be in either Posix or Windows style. Figure out
|
|
|
|
// which style we have, and use it consistently.
|
|
|
|
if (sys::path::is_absolute(Name, sys::path::Style::posix)) {
|
|
|
|
path_style = sys::path::Style::posix;
|
|
|
|
} else if (sys::path::is_absolute(Name, sys::path::Style::windows)) {
|
|
|
|
path_style = sys::path::Style::windows;
|
|
|
|
} else {
|
|
|
|
assert(NameValueNode && "Name presence should be checked earlier");
|
|
|
|
error(NameValueNode,
|
|
|
|
"entry with relative path at the root level is not discoverable");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove trailing slash(es), being careful not to remove the root path
|
2021-07-08 22:30:14 +02:00
|
|
|
StringRef Trimmed = Name;
|
2019-11-26 00:57:21 +01:00
|
|
|
size_t RootPathLen = sys::path::root_path(Trimmed, path_style).size();
|
2018-10-10 15:27:25 +02:00
|
|
|
while (Trimmed.size() > RootPathLen &&
|
2019-11-26 00:57:21 +01:00
|
|
|
sys::path::is_separator(Trimmed.back(), path_style))
|
2018-10-10 15:27:25 +02:00
|
|
|
Trimmed = Trimmed.slice(0, Trimmed.size() - 1);
|
2019-11-26 00:57:21 +01:00
|
|
|
|
2018-10-10 15:27:25 +02:00
|
|
|
// Get the last component
|
2019-11-26 00:57:21 +01:00
|
|
|
StringRef LastComponent = sys::path::filename(Trimmed, path_style);
|
2018-10-10 15:27:25 +02:00
|
|
|
|
2019-01-15 23:36:41 +01:00
|
|
|
std::unique_ptr<RedirectingFileSystem::Entry> Result;
|
2018-10-10 15:27:25 +02:00
|
|
|
switch (Kind) {
|
2019-01-15 23:36:41 +01:00
|
|
|
case RedirectingFileSystem::EK_File:
|
2021-01-16 02:44:14 +01:00
|
|
|
Result = std::make_unique<RedirectingFileSystem::FileEntry>(
|
2018-10-10 15:27:25 +02:00
|
|
|
LastComponent, std::move(ExternalContentsPath), UseExternalName);
|
|
|
|
break;
|
2021-01-15 08:33:52 +01:00
|
|
|
case RedirectingFileSystem::EK_DirectoryRemap:
|
|
|
|
Result = std::make_unique<RedirectingFileSystem::DirectoryRemapEntry>(
|
|
|
|
LastComponent, std::move(ExternalContentsPath), UseExternalName);
|
|
|
|
break;
|
2019-01-15 23:36:41 +01:00
|
|
|
case RedirectingFileSystem::EK_Directory:
|
2021-01-16 02:44:14 +01:00
|
|
|
Result = std::make_unique<RedirectingFileSystem::DirectoryEntry>(
|
|
|
|
LastComponent, std::move(EntryArrayContents),
|
|
|
|
Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(),
|
|
|
|
0, 0, 0, file_type::directory_file, sys::fs::all_all));
|
2018-10-10 15:27:25 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-11-26 00:57:21 +01:00
|
|
|
StringRef Parent = sys::path::parent_path(Trimmed, path_style);
|
2018-10-10 15:27:25 +02:00
|
|
|
if (Parent.empty())
|
|
|
|
return Result;
|
|
|
|
|
|
|
|
// if 'name' contains multiple components, create implicit directory entries
|
2019-11-26 00:57:21 +01:00
|
|
|
for (sys::path::reverse_iterator I = sys::path::rbegin(Parent, path_style),
|
2018-10-10 15:27:25 +02:00
|
|
|
E = sys::path::rend(Parent);
|
|
|
|
I != E; ++I) {
|
2019-01-15 23:36:41 +01:00
|
|
|
std::vector<std::unique_ptr<RedirectingFileSystem::Entry>> Entries;
|
2018-10-10 15:27:25 +02:00
|
|
|
Entries.push_back(std::move(Result));
|
2021-01-16 02:44:14 +01:00
|
|
|
Result = std::make_unique<RedirectingFileSystem::DirectoryEntry>(
|
|
|
|
*I, std::move(Entries),
|
|
|
|
Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(),
|
|
|
|
0, 0, 0, file_type::directory_file, sys::fs::all_all));
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
RedirectingFileSystemParser(yaml::Stream &S) : Stream(S) {}
|
|
|
|
|
|
|
|
// false on error
|
|
|
|
bool parse(yaml::Node *Root, RedirectingFileSystem *FS) {
|
|
|
|
auto *Top = dyn_cast<yaml::MappingNode>(Root);
|
|
|
|
if (!Top) {
|
|
|
|
error(Root, "expected mapping node");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyStatusPair Fields[] = {
|
|
|
|
KeyStatusPair("version", true),
|
|
|
|
KeyStatusPair("case-sensitive", false),
|
|
|
|
KeyStatusPair("use-external-names", false),
|
|
|
|
KeyStatusPair("overlay-relative", false),
|
2018-10-27 00:14:33 +02:00
|
|
|
KeyStatusPair("fallthrough", false),
|
2018-10-10 15:27:25 +02:00
|
|
|
KeyStatusPair("roots", true),
|
|
|
|
};
|
|
|
|
|
|
|
|
DenseMap<StringRef, KeyStatus> Keys(std::begin(Fields), std::end(Fields));
|
2019-01-15 23:36:41 +01:00
|
|
|
std::vector<std::unique_ptr<RedirectingFileSystem::Entry>> RootEntries;
|
2018-10-10 15:27:25 +02:00
|
|
|
|
|
|
|
// Parse configuration and 'roots'
|
|
|
|
for (auto &I : *Top) {
|
|
|
|
SmallString<10> KeyBuffer;
|
|
|
|
StringRef Key;
|
|
|
|
if (!parseScalarString(I.getKey(), Key, KeyBuffer))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!checkDuplicateOrUnknownKey(I.getKey(), Key, Keys))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (Key == "roots") {
|
|
|
|
auto *Roots = dyn_cast<yaml::SequenceNode>(I.getValue());
|
|
|
|
if (!Roots) {
|
|
|
|
error(I.getValue(), "expected array");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &I : *Roots) {
|
2019-01-15 23:36:41 +01:00
|
|
|
if (std::unique_ptr<RedirectingFileSystem::Entry> E =
|
2018-10-10 15:27:25 +02:00
|
|
|
parseEntry(&I, FS, /*IsRootEntry*/ true))
|
|
|
|
RootEntries.push_back(std::move(E));
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (Key == "version") {
|
|
|
|
StringRef VersionString;
|
|
|
|
SmallString<4> Storage;
|
|
|
|
if (!parseScalarString(I.getValue(), VersionString, Storage))
|
|
|
|
return false;
|
|
|
|
int Version;
|
|
|
|
if (VersionString.getAsInteger<int>(10, Version)) {
|
|
|
|
error(I.getValue(), "expected integer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (Version < 0) {
|
|
|
|
error(I.getValue(), "invalid version number");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (Version != 0) {
|
|
|
|
error(I.getValue(), "version mismatch, expected 0");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (Key == "case-sensitive") {
|
|
|
|
if (!parseScalarBool(I.getValue(), FS->CaseSensitive))
|
|
|
|
return false;
|
|
|
|
} else if (Key == "overlay-relative") {
|
|
|
|
if (!parseScalarBool(I.getValue(), FS->IsRelativeOverlay))
|
|
|
|
return false;
|
|
|
|
} else if (Key == "use-external-names") {
|
|
|
|
if (!parseScalarBool(I.getValue(), FS->UseExternalNames))
|
|
|
|
return false;
|
2018-10-27 00:14:33 +02:00
|
|
|
} else if (Key == "fallthrough") {
|
|
|
|
if (!parseScalarBool(I.getValue(), FS->IsFallthrough))
|
|
|
|
return false;
|
2018-10-10 15:27:25 +02:00
|
|
|
} else {
|
|
|
|
llvm_unreachable("key missing from Keys");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Stream.failed())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!checkMissingKeys(Top, Keys))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Now that we sucessefully parsed the YAML file, canonicalize the internal
|
|
|
|
// representation to a proper directory tree so that we can search faster
|
|
|
|
// inside the VFS.
|
|
|
|
for (auto &E : RootEntries)
|
|
|
|
uniqueOverlayTree(FS, E.get());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-08 23:58:46 +01:00
|
|
|
std::unique_ptr<RedirectingFileSystem>
|
2018-10-10 15:27:25 +02:00
|
|
|
RedirectingFileSystem::create(std::unique_ptr<MemoryBuffer> Buffer,
|
|
|
|
SourceMgr::DiagHandlerTy DiagHandler,
|
|
|
|
StringRef YAMLFilePath, void *DiagContext,
|
|
|
|
IntrusiveRefCntPtr<FileSystem> ExternalFS) {
|
|
|
|
SourceMgr SM;
|
|
|
|
yaml::Stream Stream(Buffer->getMemBufferRef(), SM);
|
|
|
|
|
|
|
|
SM.setDiagHandler(DiagHandler, DiagContext);
|
|
|
|
yaml::document_iterator DI = Stream.begin();
|
|
|
|
yaml::Node *Root = DI->getRoot();
|
|
|
|
if (DI == Stream.end() || !Root) {
|
|
|
|
SM.PrintMessage(SMLoc(), SourceMgr::DK_Error, "expected root node");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RedirectingFileSystemParser P(Stream);
|
|
|
|
|
|
|
|
std::unique_ptr<RedirectingFileSystem> FS(
|
2019-10-16 01:08:57 +02:00
|
|
|
new RedirectingFileSystem(ExternalFS));
|
2018-10-10 15:27:25 +02:00
|
|
|
|
|
|
|
if (!YAMLFilePath.empty()) {
|
|
|
|
// Use the YAML path from -ivfsoverlay to compute the dir to be prefixed
|
|
|
|
// to each 'external-contents' path.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
// -ivfsoverlay dummy.cache/vfs/vfs.yaml
|
|
|
|
// yields:
|
|
|
|
// FS->ExternalContentsPrefixDir => /<absolute_path_to>/dummy.cache/vfs
|
|
|
|
//
|
|
|
|
SmallString<256> OverlayAbsDir = sys::path::parent_path(YAMLFilePath);
|
|
|
|
std::error_code EC = llvm::sys::fs::make_absolute(OverlayAbsDir);
|
|
|
|
assert(!EC && "Overlay dir final path must be absolute");
|
|
|
|
(void)EC;
|
|
|
|
FS->setExternalContentsPrefixDir(OverlayAbsDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!P.parse(Root, FS.get()))
|
|
|
|
return nullptr;
|
|
|
|
|
2020-12-08 23:58:46 +01:00
|
|
|
return FS;
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 05:03:38 +01:00
|
|
|
std::unique_ptr<RedirectingFileSystem> RedirectingFileSystem::create(
|
|
|
|
ArrayRef<std::pair<std::string, std::string>> RemappedFiles,
|
|
|
|
bool UseExternalNames, FileSystem &ExternalFS) {
|
|
|
|
std::unique_ptr<RedirectingFileSystem> FS(
|
|
|
|
new RedirectingFileSystem(&ExternalFS));
|
|
|
|
FS->UseExternalNames = UseExternalNames;
|
|
|
|
|
|
|
|
StringMap<RedirectingFileSystem::Entry *> Entries;
|
|
|
|
|
|
|
|
for (auto &Mapping : llvm::reverse(RemappedFiles)) {
|
|
|
|
SmallString<128> From = StringRef(Mapping.first);
|
|
|
|
SmallString<128> To = StringRef(Mapping.second);
|
|
|
|
{
|
|
|
|
auto EC = ExternalFS.makeAbsolute(From);
|
|
|
|
(void)EC;
|
|
|
|
assert(!EC && "Could not make absolute path");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we've already mapped this file. The first one we see (in the
|
|
|
|
// reverse iteration) wins.
|
|
|
|
RedirectingFileSystem::Entry *&ToEntry = Entries[From];
|
|
|
|
if (ToEntry)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Add parent directories.
|
|
|
|
RedirectingFileSystem::Entry *Parent = nullptr;
|
|
|
|
StringRef FromDirectory = llvm::sys::path::parent_path(From);
|
|
|
|
for (auto I = llvm::sys::path::begin(FromDirectory),
|
|
|
|
E = llvm::sys::path::end(FromDirectory);
|
|
|
|
I != E; ++I) {
|
|
|
|
Parent = RedirectingFileSystemParser::lookupOrCreateEntry(FS.get(), *I,
|
|
|
|
Parent);
|
|
|
|
}
|
|
|
|
assert(Parent && "File without a directory?");
|
|
|
|
{
|
|
|
|
auto EC = ExternalFS.makeAbsolute(To);
|
|
|
|
(void)EC;
|
|
|
|
assert(!EC && "Could not make absolute path");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the file.
|
2021-01-16 02:44:14 +01:00
|
|
|
auto NewFile = std::make_unique<RedirectingFileSystem::FileEntry>(
|
|
|
|
llvm::sys::path::filename(From), To,
|
2021-01-15 08:33:52 +01:00
|
|
|
UseExternalNames ? RedirectingFileSystem::NK_External
|
|
|
|
: RedirectingFileSystem::NK_Virtual);
|
2020-11-12 05:03:38 +01:00
|
|
|
ToEntry = NewFile.get();
|
2021-01-16 02:44:14 +01:00
|
|
|
cast<RedirectingFileSystem::DirectoryEntry>(Parent)->addContent(
|
2020-11-12 05:03:38 +01:00
|
|
|
std::move(NewFile));
|
|
|
|
}
|
|
|
|
|
|
|
|
return FS;
|
|
|
|
}
|
|
|
|
|
2021-01-15 08:33:52 +01:00
|
|
|
RedirectingFileSystem::LookupResult::LookupResult(
|
|
|
|
Entry *E, sys::path::const_iterator Start, sys::path::const_iterator End)
|
|
|
|
: E(E) {
|
|
|
|
assert(E != nullptr);
|
|
|
|
// If the matched entry is a DirectoryRemapEntry, set ExternalRedirect to the
|
|
|
|
// path of the directory it maps to in the external file system plus any
|
|
|
|
// remaining path components in the provided iterator.
|
|
|
|
if (auto *DRE = dyn_cast<RedirectingFileSystem::DirectoryRemapEntry>(E)) {
|
|
|
|
SmallString<256> Redirect(DRE->getExternalContentsPath());
|
|
|
|
sys::path::append(Redirect, Start, End,
|
|
|
|
getExistingStyle(DRE->getExternalContentsPath()));
|
|
|
|
ExternalRedirect = std::string(Redirect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 02:44:14 +01:00
|
|
|
bool RedirectingFileSystem::shouldFallBackToExternalFS(
|
2021-01-15 08:33:52 +01:00
|
|
|
std::error_code EC, RedirectingFileSystem::Entry *E) const {
|
|
|
|
if (E && !isa<RedirectingFileSystem::DirectoryRemapEntry>(E))
|
|
|
|
return false;
|
2021-01-16 02:44:14 +01:00
|
|
|
return shouldUseExternalFS() && EC == llvm::errc::no_such_file_or_directory;
|
2021-01-30 07:52:43 +01:00
|
|
|
}
|
2021-01-16 02:44:14 +01:00
|
|
|
|
2021-01-22 22:51:59 +01:00
|
|
|
std::error_code
|
|
|
|
RedirectingFileSystem::makeCanonical(SmallVectorImpl<char> &Path) const {
|
2018-10-10 15:27:25 +02:00
|
|
|
if (std::error_code EC = makeAbsolute(Path))
|
|
|
|
return EC;
|
|
|
|
|
2021-01-22 22:51:59 +01:00
|
|
|
llvm::SmallString<256> CanonicalPath =
|
|
|
|
canonicalize(StringRef(Path.data(), Path.size()));
|
|
|
|
if (CanonicalPath.empty())
|
2018-10-10 15:27:25 +02:00
|
|
|
return make_error_code(llvm::errc::invalid_argument);
|
|
|
|
|
2021-01-22 22:51:59 +01:00
|
|
|
Path.assign(CanonicalPath.begin(), CanonicalPath.end());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-01-15 08:33:52 +01:00
|
|
|
ErrorOr<RedirectingFileSystem::LookupResult>
|
2021-01-22 22:51:59 +01:00
|
|
|
RedirectingFileSystem::lookupPath(StringRef Path) const {
|
2018-10-10 15:27:25 +02:00
|
|
|
sys::path::const_iterator Start = sys::path::begin(Path);
|
|
|
|
sys::path::const_iterator End = sys::path::end(Path);
|
|
|
|
for (const auto &Root : Roots) {
|
2021-01-15 08:33:52 +01:00
|
|
|
ErrorOr<RedirectingFileSystem::LookupResult> Result =
|
|
|
|
lookupPathImpl(Start, End, Root.get());
|
2018-10-10 15:27:25 +02:00
|
|
|
if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
return make_error_code(llvm::errc::no_such_file_or_directory);
|
|
|
|
}
|
|
|
|
|
2021-01-15 08:33:52 +01:00
|
|
|
ErrorOr<RedirectingFileSystem::LookupResult>
|
|
|
|
RedirectingFileSystem::lookupPathImpl(
|
|
|
|
sys::path::const_iterator Start, sys::path::const_iterator End,
|
|
|
|
RedirectingFileSystem::Entry *From) const {
|
2018-10-10 15:27:25 +02:00
|
|
|
assert(!isTraversalComponent(*Start) &&
|
|
|
|
!isTraversalComponent(From->getName()) &&
|
|
|
|
"Paths should not contain traversal components");
|
|
|
|
|
|
|
|
StringRef FromName = From->getName();
|
|
|
|
|
|
|
|
// Forward the search to the next component in case this is an empty one.
|
|
|
|
if (!FromName.empty()) {
|
2019-11-07 19:50:33 +01:00
|
|
|
if (!pathComponentMatches(*Start, FromName))
|
2018-10-10 15:27:25 +02:00
|
|
|
return make_error_code(llvm::errc::no_such_file_or_directory);
|
|
|
|
|
|
|
|
++Start;
|
|
|
|
|
|
|
|
if (Start == End) {
|
|
|
|
// Match!
|
2021-01-15 08:33:52 +01:00
|
|
|
return LookupResult(From, Start, End);
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 08:33:52 +01:00
|
|
|
if (isa<RedirectingFileSystem::FileEntry>(From))
|
2018-10-10 15:27:25 +02:00
|
|
|
return make_error_code(llvm::errc::not_a_directory);
|
|
|
|
|
2021-01-15 08:33:52 +01:00
|
|
|
if (isa<RedirectingFileSystem::DirectoryRemapEntry>(From))
|
|
|
|
return LookupResult(From, Start, End);
|
|
|
|
|
|
|
|
auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(From);
|
2019-01-15 23:36:41 +01:00
|
|
|
for (const std::unique_ptr<RedirectingFileSystem::Entry> &DirEntry :
|
2018-10-10 15:27:25 +02:00
|
|
|
llvm::make_range(DE->contents_begin(), DE->contents_end())) {
|
2021-01-15 08:33:52 +01:00
|
|
|
ErrorOr<RedirectingFileSystem::LookupResult> Result =
|
|
|
|
lookupPathImpl(Start, End, DirEntry.get());
|
2018-10-10 15:27:25 +02:00
|
|
|
if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
|
|
|
|
return Result;
|
|
|
|
}
|
2019-11-07 19:50:33 +01:00
|
|
|
|
2018-10-10 15:27:25 +02:00
|
|
|
return make_error_code(llvm::errc::no_such_file_or_directory);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Status getRedirectedFileStatus(const Twine &Path, bool UseExternalNames,
|
|
|
|
Status ExternalStatus) {
|
|
|
|
Status S = ExternalStatus;
|
|
|
|
if (!UseExternalNames)
|
2019-02-24 00:48:47 +01:00
|
|
|
S = Status::copyWithNewName(S, Path);
|
2018-10-10 15:27:25 +02:00
|
|
|
S.IsVFSMapped = true;
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2021-01-15 08:33:52 +01:00
|
|
|
ErrorOr<Status> RedirectingFileSystem::status(
|
|
|
|
const Twine &Path, const RedirectingFileSystem::LookupResult &Result) {
|
|
|
|
if (Optional<StringRef> ExtRedirect = Result.getExternalRedirect()) {
|
|
|
|
ErrorOr<Status> S = ExternalFS->status(*ExtRedirect);
|
|
|
|
if (!S)
|
|
|
|
return S;
|
|
|
|
auto *RE = cast<RedirectingFileSystem::RemapEntry>(Result.E);
|
|
|
|
return getRedirectedFileStatus(Path, RE->useExternalName(UseExternalNames),
|
|
|
|
*S);
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
2021-01-15 08:33:52 +01:00
|
|
|
|
|
|
|
auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(Result.E);
|
|
|
|
return Status::copyWithNewName(DE->getStatus(), Path);
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
|
2021-01-22 22:51:59 +01:00
|
|
|
ErrorOr<Status> RedirectingFileSystem::status(const Twine &Path_) {
|
|
|
|
SmallString<256> Path;
|
|
|
|
Path_.toVector(Path);
|
|
|
|
|
|
|
|
if (std::error_code EC = makeCanonical(Path))
|
|
|
|
return EC;
|
|
|
|
|
2021-01-15 08:33:52 +01:00
|
|
|
ErrorOr<RedirectingFileSystem::LookupResult> Result = lookupPath(Path);
|
2018-10-27 00:14:33 +02:00
|
|
|
if (!Result) {
|
2021-01-16 02:44:14 +01:00
|
|
|
if (shouldFallBackToExternalFS(Result.getError()))
|
2018-10-27 00:14:33 +02:00
|
|
|
return ExternalFS->status(Path);
|
2018-10-10 15:27:25 +02:00
|
|
|
return Result.getError();
|
2018-10-27 00:14:33 +02:00
|
|
|
}
|
2021-01-15 08:33:52 +01:00
|
|
|
|
|
|
|
ErrorOr<Status> S = status(Path, *Result);
|
|
|
|
if (!S && shouldFallBackToExternalFS(S.getError(), Result->E))
|
|
|
|
S = ExternalFS->status(Path);
|
|
|
|
return S;
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// Provide a file wrapper with an overriden status.
|
|
|
|
class FileWithFixedStatus : public File {
|
|
|
|
std::unique_ptr<File> InnerFile;
|
|
|
|
Status S;
|
|
|
|
|
|
|
|
public:
|
|
|
|
FileWithFixedStatus(std::unique_ptr<File> InnerFile, Status S)
|
|
|
|
: InnerFile(std::move(InnerFile)), S(std::move(S)) {}
|
|
|
|
|
|
|
|
ErrorOr<Status> status() override { return S; }
|
|
|
|
ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
|
|
|
|
|
|
|
|
getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
|
|
|
|
bool IsVolatile) override {
|
|
|
|
return InnerFile->getBuffer(Name, FileSize, RequiresNullTerminator,
|
|
|
|
IsVolatile);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code close() override { return InnerFile->close(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
ErrorOr<std::unique_ptr<File>>
|
2021-01-22 22:51:59 +01:00
|
|
|
RedirectingFileSystem::openFileForRead(const Twine &Path_) {
|
|
|
|
SmallString<256> Path;
|
|
|
|
Path_.toVector(Path);
|
|
|
|
|
|
|
|
if (std::error_code EC = makeCanonical(Path))
|
|
|
|
return EC;
|
|
|
|
|
2021-01-15 08:33:52 +01:00
|
|
|
ErrorOr<RedirectingFileSystem::LookupResult> Result = lookupPath(Path);
|
|
|
|
if (!Result) {
|
|
|
|
if (shouldFallBackToExternalFS(Result.getError()))
|
2018-10-27 00:14:33 +02:00
|
|
|
return ExternalFS->openFileForRead(Path);
|
2021-01-15 08:33:52 +01:00
|
|
|
return Result.getError();
|
2018-10-27 00:14:33 +02:00
|
|
|
}
|
2018-10-10 15:27:25 +02:00
|
|
|
|
2021-01-15 08:33:52 +01:00
|
|
|
if (!Result->getExternalRedirect()) // FIXME: errc::not_a_file?
|
2018-10-10 15:27:25 +02:00
|
|
|
return make_error_code(llvm::errc::invalid_argument);
|
|
|
|
|
2021-01-15 08:33:52 +01:00
|
|
|
StringRef ExtRedirect = *Result->getExternalRedirect();
|
|
|
|
auto *RE = cast<RedirectingFileSystem::RemapEntry>(Result->E);
|
2018-10-10 15:27:25 +02:00
|
|
|
|
2021-01-15 08:33:52 +01:00
|
|
|
auto ExternalFile = ExternalFS->openFileForRead(ExtRedirect);
|
|
|
|
if (!ExternalFile) {
|
|
|
|
if (shouldFallBackToExternalFS(ExternalFile.getError(), Result->E))
|
|
|
|
return ExternalFS->openFileForRead(Path);
|
|
|
|
return ExternalFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ExternalStatus = (*ExternalFile)->status();
|
2018-10-10 15:27:25 +02:00
|
|
|
if (!ExternalStatus)
|
|
|
|
return ExternalStatus.getError();
|
|
|
|
|
|
|
|
// FIXME: Update the status with the name and VFSMapped.
|
2021-01-15 08:33:52 +01:00
|
|
|
Status S = getRedirectedFileStatus(
|
|
|
|
Path, RE->useExternalName(UseExternalNames), *ExternalStatus);
|
2018-10-10 15:27:25 +02:00
|
|
|
return std::unique_ptr<File>(
|
2021-01-15 08:33:52 +01:00
|
|
|
std::make_unique<FileWithFixedStatus>(std::move(*ExternalFile), S));
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
|
2018-11-16 02:15:54 +01:00
|
|
|
std::error_code
|
2021-01-22 22:51:59 +01:00
|
|
|
RedirectingFileSystem::getRealPath(const Twine &Path_,
|
2018-11-16 02:15:54 +01:00
|
|
|
SmallVectorImpl<char> &Output) const {
|
2021-01-22 22:51:59 +01:00
|
|
|
SmallString<256> Path;
|
|
|
|
Path_.toVector(Path);
|
|
|
|
|
|
|
|
if (std::error_code EC = makeCanonical(Path))
|
|
|
|
return EC;
|
|
|
|
|
2021-01-15 08:33:52 +01:00
|
|
|
ErrorOr<RedirectingFileSystem::LookupResult> Result = lookupPath(Path);
|
2018-11-16 02:15:54 +01:00
|
|
|
if (!Result) {
|
2021-01-16 02:44:14 +01:00
|
|
|
if (shouldFallBackToExternalFS(Result.getError()))
|
2018-11-16 02:15:54 +01:00
|
|
|
return ExternalFS->getRealPath(Path, Output);
|
|
|
|
return Result.getError();
|
|
|
|
}
|
|
|
|
|
2021-01-15 08:33:52 +01:00
|
|
|
// If we found FileEntry or DirectoryRemapEntry, look up the mapped
|
|
|
|
// path in the external file system.
|
|
|
|
if (auto ExtRedirect = Result->getExternalRedirect()) {
|
|
|
|
auto P = ExternalFS->getRealPath(*ExtRedirect, Output);
|
|
|
|
if (!P && shouldFallBackToExternalFS(P, Result->E)) {
|
|
|
|
return ExternalFS->getRealPath(Path, Output);
|
|
|
|
}
|
|
|
|
return P;
|
2018-11-16 02:15:54 +01:00
|
|
|
}
|
2021-01-15 08:33:52 +01:00
|
|
|
|
|
|
|
// If we found a DirectoryEntry, still fall back to ExternalFS if allowed,
|
2018-11-16 02:15:54 +01:00
|
|
|
// because directories don't have a single external contents path.
|
2019-10-16 01:08:57 +02:00
|
|
|
return shouldUseExternalFS() ? ExternalFS->getRealPath(Path, Output)
|
|
|
|
: llvm::errc::invalid_argument;
|
2018-11-16 02:15:54 +01:00
|
|
|
}
|
|
|
|
|
2020-12-08 23:58:46 +01:00
|
|
|
std::unique_ptr<FileSystem>
|
2018-10-10 15:27:25 +02:00
|
|
|
vfs::getVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer,
|
|
|
|
SourceMgr::DiagHandlerTy DiagHandler,
|
|
|
|
StringRef YAMLFilePath, void *DiagContext,
|
|
|
|
IntrusiveRefCntPtr<FileSystem> ExternalFS) {
|
|
|
|
return RedirectingFileSystem::create(std::move(Buffer), DiagHandler,
|
|
|
|
YAMLFilePath, DiagContext,
|
|
|
|
std::move(ExternalFS));
|
|
|
|
}
|
|
|
|
|
2019-01-15 23:36:41 +01:00
|
|
|
static void getVFSEntries(RedirectingFileSystem::Entry *SrcE,
|
|
|
|
SmallVectorImpl<StringRef> &Path,
|
2018-10-10 15:27:25 +02:00
|
|
|
SmallVectorImpl<YAMLVFSEntry> &Entries) {
|
|
|
|
auto Kind = SrcE->getKind();
|
2019-01-15 23:36:41 +01:00
|
|
|
if (Kind == RedirectingFileSystem::EK_Directory) {
|
2021-01-16 02:44:14 +01:00
|
|
|
auto *DE = dyn_cast<RedirectingFileSystem::DirectoryEntry>(SrcE);
|
2018-10-10 15:27:25 +02:00
|
|
|
assert(DE && "Must be a directory");
|
2019-01-15 23:36:41 +01:00
|
|
|
for (std::unique_ptr<RedirectingFileSystem::Entry> &SubEntry :
|
2018-10-10 15:27:25 +02:00
|
|
|
llvm::make_range(DE->contents_begin(), DE->contents_end())) {
|
|
|
|
Path.push_back(SubEntry->getName());
|
|
|
|
getVFSEntries(SubEntry.get(), Path, Entries);
|
|
|
|
Path.pop_back();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-15 08:33:52 +01:00
|
|
|
if (Kind == RedirectingFileSystem::EK_DirectoryRemap) {
|
|
|
|
auto *DR = dyn_cast<RedirectingFileSystem::DirectoryRemapEntry>(SrcE);
|
|
|
|
assert(DR && "Must be a directory remap");
|
|
|
|
SmallString<128> VPath;
|
|
|
|
for (auto &Comp : Path)
|
|
|
|
llvm::sys::path::append(VPath, Comp);
|
|
|
|
Entries.push_back(
|
|
|
|
YAMLVFSEntry(VPath.c_str(), DR->getExternalContentsPath()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-15 23:36:41 +01:00
|
|
|
assert(Kind == RedirectingFileSystem::EK_File && "Must be a EK_File");
|
2021-01-16 02:44:14 +01:00
|
|
|
auto *FE = dyn_cast<RedirectingFileSystem::FileEntry>(SrcE);
|
2018-10-10 15:27:25 +02:00
|
|
|
assert(FE && "Must be a file");
|
|
|
|
SmallString<128> VPath;
|
|
|
|
for (auto &Comp : Path)
|
|
|
|
llvm::sys::path::append(VPath, Comp);
|
|
|
|
Entries.push_back(YAMLVFSEntry(VPath.c_str(), FE->getExternalContentsPath()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void vfs::collectVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer,
|
|
|
|
SourceMgr::DiagHandlerTy DiagHandler,
|
|
|
|
StringRef YAMLFilePath,
|
|
|
|
SmallVectorImpl<YAMLVFSEntry> &CollectedEntries,
|
|
|
|
void *DiagContext,
|
|
|
|
IntrusiveRefCntPtr<FileSystem> ExternalFS) {
|
2020-12-08 23:58:46 +01:00
|
|
|
std::unique_ptr<RedirectingFileSystem> VFS = RedirectingFileSystem::create(
|
2018-10-10 15:27:25 +02:00
|
|
|
std::move(Buffer), DiagHandler, YAMLFilePath, DiagContext,
|
|
|
|
std::move(ExternalFS));
|
2021-04-27 02:01:51 +02:00
|
|
|
if (!VFS)
|
|
|
|
return;
|
2021-01-15 08:33:52 +01:00
|
|
|
ErrorOr<RedirectingFileSystem::LookupResult> RootResult =
|
|
|
|
VFS->lookupPath("/");
|
|
|
|
if (!RootResult)
|
2018-10-10 15:27:25 +02:00
|
|
|
return;
|
|
|
|
SmallVector<StringRef, 8> Components;
|
|
|
|
Components.push_back("/");
|
2021-01-15 08:33:52 +01:00
|
|
|
getVFSEntries(RootResult->E, Components, CollectedEntries);
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
UniqueID vfs::getNextVirtualUniqueID() {
|
|
|
|
static std::atomic<unsigned> UID;
|
|
|
|
unsigned ID = ++UID;
|
|
|
|
// The following assumes that uint64_t max will never collide with a real
|
|
|
|
// dev_t value from the OS.
|
|
|
|
return UniqueID(std::numeric_limits<uint64_t>::max(), ID);
|
|
|
|
}
|
|
|
|
|
2020-03-27 23:02:24 +01:00
|
|
|
void YAMLVFSWriter::addEntry(StringRef VirtualPath, StringRef RealPath,
|
|
|
|
bool IsDirectory) {
|
2018-10-10 15:27:25 +02:00
|
|
|
assert(sys::path::is_absolute(VirtualPath) && "virtual path not absolute");
|
|
|
|
assert(sys::path::is_absolute(RealPath) && "real path not absolute");
|
|
|
|
assert(!pathHasTraversal(VirtualPath) && "path traversal is not supported");
|
2020-03-27 23:02:24 +01:00
|
|
|
Mappings.emplace_back(VirtualPath, RealPath, IsDirectory);
|
|
|
|
}
|
|
|
|
|
|
|
|
void YAMLVFSWriter::addFileMapping(StringRef VirtualPath, StringRef RealPath) {
|
|
|
|
addEntry(VirtualPath, RealPath, /*IsDirectory=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void YAMLVFSWriter::addDirectoryMapping(StringRef VirtualPath,
|
|
|
|
StringRef RealPath) {
|
|
|
|
addEntry(VirtualPath, RealPath, /*IsDirectory=*/true);
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class JSONWriter {
|
|
|
|
llvm::raw_ostream &OS;
|
|
|
|
SmallVector<StringRef, 16> DirStack;
|
|
|
|
|
|
|
|
unsigned getDirIndent() { return 4 * DirStack.size(); }
|
|
|
|
unsigned getFileIndent() { return 4 * (DirStack.size() + 1); }
|
|
|
|
bool containedIn(StringRef Parent, StringRef Path);
|
|
|
|
StringRef containedPart(StringRef Parent, StringRef Path);
|
|
|
|
void startDirectory(StringRef Path);
|
|
|
|
void endDirectory();
|
|
|
|
void writeEntry(StringRef VPath, StringRef RPath);
|
|
|
|
|
|
|
|
public:
|
|
|
|
JSONWriter(llvm::raw_ostream &OS) : OS(OS) {}
|
|
|
|
|
|
|
|
void write(ArrayRef<YAMLVFSEntry> Entries, Optional<bool> UseExternalNames,
|
|
|
|
Optional<bool> IsCaseSensitive, Optional<bool> IsOverlayRelative,
|
2018-10-25 00:40:54 +02:00
|
|
|
StringRef OverlayDir);
|
2018-10-10 15:27:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool JSONWriter::containedIn(StringRef Parent, StringRef Path) {
|
|
|
|
using namespace llvm::sys;
|
|
|
|
|
|
|
|
// Compare each path component.
|
|
|
|
auto IParent = path::begin(Parent), EParent = path::end(Parent);
|
|
|
|
for (auto IChild = path::begin(Path), EChild = path::end(Path);
|
|
|
|
IParent != EParent && IChild != EChild; ++IParent, ++IChild) {
|
|
|
|
if (*IParent != *IChild)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Have we exhausted the parent path?
|
|
|
|
return IParent == EParent;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef JSONWriter::containedPart(StringRef Parent, StringRef Path) {
|
|
|
|
assert(!Parent.empty());
|
|
|
|
assert(containedIn(Parent, Path));
|
|
|
|
return Path.slice(Parent.size() + 1, StringRef::npos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSONWriter::startDirectory(StringRef Path) {
|
|
|
|
StringRef Name =
|
|
|
|
DirStack.empty() ? Path : containedPart(DirStack.back(), Path);
|
|
|
|
DirStack.push_back(Path);
|
|
|
|
unsigned Indent = getDirIndent();
|
|
|
|
OS.indent(Indent) << "{\n";
|
|
|
|
OS.indent(Indent + 2) << "'type': 'directory',\n";
|
|
|
|
OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(Name) << "\",\n";
|
|
|
|
OS.indent(Indent + 2) << "'contents': [\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSONWriter::endDirectory() {
|
|
|
|
unsigned Indent = getDirIndent();
|
|
|
|
OS.indent(Indent + 2) << "]\n";
|
|
|
|
OS.indent(Indent) << "}";
|
|
|
|
|
|
|
|
DirStack.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSONWriter::writeEntry(StringRef VPath, StringRef RPath) {
|
|
|
|
unsigned Indent = getFileIndent();
|
|
|
|
OS.indent(Indent) << "{\n";
|
|
|
|
OS.indent(Indent + 2) << "'type': 'file',\n";
|
|
|
|
OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(VPath) << "\",\n";
|
|
|
|
OS.indent(Indent + 2) << "'external-contents': \""
|
|
|
|
<< llvm::yaml::escape(RPath) << "\"\n";
|
|
|
|
OS.indent(Indent) << "}";
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries,
|
|
|
|
Optional<bool> UseExternalNames,
|
|
|
|
Optional<bool> IsCaseSensitive,
|
|
|
|
Optional<bool> IsOverlayRelative,
|
|
|
|
StringRef OverlayDir) {
|
|
|
|
using namespace llvm::sys;
|
|
|
|
|
|
|
|
OS << "{\n"
|
|
|
|
" 'version': 0,\n";
|
|
|
|
if (IsCaseSensitive.hasValue())
|
|
|
|
OS << " 'case-sensitive': '"
|
|
|
|
<< (IsCaseSensitive.getValue() ? "true" : "false") << "',\n";
|
|
|
|
if (UseExternalNames.hasValue())
|
|
|
|
OS << " 'use-external-names': '"
|
|
|
|
<< (UseExternalNames.getValue() ? "true" : "false") << "',\n";
|
|
|
|
bool UseOverlayRelative = false;
|
|
|
|
if (IsOverlayRelative.hasValue()) {
|
|
|
|
UseOverlayRelative = IsOverlayRelative.getValue();
|
|
|
|
OS << " 'overlay-relative': '" << (UseOverlayRelative ? "true" : "false")
|
|
|
|
<< "',\n";
|
|
|
|
}
|
|
|
|
OS << " 'roots': [\n";
|
|
|
|
|
|
|
|
if (!Entries.empty()) {
|
|
|
|
const YAMLVFSEntry &Entry = Entries.front();
|
2020-05-12 23:42:22 +02:00
|
|
|
|
|
|
|
startDirectory(
|
|
|
|
Entry.IsDirectory ? Entry.VPath : path::parent_path(Entry.VPath)
|
|
|
|
);
|
2018-10-10 15:27:25 +02:00
|
|
|
|
|
|
|
StringRef RPath = Entry.RPath;
|
|
|
|
if (UseOverlayRelative) {
|
|
|
|
unsigned OverlayDirLen = OverlayDir.size();
|
|
|
|
assert(RPath.substr(0, OverlayDirLen) == OverlayDir &&
|
|
|
|
"Overlay dir must be contained in RPath");
|
|
|
|
RPath = RPath.slice(OverlayDirLen, RPath.size());
|
|
|
|
}
|
|
|
|
|
2020-05-12 23:42:22 +02:00
|
|
|
bool IsCurrentDirEmpty = true;
|
|
|
|
if (!Entry.IsDirectory) {
|
2020-03-27 23:02:24 +01:00
|
|
|
writeEntry(path::filename(Entry.VPath), RPath);
|
2020-05-12 23:42:22 +02:00
|
|
|
IsCurrentDirEmpty = false;
|
|
|
|
}
|
2018-10-10 15:27:25 +02:00
|
|
|
|
|
|
|
for (const auto &Entry : Entries.slice(1)) {
|
2020-03-27 23:02:24 +01:00
|
|
|
StringRef Dir =
|
|
|
|
Entry.IsDirectory ? Entry.VPath : path::parent_path(Entry.VPath);
|
|
|
|
if (Dir == DirStack.back()) {
|
2020-05-12 23:42:22 +02:00
|
|
|
if (!IsCurrentDirEmpty) {
|
2020-03-27 23:02:24 +01:00
|
|
|
OS << ",\n";
|
|
|
|
}
|
|
|
|
} else {
|
2020-05-12 23:42:22 +02:00
|
|
|
bool IsDirPoppedFromStack = false;
|
2018-10-10 15:27:25 +02:00
|
|
|
while (!DirStack.empty() && !containedIn(DirStack.back(), Dir)) {
|
|
|
|
OS << "\n";
|
|
|
|
endDirectory();
|
2020-05-12 23:42:22 +02:00
|
|
|
IsDirPoppedFromStack = true;
|
|
|
|
}
|
|
|
|
if (IsDirPoppedFromStack || !IsCurrentDirEmpty) {
|
|
|
|
OS << ",\n";
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
startDirectory(Dir);
|
2020-05-12 23:42:22 +02:00
|
|
|
IsCurrentDirEmpty = true;
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
StringRef RPath = Entry.RPath;
|
|
|
|
if (UseOverlayRelative) {
|
|
|
|
unsigned OverlayDirLen = OverlayDir.size();
|
|
|
|
assert(RPath.substr(0, OverlayDirLen) == OverlayDir &&
|
|
|
|
"Overlay dir must be contained in RPath");
|
|
|
|
RPath = RPath.slice(OverlayDirLen, RPath.size());
|
|
|
|
}
|
2020-05-12 23:42:22 +02:00
|
|
|
if (!Entry.IsDirectory) {
|
2020-05-06 08:45:49 +02:00
|
|
|
writeEntry(path::filename(Entry.VPath), RPath);
|
2020-05-12 23:42:22 +02:00
|
|
|
IsCurrentDirEmpty = false;
|
|
|
|
}
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (!DirStack.empty()) {
|
|
|
|
OS << "\n";
|
|
|
|
endDirectory();
|
|
|
|
}
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << " ]\n"
|
|
|
|
<< "}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void YAMLVFSWriter::write(llvm::raw_ostream &OS) {
|
|
|
|
llvm::sort(Mappings, [](const YAMLVFSEntry &LHS, const YAMLVFSEntry &RHS) {
|
|
|
|
return LHS.VPath < RHS.VPath;
|
|
|
|
});
|
|
|
|
|
|
|
|
JSONWriter(OS).write(Mappings, UseExternalNames, IsCaseSensitive,
|
2018-10-25 00:40:54 +02:00
|
|
|
IsOverlayRelative, OverlayDir);
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vfs::recursive_directory_iterator::recursive_directory_iterator(
|
|
|
|
FileSystem &FS_, const Twine &Path, std::error_code &EC)
|
|
|
|
: FS(&FS_) {
|
|
|
|
directory_iterator I = FS->dir_begin(Path, EC);
|
|
|
|
if (I != directory_iterator()) {
|
2018-11-01 00:36:10 +01:00
|
|
|
State = std::make_shared<detail::RecDirIterState>();
|
|
|
|
State->Stack.push(I);
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vfs::recursive_directory_iterator &
|
|
|
|
recursive_directory_iterator::increment(std::error_code &EC) {
|
2018-11-01 00:36:10 +01:00
|
|
|
assert(FS && State && !State->Stack.empty() && "incrementing past end");
|
|
|
|
assert(!State->Stack.top()->path().empty() && "non-canonical end iterator");
|
2018-10-10 15:27:25 +02:00
|
|
|
vfs::directory_iterator End;
|
2018-11-01 00:36:10 +01:00
|
|
|
|
|
|
|
if (State->HasNoPushRequest)
|
|
|
|
State->HasNoPushRequest = false;
|
|
|
|
else {
|
|
|
|
if (State->Stack.top()->type() == sys::fs::file_type::directory_file) {
|
|
|
|
vfs::directory_iterator I = FS->dir_begin(State->Stack.top()->path(), EC);
|
|
|
|
if (I != End) {
|
|
|
|
State->Stack.push(I);
|
|
|
|
return *this;
|
|
|
|
}
|
2018-10-10 15:27:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 00:36:10 +01:00
|
|
|
while (!State->Stack.empty() && State->Stack.top().increment(EC) == End)
|
|
|
|
State->Stack.pop();
|
2018-10-10 15:27:25 +02:00
|
|
|
|
2018-11-01 00:36:10 +01:00
|
|
|
if (State->Stack.empty())
|
2018-10-10 15:27:25 +02:00
|
|
|
State.reset(); // end iterator
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|