How can I perform a Copy­File in unbuffered mode?

A customer was copying a file with Copy­File, but they wanted the file handles to be opened as FILE_FLAG_NO_BUFFERING.

We saw some time ago that you can use the progress callback to Copy­File­Ex or Copy­File2 to flush the output handle. Maybe we can use the progress callback to open the handle as unbuffered?

Nope, that doesn't work because the progress callback gives you the already-opened handle. You can't change its buffering flag after the fact.

But that's okay, because Copy­File­Ex and Copy­File2 also have a flags parameter, and one of the flags is COPY_FILE_NO_BUFFERING, which means that the handle should be opened as FILE_FLAG_NO_BUFFERING.

BOOL success = CopyFileEx(
    sourceFilePath, destinationFilePath,
    nullptr, nullptr, nullptr,
    COPY_FILE_NO_BUFFERING);

You can do the same with Copy­File2, but the flags are in the options structure.

COPYFILE2_EXTENDED_PARAMETERS parameters{};
parameters.dwSize = sizeof(parameters);
parameters.dwCopyFlags = COPY_FILE_NO_BUFFERING;
HRESULT hr = CopyFile2(sourceFilePath, destinationFilePath, &parameters);
添加评论
点赞收藏
点踩分享查看原文
评论
?
参与讨论