2016-11-02 00:11:14 +08:00
|
|
|
//===-- FileSystemTest.cpp --------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08: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
|
2016-11-02 00:11:14 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-11-01 05:49:27 +08:00
|
|
|
#include "gmock/gmock.h"
|
2016-11-02 00:11:14 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
#include "lldb/Host/FileSystem.h"
|
2018-11-01 05:49:27 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2016-11-02 00:11:14 +08:00
|
|
|
|
|
|
|
extern const char *TestMainArgv0;
|
|
|
|
|
|
|
|
using namespace lldb_private;
|
2018-11-01 05:49:27 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using llvm::sys::fs::UniqueID;
|
|
|
|
|
|
|
|
// Modified from llvm/unittests/Support/VirtualFileSystemTest.cpp
|
|
|
|
namespace {
|
|
|
|
struct DummyFile : public vfs::File {
|
|
|
|
vfs::Status S;
|
|
|
|
explicit DummyFile(vfs::Status S) : S(S) {}
|
|
|
|
llvm::ErrorOr<vfs::Status> status() override { return S; }
|
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
|
|
|
|
getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
|
|
|
|
bool IsVolatile) override {
|
|
|
|
llvm_unreachable("unimplemented");
|
|
|
|
}
|
|
|
|
std::error_code close() override { return std::error_code(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
class DummyFileSystem : public vfs::FileSystem {
|
|
|
|
int FSID; // used to produce UniqueIDs
|
|
|
|
int FileID; // used to produce UniqueIDs
|
|
|
|
std::string cwd;
|
|
|
|
std::map<std::string, vfs::Status> FilesAndDirs;
|
|
|
|
|
|
|
|
static int getNextFSID() {
|
|
|
|
static int Count = 0;
|
|
|
|
return Count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
DummyFileSystem() : FSID(getNextFSID()), FileID(0) {}
|
|
|
|
|
|
|
|
ErrorOr<vfs::Status> status(const Twine &Path) override {
|
|
|
|
std::map<std::string, vfs::Status>::iterator I =
|
|
|
|
FilesAndDirs.find(Path.str());
|
|
|
|
if (I == FilesAndDirs.end())
|
|
|
|
return make_error_code(llvm::errc::no_such_file_or_directory);
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
ErrorOr<std::unique_ptr<vfs::File>>
|
|
|
|
openFileForRead(const Twine &Path) override {
|
|
|
|
auto S = status(Path);
|
|
|
|
if (S)
|
|
|
|
return std::unique_ptr<vfs::File>(new DummyFile{*S});
|
|
|
|
return S.getError();
|
|
|
|
}
|
|
|
|
llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
|
|
|
|
return cwd;
|
|
|
|
}
|
|
|
|
std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
|
|
|
|
cwd = Path.str();
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
// Map any symlink to "/symlink".
|
2018-11-09 23:18:02 +08:00
|
|
|
std::error_code getRealPath(const Twine &Path,
|
|
|
|
SmallVectorImpl<char> &Output) const override {
|
2018-11-01 05:49:27 +08:00
|
|
|
auto I = FilesAndDirs.find(Path.str());
|
|
|
|
if (I == FilesAndDirs.end())
|
|
|
|
return make_error_code(llvm::errc::no_such_file_or_directory);
|
|
|
|
if (I->second.isSymlink()) {
|
|
|
|
Output.clear();
|
|
|
|
Twine("/symlink").toVector(Output);
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
Output.clear();
|
|
|
|
Path.toVector(Output);
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DirIterImpl : public llvm::vfs::detail::DirIterImpl {
|
|
|
|
std::map<std::string, vfs::Status> &FilesAndDirs;
|
|
|
|
std::map<std::string, vfs::Status>::iterator I;
|
|
|
|
std::string Path;
|
|
|
|
bool isInPath(StringRef S) {
|
|
|
|
if (Path.size() < S.size() && S.find(Path) == 0) {
|
|
|
|
auto LastSep = S.find_last_of('/');
|
|
|
|
if (LastSep == Path.size() || LastSep == Path.size() - 1)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
DirIterImpl(std::map<std::string, vfs::Status> &FilesAndDirs,
|
|
|
|
const Twine &_Path)
|
|
|
|
: FilesAndDirs(FilesAndDirs), I(FilesAndDirs.begin()),
|
|
|
|
Path(_Path.str()) {
|
|
|
|
for (; I != FilesAndDirs.end(); ++I) {
|
|
|
|
if (isInPath(I->first)) {
|
|
|
|
CurrentEntry =
|
|
|
|
vfs::directory_entry(I->second.getName(), I->second.getType());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::error_code increment() override {
|
|
|
|
++I;
|
|
|
|
for (; I != FilesAndDirs.end(); ++I) {
|
|
|
|
if (isInPath(I->first)) {
|
|
|
|
CurrentEntry =
|
|
|
|
vfs::directory_entry(I->second.getName(), I->second.getType());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (I == FilesAndDirs.end())
|
|
|
|
CurrentEntry = vfs::directory_entry();
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
vfs::directory_iterator dir_begin(const Twine &Dir,
|
|
|
|
std::error_code &EC) override {
|
|
|
|
return vfs::directory_iterator(
|
|
|
|
std::make_shared<DirIterImpl>(FilesAndDirs, Dir));
|
|
|
|
}
|
|
|
|
|
|
|
|
void addEntry(StringRef Path, const vfs::Status &Status) {
|
|
|
|
FilesAndDirs[Path] = Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addRegularFile(StringRef Path, sys::fs::perms Perms = sys::fs::all_all) {
|
|
|
|
vfs::Status S(Path, UniqueID(FSID, FileID++),
|
|
|
|
std::chrono::system_clock::now(), 0, 0, 1024,
|
|
|
|
sys::fs::file_type::regular_file, Perms);
|
|
|
|
addEntry(Path, S);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addDirectory(StringRef Path, sys::fs::perms Perms = sys::fs::all_all) {
|
|
|
|
vfs::Status S(Path, UniqueID(FSID, FileID++),
|
|
|
|
std::chrono::system_clock::now(), 0, 0, 0,
|
|
|
|
sys::fs::file_type::directory_file, Perms);
|
|
|
|
addEntry(Path, S);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addSymlink(StringRef Path) {
|
|
|
|
vfs::Status S(Path, UniqueID(FSID, FileID++),
|
|
|
|
std::chrono::system_clock::now(), 0, 0, 0,
|
|
|
|
sys::fs::file_type::symlink_file, sys::fs::all_all);
|
|
|
|
addEntry(Path, S);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
2016-11-02 00:11:14 +08:00
|
|
|
|
|
|
|
TEST(FileSystemTest, FileAndDirectoryComponents) {
|
|
|
|
using namespace std::chrono;
|
2018-11-02 05:05:36 +08:00
|
|
|
FileSystem fs;
|
2016-11-02 00:11:14 +08:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2018-11-02 05:05:36 +08:00
|
|
|
FileSpec fs1("C:\\FILE\\THAT\\DOES\\NOT\\EXIST.TXT");
|
2016-11-02 00:11:14 +08:00
|
|
|
#else
|
2018-11-02 05:05:36 +08:00
|
|
|
FileSpec fs1("/file/that/does/not/exist.txt");
|
2016-11-02 00:11:14 +08:00
|
|
|
#endif
|
2018-11-02 05:05:36 +08:00
|
|
|
FileSpec fs2(TestMainArgv0);
|
2016-11-02 00:11:14 +08:00
|
|
|
|
2018-11-02 05:05:36 +08:00
|
|
|
fs.Resolve(fs2);
|
2018-11-01 05:49:27 +08:00
|
|
|
|
|
|
|
EXPECT_EQ(system_clock::time_point(), fs.GetModificationTime(fs1));
|
2016-11-02 00:11:14 +08:00
|
|
|
EXPECT_LT(system_clock::time_point() + hours(24 * 365 * 20),
|
2018-11-01 05:49:27 +08:00
|
|
|
fs.GetModificationTime(fs2));
|
|
|
|
}
|
|
|
|
|
|
|
|
static IntrusiveRefCntPtr<DummyFileSystem> GetSimpleDummyFS() {
|
|
|
|
IntrusiveRefCntPtr<DummyFileSystem> D(new DummyFileSystem());
|
|
|
|
D->addRegularFile("/foo");
|
|
|
|
D->addDirectory("/bar");
|
|
|
|
D->addSymlink("/baz");
|
|
|
|
D->addRegularFile("/qux", ~sys::fs::perms::all_read);
|
|
|
|
D->setCurrentWorkingDirectory("/");
|
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FileSystemTest, Exists) {
|
|
|
|
FileSystem fs(GetSimpleDummyFS());
|
|
|
|
|
|
|
|
EXPECT_TRUE(fs.Exists("/foo"));
|
2018-11-02 05:05:36 +08:00
|
|
|
EXPECT_TRUE(fs.Exists(FileSpec("/foo", FileSpec::Style::posix)));
|
2018-11-01 05:49:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FileSystemTest, Readable) {
|
|
|
|
FileSystem fs(GetSimpleDummyFS());
|
|
|
|
|
|
|
|
EXPECT_TRUE(fs.Readable("/foo"));
|
2018-11-02 05:05:36 +08:00
|
|
|
EXPECT_TRUE(fs.Readable(FileSpec("/foo", FileSpec::Style::posix)));
|
2018-11-01 05:49:27 +08:00
|
|
|
|
|
|
|
EXPECT_FALSE(fs.Readable("/qux"));
|
2018-11-02 05:05:36 +08:00
|
|
|
EXPECT_FALSE(fs.Readable(FileSpec("/qux", FileSpec::Style::posix)));
|
2018-11-01 05:49:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FileSystemTest, GetByteSize) {
|
|
|
|
FileSystem fs(GetSimpleDummyFS());
|
|
|
|
|
|
|
|
EXPECT_EQ((uint64_t)1024, fs.GetByteSize("/foo"));
|
|
|
|
EXPECT_EQ((uint64_t)1024,
|
2018-11-02 05:05:36 +08:00
|
|
|
fs.GetByteSize(FileSpec("/foo", FileSpec::Style::posix)));
|
2018-11-01 05:49:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FileSystemTest, GetPermissions) {
|
|
|
|
FileSystem fs(GetSimpleDummyFS());
|
|
|
|
|
|
|
|
EXPECT_EQ(sys::fs::all_all, fs.GetPermissions("/foo"));
|
|
|
|
EXPECT_EQ(sys::fs::all_all,
|
2018-11-02 05:05:36 +08:00
|
|
|
fs.GetPermissions(FileSpec("/foo", FileSpec::Style::posix)));
|
2018-11-01 05:49:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FileSystemTest, MakeAbsolute) {
|
|
|
|
FileSystem fs(GetSimpleDummyFS());
|
|
|
|
|
|
|
|
{
|
|
|
|
StringRef foo_relative = "foo";
|
|
|
|
SmallString<16> foo(foo_relative);
|
|
|
|
auto EC = fs.MakeAbsolute(foo);
|
|
|
|
EXPECT_FALSE(EC);
|
|
|
|
EXPECT_TRUE(foo.equals("/foo"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-11-02 05:05:36 +08:00
|
|
|
FileSpec file_spec("foo");
|
2018-11-01 05:49:27 +08:00
|
|
|
auto EC = fs.MakeAbsolute(file_spec);
|
|
|
|
EXPECT_FALSE(EC);
|
2018-11-28 09:18:10 +08:00
|
|
|
EXPECT_EQ(FileSpec("/foo"), file_spec);
|
2018-11-01 05:49:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FileSystemTest, Resolve) {
|
|
|
|
FileSystem fs(GetSimpleDummyFS());
|
|
|
|
|
|
|
|
{
|
|
|
|
StringRef foo_relative = "foo";
|
|
|
|
SmallString<16> foo(foo_relative);
|
|
|
|
fs.Resolve(foo);
|
|
|
|
EXPECT_TRUE(foo.equals("/foo"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-11-02 05:05:36 +08:00
|
|
|
FileSpec file_spec("foo");
|
2018-11-01 05:49:27 +08:00
|
|
|
fs.Resolve(file_spec);
|
2018-11-28 09:18:10 +08:00
|
|
|
EXPECT_EQ(FileSpec("/foo"), file_spec);
|
2018-11-01 05:49:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
StringRef foo_relative = "bogus";
|
|
|
|
SmallString<16> foo(foo_relative);
|
|
|
|
fs.Resolve(foo);
|
|
|
|
EXPECT_TRUE(foo.equals("bogus"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-11-02 05:05:36 +08:00
|
|
|
FileSpec file_spec("bogus");
|
2018-11-01 05:49:27 +08:00
|
|
|
fs.Resolve(file_spec);
|
2018-11-28 09:18:10 +08:00
|
|
|
EXPECT_EQ(FileSpec("bogus"), file_spec);
|
2018-11-01 05:49:27 +08:00
|
|
|
}
|
|
|
|
}
|
2018-11-01 08:26:09 +08:00
|
|
|
|
|
|
|
FileSystem::EnumerateDirectoryResult
|
|
|
|
VFSCallback(void *baton, llvm::sys::fs::file_type file_type,
|
|
|
|
llvm::StringRef path) {
|
|
|
|
auto visited = static_cast<std::vector<std::string> *>(baton);
|
|
|
|
visited->push_back(path.str());
|
|
|
|
return FileSystem::eEnumerateDirectoryResultNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FileSystemTest, EnumerateDirectory) {
|
|
|
|
FileSystem fs(GetSimpleDummyFS());
|
|
|
|
|
|
|
|
std::vector<std::string> visited;
|
|
|
|
|
|
|
|
constexpr bool find_directories = true;
|
|
|
|
constexpr bool find_files = true;
|
|
|
|
constexpr bool find_other = true;
|
|
|
|
|
|
|
|
fs.EnumerateDirectory("/", find_directories, find_files, find_other,
|
|
|
|
VFSCallback, &visited);
|
|
|
|
|
|
|
|
EXPECT_THAT(visited,
|
|
|
|
testing::UnorderedElementsAre("/foo", "/bar", "/baz", "/qux"));
|
|
|
|
}
|