0

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?

IInspectable
  • 46,945
  • 8
  • 85
  • 181
Artemus
  • 1
  • 1
  • I need some files to be laid out continuously on the disk in a certain way. I can't use a defragmentation tool beacuse I need a bit more control over where the files should be in the disk. – Artemus Dec 04 '20 at 20:24
  • What level of USB controller and drive are you using? If they're not both USB 3, it's going to be slow. – Andrew Henle Dec 04 '20 at 22:24
  • I'm using a USB 3 pendrive connected to a USB 2 port. But USB 2 should be able to reach at least 10MB/sec write speed on a fully defragmented drive and right now I'm getting less than 1MB/sec, so I don't think that's the reason for the slowness. – Artemus Dec 04 '20 at 23:17

0 Answers0