1

I want to switch from SPIFFS to FAT on my Esp32 projects due to encryption. In my example project I have this.

esp_vfs_spiffs_conf_t conf = {
  .base_path = "/spiffs",
  .partition_label = NULL,
  .max_files = 5,   // This decides the maximum number of files that can be created on the storage
  .format_if_mount_failed = true
};
esp_err_t ret = esp_vfs_spiffs_register(&conf);

...

ESP_LOGI(TAG, "Opening file 1");
FILE *f = fopen("/spiffs/hello.json", "w");
if (f == NULL) {
    ESP_LOGE(TAG, "Failed to open file 1 for writing test");
}
ESP_LOGI(TAG, "Opening file 2");
FILE *f2 = fopen("/spiffs/hello.txt", "w");
if (f2 == NULL) {
    ESP_LOGE(TAG, "Failed to open file 2 for writing test");
    return ESP_FAIL;
}

It works fine and creates both files as expected, but this:

esp_vfs_fat_mount_config_t conf = {
     .format_if_mount_failed = true,
     .max_files = 5,
     .allocation_unit_size = CONFIG_WL_SECTOR_SIZE
   };
esp_err_t ret = esp_vfs_fat_spiflash_mount("/fatfs", "storage", &conf, &s_wl_handle);    

ESP_LOGI(TAG, "Opening file 1");
FILE *f = fopen("/fatfs/hello.json", "w");
if (f == NULL) {
    ESP_LOGE(TAG, "Failed to open file 1 for writing test");
}
ESP_LOGI(TAG, "Opening file 2");
FILE *f2 = fopen("/fatfs/hello.txt", "w");
if (f2 == NULL) {
    ESP_LOGE(TAG, "Failed to open file 2 for writing test");
    return ESP_FAIL;
}

fails for the json file.

I (3672) example: Opening file 1
E (3672) example: Failed to open file 1 for writing test
I (3682) example: Opening file 2
...

Google so far gave me nothing on FatFS having any issue with file extensions. Can someone help me understand this?

karel
  • 5,489
  • 46
  • 45
  • 50
SolvedForHome
  • 152
  • 1
  • 15

1 Answers1

2

I hope the issue is already resolved, but I will answer anyway to support other developers with a similar struggle.

I believe this may happen because you didn't enable long file names for FATFS. With no LFN support, you only can have three characters in the extension (and 8 in the filename). To fix this, please invoke menuconfig using idf.py menuconfig and then head to Component config -> FAT Filesystem support -> Long filename support -> Long filename buffer {in heap|on stack}.

Then rebuild the project and check if it works now.

Martin Gaňo
  • 41
  • 1
  • 5
  • I have not worked on it in a while. But it was not resolved, I think. As soon as possible I will re-check, but I think LFN was enabled. Will update here when there is news. Thanks. – SolvedForHome Jul 07 '22 at 06:39
  • I think there's a bug in the FatFS library. We are using `R0.11a` and I cannot for the life of me figure out why LFN is not working as fully intended. With it enabled, I am able to write a file in an 8.4 format (`blahblah.json`). However, if I attempt to read from the directory, it treats it cuts off the last letter of the extension (so the filename is `blahblah.jso`) . Stepping through the code, it enters all LFN blocks, but fails to extract the entire filename. – jabroni Jun 06 '23 at 04:41