littlefs-fuse
littlefs-fuse copied to clipboard
file size directly after write wrong
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;
}
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.
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.