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