#include "filesys.h" FSInfo fsinfo; auto mount_fs() -> bool { 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 { printf("Formatting FS ! \n\r"); return LittleFS.format(); } void ls(const char * dirname) { Serial.printf("ls -l %s\n\r", dirname); Dir root = LittleFS.openDir(dirname); while (root.next()) { File file = root.openFile("r"); time_t lw = file.getLastWrite(); 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()); file.close(); } Serial.println(); } File open(const char * path) { return LittleFS.open(path, "r"); } void printFile(const char * path) { Serial.printf("Reading file: %s\n\r", path); 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) { Serial.printf("Writing file: %s\n\r", path); 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) { Serial.printf("Appending to file: %s\n\r", path); 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) { Serial.printf("Renaming file %s to %s\n\r", path1, path2); if (LittleFS.rename(path1, path2)) { Serial.println("File renamed"); } else { Serial.println("Rename failed"); } } void deleteFile(const char * path) { Serial.printf("Deleting file: %s\n\r", path); if (LittleFS.remove(path)) { Serial.println("File deleted"); } else { Serial.println("Delete failed"); } }