Skip to content

Fail to open file for write on SPIFFS #4311

Closed
@brianjmurrell

Description

@brianjmurrell

Basic Infos

Same as in #4291

Sketch

#include <FS.h>

void setup() {

  Serial.begin(115200);

  SPIFFS.begin();

  FSInfo fs_info;
  SPIFFS.info(fs_info);
  printf("SPIFFS: %lu of %lu bytes used.\n",
         fs_info.usedBytes, fs_info.totalBytes);

  SPIFFS.remove("/stats-test.txt");
  File f = SPIFFS.open("/stats-test.txt", "w");
  if (!f) {
    Serial.println("Failed to open /stats-test.txt.  Nothing else is going to work.");
  }
  f.close();

}

loop() {
  File f = SPIFFS.open("/stats-test.txt", "a");
  if (!f) {
    Serial.println("Failed to open /stats-test.txt");
    return;
  }
  if (!f.println(msg)) {
    Serial.println("Failed to write to /stats-test.txt");
    f.close();
    return;
  }
  unsigned long fsize = f.size();
  f.close();

  if (fsize > last_fsize) {
    Serial.println("appending ");
  } else {
    Serial.println("stopping, nothing new written to file");
    
    FSInfo fs_info;
    SPIFFS.info(fs_info);
    printf("SPIFFS: %lu of %lu bytes used.\n",
           fs_info.usedBytes, fs_info.totalBytes);
    
    while (true) {
      delay(1000);
    }
  }
}

Debug Messages

SPIFFS: 2206039 of 2949250 bytes used.
Failed to open /stats-test.txt.  Nothing else is going to work.
Failed to open /stats-test.txt
Failed to open /stats-test.txt
Failed to open /stats-test.txt
Failed to open /stats-test.txt
Failed to open /stats-test.txt
Failed to open /stats-test.txt
Failed to open /stats-test.txt
Failed to open /stats-test.txt
Failed to open /stats-test.txt
Failed to open /stats-test.txt
Failed to open /stats-test.txt
stopping, nothing new written to file
SPIFFS: 2206039 of 2949250 bytes used.

Why did it fail to open so many times? Then it did finally open but the write added nothing new.

There is still lots of space available on the SPIFFS.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions