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

221 lines
7.2 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/>.
//
// ----------------------------------------------------------------------------
2020-12-31 23:08:23 +00:00
// Copyright © 2011-2021 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;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Structs;
2019-08-01 16:21:10 +01:00
2020-02-27 00:33:26 +00:00
namespace Aaru.Filesystems
2019-08-01 16:21:10 +01:00
{
2020-07-22 13:20:25 +01:00
public sealed partial class OperaFS
2019-08-01 16:21:10 +01:00
{
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2019-08-02 00:42:25 +01:00
public Errno MapBlock(string path, long fileBlock, out long deviceBlock)
{
deviceBlock = 0;
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(!_mounted)
2020-02-29 18:03:35 +00:00
return Errno.AccessDenied;
2019-08-02 00:42:25 +01:00
Errno err = GetFileEntry(path, out DirectoryEntryWithPointers entry);
2020-02-29 18:03:35 +00:00
if(err != Errno.NoError)
return err;
2021-08-17 16:27:42 +01:00
if((entry.Entry.flags & FLAGS_MASK) == (uint)FileFlags.Directory &&
2020-07-20 21:11:32 +01:00
!_debug)
2020-02-29 18:03:35 +00:00
return Errno.IsDirectory;
2019-08-02 00:42:25 +01:00
2021-08-17 16:27:42 +01:00
deviceBlock = entry.Pointers[0] + fileBlock;
2019-08-02 00:42:25 +01:00
return Errno.NoError;
}
2019-08-01 16:21:10 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2019-08-02 00:34:03 +01:00
public Errno GetAttributes(string path, out FileAttributes attributes)
{
attributes = new FileAttributes();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(!_mounted)
2020-02-29 18:03:35 +00:00
return Errno.AccessDenied;
2019-08-02 00:34:03 +01:00
Errno err = Stat(path, out FileEntryInfo stat);
2020-02-29 18:03:35 +00:00
if(err != Errno.NoError)
return err;
2019-08-02 00:34:03 +01:00
attributes = stat.Attributes;
return Errno.NoError;
}
2019-08-01 16:21:10 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2019-08-02 01:41:42 +01:00
public Errno Read(string path, long offset, long size, ref byte[] buf)
{
buf = null;
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(!_mounted)
2020-02-29 18:03:35 +00:00
return Errno.AccessDenied;
2019-08-02 01:41:42 +01:00
Errno err = GetFileEntry(path, out DirectoryEntryWithPointers entry);
2020-02-29 18:03:35 +00:00
if(err != Errno.NoError)
return err;
2019-08-02 01:41:42 +01:00
2021-08-17 16:27:42 +01:00
if((entry.Entry.flags & FLAGS_MASK) == (uint)FileFlags.Directory &&
2020-07-20 21:11:32 +01:00
!_debug)
2020-02-29 18:03:35 +00:00
return Errno.IsDirectory;
2021-08-17 16:27:42 +01:00
if(entry.Pointers.Length < 1)
2020-02-29 18:03:35 +00:00
return Errno.InvalidArgument;
2019-08-02 01:41:42 +01:00
2021-08-17 16:27:42 +01:00
if(entry.Entry.byte_count == 0)
2019-08-02 01:41:42 +01:00
{
2021-08-17 18:21:12 +01:00
buf = Array.Empty<byte>();
2020-02-29 18:03:35 +00:00
2019-08-02 01:41:42 +01:00
return Errno.NoError;
}
2021-08-17 16:27:42 +01:00
if(offset >= entry.Entry.byte_count)
2020-02-29 18:03:35 +00:00
return Errno.InvalidArgument;
2019-08-02 01:41:42 +01:00
2021-08-17 16:27:42 +01:00
if(size + offset >= entry.Entry.byte_count)
size = entry.Entry.byte_count - offset;
2019-08-02 01:41:42 +01:00
2021-08-17 16:27:42 +01: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
2021-08-17 16:27:42 +01:00
if((size + offsetInBlock) % entry.Entry.block_size > 0)
2020-02-29 18:03:35 +00:00
sizeInBlocks++;
2019-08-02 01:41:42 +01:00
uint fileBlockSizeRatio;
2020-07-20 21:11:32 +01:00
if(_image.Info.SectorSize == 2336 ||
_image.Info.SectorSize == 2352 ||
_image.Info.SectorSize == 2448)
2021-08-17 16:27:42 +01:00
fileBlockSizeRatio = entry.Entry.block_size / 2048;
2020-02-29 18:03:35 +00:00
else
2021-08-17 16:27:42 +01:00
fileBlockSizeRatio = entry.Entry.block_size / _image.Info.SectorSize;
2020-02-29 18:03:35 +00:00
2021-08-17 16:27:42 +01:00
byte[] buffer = _image.ReadSectors((ulong)(entry.Pointers[0] + (firstBlock * fileBlockSizeRatio)),
2020-07-20 21:11:32 +01:00
(uint)(sizeInBlocks * fileBlockSizeRatio));
2019-08-02 01:41:42 +01:00
buf = new byte[size];
Array.Copy(buffer, offsetInBlock, buf, 0, size);
return Errno.NoError;
}
2019-08-01 16:21:10 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2019-08-02 00:25:36 +01:00
public Errno Stat(string path, out FileEntryInfo stat)
{
stat = null;
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(!_mounted)
2020-02-29 18:03:35 +00:00
return Errno.AccessDenied;
2019-08-02 00:25:36 +01:00
Errno err = GetFileEntry(path, out DirectoryEntryWithPointers entryWithPointers);
2020-02-29 18:03:35 +00:00
if(err != Errno.NoError)
return err;
2019-08-02 00:25:36 +01:00
2021-08-17 16:27:42 +01:00
DirectoryEntry entry = entryWithPointers.Entry;
2019-08-02 00:25:36 +01:00
stat = new FileEntryInfo
{
2020-07-20 04:34:16 +01:00
Attributes = new FileAttributes(),
Blocks = entry.block_count,
BlockSize = entry.block_size,
Length = entry.byte_count,
Inode = entry.id,
2021-08-17 16:27:42 +01:00
Links = (ulong)entryWithPointers.Pointers.Length
2019-08-02 00:25:36 +01:00
};
2020-02-29 18:03:35 +00:00
var flags = (FileFlags)(entry.flags & FLAGS_MASK);
if(flags == FileFlags.Directory)
stat.Attributes |= FileAttributes.Directory;
2019-08-02 00:25:36 +01:00
2020-02-29 18:03:35 +00:00
if(flags == FileFlags.Special)
stat.Attributes |= FileAttributes.Device;
2019-08-02 00:25:36 +01:00
return Errno.NoError;
}
Errno GetFileEntry(string path, out DirectoryEntryWithPointers entry)
{
entry = null;
string cutPath = path.StartsWith("/", StringComparison.Ordinal)
? path.Substring(1).ToLower(CultureInfo.CurrentUICulture)
: path.ToLower(CultureInfo.CurrentUICulture);
2020-02-29 18:03:35 +00:00
string[] pieces = cutPath.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);
if(pieces.Length == 0)
return Errno.InvalidArgument;
2019-08-02 00:25:36 +01:00
string parentPath = string.Join("/", pieces, 0, pieces.Length - 1);
2020-07-20 21:11:32 +01:00
if(!_directoryCache.TryGetValue(parentPath, out _))
2019-08-02 00:25:36 +01:00
{
Errno err = ReadDir(parentPath, out _);
2020-02-29 18:03:35 +00:00
if(err != Errno.NoError)
return err;
2019-08-02 00:25:36 +01:00
}
Dictionary<string, DirectoryEntryWithPointers> parent;
2020-02-29 18:03:35 +00:00
if(pieces.Length == 1)
2020-07-20 21:11:32 +01:00
parent = _rootDirectoryCache;
else if(!_directoryCache.TryGetValue(parentPath, out parent))
2020-02-29 18:03:35 +00:00
return Errno.InvalidArgument;
2019-08-02 00:25:36 +01:00
KeyValuePair<string, DirectoryEntryWithPointers> dirent =
2020-04-22 00:22:34 +01:00
parent.FirstOrDefault(t => t.Key.ToLower(CultureInfo.CurrentUICulture) == pieces[^1]);
2019-08-02 00:25:36 +01:00
2020-02-29 18:03:35 +00:00
if(string.IsNullOrEmpty(dirent.Key))
return Errno.NoSuchFile;
2019-08-02 00:25:36 +01:00
entry = dirent.Value;
2020-02-29 18:03:35 +00:00
2019-08-02 00:25:36 +01:00
return Errno.NoError;
}
2019-08-01 16:21:10 +01:00
}
}