use ZwCreateFile - AllocationSize parameter. if it present file system try find first continuous free clusters run with RtlFindClearBits. this is solution
i make small test
void PrintAllocSize(HANDLE hFile)
{
IO_STATUS_BLOCK iosb;
FILE_STANDARD_INFORMATION fsi;
if (0 <= ZwQueryInformationFile(hFile, &iosb, &fsi, sizeof(fsi), FileStandardInformation))
{
DbgPrint("AllocationSize=%I64x, EndOfFile=%I64x\n", fsi.AllocationSize.QuadPart, fsi.EndOfFile.QuadPart);
}
STARTING_VCN_INPUT_BUFFER vcn = {};
RETRIEVAL_POINTERS_BUFFER rpb;
NTSTATUS status = ZwFsControlFile(hFile, 0, 0, 0, &iosb, FSCTL_GET_RETRIEVAL_POINTERS, &vcn, sizeof(vcn), &rpb, sizeof(rpb));
switch (status)
{
case STATUS_SUCCESS:
case STATUS_BUFFER_OVERFLOW:
DbgPrint("ExtentCount=%x\n", rpb.ExtentCount);
break;
case STATUS_END_OF_FILE:
DbgPrint("File Is Empty\n", rpb.ExtentCount);
break;
default:
DbgPrint("ZwFsControlFile return %x\n", status);
}
}
void DoTest(POBJECT_ATTRIBUTES poa)
{
HANDLE hFile;
IO_STATUS_BLOCK iosb;
LARGE_INTEGER AllocationSize = { 0x100000 };
if (0 <= ZwCreateFile(&hFile, FILE_APPEND_DATA|SYNCHRONIZE, poa, &iosb, &AllocationSize, 0, 0, FILE_SUPERSEDE, FILE_SYNCHRONOUS_IO_NONALERT, 0, 0))
{
PrintAllocSize(hFile);
ZwClose(hFile);
}
DbgPrint("===============================\n");
if (0 <= ZwOpenFile(&hFile, SYNCHRONIZE, poa, &iosb, 0, FILE_SYNCHRONOUS_IO_NONALERT))
{
PrintAllocSize(hFile);
ZwClose(hFile);
}
ZwDeleteFile(poa);
}
and output
AllocationSize=100000, EndOfFile=0
ExtentCount=1
===============================
AllocationSize=0, EndOfFile=0
File Is Empty
when we create file with not zero AllocationSize — file-system reserve space, but file size still 0 — (AllocationSize=100000, EndOfFile=0, ExtentCount=1) if we close handle, without write data - file-system free allocated clusters (AllocationSize=0, EndOfFile=0, File Is Empty)