1 //===-- FileSystem.h --------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef liblldb_Host_FileSystem_h 11 #define liblldb_Host_FileSystem_h 12 13 #include "lldb/Host/File.h" 14 #include "lldb/Utility/DataBufferLLVM.h" 15 #include "lldb/Utility/FileSpec.h" 16 #include "lldb/Utility/Status.h" 17 18 #include "llvm/ADT/Optional.h" 19 #include "llvm/Support/Chrono.h" 20 #include "llvm/Support/VirtualFileSystem.h" 21 22 #include "lldb/lldb-types.h" 23 24 #include <stdint.h> 25 #include <stdio.h> 26 #include <sys/stat.h> 27 28 namespace lldb_private { 29 class FileSystem { 30 public: 31 static const char *DEV_NULL; 32 static const char *PATH_CONVERSION_ERROR; 33 FileSystem()34 FileSystem() : m_fs(llvm::vfs::getRealFileSystem()) {} FileSystem(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs)35 FileSystem(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs) : m_fs(fs) {} 36 37 FileSystem(const FileSystem &fs) = delete; 38 FileSystem &operator=(const FileSystem &fs) = delete; 39 40 static FileSystem &Instance(); 41 42 static void Initialize(); 43 static void Initialize(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs); 44 static void Terminate(); 45 46 Status Symlink(const FileSpec &src, const FileSpec &dst); 47 Status Readlink(const FileSpec &src, FileSpec &dst); 48 49 Status ResolveSymbolicLink(const FileSpec &src, FileSpec &dst); 50 51 /// Wraps ::fopen in a platform-independent way. 52 FILE *Fopen(const char *path, const char *mode); 53 54 /// Wraps ::open in a platform-independent way. 55 int Open(const char *path, int flags, int mode); 56 57 Status Open(File &File, const FileSpec &file_spec, uint32_t options, 58 uint32_t permissions = lldb::eFilePermissionsFileDefault); 59 60 /// Get a directory iterator. 61 /// @{ 62 llvm::vfs::directory_iterator DirBegin(const FileSpec &file_spec, 63 std::error_code &ec); 64 llvm::vfs::directory_iterator DirBegin(const llvm::Twine &dir, 65 std::error_code &ec); 66 /// @} 67 68 /// Returns the Status object for the given file. 69 /// @{ 70 llvm::ErrorOr<llvm::vfs::Status> GetStatus(const FileSpec &file_spec) const; 71 llvm::ErrorOr<llvm::vfs::Status> GetStatus(const llvm::Twine &path) const; 72 /// @} 73 74 /// Returns the modification time of the given file. 75 /// @{ 76 llvm::sys::TimePoint<> GetModificationTime(const FileSpec &file_spec) const; 77 llvm::sys::TimePoint<> GetModificationTime(const llvm::Twine &path) const; 78 /// @} 79 80 /// Returns the on-disk size of the given file in bytes. 81 /// @{ 82 uint64_t GetByteSize(const FileSpec &file_spec) const; 83 uint64_t GetByteSize(const llvm::Twine &path) const; 84 /// @} 85 86 /// Return the current permissions of the given file. 87 /// 88 /// Returns a bitmask for the current permissions of the file (zero or more 89 /// of the permission bits defined in File::Permissions). 90 /// @{ 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; 95 /// @} 96 97 /// Returns whether the given file exists. 98 /// @{ 99 bool Exists(const FileSpec &file_spec) const; 100 bool Exists(const llvm::Twine &path) const; 101 /// @} 102 103 /// Returns whether the given file is readable. 104 /// @{ 105 bool Readable(const FileSpec &file_spec) const; 106 bool Readable(const llvm::Twine &path) const; 107 /// @} 108 109 /// Returns whether the given path is a directory. 110 /// @{ 111 bool IsDirectory(const FileSpec &file_spec) const; 112 bool IsDirectory(const llvm::Twine &path) const; 113 /// @} 114 115 /// Returns whether the given path is local to the file system. 116 /// @{ 117 bool IsLocal(const FileSpec &file_spec) const; 118 bool IsLocal(const llvm::Twine &path) const; 119 /// @} 120 121 /// Make the given file path absolute. 122 /// @{ 123 std::error_code MakeAbsolute(llvm::SmallVectorImpl<char> &path) const; 124 std::error_code MakeAbsolute(FileSpec &file_spec) const; 125 /// @} 126 127 /// Resolve path to make it canonical. 128 /// @{ 129 void Resolve(llvm::SmallVectorImpl<char> &path); 130 void Resolve(FileSpec &file_spec); 131 /// @} 132 133 //// Create memory buffer from path. 134 /// @{ 135 std::shared_ptr<DataBufferLLVM> CreateDataBuffer(const llvm::Twine &path, 136 uint64_t size = 0, 137 uint64_t offset = 0); 138 std::shared_ptr<DataBufferLLVM> CreateDataBuffer(const FileSpec &file_spec, 139 uint64_t size = 0, 140 uint64_t offset = 0); 141 /// @} 142 143 /// Call into the Host to see if it can help find the file. 144 bool ResolveExecutableLocation(FileSpec &file_spec); 145 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 150 /// if not. 151 eEnumerateDirectoryResultEnter, 152 /// Stop directory enumerations at any level. 153 eEnumerateDirectoryResultQuit 154 }; 155 156 typedef EnumerateDirectoryResult (*EnumerateDirectoryCallbackType)( 157 void *baton, llvm::sys::fs::file_type file_type, llvm::StringRef); 158 159 typedef std::function<EnumerateDirectoryResult( 160 llvm::sys::fs::file_type file_type, llvm::StringRef)> 161 DirectoryCallback; 162 163 void EnumerateDirectory(llvm::Twine path, bool find_directories, 164 bool find_files, bool find_other, 165 EnumerateDirectoryCallbackType callback, 166 void *callback_baton); 167 168 std::error_code GetRealPath(const llvm::Twine &path, 169 llvm::SmallVectorImpl<char> &output) const; 170 171 private: 172 static llvm::Optional<FileSystem> &InstanceImpl(); 173 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> m_fs; 174 }; 175 } // namespace lldb_private 176 177 #endif 178