Arduino TinyFTP
Loading...
Searching...
No Matches
CallbackFS.h
1#pragma once
2#include <Arduino.h>
3
4#include <functional>
5#include <memory>
6#include <vector>
7
8#include "../common/FTPLogger.h"
9
10namespace tinyftp {
11
12class CallbackFSClass;
13
16 String name; // file name (without directory)
17 size_t size = 0;
18 bool isDirectory = false;
19};
20
29 public:
30 CallbackFile() = default;
31
33 CallbackFile(const CallbackFileInfo &info) : kind(Entry), info(info) {}
34
36 CallbackFile(const char *path, std::vector<CallbackFileInfo> entries)
37 : kind(Directory),
38 entries(std::make_shared<std::vector<CallbackFileInfo>>(entries)) {
39 info.name = path;
40 info.isDirectory = true;
41 }
42
44 CallbackFile(CallbackFSClass *fs, const char *path, const char *mode,
45 size_t size)
46 : kind(mode[0] == 'r' ? Reader : Writer), fs(fs), mode(mode) {
47 info.name = path;
48 info.size = size;
49 }
50
51 operator bool() { return kind != Invalid; }
52 bool isDirectory() { return info.isDirectory; }
53
56 if (!entries || pos >= entries->size()) return CallbackFile();
57 return CallbackFile((*entries)[pos++]);
58 }
59
60 size_t size() { return info.size; }
61 const char *name() { return info.name.c_str(); }
62
64 int available() { return kind == Reader && !eof ? 1 : 0; }
65
67 int read(uint8_t *data, size_t len);
68
70 size_t write(const uint8_t *data, size_t len);
71
73 void close();
74
75 protected:
76 enum Kind { Invalid, Directory, Entry, Reader, Writer };
77 Kind kind = Invalid;
78 CallbackFileInfo info;
79 std::shared_ptr<std::vector<CallbackFileInfo>> entries;
80 size_t pos = 0; // next directory entry or read position
81 CallbackFSClass *fs = nullptr;
82 const char *mode = "r";
83 bool eof = false;
84};
85
102 public:
104 enum Operation { Open, Close, Exists, Remove, Mkdir, Rmdir, Rename };
105
109 using Callback =
110 std::function<bool(Operation op, const char *path, const char *param)>;
111
114 using ListCallback = std::function<bool(
115 const char *path, std::vector<CallbackFileInfo> &entries)>;
116
121 using ReadCallback = std::function<int(const char *path, size_t pos,
122 uint8_t *data, size_t len)>;
123
128 std::function<bool(const char *path, const uint8_t *data, size_t len)>;
129
132 static CallbackFSClass fs;
133 return fs;
134 }
135
137 void setCallback(Callback callback) { this->callback = callback; }
138
140 void setListCallback(ListCallback callback) { listCallback = callback; }
141
143 void setReadCallback(ReadCallback callback) { readCallback = callback; }
144
146 void setWriteCallback(WriteCallback callback) { writeCallback = callback; }
147
149 static const char *toString(Operation op) {
150 switch (op) {
151 case Open:
152 return "open";
153 case Close:
154 return "close";
155 case Exists:
156 return "exists";
157 case Remove:
158 return "remove";
159 case Mkdir:
160 return "mkdir";
161 case Rmdir:
162 return "rmdir";
163 case Rename:
164 return "rename";
165 }
166 return "?";
167 }
168
169 CallbackFile open(const char *path) { return openFile(path, "r"); }
170
172 CallbackFile open(const char *path, const char *mode) {
173 const char *m = mode == nullptr || mode[0] == 'r' ? "r"
174 : mode[0] == 'a' ? "a"
175 : "w";
176 return openFile(path, m);
177 }
178
179 bool exists(const char *path) { return report(Exists, path); }
180
181 bool remove(const char *path) { return report(Remove, path); }
182
183 bool mkdir(const char *path) { return report(Mkdir, path); }
184
185 bool rmdir(const char *path) { return report(Rmdir, path); }
186
187 bool rename(const char *from, const char *to) {
188 return report(Rename, from, to);
189 }
190
191 protected:
192 friend class CallbackFile;
193 Callback callback;
194 ListCallback listCallback;
195 ReadCallback readCallback;
196 WriteCallback writeCallback;
197
198 CallbackFSClass() = default;
199
200 CallbackFile openFile(const char *path, const char *mode) {
201 report(Open, path, mode);
202 if (mode[0] == 'r') {
203 if (listCallback) {
204 std::vector<CallbackFileInfo> entries;
205 if (listCallback(path, entries)) return CallbackFile(path, entries);
206 }
207 if (readCallback) {
208 int size = readCallback(path, 0, nullptr, 0);
209 if (size >= 0) return CallbackFile(this, path, mode, size);
210 }
211 } else if (writeCallback && writeCallback(path, nullptr, 0)) {
212 return CallbackFile(this, path, mode, 0);
213 }
214 return CallbackFile();
215 }
216
218 bool report(Operation op, const char *path, const char *param = nullptr) {
219 char msg[160];
220 snprintf(msg, sizeof(msg), "%s %s%s%s", toString(op),
221 path == nullptr ? "" : path, param == nullptr ? "" : " ",
222 param == nullptr ? "" : param);
223 FTPLogger.writeLog(LOG_INFO, "CallbackFS", msg);
224 return callback ? callback(op, path, param) : false;
225 }
226};
227
228inline int CallbackFile::read(uint8_t *data, size_t len) {
229 if (kind != Reader || eof || !fs->readCallback) return 0;
230 int result = fs->readCallback(info.name.c_str(), pos, data, len);
231 if (result <= 0) {
232 eof = true;
233 return 0;
234 }
235 pos += result;
236 return result;
237}
238
239inline size_t CallbackFile::write(const uint8_t *data, size_t len) {
240 if (kind != Writer || !fs->writeCallback) return 0;
241 return fs->writeCallback(info.name.c_str(), data, len) ? len : 0;
242}
243
244inline void CallbackFile::close() {
245 if (kind == Reader || kind == Writer) {
246 fs->report(CallbackFSClass::Close, info.name.c_str(), mode);
247 }
248 kind = Invalid;
249}
250
252static CallbackFSClass &CallbackFS = CallbackFSClass::instance();
253
254} // namespace tinyftp
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