Files
Aaru/Aaru.Filesystems/Opera/File.cs

225 lines
7.1 KiB
C#
Raw Normal View History

2020-03-11 21:56:55 +00:00
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : File.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Opera filesystem 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/>.
//
// ----------------------------------------------------------------------------
2024-05-01 04:17:32 +01:00
// Copyright © 2011-2024 Natalia Portillo
2020-03-11 21:56:55 +00:00
// ****************************************************************************/
2019-08-01 16:21:10 +01:00
using System;
2019-08-02 00:25:36 +01:00
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
2021-09-16 04:42:14 +01:00
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Structs;
2019-08-01 16:21:10 +01:00
namespace Aaru.Filesystems;
2022-03-06 13:29:38 +00:00
public sealed partial class OperaFS
2019-08-01 16:21:10 +01:00
{
#region IReadOnlyFilesystem Members
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber GetAttributes(string path, out FileAttributes attributes)
{
attributes = new FileAttributes();
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
if(!_mounted) return ErrorNumber.AccessDenied;
2019-08-02 00:34:03 +01:00
2022-03-06 13:29:38 +00:00
ErrorNumber err = Stat(path, out FileEntryInfo stat);
2019-08-02 00:34:03 +01:00
2024-05-01 04:05:22 +01:00
if(err != ErrorNumber.NoError) return err;
2019-08-02 00:34:03 +01:00
2022-03-06 13:29:38 +00:00
attributes = stat.Attributes;
2019-08-02 00:34:03 +01:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2019-08-01 16:21:10 +01:00
/// <inheritdoc />
public ErrorNumber OpenFile(string path, out IFileNode node)
{
node = null;
2024-05-01 04:05:22 +01:00
if(!_mounted) return ErrorNumber.AccessDenied;
ErrorNumber err = GetFileEntry(path, out DirectoryEntryWithPointers entry);
2024-05-01 04:05:22 +01:00
if(err != ErrorNumber.NoError) return err;
2024-05-01 04:05:22 +01:00
if((entry.Entry.flags & FLAGS_MASK) == (uint)FileFlags.Directory && !_debug) return ErrorNumber.IsDirectory;
2024-05-01 04:05:22 +01:00
if(entry.Pointers.Length < 1) return ErrorNumber.InvalidArgument;
node = new OperaFileNode
{
2023-10-05 01:52:48 +01:00
Path = path,
Length = entry.Entry.byte_count,
Offset = 0,
Dentry = entry
};
return ErrorNumber.NoError;
}
/// <inheritdoc />
public ErrorNumber CloseFile(IFileNode node)
{
2024-05-01 04:05:22 +01:00
if(!_mounted) return ErrorNumber.AccessDenied;
2024-05-01 04:05:22 +01:00
if(node is not OperaFileNode mynode) return ErrorNumber.InvalidArgument;
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadFile(IFileNode node, long length, byte[] buffer, out long read)
2022-03-06 13:29:38 +00:00
{
read = 0;
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
if(!_mounted) return ErrorNumber.AccessDenied;
2019-08-02 01:41:42 +01:00
2024-05-01 04:05:22 +01:00
if(buffer is null || buffer.Length < length) return ErrorNumber.InvalidArgument;
2019-08-02 01:41:42 +01:00
2024-05-01 04:05:22 +01:00
if(node is not OperaFileNode mynode) return ErrorNumber.InvalidArgument;
2019-08-02 01:41:42 +01:00
read = length;
2019-08-02 01:41:42 +01:00
2024-05-01 04:05:22 +01:00
if(length + mynode.Offset >= mynode.Length) read = mynode.Length - mynode.Offset;
2020-02-29 18:03:35 +00:00
2023-10-05 01:52:48 +01:00
long firstBlock = mynode.Offset / mynode.Dentry.Entry.block_size;
long offsetInBlock = mynode.Offset % mynode.Dentry.Entry.block_size;
long sizeInBlocks = (read + offsetInBlock) / mynode.Dentry.Entry.block_size;
2024-05-01 04:05:22 +01:00
if((read + offsetInBlock) % mynode.Dentry.Entry.block_size > 0) sizeInBlocks++;
2019-08-02 01:41:42 +01:00
2022-03-06 13:29:38 +00:00
uint fileBlockSizeRatio;
2019-08-02 01:41:42 +01:00
2022-03-16 11:47:00 +00:00
if(_image.Info.SectorSize is 2336 or 2352 or 2448)
2023-10-05 01:52:48 +01:00
fileBlockSizeRatio = mynode.Dentry.Entry.block_size / 2048;
2022-03-06 13:29:38 +00:00
else
2023-10-05 01:52:48 +01:00
fileBlockSizeRatio = mynode.Dentry.Entry.block_size / _image.Info.SectorSize;
2020-02-29 18:03:35 +00:00
2023-10-05 01:52:48 +01:00
ErrorNumber errno = _image.ReadSectors((ulong)(mynode.Dentry.Pointers[0] + firstBlock * fileBlockSizeRatio),
2024-05-01 04:05:22 +01:00
(uint)(sizeInBlocks * fileBlockSizeRatio),
out byte[] buf);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
{
read = 0;
2022-03-06 13:29:38 +00:00
return errno;
}
Array.Copy(buf, offsetInBlock, buffer, 0, read);
2019-08-02 01:41:42 +01:00
mynode.Offset += read;
2019-08-02 01:41:42 +01:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2019-08-01 16:21:10 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Stat(string path, out FileEntryInfo stat)
{
stat = null;
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
if(!_mounted) return ErrorNumber.AccessDenied;
2019-08-02 00:25:36 +01:00
2022-03-06 13:29:38 +00:00
ErrorNumber err = GetFileEntry(path, out DirectoryEntryWithPointers entryWithPointers);
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
if(err != ErrorNumber.NoError) return err;
2019-08-02 00:25:36 +01:00
2022-03-06 13:29:38 +00:00
DirectoryEntry entry = entryWithPointers.Entry;
2019-08-02 00:25:36 +01:00
2022-03-06 13:29:38 +00:00
stat = new FileEntryInfo
{
Attributes = new FileAttributes(),
Blocks = entry.block_count,
BlockSize = entry.block_size,
Length = entry.byte_count,
Inode = entry.id,
Links = (ulong)entryWithPointers.Pointers.Length
};
2019-08-02 00:25:36 +01:00
2022-03-06 13:29:38 +00:00
var flags = (FileFlags)(entry.flags & FLAGS_MASK);
2020-02-29 18:03:35 +00:00
2022-11-13 19:38:03 +00:00
switch(flags)
{
case FileFlags.Directory:
stat.Attributes |= FileAttributes.Directory;
break;
case FileFlags.Special:
stat.Attributes |= FileAttributes.Device;
2019-08-02 00:25:36 +01:00
2022-11-13 19:38:03 +00:00
break;
}
2019-08-02 00:25:36 +01:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2019-08-02 00:25:36 +01:00
#endregion
2022-03-06 13:29:38 +00:00
ErrorNumber GetFileEntry(string path, out DirectoryEntryWithPointers entry)
{
entry = null;
2019-08-02 00:25:36 +01:00
2022-03-06 13:29:38 +00:00
string cutPath = path.StartsWith("/", StringComparison.Ordinal)
2022-11-14 01:15:06 +00:00
? path[1..].ToLower(CultureInfo.CurrentUICulture)
2022-03-06 13:29:38 +00:00
: path.ToLower(CultureInfo.CurrentUICulture);
2019-08-02 00:25:36 +01:00
string[] pieces = cutPath.Split(new[]
2024-05-01 04:05:22 +01:00
{
'/'
},
StringSplitOptions.RemoveEmptyEntries);
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
if(pieces.Length == 0) return ErrorNumber.InvalidArgument;
2019-08-02 00:25:36 +01:00
var parentPath = string.Join("/", pieces, 0, pieces.Length - 1);
2019-08-02 00:25:36 +01:00
2022-03-06 13:29:38 +00:00
if(!_directoryCache.TryGetValue(parentPath, out _))
{
ErrorNumber err = OpenDir(parentPath, out IDirNode node);
2019-08-02 00:25:36 +01:00
2024-05-01 04:05:22 +01:00
if(err != ErrorNumber.NoError) return err;
CloseDir(node);
2022-03-06 13:29:38 +00:00
}
2019-08-02 00:25:36 +01:00
2022-03-06 13:29:38 +00:00
Dictionary<string, DirectoryEntryWithPointers> parent;
2019-08-02 00:25:36 +01:00
2022-03-06 13:29:38 +00:00
if(pieces.Length == 1)
parent = _rootDirectoryCache;
2024-05-01 04:05:22 +01:00
else if(!_directoryCache.TryGetValue(parentPath, out parent)) return ErrorNumber.InvalidArgument;
2019-08-02 00:25:36 +01:00
2022-03-06 13:29:38 +00:00
KeyValuePair<string, DirectoryEntryWithPointers> dirent =
parent.FirstOrDefault(t => t.Key.Equals(pieces[^1], StringComparison.CurrentCultureIgnoreCase));
2019-08-02 00:25:36 +01:00
2024-05-01 04:05:22 +01:00
if(string.IsNullOrEmpty(dirent.Key)) return ErrorNumber.NoSuchFile;
2019-08-02 00:25:36 +01:00
2022-03-06 13:29:38 +00:00
entry = dirent.Value;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
2019-08-01 16:21:10 +01:00
}
}