1 //===-- FileSystem.h --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Host_FileSystem_h
11 #define liblldb_Host_FileSystem_h
13 #include "lldb/Host/File.h"
14 #include "lldb/Utility/DataBufferLLVM.h"
15 #include "lldb/Utility/FileSpec.h"
16 #include "lldb/Utility/Status.h"
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/Support/Chrono.h"
20 #include "llvm/Support/VirtualFileSystem.h"
22 #include "lldb/lldb-types.h"
28 namespace lldb_private {
31 static const char *DEV_NULL;
32 static const char *PATH_CONVERSION_ERROR;
34 FileSystem() : m_fs(llvm::vfs::getRealFileSystem()) {}
35 FileSystem(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs) : m_fs(fs) {}
37 FileSystem(const FileSystem &fs) = delete;
38 FileSystem &operator=(const FileSystem &fs) = delete;
40 static FileSystem &Instance();
42 static void Initialize();
43 static void Initialize(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs);
44 static void Terminate();
46 Status Symlink(const FileSpec &src, const FileSpec &dst);
47 Status Readlink(const FileSpec &src, FileSpec &dst);
49 Status ResolveSymbolicLink(const FileSpec &src, FileSpec &dst);
51 /// Wraps ::fopen in a platform-independent way.
52 FILE *Fopen(const char *path, const char *mode);
54 /// Wraps ::open in a platform-independent way.
55 int Open(const char *path, int flags, int mode);
57 Status Open(File &File, const FileSpec &file_spec, uint32_t options,
58 uint32_t permissions = lldb::eFilePermissionsFileDefault);
60 /// Get a directory iterator.
62 llvm::vfs::directory_iterator DirBegin(const FileSpec &file_spec,
64 llvm::vfs::directory_iterator DirBegin(const llvm::Twine &dir,
68 /// Returns the Status object for the given file.
70 llvm::ErrorOr<llvm::vfs::Status> GetStatus(const FileSpec &file_spec) const;
71 llvm::ErrorOr<llvm::vfs::Status> GetStatus(const llvm::Twine &path) const;
74 /// Returns the modification time of the given file.
76 llvm::sys::TimePoint<> GetModificationTime(const FileSpec &file_spec) const;
77 llvm::sys::TimePoint<> GetModificationTime(const llvm::Twine &path) const;
80 /// Returns the on-disk size of the given file in bytes.
82 uint64_t GetByteSize(const FileSpec &file_spec) const;
83 uint64_t GetByteSize(const llvm::Twine &path) const;
86 /// Return the current permissions of the given file.
88 /// Returns a bitmask for the current permissions of the file (zero or more
89 /// of the permission bits defined in File::Permissions).
91 uint32_t GetPermissions(const FileSpec &file_spec) const;
92 uint32_t GetPermissions(const llvm::Twine &path) const;
93 uint32_t GetPermissions(const FileSpec &file_spec, std::error_code &ec) const;
94 uint32_t GetPermissions(const llvm::Twine &path, std::error_code &ec) const;
97 /// Returns whether the given file exists.
99 bool Exists(const FileSpec &file_spec) const;
100 bool Exists(const llvm::Twine &path) const;
103 /// Returns whether the given file is readable.
105 bool Readable(const FileSpec &file_spec) const;
106 bool Readable(const llvm::Twine &path) const;
109 /// Returns whether the given path is a directory.
111 bool IsDirectory(const FileSpec &file_spec) const;
112 bool IsDirectory(const llvm::Twine &path) const;
115 /// Returns whether the given path is local to the file system.
117 bool IsLocal(const FileSpec &file_spec) const;
118 bool IsLocal(const llvm::Twine &path) const;
121 /// Make the given file path absolute.
123 std::error_code MakeAbsolute(llvm::SmallVectorImpl<char> &path) const;
124 std::error_code MakeAbsolute(FileSpec &file_spec) const;
127 /// Resolve path to make it canonical.
129 void Resolve(llvm::SmallVectorImpl<char> &path);
130 void Resolve(FileSpec &file_spec);
133 //// Create memory buffer from path.
135 std::shared_ptr<DataBufferLLVM> CreateDataBuffer(const llvm::Twine &path,
137 uint64_t offset = 0);
138 std::shared_ptr<DataBufferLLVM> CreateDataBuffer(const FileSpec &file_spec,
140 uint64_t offset = 0);
143 /// Call into the Host to see if it can help find the file.
144 bool ResolveExecutableLocation(FileSpec &file_spec);
146 enum EnumerateDirectoryResult {
147 /// Enumerate next entry in the current directory.
148 eEnumerateDirectoryResultNext,
149 /// Recurse into the current entry if it is a directory or symlink, or next
151 eEnumerateDirectoryResultEnter,
152 /// Stop directory enumerations at any level.
153 eEnumerateDirectoryResultQuit
156 typedef EnumerateDirectoryResult (*EnumerateDirectoryCallbackType)(
157 void *baton, llvm::sys::fs::file_type file_type, llvm::StringRef);
159 typedef std::function<EnumerateDirectoryResult(
160 llvm::sys::fs::file_type file_type, llvm::StringRef)>
163 void EnumerateDirectory(llvm::Twine path, bool find_directories,
164 bool find_files, bool find_other,
165 EnumerateDirectoryCallbackType callback,
166 void *callback_baton);
168 std::error_code GetRealPath(const llvm::Twine &path,
169 llvm::SmallVectorImpl<char> &output) const;
172 static llvm::Optional<FileSystem> &InstanceImpl();
173 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> m_fs;
175 } // namespace lldb_private