mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-22 21:23:07 +03:00
* Add time to filesystem API Support the ESP32 File::getLastWrite() call and setting the time on all filesystems automatically (assuming the system clock has been set properly and time(NULL) returns the proper time!). Adds Dir::fileTime() to get the time of a file being listed, similar to Dir::fileName() and Dir::fileSize(). Adds ::setTimeCallback(time_t (*cb)()) to File, Dir, and FS, allowing users to override the default timestamp on a per-file, directory, or filesystem basis. By default, a simple callback returning time(nullptr) is implemented. LittleFS uses the 't' attribute and should be backwards compatible. SD/SDFS work and include wrappers for obsolete SdFat timestamp callbacks using the MSDOS time. This PR does not update SPIFFS, due to compatability concerns and a possible massive rewrite which would make it possible to determine if an old-style ot metadata enabled FS is present at mount time. Includes an updated SD/listfiles and LittleFS_time example. Replaces #6315 * Add links to new mklittlefs w/timestamp support Include the update mklittlefs which generated 't' metadata on imported files. ../tools/sdk/lwip2/include/netif/lowpan6_opts.h * Add explicit note about timestamp being local time * Address review concerns Clean up some awkward object instantiations. Remove the _enableTime flag/setter from SPIFFS. Clean up the FSConfig constructors using C++ style init lists.
182 lines
4.3 KiB
C++
182 lines
4.3 KiB
C++
/* Example showing timestamp support in LittleFS */
|
|
/* Released into the public domain. */
|
|
/* Earle F. Philhower, III <earlephilhower@yahoo.com> */
|
|
|
|
#include <FS.h>
|
|
#include <LittleFS.h>
|
|
#include <time.h>
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#ifndef STASSID
|
|
#define STASSID "your-ssid"
|
|
#define STAPSK "your-password"
|
|
#endif
|
|
|
|
const char *ssid = STASSID;
|
|
const char *pass = STAPSK;
|
|
|
|
long timezone = 2;
|
|
byte daysavetime = 1;
|
|
|
|
|
|
bool getLocalTime(struct tm * info, uint32_t ms) {
|
|
uint32_t count = ms / 10;
|
|
time_t now;
|
|
|
|
time(&now);
|
|
localtime_r(&now, info);
|
|
|
|
if (info->tm_year > (2016 - 1900)) {
|
|
return true;
|
|
}
|
|
|
|
while (count--) {
|
|
delay(10);
|
|
time(&now);
|
|
localtime_r(&now, info);
|
|
if (info->tm_year > (2016 - 1900)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
void listDir(const char * dirname) {
|
|
Serial.printf("Listing directory: %s\n", dirname);
|
|
|
|
Dir root = LittleFS.openDir(dirname);
|
|
|
|
while (root.next()) {
|
|
File file = root.openFile("r");
|
|
Serial.print(" FILE: ");
|
|
Serial.print(root.fileName());
|
|
Serial.print(" SIZE: ");
|
|
Serial.print(file.size());
|
|
time_t t = file.getLastWrite();
|
|
struct tm * tmstruct = localtime(&t);
|
|
file.close();
|
|
Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
|
|
}
|
|
}
|
|
|
|
|
|
void readFile(const char * path) {
|
|
Serial.printf("Reading file: %s\n", 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", 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");
|
|
}
|
|
file.close();
|
|
}
|
|
|
|
void appendFile(const char * path, const char * message) {
|
|
Serial.printf("Appending to file: %s\n", 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", 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", path);
|
|
if (LittleFS.remove(path)) {
|
|
Serial.println("File deleted");
|
|
} else {
|
|
Serial.println("Delete failed");
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
// We start by connecting to a WiFi network
|
|
Serial.println();
|
|
Serial.println();
|
|
Serial.print("Connecting to ");
|
|
Serial.println(ssid);
|
|
|
|
WiFi.begin(ssid, pass);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("WiFi connected");
|
|
Serial.println("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
Serial.println("Contacting Time Server");
|
|
configTime(3600 * timezone, daysavetime * 3600, "time.nist.gov", "0.pool.ntp.org", "1.pool.ntp.org");
|
|
struct tm tmstruct ;
|
|
delay(2000);
|
|
tmstruct.tm_year = 0;
|
|
getLocalTime(&tmstruct, 5000);
|
|
Serial.printf("\nNow is : %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct.tm_year) + 1900, (tmstruct.tm_mon) + 1, tmstruct.tm_mday, tmstruct.tm_hour, tmstruct.tm_min, tmstruct.tm_sec);
|
|
Serial.println("");
|
|
|
|
Serial.printf("Formatting LittleFS filesystem\n");
|
|
LittleFS.format();
|
|
listDir("/");
|
|
deleteFile("/hello.txt");
|
|
writeFile("/hello.txt", "Hello ");
|
|
appendFile("/hello.txt", "World!\n");
|
|
listDir("/");
|
|
|
|
Serial.printf("The timestamp should be valid above\n");
|
|
|
|
Serial.printf("Now unmount and remount and perform the same operation.\n");
|
|
Serial.printf("Timestamp should be valid, data should be good.\n");
|
|
LittleFS.end();
|
|
Serial.printf("Now mount it\n");
|
|
if (!LittleFS.begin()) {
|
|
Serial.println("LittleFS mount failed");
|
|
return;
|
|
}
|
|
readFile("/hello.txt");
|
|
listDir("/");
|
|
|
|
|
|
}
|
|
|
|
void loop() { }
|
|
|