Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a fixed size disk requires 'extra space' in the underlying stream #272

Open
Yazwh0 opened this issue Feb 11, 2023 · 1 comment
Open

Comments

@Yazwh0
Copy link

Yazwh0 commented Feb 11, 2023

Take the code below, if the - 512 isn't included you get a System.NotSupportedException: 'Memory stream is not expandable.'

Presumably this is for the VHD footer?

using DiscUtils.Fat;
using DiscUtils.Partitions;
using DiscUtils.Vhd;

// create disk
{
    var size = 32 * 1024 * 1024;
    var rawMemory = new byte[size];
    var data = new MemoryStream(rawMemory, 0, size, true);

    var disk = Disk.InitializeFixed(data, DiscUtils.Streams.Ownership.None, (long)size - 512); // needed for the vhd footer??
    BiosPartitionTable.Initialize(disk, WellKnownPartitionType.WindowsFat);
    var fileSystem = FatFileSystem.FormatPartition(disk, 0, "TEST");

    Console.WriteLine($"Is : {fileSystem.FatVariant}");

    var newFile = fileSystem.OpenFile("FOO.TXT", FileMode.CreateNew, FileAccess.Write);
    newFile.Write(new byte[] { 1, 2, 3, 4 });
    newFile.Close();

    Console.WriteLine($"File exists? {fileSystem.Exists("FOO.TXT")}");

    File.WriteAllBytes(@"c:\temp\test.vhd", rawMemory);
}
@LTRData
Copy link

LTRData commented Feb 11, 2023

Yes, most disk image formats need space for metadata and they also need to be aligned to a certain block size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants