1 //===-- FileSystem.h --------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_Host_FileSystem_h
10 #define liblldb_Host_FileSystem_h
12 #include "lldb/Host/File.h"
13 #include "lldb/Utility/DataBufferLLVM.h"
14 #include "lldb/Utility/FileCollector.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;
35 : m_fs(llvm::vfs::getRealFileSystem()), m_collector(nullptr),
37 FileSystem(FileCollector &collector)
38 : m_fs(llvm::vfs::getRealFileSystem()), m_collector(&collector),
40 FileSystem(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
42 : m_fs(fs), m_collector(nullptr), m_mapped(mapped) {}
44 FileSystem(const FileSystem &fs) = delete;
45 FileSystem &operator=(const FileSystem &fs) = delete;
47 static FileSystem &Instance();
49 static void Initialize();
50 static void Initialize(FileCollector &collector);
51 static llvm::Error Initialize(const FileSpec &mapping);
52 static void Initialize(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs);
53 static void Terminate();
55 Status Symlink(const FileSpec &src, const FileSpec &dst);
56 Status Readlink(const FileSpec &src, FileSpec &dst);
58 Status ResolveSymbolicLink(const FileSpec &src, FileSpec &dst);
60 /// Wraps ::fopen in a platform-independent way.
61 FILE *Fopen(const char *path, const char *mode);
63 /// Wraps ::open in a platform-independent way.
64 int Open(const char *path, int flags, int mode);
66 Status Open(File &File, const FileSpec &file_spec, uint32_t options,
67 uint32_t permissions = lldb::eFilePermissionsFileDefault,
68 bool should_close_fd = true);
70 /// Get a directory iterator.
72 llvm::vfs::directory_iterator DirBegin(const FileSpec &file_spec,
74 llvm::vfs::directory_iterator DirBegin(const llvm::Twine &dir,
78 /// Returns the Status object for the given file.
80 llvm::ErrorOr<llvm::vfs::Status> GetStatus(const FileSpec &file_spec) const;
81 llvm::ErrorOr<llvm::vfs::Status> GetStatus(const llvm::Twine &path) const;
84 /// Returns the modification time of the given file.
86 llvm::sys::TimePoint<> GetModificationTime(const FileSpec &file_spec) const;
87 llvm::sys::TimePoint<> GetModificationTime(const llvm::Twine &path) const;
90 /// Returns the on-disk size of the given file in bytes.
92 uint64_t GetByteSize(const FileSpec &file_spec) const;
93 uint64_t GetByteSize(const llvm::Twine &path) const;
96 /// Return the current permissions of the given file.
98 /// Returns a bitmask for the current permissions of the file (zero or more
99 /// of the permission bits defined in File::Permissions).
101 uint32_t GetPermissions(const FileSpec &file_spec) const;
102 uint32_t GetPermissions(const llvm::Twine &path) const;
103 uint32_t GetPermissions(const FileSpec &file_spec, std::error_code &ec) const;
104 uint32_t GetPermissions(const llvm::Twine &path, std::error_code &ec) const;
107 /// Returns whether the given file exists.
109 bool Exists(const FileSpec &file_spec) const;
110 bool Exists(const llvm::Twine &path) const;
113 /// Returns whether the given file is readable.
115 bool Readable(const FileSpec &file_spec) const;
116 bool Readable(const llvm::Twine &path) const;
119 /// Returns whether the given path is a directory.
121 bool IsDirectory(const FileSpec &file_spec) const;
122 bool IsDirectory(const llvm::Twine &path) const;
125 /// Returns whether the given path is local to the file system.
127 bool IsLocal(const FileSpec &file_spec) const;
128 bool IsLocal(const llvm::Twine &path) const;
131 /// Make the given file path absolute.
133 std::error_code MakeAbsolute(llvm::SmallVectorImpl<char> &path) const;
134 std::error_code MakeAbsolute(FileSpec &file_spec) const;
137 /// Resolve path to make it canonical.
139 void Resolve(llvm::SmallVectorImpl<char> &path);
140 void Resolve(FileSpec &file_spec);
143 //// Create memory buffer from path.
145 std::shared_ptr<DataBufferLLVM> CreateDataBuffer(const llvm::Twine &path,
147 uint64_t offset = 0);
148 std::shared_ptr<DataBufferLLVM> CreateDataBuffer(const FileSpec &file_spec,
150 uint64_t offset = 0);
153 /// Call into the Host to see if it can help find the file.
154 bool ResolveExecutableLocation(FileSpec &file_spec);
156 enum EnumerateDirectoryResult {
157 /// Enumerate next entry in the current directory.
158 eEnumerateDirectoryResultNext,
159 /// Recurse into the current entry if it is a directory or symlink, or next
161 eEnumerateDirectoryResultEnter,
162 /// Stop directory enumerations at any level.
163 eEnumerateDirectoryResultQuit
166 typedef EnumerateDirectoryResult (*EnumerateDirectoryCallbackType)(
167 void *baton, llvm::sys::fs::file_type file_type, llvm::StringRef);
169 typedef std::function<EnumerateDirectoryResult(
170 llvm::sys::fs::file_type file_type, llvm::StringRef)>
173 void EnumerateDirectory(llvm::Twine path, bool find_directories,
174 bool find_files, bool find_other,
175 EnumerateDirectoryCallbackType callback,
176 void *callback_baton);
178 std::error_code GetRealPath(const llvm::Twine &path,
179 llvm::SmallVectorImpl<char> &output) const;
181 llvm::ErrorOr<std::string> GetExternalPath(const llvm::Twine &path);
182 llvm::ErrorOr<std::string> GetExternalPath(const FileSpec &file_spec);
184 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> GetVirtualFileSystem() {
189 static llvm::Optional<FileSystem> &InstanceImpl();
190 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> m_fs;
191 FileCollector *m_collector;
194 } // namespace lldb_private