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/DataBufferLLVM.h"
12 #include "lldb/Utility/LLDBAssert.h"
13 #include "lldb/Utility/TildeExpressionResolver.h"
14 
15 #include "llvm/Support/Errc.h"
16 #include "llvm/Support/Errno.h"
17 #include "llvm/Support/Error.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/Path.h"
20 #include "llvm/Support/Program.h"
21 #include "llvm/Support/Threading.h"
22 
23 #include <cerrno>
24 #include <climits>
25 #include <cstdarg>
26 #include <cstdio>
27 #include <fcntl.h>
28 
29 #ifdef _WIN32
30 #include "lldb/Host/windows/windows.h"
31 #else
32 #include <sys/ioctl.h>
33 #include <sys/stat.h>
34 #include <termios.h>
35 #include <unistd.h>
36 #endif
37 
38 #include <algorithm>
39 #include <fstream>
40 #include <optional>
41 #include <vector>
42 
43 using namespace lldb;
44 using namespace lldb_private;
45 using namespace llvm;
46 
Instance()47 FileSystem &FileSystem::Instance() { return *InstanceImpl(); }
48 
Initialize()49 void FileSystem::Initialize() {
50   lldbassert(!InstanceImpl() && "Already initialized.");
51   InstanceImpl().emplace();
52 }
53 
Initialize(IntrusiveRefCntPtr<vfs::FileSystem> fs)54 void FileSystem::Initialize(IntrusiveRefCntPtr<vfs::FileSystem> fs) {
55   lldbassert(!InstanceImpl() && "Already initialized.");
56   InstanceImpl().emplace(fs);
57 }
58 
Terminate()59 void FileSystem::Terminate() {
60   lldbassert(InstanceImpl() && "Already terminated.");
61   InstanceImpl().reset();
62 }
63 
InstanceImpl()64 std::optional<FileSystem> &FileSystem::InstanceImpl() {
65   static std::optional<FileSystem> g_fs;
66   return g_fs;
67 }
68 
DirBegin(const FileSpec & file_spec,std::error_code & ec)69 vfs::directory_iterator FileSystem::DirBegin(const FileSpec &file_spec,
70                                              std::error_code &ec) {
71   if (!file_spec) {
72     ec = std::error_code(static_cast<int>(errc::no_such_file_or_directory),
73                          std::system_category());
74     return {};
75   }
76   return DirBegin(file_spec.GetPath(), ec);
77 }
78 
DirBegin(const Twine & dir,std::error_code & ec)79 vfs::directory_iterator FileSystem::DirBegin(const Twine &dir,
80                                              std::error_code &ec) {
81   return m_fs->dir_begin(dir, ec);
82 }
83 
84 llvm::ErrorOr<vfs::Status>
GetStatus(const FileSpec & file_spec) const85 FileSystem::GetStatus(const FileSpec &file_spec) const {
86   if (!file_spec)
87     return std::error_code(static_cast<int>(errc::no_such_file_or_directory),
88                            std::system_category());
89   return GetStatus(file_spec.GetPath());
90 }
91 
GetStatus(const Twine & path) const92 llvm::ErrorOr<vfs::Status> FileSystem::GetStatus(const Twine &path) const {
93   return m_fs->status(path);
94 }
95 
96 sys::TimePoint<>
GetModificationTime(const FileSpec & file_spec) const97 FileSystem::GetModificationTime(const FileSpec &file_spec) const {
98   if (!file_spec)
99     return sys::TimePoint<>();
100   return GetModificationTime(file_spec.GetPath());
101 }
102 
GetModificationTime(const Twine & path) const103 sys::TimePoint<> FileSystem::GetModificationTime(const Twine &path) const {
104   ErrorOr<vfs::Status> status = m_fs->status(path);
105   if (!status)
106     return sys::TimePoint<>();
107   return status->getLastModificationTime();
108 }
109 
GetByteSize(const FileSpec & file_spec) const110 uint64_t FileSystem::GetByteSize(const FileSpec &file_spec) const {
111   if (!file_spec)
112     return 0;
113   return GetByteSize(file_spec.GetPath());
114 }
115 
GetByteSize(const Twine & path) const116 uint64_t FileSystem::GetByteSize(const Twine &path) const {
117   ErrorOr<vfs::Status> status = m_fs->status(path);
118   if (!status)
119     return 0;
120   return status->getSize();
121 }
122 
GetPermissions(const FileSpec & file_spec) const123 uint32_t FileSystem::GetPermissions(const FileSpec &file_spec) const {
124   return GetPermissions(file_spec.GetPath());
125 }
126 
GetPermissions(const FileSpec & file_spec,std::error_code & ec) const127 uint32_t FileSystem::GetPermissions(const FileSpec &file_spec,
128                                     std::error_code &ec) const {
129   if (!file_spec)
130     return sys::fs::perms::perms_not_known;
131   return GetPermissions(file_spec.GetPath(), ec);
132 }
133 
GetPermissions(const Twine & path) const134 uint32_t FileSystem::GetPermissions(const Twine &path) const {
135   std::error_code ec;
136   return GetPermissions(path, ec);
137 }
138 
GetPermissions(const Twine & path,std::error_code & ec) const139 uint32_t FileSystem::GetPermissions(const Twine &path,
140                                     std::error_code &ec) const {
141   ErrorOr<vfs::Status> status = m_fs->status(path);
142   if (!status) {
143     ec = status.getError();
144     return sys::fs::perms::perms_not_known;
145   }
146   return status->getPermissions();
147 }
148 
Exists(const Twine & path) const149 bool FileSystem::Exists(const Twine &path) const { return m_fs->exists(path); }
150 
Exists(const FileSpec & file_spec) const151 bool FileSystem::Exists(const FileSpec &file_spec) const {
152   return file_spec && Exists(file_spec.GetPath());
153 }
154 
Readable(const Twine & path) const155 bool FileSystem::Readable(const Twine &path) const {
156   return GetPermissions(path) & sys::fs::perms::all_read;
157 }
158 
Readable(const FileSpec & file_spec) const159 bool FileSystem::Readable(const FileSpec &file_spec) const {
160   return file_spec && Readable(file_spec.GetPath());
161 }
162 
IsDirectory(const Twine & path) const163 bool FileSystem::IsDirectory(const Twine &path) const {
164   ErrorOr<vfs::Status> status = m_fs->status(path);
165   if (!status)
166     return false;
167   return status->isDirectory();
168 }
169 
IsDirectory(const FileSpec & file_spec) const170 bool FileSystem::IsDirectory(const FileSpec &file_spec) const {
171   return file_spec && IsDirectory(file_spec.GetPath());
172 }
173 
IsLocal(const Twine & path) const174 bool FileSystem::IsLocal(const Twine &path) const {
175   bool b = false;
176   m_fs->isLocal(path, b);
177   return b;
178 }
179 
IsLocal(const FileSpec & file_spec) const180 bool FileSystem::IsLocal(const FileSpec &file_spec) const {
181   return file_spec && IsLocal(file_spec.GetPath());
182 }
183 
EnumerateDirectory(Twine path,bool find_directories,bool find_files,bool find_other,EnumerateDirectoryCallbackType callback,void * callback_baton)184 void FileSystem::EnumerateDirectory(Twine path, bool find_directories,
185                                     bool find_files, bool find_other,
186                                     EnumerateDirectoryCallbackType callback,
187                                     void *callback_baton) {
188   std::error_code EC;
189   vfs::recursive_directory_iterator Iter(*m_fs, path, EC);
190   vfs::recursive_directory_iterator End;
191   for (; Iter != End && !EC; Iter.increment(EC)) {
192     const auto &Item = *Iter;
193     ErrorOr<vfs::Status> Status = m_fs->status(Item.path());
194     if (!Status)
195       break;
196     if (!find_files && Status->isRegularFile())
197       continue;
198     if (!find_directories && Status->isDirectory())
199       continue;
200     if (!find_other && Status->isOther())
201       continue;
202 
203     auto Result = callback(callback_baton, Status->getType(), Item.path());
204     if (Result == eEnumerateDirectoryResultQuit)
205       return;
206     if (Result == eEnumerateDirectoryResultNext) {
207       // Default behavior is to recurse. Opt out if the callback doesn't want
208       // this behavior.
209       Iter.no_push();
210     }
211   }
212 }
213 
MakeAbsolute(SmallVectorImpl<char> & path) const214 std::error_code FileSystem::MakeAbsolute(SmallVectorImpl<char> &path) const {
215   return m_fs->makeAbsolute(path);
216 }
217 
MakeAbsolute(FileSpec & file_spec) const218 std::error_code FileSystem::MakeAbsolute(FileSpec &file_spec) const {
219   SmallString<128> path;
220   file_spec.GetPath(path, false);
221 
222   auto EC = MakeAbsolute(path);
223   if (EC)
224     return EC;
225 
226   FileSpec new_file_spec(path, file_spec.GetPathStyle());
227   file_spec = new_file_spec;
228   return {};
229 }
230 
GetRealPath(const Twine & path,SmallVectorImpl<char> & output) const231 std::error_code FileSystem::GetRealPath(const Twine &path,
232                                         SmallVectorImpl<char> &output) const {
233   return m_fs->getRealPath(path, output);
234 }
235 
Resolve(SmallVectorImpl<char> & path)236 void FileSystem::Resolve(SmallVectorImpl<char> &path) {
237   if (path.empty())
238     return;
239 
240   // Resolve tilde in path.
241   SmallString<128> resolved(path.begin(), path.end());
242   StandardTildeExpressionResolver Resolver;
243   Resolver.ResolveFullPath(llvm::StringRef(path.begin(), path.size()),
244                            resolved);
245 
246   // Try making the path absolute if it exists.
247   SmallString<128> absolute(resolved.begin(), resolved.end());
248   MakeAbsolute(absolute);
249 
250   path.clear();
251   if (Exists(absolute)) {
252     path.append(absolute.begin(), absolute.end());
253   } else {
254     path.append(resolved.begin(), resolved.end());
255   }
256 }
257 
Resolve(FileSpec & file_spec)258 void FileSystem::Resolve(FileSpec &file_spec) {
259   if (!file_spec)
260     return;
261 
262   // Extract path from the FileSpec.
263   SmallString<128> path;
264   file_spec.GetPath(path);
265 
266   // Resolve the path.
267   Resolve(path);
268 
269   // Update the FileSpec with the resolved path.
270   if (file_spec.GetFilename().IsEmpty())
271     file_spec.SetDirectory(path);
272   else
273     file_spec.SetPath(path);
274   file_spec.SetIsResolved(true);
275 }
276 
277 template <typename T>
GetMemoryBuffer(const llvm::Twine & path,uint64_t size,uint64_t offset,bool is_volatile)278 static std::unique_ptr<T> GetMemoryBuffer(const llvm::Twine &path,
279                                           uint64_t size, uint64_t offset,
280                                           bool is_volatile) {
281   std::unique_ptr<T> buffer;
282   if (size == 0) {
283     auto buffer_or_error = T::getFile(path, is_volatile);
284     if (!buffer_or_error)
285       return nullptr;
286     buffer = std::move(*buffer_or_error);
287   } else {
288     auto buffer_or_error = T::getFileSlice(path, size, offset, is_volatile);
289     if (!buffer_or_error)
290       return nullptr;
291     buffer = std::move(*buffer_or_error);
292   }
293   return buffer;
294 }
295 
296 std::shared_ptr<WritableDataBuffer>
CreateWritableDataBuffer(const llvm::Twine & path,uint64_t size,uint64_t offset)297 FileSystem::CreateWritableDataBuffer(const llvm::Twine &path, uint64_t size,
298                                      uint64_t offset) {
299   const bool is_volatile = !IsLocal(path);
300   auto buffer = GetMemoryBuffer<llvm::WritableMemoryBuffer>(path, size, offset,
301                                                             is_volatile);
302   if (!buffer)
303     return {};
304   return std::shared_ptr<WritableDataBufferLLVM>(
305       new WritableDataBufferLLVM(std::move(buffer)));
306 }
307 
308 std::shared_ptr<DataBuffer>
CreateDataBuffer(const llvm::Twine & path,uint64_t size,uint64_t offset)309 FileSystem::CreateDataBuffer(const llvm::Twine &path, uint64_t size,
310                              uint64_t offset) {
311   const bool is_volatile = !IsLocal(path);
312   auto buffer =
313       GetMemoryBuffer<llvm::MemoryBuffer>(path, size, offset, is_volatile);
314   if (!buffer)
315     return {};
316   return std::shared_ptr<DataBufferLLVM>(new DataBufferLLVM(std::move(buffer)));
317 }
318 
319 std::shared_ptr<WritableDataBuffer>
CreateWritableDataBuffer(const FileSpec & file_spec,uint64_t size,uint64_t offset)320 FileSystem::CreateWritableDataBuffer(const FileSpec &file_spec, uint64_t size,
321                                      uint64_t offset) {
322   return CreateWritableDataBuffer(file_spec.GetPath(), size, offset);
323 }
324 
325 std::shared_ptr<DataBuffer>
CreateDataBuffer(const FileSpec & file_spec,uint64_t size,uint64_t offset)326 FileSystem::CreateDataBuffer(const FileSpec &file_spec, uint64_t size,
327                              uint64_t offset) {
328   return CreateDataBuffer(file_spec.GetPath(), size, offset);
329 }
330 
ResolveExecutableLocation(FileSpec & file_spec)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 
GetHomeDirectory(SmallVectorImpl<char> & path) const364 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 
GetHomeDirectory(FileSpec & file_spec) const372 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 
OpenWithFS(const FileSystem & fs,const char * path,int flags,int mode)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 
GetOpenFlags(File::OpenOptions options)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 
GetOpenMode(uint32_t permissions)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 
Open(const FileSpec & file_spec,File::OpenOptions options,uint32_t permissions,bool should_close_fd)451 Expected<FileUP> FileSystem::Open(const FileSpec &file_spec,
452                                   File::OpenOptions options,
453                                   uint32_t permissions, bool should_close_fd) {
454   const int open_flags = GetOpenFlags(options);
455   const mode_t open_mode =
456       (open_flags & O_CREAT) ? GetOpenMode(permissions) : 0;
457 
458   auto path = file_spec.GetPath();
459 
460   int descriptor = llvm::sys::RetryAfterSignal(
461       -1, OpenWithFS, *this, path.c_str(), open_flags, open_mode);
462 
463   if (!File::DescriptorIsValid(descriptor))
464     return llvm::errorCodeToError(
465         std::error_code(errno, std::system_category()));
466 
467   auto file = std::unique_ptr<File>(
468       new NativeFile(descriptor, options, should_close_fd));
469   assert(file->IsValid());
470   return std::move(file);
471 }
472 
SetHomeDirectory(std::string home_directory)473 void FileSystem::SetHomeDirectory(std::string home_directory) {
474   m_home_directory = std::move(home_directory);
475 }
476 
RemoveFile(const FileSpec & file_spec)477 Status FileSystem::RemoveFile(const FileSpec &file_spec) {
478   return RemoveFile(file_spec.GetPath());
479 }
480 
RemoveFile(const llvm::Twine & path)481 Status FileSystem::RemoveFile(const llvm::Twine &path) {
482   return Status(llvm::sys::fs::remove(path));
483 }
484