ARMmbed / mbed-littlefs

[experimental] Mbed OS wrapper for LittleFS v2.0 (alpha)
https://github.com/ARMmbed/littlefs
76 stars 34 forks source link

LittleFS: file created, written, closed but seems to be empty for lfs_stat() and lfs_file_read(). #12

Closed dl-dl closed 6 years ago

dl-dl commented 6 years ago

The following code demonstrates a bug in LittleFS. A file is created, written, closed with no errors, but seems to be empty (lfs_stat() shows zero size, lfs_file_read() returns zero). In the sample below errorCnt increments every 5th iteration. The bug recurrence actually depends on the block_size. The problem occurs when LFS allocates new block to extend the directory. Reproduced on both HeapBlockDevice and lfs_emubd.

HeapBlockDevice hbd(1024 * 512, 128);
hbd.init();

lfs_t fs = {0};
lfs_config cfg = {0};

cfg.context = &hbd;
cfg.read = lfs_bd_read;
cfg.prog = lfs_bd_prog;
cfg.erase = lfs_bd_erase;
cfg.sync = lfs_bd_sync;
cfg.read_size = hbd.get_read_size();
cfg.prog_size = hbd.get_program_size();
cfg.block_size = hbd.get_erase_size();
cfg.block_count = hbd.size() / cfg.block_size;
cfg.lookahead = 32 * ((cfg.block_count + 31) / 32);

lfs_format(&fs, &cfg);
lfs_mount(&fs, &cfg);

for (int i = 0; i < 32; ++i)
{
    char name[256];
    snprintf(name, 256, "TestFile%u", i);
    lfs_file_t ff;
    lfs_file_open(&fs, &ff, name, LFS_O_WRONLY | LFS_O_CREAT);
    lfs_file_write(&fs, &ff, "Hello World", 11);
    lfs_file_close(&fs, &ff);

    lfs_info info;
    int err = lfs_stat(&fs, name, &info);
    if(0 == err)
    {
        if(info.size != 11)
        {
            static int errorCnt = 0;
            errorCnt++;
        }
    }
}
lfs_unmount(&fs);
hbd.deinit();
dl-dl commented 6 years ago

Duplicate of: littlefs: Fix issue updating dir struct when extended dir chain #6338

geky commented 6 years ago

Ah! Thanks for raising an issue. Sorry I didn't see this earlier.

Glad to see the issue was fixed. https://github.com/ARMmbed/mbed-os/pull/6338 should be on mbed-os release 5.8.1.

Now that littlefs has been merged into mbed-os, it's probably better to make these issues against mbed-os in the future.