11 #include "llvm/ADT/None.h"
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/Path.h"
17 #include "llvm/Support/VirtualFileSystem.h"
29 class VolatileFileSystem :
public llvm::vfs::ProxyFileSystem {
31 explicit VolatileFileSystem(llvm::IntrusiveRefCntPtr<FileSystem>
FS)
32 : ProxyFileSystem(std::move(
FS)) {}
34 llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
35 openFileForRead(
const llvm::Twine &InPath)
override {
36 llvm::SmallString<128>
Path;
37 InPath.toVector(
Path);
39 auto File = getUnderlyingFS().openFileForRead(
Path);
44 llvm::StringRef
FileName = llvm::sys::path::filename(
Path);
47 return std::unique_ptr<VolatileFile>(
new VolatileFile(std::move(*
File)));
51 class VolatileFile :
public llvm::vfs::File {
53 VolatileFile(std::unique_ptr<llvm::vfs::File> Wrapped)
54 : Wrapped(std::move(Wrapped)) {
55 assert(this->Wrapped);
58 virtual llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
60 bool RequiresNullTerminator,
bool )
override {
61 return Wrapped->getBuffer(
Name, FileSize, RequiresNullTerminator,
65 llvm::ErrorOr<llvm::vfs::Status> status()
override {
66 return Wrapped->status();
68 llvm::ErrorOr<std::string> getName()
override {
return Wrapped->getName(); }
69 std::error_code close()
override {
return Wrapped->close(); }
72 std::unique_ptr<File> Wrapped;
77 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
80 if (
auto EC =
FS->setCurrentWorkingDirectory(CWD))
81 elog(
"VFS: failed to set CWD to {0}: {1}", CWD, EC.message());
85 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
86 RealThreadsafeFS::viewImpl()
const {
90 return new VolatileFileSystem(
91 llvm::vfs::createPhysicalFileSystem().release());