Skip to content

DestinationMustMatchSourceSparseness flag not working, block-cloned file is always sparsed #17

Closed

Description

@elf2k00

If source file is NOT sparsed and CloneFlag.DestinationMustMatchSourceSparseness is set, block-cloned destination file should be NOT sparsed, however destination file is sparsed.

This Windows command can be used to check a file sparness:
fsutil sparse queryflag C:\Temp\myfile.vhdx

To set the destination file as not sparsed a FILE_SET_SPARSE_BUFFER structure should be used, currently a bool[] value is used.

This sample code successfully sets a file as not sparsed. Tested on Windows 2016 1607 and Windows 10 21H2. The only variation with the code on CloneFileAsync method is the use of a FILE_SET_SPARSE_BUFFER struct.

var FILE_SET_SPARSE_BUFFER = new FILE_SET_SPARSE_BUFFER
{
      SetSparse = false
};

if (!NativeMethods.DeviceIoControl(
    destFileHandle,
    NativeMethods.FSCTL_SET_SPARSE,
    FILE_SET_SPARSE_BUFFER,
    Marshal.SizeOf(FILE_SET_SPARSE_BUFFER),
    null,
    0,
    ref numBytesReturned,
    IntPtr.Zero))
 {
     int lastErr = Marshal.GetLastWin32Error();
     NativeMethods.ThrowSpecificIoException(lastErr,
     $"Failed to turn off file sparseness with winerror {lastErr} for destination file '{destination}'");
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions