LLDB  mainline
FileSystem.h
Go to the documentation of this file.
1 //===-- FileSystem.h --------------------------------------------*- 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 #ifndef LLDB_HOST_FILESYSTEM_H
10 #define LLDB_HOST_FILESYSTEM_H
11 
12 #include "lldb/Host/File.h"
14 #include "lldb/Utility/FileSpec.h"
15 #include "lldb/Utility/Status.h"
16 
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/Support/Chrono.h"
19 #include "llvm/Support/FileCollector.h"
20 #include "llvm/Support/VirtualFileSystem.h"
21 
22 #include "lldb/lldb-types.h"
23 
24 #include <cstdint>
25 #include <cstdio>
26 #include <sys/stat.h>
27 
28 namespace lldb_private {
29 class FileSystem {
30 public:
31  static const char *DEV_NULL;
32  static const char *PATH_CONVERSION_ERROR;
33 
35  : m_fs(llvm::vfs::getRealFileSystem()), m_collector(nullptr),
36  m_home_directory() {}
37  FileSystem(std::shared_ptr<llvm::FileCollectorBase> collector)
38  : m_fs(llvm::vfs::getRealFileSystem()), m_collector(std::move(collector)),
39  m_home_directory(), m_mapped(false) {}
40  FileSystem(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
41  bool mapped = false)
42  : m_fs(std::move(fs)), m_collector(nullptr), m_home_directory(),
43  m_mapped(mapped) {}
44 
45  FileSystem(const FileSystem &fs) = delete;
46  FileSystem &operator=(const FileSystem &fs) = delete;
47 
48  static FileSystem &Instance();
49 
50  static void Initialize();
51  static void Initialize(std::shared_ptr<llvm::FileCollectorBase> collector);
52  static llvm::Error Initialize(const FileSpec &mapping);
53  static void Initialize(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs);
54  static void Terminate();
55 
56  Status Symlink(const FileSpec &src, const FileSpec &dst);
57  Status Readlink(const FileSpec &src, FileSpec &dst);
58 
59  Status ResolveSymbolicLink(const FileSpec &src, FileSpec &dst);
60 
61  /// Wraps ::fopen in a platform-independent way.
62  FILE *Fopen(const char *path, const char *mode);
63 
64  /// Wraps ::open in a platform-independent way.
65  int Open(const char *path, int flags, int mode);
66 
67  llvm::Expected<std::unique_ptr<File>>
68  Open(const FileSpec &file_spec, File::OpenOptions options,
69  uint32_t permissions = lldb::eFilePermissionsFileDefault,
70  bool should_close_fd = true);
71 
72  /// Get a directory iterator.
73  /// \{
74  llvm::vfs::directory_iterator DirBegin(const FileSpec &file_spec,
75  std::error_code &ec);
76  llvm::vfs::directory_iterator DirBegin(const llvm::Twine &dir,
77  std::error_code &ec);
78  /// \}
79 
80  /// Returns the Status object for the given file.
81  /// \{
82  llvm::ErrorOr<llvm::vfs::Status> GetStatus(const FileSpec &file_spec) const;
83  llvm::ErrorOr<llvm::vfs::Status> GetStatus(const llvm::Twine &path) const;
84  /// \}
85 
86  /// Returns the modification time of the given file.
87  /// \{
88  llvm::sys::TimePoint<> GetModificationTime(const FileSpec &file_spec) const;
89  llvm::sys::TimePoint<> GetModificationTime(const llvm::Twine &path) const;
90  /// \}
91 
92  /// Returns the on-disk size of the given file in bytes.
93  /// \{
94  uint64_t GetByteSize(const FileSpec &file_spec) const;
95  uint64_t GetByteSize(const llvm::Twine &path) const;
96  /// \}
97 
98  /// Return the current permissions of the given file.
99  ///
100  /// Returns a bitmask for the current permissions of the file (zero or more
101  /// of the permission bits defined in File::Permissions).
102  /// \{
103  uint32_t GetPermissions(const FileSpec &file_spec) const;
104  uint32_t GetPermissions(const llvm::Twine &path) const;
105  uint32_t GetPermissions(const FileSpec &file_spec, std::error_code &ec) const;
106  uint32_t GetPermissions(const llvm::Twine &path, std::error_code &ec) const;
107  /// \}
108 
109  /// Returns whether the given file exists.
110  /// \{
111  bool Exists(const FileSpec &file_spec) const;
112  bool Exists(const llvm::Twine &path) const;
113  /// \}
114 
115  /// Returns whether the given file is readable.
116  /// \{
117  bool Readable(const FileSpec &file_spec) const;
118  bool Readable(const llvm::Twine &path) const;
119  /// \}
120 
121  /// Returns whether the given path is a directory.
122  /// \{
123  bool IsDirectory(const FileSpec &file_spec) const;
124  bool IsDirectory(const llvm::Twine &path) const;
125  /// \}
126 
127  /// Returns whether the given path is local to the file system.
128  /// \{
129  bool IsLocal(const FileSpec &file_spec) const;
130  bool IsLocal(const llvm::Twine &path) const;
131  /// \}
132 
133  /// Make the given file path absolute.
134  /// \{
135  std::error_code MakeAbsolute(llvm::SmallVectorImpl<char> &path) const;
136  std::error_code MakeAbsolute(FileSpec &file_spec) const;
137  /// \}
138 
139  /// Resolve path to make it canonical.
140  /// \{
142  void Resolve(FileSpec &file_spec);
143  /// \}
144 
145  //// Create memory buffer from path.
146  /// \{
147  std::shared_ptr<DataBufferLLVM> CreateDataBuffer(const llvm::Twine &path,
148  uint64_t size = 0,
149  uint64_t offset = 0);
150  std::shared_ptr<DataBufferLLVM> CreateDataBuffer(const FileSpec &file_spec,
151  uint64_t size = 0,
152  uint64_t offset = 0);
153  /// \}
154 
155  /// Call into the Host to see if it can help find the file.
156  bool ResolveExecutableLocation(FileSpec &file_spec);
157 
158  /// Get the user home directory.
160  bool GetHomeDirectory(FileSpec &file_spec) const;
161 
163  /// Enumerate next entry in the current directory.
165  /// Recurse into the current entry if it is a directory or symlink, or next
166  /// if not.
168  /// Stop directory enumerations at any level.
170  };
171 
173  void *baton, llvm::sys::fs::file_type file_type, llvm::StringRef);
174 
175  typedef std::function<EnumerateDirectoryResult(
176  llvm::sys::fs::file_type file_type, llvm::StringRef)>
178 
179  void EnumerateDirectory(llvm::Twine path, bool find_directories,
180  bool find_files, bool find_other,
182  void *callback_baton);
183 
184  std::error_code GetRealPath(const llvm::Twine &path,
185  llvm::SmallVectorImpl<char> &output) const;
186 
187  llvm::ErrorOr<std::string> GetExternalPath(const llvm::Twine &path);
188  llvm::ErrorOr<std::string> GetExternalPath(const FileSpec &file_spec);
189 
190  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> GetVirtualFileSystem() {
191  return m_fs;
192  }
193 
194  void Collect(const FileSpec &file_spec);
195  void Collect(const llvm::Twine &file);
196 
197  void SetHomeDirectory(std::string home_directory);
198 
199 private:
200  static llvm::Optional<FileSystem> &InstanceImpl();
201  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> m_fs;
202  std::shared_ptr<llvm::FileCollectorBase> m_collector;
204  bool m_mapped = false;
205 };
206 } // namespace lldb_private
207 
208 #endif
lldb_private::FileSystem::FileSystem
FileSystem(llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > fs, bool mapped=false)
Definition: FileSystem.h:40
lldb_private::FileSystem::Open
int Open(const char *path, int flags, int mode)
Wraps ::open in a platform-independent way.
Definition: FileSystemPosix.cpp:79
llvm
Definition: Debugger.h:49
lldb_private::FileSystem::Collect
void Collect(const FileSpec &file_spec)
Definition: common/FileSystem.cpp:499
lldb_private::FileSystem::MakeAbsolute
std::error_code MakeAbsolute(llvm::SmallVectorImpl< char > &path) const
Make the given file path absolute.
Definition: common/FileSystem.cpp:233
lldb_private::FileSystem::Fopen
FILE * Fopen(const char *path, const char *mode)
Wraps ::fopen in a platform-independent way.
Definition: FileSystemPosix.cpp:74
lldb_private::File::OpenOptions
OpenOptions
Definition: File.h:49
lldb_private::FileSystem::FileSystem
FileSystem()
Definition: FileSystem.h:34
lldb_private::FileSystem::SetHomeDirectory
void SetHomeDirectory(std::string home_directory)
Definition: common/FileSystem.cpp:513
lldb_private::FileSystem::GetStatus
llvm::ErrorOr< llvm::vfs::Status > GetStatus(const FileSpec &file_spec) const
Returns the Status object for the given file.
Definition: common/FileSystem.cpp:104
lldb_private::FileSystem::IsDirectory
bool IsDirectory(const FileSpec &file_spec) const
Returns whether the given path is a directory.
Definition: common/FileSystem.cpp:189
lldb_private::FileSystem::m_collector
std::shared_ptr< llvm::FileCollectorBase > m_collector
Definition: FileSystem.h:202
lldb_private::FileSystem::GetHomeDirectory
bool GetHomeDirectory(llvm::SmallVectorImpl< char > &path) const
Get the user home directory.
Definition: common/FileSystem.cpp:363
lldb_private::FileSystem::ResolveExecutableLocation
bool ResolveExecutableLocation(FileSpec &file_spec)
Call into the Host to see if it can help find the file.
Definition: common/FileSystem.cpp:330
lldb_private::FileSystem::Exists
bool Exists(const FileSpec &file_spec) const
Returns whether the given file exists.
Definition: common/FileSystem.cpp:170
lldb_private::FileSystem
Definition: FileSystem.h:29
lldb_private::FileSystem::DEV_NULL
static const char * DEV_NULL
Definition: FileSystem.h:31
lldb_private::FileSpec
Definition: FileSpec.h:56
lldb_private::FileSystem::m_home_directory
std::string m_home_directory
Definition: FileSystem.h:203
lldb_private::FileSystem::Terminate
static void Terminate()
Definition: common/FileSystem.cpp:78
lldb_private::FileSystem::ResolveSymbolicLink
Status ResolveSymbolicLink(const FileSpec &src, FileSpec &dst)
Definition: FileSystemPosix.cpp:56
lldb_private::FileSystem::IsLocal
bool IsLocal(const FileSpec &file_spec) const
Returns whether the given path is local to the file system.
Definition: common/FileSystem.cpp:199
lldb_private::FileSystem::EnumerateDirectoryResult
EnumerateDirectoryResult
Definition: FileSystem.h:162
lldb_private::FileSystem::CreateDataBuffer
std::shared_ptr< DataBufferLLVM > CreateDataBuffer(const llvm::Twine &path, uint64_t size=0, uint64_t offset=0)
Create memory buffer from path.
Definition: common/FileSystem.cpp:297
lldb_private::FileSystem::m_mapped
bool m_mapped
Definition: FileSystem.h:204
lldb_private::FileSystem::GetByteSize
uint64_t GetByteSize(const FileSpec &file_spec) const
Returns the on-disk size of the given file in bytes.
Definition: common/FileSystem.cpp:129
lldb_private::FileSystem::operator=
FileSystem & operator=(const FileSystem &fs)=delete
lldb_private::FileSystem::m_fs
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > m_fs
Definition: FileSystem.h:201
lldb_private::FileSystem::DirectoryCallback
std::function< EnumerateDirectoryResult(llvm::sys::fs::file_type file_type, llvm::StringRef)> DirectoryCallback
Definition: FileSystem.h:177
lldb_private::FileSystem::eEnumerateDirectoryResultQuit
@ eEnumerateDirectoryResultQuit
Stop directory enumerations at any level.
Definition: FileSystem.h:169
lldb_private::FileSystem::GetModificationTime
llvm::sys::TimePoint GetModificationTime(const FileSpec &file_spec) const
Returns the modification time of the given file.
Definition: common/FileSystem.cpp:116
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::FileSystem::Resolve
void Resolve(llvm::SmallVectorImpl< char > &path)
Resolve path to make it canonical.
Definition: common/FileSystem.cpp:255
lldb_private::FileSystem::Readlink
Status Readlink(const FileSpec &src, FileSpec &dst)
Definition: FileSystemPosix.cpp:43
lldb_private::FileSystem::EnumerateDirectoryCallbackType
EnumerateDirectoryResult(* EnumerateDirectoryCallbackType)(void *baton, llvm::sys::fs::file_type file_type, llvm::StringRef)
Definition: FileSystem.h:172
lldb-types.h
lldb_private::FileSystem::eEnumerateDirectoryResultNext
@ eEnumerateDirectoryResultNext
Enumerate next entry in the current directory.
Definition: FileSystem.h:164
lldb_private::Status
Definition: Status.h:44
lldb_private::FileSystem::DirBegin
llvm::vfs::directory_iterator DirBegin(const FileSpec &file_spec, std::error_code &ec)
Get a directory iterator.
Definition: common/FileSystem.cpp:88
uint32_t
DataBufferLLVM.h
lldb_private::FileSystem::Initialize
static void Initialize()
Definition: common/FileSystem.cpp:47
lldb_private::FileSystem::eEnumerateDirectoryResultEnter
@ eEnumerateDirectoryResultEnter
Recurse into the current entry if it is a directory or symlink, or next if not.
Definition: FileSystem.h:167
lldb_private::FileSystem::EnumerateDirectory
void EnumerateDirectory(llvm::Twine path, bool find_directories, bool find_files, bool find_other, EnumerateDirectoryCallbackType callback, void *callback_baton)
Definition: common/FileSystem.cpp:203
lldb_private::FileSystem::PATH_CONVERSION_ERROR
static const char * PATH_CONVERSION_ERROR
Definition: FileSystem.h:32
lldb_private::FileSystem::Readable
bool Readable(const FileSpec &file_spec) const
Returns whether the given file is readable.
Definition: common/FileSystem.cpp:178
lldb_private::FileSystem::GetPermissions
uint32_t GetPermissions(const FileSpec &file_spec) const
Return the current permissions of the given file.
Definition: common/FileSystem.cpp:142
lldb_private::FileSystem::Symlink
Status Symlink(const FileSpec &src, const FileSpec &dst)
Definition: FileSystemPosix.cpp:36
lldb_private::FileSystem::Instance
static FileSystem & Instance()
Definition: common/FileSystem.cpp:45
Status.h
lldb_private::FileSystem::GetVirtualFileSystem
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > GetVirtualFileSystem()
Definition: FileSystem.h:190
lldb_private::FileSystem::FileSystem
FileSystem(std::shared_ptr< llvm::FileCollectorBase > collector)
Definition: FileSystem.h:37
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
Error
llvm::Error Error
Definition: UdtRecordCompleter.cpp:29
FileSpec.h
lldb_private::FileSystem::GetExternalPath
llvm::ErrorOr< std::string > GetExternalPath(const llvm::Twine &path)
Definition: common/FileSystem.cpp:476
lldb_private::FileSystem::GetRealPath
std::error_code GetRealPath(const llvm::Twine &path, llvm::SmallVectorImpl< char > &output) const
Definition: common/FileSystem.cpp:250
llvm::SmallVectorImpl
Definition: Disassembler.h:42
lldb_private::FileSystem::InstanceImpl
static llvm::Optional< FileSystem > & InstanceImpl()
Definition: common/FileSystem.cpp:83
File.h