窗户在特定的逻辑集群上创建新文件
Windows. Create new file at specific logical cluster
相关的Microsoft文档是:https://msdn.microsoft.com/en-us/library/aa363911.aspx
但是它描述了如何将现有的碎片文件移动到新的连续逻辑块中。我想创建一个没有碎片的新文件。首先,我通过FSCTL_GET_VOLUME_BITMAP获得免费逻辑块。如何写文件在具体的自由逻辑块现在?
使用ZwCreateFile - AllocationSize参数。如果存在文件系统,尝试使用RtlFindClearBits找到第一个连续的空闲集群。这是解决方案
我做小测试
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=%I64xn", 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=%xn", rpb.ExtentCount);
break;
case STATUS_END_OF_FILE:
DbgPrint("File Is Emptyn", rpb.ExtentCount);
break;
default:
DbgPrint("ZwFsControlFile return %xn", 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);
}
和输出
AllocationSize=100000, EndOfFile=0
ExtentCount=1
===============================
AllocationSize=0, EndOfFile=0
File Is Empty
当我们创建文件时AllocationSize不为零-文件系统保留空间,但文件大小仍然为0 - (AllocationSize=100000, EndOfFile=0, ExtentCount=1)如果我们关闭句柄,没有写入数据-文件系统空闲分配集群(AllocationSize=0, EndOfFile=0, file Is Empty)
相关文章:
- 从文件中读取多行.txt字符串删除空格并创建新文件进行输出
- 如何限制文件大小,以便我的程序在变大之后创建一个新文件?并编辑新创建的文件的名称
- 从Ubuntu中的CPP程序创建新文件
- Windows Portable设备 - 创建 /复制 /删除新文件时通知
- 代码块在尝试编译或创建新文件时出现"An assertion failed!"错误
- 可执行文件无法在双击执行时创建新文件
- 流未正确创建新文件
- inotify 的替代方法,用于检测何时在文件夹下创建新文件
- 如何让我的c++程序创建一个新文件
- C++ - 修改文件而不创建新文件
- C++:为什么 ofstream 不附加或创建新文件
- 复制另一个文件时,在新文件中创建新行
- 如何在创建新文件之前检查文件是否存在
- 创建新文件时std::string to const char*错误
- Win32“浏览文件夹”对话框:用户创建新文件夹时返回错误的文件夹
- fstream未创建新文件
- 在Qml中创建一个新文件夹
- 在SDL中创建一个新文件夹或目录
- 如何使用c++在%APPDATA%中创建一个新文件夹
- Ofstream,什么时候它会失败而不是创建一个新文件