Arduino TinyFTP
Loading...
Searching...
No Matches
FTPFile.h
1#pragma once
2
3#include "Arduino.h"
4#include "FTPBasicAPI.h"
5#include "Stream.h"
6
7namespace tinyftp {
8
20class FTPFile : public Stream {
21 public:
22 FTPFile() = default;
23
26 FTPFile(FTPSessionProvider *provider, FTPBasicAPI *api, const char *name,
27 FileMode mode) {
28 FTPLogger.writeLog(LOG_DEBUG, "FTPFile", name);
29 if (name != nullptr) file_name = name;
30 this->mode = mode;
31 this->provider = provider;
32 transfer.attach(api);
33 has_session = api != nullptr;
34 is_open = provider != nullptr || api != nullptr;
35 }
36
37 size_t write(uint8_t data) override {
38 FTPLogger.writeLog(LOG_DEBUG, "FTPFile", "write");
39 Client *out = writeStream();
40 return out == nullptr ? 0 : out->write(data);
41 }
42
43 size_t write(const uint8_t *data, size_t len) override {
44 FTPLogger.writeLog(LOG_DEBUG, "FTPFile", "write");
45 Client *out = writeStream();
46 return out == nullptr ? 0 : out->write(data, len);
47 }
48
49 size_t write(const char *data, int len) {
50 return write((const uint8_t *)data, len);
51 }
52
53 int read() override {
54 FTPLogger.writeLog(LOG_DEBUG, "FTPFile", "read");
55 Client *in = readStream();
56 return in == nullptr ? -1 : in->read();
57 }
58
59 size_t readBytes(char *buf, size_t nbyte) {
60 return readBytes((uint8_t *)buf, nbyte);
61 }
62
65 size_t readBytes(uint8_t *buf, size_t nbyte) {
66 FTPLogger.writeLog(LOG_DEBUG, "FTPFile", "readBytes");
67 Client *in = readStream();
68 if (in == nullptr) return 0;
69 memset(buf, 0, nbyte);
70 return readData(in, buf, nbyte, -1);
71 }
72
74 size_t readln(char *buf, size_t nbyte) {
75 FTPLogger.writeLog(LOG_DEBUG, "FTPFile", "readln");
76 Client *in = readStream();
77 if (in == nullptr) return 0;
78 memset(buf, 0, nbyte);
79 return readData(in, (uint8_t *)buf, nbyte, eol[0]);
80 }
81
82 int peek() override {
83 FTPLogger.writeLog(LOG_DEBUG, "FTPFile", "peek");
84 Client *in = readStream();
85 return in == nullptr ? -1 : in->peek();
86 }
87
91 int available() override {
92 Client *in = readStream();
93 if (in == nullptr) return 0;
94 FTPBasicAPI *api = transfer.api();
95 unsigned long start = millis();
96 while (in->available() == 0 && api->dataConnected() &&
97 millis() - start < getTimeout()) {
98 delay(1);
99 }
100 int len = in->available();
101 char msg[80];
102 snprintf(msg, sizeof(msg), "available: %d", len);
103 FTPLogger.writeLog(LOG_DEBUG, "FTPFile", msg);
104 return len;
105 }
106
107 void flush() override {
108 FTPBasicAPI *api = transfer.api();
109 if (api != nullptr && api->currentOperation() == WRITE_OP) {
110 FTPLogger.writeLog(LOG_DEBUG, "FTPFile", "flush");
111 api->flush();
112 }
113 }
114
117 void reopen() {
118 if (provider == nullptr) return;
119 close();
120 is_open = true;
121 has_session = false;
122 }
123
125 void close() {
126 if (is_open) {
127 FTPLogger.writeLog(LOG_DEBUG, "FTPFile", "close");
128 FTPBasicAPI *api = transfer.api();
129 if (api != nullptr) api->endTransfer("close");
130 transfer.release();
131 is_open = false;
132 }
133 }
134
136 bool cancel() {
137 bool result = true;
138 if (is_open) {
139 FTPLogger.writeLog(LOG_INFO, "FTPFile", "cancel");
140 FTPBasicAPI *api = transfer.api();
141 if (api != nullptr) result = api->abort();
142 transfer.release();
143 is_open = false;
144 }
145 return result;
146 }
147
148 const char *name() const { return file_name.c_str(); }
149
150 size_t size() const {
151 if (!is_open) return 0;
152 FTPBasicAPI *api = commandSession();
153 size_t size = api == nullptr ? 0 : api->size(file_name.c_str());
154 char msg[80];
155 snprintf(msg, sizeof(msg), "size: %lu", (unsigned long)size);
156 FTPLogger.writeLog(LOG_DEBUG, "FTPFile", msg);
157 return size;
158 }
159
160 void setEOL(char *eol) {
161 FTPLogger.writeLog(LOG_DEBUG, "FTPFile", "setEOL");
162 this->eol = eol;
163 }
164
165 bool isDirectory() const {
166 if (!is_open) return false;
167 FTPLogger.writeLog(LOG_DEBUG, "FTPFile", "isDirectory");
168 FTPBasicAPI *api = commandSession();
169 return api != nullptr &&
170 api->objectType(file_name.c_str()) == TypeDirectory;
171 }
172
173 operator bool() { return is_open && file_name.length() > 0; }
174
175 protected:
176 String file_name;
177 const char *eol = "\n";
178 FileMode mode = READ_MODE;
179 FTPSessionProvider *provider = nullptr;
180 FTPTransferHandle transfer;
181 bool has_session = false; // a session was assigned for the transfer
182 bool is_open = false;
183
187 if (!has_session && provider != nullptr) {
188 has_session = true;
189 transfer.attach(provider->openDataSession());
190 }
191 return transfer.api();
192 }
193
197 FTPBasicAPI *api = transfer.api();
198 if (api != nullptr && api->currentOperation() == RESERVED_OP) return api;
199 return provider != nullptr ? provider->idleSession() : nullptr;
200 }
201
204 size_t readData(Client *in, uint8_t *buf, size_t len, int terminator) {
205 FTPBasicAPI *api = transfer.api();
206 size_t n = 0;
207 unsigned long start = millis();
208 while (n < len) {
209 int avail = in->available();
210 if (avail > 0) {
211 if (terminator < 0) {
212 size_t chunk = len - n < (size_t)avail ? len - n : (size_t)avail;
213 int got = in->read(buf + n, chunk);
214 if (got > 0) n += got;
215 } else {
216 int c = in->read();
217 if (c == terminator) break;
218 if (c >= 0) buf[n++] = (uint8_t)c;
219 }
220 start = millis();
221 } else if (!api->dataConnected() || millis() - start >= getTimeout()) {
222 break;
223 } else {
224 delay(1);
225 }
226 }
227 return n;
228 }
229
231 Client *readStream() {
232 if (!is_open) return nullptr;
233 if (mode != READ_MODE) {
234 FTPLogger.writeLog(LOG_ERROR, "FTPFile", "Cannot read in WRITE_MODE");
235 return nullptr;
236 }
237 FTPBasicAPI *api = dataSession();
238 return api == nullptr ? nullptr : api->read(file_name.c_str());
239 }
240
242 Client *writeStream() {
243 if (!is_open) return nullptr;
244 if (mode == READ_MODE) {
245 FTPLogger.writeLog(LOG_ERROR, "FTPFile", "Cannot write in READ_MODE");
246 return nullptr;
247 }
248 FTPBasicAPI *api = dataSession();
249 return api == nullptr ? nullptr : api->write(file_name.c_str(), mode);
250 }
251};
252
253} // namespace tinyftp
FTPBasicAPI Implementation of Low Level FTP protocol. In order to simplify the logic we always use Pa...
Definition FTPBasicAPI.h:17
Client * read(const char *file_name)
Starts the download (if necessary): returns nullptr if it failed.
Definition FTPBasicAPI.h:173
bool endTransfer(const char *tag)
Definition FTPBasicAPI.h:225
bool dataConnected()
Definition FTPBasicAPI.h:215
Client * write(const char *file_name, FileMode mode)
Starts the upload (if necessary): returns nullptr if it failed.
Definition FTPBasicAPI.h:187
FTPFile A single file which supports read and write operations. This class is implemented as an Ardui...
Definition FTPFile.h:20
FTPFile(FTPSessionProvider *provider, FTPBasicAPI *api, const char *name, FileMode mode)
Definition FTPFile.h:26
bool cancel()
Aborts the transfer (of all copies) and releases the session.
Definition FTPFile.h:136
size_t readln(char *buf, size_t nbyte)
Reads a line (w/o the end of line character)
Definition FTPFile.h:74
int available() override
Definition FTPFile.h:91
void close()
Ends the transfer (of all copies) and releases the session.
Definition FTPFile.h:125
Client * readStream()
Starts the download if necessary and provides the data stream.
Definition FTPFile.h:231
FTPBasicAPI * commandSession() const
Definition FTPFile.h:196
Client * writeStream()
Starts the upload if necessary and provides the data stream.
Definition FTPFile.h:242
size_t readData(Client *in, uint8_t *buf, size_t len, int terminator)
Definition FTPFile.h:204
FTPBasicAPI * dataSession()
Definition FTPFile.h:186
size_t readBytes(uint8_t *buf, size_t nbyte)
Definition FTPFile.h:65
void reopen()
Definition FTPFile.h:117
FTPSessionProvider Provides sessions on demand (implemented by the FTPClient)
Definition FTPBasicAPI.h:464
void attach(FTPBasicAPI *api)
Takes part in the current transfer of the session.
Definition FTPBasicAPI.h:503
FTPBasicAPI * api() const
Provides the session while the transfer is active (or nullptr)
Definition FTPBasicAPI.h:518
void release()
Gives up this reference.
Definition FTPBasicAPI.h:511
FTP client and server functionality.
Definition FTPBasicAPI.h:7
FileMode
File Mode.
Definition FTPCommon.h:44