13#include "llvm/Support/Errc.h"
14#include "llvm/Support/Errno.h"
15#include "llvm/Support/Error.h"
16#include "llvm/Support/FileSystem.h"
17#include "llvm/Support/Path.h"
18#include "llvm/Support/Program.h"
19#include "llvm/Support/Threading.h"
53 static std::optional<FileSystem> g_fs;
58 std::error_code &ec) {
60 ec = std::error_code(
static_cast<int>(errc::no_such_file_or_directory),
61 std::system_category());
68 std::error_code &ec) {
69 return m_fs->dir_begin(dir, ec);
72llvm::ErrorOr<vfs::Status>
75 return std::error_code(
static_cast<int>(errc::no_such_file_or_directory),
76 std::system_category());
81 return m_fs->status(path);
87 return sys::TimePoint<>();
92 ErrorOr<vfs::Status> status =
m_fs->status(path);
94 return sys::TimePoint<>();
95 return status->getLastModificationTime();
105 ErrorOr<vfs::Status> status =
m_fs->status(path);
108 return status->getSize();
116 std::error_code &ec)
const {
118 return sys::fs::perms::perms_not_known;
128 std::error_code &ec)
const {
129 ErrorOr<vfs::Status> status =
m_fs->status(path);
131 ec = status.getError();
132 return sys::fs::perms::perms_not_known;
134 return status->getPermissions();
152 ErrorOr<vfs::Status> status =
m_fs->status(path);
155 return status->isDirectory();
164 m_fs->isLocal(path, b);
173 bool find_files,
bool find_other,
174 EnumerateDirectoryCallbackType callback,
175 void *callback_baton) {
177 vfs::recursive_directory_iterator Iter(*
m_fs, path, EC);
178 vfs::recursive_directory_iterator End;
179 for (; Iter != End && !EC; Iter.increment(EC)) {
180 const auto &Item = *Iter;
181 ErrorOr<vfs::Status>
Status =
m_fs->status(Item.path());
184 if (!find_files &&
Status->isRegularFile())
186 if (!find_directories &&
Status->isDirectory())
188 if (!find_other &&
Status->isOther())
191 auto Result = callback(callback_baton,
Status->getType(), Item.path());
203 return m_fs->makeAbsolute(path);
207 SmallString<128> path;
208 file_spec.
GetPath(path,
false);
215 file_spec = new_file_spec;
221 return m_fs->getRealPath(path, output);
229 SmallString<128> resolved(path.begin(), path.end());
230 assert(
m_tilde_resolver &&
"must initialize tilde resolver in constructor");
231 m_tilde_resolver->ResolveFullPath(llvm::StringRef(path.begin(), path.size()),
235 SmallString<128> absolute(resolved.begin(), resolved.end());
240 path.append(absolute.begin(), absolute.end());
242 path.append(resolved.begin(), resolved.end());
251 SmallString<128> path;
266 uint64_t size, uint64_t offset,
268 std::unique_ptr<T> buffer;
270 auto buffer_or_error = T::getFile(path, is_volatile);
271 if (!buffer_or_error)
273 buffer = std::move(*buffer_or_error);
275 auto buffer_or_error = T::getFileSlice(path, size, offset, is_volatile);
276 if (!buffer_or_error)
278 buffer = std::move(*buffer_or_error);
283std::shared_ptr<WritableDataBuffer>
286 const bool is_volatile = !
IsLocal(path);
287 auto buffer = GetMemoryBuffer<llvm::WritableMemoryBuffer>(path, size, offset,
291 return std::shared_ptr<WritableDataBufferLLVM>(
295std::shared_ptr<DataBuffer>
298 const bool is_volatile = !
IsLocal(path);
300 GetMemoryBuffer<llvm::MemoryBuffer>(path, size, offset, is_volatile);
303 return std::shared_ptr<DataBufferLLVM>(
new DataBufferLLVM(std::move(buffer)));
306std::shared_ptr<WritableDataBuffer>
312std::shared_ptr<DataBuffer>
330 const std::string filename_str(filename.
GetCString());
331 llvm::ErrorOr<std::string> error_or_path =
332 llvm::sys::findProgramByName(filename_str);
337 llvm::StringRef path = *error_or_path;
338 llvm::StringRef parent = llvm::sys::path::parent_path(path);
339 if (parent.empty() || parent ==
".")
356 return llvm::sys::path::home_directory(path);
360 SmallString<128> home_dir;
369 return const_cast<FileSystem &
>(fs).Open(path, flags, mode);
379 open_flags |= O_RDWR;
381 open_flags |= O_WRONLY;
384 open_flags |= O_APPEND;
387 open_flags |= O_TRUNC;
390 open_flags |= O_CREAT;
393 open_flags |= O_CREAT | O_EXCL;
395 open_flags |= O_RDONLY;
399 open_flags |= O_NOFOLLOW;
407 open_flags |= O_CLOEXEC;
409 open_flags |= O_BINARY;
417 if (permissions & lldb::eFilePermissionsUserRead)
419 if (permissions & lldb::eFilePermissionsUserWrite)
421 if (permissions & lldb::eFilePermissionsUserExecute)
423 if (permissions & lldb::eFilePermissionsGroupRead)
425 if (permissions & lldb::eFilePermissionsGroupWrite)
427 if (permissions & lldb::eFilePermissionsGroupExecute)
429 if (permissions & lldb::eFilePermissionsWorldRead)
431 if (permissions & lldb::eFilePermissionsWorldWrite)
433 if (permissions & lldb::eFilePermissionsWorldExecute)
440 uint32_t permissions,
bool should_close_fd) {
442 const mode_t open_mode =
443 (open_flags & O_CREAT) ?
GetOpenMode(permissions) : 0;
445 auto path = file_spec.
GetPath();
447 int descriptor = llvm::sys::RetryAfterSignal(
448 -1,
OpenWithFS, *
this, path.c_str(), open_flags, open_mode);
451 return llvm::errorCodeToError(
452 std::error_code(errno, std::system_category()));
454 auto file = std::unique_ptr<File>(
455 new NativeFile(descriptor, options, should_close_fd));
456 assert(file->IsValid());
457 return std::move(file);
469 return Status(llvm::sys::fs::remove(path));
A uniqued constant string class.
bool IsEmpty() const
Test for empty string.
const char * GetCString() const
Get the string value as a C string.
void SetDirectory(ConstString directory)
Directory string set accessor.
const ConstString & GetFilename() const
Filename string const get accessor.
const ConstString & GetDirectory() const
Directory string const get accessor.
void SetPath(llvm::StringRef p)
Temporary helper for FileSystem change.
Style GetPathStyle() const
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
uint64_t GetByteSize(const FileSpec &file_spec) const
Returns the on-disk size of the given file in bytes.
void Resolve(llvm::SmallVectorImpl< char > &path)
Resolve path to make it canonical.
bool GetHomeDirectory(llvm::SmallVectorImpl< char > &path) const
Get the user home directory.
void EnumerateDirectory(llvm::Twine path, bool find_directories, bool find_files, bool find_other, EnumerateDirectoryCallbackType callback, void *callback_baton)
@ eEnumerateDirectoryResultNext
Enumerate next entry in the current directory.
@ eEnumerateDirectoryResultQuit
Stop directory enumerations at any level.
llvm::sys::TimePoint GetModificationTime(const FileSpec &file_spec) const
Returns the modification time of the given file.
std::string m_home_directory
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.
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)
static std::optional< FileSystem > & InstanceImpl()
std::unique_ptr< TildeExpressionResolver > m_tilde_resolver
uint32_t GetPermissions(const FileSpec &file_spec) const
Return the current permissions of the given file.
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 > m_fs
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()
Status RemoveFile(const FileSpec &file_spec)
Remove a single file.
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.
static bool DescriptorIsValid(int descriptor)
@ eOpenOptionCanCreateNewOnly
@ eOpenOptionDontFollowSymlinks
static int GetOpenFlags(File::OpenOptions options)
static mode_t GetOpenMode(uint32_t permissions)
static std::unique_ptr< T > GetMemoryBuffer(const llvm::Twine &path, uint64_t size, uint64_t offset, bool is_volatile)
static int OpenWithFS(const FileSystem &fs, const char *path, int flags, int mode)
A class that represents a running process on the host machine.