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"
16#include "lldb/Utility/Status.h"
18
19#include "llvm/Support/Chrono.h"
20#include "llvm/Support/VirtualFileSystem.h"
21
22#include "lldb/lldb-types.h"
23
24#include <cstdint>
25#include <cstdio>
26#include <optional>
27#include <sys/stat.h>
28
29namespace lldb_private {
31public:
32 static const char *DEV_NULL;
33 static const char *PATH_CONVERSION_ERROR;
34
36 : m_fs(llvm::vfs::getRealFileSystem()),
38 FileSystem(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs)
39 : m_fs(std::move(fs)),
41 FileSystem(std::unique_ptr<TildeExpressionResolver> tilde_resolver)
42 : m_fs(llvm::vfs::getRealFileSystem()),
43 m_tilde_resolver(std::move(tilde_resolver)) {}
44
45 FileSystem(const FileSystem &fs) = delete;
46 FileSystem &operator=(const FileSystem &fs) = delete;
47
48 static FileSystem &Instance();
49
50 template <class... T> static void Initialize(T &&...t) {
51 lldbassert(!InstanceImpl() && "Already initialized.");
52 InstanceImpl().emplace(std::forward<T>(t)...);
53 }
54 static void Terminate();
55
56 Status Symlink(const FileSpec &src, const FileSpec &dst);
57 Status Readlink(const FileSpec &src, FileSpec &dst);
58
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 = 0600);
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 ///
141 /// If force_make_absolute is specified, we'll make the path absolute even if
142 /// it does not exist.
143 /// \{
145 bool force_make_absolute = false);
146 void Resolve(FileSpec &file_spec, bool force_make_absolute = false);
147 /// \}
148
149 /// Remove a single file.
150 ///
151 /// The path must specify a file and not a directory.
152 /// \{
153 Status RemoveFile(const FileSpec &file_spec);
154 Status RemoveFile(const llvm::Twine &path);
155 /// \}
156
157 //// Create memory buffer from path.
158 /// \{
159 std::shared_ptr<DataBuffer> CreateDataBuffer(const llvm::Twine &path,
160 uint64_t size = 0,
161 uint64_t offset = 0);
162 std::shared_ptr<DataBuffer> CreateDataBuffer(const FileSpec &file_spec,
163 uint64_t size = 0,
164 uint64_t offset = 0);
165 std::shared_ptr<WritableDataBuffer>
166 CreateWritableDataBuffer(const llvm::Twine &path, uint64_t size = 0,
167 uint64_t offset = 0);
168 std::shared_ptr<WritableDataBuffer>
169 CreateWritableDataBuffer(const FileSpec &file_spec, uint64_t size = 0,
170 uint64_t offset = 0);
171 /// \}
172
173 /// Call into the Host to see if it can help find the file.
174 bool ResolveExecutableLocation(FileSpec &file_spec);
175
176 /// Get the user home directory.
178 bool GetHomeDirectory(FileSpec &file_spec) const;
179
181 /// Enumerate next entry in the current directory.
183 /// Recurse into the current entry if it is a directory or symlink, or next
184 /// if not.
186 /// Stop directory enumerations at any level.
188 };
189
191 void *baton, llvm::sys::fs::file_type file_type, llvm::StringRef);
192
193 typedef std::function<EnumerateDirectoryResult(
194 llvm::sys::fs::file_type file_type, llvm::StringRef)>
196
197 void EnumerateDirectory(llvm::Twine path, bool find_directories,
198 bool find_files, bool find_other,
200 void *callback_baton);
201
202 std::error_code GetRealPath(const llvm::Twine &path,
203 llvm::SmallVectorImpl<char> &output) const;
204
205 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> GetVirtualFileSystem() {
206 return m_fs;
207 }
208
209 void SetHomeDirectory(std::string home_directory);
210
211private:
212 static std::optional<FileSystem> &InstanceImpl();
213 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> m_fs;
214 std::unique_ptr<TildeExpressionResolver> m_tilde_resolver;
215 std::string m_home_directory;
216};
217} // namespace lldb_private
218
219#endif
#define lldbassert(x)
Definition LLDBAssert.h:16
A file utility class.
Definition FileSpec.h:57
FileSystem & operator=(const FileSystem &fs)=delete
FileSystem(llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > fs)
Definition FileSystem.h:38
uint64_t GetByteSize(const FileSpec &file_spec) const
Returns the on-disk size of the given file in bytes.
llvm::ErrorOr< llvm::vfs::Status > GetStatus(const llvm::Twine &path) const
bool GetHomeDirectory(llvm::SmallVectorImpl< char > &path) const
Get the user home directory.
static const char * PATH_CONVERSION_ERROR
Definition FileSystem.h:33
static const char * DEV_NULL
Definition FileSystem.h:32
bool IsLocal(const llvm::Twine &path) const
void EnumerateDirectory(llvm::Twine path, bool find_directories, bool find_files, bool find_other, EnumerateDirectoryCallbackType callback, void *callback_baton)
@ eEnumerateDirectoryResultEnter
Recurse into the current entry if it is a directory or symlink, or next if not.
Definition FileSystem.h:185
@ eEnumerateDirectoryResultNext
Enumerate next entry in the current directory.
Definition FileSystem.h:182
@ eEnumerateDirectoryResultQuit
Stop directory enumerations at any level.
Definition FileSystem.h:187
uint64_t GetByteSize(const llvm::Twine &path) const
uint32_t GetPermissions(const llvm::Twine &path) const
bool Exists(const llvm::Twine &path) const
llvm::sys::TimePoint GetModificationTime(const FileSpec &file_spec) const
Returns the modification time of the given file.
EnumerateDirectoryResult(* EnumerateDirectoryCallbackType)(void *baton, llvm::sys::fs::file_type file_type, llvm::StringRef)
Definition FileSystem.h:190
Status Symlink(const FileSpec &src, const FileSpec &dst)
bool IsDirectory(const llvm::Twine &path) const
Status ResolveSymbolicLink(const FileSpec &src, FileSpec &dst)
std::string m_home_directory
Definition FileSystem.h:215
bool ResolveExecutableLocation(FileSpec &file_spec)
Call into the Host to see if it can help find the file.
bool Exists(const FileSpec &file_spec) const
Returns whether the given file exists.
bool Readable(const llvm::Twine &path) const
int Open(const char *path, int flags, int mode=0600)
Wraps open in a platform-independent way.
std::error_code GetRealPath(const llvm::Twine &path, llvm::SmallVectorImpl< char > &output) const
void SetHomeDirectory(std::string home_directory)
Status Readlink(const FileSpec &src, FileSpec &dst)
static std::optional< FileSystem > & InstanceImpl()
std::unique_ptr< TildeExpressionResolver > m_tilde_resolver
Definition FileSystem.h:214
uint32_t GetPermissions(const FileSpec &file_spec) const
Return the current permissions of the given file.
FileSystem(const FileSystem &fs)=delete
static void Initialize(T &&...t)
Definition FileSystem.h:50
uint32_t GetPermissions(const llvm::Twine &path, std::error_code &ec) const
llvm::sys::TimePoint GetModificationTime(const llvm::Twine &path) const
FileSystem(std::unique_ptr< TildeExpressionResolver > tilde_resolver)
Definition FileSystem.h:41
llvm::vfs::directory_iterator DirBegin(const FileSpec &file_spec, std::error_code &ec)
Get a directory iterator.
std::error_code MakeAbsolute(llvm::SmallVectorImpl< char > &path) const
Make the given file path absolute.
llvm::ErrorOr< llvm::vfs::Status > GetStatus(const FileSpec &file_spec) const
Returns the Status object for the given file.
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > GetVirtualFileSystem()
Definition FileSystem.h:205
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > m_fs
Definition FileSystem.h:213
llvm::vfs::directory_iterator DirBegin(const llvm::Twine &dir, std::error_code &ec)
std::shared_ptr< WritableDataBuffer > CreateWritableDataBuffer(const llvm::Twine &path, uint64_t size=0, uint64_t offset=0)
bool Readable(const FileSpec &file_spec) const
Returns whether the given file is readable.
bool IsDirectory(const FileSpec &file_spec) const
Returns whether the given path is a directory.
static FileSystem & Instance()
void Resolve(llvm::SmallVectorImpl< char > &path, bool force_make_absolute=false)
Resolve path to make it canonical.
std::function< EnumerateDirectoryResult(llvm::sys::fs::file_type file_type, llvm::StringRef)> DirectoryCallback
Definition FileSystem.h:195
Status RemoveFile(const FileSpec &file_spec)
Remove a single file.
FILE * Fopen(const char *path, const char *mode)
Wraps fopen in a platform-independent way.
bool IsLocal(const FileSpec &file_spec) const
Returns whether the given path is local to the file system.
std::shared_ptr< DataBuffer > CreateDataBuffer(const llvm::Twine &path, uint64_t size=0, uint64_t offset=0)
Create memory buffer from path.
An error handling class.
Definition Status.h:118
A class that represents a running process on the host machine.