LLDB  mainline
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 liblldb_Host_FileSystem_h
10 #define liblldb_Host_FileSystem_h
11 
12 #include "lldb/Host/File.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 
35  : m_fs(llvm::vfs::getRealFileSystem()), m_collector(nullptr),
36  m_mapped(false) {}
38  : m_fs(llvm::vfs::getRealFileSystem()), m_collector(&collector),
39  m_mapped(false) {}
40  FileSystem(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
41  bool mapped = false)
42  : m_fs(fs), m_mapped(mapped) {}
43 
44  FileSystem(const FileSystem &fs) = delete;
45  FileSystem &operator=(const FileSystem &fs) = delete;
46 
47  static FileSystem &Instance();
48 
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();
54 
55  Status Symlink(const FileSpec &src, const FileSpec &dst);
56  Status Readlink(const FileSpec &src, FileSpec &dst);
57 
58  Status ResolveSymbolicLink(const FileSpec &src, FileSpec &dst);
59 
60  /// Wraps ::fopen in a platform-independent way.
61  FILE *Fopen(const char *path, const char *mode);
62 
63  /// Wraps ::open in a platform-independent way.
64  int Open(const char *path, int flags, int mode);
65 
66  Status Open(File &File, const FileSpec &file_spec, uint32_t options,
67  uint32_t permissions = lldb::eFilePermissionsFileDefault,
68  bool should_close_fd = true);
69 
70  /// Get a directory iterator.
71  /// \{
72  llvm::vfs::directory_iterator DirBegin(const FileSpec &file_spec,
73  std::error_code &ec);
74  llvm::vfs::directory_iterator DirBegin(const llvm::Twine &dir,
75  std::error_code &ec);
76  /// \}
77 
78  /// Returns the Status object for the given file.
79  /// \{
80  llvm::ErrorOr<llvm::vfs::Status> GetStatus(const FileSpec &file_spec) const;
81  llvm::ErrorOr<llvm::vfs::Status> GetStatus(const llvm::Twine &path) const;
82  /// \}
83 
84  /// Returns the modification time of the given file.
85  /// \{
86  llvm::sys::TimePoint<> GetModificationTime(const FileSpec &file_spec) const;
87  llvm::sys::TimePoint<> GetModificationTime(const llvm::Twine &path) const;
88  /// \}
89 
90  /// Returns the on-disk size of the given file in bytes.
91  /// \{
92  uint64_t GetByteSize(const FileSpec &file_spec) const;
93  uint64_t GetByteSize(const llvm::Twine &path) const;
94  /// \}
95 
96  /// Return the current permissions of the given file.
97  ///
98  /// Returns a bitmask for the current permissions of the file (zero or more
99  /// of the permission bits defined in File::Permissions).
100  /// \{
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;
105  /// \}
106 
107  /// Returns whether the given file exists.
108  /// \{
109  bool Exists(const FileSpec &file_spec) const;
110  bool Exists(const llvm::Twine &path) const;
111  /// \}
112 
113  /// Returns whether the given file is readable.
114  /// \{
115  bool Readable(const FileSpec &file_spec) const;
116  bool Readable(const llvm::Twine &path) const;
117  /// \}
118 
119  /// Returns whether the given path is a directory.
120  /// \{
121  bool IsDirectory(const FileSpec &file_spec) const;
122  bool IsDirectory(const llvm::Twine &path) const;
123  /// \}
124 
125  /// Returns whether the given path is local to the file system.
126  /// \{
127  bool IsLocal(const FileSpec &file_spec) const;
128  bool IsLocal(const llvm::Twine &path) const;
129  /// \}
130 
131  /// Make the given file path absolute.
132  /// \{
133  std::error_code MakeAbsolute(llvm::SmallVectorImpl<char> &path) const;
134  std::error_code MakeAbsolute(FileSpec &file_spec) const;
135  /// \}
136 
137  /// Resolve path to make it canonical.
138  /// \{
140  void Resolve(FileSpec &file_spec);
141  /// \}
142 
143  //// Create memory buffer from path.
144  /// \{
145  std::shared_ptr<DataBufferLLVM> CreateDataBuffer(const llvm::Twine &path,
146  uint64_t size = 0,
147  uint64_t offset = 0);
148  std::shared_ptr<DataBufferLLVM> CreateDataBuffer(const FileSpec &file_spec,
149  uint64_t size = 0,
150  uint64_t offset = 0);
151  /// \}
152 
153  /// Call into the Host to see if it can help find the file.
154  bool ResolveExecutableLocation(FileSpec &file_spec);
155 
157  /// Enumerate next entry in the current directory.
159  /// Recurse into the current entry if it is a directory or symlink, or next
160  /// if not.
162  /// Stop directory enumerations at any level.
164  };
165 
167  void *baton, llvm::sys::fs::file_type file_type, llvm::StringRef);
168 
169  typedef std::function<EnumerateDirectoryResult(
170  llvm::sys::fs::file_type file_type, llvm::StringRef)>
172 
173  void EnumerateDirectory(llvm::Twine path, bool find_directories,
174  bool find_files, bool find_other,
176  void *callback_baton);
177 
178  std::error_code GetRealPath(const llvm::Twine &path,
179  llvm::SmallVectorImpl<char> &output) const;
180 
181  llvm::ErrorOr<std::string> GetExternalPath(const llvm::Twine &path);
182  llvm::ErrorOr<std::string> GetExternalPath(const FileSpec &file_spec);
183 
184  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> GetVirtualFileSystem() {
185  return m_fs;
186  }
187 
188 private:
189  static llvm::Optional<FileSystem> &InstanceImpl();
190  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> m_fs;
191  FileCollector *m_collector;
192  bool m_mapped;
193 };
194 } // namespace lldb_private
195 
196 #endif
bool ResolveExecutableLocation(FileSpec &file_spec)
Call into the Host to see if it can help find the file.
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
Definition: Debugger.h:71
llvm::Error Error
static const char * PATH_CONVERSION_ERROR
Definition: FileSystem.h:32
std::error_code MakeAbsolute(llvm::SmallVectorImpl< char > &path) const
Make the given file path absolute.
std::shared_ptr< DataBufferLLVM > CreateDataBuffer(const llvm::Twine &path, uint64_t size=0, uint64_t offset=0)
Create memory buffer from path.
A file utility class.
Definition: FileSpec.h:55
bool IsDirectory(const FileSpec &file_spec) const
Returns whether the given path is a directory.
Status Readlink(const FileSpec &src, FileSpec &dst)
llvm::vfs::directory_iterator DirBegin(const FileSpec &file_spec, std::error_code &ec)
Get a directory iterator.
static FileSystem & Instance()
FileSystem(FileCollector &collector)
Definition: FileSystem.h:37
A file class.
Definition: File.h:29
FileSystem & operator=(const FileSystem &fs)=delete
FileSystem(llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > fs, bool mapped=false)
Definition: FileSystem.h:40
FILE * Fopen(const char *path, const char *mode)
Wraps ::fopen in a platform-independent way.
std::function< EnumerateDirectoryResult(llvm::sys::fs::file_type file_type, llvm::StringRef)> DirectoryCallback
Definition: FileSystem.h:171
Status ResolveSymbolicLink(const FileSpec &src, FileSpec &dst)
Collects files into a directory and generates a mapping that can be used by the VFS.
Definition: FileCollector.h:26
llvm::ErrorOr< std::string > GetExternalPath(const llvm::Twine &path)
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > GetVirtualFileSystem()
Definition: FileSystem.h:184
Stop directory enumerations at any level.
Definition: FileSystem.h:163
void Resolve(llvm::SmallVectorImpl< char > &path)
Resolve path to make it canonical.
uint32_t GetPermissions(const FileSpec &file_spec) const
Return the current permissions of the given file.
Status Symlink(const FileSpec &src, const FileSpec &dst)
EnumerateDirectoryResult(* EnumerateDirectoryCallbackType)(void *baton, llvm::sys::fs::file_type file_type, llvm::StringRef)
Definition: FileSystem.h:166
bool IsLocal(const FileSpec &file_spec) const
Returns whether the given path is local to the file system.
void EnumerateDirectory(llvm::Twine path, bool find_directories, bool find_files, bool find_other, EnumerateDirectoryCallbackType callback, void *callback_baton)
bool Readable(const FileSpec &file_spec) const
Returns whether the given file is readable.
bool Exists(const FileSpec &file_spec) const
Returns whether the given file exists.
uint64_t GetByteSize(const FileSpec &file_spec) const
Returns the on-disk size of the given file in bytes.
static const char * DEV_NULL
Definition: FileSystem.h:31
std::error_code GetRealPath(const llvm::Twine &path, llvm::SmallVectorImpl< char > &output) const
llvm::ErrorOr< llvm::vfs::Status > GetStatus(const FileSpec &file_spec) const
Returns the Status object for the given file.
Recurse into the current entry if it is a directory or symlink, or next if not.
Definition: FileSystem.h:161
llvm::sys::TimePoint GetModificationTime(const FileSpec &file_spec) const
Returns the modification time of the given file.
int Open(const char *path, int flags, int mode)
Wraps ::open in a platform-independent way.
Enumerate next entry in the current directory.
Definition: FileSystem.h:158
An error handling class.
Definition: Status.h:44