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 void FileSystem::Initialize(IntrusiveRefCntPtr<vfs::FileSystem> fs) {
58   lldbassert(!InstanceImpl() && "Already initialized.");
59   InstanceImpl().emplace(fs);
60 }
61 
62 void FileSystem::Terminate() {
63   lldbassert(InstanceImpl() && "Already terminated.");
64   InstanceImpl().reset();
65 }
66 
67 Optional<FileSystem> &FileSystem::InstanceImpl() {
68   static Optional<FileSystem> g_fs;
69   return g_fs;
70 }
71 
72 vfs::directory_iterator FileSystem::DirBegin(const FileSpec &file_spec,
73                                              std::error_code &ec) {
74   if (!file_spec) {
75     ec = std::error_code(static_cast<int>(errc::no_such_file_or_directory),
76                          std::system_category());
77     return {};
78   }
79   return DirBegin(file_spec.GetPath(), ec);
80 }
81 
82 vfs::directory_iterator FileSystem::DirBegin(const Twine &dir,
83                                              std::error_code &ec) {
84   return m_fs->dir_begin(dir, ec);
85 }
86 
87 llvm::ErrorOr<vfs::Status>
88 FileSystem::GetStatus(const FileSpec &file_spec) const {
89   if (!file_spec)
90     return std::error_code(static_cast<int>(errc::no_such_file_or_directory),
91                            std::system_category());
92   return GetStatus(file_spec.GetPath());
93 }
94 
95 llvm::ErrorOr<vfs::Status> FileSystem::GetStatus(const Twine &path) const {
96   return m_fs->status(path);
97 }
98 
99 sys::TimePoint<>
100 FileSystem::GetModificationTime(const FileSpec &file_spec) const {
101   if (!file_spec)
102     return sys::TimePoint<>();
103   return GetModificationTime(file_spec.GetPath());
104 }
105 
106 sys::TimePoint<> FileSystem::GetModificationTime(const Twine &path) const {
107   ErrorOr<vfs::Status> status = m_fs->status(path);
108   if (!status)
109     return sys::TimePoint<>();
110   return status->getLastModificationTime();
111 }
112 
113 uint64_t FileSystem::GetByteSize(const FileSpec &file_spec) const {
114   if (!file_spec)
115     return 0;
116   return GetByteSize(file_spec.GetPath());
117 }
118 
119 uint64_t FileSystem::GetByteSize(const Twine &path) const {
120   ErrorOr<vfs::Status> status = m_fs->status(path);
121   if (!status)
122     return 0;
123   return status->getSize();
124 }
125 
126 uint32_t FileSystem::GetPermissions(const FileSpec &file_spec) const {
127   return GetPermissions(file_spec.GetPath());
128 }
129 
130 uint32_t FileSystem::GetPermissions(const FileSpec &file_spec,
131                                     std::error_code &ec) const {
132   if (!file_spec)
133     return sys::fs::perms::perms_not_known;
134   return GetPermissions(file_spec.GetPath(), ec);
135 }
136 
137 uint32_t FileSystem::GetPermissions(const Twine &path) const {
138   std::error_code ec;
139   return GetPermissions(path, ec);
140 }
141 
142 uint32_t FileSystem::GetPermissions(const Twine &path,
143                                     std::error_code &ec) const {
144   ErrorOr<vfs::Status> status = m_fs->status(path);
145   if (!status) {
146     ec = status.getError();
147     return sys::fs::perms::perms_not_known;
148   }
149   return status->getPermissions();
150 }
151 
152 bool FileSystem::Exists(const Twine &path) const { return m_fs->exists(path); }
153 
154 bool FileSystem::Exists(const FileSpec &file_spec) const {
155   return file_spec && Exists(file_spec.GetPath());
156 }
157 
158 bool FileSystem::Readable(const Twine &path) const {
159   return GetPermissions(path) & sys::fs::perms::all_read;
160 }
161 
162 bool FileSystem::Readable(const FileSpec &file_spec) const {
163   return file_spec && Readable(file_spec.GetPath());
164 }
165 
166 bool FileSystem::IsDirectory(const Twine &path) const {
167   ErrorOr<vfs::Status> status = m_fs->status(path);
168   if (!status)
169     return false;
170   return status->isDirectory();
171 }
172 
173 bool FileSystem::IsDirectory(const FileSpec &file_spec) const {
174   return file_spec && IsDirectory(file_spec.GetPath());
175 }
176 
177 bool FileSystem::IsLocal(const Twine &path) const {
178   bool b = false;
179   if (m_fs)
180     m_fs->isLocal(path, b);
181   return b;
182 }
183 
184 bool FileSystem::IsLocal(const FileSpec &file_spec) const {
185   return file_spec && IsLocal(file_spec.GetPath());
186 }
187 
188 void FileSystem::EnumerateDirectory(Twine path, bool find_directories,
189                                     bool find_files, bool find_other,
190                                     EnumerateDirectoryCallbackType callback,
191                                     void *callback_baton) {
192   std::error_code EC;
193   vfs::recursive_directory_iterator Iter(*m_fs, path, EC);
194   vfs::recursive_directory_iterator End;
195   for (; Iter != End && !EC; Iter.increment(EC)) {
196     const auto &Item = *Iter;
197     ErrorOr<vfs::Status> Status = m_fs->status(Item.path());
198     if (!Status)
199       break;
200     if (!find_files && Status->isRegularFile())
201       continue;
202     if (!find_directories && Status->isDirectory())
203       continue;
204     if (!find_other && Status->isOther())
205       continue;
206 
207     auto Result = callback(callback_baton, Status->getType(), Item.path());
208     if (Result == eEnumerateDirectoryResultQuit)
209       return;
210     if (Result == eEnumerateDirectoryResultNext) {
211       // Default behavior is to recurse. Opt out if the callback doesn't want
212       // this behavior.
213       Iter.no_push();
214     }
215   }
216 }
217 
218 std::error_code FileSystem::MakeAbsolute(SmallVectorImpl<char> &path) const {
219   return m_fs->makeAbsolute(path);
220 }
221 
222 std::error_code FileSystem::MakeAbsolute(FileSpec &file_spec) const {
223   SmallString<128> path;
224   file_spec.GetPath(path, false);
225 
226   auto EC = MakeAbsolute(path);
227   if (EC)
228     return EC;
229 
230   FileSpec new_file_spec(path, file_spec.GetPathStyle());
231   file_spec = new_file_spec;
232   return {};
233 }
234 
235 std::error_code FileSystem::GetRealPath(const Twine &path,
236                                         SmallVectorImpl<char> &output) const {
237   return m_fs->getRealPath(path, output);
238 }
239 
240 void FileSystem::Resolve(SmallVectorImpl<char> &path) {
241   if (path.empty())
242     return;
243 
244   // Resolve tilde in path.
245   SmallString<128> resolved(path.begin(), path.end());
246   StandardTildeExpressionResolver Resolver;
247   Resolver.ResolveFullPath(llvm::StringRef(path.begin(), path.size()),
248                            resolved);
249 
250   // Try making the path absolute if it exists.
251   SmallString<128> absolute(resolved.begin(), resolved.end());
252   MakeAbsolute(absolute);
253 
254   path.clear();
255   if (Exists(absolute)) {
256     path.append(absolute.begin(), absolute.end());
257   } else {
258     path.append(resolved.begin(), resolved.end());
259   }
260 }
261 
262 void FileSystem::Resolve(FileSpec &file_spec) {
263   if (!file_spec)
264     return;
265 
266   // Extract path from the FileSpec.
267   SmallString<128> path;
268   file_spec.GetPath(path);
269 
270   // Resolve the path.
271   Resolve(path);
272 
273   // Update the FileSpec with the resolved path.
274   if (file_spec.GetFilename().IsEmpty())
275     file_spec.GetDirectory().SetString(path);
276   else
277     file_spec.SetPath(path);
278   file_spec.SetIsResolved(true);
279 }
280 
281 std::shared_ptr<DataBufferLLVM>
282 FileSystem::CreateDataBuffer(const llvm::Twine &path, uint64_t size,
283                              uint64_t offset) {
284   Collect(path);
285 
286   const bool is_volatile = !IsLocal(path);
287   std::unique_ptr<llvm::WritableMemoryBuffer> buffer;
288   if (size == 0) {
289     auto buffer_or_error =
290         llvm::WritableMemoryBuffer::getFile(path, is_volatile);
291     if (!buffer_or_error)
292       return nullptr;
293     buffer = std::move(*buffer_or_error);
294   } else {
295     auto buffer_or_error = llvm::WritableMemoryBuffer::getFileSlice(
296         path, size, offset, is_volatile);
297     if (!buffer_or_error)
298       return nullptr;
299     buffer = std::move(*buffer_or_error);
300   }
301   return std::shared_ptr<DataBufferLLVM>(new DataBufferLLVM(std::move(buffer)));
302 }
303 
304 std::shared_ptr<DataBufferLLVM>
305 FileSystem::CreateDataBuffer(const FileSpec &file_spec, uint64_t size,
306                              uint64_t offset) {
307   return CreateDataBuffer(file_spec.GetPath(), size, offset);
308 }
309 
310 bool FileSystem::ResolveExecutableLocation(FileSpec &file_spec) {
311   // If the directory is set there's nothing to do.
312   ConstString directory = file_spec.GetDirectory();
313   if (directory)
314     return false;
315 
316   // We cannot look for a file if there's no file name.
317   ConstString filename = file_spec.GetFilename();
318   if (!filename)
319     return false;
320 
321   // Search for the file on the host.
322   const std::string filename_str(filename.GetCString());
323   llvm::ErrorOr<std::string> error_or_path =
324       llvm::sys::findProgramByName(filename_str);
325   if (!error_or_path)
326     return false;
327 
328   // findProgramByName returns "." if it can't find the file.
329   llvm::StringRef path = *error_or_path;
330   llvm::StringRef parent = llvm::sys::path::parent_path(path);
331   if (parent.empty() || parent == ".")
332     return false;
333 
334   // Make sure that the result exists.
335   FileSpec result(*error_or_path);
336   if (!Exists(result))
337     return false;
338 
339   file_spec = result;
340   return true;
341 }
342 
343 bool FileSystem::GetHomeDirectory(SmallVectorImpl<char> &path) const {
344   if (!m_home_directory.empty()) {
345     path.assign(m_home_directory.begin(), m_home_directory.end());
346     return true;
347   }
348   return llvm::sys::path::home_directory(path);
349 }
350 
351 bool FileSystem::GetHomeDirectory(FileSpec &file_spec) const {
352   SmallString<128> home_dir;
353   if (!GetHomeDirectory(home_dir))
354     return false;
355   file_spec.SetPath(home_dir);
356   return true;
357 }
358 
359 static int OpenWithFS(const FileSystem &fs, const char *path, int flags,
360                       int mode) {
361   return const_cast<FileSystem &>(fs).Open(path, flags, mode);
362 }
363 
364 static int GetOpenFlags(File::OpenOptions options) {
365   int open_flags = 0;
366   File::OpenOptions rw =
367       options & (File::eOpenOptionReadOnly | File::eOpenOptionWriteOnly |
368                  File::eOpenOptionReadWrite);
369   if (rw == File::eOpenOptionWriteOnly || rw == File::eOpenOptionReadWrite) {
370     if (rw == File::eOpenOptionReadWrite)
371       open_flags |= O_RDWR;
372     else
373       open_flags |= O_WRONLY;
374 
375     if (options & File::eOpenOptionAppend)
376       open_flags |= O_APPEND;
377 
378     if (options & File::eOpenOptionTruncate)
379       open_flags |= O_TRUNC;
380 
381     if (options & File::eOpenOptionCanCreate)
382       open_flags |= O_CREAT;
383 
384     if (options & File::eOpenOptionCanCreateNewOnly)
385       open_flags |= O_CREAT | O_EXCL;
386   } else if (rw == File::eOpenOptionReadOnly) {
387     open_flags |= O_RDONLY;
388 
389 #ifndef _WIN32
390     if (options & File::eOpenOptionDontFollowSymlinks)
391       open_flags |= O_NOFOLLOW;
392 #endif
393   }
394 
395 #ifndef _WIN32
396   if (options & File::eOpenOptionNonBlocking)
397     open_flags |= O_NONBLOCK;
398   if (options & File::eOpenOptionCloseOnExec)
399     open_flags |= O_CLOEXEC;
400 #else
401   open_flags |= O_BINARY;
402 #endif
403 
404   return open_flags;
405 }
406 
407 static mode_t GetOpenMode(uint32_t permissions) {
408   mode_t mode = 0;
409   if (permissions & lldb::eFilePermissionsUserRead)
410     mode |= S_IRUSR;
411   if (permissions & lldb::eFilePermissionsUserWrite)
412     mode |= S_IWUSR;
413   if (permissions & lldb::eFilePermissionsUserExecute)
414     mode |= S_IXUSR;
415   if (permissions & lldb::eFilePermissionsGroupRead)
416     mode |= S_IRGRP;
417   if (permissions & lldb::eFilePermissionsGroupWrite)
418     mode |= S_IWGRP;
419   if (permissions & lldb::eFilePermissionsGroupExecute)
420     mode |= S_IXGRP;
421   if (permissions & lldb::eFilePermissionsWorldRead)
422     mode |= S_IROTH;
423   if (permissions & lldb::eFilePermissionsWorldWrite)
424     mode |= S_IWOTH;
425   if (permissions & lldb::eFilePermissionsWorldExecute)
426     mode |= S_IXOTH;
427   return mode;
428 }
429 
430 Expected<FileUP> FileSystem::Open(const FileSpec &file_spec,
431                                   File::OpenOptions options,
432                                   uint32_t permissions, bool should_close_fd) {
433   Collect(file_spec.GetPath());
434 
435   const int open_flags = GetOpenFlags(options);
436   const mode_t open_mode =
437       (open_flags & O_CREAT) ? GetOpenMode(permissions) : 0;
438 
439   auto path = file_spec.GetPath();
440 
441   int descriptor = llvm::sys::RetryAfterSignal(
442       -1, OpenWithFS, *this, path.c_str(), open_flags, open_mode);
443 
444   if (!File::DescriptorIsValid(descriptor))
445     return llvm::errorCodeToError(
446         std::error_code(errno, std::system_category()));
447 
448   auto file = std::unique_ptr<File>(
449       new NativeFile(descriptor, options, should_close_fd));
450   assert(file->IsValid());
451   return std::move(file);
452 }
453 
454 void FileSystem::Collect(const FileSpec &file_spec) {
455   Collect(file_spec.GetPath());
456 }
457 
458 void FileSystem::Collect(const llvm::Twine &file) {
459   if (!m_collector)
460     return;
461 
462   if (llvm::sys::fs::is_directory(file))
463     m_collector->addDirectory(file);
464   else
465     m_collector->addFile(file);
466 }
467 
468 void FileSystem::SetHomeDirectory(std::string home_directory) {
469   m_home_directory = std::move(home_directory);
470 }
471 
472 Status FileSystem::RemoveFile(const FileSpec &file_spec) {
473   return RemoveFile(file_spec.GetPath());
474 }
475 
476 Status FileSystem::RemoveFile(const llvm::Twine &path) {
477   return Status(llvm::sys::fs::remove(path));
478 }
479