1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
| //===-- FileSystemTest.cpp --------------------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "lldb/Host/FileSystem.h"
#include "llvm/Support/Errc.h"
extern const char *TestMainArgv0;
using namespace lldb_private;
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".
std::error_code getRealPath(const Twine &Path,
SmallVectorImpl<char> &Output) const override {
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
TEST(FileSystemTest, FileAndDirectoryComponents) {
using namespace std::chrono;
FileSystem fs;
#ifdef _WIN32
FileSpec fs1("C:\\FILE\\THAT\\DOES\\NOT\\EXIST.TXT");
#else
FileSpec fs1("/file/that/does/not/exist.txt");
#endif
FileSpec fs2(TestMainArgv0);
fs.Resolve(fs2);
EXPECT_EQ(system_clock::time_point(), fs.GetModificationTime(fs1));
EXPECT_LT(system_clock::time_point() + hours(24 * 365 * 20),
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"));
EXPECT_TRUE(fs.Exists(FileSpec("/foo", FileSpec::Style::posix)));
}
TEST(FileSystemTest, Readable) {
FileSystem fs(GetSimpleDummyFS());
EXPECT_TRUE(fs.Readable("/foo"));
EXPECT_TRUE(fs.Readable(FileSpec("/foo", FileSpec::Style::posix)));
EXPECT_FALSE(fs.Readable("/qux"));
EXPECT_FALSE(fs.Readable(FileSpec("/qux", FileSpec::Style::posix)));
}
TEST(FileSystemTest, GetByteSize) {
FileSystem fs(GetSimpleDummyFS());
EXPECT_EQ((uint64_t)1024, fs.GetByteSize("/foo"));
EXPECT_EQ((uint64_t)1024,
fs.GetByteSize(FileSpec("/foo", FileSpec::Style::posix)));
}
TEST(FileSystemTest, GetPermissions) {
FileSystem fs(GetSimpleDummyFS());
EXPECT_EQ(sys::fs::all_all, fs.GetPermissions("/foo"));
EXPECT_EQ(sys::fs::all_all,
fs.GetPermissions(FileSpec("/foo", FileSpec::Style::posix)));
}
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"));
}
{
FileSpec file_spec("foo");
auto EC = fs.MakeAbsolute(file_spec);
EXPECT_FALSE(EC);
EXPECT_EQ(FileSpec("/foo"), file_spec);
}
}
TEST(FileSystemTest, Resolve) {
FileSystem fs(GetSimpleDummyFS());
{
StringRef foo_relative = "foo";
SmallString<16> foo(foo_relative);
fs.Resolve(foo);
EXPECT_TRUE(foo.equals("/foo"));
}
{
FileSpec file_spec("foo");
fs.Resolve(file_spec);
EXPECT_EQ(FileSpec("/foo"), file_spec);
}
{
StringRef foo_relative = "bogus";
SmallString<16> foo(foo_relative);
fs.Resolve(foo);
EXPECT_TRUE(foo.equals("bogus"));
}
{
FileSpec file_spec("bogus");
fs.Resolve(file_spec);
EXPECT_EQ(FileSpec("bogus"), file_spec);
}
}
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"));
}
TEST(FileSystemTest, OpenErrno) {
#ifdef _WIN32
FileSpec spec("C:\\FILE\\THAT\\DOES\\NOT\\EXIST.TXT");
#else
FileSpec spec("/file/that/does/not/exist.txt");
#endif
FileSystem fs;
auto file = fs.Open(spec, File::eOpenOptionRead, 0, true);
ASSERT_FALSE(file);
std::error_code code = errorToErrorCode(file.takeError());
EXPECT_EQ(code.category(), std::system_category());
EXPECT_EQ(code.value(), ENOENT);
}
|