LLDB  mainline
common/FileSystem.cpp
Go to the documentation of this file.
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 
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
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 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(llvm::vfs::getVFSFromYAML(std::move(buffer.get()),
67  nullptr, mapping.GetPath()),
68  true);
69 
70  return llvm::Error::success();
71 }
72 
73 void FileSystem::Initialize(IntrusiveRefCntPtr<vfs::FileSystem> fs) {
74  lldbassert(!InstanceImpl() && "Already initialized.");
75  InstanceImpl().emplace(fs);
76 }
77 
78 void FileSystem::Terminate() {
79  lldbassert(InstanceImpl() && "Already terminated.");
80  InstanceImpl().reset();
81 }
82 
83 Optional<FileSystem> &FileSystem::InstanceImpl() {
84  static Optional<FileSystem> g_fs;
85  return g_fs;
86 }
87 
88 vfs::directory_iterator FileSystem::DirBegin(const FileSpec &file_spec,
89  std::error_code &ec) {
90  if (!file_spec) {
91  ec = std::error_code(static_cast<int>(errc::no_such_file_or_directory),
92  std::system_category());
93  return {};
94  }
95  return DirBegin(file_spec.GetPath(), ec);
96 }
97 
98 vfs::directory_iterator FileSystem::DirBegin(const Twine &dir,
99  std::error_code &ec) {
100  return m_fs->dir_begin(dir, ec);
101 }
102 
103 llvm::ErrorOr<vfs::Status>
104 FileSystem::GetStatus(const FileSpec &file_spec) const {
105  if (!file_spec)
106  return std::error_code(static_cast<int>(errc::no_such_file_or_directory),
107  std::system_category());
108  return GetStatus(file_spec.GetPath());
109 }
110 
111 llvm::ErrorOr<vfs::Status> FileSystem::GetStatus(const Twine &path) const {
112  return m_fs->status(path);
113 }
114 
115 sys::TimePoint<>
116 FileSystem::GetModificationTime(const FileSpec &file_spec) const {
117  if (!file_spec)
118  return sys::TimePoint<>();
119  return GetModificationTime(file_spec.GetPath());
120 }
121 
122 sys::TimePoint<> FileSystem::GetModificationTime(const Twine &path) const {
123  ErrorOr<vfs::Status> status = m_fs->status(path);
124  if (!status)
125  return sys::TimePoint<>();
126  return status->getLastModificationTime();
127 }
128 
129 uint64_t FileSystem::GetByteSize(const FileSpec &file_spec) const {
130  if (!file_spec)
131  return 0;
132  return GetByteSize(file_spec.GetPath());
133 }
134 
135 uint64_t FileSystem::GetByteSize(const Twine &path) const {
136  ErrorOr<vfs::Status> status = m_fs->status(path);
137  if (!status)
138  return 0;
139  return status->getSize();
140 }
141 
143  return GetPermissions(file_spec.GetPath());
144 }
145 
147  std::error_code &ec) const {
148  if (!file_spec)
149  return sys::fs::perms::perms_not_known;
150  return GetPermissions(file_spec.GetPath(), ec);
151 }
152 
153 uint32_t FileSystem::GetPermissions(const Twine &path) const {
154  std::error_code ec;
155  return GetPermissions(path, ec);
156 }
157 
158 uint32_t FileSystem::GetPermissions(const Twine &path,
159  std::error_code &ec) const {
160  ErrorOr<vfs::Status> status = m_fs->status(path);
161  if (!status) {
162  ec = status.getError();
163  return sys::fs::perms::perms_not_known;
164  }
165  return status->getPermissions();
166 }
167 
168 bool FileSystem::Exists(const Twine &path) const { return m_fs->exists(path); }
169 
170 bool FileSystem::Exists(const FileSpec &file_spec) const {
171  return file_spec && Exists(file_spec.GetPath());
172 }
173 
174 bool FileSystem::Readable(const Twine &path) const {
175  return GetPermissions(path) & sys::fs::perms::all_read;
176 }
177 
178 bool FileSystem::Readable(const FileSpec &file_spec) const {
179  return file_spec && Readable(file_spec.GetPath());
180 }
181 
182 bool FileSystem::IsDirectory(const Twine &path) const {
183  ErrorOr<vfs::Status> status = m_fs->status(path);
184  if (!status)
185  return false;
186  return status->isDirectory();
187 }
188 
189 bool FileSystem::IsDirectory(const FileSpec &file_spec) const {
190  return file_spec && IsDirectory(file_spec.GetPath());
191 }
192 
193 bool FileSystem::IsLocal(const Twine &path) const {
194  bool b = false;
195  m_fs->isLocal(path, b);
196  return b;
197 }
198 
199 bool FileSystem::IsLocal(const FileSpec &file_spec) const {
200  return file_spec && IsLocal(file_spec.GetPath());
201 }
202 
203 void FileSystem::EnumerateDirectory(Twine path, bool find_directories,
204  bool find_files, bool find_other,
205  EnumerateDirectoryCallbackType callback,
206  void *callback_baton) {
207  std::error_code EC;
208  vfs::recursive_directory_iterator Iter(*m_fs, path, EC);
209  vfs::recursive_directory_iterator End;
210  for (; Iter != End && !EC; Iter.increment(EC)) {
211  const auto &Item = *Iter;
212  ErrorOr<vfs::Status> Status = m_fs->status(Item.path());
213  if (!Status)
214  break;
215  if (!find_files && Status->isRegularFile())
216  continue;
217  if (!find_directories && Status->isDirectory())
218  continue;
219  if (!find_other && Status->isOther())
220  continue;
221 
222  auto Result = callback(callback_baton, Status->getType(), Item.path());
223  if (Result == eEnumerateDirectoryResultQuit)
224  return;
225  if (Result == eEnumerateDirectoryResultNext) {
226  // Default behavior is to recurse. Opt out if the callback doesn't want
227  // this behavior.
228  Iter.no_push();
229  }
230  }
231 }
232 
233 std::error_code FileSystem::MakeAbsolute(SmallVectorImpl<char> &path) const {
234  return m_fs->makeAbsolute(path);
235 }
236 
237 std::error_code FileSystem::MakeAbsolute(FileSpec &file_spec) const {
238  SmallString<128> path;
239  file_spec.GetPath(path, false);
240 
241  auto EC = MakeAbsolute(path);
242  if (EC)
243  return EC;
244 
245  FileSpec new_file_spec(path, file_spec.GetPathStyle());
246  file_spec = new_file_spec;
247  return {};
248 }
249 
250 std::error_code FileSystem::GetRealPath(const Twine &path,
251  SmallVectorImpl<char> &output) const {
252  return m_fs->getRealPath(path, output);
253 }
254 
255 void FileSystem::Resolve(SmallVectorImpl<char> &path) {
256  if (path.empty())
257  return;
258 
259  // Resolve tilde in path.
260  SmallString<128> resolved(path.begin(), path.end());
262  Resolver.ResolveFullPath(llvm::StringRef(path.begin(), path.size()),
263  resolved);
264 
265  // Try making the path absolute if it exists.
266  SmallString<128> absolute(resolved.begin(), resolved.end());
267  MakeAbsolute(absolute);
268 
269  path.clear();
270  if (Exists(absolute)) {
271  path.append(absolute.begin(), absolute.end());
272  } else {
273  path.append(resolved.begin(), resolved.end());
274  }
275 }
276 
277 void FileSystem::Resolve(FileSpec &file_spec) {
278  if (!file_spec)
279  return;
280 
281  // Extract path from the FileSpec.
282  SmallString<128> path;
283  file_spec.GetPath(path);
284 
285  // Resolve the path.
286  Resolve(path);
287 
288  // Update the FileSpec with the resolved path.
289  if (file_spec.GetFilename().IsEmpty())
290  file_spec.GetDirectory().SetString(path);
291  else
292  file_spec.SetPath(path);
293  file_spec.SetIsResolved(true);
294 }
295 
296 std::shared_ptr<DataBufferLLVM>
297 FileSystem::CreateDataBuffer(const llvm::Twine &path, uint64_t size,
298  uint64_t offset) {
299  Collect(path);
300 
301  const bool is_volatile = !IsLocal(path);
302  const ErrorOr<std::string> external_path = GetExternalPath(path);
303 
304  if (!external_path)
305  return nullptr;
306 
307  std::unique_ptr<llvm::WritableMemoryBuffer> buffer;
308  if (size == 0) {
309  auto buffer_or_error =
310  llvm::WritableMemoryBuffer::getFile(*external_path, is_volatile);
311  if (!buffer_or_error)
312  return nullptr;
313  buffer = std::move(*buffer_or_error);
314  } else {
315  auto buffer_or_error = llvm::WritableMemoryBuffer::getFileSlice(
316  *external_path, size, offset, is_volatile);
317  if (!buffer_or_error)
318  return nullptr;
319  buffer = std::move(*buffer_or_error);
320  }
321  return std::shared_ptr<DataBufferLLVM>(new DataBufferLLVM(std::move(buffer)));
322 }
323 
324 std::shared_ptr<DataBufferLLVM>
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  Collect(file_spec.GetPath());
454 
455  const int open_flags = GetOpenFlags(options);
456  const mode_t open_mode =
457  (open_flags & O_CREAT) ? GetOpenMode(permissions) : 0;
458 
459  auto path = GetExternalPath(file_spec);
460  if (!path)
461  return errorCodeToError(path.getError());
462 
463  int descriptor = llvm::sys::RetryAfterSignal(
464  -1, OpenWithFS, *this, path->c_str(), open_flags, open_mode);
465 
466  if (!File::DescriptorIsValid(descriptor))
467  return llvm::errorCodeToError(
468  std::error_code(errno, std::system_category()));
469 
470  auto file = std::unique_ptr<File>(
471  new NativeFile(descriptor, options, should_close_fd));
472  assert(file->IsValid());
473  return std::move(file);
474 }
475 
476 ErrorOr<std::string> FileSystem::GetExternalPath(const llvm::Twine &path) {
477  if (!m_mapped)
478  return path.str();
479 
480  // If VFS mapped we know the underlying FS is a RedirectingFileSystem.
481  ErrorOr<vfs::RedirectingFileSystem::LookupResult> Result =
482  static_cast<vfs::RedirectingFileSystem &>(*m_fs).lookupPath(path.str());
483  if (!Result) {
484  if (Result.getError() == llvm::errc::no_such_file_or_directory) {
485  return path.str();
486  }
487  return Result.getError();
488  }
489 
490  if (Optional<StringRef> ExtRedirect = Result->getExternalRedirect())
491  return std::string(*ExtRedirect);
492  return make_error_code(llvm::errc::not_supported);
493 }
494 
495 ErrorOr<std::string> FileSystem::GetExternalPath(const FileSpec &file_spec) {
496  return GetExternalPath(file_spec.GetPath());
497 }
498 
499 void FileSystem::Collect(const FileSpec &file_spec) {
500  Collect(file_spec.GetPath());
501 }
502 
503 void FileSystem::Collect(const llvm::Twine &file) {
504  if (!m_collector)
505  return;
506 
507  if (llvm::sys::fs::is_directory(file))
508  m_collector->addDirectory(file);
509  else
510  m_collector->addFile(file);
511 }
512 
513 void FileSystem::SetHomeDirectory(std::string home_directory) {
514  m_home_directory = std::move(home_directory);
515 }
llvm
Definition: Debugger.h:49
lldb_private::StandardTildeExpressionResolver
Definition: TildeExpressionResolver.h:56
FileSystem.h
O_NONBLOCK
#define O_NONBLOCK
Definition: windows/PosixApi.h:29
lldb_private::File::OpenOptions
OpenOptions
Definition: File.h:48
lldb_private::FileSpec::GetPathStyle
Style GetPathStyle() const
Definition: FileSpec.cpp:332
S_IXUSR
#define S_IXUSR
Definition: windows/PosixApi.h:37
GetPermissions
static Permissions GetPermissions(const ELFSectionHeader &H)
Definition: ObjectFileELF.cpp:1655
lldb_private::FileSystem
Definition: FileSystem.h:29
lldb_private::FileSpec::SetPath
void SetPath(llvm::StringRef p)
Temporary helper for FileSystem change.
Definition: FileSpec.h:270
S_IXGRP
#define S_IXGRP
Definition: windows/PosixApi.h:42
S_IWOTH
#define S_IWOTH
Definition: windows/PosixApi.h:46
S_IWGRP
#define S_IWGRP
Definition: windows/PosixApi.h:41
lldb_private::FileSpec
Definition: FileSpec.h:56
LLDBAssert.h
lldb_private::ConstString::SetString
void SetString(const llvm::StringRef &s)
Definition: ConstString.cpp:306
MakeAbsolute
static FileSpec MakeAbsolute(const FileSpec &file_spec)
Definition: Reproducer.cpp:147
lldb_private::ConstString::IsEmpty
bool IsEmpty() const
Test for empty string.
Definition: ConstString.h:304
OpenWithFS
static int OpenWithFS(const FileSystem &fs, const char *path, int flags, int mode)
Definition: common/FileSystem.cpp:379
lldb_private::FileSpec::SetIsResolved
void SetIsResolved(bool is_resolved)
Set if the file path has been resolved or not.
Definition: FileSpec.h:380
S_IXOTH
#define S_IXOTH
Definition: windows/PosixApi.h:47
lldb_private::TildeExpressionResolver::ResolveFullPath
bool ResolveFullPath(llvm::StringRef Expr, llvm::SmallVectorImpl< char > &Output)
Resolve an entire path that begins with a tilde expression, replacing the username portion with the m...
Definition: TildeExpressionResolver.cpp:76
lldb_private::ConstString
Definition: ConstString.h:40
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:39
lldbassert
#define lldbassert(x)
Definition: LLDBAssert.h:15
S_IWUSR
#define S_IWUSR
Definition: windows/PosixApi.h:36
lldb_private::Status
Definition: Status.h:44
TildeExpressionResolver.h
uint32_t
lldb_private::NativeFile
Definition: File.h:376
windows.h
lldb_private::ConstString::GetCString
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
GetOpenFlags
static int GetOpenFlags(File::OpenOptions options)
Definition: common/FileSystem.cpp:384
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
S_IROTH
#define S_IROTH
Definition: windows/PosixApi.h:45
Error
llvm::Error Error
Definition: UdtRecordCompleter.cpp:29
S_IRUSR
#define S_IRUSR
Definition: windows/PosixApi.h:35
lldb_private::FileSpec::GetDirectory
ConstString & GetDirectory()
Directory string get accessor.
Definition: FileSpec.cpp:335
lldb_private::DataBufferLLVM
Definition: DataBufferLLVM.h:26
llvm::SmallVectorImpl
Definition: Disassembler.h:42
GetOpenMode
static mode_t GetOpenMode(uint32_t permissions)
Definition: common/FileSystem.cpp:427
S_IRGRP
#define S_IRGRP
Definition: windows/PosixApi.h:40
lldb_private::FileSpec::GetPath
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:348
lldb
Definition: SBAddress.h:15
lldb_private::FileSpec::GetFilename
ConstString & GetFilename()
Filename string get accessor.
Definition: FileSpec.cpp:341