Page 1 of 1

FAT FS Maximum number of files

Posted: Sat Sep 19, 2020 7:08 am
by Wootsies
Hi everyone I am pretty new to the ESP32 and I have been experimenting with a persistent storage mechanism.

Because I intend to use flash encryption eventually, I have been testing a FAT file system although at this stage I have not actually turned it on in the compile options. While using the example for wear leveling in ESP IDF, I tried to create 600 files and met an issue where I cant seem to create more than 512 files. Does anyone know what is the cause for this? When I return the errno value I get an error of -2 and I cant tell what exactly it means. When I check the space left in the FAT using f_getfree I find that I still have space remaining so it is not because I have run out of space s I have set the partition as 1MB.

Any help to point me in the right direction would be appreciated.
Thanks in advance!

I set my

Code: Select all

 CONFIG_WL_SECTOR_SIZE = 512
in the wear leveling options of idf menu

Code snippet:

Code: Select all

    static wl_handle_t s_wl_handle = WL_INVALID_HANDLE;

    esp_err_t err;
    char *basePath = "/spiflash";
    char *logFilePath = "/spiflash/";

    FILE *f;
    const esp_vfs_fat_mount_config_t mount_config = {
        .max_files = 4,
        .format_if_mount_failed = true,
        .allocation_unit_size = CONFIG_WL_SECTOR_SIZE};
    err = esp_vfs_fat_spiflash_mount(basePath, "storage", &mount_config, &s_wl_handle);

    char filepath[27] = ""; 
    char str[12];           // Index

    for (int k = 0; k < 600; k++)
    {
        filepath[0] = '\0'; 
        sprintf(str, "%d", k);              // convert int to char
        strncat(filepath, logFilePath, 14); // Put Base path
        strncat(filepath, str, 12);         // Add index
        ESP_LOGI(TAG, "Get Entry FilePath: %s", filepath);

        f = fopen(filepath, "w");
        if (f == NULL)
        {
            ESP_LOGE(TAG, "Failed to open file for writing error: %d", errno);
            return;
        }
        else
        {
            if (fprintf(f, "%s", "helloWorld") == EOF)
            {
                ESP_LOGE(TAG, "ERROR RETURNED, Memory Might be full");
            }
            fclose(f);
        }

        fclose(f);

    }

    esp_vfs_fat_spiflash_unmount(basePath, s_wl_handle);

Partition:

Code: Select all

# Name,   Type, SubType, Offset,  Size, Flags
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
nvs,      data, nvs,     0x9000,  0x6000,
phy_init, data, phy,     0xf000,  0x1000,
factory,  app,  factory, 0x10000, 1M,
storage,  data, fat,     ,        1M, encrypted

Re: FAT FS Maximum number of files

Posted: Sun Sep 20, 2020 7:40 am
by ESP_Sprite
Did you create these files in the root folder of the FAT partition? If I recall correctly, one of the quirks of FAT (in general, so not only the ESP32 implementation) is that it only allows a limited amount of files in the root folder. Non-root folders do not have that limitation.

Re: FAT FS Maximum number of files

Posted: Sun Sep 20, 2020 12:29 pm
by chegewara
There is 2 problems you have:
- like @ESP_Sprite mentioned, FAT root folder has limited number of entries (folders and files),
- this is bigger problem: each single file is using at least 1 cluster and if cluster size is 4096 bytes, then you are limited to about 256 files on 1MB partition; since you have sector/cluster size 512 your limitation will be about 1000 files.

Since we dont know files size and this is only theoretically calculation, you may encounter any of those issues.

Re: FAT FS Maximum number of files

Posted: Sun Sep 20, 2020 3:47 pm
by Wootsies
Thanks for the response everyone.
You were precisely right, it seems that the limit on the root directory is 512. Once I created a sub-directory, I was not limited anymore.