Cygwin 2.8.0 returns stat.st_blocks = 0 if a file is compressed with
CompactOS method (at least on Win10 1607):
Testcase:
$ ls -ls file
280 -rw-r--r-- 1 ... ... 285363 Apr 22 13:52 file
$ compact /c file
...
$ ls -ls file
56 -rw-r--r-- 1 ... ... 285363 Apr 22 13:52 file
$ compact /u file
...
$ compact /c /exe file
...
$ ls -ls file
0 -rw-r--r-- 1 ... ... 285363 Apr 22 13:52 file
This is because StandardInformation.AllocationSize is always 0 for
theses files. CompressedFileSize returns the correct value.
This is likely related to the interesting method how these files are
encoded in the MFT:
The default $DATA stream is a sparse stream with original size but no
allocated blocks.
An alternate $DATA stream WofCompressedData contains the compressed data.
An additional $REPARSE_POINT possibly marks this file a special and lets
accesses fail on older Windows releases (and on Linux, most current
forensic tools, ...).
With the attached patch, stat.st_blocks work as expected:
$ ls -ls file
48 -rw-r--r-- 1 ... ... 285363 Apr 22 13:52 file
The only drawback is an unnecessary FileCompressionInformation query for
sparse files with no blocks.
Christian
Always retrieve FileCompressionInformation for non-empty
files if FileStandardInformation returns 0 allocated blocks.
This fixes stat.st_blocks for files compressed with CompactOS method.
Signed-off-by: Christian Franke <fra...@computer.org>
---
winsup/cygwin/fhandler_disk_file.cc | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)
diff --git a/winsup/cygwin/fhandler_disk_file.cc
b/winsup/cygwin/fhandler_disk_file.cc
index fcbe15c..bf5f988 100644
--- a/winsup/cygwin/fhandler_disk_file.cc
+++ b/winsup/cygwin/fhandler_disk_file.cc
@@ -463,18 +463,25 @@ fhandler_base::fstat_helper (struct stat *buf)
buf->st_blksize = PREFERRED_IO_BLKSIZE;
- if (pfai->StandardInformation.AllocationSize.QuadPart >= 0LL)
+ if (buf->st_size == 0
+ && pfai->StandardInformation.AllocationSize.QuadPart == 0LL)
+ /* File is empty and no blocks are preallocated. */
+ buf->st_blocks = 0;
+ else if (pfai->StandardInformation.AllocationSize.QuadPart > 0LL)
/* A successful NtQueryInformationFile returns the allocation size
- correctly for compressed and sparse files as well. */
+ correctly for compressed and sparse files as well.
+ Allocation size 0 is ignored here because (at least) Windows 10
+ 1607 always returns 0 for CompactOS compressed files. */
buf->st_blocks = (pfai->StandardInformation.AllocationSize.QuadPart
+ S_BLKSIZE - 1) / S_BLKSIZE;
- else if (::has_attribute (attributes, FILE_ATTRIBUTE_COMPRESSED
- | FILE_ATTRIBUTE_SPARSE_FILE)
+ else if ((pfai->StandardInformation.AllocationSize.QuadPart == 0LL
+ || ::has_attribute (attributes, FILE_ATTRIBUTE_COMPRESSED
+ | FILE_ATTRIBUTE_SPARSE_FILE))
&& h && !is_fs_special ()
&& !NtQueryInformationFile (h, &st, (PVOID) &fci, sizeof fci,
FileCompressionInformation))
/* Otherwise we request the actual amount of bytes allocated for
- compressed and sparsed files. */
+ compressed, sparsed and CompactOS files. */
buf->st_blocks = (fci.CompressedFileSize.QuadPart + S_BLKSIZE - 1)
/ S_BLKSIZE;
else