15#include "llvm/Support/Errc.h"
16#include "llvm/Support/Errno.h"
17#include "llvm/Support/Error.h"
18#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/Path.h"
20#include "llvm/Support/Program.h"
21#include "llvm/Support/Threading.h"
65 static std::optional<FileSystem> g_fs;
70 std::error_code &ec) {
72 ec = std::error_code(
static_cast<int>(errc::no_such_file_or_directory),
73 std::system_category());
80 std::error_code &ec) {
81 return m_fs->dir_begin(dir, ec);
84llvm::ErrorOr<vfs::Status>
87 return std::error_code(
static_cast<int>(errc::no_such_file_or_directory),
88 std::system_category());
93 return m_fs->status(path);
99 return sys::TimePoint<>();
104 ErrorOr<vfs::Status> status =
m_fs->status(path);
106 return sys::TimePoint<>();
107 return status->getLastModificationTime();
117 ErrorOr<vfs::Status> status =
m_fs->status(path);
120 return status->getSize();
128 std::error_code &ec)
const {
130 return sys::fs::perms::perms_not_known;
140 std::error_code &ec)
const {
141 ErrorOr<vfs::Status> status =
m_fs->status(path);
143 ec = status.getError();
144 return sys::fs::perms::perms_not_known;
146 return status->getPermissions();
164 ErrorOr<vfs::Status> status =
m_fs->status(path);
167 return status->isDirectory();
176 m_fs->isLocal(path, b);
185 bool find_files,
bool find_other,
186 EnumerateDirectoryCallbackType callback,
187 void *callback_baton) {
189 vfs::recursive_directory_iterator Iter(*
m_fs, path, EC);
190 vfs::recursive_directory_iterator End;
191 for (; Iter != End && !EC; Iter.increment(EC)) {
192 const auto &Item = *Iter;
193 ErrorOr<vfs::Status>
Status =
m_fs->status(Item.path());
196 if (!find_files &&
Status->isRegularFile())
198 if (!find_directories &&
Status->isDirectory())
200 if (!find_other &&
Status->isOther())
203 auto Result = callback(callback_baton,
Status->getType(), Item.path());
215 return m_fs->makeAbsolute(path);
219 SmallString<128> path;
220 file_spec.
GetPath(path,
false);
227 file_spec = new_file_spec;
233 return m_fs->getRealPath(path, output);
241 SmallString<128> resolved(path.begin(), path.end());
247 SmallString<128> absolute(resolved.begin(), resolved.end());
252 path.append(absolute.begin(), absolute.end());
254 path.append(resolved.begin(), resolved.end());
263 SmallString<128> path;
279 uint64_t size, uint64_t offset,
281 std::unique_ptr<T> buffer;
283 auto buffer_or_error = T::getFile(path, is_volatile);
284 if (!buffer_or_error)
286 buffer = std::move(*buffer_or_error);
288 auto buffer_or_error = T::getFileSlice(path, size, offset, is_volatile);
289 if (!buffer_or_error)
291 buffer = std::move(*buffer_or_error);
296std::shared_ptr<WritableDataBuffer>
299 const bool is_volatile = !
IsLocal(path);
300 auto buffer = GetMemoryBuffer<llvm::WritableMemoryBuffer>(path, size, offset,
304 return std::shared_ptr<WritableDataBufferLLVM>(
308std::shared_ptr<DataBuffer>
311 const bool is_volatile = !
IsLocal(path);
313 GetMemoryBuffer<llvm::MemoryBuffer>(path, size, offset, is_volatile);
316 return std::shared_ptr<DataBufferLLVM>(
new DataBufferLLVM(std::move(buffer)));
319std::shared_ptr<WritableDataBuffer>
325std::shared_ptr<DataBuffer>
343 const std::string filename_str(filename.
GetCString());
344 llvm::ErrorOr<std::string> error_or_path =
345 llvm::sys::findProgramByName(filename_str);
350 llvm::StringRef path = *error_or_path;
351 llvm::StringRef parent = llvm::sys::path::parent_path(path);
352 if (parent.empty() || parent ==
".")
369 return llvm::sys::path::home_directory(path);
373 SmallString<128> home_dir;
382 return const_cast<FileSystem &
>(fs).Open(path, flags, mode);
392 open_flags |= O_RDWR;
394 open_flags |= O_WRONLY;
397 open_flags |= O_APPEND;
400 open_flags |= O_TRUNC;
403 open_flags |= O_CREAT;
406 open_flags |= O_CREAT | O_EXCL;
408 open_flags |= O_RDONLY;
412 open_flags |= O_NOFOLLOW;
420 open_flags |= O_CLOEXEC;
422 open_flags |= O_BINARY;
430 if (permissions & lldb::eFilePermissionsUserRead)
432 if (permissions & lldb::eFilePermissionsUserWrite)
434 if (permissions & lldb::eFilePermissionsUserExecute)
436 if (permissions & lldb::eFilePermissionsGroupRead)
438 if (permissions & lldb::eFilePermissionsGroupWrite)
440 if (permissions & lldb::eFilePermissionsGroupExecute)
442 if (permissions & lldb::eFilePermissionsWorldRead)
444 if (permissions & lldb::eFilePermissionsWorldWrite)
446 if (permissions & lldb::eFilePermissionsWorldExecute)
453 uint32_t permissions,
bool should_close_fd) {
455 const mode_t open_mode =
456 (open_flags & O_CREAT) ?
GetOpenMode(permissions) : 0;
458 auto path = file_spec.
GetPath();
460 int descriptor = llvm::sys::RetryAfterSignal(
461 -1,
OpenWithFS, *
this, path.c_str(), open_flags, open_mode);
464 return llvm::errorCodeToError(
465 std::error_code(errno, std::system_category()));
467 auto file = std::unique_ptr<File>(
468 new NativeFile(descriptor, options, should_close_fd));
469 assert(file->IsValid());
470 return std::move(file);
482 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.
void SetIsResolved(bool is_resolved)
Set if the file path has been resolved or not.
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.
std::error_code GetRealPath(const llvm::Twine &path, llvm::SmallVectorImpl< char > &output) const
void SetHomeDirectory(std::string home_directory)
static std::optional< FileSystem > & InstanceImpl()
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.
int Open(const char *path, int flags, int mode)
Wraps ::open in a platform-independent way.
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
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...
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.