Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LittleFS.setTimeCallback only sets file creation time, not lastWrite #7775

Closed
alka79 opened this issue Dec 17, 2020 · 0 comments · Fixed by #7785
Closed

LittleFS.setTimeCallback only sets file creation time, not lastWrite #7775

alka79 opened this issue Dec 17, 2020 · 0 comments · Fixed by #7785
Assignees

Comments

@alka79
Copy link

alka79 commented Dec 17, 2020

Basic Infos

  • [x ] This issue complies with the issue POLICY doc.
  • [x ] I have read the documentation at readthedocs and the issue is not addressed there.
  • [x ] I have tested that the issue is present in current master branch (aka latest git).
  • [x ] I have searched the issue tracker for a similar issue.
  • [x ] I have filled out all fields below.

Platform

  • Hardware: [ESP-12]
  • Core Version: [latest 2.7.4]
  • Development Env: [Arduino IDE]
  • Operating System: [Windows]

Settings in IDE

  • Module: [Nodemcu]
  • Flash Mode: [qio|dio|other]
  • Flash Size: [4MB]
  • lwip Variant: [v2 Lower Memory]
  • Reset Method: [nodemcu]
  • Flash Frequency: [40Mhz]
  • CPU Frequency: [80Mhz]
  • Upload Using: [SERIAL]
  • Upload Speed: [512000] (serial upload only)

Problem Description

when set by LittleFS.setTimeCallback() , the callback function is called to set the creation time of a file but seems not called for lastWrite time.
It looks like lastWrite uses time(nullptr) which here starts at 0.

myTimeCb() function here returns a fixed date. In real life, it would be a RTC or other time provider.

MCVE Sketch

/* Example based on the LittleFS_Timestamp.ino example available at github
 * MODIFIED TO USE MANUAL TIMESTAMP WITH setTimeCallback(myTimeCb)
 * This sketch  basically creates, appends and deletes a file named zhello.txt
 * It shows that file creation date is correct but lastwrite and fileTime are not
 * when using setTimeCallback().
 * Key changes in the original sketch are marked around ************* comment lines
 * 
 * Note: I tested this  on a working project with LittleFS working. 
 * If you want to first format, uncomment the LittleFS.format(); line
 */
 
/* ORIGINAL SKETCH LittleFS_Timestamp.ino
/* 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 "..."
#define STAPSK  "..."
#endif

const char *ssid = STASSID;
const char *pass = STAPSK;


// **************************
time_t myTimeCb() 
{
  Serial.println("--- myTimeCb was called");
  return 1608231600; // 17 dec 2020 @ 19:00:00 GMT
}
// ***************************

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.println(file.size());
    time_t cr = file.getCreationTime();
    time_t lw = file.getLastWrite();
    file.close();
    time_t ft = root.fileTime(); 
    struct tm * tmstruct = localtime(&cr);
    Serial.printf("  CREATION  : %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);
    tmstruct = localtime(&lw);
    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);
    tmstruct = localtime(&ft);
    Serial.printf("  fileTime  : %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 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");
  }
  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", 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 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());
  
 
  /**************************
  /* no LittleFS formating for me ! */
  /*
  Serial.println("Formatting LittleFS filesystem");
  LittleFS.format();
  */
  
  Serial.println("Mount LittleFS");
  if (!LittleFS.begin()) {
    Serial.println("LittleFS mount failed");
    return;
  }

  // ***********************************
   LittleFS.setTimeCallback(myTimeCb);   
  // ***********************************
  
  
  listDir("/");
  deleteFile("/zhello.txt");
  writeFile("/zhello.txt", "Hello ");
  

  // to differentiate creation and lastwrite timestamps
  Serial.println("\n...Waiting 2 secs...\n");
  delay(2000);     

  
  appendFile("/zhello.txt", "World!\n");

  Serial.println("");
  listDir("/");


  Serial.println("\nThe timestamp above show ");
  Serial.println("zhello.txt creation date is as provided by myTimeCb" );
  Serial.println("but lastWrite and fileTime are 01.01.1970. Probably using time(nullptr)." );

  
}

void loop() { }

Debug Messages

Serial output:

Mount LittleFS
Listing directory: /
FILE: zhello.txt  SIZE: 13                <- present from a previous run !
  CREATION  : 2020-12-17 19:00:00
  LAST WRITE: 1970-01-01 00:00:07
  fileTime  : 1970-01-01 00:00:07
Deleting file: /zhello.txt
File deleted
Writing file: /zhello.txt                 <- creates the file
--- myTimeCb was called            <- as expected
File written

...Waiting 2 secs...

Appending to file: /zhello.txt
Message appended                     <- no call to myTimeCb ??

Listing directory: /
FILE: zhello.txt  SIZE: 13
  CREATION  : 2020-12-17 19:00:00    <- as expected
  LAST WRITE: 1970-01-01 00:00:07   <- not as expected
  fileTime  : 1970-01-01 00:00:07

The timestamps above show 
zhello.txt creation date is as provided by myTimeCb
but lastWrite and fileTime are 01.01.1970. Probably using time(nullptr).
      
earlephilhower added a commit to earlephilhower/Arduino that referenced this issue Dec 21, 2020
Fixes esp8266#7775

Clean up the passing/setting of custom File time callbacks and add a
host test verifying they work.  Existing core was not passing custom
timeCallbacks set at the FS level down to open()ed files, resulting in
them calling the default time(nullptr) and reporting wrong file modify
times.
@earlephilhower earlephilhower self-assigned this Dec 21, 2020
d-a-v pushed a commit that referenced this issue Dec 22, 2020
Fixes #7775

Clean up the passing/setting of custom File time callbacks and add a
host test verifying they work.  Existing core was not passing custom
timeCallbacks set at the FS level down to open()ed files, resulting in
them calling the default time(nullptr) and reporting wrong file modify
times.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants