Reputation: 1
I'm writing a small program to relocate virtual clusters of large files (from 1GB up to 4GB) inside a USB pendrive using DeviceIoControl with the FSCTL_MOVE_FILE control code. The pendrive is formatted as FAT32 (this is a requirement) with a 64K allocation unit size. So far I'm able to move files without problem but the process is very slow.
I did some testing with an unfragmented 100MB file (I made sure no other processes were using the pendrive while moving the file) and it takes aprox. 2 minutes to realocate. Copying files inside the pendrive doesn't take nealry as long so it should be possible to achieve better speeds than that.
Here's the relevant part of my code:
HANDLE volumeHandle = CreateFile( // Opening volume handle
volumeDrive.c_str(),
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (volumeHandle == INVALID_HANDLE_VALUE)
{
ReportError(L"Invalid volume handle");
return 1;
}
MOVE_FILE_DATA moveData = {0};
moveData.FileHandle = CreateFile( // Opening file handle
argv[1],
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (moveData.FileHandle == INVALID_HANDLE_VALUE)
{
ReportError(L"Invalid file handle");
return 1;
}
// Fill rest of input buffer
moveData.StartingVcn.QuadPart = 0;
moveData.StartingLcn.QuadPart = destination.startingLCN;
moveData.ClusterCount = (DWORD)totalFileLengthInClusters;
DWORD unused;
// Move file
BOOL result = DeviceIoControl(
volumeHandle, // handle to volume
FSCTL_MOVE_FILE, // dwIoControlCode
&moveData, // MOVE_FILE_DATA structure
sizeof(moveData), // size of input buffer
NULL, // lpOutBuffer
0, // nOutBufferSize
&unused, // number of bytes returned
NULL // OVERLAPPED structure
);
My question is: Am I using the right flags when opening the volume and file handles for optimal speed? Is there anything else I can do to speed up the relocation process?
Upvotes: 0
Views: 196