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

228 lines
7.0 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.
//
// --[ Description ] ----------------------------------------------------------
//
// Methods to handle files.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
2020-03-11 21:56:55 +00:00
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
namespace Aaru.Filesystems;
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;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Structs;
2019-08-01 16:21:10 +01:00
2022-03-06 13:29:38 +00:00
public sealed partial class OperaFS
2019-08-01 16:21:10 +01:00
{
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber MapBlock(string path, long fileBlock, out long deviceBlock)
2019-08-01 16:21:10 +01:00
{
2022-03-06 13:29:38 +00:00
deviceBlock = 0;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(!_mounted)
return ErrorNumber.AccessDenied;
2019-08-02 00:42:25 +01:00
2022-03-06 13:29:38 +00:00
ErrorNumber err = GetFileEntry(path, out DirectoryEntryWithPointers entry);
2019-08-02 00:42:25 +01:00
2022-03-06 13:29:38 +00:00
if(err != ErrorNumber.NoError)
return err;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if((entry.Entry.flags & FLAGS_MASK) == (uint)FileFlags.Directory &&
!_debug)
return ErrorNumber.IsDirectory;
2019-08-02 00:42:25 +01:00
2022-03-06 13:29:38 +00:00
deviceBlock = entry.Pointers[0] + fileBlock;
2019-08-02 00:42:25 +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 GetAttributes(string path, out FileAttributes attributes)
{
attributes = new FileAttributes();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00: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
2022-03-06 13:29:38 +00: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
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Read(string path, long offset, long size, ref byte[] buf)
{
buf = null;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(!_mounted)
return ErrorNumber.AccessDenied;
2019-08-02 01:41:42 +01:00
2022-03-06 13:29:38 +00:00
ErrorNumber err = GetFileEntry(path, out DirectoryEntryWithPointers entry);
2019-08-02 01:41:42 +01:00
2022-03-06 13:29:38 +00:00
if(err != ErrorNumber.NoError)
return err;
2019-08-02 01:41:42 +01:00
2022-03-06 13:29:38 +00:00
if((entry.Entry.flags & FLAGS_MASK) == (uint)FileFlags.Directory &&
!_debug)
return ErrorNumber.IsDirectory;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(entry.Pointers.Length < 1)
return ErrorNumber.InvalidArgument;
2019-08-02 01:41:42 +01:00
2022-03-06 13:29:38 +00:00
if(entry.Entry.byte_count == 0)
{
buf = Array.Empty<byte>();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2019-08-02 01:41:42 +01:00
2022-03-06 13:29:38 +00:00
if(offset >= entry.Entry.byte_count)
return ErrorNumber.InvalidArgument;
2019-08-02 01:41:42 +01:00
2022-03-06 13:29:38 +00:00
if(size + offset >= entry.Entry.byte_count)
size = entry.Entry.byte_count - offset;
2019-08-02 01:41:42 +01:00
2022-03-06 13:29:38 +00:00
long firstBlock = offset / entry.Entry.block_size;
long offsetInBlock = offset % entry.Entry.block_size;
long sizeInBlocks = (size + offsetInBlock) / entry.Entry.block_size;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if((size + offsetInBlock) % entry.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)
2022-03-06 13:29:38 +00:00
fileBlockSizeRatio = entry.Entry.block_size / 2048;
else
fileBlockSizeRatio = entry.Entry.block_size / _image.Info.SectorSize;
2020-02-29 18:03:35 +00:00
2022-03-07 07:36:44 +00:00
ErrorNumber errno = _image.ReadSectors((ulong)(entry.Pointers[0] + firstBlock * fileBlockSizeRatio),
2022-03-06 13:29:38 +00:00
(uint)(sizeInBlocks * fileBlockSizeRatio), out byte[] buffer);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
2019-08-02 01:41:42 +01:00
2022-03-06 13:29:38 +00:00
buf = new byte[size];
Array.Copy(buffer, offsetInBlock, buf, 0, size);
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
2022-03-06 13:29:38 +00: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
2022-03-06 13:29:38 +00: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
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
2022-03-06 13:29:38 +00:00
string[] pieces = cutPath.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(pieces.Length == 0)
return ErrorNumber.InvalidArgument;
2019-08-02 00:25:36 +01:00
2022-03-07 07:36:44 +00: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 = ReadDir(parentPath, out _);
2019-08-02 00:25:36 +01:00
2022-03-06 13:29:38 +00:00
if(err != ErrorNumber.NoError)
return err;
}
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;
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.ToLower(CultureInfo.CurrentUICulture) == pieces[^1]);
2019-08-02 00:25:36 +01:00
2022-03-06 13:29:38 +00: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
}
}