Skip to content

Commit

Permalink
[CBMFS] Implement full support for mounting, reading and extracting C…
Browse files Browse the repository at this point in the history
…ommodore 1540/1541/1571/1581 filesystems.
  • Loading branch information
claunia committed Apr 22, 2024
1 parent 4bec55a commit 99b98ba
Show file tree
Hide file tree
Showing 9 changed files with 750 additions and 3 deletions.
52 changes: 50 additions & 2 deletions Aaru.Filesystems/CBM/CBM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,27 @@
// ****************************************************************************/

using System;
using System.Collections.Generic;
using Aaru.CommonTypes.AaruMetadata;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.CommonTypes.Structs;

namespace Aaru.Filesystems;

/// <inheritdoc />
/// <summary>Implements detection of the filesystem used in 8-bit Commodore microcomputers</summary>
public sealed partial class CBM : IFilesystem
public sealed partial class CBM : IReadOnlyFilesystem
{
#region IFilesystem Members
byte[] _bam;
Dictionary<string, CachedFile> _cache;
bool _debug;
byte[] _diskHeader;
bool _mounted;
byte[] _root;
FileSystemInfo _statfs;

#region IReadOnlyFilesystem Members

/// <inheritdoc />
public string Name => Localization.CBM_Name;
Expand All @@ -46,5 +58,41 @@ public sealed partial class CBM : IFilesystem
/// <inheritdoc />
public string Author => Authors.NataliaPortillo;

/// <inheritdoc />
public ErrorNumber ListXAttr(string path, out List<string> xattrs)
{
xattrs = null;

return ErrorNumber.NotSupported;
}

/// <inheritdoc />
public ErrorNumber GetXattr(string path, string xattr, ref byte[] buf) => ErrorNumber.NotSupported;

/// <inheritdoc />
public ErrorNumber ReadLink(string path, out string dest)
{
dest = null;

return ErrorNumber.NotSupported;
}

/// <inheritdoc />
public FileSystem Metadata { get; private set; }

/// <inheritdoc />
public IEnumerable<(string name, Type type, string description)> SupportedOptions =>
Array.Empty<(string name, Type type, string description)>();

/// <inheritdoc />
public Dictionary<string, string> Namespaces => null;

#endregion

static Dictionary<string, string> GetDefaultOptions() => new()
{
{
"debug", false.ToString()
}
};
}
100 changes: 100 additions & 0 deletions Aaru.Filesystems/CBM/Dir.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : CBM.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Commodore file system plugin.
//
// --[ License ] --------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2023 Natalia Portillo
// ****************************************************************************/

using System;
using System.Linq;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;

namespace Aaru.Filesystems;

/// <inheritdoc />
/// <summary>Implements detection of the filesystem used in 8-bit Commodore microcomputers</summary>
public sealed partial class CBM
{
#region IReadOnlyFilesystem Members

/// <inheritdoc />
public ErrorNumber OpenDir(string path, out IDirNode node)
{
node = null;

if(!_mounted)
return ErrorNumber.AccessDenied;

if(!string.IsNullOrEmpty(path) && string.Compare(path, "/", StringComparison.OrdinalIgnoreCase) != 0)
return ErrorNumber.NotSupported;

var contents = _cache.Keys.ToList();

contents.Sort();

node = new CbmDirNode
{
Path = path, Position = 0, Contents = contents.ToArray()
};

return ErrorNumber.NoError;
}

/// <inheritdoc />
public ErrorNumber CloseDir(IDirNode node)
{
if(node is not CbmDirNode mynode)
return ErrorNumber.InvalidArgument;

mynode.Position = -1;
mynode.Contents = null;

return ErrorNumber.NoError;
}

/// <inheritdoc />
public ErrorNumber ReadDir(IDirNode node, out string filename)
{
filename = null;

if(!_mounted)
return ErrorNumber.AccessDenied;

if(node is not CbmDirNode mynode)
return ErrorNumber.InvalidArgument;

if(mynode.Position < 0)
return ErrorNumber.InvalidArgument;

if(mynode.Position >= mynode.Contents.Length)
return ErrorNumber.NoError;

filename = mynode.Contents[mynode.Position++];

return ErrorNumber.NoError;
}

#endregion
}
178 changes: 178 additions & 0 deletions Aaru.Filesystems/CBM/File.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : CBM.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Commodore file system plugin.
//
// --[ License ] --------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2023 Natalia Portillo
// ****************************************************************************/

