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