4#include "FTPBasicAPI.h"
28 FTPLogger.writeLog(LOG_DEBUG,
"FTPFile", name);
29 if (name !=
nullptr) file_name = name;
31 this->provider = provider;
33 has_session = api !=
nullptr;
34 is_open = provider !=
nullptr || api !=
nullptr;
37 size_t write(uint8_t data)
override {
38 FTPLogger.writeLog(LOG_DEBUG,
"FTPFile",
"write");
40 return out ==
nullptr ? 0 : out->write(data);
43 size_t write(
const uint8_t *data,
size_t len)
override {
44 FTPLogger.writeLog(LOG_DEBUG,
"FTPFile",
"write");
46 return out ==
nullptr ? 0 : out->write(data, len);
49 size_t write(
const char *data,
int len) {
50 return write((
const uint8_t *)data, len);
54 FTPLogger.writeLog(LOG_DEBUG,
"FTPFile",
"read");
56 return in ==
nullptr ? -1 : in->read();
59 size_t readBytes(
char *buf,
size_t nbyte) {
60 return readBytes((uint8_t *)buf, nbyte);
66 FTPLogger.writeLog(LOG_DEBUG,
"FTPFile",
"readBytes");
68 if (in ==
nullptr)
return 0;
69 memset(buf, 0, nbyte);
74 size_t readln(
char *buf,
size_t nbyte) {
75 FTPLogger.writeLog(LOG_DEBUG,
"FTPFile",
"readln");
77 if (in ==
nullptr)
return 0;
78 memset(buf, 0, nbyte);
79 return readData(in, (uint8_t *)buf, nbyte, eol[0]);
83 FTPLogger.writeLog(LOG_DEBUG,
"FTPFile",
"peek");
85 return in ==
nullptr ? -1 : in->peek();
93 if (in ==
nullptr)
return 0;
95 unsigned long start = millis();
97 millis() - start < getTimeout()) {
100 int len = in->available();
102 snprintf(msg,
sizeof(msg),
"available: %d", len);
103 FTPLogger.writeLog(LOG_DEBUG,
"FTPFile", msg);
107 void flush()
override {
109 if (api !=
nullptr && api->currentOperation() == WRITE_OP) {
110 FTPLogger.writeLog(LOG_DEBUG,
"FTPFile",
"flush");
118 if (provider ==
nullptr)
return;
127 FTPLogger.writeLog(LOG_DEBUG,
"FTPFile",
"close");
139 FTPLogger.writeLog(LOG_INFO,
"FTPFile",
"cancel");
141 if (api !=
nullptr) result = api->abort();
148 const char *name()
const {
return file_name.c_str(); }
150 size_t size()
const {
151 if (!is_open)
return 0;
153 size_t size = api ==
nullptr ? 0 : api->size(file_name.c_str());
155 snprintf(msg,
sizeof(msg),
"size: %lu", (
unsigned long)size);
156 FTPLogger.writeLog(LOG_DEBUG,
"FTPFile", msg);
160 void setEOL(
char *eol) {
161 FTPLogger.writeLog(LOG_DEBUG,
"FTPFile",
"setEOL");
165 bool isDirectory()
const {
166 if (!is_open)
return false;
167 FTPLogger.writeLog(LOG_DEBUG,
"FTPFile",
"isDirectory");
169 return api !=
nullptr &&
170 api->objectType(file_name.c_str()) == TypeDirectory;
173 operator bool() {
return is_open && file_name.length() > 0; }
177 const char *eol =
"\n";
179 FTPSessionProvider *provider =
nullptr;
180 FTPTransferHandle transfer;
181 bool has_session =
false;
182 bool is_open =
false;
187 if (!has_session && provider !=
nullptr) {
189 transfer.attach(provider->openDataSession());
191 return transfer.api();
198 if (api !=
nullptr && api->currentOperation() == RESERVED_OP)
return api;
199 return provider !=
nullptr ? provider->idleSession() :
nullptr;
204 size_t readData(Client *in, uint8_t *buf,
size_t len,
int terminator) {
207 unsigned long start = millis();
209 int avail = in->available();
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;
217 if (c == terminator)
break;
218 if (c >= 0) buf[n++] = (uint8_t)c;
221 }
else if (!api->
dataConnected() || millis() - start >= getTimeout()) {
232 if (!is_open)
return nullptr;
233 if (mode != READ_MODE) {
234 FTPLogger.writeLog(LOG_ERROR,
"FTPFile",
"Cannot read in WRITE_MODE");
238 return api ==
nullptr ? nullptr : api->
read(file_name.c_str());
243 if (!is_open)
return nullptr;
244 if (mode == READ_MODE) {
245 FTPLogger.writeLog(LOG_ERROR,
"FTPFile",
"Cannot write in READ_MODE");
249 return api ==
nullptr ? nullptr : api->
write(file_name.c_str(), mode);
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