1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/common/local_file_system.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/common/file_system.hpp"
12
13namespace duckdb {
14
15class LocalFileSystem : public FileSystem {
16public:
17 unique_ptr<FileHandle> OpenFile(const string &path, uint8_t flags, FileLockType lock = FileLockType::NO_LOCK,
18 FileCompressionType compression = FileCompressionType::UNCOMPRESSED,
19 FileOpener *opener = nullptr) override;
20
21 //! Read exactly nr_bytes from the specified location in the file. Fails if nr_bytes could not be read. This is
22 //! equivalent to calling SetFilePointer(location) followed by calling Read().
23 void Read(FileHandle &handle, void *buffer, int64_t nr_bytes, idx_t location) override;
24 //! Write exactly nr_bytes to the specified location in the file. Fails if nr_bytes could not be written. This is
25 //! equivalent to calling SetFilePointer(location) followed by calling Write().
26 void Write(FileHandle &handle, void *buffer, int64_t nr_bytes, idx_t location) override;
27 //! Read nr_bytes from the specified file into the buffer, moving the file pointer forward by nr_bytes. Returns the
28 //! amount of bytes read.
29 int64_t Read(FileHandle &handle, void *buffer, int64_t nr_bytes) override;
30 //! Write nr_bytes from the buffer into the file, moving the file pointer forward by nr_bytes.
31 int64_t Write(FileHandle &handle, void *buffer, int64_t nr_bytes) override;
32
33 //! Returns the file size of a file handle, returns -1 on error
34 int64_t GetFileSize(FileHandle &handle) override;
35 //! Returns the file last modified time of a file handle, returns timespec with zero on all attributes on error
36 time_t GetLastModifiedTime(FileHandle &handle) override;
37 //! Returns the file last modified time of a file handle, returns timespec with zero on all attributes on error
38 FileType GetFileType(FileHandle &handle) override;
39 //! Truncate a file to a maximum size of new_size, new_size should be smaller than or equal to the current size of
40 //! the file
41 void Truncate(FileHandle &handle, int64_t new_size) override;
42
43 //! Check if a directory exists
44 bool DirectoryExists(const string &directory) override;
45 //! Create a directory if it does not exist
46 void CreateDirectory(const string &directory) override;
47 //! Recursively remove a directory and all files in it
48 void RemoveDirectory(const string &directory) override;
49 //! List files in a directory, invoking the callback method for each one with (filename, is_dir)
50 bool ListFiles(const string &directory, const std::function<void(const string &, bool)> &callback,
51 FileOpener *opener = nullptr) override;
52 //! Move a file from source path to the target, StorageManager relies on this being an atomic action for ACID
53 //! properties
54 void MoveFile(const string &source, const string &target) override;
55 //! Check if a file exists
56 bool FileExists(const string &filename) override;
57
58 //! Check if path is a pipe
59 bool IsPipe(const string &filename) override;
60 //! Remove a file from disk
61 void RemoveFile(const string &filename) override;
62 //! Sync a file handle to disk
63 void FileSync(FileHandle &handle) override;
64
65 //! Runs a glob on the file system, returning a list of matching files
66 vector<string> Glob(const string &path, FileOpener *opener = nullptr) override;
67
68 bool CanHandleFile(const string &fpath) override {
69 //! Whether or not a sub-system can handle a specific file path
70 return false;
71 }
72
73 //! Set the file pointer of a file handle to a specified location. Reads and writes will happen from this location
74 void Seek(FileHandle &handle, idx_t location) override;
75 //! Return the current seek posiiton in the file.
76 idx_t SeekPosition(FileHandle &handle) override;
77
78 //! Whether or not we can seek into the file
79 bool CanSeek() override;
80 //! Whether or not the FS handles plain files on disk. This is relevant for certain optimizations, as random reads
81 //! in a file on-disk are much cheaper than e.g. random reads in a file over the network
82 bool OnDiskFile(FileHandle &handle) override;
83
84 std::string GetName() const override {
85 return "LocalFileSystem";
86 }
87
88 //! Returns the last Win32 error, in string format. Returns an empty string if there is no error, or on non-Windows
89 //! systems.
90 static std::string GetLastErrorAsString();
91
92private:
93 //! Set the file pointer of a file handle to a specified location. Reads and writes will happen from this location
94 void SetFilePointer(FileHandle &handle, idx_t location);
95 idx_t GetFilePointer(FileHandle &handle);
96
97 vector<string> FetchFileWithoutGlob(const string &path, FileOpener *opener, bool absolute_path);
98};
99
100} // namespace duckdb
101