Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file size directly after write wrong #4

Open
s-macke opened this issue Feb 4, 2018 · 2 comments
Open

file size directly after write wrong #4

s-macke opened this issue Feb 4, 2018 · 2 comments

Comments

@s-macke
Copy link

s-macke commented Feb 4, 2018

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;
}
@geky
Copy link
Member

geky commented Feb 4, 2018

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.

@s-macke
Copy link
Author

s-macke commented Feb 4, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants