Is there a function to detect the file system mounted on the device?

I would like to know if there is a function to detect the file system mounted on the device?

When I asked ChatGPT, it says there is none, but suggested creating a function:
Is this the most appropriate way or is there a better approach?

#include <stdio.h>
#include "esp_err.h"
#include "esp_vfs.h"
#include "esp_vfs_fat.h"
#include "esp_vfs_spiffs.h"
#include "esp_vfs_littlefs.h"

#define TEST_FILE_PATH "/fs_test_file.txt"

typedef enum {
    FILESYSTEM_UNKNOWN = 0,
    FILESYSTEM_LITTLEFS,
    FILESYSTEM_SPIFFS,
    FILESYSTEM_FATFS
} filesystem_type_t;

filesystem_type_t detect_filesystem() {
    FILE *f = fopen(TEST_FILE_PATH, "r");
    if (f) {
        fclose(f);
        return FILESYSTEM_LITTLEFS;  // Assumindo que o sistema de arquivos esperado é LittleFS
    }
    
    // Tenta abrir um arquivo em SPIFFS
    f = fopen("/spiffs" TEST_FILE_PATH, "r");
    if (f) {
        fclose(f);
        return FILESYSTEM_SPIFFS;
    }

    // Tenta abrir um arquivo em FATFS
    f = fopen("/fat" TEST_FILE_PATH, "r");
    if (f) {
        fclose(f);
        return FILESYSTEM_FATFS;
    }
    
    return FILESYSTEM_UNKNOWN;
}

void app_main() {
    filesystem_type_t fs_type = detect_filesystem();
    switch (fs_type) {
        case FILESYSTEM_LITTLEFS:
            printf("LittleFS detectado!\n");
            break;
        case FILESYSTEM_SPIFFS:
            printf("SPIFFS detectado!\n");
            break;
        case FILESYSTEM_FATFS:
            printf("FATFS detectado!\n");
            break;
        default:
            printf("Nenhum sistema de arquivos detectado.\n");
            break;
    }
}

LittleFS, SPIFFS and FAT32 all have unique filesystem structures. Theoretically, you can read out the first block for the storage partition (if there is one at all) and try to do detection on that. For example, littlefs’s superblock contains the magic string "littlefs".

An easer way would be just try to mount each filesystem, with the setting to not format the filesystem if it could not be mounted (format_if_mount_failed = false). That way you can try mounting the next type of filesystem. For example

Just try these in sequence, and if one could be mounted, you found the right filesystem.

Of course, this is all a bit strange. After all, as the firmware developer, you usually just decide what filesystem the firmware will use. And if you never format your storage partition with a certain file system, then you won’t be able to use it anyway. So I absolutely don’t understand why you would need to “find out” what filesystem exist in the flash of the ESP32. You will decide what’s there.