2020-03-11 21:56:55 +00:00
|
|
|
// /***************************************************************************
|
|
|
|
|
// Aaru Data Preservation Suite
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : Dir.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-12-19 10:45:18 +00:00
|
|
|
// Copyright © 2011-2025 Natalia Portillo
|
2020-03-11 21:56:55 +00:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
2019-08-01 16:21:10 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-08-02 00:12:33 +01:00
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
2021-09-16 04:42:14 +01:00
|
|
|
using Aaru.CommonTypes.Enums;
|
2022-12-21 19:10:51 +00:00
|
|
|
using Aaru.CommonTypes.Interfaces;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.Helpers;
|
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
|
|
|
{
|
2025-10-22 14:28:58 +01:00
|
|
|
Dictionary<string, DirectoryEntryWithPointers> DecodeDirectory(int firstBlock)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, DirectoryEntryWithPointers> entries = new();
|
|
|
|
|
|
|
|
|
|
int nextBlock = firstBlock;
|
|
|
|
|
|
|
|
|
|
DirectoryHeader header;
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
ErrorNumber errno = _image.ReadSectors((ulong)(nextBlock * _volumeBlockSizeRatio),
|
2025-10-23 03:07:43 +01:00
|
|
|
false,
|
2025-10-22 14:28:58 +01:00
|
|
|
_volumeBlockSizeRatio,
|
|
|
|
|
out byte[] data,
|
|
|
|
|
out _);
|
|
|
|
|
|
|
|
|
|
if(errno != ErrorNumber.NoError) break;
|
|
|
|
|
|
|
|
|
|
header = Marshal.ByteArrayToStructureBigEndian<DirectoryHeader>(data);
|
|
|
|
|
nextBlock = header.next_block + firstBlock;
|
|
|
|
|
|
|
|
|
|
var off = (int)header.first_used;
|
|
|
|
|
|
|
|
|
|
var entry = new DirectoryEntry();
|
|
|
|
|
|
|
|
|
|
while(off + _directoryEntrySize < data.Length)
|
|
|
|
|
{
|
|
|
|
|
entry = Marshal.ByteArrayToStructureBigEndian<DirectoryEntry>(data, off, _directoryEntrySize);
|
|
|
|
|
string name = StringHandlers.CToString(entry.name, _encoding);
|
|
|
|
|
|
|
|
|
|
var entryWithPointers = new DirectoryEntryWithPointers
|
|
|
|
|
{
|
|
|
|
|
Entry = entry,
|
|
|
|
|
Pointers = new uint[entry.last_copy + 1]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for(var i = 0; i <= entry.last_copy; i++)
|
|
|
|
|
{
|
|
|
|
|
entryWithPointers.Pointers[i] =
|
|
|
|
|
BigEndianBitConverter.ToUInt32(data, off + _directoryEntrySize + i * 4);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entries.Add(name, entryWithPointers);
|
|
|
|
|
|
|
|
|
|
if((entry.flags & (uint)FileFlags.LastEntry) != 0 ||
|
|
|
|
|
(entry.flags & (uint)FileFlags.LastEntryInBlock) != 0)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
off += (int)(_directoryEntrySize + (entry.last_copy + 1) * 4);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if((entry.flags & (uint)FileFlags.LastEntry) != 0) break;
|
|
|
|
|
} while(header.next_block != -1);
|
|
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-03 23:22:08 +01:00
|
|
|
#region IReadOnlyFilesystem Members
|
|
|
|
|
|
2022-12-21 19:10:51 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ErrorNumber OpenDir(string path, out IDirNode node)
|
|
|
|
|
{
|
|
|
|
|
node = null;
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!_mounted) return ErrorNumber.AccessDenied;
|
2022-12-21 19:10:51 +00:00
|
|
|
|
2023-10-04 17:34:40 +01:00
|
|
|
if(string.IsNullOrWhiteSpace(path) || path == "/")
|
2022-12-21 19:10:51 +00:00
|
|
|
{
|
|
|
|
|
node = new OperaDirNode
|
|
|
|
|
{
|
2023-10-05 01:52:48 +01:00
|
|
|
Path = path,
|
|
|
|
|
Contents = _rootDirectoryCache.Keys.ToArray(),
|
|
|
|
|
Position = 0
|
2022-12-21 19:10:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string cutPath = path.StartsWith("/", StringComparison.Ordinal)
|
|
|
|
|
? path[1..].ToLower(CultureInfo.CurrentUICulture)
|
|
|
|
|
: path.ToLower(CultureInfo.CurrentUICulture);
|
|
|
|
|
|
|
|
|
|
if(_directoryCache.TryGetValue(cutPath, out Dictionary<string, DirectoryEntryWithPointers> currentDirectory))
|
|
|
|
|
{
|
|
|
|
|
node = new OperaDirNode
|
|
|
|
|
{
|
2023-10-05 01:52:48 +01:00
|
|
|
Path = path,
|
|
|
|
|
Contents = currentDirectory.Keys.ToArray(),
|
|
|
|
|
Position = 0
|
2022-12-21 19:10:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-04 17:34:40 +01:00
|
|
|
string[] pieces = cutPath.Split(new[]
|
2024-05-01 04:05:22 +01:00
|
|
|
{
|
|
|
|
|
'/'
|
|
|
|
|
},
|
|
|
|
|
StringSplitOptions.RemoveEmptyEntries);
|
2022-12-21 19:10:51 +00:00
|
|
|
|
|
|
|
|
KeyValuePair<string, DirectoryEntryWithPointers> entry =
|
2023-10-04 08:13:57 +01:00
|
|
|
_rootDirectoryCache.FirstOrDefault(t => t.Key.Equals(pieces[0], StringComparison.CurrentCultureIgnoreCase));
|
2022-12-21 19:10:51 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(string.IsNullOrEmpty(entry.Key)) return ErrorNumber.NoSuchFile;
|
2022-12-21 19:10:51 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if((entry.Value.Entry.flags & FLAGS_MASK) != (int)FileFlags.Directory) return ErrorNumber.NotDirectory;
|
2022-12-21 19:10:51 +00:00
|
|
|
|
|
|
|
|
string currentPath = pieces[0];
|
|
|
|
|
|
|
|
|
|
currentDirectory = _rootDirectoryCache;
|
|
|
|
|
|
2023-10-03 23:22:08 +01:00
|
|
|
for(var p = 0; p < pieces.Length; p++)
|
2022-12-21 19:10:51 +00:00
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
entry = currentDirectory.FirstOrDefault(t => t.Key.Equals(pieces[p],
|
|
|
|
|
StringComparison.CurrentCultureIgnoreCase));
|
2022-12-21 19:10:51 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(string.IsNullOrEmpty(entry.Key)) return ErrorNumber.NoSuchFile;
|
2022-12-21 19:10:51 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if((entry.Value.Entry.flags & FLAGS_MASK) != (int)FileFlags.Directory) return ErrorNumber.NotDirectory;
|
2022-12-21 19:10:51 +00:00
|
|
|
|
|
|
|
|
currentPath = p == 0 ? pieces[0] : $"{currentPath}/{pieces[p]}";
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(_directoryCache.TryGetValue(currentPath, out currentDirectory)) continue;
|
2022-12-21 19:10:51 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(entry.Value.Pointers.Length < 1) return ErrorNumber.InvalidArgument;
|
2022-12-21 19:10:51 +00:00
|
|
|
|
|
|
|
|
currentDirectory = DecodeDirectory((int)entry.Value.Pointers[0]);
|
|
|
|
|
|
|
|
|
|
_directoryCache.Add(currentPath, currentDirectory);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(currentDirectory is null) return ErrorNumber.NoSuchFile;
|
2022-12-21 19:10:51 +00:00
|
|
|
|
|
|
|
|
node = new OperaDirNode
|
|
|
|
|
{
|
2023-10-05 01:52:48 +01:00
|
|
|
Path = path,
|
|
|
|
|
Contents = currentDirectory.Keys.ToArray(),
|
|
|
|
|
Position = 0
|
2022-12-21 19:10:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
2022-12-21 20:03:24 +00:00
|
|
|
public ErrorNumber ReadDir(IDirNode node, out string filename)
|
2019-08-01 16:21:10 +01:00
|
|
|
{
|
2022-12-21 20:03:24 +00:00
|
|
|
filename = null;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!_mounted) return ErrorNumber.AccessDenied;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(node is not OperaDirNode mynode) return ErrorNumber.InvalidArgument;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(mynode.Position < 0) return ErrorNumber.InvalidArgument;
|
2019-08-02 00:12:33 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(mynode.Position >= mynode.Contents.Length) return ErrorNumber.NoError;
|
2019-08-02 00:12:33 +01:00
|
|
|
|
2023-10-05 01:52:48 +01:00
|
|
|
filename = mynode.Contents[mynode.Position++];
|
2019-08-02 00:12:33 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
2019-08-02 00:12:33 +01:00
|
|
|
|
2022-12-21 19:24:06 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ErrorNumber CloseDir(IDirNode node)
|
|
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
if(node is not OperaDirNode mynode) return ErrorNumber.InvalidArgument;
|
2022-12-21 19:24:06 +00:00
|
|
|
|
2023-10-05 01:52:48 +01:00
|
|
|
mynode.Position = -1;
|
|
|
|
|
mynode.Contents = null;
|
2022-12-21 19:24:06 +00:00
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-03 23:22:08 +01:00
|
|
|
#endregion
|
2019-08-01 16:21:10 +01:00
|
|
|
}
|