oyama / pico-vfs

Thin virtual file system for Raspberry Pi Pico
Other
13 stars 4 forks source link

Some file stats don't make sense with littlefs #59

Closed Slion closed 1 month ago

Slion commented 1 month ago

I've setup my file system as follow:

#include <stdio.h>
#include <string.h>
#include <hardware/clocks.h>
#include <hardware/flash.h>
#include <pico/filesystem.h>
#include <pico/filesystem/blockdevice/flash.h>
#include <pico/filesystem/blockdevice/sd.h>
#include <pico/filesystem/filesystem/fat.h>
#include <pico/filesystem/filesystem/littlefs.h>
#include <pico/btstack_flash_bank.h>

bool fs_init(void)
{
    // Don't overwrite btstack storage
    size_t eof = PICO_FLASH_BANK_STORAGE_OFFSET;
    size_t size = FLASH_SECTOR_SIZE * 10u;
    blockdevice_t *flash = blockdevice_flash_create(eof-size, size);
    filesystem_t *lfs = filesystem_littlefs_create(500, 16);

    int err = fs_mount("/", lfs, flash);
    if (err!=0)
    {
        printf("Formatting FS\n");
        fs_format(lfs,flash);
        err = fs_mount("/", lfs, flash);
    }

    printf("fs_mount: %d :%s\n", err, fs_strerror(err));

    return true;
}

Then I'm doing some basic tests and printing some of that file stats:

    // Setup our file system
    fs_init();

    // Test our file system
    struct stat stats;
    auto fileName = "/HELLO.TXT";
    if (stat(fileName, &stats) == 0)
    {
        printf("File size / block / blocks: %d / %d / %d\n",stats.st_size, stats.st_blksize, stats.st_blocks);
        // File exists
        FILE * f = fopen (fileName, "rb");
        char content[64];
        fread(content,1,stats.st_size,f);
        fclose(f);
        printf("File content: %s\n",content);
    }
    else
    {
        // File does not exists yet
        FILE *fp = fopen(fileName, "w");
        fprintf(fp, "Hello World!\n");
        fclose(fp);
    }

Here is the output I get:

File size / block / blocks: 13 / 536885040 / 268614845
File content: Hello World!

Block size and block don't make sense to me. Could it be they are not correct?

Slion commented 1 month ago

That's because littlefs file stat does not provide those block size and block stats. See: https://github.com/oyama/pico-vfs/blob/e28888a6ac89541c103cde7f77b4e057305a877e/src/filesystem/littlefs.c#L255

If you init your stats structure with zeros they remain in place:

memset(&stats,0,sizeof(stats));
File size / block / blocks: 13 / 0 / 0