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