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

Number of file blocks calculated incorrectly #120

Open
phuclv90 opened this issue Jul 28, 2018 · 0 comments
Open

Number of file blocks calculated incorrectly #120

phuclv90 opened this issue Jul 28, 2018 · 0 comments

Comments

@phuclv90
Copy link

After a quick look it seems that FileStat.blocks() uses _stat64 on Windows to calculate the number from st_size, which is seriously wrong

public long blocks() {
        return (layout.st_size.get(memory) + 512 - 1) / 512;
}

A file may have less number of blocks allocated (like sparse files and compressed files), or no blocks allocated at all (called resident files on Windows, inline files on Linux). So you can't divide the size by 512 to get the number of blocks. You must call the function GetCompressedFileSize() instead. Alternatively you can use the command fsutil file layout to get that information

On Linux

The st_blocks field indicates the number of blocks allocated to the file, 512-byte units. (This may be smaller than st_size/512 when the file has holes.)
https://linux.die.net/man/2/stat64

so it'll work for sparse files, but I'm not sure if it works for inline files and compressed files. If it doesn't return the number of real blocks for those then you must use whatever system call du is using

And why is there no documents for the interfaces? I couldn't understand why blocks() assumes block size as 512 (which is incorrect in most cases) until I read the stat64 man page

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

1 participant