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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2022-12-03 16:07:10 +00:00
|
|
|
// Copyright © 2011-2023 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;
|
2022-12-19 00:26:55 +00:00
|
|
|
using Aaru.CommonTypes.Interfaces;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.CommonTypes.Structs;
|
2019-08-01 16:21:10 +01:00
|
|
|
|
2022-11-15 15:58:43 +00: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
|
|
|
{
|
2023-10-03 23:22:08 +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
|
|
|
|
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-12-19 00:26:55 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ErrorNumber OpenFile(string path, out IFileNode node)
|
|
|
|
|
{
|
|
|
|
|
node = null;
|
|
|
|
|
|
|
|
|
|
if(!_mounted)
|
|
|
|
|
return ErrorNumber.AccessDenied;
|
|
|
|
|
|
|
|
|
|
ErrorNumber err = GetFileEntry(path, out DirectoryEntryWithPointers entry);
|
|
|
|
|
|
|
|
|
|
if(err != ErrorNumber.NoError)
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
if((entry.Entry.flags & FLAGS_MASK) == (uint)FileFlags.Directory &&
|
|
|
|
|
!_debug)
|
|
|
|
|
return ErrorNumber.IsDirectory;
|
|
|
|
|
|
|
|
|
|
if(entry.Pointers.Length < 1)
|
|
|
|
|
return ErrorNumber.InvalidArgument;
|
|
|
|
|
|
|
|
|
|
node = new OperaFileNode
|
|
|
|
|
{
|
|
|
|
|
Path = path,
|
|
|
|
|
Length = entry.Entry.byte_count,
|
|
|
|
|
Offset = 0,
|
|
|
|
|
_dentry = entry
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ErrorNumber CloseFile(IFileNode node)
|
|
|
|
|
{
|
|
|
|
|
if(!_mounted)
|
|
|
|
|
return ErrorNumber.AccessDenied;
|
|
|
|
|
|
|
|
|
|
if(node is not OperaFileNode mynode)
|
|
|
|
|
return ErrorNumber.InvalidArgument;
|
|
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
2022-12-19 11:03:51 +00:00
|
|
|
public ErrorNumber ReadFile(IFileNode node, long length, byte[] buffer, out long read)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2022-12-19 11:03:51 +00:00
|
|
|
read = 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 01:41:42 +01:00
|
|
|
|
2022-12-19 11:03:51 +00:00
|
|
|
if(buffer is null ||
|
|
|
|
|
buffer.Length < length)
|
2022-03-06 13:29:38 +00:00
|
|
|
return ErrorNumber.InvalidArgument;
|
2019-08-02 01:41:42 +01:00
|
|
|
|
2022-12-19 11:03:51 +00:00
|
|
|
if(node is not OperaFileNode mynode)
|
2022-03-06 13:29:38 +00:00
|
|
|
return ErrorNumber.InvalidArgument;
|
2019-08-02 01:41:42 +01:00
|
|
|
|
2022-12-19 11:03:51 +00:00
|
|
|
read = length;
|
2019-08-02 01:41:42 +01:00
|
|
|
|
2022-12-19 11:03:51 +00:00
|
|
|
if(length + mynode.Offset >= mynode.Length)
|
|
|
|
|
read = mynode.Length - mynode.Offset;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-12-19 11:03:51 +00: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;
|
|
|
|
|
|
|
|
|
|
if((read + offsetInBlock) % mynode._dentry.Entry.block_size > 0)
|
2022-03-06 13:29:38 +00:00
|
|
|
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-12-19 11:03:51 +00:00
|
|
|
fileBlockSizeRatio = mynode._dentry.Entry.block_size / 2048;
|
2022-03-06 13:29:38 +00:00
|
|
|
else
|
2022-12-19 11:03:51 +00:00
|
|
|
fileBlockSizeRatio = mynode._dentry.Entry.block_size / _image.Info.SectorSize;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2023-10-03 23:22:08 +01:00
|
|
|
ErrorNumber errno = _image.ReadSectors((ulong)(mynode._dentry.Pointers[0] + firstBlock * fileBlockSizeRatio),
|
2022-12-19 11:03:51 +00:00
|
|
|
(uint)(sizeInBlocks * fileBlockSizeRatio), out byte[] buf);
|
2021-09-19 21:16:47 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(errno != ErrorNumber.NoError)
|
2022-12-19 11:03:51 +00:00
|
|
|
{
|
|
|
|
|
read = 0;
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return errno;
|
2022-12-19 11:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Array.Copy(buf, offsetInBlock, buffer, 0, read);
|
2019-08-02 01:41:42 +01:00
|
|
|
|
2022-12-19 11:03:51 +00: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
|
|
|
|
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
|
|
|
|
2023-10-03 23:22:08 +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
|
|
|
|
2023-10-03 23:22:08 +01: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
|
|
|
|
2023-10-03 23:22:08 +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 _))
|
|
|
|
|
{
|
2022-12-21 20:03:24 +00:00
|
|
|
ErrorNumber err = OpenDir(parentPath, out IDirNode node);
|
2019-08-02 00:25:36 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(err != ErrorNumber.NoError)
|
|
|
|
|
return err;
|
2022-12-21 20:03:24 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|