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;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.CommonTypes.Structs;
|
|
|
|
|
using Aaru.Helpers;
|
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
|
|
|
{
|
|
|
|
|
public partial class OperaFS
|
|
|
|
|
{
|
2019-08-02 00:12:33 +01:00
|
|
|
public Errno ReadDir(string path, out List<string> contents)
|
|
|
|
|
{
|
|
|
|
|
contents = null;
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(!mounted)
|
|
|
|
|
return Errno.AccessDenied;
|
|
|
|
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(path) ||
|
|
|
|
|
path == "/")
|
2019-08-02 00:12:33 +01:00
|
|
|
{
|
|
|
|
|
contents = rootDirectoryCache.Keys.ToList();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2019-08-02 00:12:33 +01:00
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string cutPath = path.StartsWith("/", StringComparison.Ordinal)
|
|
|
|
|
? path.Substring(1).ToLower(CultureInfo.CurrentUICulture)
|
|
|
|
|
: path.ToLower(CultureInfo.CurrentUICulture);
|
|
|
|
|
|
|
|
|
|
if(directoryCache.TryGetValue(cutPath, out Dictionary<string, DirectoryEntryWithPointers> currentDirectory))
|
|
|
|
|
{
|
|
|
|
|
contents = currentDirectory.Keys.ToList();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2019-08-02 00:12:33 +01:00
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
string[] pieces = cutPath.Split(new[]
|
|
|
|
|
{
|
|
|
|
|
'/'
|
|
|
|
|
}, StringSplitOptions.RemoveEmptyEntries);
|
2019-08-02 00:12:33 +01:00
|
|
|
|
|
|
|
|
KeyValuePair<string, DirectoryEntryWithPointers> entry =
|
|
|
|
|
rootDirectoryCache.FirstOrDefault(t => t.Key.ToLower(CultureInfo.CurrentUICulture) == pieces[0]);
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(string.IsNullOrEmpty(entry.Key))
|
|
|
|
|
return Errno.NoSuchFile;
|
2019-08-02 00:12:33 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if((entry.Value.entry.flags & FLAGS_MASK) != (int)FileFlags.Directory)
|
|
|
|
|
return Errno.NotDirectory;
|
2019-08-02 00:12:33 +01:00
|
|
|
|
|
|
|
|
string currentPath = pieces[0];
|
|
|
|
|
|
|
|
|
|
currentDirectory = rootDirectoryCache;
|
|
|
|
|
|
|
|
|
|
for(int p = 0; p < pieces.Length; p++)
|
|
|
|
|
{
|
|
|
|
|
entry = currentDirectory.FirstOrDefault(t => t.Key.ToLower(CultureInfo.CurrentUICulture) == pieces[p]);
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(string.IsNullOrEmpty(entry.Key))
|
|
|
|
|
return Errno.NoSuchFile;
|
2019-08-02 00:12:33 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if((entry.Value.entry.flags & FLAGS_MASK) != (int)FileFlags.Directory)
|
|
|
|
|
return Errno.NotDirectory;
|
2019-08-02 00:12:33 +01:00
|
|
|
|
|
|
|
|
currentPath = p == 0 ? pieces[0] : $"{currentPath}/{pieces[p]}";
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(directoryCache.TryGetValue(currentPath, out currentDirectory))
|
|
|
|
|
continue;
|
2019-08-02 00:12:33 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(entry.Value.pointers.Length < 1)
|
|
|
|
|
return Errno.InvalidArgument;
|
2019-08-02 00:12:33 +01:00
|
|
|
|
|
|
|
|
currentDirectory = DecodeDirectory((int)entry.Value.pointers[0]);
|
|
|
|
|
|
|
|
|
|
directoryCache.Add(currentPath, currentDirectory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
contents = currentDirectory?.Keys.ToList();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2019-08-02 00:12:33 +01:00
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
2019-08-01 23:37:21 +01:00
|
|
|
|
|
|
|
|
Dictionary<string, DirectoryEntryWithPointers> DecodeDirectory(int firstBlock)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, DirectoryEntryWithPointers> entries =
|
|
|
|
|
new Dictionary<string, DirectoryEntryWithPointers>();
|
|
|
|
|
|
|
|
|
|
int nextBlock = firstBlock;
|
|
|
|
|
|
2019-08-02 01:42:22 +01:00
|
|
|
DirectoryHeader header;
|
|
|
|
|
|
2019-08-01 23:37:21 +01:00
|
|
|
do
|
|
|
|
|
{
|
2019-08-02 01:42:22 +01:00
|
|
|
byte[] data = image.ReadSectors((ulong)(nextBlock * volumeBlockSizeRatio), volumeBlockSizeRatio);
|
|
|
|
|
header = Marshal.ByteArrayToStructureBigEndian<DirectoryHeader>(data);
|
|
|
|
|
nextBlock = header.next_block + firstBlock;
|
2019-08-01 23:37:21 +01:00
|
|
|
|
|
|
|
|
int off = (int)header.first_used;
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
var entry = new DirectoryEntry();
|
2019-08-01 23:37:21 +01:00
|
|
|
|
|
|
|
|
while(off + DirectoryEntrySize < data.Length)
|
|
|
|
|
{
|
|
|
|
|
entry = Marshal.ByteArrayToStructureBigEndian<DirectoryEntry>(data, off, DirectoryEntrySize);
|
|
|
|
|
string name = StringHandlers.CToString(entry.name, Encoding);
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
var entryWithPointers = new DirectoryEntryWithPointers
|
|
|
|
|
{
|
|
|
|
|
entry = entry, pointers = new uint[entry.last_copy + 1]
|
|
|
|
|
};
|
2019-08-01 23:37:21 +01:00
|
|
|
|
|
|
|
|
for(int i = 0; i <= entry.last_copy; i++)
|
|
|
|
|
entryWithPointers.pointers[i] =
|
2020-02-29 18:03:35 +00:00
|
|
|
BigEndianBitConverter.ToUInt32(data, off + DirectoryEntrySize + (i * 4));
|
2019-08-01 23:37:21 +01:00
|
|
|
|
|
|
|
|
entries.Add(name, entryWithPointers);
|
|
|
|
|
|
|
|
|
|
if((entry.flags & (uint)FileFlags.LastEntry) != 0 ||
|
2020-02-29 18:03:35 +00:00
|
|
|
(entry.flags & (uint)FileFlags.LastEntryInBlock) != 0)
|
|
|
|
|
break;
|
2019-08-01 23:37:21 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
off += (int)(DirectoryEntrySize + ((entry.last_copy + 1) * 4));
|
2019-08-01 23:37:21 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if((entry.flags & (uint)FileFlags.LastEntry) != 0)
|
|
|
|
|
break;
|
|
|
|
|
} while(header.next_block != -1);
|
2019-08-01 23:37:21 +01:00
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
|
}
|
2019-08-01 16:21:10 +01:00
|
|
|
}
|
|
|
|
|
}
|