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 std::shared_ptr<DataBuffer>
277 FileSystem::CreateDataBuffer(const llvm::Twine &path, uint64_t size,
278                              uint64_t offset) {
279   const bool is_volatile = !IsLocal(path);
280   std::unique_ptr<llvm::WritableMemoryBuffer> buffer;
281   if (size == 0) {
282     auto buffer_or_error =
283         llvm::WritableMemoryBuffer::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 = llvm::WritableMemoryBuffer::getFileSlice(
289         path, size, offset, is_volatile);
290     if (!buffer_or_error)
291       return nullptr;
292     buffer = std::move(*buffer_or_error);
293   }
294   return std::shared_ptr<DataBufferLLVM>(new DataBufferLLVM(std::move(buffer)));
295 }
296 
297 std::shared_ptr<DataBuffer>
298 FileSystem::CreateDataBuffer(const FileSpec &file_spec, uint64_t size,
299                              uint64_t offset) {
300   return CreateDataBuffer(file_spec.GetPath(), size, offset);
301 }
302 
303 bool FileSystem::ResolveExecutableLocation(FileSpec &file_spec) {
304   // If the directory is set there's nothing to do.
305   ConstString directory = file_spec.GetDirectory();
306   if (directory)
307     return false;
308 
309   // We cannot look for a file if there's no file name.
310   ConstString filename = file_spec.GetFilename();
311   if (!filename)
312     return false;
313 
314   // Search for the file on the host.
315   const std::string filename_str(filename.GetCString());
316   llvm::ErrorOr<std::string> error_or_path =
317       llvm::sys::findProgramByName(filename_str);
318   if (!error_or_path)
319     return false;
320 
321   // findProgramByName returns "." if it can't find the file.
322   llvm::StringRef path = *error_or_path;
323   llvm::StringRef parent = llvm::sys::path::parent_path(path);
324   if (parent.empty() || parent == ".")
325     return false;
326 
327   // Make sure that the result exists.
328   FileSpec result(*error_or_path);
329   if (!Exists(result))
330     return false;
331 
332   file_spec = result;
333   return true;
334 }
335 
336 bool FileSystem::GetHomeDirectory(SmallVectorImpl<char> &path) const {
337   if (!m_home_directory.empty()) {
338     path.assign(m_home_directory.begin(), m_home_directory.end());
339     return true;
340   }
341   return llvm::sys::path::home_directory(path);
342 }
343 
344 bool FileSystem::GetHomeDirectory(FileSpec &file_spec) const {
345   SmallString<128> home_dir;
346   if (!GetHomeDirectory(home_dir))
347     return false;
348   file_spec.SetPath(home_dir);
349   return true;
350 }
351 
352 static int OpenWithFS(const FileSystem &fs, const char *path, int flags,
353                       int mode) {
354   return const_cast<FileSystem &>(fs).Open(path, flags, mode);
355 }
356 
357 static int GetOpenFlags(File::OpenOptions options) {
358   int open_flags = 0;
359   File::OpenOptions rw =
360       options & (File::eOpenOptionReadOnly | File::eOpenOptionWriteOnly |
361                  File::eOpenOptionReadWrite);
362   if (rw == File::eOpenOptionWriteOnly || rw == File::eOpenOptionReadWrite) {
363     if (rw == File::eOpenOptionReadWrite)
364       open_flags |= O_RDWR;
365     else
366       open_flags |= O_WRONLY;
367 
368     if (options & File::eOpenOptionAppend)
369       open_flags |= O_APPEND;
370 
371     if (options & File::eOpenOptionTruncate)
372       open_flags |= O_TRUNC;
373 
374     if (options & File::eOpenOptionCanCreate)
375       open_flags |= O_CREAT;
376 
377     if (options & File::eOpenOptionCanCreateNewOnly)
378       open_flags |= O_CREAT | O_EXCL;
379   } else if (rw == File::eOpenOptionReadOnly) {
380     open_flags |= O_RDONLY;
381 
382 #ifndef _WIN32
383     if (options & File::eOpenOptionDontFollowSymlinks)
384       open_flags |= O_NOFOLLOW;
385 #endif
386   }
387 
388 #ifndef _WIN32
389   if (options & File::eOpenOptionNonBlocking)
390     open_flags |= O_NONBLOCK;
391   if (options & File::eOpenOptionCloseOnExec)
392     open_flags |= O_CLOEXEC;
393 #else
394   open_flags |= O_BINARY;
395 #endif
396 
397   return open_flags;
398 }
399 
400 static mode_t GetOpenMode(uint32_t permissions) {
401   mode_t mode = 0;
402   if (permissions & lldb::eFilePermissionsUserRead)
403     mode |= S_IRUSR;
404   if (permissions & lldb::eFilePermissionsUserWrite)
405     mode |= S_IWUSR;
406   if (permissions & lldb::eFilePermissionsUserExecute)
407     mode |= S_IXUSR;
408   if (permissions & lldb::eFilePermissionsGroupRead)
409     mode |= S_IRGRP;
410   if (permissions & lldb::eFilePermissionsGroupWrite)
411     mode |= S_IWGRP;
412   if (permissions & lldb::eFilePermissionsGroupExecute)
413     mode |= S_IXGRP;
414   if (permissions & lldb::eFilePermissionsWorldRead)
415     mode |= S_IROTH;
416   if (permissions & lldb::eFilePermissionsWorldWrite)
417     mode |= S_IWOTH;
418   if (permissions & lldb::eFilePermissionsWorldExecute)
419     mode |= S_IXOTH;
420   return mode;
421 }
422 
423 Expected<FileUP> FileSystem::Open(const FileSpec &file_spec,
424                                   File::OpenOptions options,
425                                   uint32_t permissions, bool should_close_fd) {
426   const int open_flags = GetOpenFlags(options);
427   const mode_t open_mode =
428       (open_flags & O_CREAT) ? GetOpenMode(permissions) : 0;
429 
430   auto path = file_spec.GetPath();
431 
432   int descriptor = llvm::sys::RetryAfterSignal(
433       -1, OpenWithFS, *this, path.c_str(), open_flags, open_mode);
434 
435   if (!File::DescriptorIsValid(descriptor))
436     return llvm::errorCodeToError(
437         std::error_code(errno, std::system_category()));
438 
439   auto file = std::unique_ptr<File>(
440       new NativeFile(descriptor, options, should_close_fd));
441   assert(file->IsValid());
442   return std::move(file);
443 }
444 
445 void FileSystem::SetHomeDirectory(std::string home_directory) {
446   m_home_directory = std::move(home_directory);
447 }
448 
449 Status FileSystem::RemoveFile(const FileSpec &file_spec) {
450   return RemoveFile(file_spec.GetPath());
451 }
452 
453 Status FileSystem::RemoveFile(const llvm::Twine &path) {
454   return Status(llvm::sys::fs::remove(path));
455 }
456