Skip to content

Commit

Permalink
prepare dual mode
Browse files Browse the repository at this point in the history
  • Loading branch information
gemu2015 committed Jan 4, 2024
1 parent 44cc775 commit 104e94f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
64 changes: 37 additions & 27 deletions lib/lib_div/ESPFtpServer/ESPFtpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,21 @@
#define F(A) A
#endif

void FtpServer::begin (String uname, String pword, FS *fp) {
void FtpServer::begin (String uname, String pword, FS *ufp, FS *ffp) {

if (is_up) return;

ufsp = fp;
ufsp = ufp;
ffsp = ffp;


dual_mode = false;

if ((uint32_t)ufsp != (uint32_t)ffsp) {
dual_mode = true;
}

cfsp = ufsp;

// Tells the ftp server to begin listening for incoming connection
_FTP_USER = uname;
Expand Down Expand Up @@ -247,7 +257,7 @@ boolean FtpServer::processCommand () {
// if found, ends the string on its position
if (ok) {
* pSep = 0;
ok = ufsp->exists (cwdName);
ok = cfsp->exists (cwdName);
}
}
// if an error appends, move to root
Expand Down Expand Up @@ -401,16 +411,16 @@ boolean FtpServer::processCommand () {
client.println (F("501 No file name"));
}
else if (makePath (path)) {
if (!ufsp->exists (path)) {
if (!cfsp->exists (path)) {
client.println (F("550 File ") + String (parameters) + F(" not found"));
}
else {
if (ufsp->remove (path)) {
if (cfsp->remove (path)) {
client.println (F("250 Deleted ") + String (parameters));
// silently recreate the directory if it vanished with the last file it contained
String directory = String (path).substring (0, String(path).lastIndexOf ("/"));
if (!ufsp->exists (directory.c_str())) {
ufsp->mkdir (directory.c_str());
if (!cfsp->exists (directory.c_str())) {
cfsp->mkdir (directory.c_str());
}
}
else {
Expand All @@ -429,7 +439,7 @@ boolean FtpServer::processCommand () {
uint16_t nm = 0;

#ifdef ESP8266
Dir dir = ufsp->openDir (cwdName);
Dir dir = cfsp->openDir (cwdName);
while (dir.next ()) {
String fname, fsize;
fname = dir.fileName ();
Expand All @@ -450,7 +460,7 @@ boolean FtpServer::processCommand () {
client.println( "226 " + String (nm) + " matches total");
#endif
#ifdef ESP32
File dir = ufsp->open (cwdName);
File dir = cfsp->open (cwdName);
if ((!dir) || (!dir.isDirectory ())) {
client.println (F("550 Can't open directory ") + String (cwdName));
}
Expand Down Expand Up @@ -502,7 +512,7 @@ boolean FtpServer::processCommand () {
client.println (F("150 Accepted data connection"));
uint16_t nm = 0;
#ifdef ESP8266
Dir dir = ufsp->openDir (cwdName);
Dir dir = cfsp->openDir (cwdName);
char dtStr[15];
while (dir.next ()) {
String fn, fs;
Expand All @@ -525,9 +535,9 @@ boolean FtpServer::processCommand () {
client.println("226 " + String(nm) + F(" matches total"));
#endif
#ifdef ESP32
File dir = ufsp->open (cwdName);
File dir = cfsp->open (cwdName);
char dtStr[15];
if (!ufsp->exists (cwdName)) {
if (!cfsp->exists (cwdName)) {
client.println (F("550 Can't open directory ") + String (parameters));
}
else {
Expand Down Expand Up @@ -572,8 +582,8 @@ boolean FtpServer::processCommand () {
client.println (F("150 Accepted data connection"));
uint16_t nm = 0;
#ifdef ESP8266
Dir dir = ufsp->openDir (cwdName);
if (!ufsp->exists (cwdName)) {
Dir dir = cfsp->openDir (cwdName);
if (!cfsp->exists (cwdName)) {
client.println (F("550 Can't open directory ") + String (parameters));
}
else {
Expand All @@ -585,8 +595,8 @@ boolean FtpServer::processCommand () {
}
#endif
#ifdef ESP32
File dir = ufsp->open (cwdName);
if (!ufsp->exists (cwdName)) {
File dir = cfsp->open (cwdName);
if (!cfsp->exists (cwdName)) {
client.println (F("550 Can't open directory ") + String (parameters));
}
else {
Expand Down Expand Up @@ -622,7 +632,7 @@ boolean FtpServer::processCommand () {
client.println (F("501 No file name"));
}
else if (makePath (path)) {
file = ufsp->open (path, "r");
file = cfsp->open (path, "r");
if (!file) {
client.println (F("550 File ") + String (parameters) + F(" not found"));
}
Expand Down Expand Up @@ -654,7 +664,7 @@ boolean FtpServer::processCommand () {
client.println (F("501 No file name"));
}
else if (makePath (path)) {
file = ufsp->open (path, "w");
file = cfsp->open (path, "w");
if (!file) {
client.println (F("451 Can't open/create ") + String (parameters));
}
Expand All @@ -681,11 +691,11 @@ boolean FtpServer::processCommand () {
else if (!strcmp (command, "MKD")) {
char path[FTP_CWD_SIZE];
if (haveParameter () && makePath (path)) {
if (ufsp->exists (path)) {
if (cfsp->exists (path)) {
client.println (F("521 Can't create \"") + String (parameters) + F("\", Directory exists"));
}
else {
if (ufsp->mkdir (path)) {
if (cfsp->mkdir (path)) {
client.println (F("257 \"") + String (parameters) + F("\" created"));
}
else {
Expand All @@ -701,14 +711,14 @@ boolean FtpServer::processCommand () {
else if (!strcmp (command, "RMD")) {
char path[FTP_CWD_SIZE];
if (haveParameter () && makePath (path)) {
if (ufsp->rmdir (path)) {
if (cfsp->rmdir (path)) {
#ifdef FTP_DEBUG
Serial.println (F("-> deleting ") + String (parameters));
#endif
client.println ("250 \"" + String (parameters) + F("\" deleted"));
}
else {
if (ufsp->exists (path)) { // hack
if (cfsp->exists (path)) { // hack
client.println (F("550 Can't remove \"") + String (parameters) + F("\". Directory not empty?"));
}
else {
Expand All @@ -730,7 +740,7 @@ boolean FtpServer::processCommand () {
client.println (F("501 No file name"));
}
else if (makePath (buf)) {
if (!ufsp->exists (buf)) {
if (!cfsp->exists (buf)) {
client.println (F("550 File ") + String (parameters) + F(" not found"));
}
else {
Expand All @@ -756,14 +766,14 @@ boolean FtpServer::processCommand () {
client.println (F("501 No file name"));
}
else if (makePath (path)) {
if (ufsp->exists (path)) {
if (cfsp->exists (path)) {
client.println (F("553 ") + String (parameters) + F(" already exists"));
}
else {
#ifdef FTP_DEBUG
Serial.println (F("-> renaming ") + String (buf) + " to " + String (path));
#endif
if (ufsp->rename (buf, path)) {
if (cfsp->rename (buf, path)) {
client.println (F("250 File successfully renamed or moved"));
}
else {
Expand Down Expand Up @@ -805,7 +815,7 @@ boolean FtpServer::processCommand () {
client.println (F("501 No file name"));
}
else if (makePath (path)) {
file = ufsp->open (path, "r");
file = cfsp->open (path, "r");
if (!file) {
client.println (F("450 Can't open ") + String (parameters));
}
Expand Down Expand Up @@ -1114,7 +1124,7 @@ bool FtpServer::makeExistsPath (char * path, char * param) {
if (!makePath (path, param)) {
return false;
}
if (ufsp->exists (path)) {
if (cfsp->exists (path)) {
return true;
}
client.println (F("550 ") + String (path) + F(" not found."));
Expand Down
6 changes: 5 additions & 1 deletion lib/lib_div/ESPFtpServer/ESPFtpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

class FtpServer {
public:
void begin(String uname, String pword, FS *fp);
void begin(String uname, String pword, FS *ufp, FS *ffp);
void handleFTP (void);
~FtpServer(void);
bool is_up = false;
Expand Down Expand Up @@ -99,6 +99,10 @@ class FtpServer {
WiFiServer *dataServer;

FS *ufsp;
FS *ffsp;
FS *cfsp;

bool dual_mode;

File file;

Expand Down
2 changes: 1 addition & 1 deletion tasmota/tasmota_xdrv_driver/xdrv_50_filesystem.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ void FTP_Server(bool onoff) {
if (onoff == true) {
if (!ftpSrv) {
ftpSrv = new FtpServer;
ftpSrv->begin(USER_FTP,PW_FTP, ufsp);
ftpSrv->begin(USER_FTP,PW_FTP, ufsp, ffsp);
}
} else {
if (ftpSrv) {
Expand Down

0 comments on commit 104e94f

Please sign in to comment.