Skip to content

Commit

Permalink
Added a new disk scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Meulengracht committed Dec 26, 2022
1 parent b5061e5 commit 634e7d3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions osbuilder/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ static DiskLayouts.IDiskScheme CreateDiskScheme(IDisk disk, ProjectConfiguration
scheme = new DiskLayouts.MBR();
else if (config.Scheme.ToLower() == "gpt")
scheme = new DiskLayouts.GPT();
else if (config.Scheme.ToLower() == "singleton")
scheme = new DiskLayouts.Singleton();
else {
throw new Exception($"{nameof(Program)} | {nameof(CreateDiskScheme)} | ERROR: Invalid schema specified in the model");
}
Expand Down
83 changes: 83 additions & 0 deletions osbuilder/disklayouts/Singleton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System;

namespace OSBuilder.DiskLayouts {
public class Singleton : IDiskScheme {
private IDisk _disk = null;
private List<FileSystems.IFileSystem> _fileSystems = new List<FileSystems.IFileSystem>();

private ulong _sectorsAllocated = 0;

public bool Open(IDisk disk)
{
// Store disk
_disk = disk;

// ensure disk is open for read/write
if (!_disk.IsOpen())
return false;

return true;
}

public void Dispose()
{
if (_disk == null)
return;

// dispose of filesystems
foreach (var fs in _fileSystems)
{
fs.Dispose();
}

// cleanup
_fileSystems.Clear();
_disk = null;
}


public bool Create(IDisk disk)
{
_disk = disk;

// ensure disk is open for read/write
if (!_disk.IsOpen())
return false;

return true;
}

public bool AddPartition(FileSystems.IFileSystem fileSystem, ulong sectorCount, string vbrImage, string reservedSectorsImage)
{
ulong partitionSize = GetFreeSectorCount();
if (_disk == null || fileSystem == null)
return false;

if (_fileSystems.Count != 0)
{
return false;
}
_sectorsAllocated += partitionSize;
// Initialize the file-system
fileSystem.Initialize(_disk, 0, partitionSize, vbrImage, reservedSectorsImage);

// Add sectors allocated
_fileSystems.Add(fileSystem);
return fileSystem.Format();
}
public ulong GetFreeSectorCount()
{
if (_disk == null)
return 0;
return _disk.SectorCount - _sectorsAllocated;
}
public IEnumerable<FileSystems.IFileSystem> GetFileSystems()
{
return _fileSystems;
}

}
}

0 comments on commit 634e7d3

Please sign in to comment.