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
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
deviceBlock = entry.pointers[0] + fileBlock;
|
|
|
|
|
|
|
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
2019-08-01 16:21:10 +01:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2020-02-29 18:03:35 +00: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;
|
|
|
|
|
|
|
|
|
|
if(entry.pointers.Length < 1)
|
|
|
|
|
return Errno.InvalidArgument;
|
2019-08-02 01:41:42 +01:00
|
|
|
|
|
|
|
|
if(entry.entry.byte_count == 0)
|
|
|
|
|
{
|
|
|
|
|
buf = new byte[0];
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2019-08-02 01:41:42 +01:00
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(offset >= entry.entry.byte_count)
|
|
|
|
|
return Errno.InvalidArgument;
|
2019-08-02 01:41:42 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(size + offset >= entry.entry.byte_count)
|
|
|
|
|
size = entry.entry.byte_count - offset;
|
2019-08-02 01:41: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
|
|
|
|
|
|
|
|
if((size + offsetInBlock) % entry.entry.block_size > 0)
|
|
|
|
|
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)
|
2020-02-29 18:03:35 +00:00
|
|
|
fileBlockSizeRatio = entry.entry.block_size / 2048;
|
|
|
|
|
else
|
2020-07-20 21:11:32 +01:00
|
|
|
fileBlockSizeRatio = entry.entry.block_size / _image.Info.SectorSize;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
byte[] buffer = _image.ReadSectors((ulong)(entry.pointers[0] + (firstBlock * fileBlockSizeRatio)),
|
|
|
|
|
(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
|
|
|
|
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
|
|
|
|
|
|
|
|
DirectoryEntry entry = entryWithPointers.entry;
|
|
|
|
|
|
|
|
|
|
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,
|
2019-08-02 00:25:36 +01:00
|
|
|
Links = (ulong)entryWithPointers.pointers.Length
|
|
|
|
|
};
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|