espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.66k stars 7.42k forks source link

LittleFS - writing empty file shows "- write failed" when in fact the file was written correctly #10556

Open rFlightFPV opened 1 week ago

rFlightFPV commented 1 week ago

Board

LilyGo ESP32 T-SIM7080G

Device Description

No additional hardware

Hardware Configuration

No

Version

v3.0.6

IDE Name

Arduino IDE 2.3.3

Operating System

MacOS Sequoia 15.1

Flash frequency

240 MHz

PSRAM enabled

yes

Upload speed

460800

Description

Using LittleFS included in core and examples provided in this repository. I am using a webserver form where the user can populate a field or leave it empty. It works fine writing and reading files. LittleFS seems to be perfectly capable of writing empty files, the file is written and is listed when reading the directory (see below) with size:0. However when writing the file, the Serial printout is " -write failed" which is not true. I know it is minor issue as it's just an error in printout handling as far as I understand. Still it can be irritating while debugging.

Sketch

// write file to LittleFS
void writeFile(fs::FS &fs, const char * path, const char * message){
  Serial.printf("Writing file: %s\r\n", path);

  File file = fs.open(path, FILE_WRITE);
  if(!file){
    Serial.println("- failed to open file for writing");
    return;
  }
  if(file.print(message)){
    Serial.println("- file written");
  } else {
    Serial.println("- write failed");
  }
}

Debug Message

12:18:45.292 -> Writing file: /test.txt
12:18:45.292 -> - write failed
.
13:04:04.056 -> Listing directory: /
13:04:04.056 ->   FILE: test.txt    SIZE: 0
.
13:04:04.185 -> Reading file: /test.txt

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

P-R-O-C-H-Y commented 2 days ago

Hi @rFlightFPV, I have checked the code and if size of the message is 0 -> no message, write returns 0. From my point of view the approach is fine, as you can't write "nothing". Maybe you can add a check in the sketch, to not try to write when there is nothing to write.