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 <cerrno>
23 #include <climits>
24 #include <cstdarg>
25 #include <cstdio>
26 #include <fcntl.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 
45 FileSystem &FileSystem::Instance() { return *InstanceImpl(); }
46 
47 void FileSystem::Initialize() {
48   lldbassert(!InstanceImpl() && "Already initialized.");
49   InstanceImpl().emplace();
50 }
51 
52 void FileSystem::Initialize(std::shared_ptr<FileCollectorBase> collector) {
53   lldbassert(!InstanceImpl() && "Already initialized.");
54   InstanceImpl().emplace(collector);
55 }
56 
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 
73 void FileSystem::Initialize(IntrusiveRefCntPtr<vfs::FileSystem> fs) {
74   lldbassert(!InstanceImpl() && "Already initialized.");
75   InstanceImpl().emplace(fs);
76 }
77 
78 void FileSystem::Terminate() {
79   lldbassert(InstanceImpl() && "Already terminated.");
80   InstanceImpl().reset();
81 }
82 
83 Optional<FileSystem> &FileSystem::InstanceImpl() {
84   static Optional<FileSystem> g_fs;
85   return g_fs;
86 }
87 
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 
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>
104 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 
111 llvm::ErrorOr<vfs::Status> FileSystem::GetStatus(const Twine &path) const {
112   return m_fs->status(path);
113 }
114 
115 sys::TimePoint<>
116 FileSystem::GetModificationTime(const FileSpec &file_spec) const {
117   if (!file_spec)
118     return sys::TimePoint<>();
119   return GetModificationTime(file_spec.GetPath());
120 }
121 
122 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 
129 uint64_t FileSystem::GetByteSize(const FileSpec &file_spec) const {
130   if (!file_spec)
131     return 0;
132   return GetByteSize(file_spec.GetPath());
133 }
134 
135 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 
142 uint32_t FileSystem::GetPermissions(const FileSpec &file_spec) const {
143   return GetPermissions(file_spec.GetPath());
144 }
145 
146 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 
153 uint32_t FileSystem::GetPermissions(const Twine &path) const {
154   std::error_code ec;
155   return GetPermissions(path, ec);
156 }
157 
158 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 
168 bool FileSystem::Exists(const Twine &path) const { return m_fs->exists(path); }
169 
170 bool FileSystem::Exists(const FileSpec &file_spec) const {
171   return file_spec && Exists(file_spec.GetPath());
172 }
173 
174 bool FileSystem::Readable(const Twine &path) const {
175   return GetPermissions(path) & sys::fs::perms::all_read;
176 }
177 
178 bool FileSystem::Readable(const FileSpec &file_spec) const {
179   return file_spec && Readable(file_spec.GetPath());
180 }
181 
182 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 
189 bool FileSystem::IsDirectory(const FileSpec &file_spec) const {
190   return file_spec && IsDirectory(file_spec.GetPath());
191 }
192 
193 bool FileSystem::IsLocal(const Twine &path) const {
194   bool b = false;
195   if (m_fs)
196     m_fs->isLocal(path, b);
197   return b;
198 }
199 
200 bool FileSystem::IsLocal(const FileSpec &file_spec) const {
201   return file_spec && IsLocal(file_spec.GetPath());
202 }
203 
204 void FileSystem::EnumerateDirectory(Twine path, bool find_directories,
205                                     bool find_files, bool find_other,
206                                     EnumerateDirectoryCallbackType callback,
207                                     void *callback_baton) {
208   std::error_code EC;
209   vfs::recursive_directory_iterator Iter(*m_fs, path, EC);
210   vfs::recursive_directory_iterator End;
211   for (; Iter != End && !EC; Iter.increment(EC)) {
212     const auto &Item = *Iter;
213     ErrorOr<vfs::Status> Status = m_fs->status(Item.path());
214     if (!Status)
215       break;
216     if (!find_files && Status->isRegularFile())
217       continue;
218     if (!find_directories && Status->isDirectory())
219       continue;
220     if (!find_other && Status->isOther())
221       continue;
222 
223     auto Result = callback(callback_baton, Status->getType(), Item.path());
224     if (Result == eEnumerateDirectoryResultQuit)
225       return;
226     if (Result == eEnumerateDirectoryResultNext) {
227       // Default behavior is to recurse. Opt out if the callback doesn't want
228       // this behavior.
229       Iter.no_push();
230     }
231   }
232 }
233 
234 std::error_code FileSystem::MakeAbsolute(SmallVectorImpl<char> &path) const {
235   return m_fs->makeAbsolute(path);
236 }
237 
238 std::error_code FileSystem::MakeAbsolute(FileSpec &file_spec) const {
239   SmallString<128> path;
240   file_spec.GetPath(path, false);
241 
242   auto EC = MakeAbsolute(path);
243   if (EC)
244     return EC;
245 
246   FileSpec new_file_spec(path, file_spec.GetPathStyle());
247   file_spec = new_file_spec;
248   return {};
249 }
250 
251 std::error_code FileSystem::GetRealPath(const Twine &path,
252                                         SmallVectorImpl<char> &output) const {
253   return m_fs->getRealPath(path, output);
254 }
255 
256 void FileSystem::Resolve(SmallVectorImpl<char> &path) {
257   if (path.empty())
258     return;
259 
260   // Resolve tilde in path.
261   SmallString<128> resolved(path.begin(), path.end());
262   StandardTildeExpressionResolver Resolver;
263   Resolver.ResolveFullPath(llvm::StringRef(path.begin(), path.size()),
264                            resolved);
265 
266   // Try making the path absolute if it exists.
267   SmallString<128> absolute(resolved.begin(), resolved.end());
268   MakeAbsolute(absolute);
269 
270   path.clear();
271   if (Exists(absolute)) {
272     path.append(absolute.begin(), absolute.end());
273   } else {
274     path.append(resolved.begin(), resolved.end());
275   }
276 }
277 
278 void FileSystem::Resolve(FileSpec &file_spec) {
279   if (!file_spec)
280     return;
281 
282   // Extract path from the FileSpec.
283   SmallString<128> path;
284   file_spec.GetPath(path);
285 
286   // Resolve the path.
287   Resolve(path);
288 
289   // Update the FileSpec with the resolved path.
290   if (file_spec.GetFilename().IsEmpty())
291     file_spec.GetDirectory().SetString(path);
292   else
293     file_spec.SetPath(path);
294   file_spec.SetIsResolved(true);
295 }
296 
297 std::shared_ptr<DataBufferLLVM>
298 FileSystem::CreateDataBuffer(const llvm::Twine &path, uint64_t size,
299                              uint64_t offset) {
300   Collect(path);
301 
302   const bool is_volatile = !IsLocal(path);
303   const ErrorOr<std::string> external_path = GetExternalPath(path);
304 
305   if (!external_path)
306     return nullptr;
307 
308   std::unique_ptr<llvm::WritableMemoryBuffer> buffer;
309   if (size == 0) {
310     auto buffer_or_error =
311         llvm::WritableMemoryBuffer::getFile(*external_path, is_volatile);
312     if (!buffer_or_error)
313       return nullptr;
314     buffer = std::move(*buffer_or_error);
315   } else {
316     auto buffer_or_error = llvm::WritableMemoryBuffer::getFileSlice(
317         *external_path, size, offset, is_volatile);
318     if (!buffer_or_error)
319       return nullptr;
320     buffer = std::move(*buffer_or_error);
321   }
322   return std::shared_ptr<DataBufferLLVM>(new DataBufferLLVM(std::move(buffer)));
323 }
324 
325 std::shared_ptr<DataBufferLLVM>
326 FileSystem::CreateDataBuffer(const FileSpec &file_spec, uint64_t size,
327                              uint64_t offset) {
328   return CreateDataBuffer(file_spec.GetPath(), size, offset);
329 }
330 
331 bool FileSystem::ResolveExecutableLocation(FileSpec &file_spec) {
332   // If the directory is set there's nothing to do.
333   ConstString directory = file_spec.GetDirectory();
334   if (directory)
335     return false;
336 
337   // We cannot look for a file if there's no file name.
338   ConstString filename = file_spec.GetFilename();
339   if (!filename)
340     return false;
341 
342   // Search for the file on the host.
343   const std::string filename_str(filename.GetCString());
344   llvm::ErrorOr<std::string> error_or_path =
345       llvm::sys::findProgramByName(filename_str);
346   if (!error_or_path)
347     return false;
348 
349   // findProgramByName returns "." if it can't find the file.
350   llvm::StringRef path = *error_or_path;
351   llvm::StringRef parent = llvm::sys::path::parent_path(path);
352   if (parent.empty() || parent == ".")
353     return false;
354 
355   // Make sure that the result exists.
356   FileSpec result(*error_or_path);
357   if (!Exists(result))
358     return false;
359 
360   file_spec = result;
361   return true;
362 }
363 
364 bool FileSystem::GetHomeDirectory(SmallVectorImpl<char> &path) const {
365   if (!m_home_directory.empty()) {
366     path.assign(m_home_directory.begin(), m_home_directory.end());
367     return true;
368   }
369   return llvm::sys::path::home_directory(path);
370 }
371 
372 bool FileSystem::GetHomeDirectory(FileSpec &file_spec) const {
373   SmallString<128> home_dir;
374   if (!GetHomeDirectory(home_dir))
375     return false;
376   file_spec.SetPath(home_dir);
377   return true;
378 }
379 
380 static int OpenWithFS(const FileSystem &fs, const char *path, int flags,
381                       int mode) {
382   return const_cast<FileSystem &>(fs).Open(path, flags, mode);
383 }
384 
385 static int GetOpenFlags(File::OpenOptions options) {
386   int open_flags = 0;
387   File::OpenOptions rw =
388       options & (File::eOpenOptionReadOnly | File::eOpenOptionWriteOnly |
389                  File::eOpenOptionReadWrite);
390   if (rw == File::eOpenOptionWriteOnly || rw == File::eOpenOptionReadWrite) {
391     if (rw == File::eOpenOptionReadWrite)
392       open_flags |= O_RDWR;
393     else
394       open_flags |= O_WRONLY;
395 
396     if (options & File::eOpenOptionAppend)
397       open_flags |= O_APPEND;
398 
399     if (options & File::eOpenOptionTruncate)
400       open_flags |= O_TRUNC;
401 
402     if (options & File::eOpenOptionCanCreate)
403       open_flags |= O_CREAT;
404 
405     if (options & File::eOpenOptionCanCreateNewOnly)
406       open_flags |= O_CREAT | O_EXCL;
407   } else if (rw == File::eOpenOptionReadOnly) {
408     open_flags |= O_RDONLY;
409 
410 #ifndef _WIN32
411     if (options & File::eOpenOptionDontFollowSymlinks)
412       open_flags |= O_NOFOLLOW;
413 #endif
414   }
415 
416 #ifndef _WIN32
417   if (options & File::eOpenOptionNonBlocking)
418     open_flags |= O_NONBLOCK;
419   if (options & File::eOpenOptionCloseOnExec)
420     open_flags |= O_CLOEXEC;
421 #else
422   open_flags |= O_BINARY;
423 #endif
424 
425   return open_flags;
426 }
427 
428 static mode_t GetOpenMode(uint32_t permissions) {
429   mode_t mode = 0;
430   if (permissions & lldb::eFilePermissionsUserRead)
431     mode |= S_IRUSR;
432   if (permissions & lldb::eFilePermissionsUserWrite)
433     mode |= S_IWUSR;
434   if (permissions & lldb::eFilePermissionsUserExecute)
435     mode |= S_IXUSR;
436   if (permissions & lldb::eFilePermissionsGroupRead)
437     mode |= S_IRGRP;
438   if (permissions & lldb::eFilePermissionsGroupWrite)
439     mode |= S_IWGRP;
440   if (permissions & lldb::eFilePermissionsGroupExecute)
441     mode |= S_IXGRP;
442   if (permissions & lldb::eFilePermissionsWorldRead)
443     mode |= S_IROTH;
444   if (permissions & lldb::eFilePermissionsWorldWrite)
445     mode |= S_IWOTH;
446   if (permissions & lldb::eFilePermissionsWorldExecute)
447     mode |= S_IXOTH;
448   return mode;
449 }
450 
451 Expected<FileUP> FileSystem::Open(const FileSpec &file_spec,
452                                   File::OpenOptions options,
453                                   uint32_t permissions, bool should_close_fd) {
454   Collect(file_spec.GetPath());
455 
456   const int open_flags = GetOpenFlags(options);
457   const mode_t open_mode =
458       (open_flags & O_CREAT) ? GetOpenMode(permissions) : 0;
459 
460   auto path = GetExternalPath(file_spec);
461   if (!path)
462     return errorCodeToError(path.getError());
463 
464   int descriptor = llvm::sys::RetryAfterSignal(
465       -1, OpenWithFS, *this, path->c_str(), open_flags, open_mode);
466 
467   if (!File::DescriptorIsValid(descriptor))
468     return llvm::errorCodeToError(
469         std::error_code(errno, std::system_category()));
470 
471   auto file = std::unique_ptr<File>(
472       new NativeFile(descriptor, options, should_close_fd));
473   assert(file->IsValid());
474   return std::move(file);
475 }
476 
477 ErrorOr<std::string> FileSystem::GetExternalPath(const llvm::Twine &path) {
478   if (!m_mapped)
479     return path.str();
480 
481   // If VFS mapped we know the underlying FS is a RedirectingFileSystem.
482   ErrorOr<vfs::RedirectingFileSystem::LookupResult> Result =
483       static_cast<vfs::RedirectingFileSystem &>(*m_fs).lookupPath(path.str());
484   if (!Result) {
485     if (Result.getError() == llvm::errc::no_such_file_or_directory) {
486       return path.str();
487     }
488     return Result.getError();
489   }
490 
491   if (Optional<StringRef> ExtRedirect = Result->getExternalRedirect())
492     return std::string(*ExtRedirect);
493   return make_error_code(llvm::errc::not_supported);
494 }
495 
496 ErrorOr<std::string> FileSystem::GetExternalPath(const FileSpec &file_spec) {
497   return GetExternalPath(file_spec.GetPath());
498 }
499 
500 void FileSystem::Collect(const FileSpec &file_spec) {
501   Collect(file_spec.GetPath());
502 }
503 
504 void FileSystem::Collect(const llvm::Twine &file) {
505   if (!m_collector)
506     return;
507 
508   if (llvm::sys::fs::is_directory(file))
509     m_collector->addDirectory(file);
510   else
511     m_collector->addFile(file);
512 }
513 
514 void FileSystem::SetHomeDirectory(std::string home_directory) {
515   m_home_directory = std::move(home_directory);
516 }
517 
518 Status FileSystem::RemoveFile(const FileSpec &file_spec) {
519   return RemoveFile(file_spec.GetPath());
520 }
521 
522 Status FileSystem::RemoveFile(const llvm::Twine &path) {
523   return Status(llvm::sys::fs::remove(path));
524 }
525