1 //===-- FileSystem.cpp ----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "lldb/Host/FileSystem.h"
10
11 #include "lldb/Utility/LLDBAssert.h"
12 #include "lldb/Utility/TildeExpressionResolver.h"
13
14 #include "llvm/Support/Errc.h"
15 #include "llvm/Support/Errno.h"
16 #include "llvm/Support/Error.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/Path.h"
19 #include "llvm/Support/Program.h"
20 #include "llvm/Support/Threading.h"
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27
28 #ifdef _WIN32
29 #include "lldb/Host/windows/windows.h"
30 #else
31 #include <sys/ioctl.h>
32 #include <sys/stat.h>
33 #include <termios.h>
34 #include <unistd.h>
35 #endif
36
37 #include <algorithm>
38 #include <fstream>
39 #include <vector>
40
41 using namespace lldb;
42 using namespace lldb_private;
43 using namespace llvm;
44
Instance()45 FileSystem &FileSystem::Instance() { return *InstanceImpl(); }
46
Initialize()47 void FileSystem::Initialize() {
48 lldbassert(!InstanceImpl() && "Already initialized.");
49 InstanceImpl().emplace();
50 }
51
Initialize(std::shared_ptr<FileCollectorBase> collector)52 void FileSystem::Initialize(std::shared_ptr<FileCollectorBase> collector) {
53 lldbassert(!InstanceImpl() && "Already initialized.");
54 InstanceImpl().emplace(collector);
55 }
56
Initialize(const FileSpec & mapping)57 llvm::Error FileSystem::Initialize(const FileSpec &mapping) {
58 lldbassert(!InstanceImpl() && "Already initialized.");
59
60 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer =
61 llvm::vfs::getRealFileSystem()->getBufferForFile(mapping.GetPath());
62
63 if (!buffer)
64 return llvm::errorCodeToError(buffer.getError());
65
66 InstanceImpl().emplace(llvm::vfs::getVFSFromYAML(std::move(buffer.get()),
67 nullptr, mapping.GetPath()),
68 true);
69
70 return llvm::Error::success();
71 }
72
Initialize(IntrusiveRefCntPtr<vfs::FileSystem> fs)73 void FileSystem::Initialize(IntrusiveRefCntPtr<vfs::FileSystem> fs) {
74 lldbassert(!InstanceImpl() && "Already initialized.");
75 InstanceImpl().emplace(fs);
76 }
77
Terminate()78 void FileSystem::Terminate() {
79 lldbassert(InstanceImpl() && "Already terminated.");
80 InstanceImpl().reset();
81 }
82
InstanceImpl()83 Optional<FileSystem> &FileSystem::InstanceImpl() {
84 static Optional<FileSystem> g_fs;
85 return g_fs;
86 }
87
DirBegin(const FileSpec & file_spec,std::error_code & ec)88 vfs::directory_iterator FileSystem::DirBegin(const FileSpec &file_spec,
89 std::error_code &ec) {
90 if (!file_spec) {
91 ec = std::error_code(static_cast<int>(errc::no_such_file_or_directory),
92 std::system_category());
93 return {};
94 }
95 return DirBegin(file_spec.GetPath(), ec);
96 }
97
DirBegin(const Twine & dir,std::error_code & ec)98 vfs::directory_iterator FileSystem::DirBegin(const Twine &dir,
99 std::error_code &ec) {
100 return m_fs->dir_begin(dir, ec);
101 }
102
103 llvm::ErrorOr<vfs::Status>
GetStatus(const FileSpec & file_spec) const104 FileSystem::GetStatus(const FileSpec &file_spec) const {
105 if (!file_spec)
106 return std::error_code(static_cast<int>(errc::no_such_file_or_directory),
107 std::system_category());
108 return GetStatus(file_spec.GetPath());
109 }
110
GetStatus(const Twine & path) const111 llvm::ErrorOr<vfs::Status> FileSystem::GetStatus(const Twine &path) const {
112 return m_fs->status(path);
113 }
114
115 sys::TimePoint<>
GetModificationTime(const FileSpec & file_spec) const116 FileSystem::GetModificationTime(const FileSpec &file_spec) const {
117 if (!file_spec)
118 return sys::TimePoint<>();
119 return GetModificationTime(file_spec.GetPath());
120 }
121
GetModificationTime(const Twine & path) const122 sys::TimePoint<> FileSystem::GetModificationTime(const Twine &path) const {
123 ErrorOr<vfs::Status> status = m_fs->status(path);
124 if (!status)
125 return sys::TimePoint<>();
126 return status->getLastModificationTime();
127 }
128
GetByteSize(const FileSpec & file_spec) const129 uint64_t FileSystem::GetByteSize(const FileSpec &file_spec) const {
130 if (!file_spec)
131 return 0;
132 return GetByteSize(file_spec.GetPath());
133 }
134
GetByteSize(const Twine & path) const135 uint64_t FileSystem::GetByteSize(const Twine &path) const {
136 ErrorOr<vfs::Status> status = m_fs->status(path);
137 if (!status)
138 return 0;
139 return status->getSize();
140 }
141
GetPermissions(const FileSpec & file_spec) const142 uint32_t FileSystem::GetPermissions(const FileSpec &file_spec) const {
143 return GetPermissions(file_spec.GetPath());
144 }
145
GetPermissions(const FileSpec & file_spec,std::error_code & ec) const146 uint32_t FileSystem::GetPermissions(const FileSpec &file_spec,
147 std::error_code &ec) const {
148 if (!file_spec)
149 return sys::fs::perms::perms_not_known;
150 return GetPermissions(file_spec.GetPath(), ec);
151 }
152
GetPermissions(const Twine & path) const153 uint32_t FileSystem::GetPermissions(const Twine &path) const {
154 std::error_code ec;
155 return GetPermissions(path, ec);
156 }
157
GetPermissions(const Twine & path,std::error_code & ec) const158 uint32_t FileSystem::GetPermissions(const Twine &path,
159 std::error_code &ec) const {
160 ErrorOr<vfs::Status> status = m_fs->status(path);
161 if (!status) {
162 ec = status.getError();
163 return sys::fs::perms::perms_not_known;
164 }
165 return status->getPermissions();
166 }
167
Exists(const Twine & path) const168 bool FileSystem::Exists(const Twine &path) const { return m_fs->exists(path); }
169
Exists(const FileSpec & file_spec) const170 bool FileSystem::Exists(const FileSpec &file_spec) const {
171 return file_spec && Exists(file_spec.GetPath());
172 }
173
Readable(const Twine & path) const174 bool FileSystem::Readable(const Twine &path) const {
175 return GetPermissions(path) & sys::fs::perms::all_read;
176 }
177
Readable(const FileSpec & file_spec) const178 bool FileSystem::Readable(const FileSpec &file_spec) const {
179 return file_spec && Readable(file_spec.GetPath());
180 }
181
IsDirectory(const Twine & path) const182 bool FileSystem::IsDirectory(const Twine &path) const {
183 ErrorOr<vfs::Status> status = m_fs->status(path);
184 if (!status)
185 return false;
186 return status->isDirectory();
187 }
188
IsDirectory(const FileSpec & file_spec) const189 bool FileSystem::IsDirectory(const FileSpec &file_spec) const {
190 return file_spec && IsDirectory(file_spec.GetPath());
191 }
192
IsLocal(const Twine & path) const193 bool FileSystem::IsLocal(const Twine &path) const {
194 bool b = false;
195 m_fs->isLocal(path, b);
196 return b;
197 }
198
IsLocal(const FileSpec & file_spec) const199 bool FileSystem::IsLocal(const FileSpec &file_spec) const {
200 return file_spec && IsLocal(file_spec.GetPath());
201 }
202
EnumerateDirectory(Twine path,bool find_directories,bool find_files,bool find_other,EnumerateDirectoryCallbackType callback,void * callback_baton)203 void FileSystem::EnumerateDirectory(Twine path, bool find_directories,
204 bool find_files, bool find_other,
205 EnumerateDirectoryCallbackType callback,
206 void *callback_baton) {
207 std::error_code EC;
208 vfs::recursive_directory_iterator Iter(*m_fs, path, EC);
209 vfs::recursive_directory_iterator End;
210 for (; Iter != End && !EC; Iter.increment(EC)) {
211 const auto &Item = *Iter;
212 ErrorOr<vfs::Status> Status = m_fs->status(Item.path());
213 if (!Status)
214 break;
215 if (!find_files && Status->isRegularFile())
216 continue;
217 if (!find_directories && Status->isDirectory())
218 continue;
219 if (!find_other && Status->isOther())
220 continue;
221
222 auto Result = callback(callback_baton, Status->getType(), Item.path());
223 if (Result == eEnumerateDirectoryResultQuit)
224 return;
225 if (Result == eEnumerateDirectoryResultNext) {
226 // Default behavior is to recurse. Opt out if the callback doesn't want
227 // this behavior.
228 Iter.no_push();
229 }
230 }
231 }
232
MakeAbsolute(SmallVectorImpl<char> & path) const233 std::error_code FileSystem::MakeAbsolute(SmallVectorImpl<char> &path) const {
234 return m_fs->makeAbsolute(path);
235 }
236
MakeAbsolute(FileSpec & file_spec) const237 std::error_code FileSystem::MakeAbsolute(FileSpec &file_spec) const {
238 SmallString<128> path;
239 file_spec.GetPath(path, false);
240
241 auto EC = MakeAbsolute(path);
242 if (EC)
243 return EC;
244
245 FileSpec new_file_spec(path, file_spec.GetPathStyle());
246 file_spec = new_file_spec;
247 return {};
248 }
249
GetRealPath(const Twine & path,SmallVectorImpl<char> & output) const250 std::error_code FileSystem::GetRealPath(const Twine &path,
251 SmallVectorImpl<char> &output) const {
252 return m_fs->getRealPath(path, output);
253 }
254
Resolve(SmallVectorImpl<char> & path)255 void FileSystem::Resolve(SmallVectorImpl<char> &path) {
256 if (path.empty())
257 return;
258
259 // Resolve tilde in path.
260 SmallString<128> resolved(path.begin(), path.end());
261 StandardTildeExpressionResolver Resolver;
262 Resolver.ResolveFullPath(llvm::StringRef(path.begin(), path.size()),
263 resolved);
264
265 // Try making the path absolute if it exists.
266 SmallString<128> absolute(resolved.begin(), resolved.end());
267 MakeAbsolute(absolute);
268
269 path.clear();
270 if (Exists(absolute)) {
271 path.append(absolute.begin(), absolute.end());
272 } else {
273 path.append(resolved.begin(), resolved.end());
274 }
275 }
276
Resolve(FileSpec & file_spec)277 void FileSystem::Resolve(FileSpec &file_spec) {
278 if (!file_spec)
279 return;
280
281 // Extract path from the FileSpec.
282 SmallString<128> path;
283 file_spec.GetPath(path);
284
285 // Resolve the path.
286 Resolve(path);
287
288 // Update the FileSpec with the resolved path.
289 if (file_spec.GetFilename().IsEmpty())
290 file_spec.GetDirectory().SetString(path);
291 else
292 file_spec.SetPath(path);
293 file_spec.SetIsResolved(true);
294 }
295
296 std::shared_ptr<DataBufferLLVM>
CreateDataBuffer(const llvm::Twine & path,uint64_t size,uint64_t offset)297 FileSystem::CreateDataBuffer(const llvm::Twine &path, uint64_t size,
298 uint64_t offset) {
299 Collect(path);
300
301 const bool is_volatile = !IsLocal(path);
302 const ErrorOr<std::string> external_path = GetExternalPath(path);
303
304 if (!external_path)
305 return nullptr;
306
307 std::unique_ptr<llvm::WritableMemoryBuffer> buffer;
308 if (size == 0) {
309 auto buffer_or_error =
310 llvm::WritableMemoryBuffer::getFile(*external_path, -1, is_volatile);
311 if (!buffer_or_error)
312 return nullptr;
313 buffer = std::move(*buffer_or_error);
314 } else {
315 auto buffer_or_error = llvm::WritableMemoryBuffer::getFileSlice(
316 *external_path, size, offset, is_volatile);
317 if (!buffer_or_error)
318 return nullptr;
319 buffer = std::move(*buffer_or_error);
320 }
321 return std::shared_ptr<DataBufferLLVM>(new DataBufferLLVM(std::move(buffer)));
322 }
323
324 std::shared_ptr<DataBufferLLVM>
CreateDataBuffer(const FileSpec & file_spec,uint64_t size,uint64_t offset)325 FileSystem::CreateDataBuffer(const FileSpec &file_spec, uint64_t size,
326 uint64_t offset) {
327 return CreateDataBuffer(file_spec.GetPath(), size, offset);
328 }
329
ResolveExecutableLocation(FileSpec & file_spec)330 bool FileSystem::ResolveExecutableLocation(FileSpec &file_spec) {
331 // If the directory is set there's nothing to do.
332 ConstString directory = file_spec.GetDirectory();
333 if (directory)
334 return false;
335
336 // We cannot look for a file if there's no file name.
337 ConstString filename = file_spec.GetFilename();
338 if (!filename)
339 return false;
340
341 // Search for the file on the host.
342 const std::string filename_str(filename.GetCString());
343 llvm::ErrorOr<std::string> error_or_path =
344 llvm::sys::findProgramByName(filename_str);
345 if (!error_or_path)
346 return false;
347
348 // findProgramByName returns "." if it can't find the file.
349 llvm::StringRef path = *error_or_path;
350 llvm::StringRef parent = llvm::sys::path::parent_path(path);
351 if (parent.empty() || parent == ".")
352 return false;
353
354 // Make sure that the result exists.
355 FileSpec result(*error_or_path);
356 if (!Exists(result))
357 return false;
358
359 file_spec = result;
360 return true;
361 }
362
GetHomeDirectory(SmallVectorImpl<char> & path) const363 bool FileSystem::GetHomeDirectory(SmallVectorImpl<char> &path) const {
364 if (!m_home_directory.empty()) {
365 path.assign(m_home_directory.begin(), m_home_directory.end());
366 return true;
367 }
368 return llvm::sys::path::home_directory(path);
369 }
370
GetHomeDirectory(FileSpec & file_spec) const371 bool FileSystem::GetHomeDirectory(FileSpec &file_spec) const {
372 SmallString<128> home_dir;
373 if (!GetHomeDirectory(home_dir))
374 return false;
375 file_spec.SetPath(home_dir);
376 return true;
377 }
378
OpenWithFS(const FileSystem & fs,const char * path,int flags,int mode)379 static int OpenWithFS(const FileSystem &fs, const char *path, int flags,
380 int mode) {
381 return const_cast<FileSystem &>(fs).Open(path, flags, mode);
382 }
383
GetOpenFlags(uint32_t options)384 static int GetOpenFlags(uint32_t options) {
385 const bool read = options & File::eOpenOptionRead;
386 const bool write = options & File::eOpenOptionWrite;
387
388 int open_flags = 0;
389 if (write) {
390 if (read)
391 open_flags |= O_RDWR;
392 else
393 open_flags |= O_WRONLY;
394
395 if (options & File::eOpenOptionAppend)
396 open_flags |= O_APPEND;
397
398 if (options & File::eOpenOptionTruncate)
399 open_flags |= O_TRUNC;
400
401 if (options & File::eOpenOptionCanCreate)
402 open_flags |= O_CREAT;
403
404 if (options & File::eOpenOptionCanCreateNewOnly)
405 open_flags |= O_CREAT | O_EXCL;
406 } else if (read) {
407 open_flags |= O_RDONLY;
408
409 #ifndef _WIN32
410 if (options & File::eOpenOptionDontFollowSymlinks)
411 open_flags |= O_NOFOLLOW;
412 #endif
413 }
414
415 #ifndef _WIN32
416 if (options & File::eOpenOptionNonBlocking)
417 open_flags |= O_NONBLOCK;
418 if (options & File::eOpenOptionCloseOnExec)
419 open_flags |= O_CLOEXEC;
420 #else
421 open_flags |= O_BINARY;
422 #endif
423
424 return open_flags;
425 }
426
GetOpenMode(uint32_t permissions)427 static mode_t GetOpenMode(uint32_t permissions) {
428 mode_t mode = 0;
429 if (permissions & lldb::eFilePermissionsUserRead)
430 mode |= S_IRUSR;
431 if (permissions & lldb::eFilePermissionsUserWrite)
432 mode |= S_IWUSR;
433 if (permissions & lldb::eFilePermissionsUserExecute)
434 mode |= S_IXUSR;
435 if (permissions & lldb::eFilePermissionsGroupRead)
436 mode |= S_IRGRP;
437 if (permissions & lldb::eFilePermissionsGroupWrite)
438 mode |= S_IWGRP;
439 if (permissions & lldb::eFilePermissionsGroupExecute)
440 mode |= S_IXGRP;
441 if (permissions & lldb::eFilePermissionsWorldRead)
442 mode |= S_IROTH;
443 if (permissions & lldb::eFilePermissionsWorldWrite)
444 mode |= S_IWOTH;
445 if (permissions & lldb::eFilePermissionsWorldExecute)
446 mode |= S_IXOTH;
447 return mode;
448 }
449
Open(const FileSpec & file_spec,File::OpenOptions options,uint32_t permissions,bool should_close_fd)450 Expected<FileUP> FileSystem::Open(const FileSpec &file_spec,
451 File::OpenOptions options,
452 uint32_t permissions, bool should_close_fd) {
453 Collect(file_spec.GetPath());
454
455 const int open_flags = GetOpenFlags(options);
456 const mode_t open_mode =
457 (open_flags & O_CREAT) ? GetOpenMode(permissions) : 0;
458
459 auto path = GetExternalPath(file_spec);
460 if (!path)
461 return errorCodeToError(path.getError());
462
463 int descriptor = llvm::sys::RetryAfterSignal(
464 -1, OpenWithFS, *this, path->c_str(), open_flags, open_mode);
465
466 if (!File::DescriptorIsValid(descriptor))
467 return llvm::errorCodeToError(
468 std::error_code(errno, std::system_category()));
469
470 auto file = std::unique_ptr<File>(
471 new NativeFile(descriptor, options, should_close_fd));
472 assert(file->IsValid());
473 return std::move(file);
474 }
475
GetExternalPath(const llvm::Twine & path)476 ErrorOr<std::string> FileSystem::GetExternalPath(const llvm::Twine &path) {
477 if (!m_mapped)
478 return path.str();
479
480 // If VFS mapped we know the underlying FS is a RedirectingFileSystem.
481 ErrorOr<vfs::RedirectingFileSystem::Entry *> E =
482 static_cast<vfs::RedirectingFileSystem &>(*m_fs).lookupPath(path);
483 if (!E) {
484 if (E.getError() == llvm::errc::no_such_file_or_directory) {
485 return path.str();
486 }
487 return E.getError();
488 }
489
490 auto *F = dyn_cast<vfs::RedirectingFileSystem::RedirectingFileEntry>(*E);
491 if (!F)
492 return make_error_code(llvm::errc::not_supported);
493
494 return F->getExternalContentsPath().str();
495 }
496
GetExternalPath(const FileSpec & file_spec)497 ErrorOr<std::string> FileSystem::GetExternalPath(const FileSpec &file_spec) {
498 return GetExternalPath(file_spec.GetPath());
499 }
500
Collect(const FileSpec & file_spec)501 void FileSystem::Collect(const FileSpec &file_spec) {
502 Collect(file_spec.GetPath());
503 }
504
Collect(const llvm::Twine & file)505 void FileSystem::Collect(const llvm::Twine &file) {
506 if (!m_collector)
507 return;
508
509 if (llvm::sys::fs::is_directory(file))
510 m_collector->addDirectory(file);
511 else
512 m_collector->addFile(file);
513 }
514
SetHomeDirectory(std::string home_directory)515 void FileSystem::SetHomeDirectory(std::string home_directory) {
516 m_home_directory = std::move(home_directory);
517 }
518