Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:48

0001 //===-- FileSystem.h --------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_HOST_FILESYSTEM_H
0010 #define LLDB_HOST_FILESYSTEM_H
0011 
0012 #include "lldb/Host/File.h"
0013 #include "lldb/Utility/DataBuffer.h"
0014 #include "lldb/Utility/FileSpec.h"
0015 #include "lldb/Utility/LLDBAssert.h"
0016 #include "lldb/Utility/Status.h"
0017 #include "lldb/Utility/TildeExpressionResolver.h"
0018 
0019 #include "llvm/Support/Chrono.h"
0020 #include "llvm/Support/VirtualFileSystem.h"
0021 
0022 #include "lldb/lldb-types.h"
0023 
0024 #include <cstdint>
0025 #include <cstdio>
0026 #include <optional>
0027 #include <sys/stat.h>
0028 
0029 namespace lldb_private {
0030 class FileSystem {
0031 public:
0032   static const char *DEV_NULL;
0033   static const char *PATH_CONVERSION_ERROR;
0034 
0035   FileSystem()
0036       : m_fs(llvm::vfs::getRealFileSystem()),
0037         m_tilde_resolver(std::make_unique<StandardTildeExpressionResolver>()) {}
0038   FileSystem(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs)
0039       : m_fs(std::move(fs)),
0040         m_tilde_resolver(std::make_unique<StandardTildeExpressionResolver>()) {}
0041   FileSystem(std::unique_ptr<TildeExpressionResolver> tilde_resolver)
0042       : m_fs(llvm::vfs::getRealFileSystem()),
0043         m_tilde_resolver(std::move(tilde_resolver)) {}
0044 
0045   FileSystem(const FileSystem &fs) = delete;
0046   FileSystem &operator=(const FileSystem &fs) = delete;
0047 
0048   static FileSystem &Instance();
0049 
0050   template <class... T> static void Initialize(T &&...t) {
0051     lldbassert(!InstanceImpl() && "Already initialized.");
0052     InstanceImpl().emplace(std::forward<T>(t)...);
0053   }
0054   static void Terminate();
0055 
0056   Status Symlink(const FileSpec &src, const FileSpec &dst);
0057   Status Readlink(const FileSpec &src, FileSpec &dst);
0058 
0059   Status ResolveSymbolicLink(const FileSpec &src, FileSpec &dst);
0060 
0061   /// Wraps ::fopen in a platform-independent way.
0062   FILE *Fopen(const char *path, const char *mode);
0063 
0064   /// Wraps ::open in a platform-independent way.
0065   int Open(const char *path, int flags, int mode = 0600);
0066 
0067   llvm::Expected<std::unique_ptr<File>>
0068   Open(const FileSpec &file_spec, File::OpenOptions options,
0069        uint32_t permissions = lldb::eFilePermissionsFileDefault,
0070        bool should_close_fd = true);
0071 
0072   /// Get a directory iterator.
0073   /// \{
0074   llvm::vfs::directory_iterator DirBegin(const FileSpec &file_spec,
0075                                          std::error_code &ec);
0076   llvm::vfs::directory_iterator DirBegin(const llvm::Twine &dir,
0077                                          std::error_code &ec);
0078   /// \}
0079 
0080   /// Returns the Status object for the given file.
0081   /// \{
0082   llvm::ErrorOr<llvm::vfs::Status> GetStatus(const FileSpec &file_spec) const;
0083   llvm::ErrorOr<llvm::vfs::Status> GetStatus(const llvm::Twine &path) const;
0084   /// \}
0085 
0086   /// Returns the modification time of the given file.
0087   /// \{
0088   llvm::sys::TimePoint<> GetModificationTime(const FileSpec &file_spec) const;
0089   llvm::sys::TimePoint<> GetModificationTime(const llvm::Twine &path) const;
0090   /// \}
0091 
0092   /// Returns the on-disk size of the given file in bytes.
0093   /// \{
0094   uint64_t GetByteSize(const FileSpec &file_spec) const;
0095   uint64_t GetByteSize(const llvm::Twine &path) const;
0096   /// \}
0097 
0098   /// Return the current permissions of the given file.
0099   ///
0100   /// Returns a bitmask for the current permissions of the file (zero or more
0101   /// of the permission bits defined in File::Permissions).
0102   /// \{
0103   uint32_t GetPermissions(const FileSpec &file_spec) const;
0104   uint32_t GetPermissions(const llvm::Twine &path) const;
0105   uint32_t GetPermissions(const FileSpec &file_spec, std::error_code &ec) const;
0106   uint32_t GetPermissions(const llvm::Twine &path, std::error_code &ec) const;
0107   /// \}
0108 
0109   /// Returns whether the given file exists.
0110   /// \{
0111   bool Exists(const FileSpec &file_spec) const;
0112   bool Exists(const llvm::Twine &path) const;
0113   /// \}
0114 
0115   /// Returns whether the given file is readable.
0116   /// \{
0117   bool Readable(const FileSpec &file_spec) const;
0118   bool Readable(const llvm::Twine &path) const;
0119   /// \}
0120 
0121   /// Returns whether the given path is a directory.
0122   /// \{
0123   bool IsDirectory(const FileSpec &file_spec) const;
0124   bool IsDirectory(const llvm::Twine &path) const;
0125   /// \}
0126 
0127   /// Returns whether the given path is local to the file system.
0128   /// \{
0129   bool IsLocal(const FileSpec &file_spec) const;
0130   bool IsLocal(const llvm::Twine &path) const;
0131   /// \}
0132 
0133   /// Make the given file path absolute.
0134   /// \{
0135   std::error_code MakeAbsolute(llvm::SmallVectorImpl<char> &path) const;
0136   std::error_code MakeAbsolute(FileSpec &file_spec) const;
0137   /// \}
0138 
0139   /// Resolve path to make it canonical.
0140   /// \{
0141   void Resolve(llvm::SmallVectorImpl<char> &path);
0142   void Resolve(FileSpec &file_spec);
0143   /// \}
0144 
0145   /// Remove a single file.
0146   ///
0147   /// The path must specify a file and not a directory.
0148   /// \{
0149   Status RemoveFile(const FileSpec &file_spec);
0150   Status RemoveFile(const llvm::Twine &path);
0151   /// \}
0152 
0153   //// Create memory buffer from path.
0154   /// \{
0155   std::shared_ptr<DataBuffer> CreateDataBuffer(const llvm::Twine &path,
0156                                                uint64_t size = 0,
0157                                                uint64_t offset = 0);
0158   std::shared_ptr<DataBuffer> CreateDataBuffer(const FileSpec &file_spec,
0159                                                uint64_t size = 0,
0160                                                uint64_t offset = 0);
0161   std::shared_ptr<WritableDataBuffer>
0162   CreateWritableDataBuffer(const llvm::Twine &path, uint64_t size = 0,
0163                            uint64_t offset = 0);
0164   std::shared_ptr<WritableDataBuffer>
0165   CreateWritableDataBuffer(const FileSpec &file_spec, uint64_t size = 0,
0166                            uint64_t offset = 0);
0167   /// \}
0168 
0169   /// Call into the Host to see if it can help find the file.
0170   bool ResolveExecutableLocation(FileSpec &file_spec);
0171 
0172   /// Get the user home directory.
0173   bool GetHomeDirectory(llvm::SmallVectorImpl<char> &path) const;
0174   bool GetHomeDirectory(FileSpec &file_spec) const;
0175 
0176   enum EnumerateDirectoryResult {
0177     /// Enumerate next entry in the current directory.
0178     eEnumerateDirectoryResultNext,
0179     /// Recurse into the current entry if it is a directory or symlink, or next
0180     /// if not.
0181     eEnumerateDirectoryResultEnter,
0182     /// Stop directory enumerations at any level.
0183     eEnumerateDirectoryResultQuit
0184   };
0185 
0186   typedef EnumerateDirectoryResult (*EnumerateDirectoryCallbackType)(
0187       void *baton, llvm::sys::fs::file_type file_type, llvm::StringRef);
0188 
0189   typedef std::function<EnumerateDirectoryResult(
0190       llvm::sys::fs::file_type file_type, llvm::StringRef)>
0191       DirectoryCallback;
0192 
0193   void EnumerateDirectory(llvm::Twine path, bool find_directories,
0194                           bool find_files, bool find_other,
0195                           EnumerateDirectoryCallbackType callback,
0196                           void *callback_baton);
0197 
0198   std::error_code GetRealPath(const llvm::Twine &path,
0199                               llvm::SmallVectorImpl<char> &output) const;
0200 
0201   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> GetVirtualFileSystem() {
0202     return m_fs;
0203   }
0204 
0205   void SetHomeDirectory(std::string home_directory);
0206 
0207 private:
0208   static std::optional<FileSystem> &InstanceImpl();
0209   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> m_fs;
0210   std::unique_ptr<TildeExpressionResolver> m_tilde_resolver;
0211   std::string m_home_directory;
0212 };
0213 } // namespace lldb_private
0214 
0215 #endif