using System;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.CommonTypes.Structs;

namespace Aaru.Filesystems;

/// <inheritdoc />
/// <summary>Implements detection of the filesystem used in 8-bit Commodore microcomputers</summary>
public sealed partial class CBM
{
#region IReadOnlyFilesystem Members

/// <inheritdoc />
public ErrorNumber GetAttributes(string path, out FileAttributes attributes)
{
attributes = new FileAttributes();

if(!_mounted)
return ErrorNumber.AccessDenied;

string[] pathElements = path.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);

if(pathElements.Length != 1)
return ErrorNumber.NotSupported;

string filename = pathElements[0].ToUpperInvariant();

if(!_cache.TryGetValue(filename, out CachedFile file))
return ErrorNumber.NoSuchFile;

attributes = file.attributes;

return ErrorNumber.NoError;
}

/// <inheritdoc />
public ErrorNumber Stat(string path, out FileEntryInfo stat)
{
stat = null;

if(!_mounted)
return ErrorNumber.AccessDenied;

string[] pathElements = path.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);

if(pathElements.Length != 1)
return ErrorNumber.NotSupported;

string filename = pathElements[0].ToUpperInvariant();

if(filename.Length > 14)
return ErrorNumber.NameTooLong;

if(!_cache.TryGetValue(filename, out CachedFile file))
return ErrorNumber.NoSuchFile;

stat = new FileEntryInfo
{
Attributes = file.attributes,
BlockSize = 256,
Length = (long)file.length,
Blocks = file.blocks,
Inode = file.id,
Links = 1
};

return ErrorNumber.NoError;
}

/// <inheritdoc />
public ErrorNumber OpenFile(string path, out IFileNode node)
{
node = null;

if(!_mounted)
return ErrorNumber.AccessDenied;

string[] pathElements = path.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);

if(pathElements.Length != 1)
return ErrorNumber.NotSupported;

string filename = pathElements[0].ToUpperInvariant();

if(filename.Length > 14)
return ErrorNumber.NameTooLong;

if(!_cache.TryGetValue(filename, out CachedFile file))
return ErrorNumber.NoSuchFile;

node = new CbmFileNode
{
Path = path, Length = (long)file.length, Offset = 0, Cache = file.data
};

return ErrorNumber.NoError;
}

/// <inheritdoc />
public ErrorNumber CloseFile(IFileNode node)
{
if(!_mounted)
return ErrorNumber.AccessDenied;

if(node is not CbmFileNode mynode)
return ErrorNumber.InvalidArgument;

mynode.Cache = null;

return ErrorNumber.NoError;
}

/// <inheritdoc />
public ErrorNumber ReadFile(IFileNode node, long length, byte[] buffer, out long read)
{
read = 0;

if(!_mounted)
return ErrorNumber.AccessDenied;

if(buffer is null || buffer.Length < length)
return ErrorNumber.InvalidArgument;

if(node is not CbmFileNode mynode)
return ErrorNumber.InvalidArgument;

read = length;

if(length + mynode.Offset >= mynode.Length)
read = mynode.Length - mynode.Offset;

Array.Copy(mynode.Cache, mynode.Offset, buffer, 0, read);

mynode.Offset += read;

return ErrorNumber.NoError;
}

#endregion
}
Loading

0 comments on commit 99b98ba

Please sign in to comment.