LLDB: FileSystem.h Source File
LLDB mainline llvm-project lldb include lldb Host 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 " 13 #include " lldb/Utility/DataBuffer.h " 14 #include " lldb/Utility/FileSpec.h " 15 #include " lldb/Utility/LLDBAssert.h " 16 #include " lldb/Utility/Status.h " 17 #include " lldb/Utility/TildeExpressionResolver.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 29 namespace lldb_private { 30 class FileSystem { 31 public : 32 static const char * DEV_NULL ; 33 static const char * PATH_CONVERSION_ERROR ; 34 35 FileSystem () 36 : m_fs ( llvm ::vfs::getRealFileSystem()), 37 m_tilde_resolver (std::make_unique< StandardTildeExpressionResolver >()) {} 38 FileSystem (llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs) 39 : m_fs (std::move(fs)), 40 m_tilde_resolver (std::make_unique< StandardTildeExpressionResolver >()) {} 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 59 Status ResolveSymbolicLink ( const FileSpec &src, FileSpec &dst); 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 /// \{ 144 void Resolve ( llvm::SmallVectorImpl<char> &path, 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. 177 bool GetHomeDirectory ( llvm::SmallVectorImpl<char> &path) const ; 178 bool GetHomeDirectory ( FileSpec &file_spec) const ; 179 180 enum EnumerateDirectoryResult { 181 /// Enumerate next entry in the current directory. 182 eEnumerateDirectoryResultNext , 183 /// Recurse into the current entry if it is a directory or symlink, or next 184 /// if not.
185 eEnumerateDirectoryResultEnter , 186 /// Stop directory enumerations at any level. 187 eEnumerateDirectoryResultQuit 188 }; 189 190 typedef EnumerateDirectoryResult (* EnumerateDirectoryCallbackType )( 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)> 195 DirectoryCallback ; 196 197 void EnumerateDirectory (llvm::Twine path, bool find_directories, 198 bool find_files, bool find_other, 199 EnumerateDirectoryCallbackType callback, 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 211 private : 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 DataBuffer.h FileSpec.h File.h LLDBAssert.h lldbassert
The rest of this page is on the live site.