Search code examples
filefilesystemscross-platformfilesizestat

Is filesize/stat consistent across all filesystems?


I am considering writing software which uses the filesize as pretest to test whether two files are equivalent. There is no need to apply sophisticated file content comparisons if a simple file size integer comparison fails. The software is going to written in golang (first), but I think this question really boils down to the stat syscall and therefore is language independent.

I necessarily need a platform-independent solution. It has to work across all systems and file systems. I can be sure that the file content will be the same sequence of bytes across all filesystems, but what about the filesize?

If I transfer a file from one filesystem to another, can I be sure to get the same filesize on the other filesystem?

[Of course, I don't care about file metadata. This is obviously inconsistent. I only care about content sizes]


Solution

  • Yes, st_size should be the same across all filesystems (at least if they are posix compliant). A byte is a byte after all, no matter where you store it. The disk space consumed can be different though, depending on the underlying block size of the filesystem.