63 virtual std::string readFile(
const std::string &path) = 0;
64 virtual void writeFile(
const std::string &path,
const std::string &content) = 0;
65 virtual bool exists(
const std::string &path) = 0;
73 virtual std::string readFileBytes(
const std::string &path) {
return readFile(path); }
81 virtual void writeFileBytes(
const std::string &path,
const std::string &bytes)
83 writeFile(path, bytes);
86 virtual std::string name()
const = 0;
93 virtual std::vector<DirEntry> listDir(
const std::string &path)
95 throw std::runtime_error(name() +
": listDir not supported");
100 virtual std::optional<FileStat> stat(
const std::string &path)
105 virtual void mkdir(
const std::string &path)
107 throw std::runtime_error(name() +
": mkdir not supported");
110 virtual void rmdir(
const std::string &path)
112 throw std::runtime_error(name() +
": rmdir not supported");
115 virtual void unlink(
const std::string &path)
117 throw std::runtime_error(name() +
": unlink not supported");
123 virtual std::string tempArea()
132 virtual std::string cwd()
161 using ReadFunc = std::function<std::string(
const std::string &)>;
162 using WriteFunc = std::function<void(
const std::string &,
const std::string &)>;
163 using ExistsFunc = std::function<bool(
const std::string &)>;
164 using ReadBytesFunc = std::function<std::string(
const std::string &)>;
165 using WriteBytesFunc = std::function<void(
const std::string &,
const std::string &)>;
169 using ListDirFunc = std::function<std::vector<DirEntry>(
const std::string &)>;
170 using StatFunc = std::function<std::optional<FileStat>(
const std::string &)>;
171 using MkdirFunc = std::function<void(
const std::string &)>;
172 using RmdirFunc = std::function<void(
const std::string &)>;
173 using UnlinkFunc = std::function<void(
const std::string &)>;
174 using TempAreaFunc = std::function<std::string()>;
175 using CwdFunc = std::function<std::string()>;
177 CallbackFS(std::string n, ReadFunc r, WriteFunc w, ExistsFunc e)
178 : name_(std::move(n)), read_(std::move(r)), write_(std::move(w)), exists_(std::move(e))
181 std::string readFile(
const std::string &path)
override
184 throw std::runtime_error(name_ +
": read hook not installed");
187 void writeFile(
const std::string &path,
const std::string &content)
override
190 throw std::runtime_error(name_ +
": write hook not installed");
191 write_(path, content);
193 bool exists(
const std::string &path)
override {
return exists_ ? exists_(path) :
false; }
194 std::string name()
const override {
return name_; }
198 void setReadBytes(ReadBytesFunc f) { readBytes_ = std::move(f); }
199 std::string readFileBytes(
const std::string &path)
override
201 if (readBytes_)
return readBytes_(path);
202 return readFile(path);
207 void setWriteBytes(WriteBytesFunc f) { writeBytes_ = std::move(f); }
208 void writeFileBytes(
const std::string &path,
const std::string &bytes)
override
210 if (writeBytes_) { writeBytes_(path, bytes);
return; }
211 writeFile(path, bytes);
215 void setListDir(ListDirFunc f) { listDir_ = std::move(f); }
216 void setStat(StatFunc f) { stat_ = std::move(f); }
217 void setMkdir(MkdirFunc f) { mkdir_ = std::move(f); }
218 void setRmdir(RmdirFunc f) { rmdir_ = std::move(f); }
219 void setUnlink(UnlinkFunc f) { unlink_ = std::move(f); }
220 void setTempArea(TempAreaFunc f) { tempArea_ = std::move(f); }
221 void setCwd(CwdFunc f) { cwd_ = std::move(f); }
223 std::vector<DirEntry> listDir(
const std::string &path)
override
225 if (listDir_)
return listDir_(path);
226 return VirtualFS::listDir(path);
228 std::optional<FileStat> stat(
const std::string &path)
override
230 if (stat_)
return stat_(path);
233 void mkdir(
const std::string &path)
override
235 if (mkdir_) { mkdir_(path);
return; }
236 VirtualFS::mkdir(path);
238 void rmdir(
const std::string &path)
override
240 if (rmdir_) { rmdir_(path);
return; }
241 VirtualFS::rmdir(path);
243 void unlink(
const std::string &path)
override
245 if (unlink_) { unlink_(path);
return; }
246 VirtualFS::unlink(path);
248 std::string tempArea()
override
250 return tempArea_ ? tempArea_() : std::string{};
252 std::string cwd()
override
254 return cwd_ ? cwd_() : std::string{};
262 ReadBytesFunc readBytes_;
263 WriteBytesFunc writeBytes_;
264 ListDirFunc listDir_;
269 TempAreaFunc tempArea_;