Use global FILE_OPEN WRITE an APPEND

same meaning no need different define use only one for all filesystem
This commit is contained in:
Luc 2019-10-28 09:04:59 +01:00
parent a27045080f
commit 867b523588
12 changed files with 41 additions and 34 deletions

View File

@ -42,6 +42,9 @@
//TELNET_FEATURE : enable Telnet function //TELNET_FEATURE : enable Telnet function
#define TELNET_FEATURE #define TELNET_FEATURE
//FTP_FEATURE : enable FTP function
//#define FTP_FEATURE
//WS_DATA_FEATURE: allow to connect serial from Websocket //WS_DATA_FEATURE: allow to connect serial from Websocket
#define WS_DATA_FEATURE #define WS_DATA_FEATURE
@ -107,7 +110,7 @@
//ESP_SD_NATIVE 1 //esp32 / esp8266 //ESP_SD_NATIVE 1 //esp32 / esp8266
//ESP_SDIO 2 //esp32 only //ESP_SDIO 2 //esp32 only
//ESP_SDFAT 3 //esp8266 (same as native) / esp32 //ESP_SDFAT 3 //esp8266 (same as native) / esp32
#define SD_DEVICE ESP_SD_NATIVE #define SD_DEVICE ESP_SDFAT
//FILESYSTEM_TIMESTAMP_FEATURE: allow to get last write time from FILESYSTEM files //FILESYSTEM_TIMESTAMP_FEATURE: allow to get last write time from FILESYSTEM files
//#define SD_TIMESTAMP_FEATURE //#define SD_TIMESTAMP_FEATURE
@ -122,7 +125,7 @@
//ESP_SPIFFS_FILESYSTEM 0 //ESP_SPIFFS_FILESYSTEM 0
//ESP_FAT_FILESYSTEM 1 //ESP_FAT_FILESYSTEM 1
//ESP_LITTLEFS_FILESYSTEM 2 //ESP_LITTLEFS_FILESYSTEM 2
#define FILESYSTEM_FEATURE ESP_FAT_FILESYSTEM #define FILESYSTEM_FEATURE ESP_SPIFFS_FILESYSTEM
//DIRECT_PIN_FEATURE: allow to access pin using ESP201 command //DIRECT_PIN_FEATURE: allow to access pin using ESP201 command
#define DIRECT_PIN_FEATURE #define DIRECT_PIN_FEATURE

View File

