Skip to content

disk corruption issues #6

Description

@chrisrob5349

Hello,

A couple of problems I encountered while using ExFat.DiscUtils with a vmdk virtual disk formatted ExFat:

  1. The below code was used to copy files placed in a host directory to an empty directory in a VM.
    Then, we power on the VM ,delete the files from the VM manually via file explorer, then power off the VM.
    Doing this a couple of times (copying files to VM then deleting them and re-copying) with lots of files or directories causes disk corruption.
    Specifically, fill the host directory with 300-400 files, execute the given code, then delete the files copied into the VM (manually).
    After repeating this a couple of times the file system becomes corrupted.
    This has been reproduced a couple of times but seems a bit random.

  2. Copying an empty file (can be done with the below code) causes disk corruption.
    It seems this happens every time we copy empty files.

Code used:

private static void Test(string vmdkPath, int partitionNumber, string pathInHost, string pathInVm)
{
	using (VirtualDisk disk = new DiscUtils.Vmdk.Disk(vmdkPath,FileAccess.ReadWrite))
	using (ExFatFileSystem fs = new ExFatFileSystem(disk.partitions[partitionNumber].Open()))
	{
		string[] hostDirFilePaths= Directory.GetFiles(pathInHost, "*.*", SearchOption.TopDirectoryOnly);
		
		foreach (string hostFilePath in hostDirFilePaths)
		{
			string vmFilePath = Path.Combine(pathInVm, Path.GetFileName(hostFilePath));
			
			string dst = Path.Combine(fs.Root.FullName, vmFilePath);
			
			using (Stream dstStream = fs.OpenFile(dst, FileMode.Create, FileAccess.ReadWrite))
			using (Stream sourceStream = File.Open(hostFilePath, FileMode.Open, FileAccess.Read))
			{
				CoptStream(sourceStream, dstStream);
			}
		}
	}
}

private static void CopyStream(Stream srcStream, Stream dstStream)
{
	int bufferLength = (int)Math.Min(srcStream.Length, 4*1024*1024);
	byte[] buffer = new byte[bufferLength];
	int bytesRead= 0;
	
	while((bytesRead = srcStream.Read(buffer,0,bufferLength))>0)
	{
		dstStream.Write(buffer,0,bytesRead);
		if(bytesRead < bufferLength)
		{
			break;
		}
	}
}

Thanks,
Chris

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions