1 //===-- FileSystem.cpp ------------------------------------------*- C++ -*-===//
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 <errno.h>
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <stdarg.h>
26 #include <stdio.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(FileCollector &collector) {
53   lldbassert(!InstanceImpl() && "Already initialized.");
54   InstanceImpl().emplace(collector);
55 }
56 
57 llvm::Error FileSystem::Initialize(const FileSpec &mapping) {
58   lldbassert(!InstanceImpl() && "Already initialized.");
59 
60   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer =
61       llvm::vfs::getRealFileSystem()->getBufferForFile(mapping.GetPath());
62 
63   if (!buffer)
64     return llvm::errorCodeToError(buffer.getError());
65 
66   InstanceImpl().emplace(
67       llvm::vfs::getVFSFromYAML(std::move(buffer.get()), nullptr, ""), true);
68 
69   return llvm::Error::success();
70 }
71 
72 void FileSystem::Initialize(IntrusiveRefCntPtr<vfs::FileSystem> fs) {
73   lldbassert(!InstanceImpl() && "Already initialized.");
74   InstanceImpl().emplace(fs);
75 }
76 
77 void FileSystem::Terminate() {
78   lldbassert(InstanceImpl() && "Already terminated.");
79   InstanceImpl().reset();
80 }
81 
82 Optional<FileSystem> &FileSystem::InstanceImpl() {
83   static Optional<FileSystem> g_fs;
84   return g_fs;
85 }
86 
87 vfs::directory_iterator FileSystem::DirBegin(const FileSpec &file_spec,
88                                              std::error_code &ec) {
89   return DirBegin(file_spec.GetPath(), ec);
90 }
91 
92 vfs::directory_iterator FileSystem::DirBegin(const Twine &dir,
93                                              std::error_code &ec) {
94   return m_fs->dir_begin(dir, ec);
95 }
96 
97 llvm::ErrorOr<vfs::Status>
98 FileSystem::GetStatus(const FileSpec &file_spec) const {
99   return GetStatus(file_spec.GetPath());
100 }
101 
102 llvm::ErrorOr<vfs::Status> FileSystem::GetStatus(const Twine &path) const {
103   return m_fs->status(path);
104 }
105 
106 sys::TimePoint<>
107 FileSystem::GetModificationTime(const FileSpec &file_spec) const {
108   return GetModificationTime(file_spec.GetPath());
109 }
110 
111 sys::TimePoint<> FileSystem::GetModificationTime(const Twine &path) const {
112   ErrorOr<vfs::Status> status = m_fs->status(path);
113   if (!status)
114     return sys::TimePoint<>();
115   return status->getLastModificationTime();
116 }
117 
118 uint64_t FileSystem::GetByteSize(const FileSpec &file_spec) const {
119   return GetByteSize(file_spec.GetPath());
120 }
121 
122 uint64_t FileSystem::GetByteSize(const Twine &path) const {
123   ErrorOr<vfs::Status> status = m_fs->status(path);
124   if (!status)
125     return 0;
126   return status->getSize();
127 }
128 
129 uint32_t FileSystem::GetPermissions(const FileSpec &file_spec) const {
130   return GetPermissions(file_spec.GetPath());
131 }
132 
133 uint32_t FileSystem::GetPermissions(const FileSpec &file_spec,
134                                     std::error_code &ec) const {
135   return GetPermissions(file_spec.GetPath(), ec);
136 }
137 
138 uint32_t FileSystem::GetPermissions(const Twine &path) const {
139   std::error_code ec;
140   return GetPermissions(path, ec);
141 }
142 
143 uint32_t FileSystem::GetPermissions(const Twine &path,
144                                     std::error_code &ec) const {
145   ErrorOr<vfs::Status> status = m_fs->status(path);
146   if (!status) {
147     ec = status.getError();
148     return sys::fs::perms::perms_not_known;
149   }
150   return status->getPermissions();
151 }
152 
153 bool FileSystem::Exists(const Twine &path) const { return m_fs->exists(path); }
154 
155 bool FileSystem::Exists(const FileSpec &file_spec) const {
156   return Exists(file_spec.GetPath());
157 }
158 
159 bool FileSystem::Readable(const Twine &path) const {
160   return GetPermissions(path) & sys::fs::perms::all_read;
161 }
162 
163 bool FileSystem::Readable(const FileSpec &file_spec) const {
164   return Readable(file_spec.GetPath());
165 }
166 
167 bool FileSystem::IsDirectory(const Twine &path) const {
168   ErrorOr<vfs::Status> status = m_fs->status(path);
169   if (!status)
170     return false;
171   return status->isDirectory();
172 }
173 
174 bool FileSystem::IsDirectory(const FileSpec &file_spec) const {
175   return IsDirectory(file_spec.GetPath());
176 }
177 
178 bool FileSystem::IsLocal(const Twine &path) const {
179   bool b = false;
180   m_fs->isLocal(path, b);
181   return b;
182 }
183 
184 bool FileSystem::IsLocal(const FileSpec &file_spec) const {
185   return 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.
245   SmallString<128> original_path(path.begin(), path.end());
246   StandardTildeExpressionResolver Resolver;
247   Resolver.ResolveFullPath(original_path, path);
248 
249   // Try making the path absolute if it exists.
250   SmallString<128> absolute_path(path.begin(), path.end());
251   MakeAbsolute(path);
252   if (!Exists(path)) {
253     path.clear();
254     path.append(original_path.begin(), original_path.end());
255   }
256 }
257 
258 void FileSystem::Resolve(FileSpec &file_spec) {
259   // Extract path from the FileSpec.
260   SmallString<128> path;
261   file_spec.GetPath(path);
262 
263   // Resolve the path.
264   Resolve(path);
265 
266   // Update the FileSpec with the resolved path.
267   file_spec.SetPath(path);
268   file_spec.SetIsResolved(true);
269 }
270 
271 std::shared_ptr<DataBufferLLVM>
272 FileSystem::CreateDataBuffer(const llvm::Twine &path, uint64_t size,
273                              uint64_t offset) {
274   if (m_collector)
275     m_collector->AddFile(path);
276 
277   const bool is_volatile = !IsLocal(path);
278   const ErrorOr<std::string> external_path = GetExternalPath(path);
279 
280   if (!external_path)
281     return nullptr;
282 
283   std::unique_ptr<llvm::WritableMemoryBuffer> buffer;
284   if (size == 0) {
285     auto buffer_or_error =
286         llvm::WritableMemoryBuffer::getFile(*external_path, -1, is_volatile);
287     if (!buffer_or_error)
288       return nullptr;
289     buffer = std::move(*buffer_or_error);
290   } else {
291     auto buffer_or_error = llvm::WritableMemoryBuffer::getFileSlice(
292         *external_path, size, offset, is_volatile);
293     if (!buffer_or_error)
294       return nullptr;
295     buffer = std::move(*buffer_or_error);
296   }
297   return std::shared_ptr<DataBufferLLVM>(new DataBufferLLVM(std::move(buffer)));
298 }
299 
300 std::shared_ptr<DataBufferLLVM>
301 FileSystem::CreateDataBuffer(const FileSpec &file_spec, uint64_t size,
302                              uint64_t offset) {
303   return CreateDataBuffer(file_spec.GetPath(), size, offset);
304 }
305 
306 bool FileSystem::ResolveExecutableLocation(FileSpec &file_spec) {
307   // If the directory is set there's nothing to do.
308   const ConstString &directory = file_spec.GetDirectory();
309   if (directory)
310     return false;
311 
312   // We cannot look for a file if there's no file name.
313   const ConstString &filename = file_spec.GetFilename();
314   if (!filename)
315     return false;
316 
317   // Search for the file on the host.
318   const std::string filename_str(filename.GetCString());
319   llvm::ErrorOr<std::string> error_or_path =
320       llvm::sys::findProgramByName(filename_str);
321   if (!error_or_path)
322     return false;
323 
324   // findProgramByName returns "." if it can't find the file.
325   llvm::StringRef path = *error_or_path;
326   llvm::StringRef parent = llvm::sys::path::parent_path(path);
327   if (parent.empty() || parent == ".")
328     return false;
329 
330   // Make sure that the result exists.
331   FileSpec result(*error_or_path);
332   if (!Exists(result))
333     return false;
334 
335   file_spec = result;
336   return true;
337 }
338 
339 static int OpenWithFS(const FileSystem &fs, const char *path, int flags,
340                       int mode) {
341   return const_cast<FileSystem &>(fs).Open(path, flags, mode);
342 }
343 
344 static int GetOpenFlags(uint32_t options) {
345   const bool read = options & File::eOpenOptionRead;
346   const bool write = options & File::eOpenOptionWrite;
347 
348   int open_flags = 0;
349   if (write) {
350     if (read)
351       open_flags |= O_RDWR;
352     else
353       open_flags |= O_WRONLY;
354 
355     if (options & File::eOpenOptionAppend)
356       open_flags |= O_APPEND;
357 
358     if (options & File::eOpenOptionTruncate)
359       open_flags |= O_TRUNC;
360 
361     if (options & File::eOpenOptionCanCreate)
362       open_flags |= O_CREAT;
363 
364     if (options & File::eOpenOptionCanCreateNewOnly)
365       open_flags |= O_CREAT | O_EXCL;
366   } else if (read) {
367     open_flags |= O_RDONLY;
368 
369 #ifndef _WIN32
370     if (options & File::eOpenOptionDontFollowSymlinks)
371       open_flags |= O_NOFOLLOW;
372 #endif
373   }
374 
375 #ifndef _WIN32
376   if (options & File::eOpenOptionNonBlocking)
377     open_flags |= O_NONBLOCK;
378   if (options & File::eOpenOptionCloseOnExec)
379     open_flags |= O_CLOEXEC;
380 #else
381   open_flags |= O_BINARY;
382 #endif
383 
384   return open_flags;
385 }
386 
387 static mode_t GetOpenMode(uint32_t permissions) {
388   mode_t mode = 0;
389   if (permissions & lldb::eFilePermissionsUserRead)
390     mode |= S_IRUSR;
391   if (permissions & lldb::eFilePermissionsUserWrite)
392     mode |= S_IWUSR;
393   if (permissions & lldb::eFilePermissionsUserExecute)
394     mode |= S_IXUSR;
395   if (permissions & lldb::eFilePermissionsGroupRead)
396     mode |= S_IRGRP;
397   if (permissions & lldb::eFilePermissionsGroupWrite)
398     mode |= S_IWGRP;
399   if (permissions & lldb::eFilePermissionsGroupExecute)
400     mode |= S_IXGRP;
401   if (permissions & lldb::eFilePermissionsWorldRead)
402     mode |= S_IROTH;
403   if (permissions & lldb::eFilePermissionsWorldWrite)
404     mode |= S_IWOTH;
405   if (permissions & lldb::eFilePermissionsWorldExecute)
406     mode |= S_IXOTH;
407   return mode;
408 }
409 
410 Status FileSystem::Open(File &File, const FileSpec &file_spec, uint32_t options,
411                         uint32_t permissions) {
412   if (m_collector)
413     m_collector->AddFile(file_spec);
414 
415   if (File.IsValid())
416     File.Close();
417 
418   const int open_flags = GetOpenFlags(options);
419   const mode_t open_mode =
420       (open_flags & O_CREAT) ? GetOpenMode(permissions) : 0;
421 
422   auto path = GetExternalPath(file_spec);
423   if (!path)
424     return Status(path.getError());
425 
426   int descriptor = llvm::sys::RetryAfterSignal(
427       -1, OpenWithFS, *this, path->c_str(), open_flags, open_mode);
428 
429   Status error;
430   if (!File::DescriptorIsValid(descriptor)) {
431     File.SetDescriptor(descriptor, false);
432     error.SetErrorToErrno();
433   } else {
434     File.SetDescriptor(descriptor, true);
435     File.SetOptions(options);
436   }
437   return error;
438 }
439 
440 ErrorOr<std::string> FileSystem::GetExternalPath(const llvm::Twine &path) {
441   if (!m_mapped)
442     return path.str();
443 
444   // If VFS mapped we know the underlying FS is a RedirectingFileSystem.
445   ErrorOr<vfs::RedirectingFileSystem::Entry *> E =
446       static_cast<vfs::RedirectingFileSystem &>(*m_fs).lookupPath(path);
447   if (!E) {
448     if (E.getError() == llvm::errc::no_such_file_or_directory) {
449       return path.str();
450     }
451     return E.getError();
452   }
453 
454   auto *F = dyn_cast<vfs::RedirectingFileSystem::RedirectingFileEntry>(*E);
455   if (!F)
456     return make_error_code(llvm::errc::not_supported);
457 
458   return F->getExternalContentsPath().str();
459 }
460 
461 ErrorOr<std::string> FileSystem::GetExternalPath(const FileSpec &file_spec) {
462   return GetExternalPath(file_spec.GetPath());
463 }
464