esp-idf: FAT FS: unable to read file with long name which contains underscores (IDFGH-10008)

Answers checklist.

  • I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

IDF version.

5.0

Operating System used.

Windows

How did you build your project?

Command line with idf.py

If you are using Windows, please specify command line type.

CMD

Development Kit.

ESP32

Power Supply used.

USB

What is the expected behavior?

It should read the content of the file

What is the actual behavior?

Error while opening the file.

Steps to reproduce.

Try the below code

Debug Logs.

I (0) cpu_start: App cpu up.
I (235) cpu_start: Pro cpu start user code
I (235) cpu_start: cpu freq: 160000000 Hz
I (235) cpu_start: Application information:
I (240) cpu_start: Project name:     fatfsgen
I (245) cpu_start: App version:      v5.0-dirty
I (250) cpu_start: Compile time:     Apr 28 2023 00:34:35
I (256) cpu_start: ELF file SHA256:  0394a7ebdab63cd9...
I (262) cpu_start: ESP-IDF:          v5.0-dirty
I (268) heap_init: Initializing. RAM available for dynamic allocation:
I (275) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (281) heap_init: At 3FFB2FC0 len 0002D040 (180 KiB): DRAM
I (287) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (293) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (300) heap_init: At 4008BAA8 len 00014558 (81 KiB): IRAM
I (307) spi_flash: detected chip: generic
I (311) spi_flash: flash io: dio
I (315) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (325) example: Mounting FAT filesystem
I (335) example: FATFS Mounted.
I (335) example: Reading file : /spiflash/device_config.json
E (345) example: Failed to open device config file for reading
I (345) example: Done

More Information.

I’ve already enable the long file support in the FAT component config and with this I’m able to read the file which has length more than 8 characters as specified in the docs but it is not allowing me to read if the file extension has more tha 3 characters.

any help would be appreciated. Thanks in advance.

void read_file()
{
    const char* path = "/spiflash/device_config.json";
    ESP_LOGI(TAG, "Reading file : %s", path);

    FILE *file = fopen(path, "r");

    if (file == NULL) 
    {
        ESP_LOGE(TAG, "Failed to open device config file for reading");
        return;
    }

    fseek(file, 0, SEEK_END);
    int file_length = ftell(file);
    fseek(file, 0, SEEK_SET);

    if(file_length <= 0)
    {
        ESP_LOGE(TAG, "Failed to get device config file size");
        return;
    }

    // dynamically allocate a char array to store the file contents
    bytebeam_device_config_data = malloc(sizeof(char) * (file_length + 1));

    // if memory allocation fails just log the failure to serial and return :)
    if(bytebeam_device_config_data == NULL)
    {
        ESP_LOGE(TAG, "Failed to allocate the memory for device config file");
        return;
    }

    int temp_c;
    int loop_var = 0;

    while ((temp_c = fgetc(file)) != EOF)
    {
        bytebeam_device_config_data[loop_var] = temp_c;
        loop_var++;
    }

    bytebeam_device_config_data[loop_var] = '\0';

    ESP_LOGI(TAG, "data : %s\n", bytebeam_device_config_data);

    fclose(file);
}

void app_main(void)
{
    ESP_LOGI(TAG, "Mounting FAT filesystem");
    // To mount device we need name of device partition, define base_path
    // and allow format partition in case if it is new one and was not formatted before
    const esp_vfs_fat_mount_config_t mount_config = {
            .max_files = 4,
            .format_if_mount_failed = false,
            .allocation_unit_size = CONFIG_WL_SECTOR_SIZE
    };
    esp_err_t err;

    // if (EXAMPLE_FATFS_MODE_READ_ONLY){
    //     err = esp_vfs_fat_spiflash_mount_ro(base_path, "storage", &mount_config);
    // } else {
    //     err = esp_vfs_fat_spiflash_mount_rw_wl(base_path, "storage", &mount_config, &s_wl_handle);
    // }

    err = esp_vfs_fat_spiflash_mount_ro(base_path, "storage", &mount_config);

    if (err != ESP_OK) {
        ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
        return;
    } else {
        ESP_LOGI(TAG, "FATFS Mounted.");
    }

    read_file();
    ESP_LOGI(TAG, "Done");
}

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 17 (4 by maintainers)

Most upvoted comments

@igrr Thanks for coming up.

I was checking the fat file format and get to know that it doesn’t allow _ in the file name. I’m not sure if you notice this or not I was trying to write/read device_config.json file. So as per their logic file name is changing to DEVICE~1.JSO. You can read up more here

Solution: I’m now using deviceconfig.json file name and everything looks good.

@igrr @adokitkat Thanks for your time, I really appreciate it.

I see now, could you please describe how you are generating and flashing the filesystem? Are you following what fatfsgen example does, or doing it manually instead?

As a troubleshooting step, you can try to list all the file names in FAT partition using opendir/readdir functions. Perhaps the file name was not correctly written into the FAT image when generating it on the host?