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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
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;
|
|
|
|
|
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;
|
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
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ErrorNumber ReadDir(string path, out List<string> contents)
|
2019-08-01 16:21:10 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
contents = null;
|
|
|
|
|
|
|
|
|
|
if(!_mounted)
|
|
|
|
|
return ErrorNumber.AccessDenied;
|
|
|
|
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(path) ||
|
|
|
|
|
path == "/")
|
2019-08-02 00:12:33 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
contents = _rootDirectoryCache.Keys.ToList();
|
2019-08-02 00:12:33 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00: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);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
if(_directoryCache.TryGetValue(cutPath, out Dictionary<string, DirectoryEntryWithPointers> currentDirectory))
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
|
|
|
|
contents = currentDirectory.Keys.ToList();
|
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-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
|
|
|
KeyValuePair<string, DirectoryEntryWithPointers> entry =
|
|
|
|
|
_rootDirectoryCache.FirstOrDefault(t => t.Key.ToLower(CultureInfo.CurrentUICulture) == pieces[0]);
|
2019-08-02 00:12:33 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(string.IsNullOrEmpty(entry.Key))
|
|
|
|
|
return ErrorNumber.NoSuchFile;
|
|
|
|
|
|
|
|
|
|
if((entry.Value.Entry.flags & FLAGS_MASK) != (int)FileFlags.Directory)
|
|
|
|
|
return ErrorNumber.NotDirectory;
|
2019-08-02 00:12:33 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
string currentPath = pieces[0];
|
|
|
|
|
|
|
|
|
|
currentDirectory = _rootDirectoryCache;
|
|
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
for(int p = 0; p < pieces.Length; p++)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
|
|
|
|
entry = currentDirectory.FirstOrDefault(t => t.Key.ToLower(CultureInfo.CurrentUICulture) == pieces[p]);
|
2019-08-02 00:12:33 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(string.IsNullOrEmpty(entry.Key))
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.NoSuchFile;
|
2019-08-02 00:12:33 +01:00
|
|
|
|
2021-08-17 16:27:42 +01:00
|
|
|
if((entry.Value.Entry.flags & FLAGS_MASK) != (int)FileFlags.Directory)
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.NotDirectory;
|
2019-08-02 00:12:33 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
currentPath = p == 0 ? pieces[0] : $"{currentPath}/{pieces[p]}";
|
2019-08-02 00:12:33 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(_directoryCache.TryGetValue(currentPath, out currentDirectory))
|
|
|
|
|
continue;
|
2019-08-02 00:12:33 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(entry.Value.Pointers.Length < 1)
|
|
|
|
|
return ErrorNumber.InvalidArgument;
|
2019-08-02 00:12:33 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
currentDirectory = DecodeDirectory((int)entry.Value.Pointers[0]);
|
2019-08-02 00:12:33 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
_directoryCache.Add(currentPath, currentDirectory);
|
|
|
|
|
}
|
2019-08-02 00:12:33 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
contents = currentDirectory?.Keys.ToList();
|
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-03-06 13:29:38 +00:00
|
|
|
Dictionary<string, DirectoryEntryWithPointers> DecodeDirectory(int firstBlock)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, DirectoryEntryWithPointers> entries = new();
|
2019-08-02 00:12:33 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
int nextBlock = firstBlock;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
DirectoryHeader header;
|
2019-08-01 23:37:21 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
do
|
2019-08-01 23:37:21 +01:00
|
|
|
{
|
2022-03-07 07:36:44 +00:00
|
|
|
ErrorNumber errno = _image.ReadSectors((ulong)(nextBlock * _volumeBlockSizeRatio), _volumeBlockSizeRatio,
|
|
|
|
|
out byte[] data);
|
2019-08-01 23:37:21 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
break;
|
2019-08-02 01:42:22 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
header = Marshal.ByteArrayToStructureBigEndian<DirectoryHeader>(data);
|
|
|
|
|
nextBlock = header.next_block + firstBlock;
|
2021-09-19 21:16:47 +01:00
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
int off = (int)header.first_used;
|
2019-08-01 23:37:21 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
var entry = new DirectoryEntry();
|
2019-08-01 23:37:21 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
while(off + _directoryEntrySize < data.Length)
|
|
|
|
|
{
|
|
|
|
|
entry = Marshal.ByteArrayToStructureBigEndian<DirectoryEntry>(data, off, _directoryEntrySize);
|
|
|
|
|
string name = StringHandlers.CToString(entry.name, Encoding);
|
2019-08-01 23:37:21 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
var entryWithPointers = new DirectoryEntryWithPointers
|
2019-08-01 23:37:21 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
Entry = entry,
|
|
|
|
|
Pointers = new uint[entry.last_copy + 1]
|
|
|
|
|
};
|
2019-08-01 23:37:21 +01:00
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
for(int i = 0; i <= entry.last_copy; i++)
|
2022-03-06 13:29:38 +00:00
|
|
|
entryWithPointers.Pointers[i] =
|
2022-11-15 15:58:43 +00:00
|
|
|
BigEndianBitConverter.ToUInt32(data, off + _directoryEntrySize + (i * 4));
|
2019-08-01 23:37:21 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
entries.Add(name, entryWithPointers);
|
2019-08-01 23:37:21 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if((entry.flags & (uint)FileFlags.LastEntry) != 0 ||
|
|
|
|
|
(entry.flags & (uint)FileFlags.LastEntryInBlock) != 0)
|
|
|
|
|
break;
|
2019-08-01 23:37:21 +01:00
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
off += (int)(_directoryEntrySize + ((entry.last_copy + 1) * 4));
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2019-08-01 23:37:21 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if((entry.flags & (uint)FileFlags.LastEntry) != 0)
|
|
|
|
|
break;
|
|
|
|
|
} while(header.next_block != -1);
|
2019-08-01 23:37:21 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return entries;
|
2019-08-01 16:21:10 +01:00
|
|
|
}
|
|
|
|
|
}
|