8#include "../common/FTPLogger.h"
18 bool isDirectory =
false;
40 info.isDirectory =
true;
46 : kind(mode[0] ==
'r' ? Reader : Writer), fs(fs), mode(mode) {
51 operator bool() {
return kind != Invalid; }
52 bool isDirectory() {
return info.isDirectory; }
56 if (!entries || pos >= entries->size())
return CallbackFile();
60 size_t size() {
return info.size; }
61 const char *name() {
return info.name.c_str(); }
64 int available() {
return kind == Reader && !eof ? 1 : 0; }
76 enum Kind { Invalid, Directory, Entry, Reader, Writer };
78 CallbackFileInfo info;
79 std::shared_ptr<std::vector<CallbackFileInfo>> entries;
81 CallbackFSClass *fs =
nullptr;
82 const char *mode =
"r";
104 enum Operation { Open, Close, Exists, Remove, Mkdir, Rmdir, Rename };
115 const char *
path, std::vector<CallbackFileInfo> &entries)>;
173 const char *
m = mode ==
nullptr || mode[0] ==
'r' ?
"r"
174 : mode[0] ==
'a' ?
"a"
176 return openFile(
path,
m);
181 bool remove(
const char *path) {
return report(Remove, path); }
183 bool mkdir(
const char *path) {
return report(Mkdir, path); }
185 bool rmdir(
const char *path) {
return report(Rmdir, path); }
187 bool rename(
const char *from,
const char *to) {
188 return report(Rename, from, to);
192 friend class CallbackFile;
198 CallbackFSClass() =
default;
200 CallbackFile openFile(
const char *path,
const char *mode) {
202 if (mode[0] ==
'r') {
204 std::vector<CallbackFileInfo> entries;
205 if (listCallback(path, entries))
return CallbackFile(path, entries);
208 int size = readCallback(path, 0,
nullptr, 0);
209 if (size >= 0)
return CallbackFile(
this, path, mode, size);
211 }
else if (writeCallback && writeCallback(path,
nullptr, 0)) {
212 return CallbackFile(
this, path, mode, 0);
214 return CallbackFile();
223 FTPLogger.writeLog(LOG_INFO,
"CallbackFS",
msg);
224 return callback ? callback(
op,
path,
param) :
false;
229 if (kind != Reader || eof || !fs->readCallback)
return 0;
230 int result = fs->readCallback(info.name.c_str(), pos, data,
len);
240 if (kind != Writer || !fs->writeCallback)
return 0;
241 return fs->writeCallback(info.name.c_str(), data,
len) ?
len : 0;
245 if (kind == Reader || kind == Writer) {
246 fs->
report(CallbackFSClass::Close, info.name.c_str(), mode);
CallbackFSClass File system which can be used as storage of the FTPServer: it does not store any file...
Definition CallbackFS.h:101
CallbackFile open(const char *path, const char *mode)
Open with a mode string ("r", "w" or "a")
Definition CallbackFS.h:172
void setCallback(Callback callback)
Defines the callback which is called for all file operations.
Definition CallbackFS.h:137
Operation
File operations which are reported to the callback.
Definition CallbackFS.h:104
static const char * toString(Operation op)
Provides the name of the operation.
Definition CallbackFS.h:149
bool report(Operation op, const char *path, const char *param=nullptr)
Logs the operation and calls the callback: returns its result.
Definition CallbackFS.h:218
void setListCallback(ListCallback callback)
Defines the callback which provides the directory entries.
Definition CallbackFS.h:140
std::function< bool(const char *path, const uint8_t *data, size_t len)> WriteCallback
Definition CallbackFS.h:128
void setReadCallback(ReadCallback callback)
Defines the callback which provides the content of the files.
Definition CallbackFS.h:143
std::function< bool(Operation op, const char *path, const char *param)> Callback
Definition CallbackFS.h:110
std::function< int(const char *path, size_t pos, uint8_t *data, size_t len)> ReadCallback
Definition CallbackFS.h:122
std::function< bool(const char *path, std::vector< CallbackFileInfo > &entries)> ListCallback
Definition CallbackFS.h:115
static CallbackFSClass & instance()
Provides the single instance which is available as CallbackFS.
Definition CallbackFS.h:131
void setWriteCallback(WriteCallback callback)
Defines the callback which receives the content of uploaded files.
Definition CallbackFS.h:146
CallbackFile The file which is returned by CallbackFSClass::open(): a directory (with the entries pro...
Definition CallbackFS.h:28
void close()
Closes the file: reported as Close operation for read and written files.
Definition CallbackFS.h:244
int read(uint8_t *data, size_t len)
Reads the next data via the read callback.
Definition CallbackFS.h:228
CallbackFile(const CallbackFileInfo &info)
Directory entry.
Definition CallbackFS.h:33
size_t write(const uint8_t *data, size_t len)
Provides the data to the write callback.
Definition CallbackFS.h:239
int available()
Returns 1 while the end of the file has not been reached.
Definition CallbackFS.h:64
CallbackFile(CallbackFSClass *fs, const char *path, const char *mode, size_t size)
File which is read or written via the callbacks of fs.
Definition CallbackFS.h:44
CallbackFile(const char *path, std::vector< CallbackFileInfo > entries)
Directory with its entries.
Definition CallbackFS.h:36
CallbackFile openNextFile()
Provides the next entry of a directory (or an invalid file at the end)
Definition CallbackFS.h:55
FTPSession An FTPBasicAPI which owns its command and data connections.
Definition FTPSession.h:17
FTP client and server functionality.
Definition FTPBasicAPI.h:7
Directory entry which is provided by the list callback of CallbackFS.
Definition CallbackFS.h:15