readdir returnes inexistent files when partition is mounted using FATFS library with wear levelling

A6ESPF
Posts: 28
Joined: Tue Dec 10, 2019 12:16 pm

readdir returnes inexistent files when partition is mounted using FATFS library with wear levelling

Postby A6ESPF » Mon Dec 12, 2022 12:49 pm

I've created several partitions needed for my application (as defined in a custom flash_partitions.csv file):

Code: Select all

# 16 KiB
nvs,       data, nvs,     0x9000,   0x4000,
# 8 KiB
otadata,   data, ota,     0xd000,   0x2000,
# 4 KiB
phy_init,  data, phy,     0xf000,   0x1000,
# 1 MiB + 128 KiB
ota_0,     app,  ota_0,   0x10000,  0x120000,
# 1 MiB + 128 KiB
ota_1,     app,  ota_1,   0x130000, 0x120000,
# 1 MiB + 704 KiB
storage_1, data, fat,     0x250000, 0x1B0000,
For the sake of the test, I mounted the storage_1 partition in the main function using esp-idf fatfs component. Then, I used opendir and readdir functions in order to list all the files on the storage_1 partition, as shown below:

Code: Select all

#include <stdio.h>
#include <sys/stat.h>
#include <dirent.h>
#include "esp_vfs_fat.h"

wl_handle_t g_wl_handle = WL_INVALID_HANDLE;

void app_main(void)
{
    // Mount partition
    esp_vfs_fat_mount_config_t wear_levelling_mount_config;

    wear_levelling_mount_config.allocation_unit_size = CONFIG_WL_SECTOR_SIZE; // sector size is 4096
    wear_levelling_mount_config.max_files = 10;
    wear_levelling_mount_config.format_if_mount_failed = true;

    esp_err_t ret = esp_vfs_fat_spiflash_mount("/spiflash", "storage_1", &wear_levelling_mount_config, &g_wl_handle);
    
    // Read files from partition
    DIR* fd;
    struct dirent* dir = NULL;

    fd = opendir("/spiflash");

    if (NULL != fd)
    {
        printf("dirent: /spiflash opendir successful.\n");
        do
        {
            dir = readdir(fd);
            if (NULL != dir)
            {
                printf("file: %s\n", strlwr(dir->d_name));
            }
        } while (NULL != dir);
    }
}
The program then mounts the directory successfully, but readdir finds inexistent files. This is the output of the program:

Code: Select all

dirent: /spiflash opendir successful.
file: ��������.���
file: ��������.���
file: ��������.���
file: ��������.���
file: ��������.���
file: ��������.���
file: ��������.���
file: ��������.���
file: ��������.���
... // a lot more files
What could be the issue here?

A6ESPF
Posts: 28
Joined: Tue Dec 10, 2019 12:16 pm

Re: readdir returnes inexistent files when partition is mounted using FATFS library with wear levelling

Postby A6ESPF » Mon Dec 12, 2022 3:51 pm

When I compare my application to esp-idf storage/wear_levelling example, they are basically the same. But I can't get the inexistent files when running the example. However, when I replace the "partitions_example.csv" configuration in the example with my partitions configuration, I do get the inexistent files. Is there something wrong with my partitions configuration?
Edit: I forgot to add that I erase the flash every time before I flash the new binary, so flash corruption isn't caused by changing partitions or something.

A6ESPF
Posts: 28
Joined: Tue Dec 10, 2019 12:16 pm

Re: readdir returnes inexistent files when partition is mounted using FATFS library with wear levelling

Postby A6ESPF » Tue Dec 13, 2022 9:32 am

I found a stack overflow question that is describing the same problem as mine (https://stackoverflow.com/questions/695 ... filesystem), but the author solved the problem by just ignoring the garbage files when looking for files with readdir. I would like to dive deeper into this and find the real solution. Any help or suggestion is appreciated.

A6ESPF
Posts: 28
Joined: Tue Dec 10, 2019 12:16 pm

Re: readdir returnes inexistent files when partition is mounted using FATFS library with wear levelling

Postby A6ESPF » Thu Dec 15, 2022 11:07 am

After thorough research, I've decided to switch from FAT based wear-levelled filesystem to LittleFS, as FAT is fragile and prone to power loss failures (as suggested here https://espressif-docs.readthedocs-host ... power-loss). I haven't had any problems with garbage files since.

Who is online

Users browsing this forum: benrank, Google [Bot] and 137 guests