Report
9 months, 1 week ago.

LittleFileSystem fopen("file","w") does remove existing file

According to the C++ fopen documentation at http://www.cplusplus.com/reference/cstdio/fopen/ if 'mode' is "w" and the filename points to an existing file, the contents of the existing file are cleared.

FATFileSystem does have this behaviour but LittleFileSystem does not seem to. Here's some example code....

example

  char * filename = "/fs/eventlog.bin";
  FILE *fd = fopen(filename,"w");
  fclose(fd);
  struct stat stat_buf;
  stat(filename, &stat_buf);
  ASSERT(stat_buf.st_size!=0,"file size should be zero after opening");

When the underlying filesystem is LittleFileSystem and if the file already exists and it not empty, this asserts since the filesize is not changed on fopen.

Obviously a workaround is to use the remove method (or possibly a zero-length write?)

Thanks, reported here.

posted by Jan Jongboom 10 Jan 2018
Comment on this question

1 Answer

9 months, 1 week ago.

Thanks for reporting, looks like this was a valid bug related to how LittleFS was handling the O_TRUNC flag. Here's the patch to fix it:
https://github.com/ARMmbed/mbed-os/pull/5832

You should be able to get the patch in the next patch release of Mbed OS or so (5.7.3?)


To post an answer, please log in.