[FileSystem] Extend file system and have it use the VFS.

This patch extends the FileSystem class with a bunch of functions that
are currently implemented as methods of the FileSpec class. These
methods will be removed in future commits and replaced by calls to the
file system.

The new functions are operated in terms of the virtual file system which
was recently moved from clang into LLVM so it could be reused in lldb.
Because the VFS is stateful, we turned the FileSystem class into a
singleton.

Differential revision: https://reviews.llvm.org/D53532

llvm-svn: 345783
This commit is contained in:
Jonas Devlieghere
2018-10-31 21:49:27 +00:00
parent 063fd98bcc
commit 46376966ea
32 changed files with 632 additions and 68 deletions

View File

@@ -9,7 +9,10 @@
#include "lldb/Host/FileSystem.h"
#include "lldb/Utility/TildeExpressionResolver.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Threading.h"
#include <algorithm>
#include <fstream>
@@ -17,12 +20,160 @@
using namespace lldb;
using namespace lldb_private;
using namespace llvm;
llvm::sys::TimePoint<>
FileSystem::GetModificationTime(const FileSpec &file_spec) {
llvm::sys::fs::file_status status;
std::error_code ec = llvm::sys::fs::status(file_spec.GetPath(), status);
if (ec)
return llvm::sys::TimePoint<>();
return status.getLastModificationTime();
FileSystem &FileSystem::Instance() { return *InstanceImpl(); }
void FileSystem::Initialize() {
assert(!InstanceImpl());
InstanceImpl().emplace();
}
void FileSystem::Initialize(IntrusiveRefCntPtr<vfs::FileSystem> fs) {
assert(!InstanceImpl());
InstanceImpl().emplace(fs);
}
void FileSystem::Terminate() {
assert(InstanceImpl());
InstanceImpl().reset();
}
Optional<FileSystem> &FileSystem::InstanceImpl() {
static Optional<FileSystem> g_fs;
return g_fs;
}
void FileSystem::SetFileSystem(IntrusiveRefCntPtr<vfs::FileSystem> fs) {
m_fs = fs;
}
sys::TimePoint<>
FileSystem::GetModificationTime(const FileSpec &file_spec) const {
return GetModificationTime(file_spec.GetPath());
}
sys::TimePoint<> FileSystem::GetModificationTime(const Twine &path) const {
ErrorOr<vfs::Status> status = m_fs->status(path);
if (!status)
return sys::TimePoint<>();
return status->getLastModificationTime();
}
uint64_t FileSystem::GetByteSize(const FileSpec &file_spec) const {
return GetByteSize(file_spec.GetPath());
}
uint64_t FileSystem::GetByteSize(const Twine &path) const {
ErrorOr<vfs::Status> status = m_fs->status(path);
if (!status)
return 0;
return status->getSize();
}
uint32_t FileSystem::GetPermissions(const FileSpec &file_spec) const {
return GetPermissions(file_spec.GetPath());
}
uint32_t FileSystem::GetPermissions(const Twine &path) const {
ErrorOr<vfs::Status> status = m_fs->status(path);
if (!status)
return sys::fs::perms::perms_not_known;
return status->getPermissions();
}
bool FileSystem::Exists(const Twine &path) const { return m_fs->exists(path); }
bool FileSystem::Exists(const FileSpec &file_spec) const {
return Exists(file_spec.GetPath());
}
bool FileSystem::Readable(const Twine &path) const {
return GetPermissions(path) & sys::fs::perms::all_read;
}
bool FileSystem::Readable(const FileSpec &file_spec) const {
return Readable(file_spec.GetPath());
}
void FileSystem::EnumerateDirectory(Twine path, bool find_directories,
bool find_files, bool find_other,
EnumerateDirectoryCallbackType callback,
void *callback_baton) {
std::error_code EC;
vfs::recursive_directory_iterator Iter(*m_fs, path, EC);
vfs::recursive_directory_iterator End;
for (; Iter != End && !EC; Iter.increment(EC)) {
const auto &Item = *Iter;
ErrorOr<vfs::Status> Status = m_fs->status(Item.path());
if (!Status)
break;
if (!find_files && Status->isRegularFile())
continue;
if (!find_directories && Status->isDirectory())
continue;
if (!find_other && Status->isOther())
continue;
auto Result = callback(callback_baton, Status->getType(), Item.path());
if (Result == eEnumerateDirectoryResultQuit)
return;
if (Result == eEnumerateDirectoryResultNext) {
// Default behavior is to recurse. Opt out if the callback doesn't want
// this behavior.
Iter.no_push();
}
}
}
std::error_code FileSystem::MakeAbsolute(SmallVectorImpl<char> &path) const {
return m_fs->makeAbsolute(path);
}
std::error_code FileSystem::MakeAbsolute(FileSpec &file_spec) const {
SmallString<128> path;
file_spec.GetPath(path, false);
auto EC = MakeAbsolute(path);
if (EC)
return EC;
FileSpec new_file_spec(path, false, file_spec.GetPathStyle());
file_spec = new_file_spec;
return {};
}
std::error_code FileSystem::GetRealPath(const Twine &path,
SmallVectorImpl<char> &output) const {
return m_fs->getRealPath(path, output);
}
void FileSystem::Resolve(SmallVectorImpl<char> &path) {
if (path.empty())
return;
// Resolve tilde.
SmallString<128> original_path(path.begin(), path.end());
StandardTildeExpressionResolver Resolver;
Resolver.ResolveFullPath(original_path, path);
// Try making the path absolute if it exists.
SmallString<128> absolute_path(path.begin(), path.end());
MakeAbsolute(path);
if (!Exists(path)) {
path.clear();
path.append(original_path.begin(), original_path.end());
}
}
void FileSystem::Resolve(FileSpec &file_spec) {
// Extract path from the FileSpec.
SmallString<128> path;
file_spec.GetPath(path);
// Resolve the path.
Resolve(path);
// Update the FileSpec with the resolved path.
file_spec.SetPath(path);
}