Win32 应用程序如何找到稀疏文件中的所有漏洞? UNIX/POSIX/Linux/FreeBSD 有 lseek(..., SEEK_HOLE, ...) 和 lseek(..., SEEK_DATA, ...),但是我如何使用纯 Win32 API 做到这一点?
我们正在寻找一个 ISO C 中的代码示例,它演示了如何获取文件中所有数据部分和所有漏洞的列表,例如
file xyz.data
data from pos0...pos16383
hole from pos16384...pos65535
data from pos 65546...pos100020
ULONG GetSparseRanges(HANDLE hFile)
{
FILE_ALLOCATED_RANGE_BUFFER queryRange {{},{0, MAXLONG}}, allocRanges[16];
DWORD dwAllocRangeCount, i = 0;
BOOL fFinished = FALSE;
do
{
switch (DWORD dwError = DeviceIoControl(hFile,
FSCTL_QUERY_ALLOCATED_RANGES,
&queryRange,
sizeof(queryRange),
allocRanges,
sizeof(allocRanges),
&dwAllocRangeCount,
NULL) ? NOERROR : GetLastError())
{
case NOERROR:
fFinished = TRUE;
case ERROR_MORE_DATA:
if (dwAllocRangeCount /= sizeof(FILE_ALLOCATED_RANGE_BUFFER))
{
PFILE_ALLOCATED_RANGE_BUFFER range = allocRanges;
do
{
queryRange.FileOffset.QuadPart = range->FileOffset.QuadPart + range->Length.QuadPart;
DbgPrint("[%08x] range: [%016I64x, %016I64x) [%016I64x]\n", i++,
range->FileOffset.QuadPart,
queryRange.FileOffset.QuadPart,
range->Length.QuadPart);
} while (range++, --dwAllocRangeCount);
break;
}
return ERROR_INTERNAL_ERROR;
default:
return dwError;
}
} while (!fFinished);
return NOERROR;
}
ULONG GetSparseRanges(PCWSTR lpFileName)
{
HANDLE hFile = CreateFileW(lpFileName, FILE_READ_DATA, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if (INVALID_HANDLE_VALUE == hFile) return GetLastError();
ULONG dwError = GetSparseRanges(hFile);
CloseHandle(hFile);
return dwError;
}