Basic file operations not working as expected when using SPIFFS

parkersnable
Posts: 1
Joined: Wed Oct 18, 2023 2:04 pm

Basic file operations not working as expected when using SPIFFS

Postby parkersnable » Wed Oct 18, 2023 2:38 pm

Hi,

I am currently building a ESP-IDF project using SPIFFS to store various files.

It's mostly going as expected, but I noticed some anomalies.

Reading and writing the files with fopen, fread and fwrite works fine.

While implementing a initial test to check if the file exists and write a blank file if it doesn't I realized, that the access() function always returns -1 to signal the file does not exist. I literally open it with fopen in read mode and the pointer is not NULL, still access returns an error.

Don't I have the permission to check for existence?

Here is the code in question:
  1. ESP_LOGI(TAG, "Testing file access for: %s", path);
  2. FILE* test = fopen(path, "r");
  3. if (test == NULL) {
  4.     ESP_LOGI(TAG, "cant read file");
  5. }
  6. fclose(test);
  7. if (access(path, F_OK) == -1) {
  8.     // The log file does not exist, so initialize it with a base structure
  9.     FILE* file = fopen(path, "wb");
  10.     if (file == NULL) {
  11.         ESP_LOGE(TAG, "Failed to create file: %s", path);
  12.         return ESP_FAIL;
  13.     }
  14.     else {
  15.         ESP_LOGI(TAG, "file created: %s", path);
  16.     }
  17.     if (fwrite(data, size, 1, file) != 1) {
  18.         ESP_LOGE(TAG, "Failed to write init structure to file: %s", path);
  19.         fclose(file);
  20.         return ESP_FAIL;
  21.     }
  22.     else {
  23.         ESP_LOGI(TAG, "wrote data to file: %s", path);
  24.     }
  25.  
  26.     fclose(file);
  27. }

pcoueffin
Posts: 1
Joined: Fri Dec 08, 2023 9:10 pm

Re: Basic file operations not working as expected when using SPIFFS

Postby pcoueffin » Fri Dec 08, 2023 9:40 pm

Signed up to report that access() works for me in Dec of 2023. ESP32-C2 using espidf on platformio.

I initially thought I had this problem but it turns out that prepending the mountpoint to the filename was important.

Who is online

Users browsing this forum: Baidu [Spider] and 112 guests