@ -59,7 +59,7 @@ bool Commands::ESP740(const char* cmd_params, level_authenticate_type auth_type,
output->printf("Directory on SD : %s", parameter.c_str()); output->printf("Directory on SD : %s", parameter.c_str());
output->printLN(""); output->printLN("");
if (ESP_SD::exists(parameter.c_str())) { if (ESP_SD::exists(parameter.c_str())) {
ESP_SDFile f = ESP_SD::open(parameter.c_str(), ESP_SD_FILE_READ); ESP_SDFile f = ESP_SD::open(parameter.c_str(), ESP_FILE_READ);
uint countf = 0; uint countf = 0;
uint countd = 0; uint countd = 0;
if (f) { if (f) {
@ -77,7 +77,7 @@ bool Commands::ESP740(const char* cmd_params, level_authenticate_type auth_type,
sub = f.openNextFile(); sub = f.openNextFile();
} }
f.close(); f.close();
f = ESP_SD::open(parameter.c_str(), ESP_SD_FILE_READ); f = ESP_SD::open(parameter.c_str(), ESP_FILE_READ);
//Check files //Check files
sub = f.openNextFile(); sub = f.openNextFile();
while (sub) { while (sub) {

View File

@ -92,7 +92,7 @@ bool Commands::ESP750(const char* cmd_params, level_authenticate_type auth_type,
} }
parameter = get_param (cmd_params, "create="); parameter = get_param (cmd_params, "create=");
if (parameter.length() != 0) { if (parameter.length() != 0) {
ESP_SDFile f = ESP_SD::open(parameter.c_str(), ESP_SD_FILE_WRITE); ESP_SDFile f = ESP_SD::open(parameter.c_str(), ESP_FILE_WRITE);
if (f.isOpen()) { if (f.isOpen()) {
f.close(); f.close();
output->printMSG ("ok"); output->printMSG ("ok");

View File

@ -116,4 +116,14 @@
#define ESP_ERROR_START_UPLOAD 12 #define ESP_ERROR_START_UPLOAD 12
#define ESP_ERROR_SIZE 13 #define ESP_ERROR_SIZE 13
//File system
#define ESP_FILE_READ 0
#define ESP_FILE_WRITE 1
#define ESP_FILE_APPEND 2
#define FS_ROOT 0
#define FS_FLASH 1
#define FS_SD 2
#define FS_USBDISK 3
#endif //_DEFINES_ESP3D_H #endif //_DEFINES_ESP3D_H

View File

@ -24,9 +24,6 @@
#ifdef FILESYSTEM_TIMESTAMP_FEATURE #ifdef FILESYSTEM_TIMESTAMP_FEATURE
#include <time.h> #include <time.h>
#endif //FILESYSTEM_TIMESTAMP_FEATURE #endif //FILESYSTEM_TIMESTAMP_FEATURE
#define ESP_FILE_READ 0
#define ESP_FILE_WRITE 1
#define ESP_FILE_APPEND 2
#define ESP_FLASH_FS_HEADER "/FS:" #define ESP_FLASH_FS_HEADER "/FS:"

View File

@ -25,9 +25,6 @@
#ifdef SD_TIMESTAMP_FEATURE #ifdef SD_TIMESTAMP_FEATURE
#include <time.h> #include <time.h>
#endif //SD_TIMESTAMP_FEATURE #endif //SD_TIMESTAMP_FEATURE
#define ESP_SD_FILE_READ 0
#define ESP_SD_FILE_WRITE 1
#define ESP_SD_FILE_APPEND 2
#define ESP_SD_HEADER "/SD:" #define ESP_SD_HEADER "/SD:"
@ -87,7 +84,7 @@ public:
static uint64_t freeBytes(); static uint64_t freeBytes();
static const char * FilesystemName(); static const char * FilesystemName();
static bool format(ESP3DOutput * output = nullptr); static bool format(ESP3DOutput * output = nullptr);
static ESP_SDFile open(const char* path, uint8_t mode = ESP_SD_FILE_READ); static ESP_SDFile open(const char* path, uint8_t mode = ESP_FILE_READ);
static bool exists(const char* path); static bool exists(const char* path);
static bool remove(const char *path); static bool remove(const char *path);
static bool mkdir(const char *path); static bool mkdir(const char *path);

View File

@ -111,14 +111,14 @@ bool ESP_SD::format(ESP3DOutput * output)
ESP_SDFile ESP_SD::open(const char* path, uint8_t mode) ESP_SDFile ESP_SD::open(const char* path, uint8_t mode)
{ {
//do some check //do some check
if(((strcmp(path,"/") == 0) && ((mode == ESP_SD_FILE_WRITE) || (mode == ESP_SD_FILE_APPEND))) || (strlen(path) == 0)) { if(((strcmp(path,"/") == 0) && ((mode == ESP_FILE_WRITE) || (mode == ESP_FILE_APPEND))) || (strlen(path) == 0)) {
return ESP_SDFile(); return ESP_SDFile();
} }
// path must start by '/' // path must start by '/'
if (path[0] != '/') { if (path[0] != '/') {
return ESP_SDFile(); return ESP_SDFile();
} }
if (mode != ESP_SD_FILE_READ) { if (mode != ESP_FILE_READ) {
//check container exists //check container exists
String p = path; String p = path;
p.remove(p.lastIndexOf('/') +1); p.remove(p.lastIndexOf('/') +1);
@ -127,8 +127,8 @@ ESP_SDFile ESP_SD::open(const char* path, uint8_t mode)
return ESP_SDFile(); return ESP_SDFile();
} }
} }
File tmp = SD.open(path, (mode == ESP_SD_FILE_READ)?FILE_READ:(mode == ESP_SD_FILE_WRITE)?FILE_WRITE:FILE_APPEND); File tmp = SD.open(path, (mode == ESP_FILE_READ)?FILE_READ:(mode == ESP_FILE_WRITE)?FILE_WRITE:FILE_APPEND);
ESP_SDFile esptmp(&tmp, tmp.isDirectory(),(mode == ESP_SD_FILE_READ)?false:true, path); ESP_SDFile esptmp(&tmp, tmp.isDirectory(),(mode == ESP_FILE_READ)?false:true, path);
return esptmp; return esptmp;
} }
@ -141,7 +141,7 @@ bool ESP_SD::exists(const char* path)
} }
res = SD.exists(path); res = SD.exists(path);
if (!res) { if (!res) {
ESP_SDFile root = ESP_SD::open(path, ESP_SD_FILE_READ); ESP_SDFile root = ESP_SD::open(path, ESP_FILE_READ);
if (root) { if (root) {
res = root.isDirectory(); res = root.isDirectory();
} }

View File

@ -586,7 +586,7 @@ bool ESP_SD::format(ESP3DOutput * output)
ESP_SDFile ESP_SD::open(const char* path, uint8_t mode) ESP_SDFile ESP_SD::open(const char* path, uint8_t mode)
{ {
//do some check //do some check
if(((strcmp(path,"/") == 0) && ((mode == ESP_SD_FILE_WRITE) || (mode == ESP_SD_FILE_APPEND))) || (strlen(path) == 0)) { if(((strcmp(path,"/") == 0) && ((mode == ESP_FILE_WRITE) || (mode == ESP_FILE_APPEND))) || (strlen(path) == 0)) {
_sizechanged = true; _sizechanged = true;
return ESP_SDFile(); return ESP_SDFile();
} }
@ -594,7 +594,7 @@ ESP_SDFile ESP_SD::open(const char* path, uint8_t mode)
if (path[0] != '/') { if (path[0] != '/') {
return ESP_SDFile(); return ESP_SDFile();
} }
if (mode != ESP_SD_FILE_READ) { if (mode != ESP_FILE_READ) {
//check container exists //check container exists
String p = path; String p = path;
p.remove(p.lastIndexOf('/') +1); p.remove(p.lastIndexOf('/') +1);
@ -603,8 +603,8 @@ ESP_SDFile ESP_SD::open(const char* path, uint8_t mode)
return ESP_SDFile(); return ESP_SDFile();
} }
} }
sdfat::File tmp = SD.open(path, (mode == ESP_SD_FILE_READ)?FILE_READ:(mode == ESP_SD_FILE_WRITE)?FILE_WRITE:FILE_WRITE); sdfat::File tmp = SD.open(path, (mode == ESP_FILE_READ)?FILE_READ:(mode == ESP_FILE_WRITE)?FILE_WRITE:FILE_WRITE);
ESP_SDFile esptmp(&tmp, tmp.isDir(),(mode == ESP_SD_FILE_READ)?false:true, path); ESP_SDFile esptmp(&tmp, tmp.isDir(),(mode == ESP_FILE_READ)?false:true, path);
return esptmp; return esptmp;
} }
@ -617,7 +617,7 @@ bool ESP_SD::exists(const char* path)
} }
res = SD.exists(path); res = SD.exists(path);
if (!res) { if (!res) {
ESP_SDFile root = ESP_SD::open(path, ESP_SD_FILE_READ); ESP_SDFile root = ESP_SD::open(path, ESP_FILE_READ);
if (root) { if (root) {
res = root.isDirectory(); res = root.isDirectory();
} }

View File

@ -585,7 +585,7 @@ bool ESP_SD::format(ESP3DOutput * output)
ESP_SDFile ESP_SD::open(const char* path, uint8_t mode) ESP_SDFile ESP_SD::open(const char* path, uint8_t mode)
{ {
//do some check //do some check
if(((strcmp(path,"/") == 0) && ((mode == ESP_SD_FILE_WRITE) || (mode == ESP_SD_FILE_APPEND))) || (strlen(path) == 0)) { if(((strcmp(path,"/") == 0) && ((mode == ESP_FILE_WRITE) || (mode == ESP_FILE_APPEND))) || (strlen(path) == 0)) {
_sizechanged = true; _sizechanged = true;
return ESP_SDFile(); return ESP_SDFile();
} }
@ -593,7 +593,7 @@ ESP_SDFile ESP_SD::open(const char* path, uint8_t mode)
if (path[0] != '/') { if (path[0] != '/') {
return ESP_SDFile(); return ESP_SDFile();
} }
if (mode != ESP_SD_FILE_READ) { if (mode != ESP_FILE_READ) {
//check container exists //check container exists
String p = path; String p = path;
p.remove(p.lastIndexOf('/') +1); p.remove(p.lastIndexOf('/') +1);
@ -602,8 +602,8 @@ ESP_SDFile ESP_SD::open(const char* path, uint8_t mode)
return ESP_SDFile(); return ESP_SDFile();
} }
} }
File tmp = SD.open(path, (mode == ESP_SD_FILE_READ)?FILE_READ:(mode == ESP_SD_FILE_WRITE)?FILE_WRITE:FILE_WRITE); File tmp = SD.open(path, (mode == ESP_FILE_READ)?FILE_READ:(mode == ESP_FILE_WRITE)?FILE_WRITE:FILE_WRITE);
ESP_SDFile esptmp(&tmp, tmp.isDir(),(mode == ESP_SD_FILE_READ)?false:true, path); ESP_SDFile esptmp(&tmp, tmp.isDir(),(mode == ESP_FILE_READ)?false:true, path);
return esptmp; return esptmp;
} }
@ -616,7 +616,7 @@ bool ESP_SD::exists(const char* path)
} }
res = SD.exists(path); res = SD.exists(path);
if (!res) { if (!res) {
ESP_SDFile root = ESP_SD::open(path, ESP_SD_FILE_READ); ESP_SDFile root = ESP_SD::open(path, ESP_FILE_READ);
if (root) { if (root) {
res = root.isDirectory(); res = root.isDirectory();
} }

View File

@ -100,14 +100,14 @@ bool ESP_SD::format(ESP3DOutput * output)
ESP_SDFile ESP_SD::open(const char* path, uint8_t mode) ESP_SDFile ESP_SD::open(const char* path, uint8_t mode)
{ {
//do some check //do some check
if(((strcmp(path,"/") == 0) && ((mode == ESP_SD_FILE_WRITE) || (mode == ESP_SD_FILE_APPEND))) || (strlen(path) == 0)) { if(((strcmp(path,"/") == 0) && ((mode == ESP_FILE_WRITE) || (mode == ESP_FILE_APPEND))) || (strlen(path) == 0)) {
return ESP_SDFile(); return ESP_SDFile();
} }
// path must start by '/' // path must start by '/'
if (path[0] != '/') { if (path[0] != '/') {
return ESP_SDFile(); return ESP_SDFile();
} }
if (mode != ESP_SD_FILE_READ) { if (mode != ESP_FILE_READ) {
//check container exists //check container exists
String p = path; String p = path;
p.remove(p.lastIndexOf('/') +1); p.remove(p.lastIndexOf('/') +1);
@ -116,8 +116,8 @@ ESP_SDFile ESP_SD::open(const char* path, uint8_t mode)
return ESP_SDFile(); return ESP_SDFile();
} }
} }
File tmp = SD_MMC.open(path, (mode == ESP_SD_FILE_READ)?FILE_READ:(mode == ESP_SD_FILE_WRITE)?FILE_WRITE:FILE_APPEND); File tmp = SD_MMC.open(path, (mode == ESP_FILE_READ)?FILE_READ:(mode == ESP_FILE_WRITE)?FILE_WRITE:FILE_APPEND);
ESP_SDFile esptmp(&tmp, tmp.isDirectory(),(mode == ESP_SD_FILE_READ)?false:true, path); ESP_SDFile esptmp(&tmp, tmp.isDirectory(),(mode == ESP_FILE_READ)?false:true, path);
return esptmp; return esptmp;
} }
@ -130,7 +130,7 @@ bool ESP_SD::exists(const char* path)
} }
res = SD_MMC.exists(path); res = SD_MMC.exists(path);
if (!res) { if (!res) {
ESP_SDFile root = ESP_SD::open(path, ESP_SD_FILE_READ); ESP_SDFile root = ESP_SD::open(path, ESP_FILE_READ);
if (root) { if (root) {
res = root.isDirectory(); res = root.isDirectory();
} }

View File

@ -144,7 +144,7 @@ void HTTP_Server::handleSDFileList ()
_webserver->sendHeader("Cache-Control","no-cache"); _webserver->sendHeader("Cache-Control","no-cache");
_webserver->send(200); _webserver->send(200);
if (ESP_SD::exists(ptmp.c_str())) { if (ESP_SD::exists(ptmp.c_str())) {
ESP_SDFile f = ESP_SD::open(ptmp.c_str(), ESP_SD_FILE_READ); ESP_SDFile f = ESP_SD::open(ptmp.c_str(), ESP_FILE_READ);
//Parse files //Parse files
ESP_SDFile sub = f.openNextFile(); ESP_SDFile sub = f.openNextFile();
if (f) { if (f) {

View File

@ -78,7 +78,7 @@ void HTTP_Server::SDFileupload ()
} }
if (_upload_status!=UPLOAD_STATUS_FAILED) { if (_upload_status!=UPLOAD_STATUS_FAILED) {
//create file //create file
fsUploadFile = ESP_SD::open(filename.c_str(), ESP_SD_FILE_WRITE); fsUploadFile = ESP_SD::open(filename.c_str(), ESP_FILE_WRITE);
//check If creation succeed //check If creation succeed
if (fsUploadFile) { if (fsUploadFile) {
//if yes upload is started //if yes upload is started