littlefs-fuse icon indicating copy to clipboard operation
littlefs-fuse copied to clipboard

file size directly after write wrong

Open s-macke opened this issue 6 years ago • 2 comments

Following code write 1MB to the file "test" and immediately checks for the file size via fstat. The result should be 1MB, but is actually 0. However, when I start the program a second time with a existing file the result is correct.

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<fcntl.h>

size_t fsize(int fd)
{
    struct stat st;
    if (fstat(fd, &st) != 0)
    {
        perror("stat");
        exit(1);
    }
    return st.st_size;
}

int main()
{
    char data[1024*1024];
    int fd = open("test", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
    if (fd == -1)
    {
            perror("Error during open");
            exit(1);
    }
    write(fd, data, sizeof data);
    printf("%zu\n", fsize(fd));
    close(fd);

    return 0;
}

s-macke avatar Feb 04 '18 20:02 s-macke

Thanks for raising an issue.

It looks like this issue might be a bit more complicated to solve. FUSE apparently doesn't use fgetattr instead uses the path-based stat to satisfy fstat (related discussion). This unfortunately doesn't work on littlefs, because littlefs doesn't keep any RAM structures to know what files are in-flight, and instead relies on disk updates.

I'll have to see if there's a way to work around this. Worse case, littlefs-fuse might have to keep a dictionary of all open paths.

geky avatar Feb 04 '18 21:02 geky

Hmm, that sounds indeed difficult. Didn't know, that this is a an issue of FUSE. When I close and reopen the file before I check the file size it works.

s-macke avatar Feb 04 '18 22:02 s-macke