PaulStoffregen / LittleFS

73 stars 20 forks source link

Would like a mode option to open() to overwrite an existing file #51

Closed technoblogy closed 2 years ago

technoblogy commented 2 years ago

Your wrapper to LittleFS is compatible with the basic Arduino SD library in accepting the following mode arguments to open():

FILE_READ: open the file for reading, starting at the beginning of the file. FILE_WRITE: open the file for reading and writing, starting at the end of the file.

See: https://www.arduino.cc/en/Reference/SDopen

However your implementation doesn't seem to provide an easy way to open a file that gets overwritten if it already exists (apart from deleting it first).

Although it's not documented on the Arduino page, most SD implementations provide the following mode option to achieve this:

O_RDWR | O_CREAT | O_TRUNC

Most other implementations of LittleFS (such as on ESP8266, ESP32, and RP2040) use the following options for mode (among others):

"r" - Open text file for reading. The stream is positioned at the beginning of the file.

"w" - Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.

"a" - Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.

See: https://arduino-esp8266.readthedocs.io/en/latest/filesystem.html

so "a" behaves like your FILE_WRITE, and "w" overwrites an existing file.

Would it be possible to do one of the following:

PaulStoffregen commented 2 years ago

I believe we're supporting FILE_WRITE_BEGIN, same as with the SD library.

technoblogy commented 2 years ago

Yes, sorry, I didn't spot that in the source. That's exactly what I need!

Perhaps document the options in the README, and/or include a FILE_WRITE_BEGIN example?

Thanks!