1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/common/opener_file_system.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/common/file_system.hpp"
12
13namespace duckdb {
14
15// The OpenerFileSystem is wrapper for a file system that pushes an appropriate FileOpener into the various API calls
16class OpenerFileSystem : public FileSystem {
17public:
18 virtual FileSystem &GetFileSystem() const = 0;
19 virtual optional_ptr<FileOpener> GetOpener() const = 0;
20
21 unique_ptr<FileHandle> OpenFile(const string &path, uint8_t flags, FileLockType lock = FileLockType::NO_LOCK,
22 FileCompressionType compression = FileCompressionType::UNCOMPRESSED,
23 FileOpener *opener = nullptr) override {
24 if (opener) {
25 throw InternalException("OpenerFileSystem cannot take an opener - the opener is pushed automatically");
26 }
27 return GetFileSystem().OpenFile(path, flags, lock, compression, opener: GetOpener().get());
28 }
29
30 void Read(FileHandle &handle, void *buffer, int64_t nr_bytes, idx_t location) override {
31 GetFileSystem().Read(handle, buffer, nr_bytes, location);
32 };
33
34 void Write(FileHandle &handle, void *buffer, int64_t nr_bytes, idx_t location) override {
35 GetFileSystem().Write(handle, buffer, nr_bytes, location);
36 }
37
38 int64_t Read(FileHandle &handle, void *buffer, int64_t nr_bytes) override {
39 return GetFileSystem().Read(handle, buffer, nr_bytes);
40 }
41
42 int64_t Write(FileHandle &handle, void *buffer, int64_t nr_bytes) override {
43 return GetFileSystem().Write(handle, buffer, nr_bytes);
44 }
45
46 int64_t GetFileSize(FileHandle &handle) override {
47 return GetFileSystem().GetFileSize(handle);
48 }
49 time_t GetLastModifiedTime(FileHandle &handle) override {
50 return GetFileSystem().GetLastModifiedTime(handle);
51 }
52 FileType GetFileType(FileHandle &handle) override {
53 return GetFileSystem().GetFileType(handle);
54 }
55
56 void Truncate(FileHandle &handle, int64_t new_size) override {
57 GetFileSystem().Truncate(handle, new_size);
58 }
59
60 void FileSync(FileHandle &handle) override {
61 GetFileSystem().FileSync(handle);
62 }
63
64 bool DirectoryExists(const string &directory) override {
65 return GetFileSystem().DirectoryExists(directory);
66 }
67 void CreateDirectory(const string &directory) override {
68 return GetFileSystem().CreateDirectory(directory);
69 }
70
71 void RemoveDirectory(const string &directory) override {
72 return GetFileSystem().RemoveDirectory(directory);
73 }
74
75 bool ListFiles(const string &directory, const std::function<void(const string &, bool)> &callback,
76 FileOpener *opener = nullptr) override {
77 if (opener) {
78 throw InternalException("OpenerFileSystem cannot take an opener - the opener is pushed automatically");
79 }
80 return GetFileSystem().ListFiles(directory, callback, opener: GetOpener().get());
81 }
82
83 void MoveFile(const string &source, const string &target) override {
84 GetFileSystem().MoveFile(source, target);
85 }
86
87 string GetHomeDirectory() override {
88 return FileSystem::GetHomeDirectory(opener: GetOpener());
89 }
90
91 string ExpandPath(const string &path) override {
92 return FileSystem::ExpandPath(path, opener: GetOpener());
93 }
94
95 bool FileExists(const string &filename) override {
96 return GetFileSystem().FileExists(filename);
97 }
98
99 bool IsPipe(const string &filename) override {
100 return GetFileSystem().IsPipe(filename);
101 }
102 virtual void RemoveFile(const string &filename) override {
103 GetFileSystem().RemoveFile(filename);
104 }
105
106 virtual vector<string> Glob(const string &path, FileOpener *opener = nullptr) override {
107 if (opener) {
108 throw InternalException("OpenerFileSystem cannot take an opener - the opener is pushed automatically");
109 }
110 return GetFileSystem().Glob(path, opener: GetOpener().get());
111 }
112
113 std::string GetName() const override {
114 return "OpenerFileSystem - " + GetFileSystem().GetName();
115 }
116};
117
118} // namespace duckdb
119