ESP8266_Treppenlicht/lib/httpserver/filesys.cpp

124 lines
2.9 KiB
C++
Raw Normal View History

2021-06-22 21:38:39 +00:00
#include "filesys.h"
FSInfo fsinfo;
auto mount_fs() -> bool {
2021-06-23 13:22:38 +00:00
if(!LittleFS.begin()) {
Serial.println("[ERROR] LittleFS.info(), reset ...");
return false;
}
if(!LittleFS.info(fsinfo)) {
Serial.println("[ERROR] LittleFS.info(), reset ...");
return false;
}
printf("Filesystem opened:\n\r");
printf("\ttotalBytes:\t%d\n\r", fsinfo.totalBytes);
printf("\tusedBytes:\t%d\n\r", fsinfo.usedBytes);
printf("\tblockSize:\t%d\n\r", fsinfo.blockSize);
printf("\tpageSize:\t%d\n\r", fsinfo.pageSize);
printf("\tmaxOpenFiles:\t%d\n\r", fsinfo.maxOpenFiles);
printf("\tmaxPathLength:\t%d\n\r", fsinfo.maxPathLength);
printf("\n\r");
return true;
}
auto format_fs() -> bool {
2021-06-23 13:22:38 +00:00
printf("Formatting FS ! \n\r");
return LittleFS.format();
}
2021-06-22 23:30:28 +00:00
void ls(const char * dirname) {
Serial.printf("ls -l %s\n\r", dirname);
2021-06-22 21:38:39 +00:00
Dir root = LittleFS.openDir(dirname);
while (root.next()) {
File file = root.openFile("r");
2021-06-22 23:30:28 +00:00
2021-06-22 21:38:39 +00:00
time_t lw = file.getLastWrite();
2021-06-22 23:30:28 +00:00
struct tm * tmstruct = localtime(&lw);
Serial.printf("%8d %02d %02d %02d:%02d %s\n\r",
file.size(),
tmstruct->tm_mon + 1,
tmstruct->tm_mday,
tmstruct->tm_hour,
tmstruct->tm_min,
root.fileName().c_str());
2021-06-22 21:38:39 +00:00
file.close();
}
2021-06-22 23:30:28 +00:00
Serial.println();
2021-06-22 21:38:39 +00:00
}
File open(const char * path) {
return LittleFS.open(path, "r");
}
void printFile(const char * path) {
2021-06-22 23:30:28 +00:00
Serial.printf("Reading file: %s\n\r", path);
2021-06-22 21:38:39 +00:00
File file = LittleFS.open(path, "r");
if (!file) {
Serial.println("Failed to open file for reading");
return;
}
Serial.print("Read from file: ");
while (file.available()) {
Serial.write(file.read());
}
file.close();
}
void writeFile(const char * path, const char * message) {
2021-06-22 23:30:28 +00:00
Serial.printf("Writing file: %s\n\r", path);
2021-06-22 21:38:39 +00:00
File file = LittleFS.open(path, "w");
if (!file) {
Serial.println("Failed to open file for writing");
return;
}
if (file.print(message)) {
Serial.println("File written");
} else {
Serial.println("Write failed");
}
delay(2000); // Make sure the CREATE and LASTWRITE times are different
file.close();
}
void appendFile(const char * path, const char * message) {
2021-06-22 23:30:28 +00:00
Serial.printf("Appending to file: %s\n\r", path);
2021-06-22 21:38:39 +00:00
File file = LittleFS.open(path, "a");
if (!file) {
Serial.println("Failed to open file for appending");
return;
}
if(file.print(message)) {
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
file.close();
}
void renameFile(const char * path1, const char * path2) {
2021-06-22 23:30:28 +00:00
Serial.printf("Renaming file %s to %s\n\r", path1, path2);
2021-06-22 21:38:39 +00:00
if (LittleFS.rename(path1, path2)) {
Serial.println("File renamed");
} else {
Serial.println("Rename failed");
}
}
void deleteFile(const char * path) {
2021-06-22 23:30:28 +00:00
Serial.printf("Deleting file: %s\n\r", path);
2021-06-22 21:38:39 +00:00
if (LittleFS.remove(path)) {
Serial.println("File deleted");
} else {
Serial.println("Delete failed");
}
2021-07-05 14:15:47 +00:00
}