2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2016-07-21 17:36:51 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : File.cs
|
2016-07-28 18:13:49 +01:00
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
2016-07-21 17:36:51 +01:00
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// Component : Apple Lisa filesystem plugin.
|
2016-07-21 17:36:51 +01:00
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// Methods to handle files.
|
2016-07-21 17:36:51 +01:00
|
|
|
//
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// 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
|
2016-07-21 17:36:51 +01:00
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// 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.
|
2016-07-21 17:36:51 +01:00
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// 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/>.
|
2016-07-21 17:36:51 +01:00
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-12-31 23:08:23 +00:00
|
|
|
// Copyright © 2011-2021 Natalia Portillo
|
2016-07-21 17:36:51 +01:00
|
|
|
// ****************************************************************************/
|
2016-07-28 18:13:49 +01:00
|
|
|
|
2016-07-21 17:36:51 +01:00
|
|
|
using System;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.CommonTypes.Enums;
|
|
|
|
|
using Aaru.CommonTypes.Structs;
|
|
|
|
|
using Aaru.Console;
|
|
|
|
|
using Aaru.Decoders;
|
2020-07-20 15:43:52 +01:00
|
|
|
using Aaru.Helpers;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2020-02-27 00:33:26 +00:00
|
|
|
namespace Aaru.Filesystems.LisaFS
|
2016-07-21 17:36:51 +01:00
|
|
|
{
|
2020-07-22 13:20:25 +01:00
|
|
|
public sealed partial class LisaFS
|
2016-07-21 17:36:51 +01:00
|
|
|
{
|
2021-08-17 13:56:05 +01:00
|
|
|
/// <inheritdoc />
|
2017-12-26 08:17:28 +00:00
|
|
|
public Errno GetAttributes(string path, out FileAttributes attributes)
|
2016-07-21 17:36:51 +01:00
|
|
|
{
|
2017-12-26 08:17:28 +00:00
|
|
|
attributes = new FileAttributes();
|
|
|
|
|
|
2017-12-22 08:43:22 +00:00
|
|
|
Errno error = LookupFileId(path, out short fileId, out bool isDir);
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(error != Errno.NoError)
|
|
|
|
|
return error;
|
|
|
|
|
|
|
|
|
|
if(!isDir)
|
|
|
|
|
return GetAttributes(fileId, out attributes);
|
2016-07-31 01:53:47 +01:00
|
|
|
|
|
|
|
|
attributes = FileAttributes.Directory;
|
|
|
|
|
|
|
|
|
|
return Errno.NoError;
|
2016-07-21 17:36:51 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
/// <inheritdoc />
|
2017-12-26 07:28:40 +00:00
|
|
|
public Errno Read(string path, long offset, long size, ref byte[] buf)
|
2016-07-21 17:36:51 +01:00
|
|
|
{
|
2016-07-27 03:22:02 +01:00
|
|
|
if(size == 0)
|
|
|
|
|
{
|
|
|
|
|
buf = new byte[0];
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-07-27 03:22:02 +01:00
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(offset < 0)
|
|
|
|
|
return Errno.InvalidArgument;
|
2016-07-28 22:25:26 +01:00
|
|
|
|
2017-12-22 08:43:22 +00:00
|
|
|
Errno error = LookupFileId(path, out short fileId, out _);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
if(error != Errno.NoError)
|
|
|
|
|
return error;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
|
|
|
|
byte[] tmp;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(_debug)
|
2016-07-22 02:18:53 +01:00
|
|
|
switch(fileId)
|
|
|
|
|
{
|
|
|
|
|
case FILEID_BOOT_SIGNED:
|
|
|
|
|
case FILEID_LOADER_SIGNED:
|
|
|
|
|
case (short)FILEID_MDDF:
|
|
|
|
|
case (short)FILEID_BITMAP:
|
|
|
|
|
case (short)FILEID_SRECORD:
|
2016-07-31 01:53:47 +01:00
|
|
|
case (short)FILEID_CATALOG:
|
2016-07-22 02:18:53 +01:00
|
|
|
error = ReadSystemFile(fileId, out tmp);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-07-22 02:18:53 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
error = ReadFile(fileId, out tmp);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-07-22 02:18:53 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
else
|
|
|
|
|
error = ReadFile(fileId, out tmp);
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(error != Errno.NoError)
|
|
|
|
|
return error;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(offset >= tmp.Length)
|
|
|
|
|
return Errno.EINVAL;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(size + offset >= tmp.Length)
|
|
|
|
|
size = tmp.Length - offset;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
|
|
|
|
buf = new byte[size];
|
|
|
|
|
Array.Copy(tmp, offset, buf, 0, size);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-07-22 02:18:53 +01:00
|
|
|
return Errno.NoError;
|
2016-07-21 17:36:51 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
/// <inheritdoc />
|
2017-12-26 08:17:28 +00:00
|
|
|
public Errno Stat(string path, out FileEntryInfo stat)
|
2016-07-21 17:36:51 +01:00
|
|
|
{
|
2017-12-26 08:17:28 +00:00
|
|
|
stat = null;
|
2017-12-22 08:43:22 +00:00
|
|
|
Errno error = LookupFileId(path, out short fileId, out bool isDir);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
if(error != Errno.NoError)
|
|
|
|
|
return error;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2016-07-31 01:53:47 +01:00
|
|
|
return isDir ? StatDir(fileId, out stat) : Stat(fileId, out stat);
|
2016-07-22 02:18:53 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 08:17:28 +00:00
|
|
|
Errno GetAttributes(short fileId, out FileAttributes attributes)
|
2016-07-22 02:18:53 +01:00
|
|
|
{
|
2017-12-26 08:17:28 +00:00
|
|
|
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;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2016-07-31 01:53:47 +01:00
|
|
|
if(fileId < 4)
|
2016-07-22 02:18:53 +01:00
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
if(!_debug)
|
2020-02-29 18:03:35 +00:00
|
|
|
return Errno.NoSuchFile;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
attributes = new FileAttributes();
|
|
|
|
|
attributes = FileAttributes.System;
|
2016-07-31 01:53:47 +01:00
|
|
|
attributes |= FileAttributes.Hidden;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2016-07-31 01:53:47 +01:00
|
|
|
attributes |= FileAttributes.File;
|
|
|
|
|
|
|
|
|
|
return Errno.NoError;
|
2016-07-22 02:18:53 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-22 08:43:22 +00:00
|
|
|
Errno error = ReadExtentsFile(fileId, out ExtentFile extFile);
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(error != Errno.NoError)
|
|
|
|
|
return error;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2016-07-27 02:35:29 +01:00
|
|
|
switch(extFile.ftype)
|
|
|
|
|
{
|
|
|
|
|
case FileType.Spool:
|
|
|
|
|
attributes |= FileAttributes.CharDevice;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-07-27 02:35:29 +01:00
|
|
|
break;
|
|
|
|
|
case FileType.UserCat:
|
|
|
|
|
case FileType.RootCat:
|
|
|
|
|
attributes |= FileAttributes.Directory;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-07-27 02:35:29 +01:00
|
|
|
break;
|
|
|
|
|
case FileType.Pipe:
|
|
|
|
|
attributes |= FileAttributes.Pipe;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-07-27 02:35:29 +01:00
|
|
|
break;
|
2017-12-19 20:33:03 +00:00
|
|
|
case FileType.Undefined: break;
|
2016-07-27 02:35:29 +01:00
|
|
|
default:
|
|
|
|
|
attributes |= FileAttributes.File;
|
|
|
|
|
attributes |= FileAttributes.Extents;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-07-27 02:35:29 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(extFile.protect > 0)
|
|
|
|
|
attributes |= FileAttributes.Immutable;
|
|
|
|
|
|
|
|
|
|
if(extFile.locked > 0)
|
|
|
|
|
attributes |= FileAttributes.ReadOnly;
|
|
|
|
|
|
|
|
|
|
if(extFile.password_valid > 0)
|
|
|
|
|
attributes |= FileAttributes.Password;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
|
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 13:17:27 +00:00
|
|
|
Errno ReadSystemFile(short fileId, out byte[] buf) => ReadSystemFile(fileId, out buf, false);
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2016-07-28 22:25:26 +01:00
|
|
|
Errno ReadSystemFile(short fileId, out byte[] buf, bool tags)
|
2016-07-22 02:18:53 +01:00
|
|
|
{
|
|
|
|
|
buf = null;
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(!_mounted ||
|
|
|
|
|
!_debug)
|
2020-02-29 18:03:35 +00:00
|
|
|
return Errno.AccessDenied;
|
|
|
|
|
|
|
|
|
|
if(fileId > 4 ||
|
|
|
|
|
fileId <= 0)
|
|
|
|
|
if(fileId != FILEID_BOOT_SIGNED &&
|
|
|
|
|
fileId != FILEID_LOADER_SIGNED)
|
2018-06-22 08:08:38 +01:00
|
|
|
return Errno.InvalidArgument;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(_systemFileCache.TryGetValue(fileId, out buf) &&
|
2020-02-29 18:03:35 +00:00
|
|
|
!tags)
|
|
|
|
|
return Errno.NoError;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
2016-07-28 03:41:57 +01:00
|
|
|
if(fileId == FILEID_SRECORD)
|
|
|
|
|
if(!tags)
|
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
buf = _device.ReadSectors(_mddf.mddf_block + _volumePrefix + _mddf.srec_ptr, _mddf.srec_len);
|
|
|
|
|
_systemFileCache.Add(fileId, buf);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-07-28 03:41:57 +01:00
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
buf = _device.ReadSectorsTag(_mddf.mddf_block + _volumePrefix + _mddf.srec_ptr, _mddf.srec_len,
|
|
|
|
|
SectorTagType.AppleSectorTag);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-07-28 03:41:57 +01:00
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-21 19:23:58 +01:00
|
|
|
LisaTag.PriamTag sysTag;
|
2016-07-27 22:13:47 +01:00
|
|
|
|
2016-07-22 02:18:53 +01:00
|
|
|
// Should be enough to check 100 sectors?
|
|
|
|
|
for(ulong i = 0; i < 100; i++)
|
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
DecodeTag(_device.ReadSectorTag(i, SectorTagType.AppleSectorTag), out sysTag);
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(sysTag.FileId == fileId)
|
|
|
|
|
count++;
|
2016-07-22 02:18:53 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(count == 0)
|
|
|
|
|
return Errno.NoSuchFile;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
buf = !tags ? new byte[count * _device.Info.SectorSize] : new byte[count * _devTagSize];
|
2016-07-22 02:18:53 +01:00
|
|
|
|
|
|
|
|
// Should be enough to check 100 sectors?
|
|
|
|
|
for(ulong i = 0; i < 100; i++)
|
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
DecodeTag(_device.ReadSectorTag(i, SectorTagType.AppleSectorTag), out sysTag);
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(sysTag.FileId != fileId)
|
|
|
|
|
continue;
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
byte[] sector = !tags ? _device.ReadSector(i) : _device.ReadSectorTag(i, SectorTagType.AppleSectorTag);
|
2016-07-27 22:13:47 +01:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
// Relative block for $Loader starts at $Boot block
|
2020-02-29 18:03:35 +00:00
|
|
|
if(sysTag.FileId == FILEID_LOADER_SIGNED)
|
|
|
|
|
sysTag.RelPage--;
|
2016-07-27 22:13:47 +01:00
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
Array.Copy(sector, 0, buf, sector.Length * sysTag.RelPage, sector.Length);
|
2016-07-22 02:18:53 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(!tags)
|
2020-07-20 21:11:32 +01:00
|
|
|
_systemFileCache.Add(fileId, buf);
|
2016-07-28 23:08:22 +01:00
|
|
|
|
2016-07-22 02:18:53 +01:00
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-28 22:25:26 +01:00
|
|
|
Errno Stat(short fileId, out FileEntryInfo stat)
|
2016-07-22 02:18:53 +01:00
|
|
|
{
|
|
|
|
|
stat = null;
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(!_mounted)
|
2020-02-29 18:03:35 +00:00
|
|
|
return Errno.AccessDenied;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
Errno error;
|
2016-07-22 02:18:53 +01:00
|
|
|
ExtentFile file;
|
|
|
|
|
|
|
|
|
|
if(fileId <= 4)
|
2020-07-20 21:11:32 +01:00
|
|
|
if(!_debug ||
|
2020-02-29 18:03:35 +00:00
|
|
|
fileId == 0)
|
|
|
|
|
return Errno.NoSuchFile;
|
2016-07-22 02:18:53 +01:00
|
|
|
else
|
|
|
|
|
{
|
2021-05-31 19:14:25 +01:00
|
|
|
stat = new FileEntryInfo();
|
|
|
|
|
|
|
|
|
|
error = GetAttributes(fileId, out FileAttributes attrs);
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2021-05-31 19:14:25 +01:00
|
|
|
stat.Attributes = attrs;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(error != Errno.NoError)
|
|
|
|
|
return error;
|
|
|
|
|
|
|
|
|
|
if(fileId < 0 &&
|
|
|
|
|
fileId != FILEID_BOOT_SIGNED &&
|
|
|
|
|
fileId != FILEID_LOADER_SIGNED)
|
2016-07-22 02:18:53 +01:00
|
|
|
{
|
|
|
|
|
error = ReadExtentsFile((short)(fileId * -1), out file);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
if(error != Errno.NoError)
|
|
|
|
|
return error;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
stat.CreationTime = DateHandlers.LisaToDateTime(file.dtc);
|
|
|
|
|
stat.AccessTime = DateHandlers.LisaToDateTime(file.dta);
|
|
|
|
|
stat.BackupTime = DateHandlers.LisaToDateTime(file.dtb);
|
2016-07-22 02:18:53 +01:00
|
|
|
stat.LastWriteTime = DateHandlers.LisaToDateTime(file.dtm);
|
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
stat.Inode = (ulong)fileId;
|
|
|
|
|
stat.Links = 0;
|
2020-07-20 21:11:32 +01:00
|
|
|
stat.Length = _mddf.datasize;
|
|
|
|
|
stat.BlockSize = _mddf.datasize;
|
2018-06-22 08:08:38 +01:00
|
|
|
stat.Blocks = 1;
|
2016-07-22 02:18:53 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-12-22 08:43:22 +00:00
|
|
|
error = ReadSystemFile(fileId, out byte[] buf);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
if(error != Errno.NoError)
|
|
|
|
|
return error;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
stat.CreationTime = fileId != 4 ? _mddf.dtvc : _mddf.dtcc;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
stat.BackupTime = _mddf.dtvb;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
stat.Inode = (ulong)fileId;
|
|
|
|
|
stat.Links = 0;
|
|
|
|
|
stat.Length = buf.Length;
|
2020-07-20 21:11:32 +01:00
|
|
|
stat.BlockSize = _mddf.datasize;
|
|
|
|
|
stat.Blocks = buf.Length / _mddf.datasize;
|
2016-07-22 02:18:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 19:14:25 +01:00
|
|
|
stat = new FileEntryInfo();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2021-05-31 19:14:25 +01:00
|
|
|
error = GetAttributes(fileId, out FileAttributes fileAttributes);
|
|
|
|
|
stat.Attributes = fileAttributes;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
if(error != Errno.NoError)
|
|
|
|
|
return error;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
|
|
|
|
error = ReadExtentsFile(fileId, out file);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
if(error != Errno.NoError)
|
|
|
|
|
return error;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
stat.CreationTime = DateHandlers.LisaToDateTime(file.dtc);
|
|
|
|
|
stat.AccessTime = DateHandlers.LisaToDateTime(file.dta);
|
|
|
|
|
stat.BackupTime = DateHandlers.LisaToDateTime(file.dtb);
|
2016-07-22 02:18:53 +01:00
|
|
|
stat.LastWriteTime = DateHandlers.LisaToDateTime(file.dtm);
|
|
|
|
|
|
2019-04-22 23:06:47 +01:00
|
|
|
stat.Inode = (ulong)fileId;
|
|
|
|
|
stat.Links = 1;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(!_fileSizeCache.TryGetValue(fileId, out int len))
|
|
|
|
|
stat.Length = _srecords[fileId].filesize;
|
2020-02-29 18:03:35 +00:00
|
|
|
else
|
|
|
|
|
stat.Length = len;
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
stat.BlockSize = _mddf.datasize;
|
2018-06-22 08:08:38 +01:00
|
|
|
stat.Blocks = file.length;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
|
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 13:17:27 +00:00
|
|
|
Errno ReadFile(short fileId, out byte[] buf) => ReadFile(fileId, out buf, false);
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2016-07-28 22:25:26 +01:00
|
|
|
Errno ReadFile(short fileId, out byte[] buf, bool tags)
|
2016-07-22 02:18:53 +01:00
|
|
|
{
|
|
|
|
|
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;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
tags &= _debug;
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(fileId < 4 ||
|
2020-07-20 21:11:32 +01:00
|
|
|
(fileId == 4 && _mddf.fsversion != LISA_V2 && _mddf.fsversion != LISA_V1))
|
2016-07-22 02:18:53 +01:00
|
|
|
return Errno.InvalidArgument;
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(!tags &&
|
2020-07-20 21:11:32 +01:00
|
|
|
_fileCache.TryGetValue(fileId, out buf))
|
2020-02-29 18:03:35 +00:00
|
|
|
return Errno.NoError;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2017-12-22 08:43:22 +00:00
|
|
|
Errno error = ReadExtentsFile(fileId, out ExtentFile file);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
if(error != Errno.NoError)
|
|
|
|
|
return error;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2016-07-27 02:35:29 +01:00
|
|
|
int sectorSize;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
if(tags)
|
2020-07-20 21:11:32 +01:00
|
|
|
sectorSize = _devTagSize;
|
2020-02-29 18:03:35 +00:00
|
|
|
else
|
2020-07-20 21:11:32 +01:00
|
|
|
sectorSize = (int)_device.Info.SectorSize;
|
2016-07-27 02:35:29 +01:00
|
|
|
|
|
|
|
|
byte[] temp = new byte[file.length * sectorSize];
|
2016-07-22 02:18:53 +01:00
|
|
|
|
|
|
|
|
int offset = 0;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-07-22 02:18:53 +01:00
|
|
|
for(int i = 0; i < file.extents.Length; i++)
|
|
|
|
|
{
|
2016-07-27 02:35:29 +01:00
|
|
|
byte[] sector;
|
|
|
|
|
|
|
|
|
|
if(!tags)
|
2020-07-20 21:11:32 +01:00
|
|
|
sector = _device.ReadSectors((ulong)file.extents[i].start + _mddf.mddf_block + _volumePrefix,
|
|
|
|
|
(uint)file.extents[i].length);
|
2016-07-27 02:35:29 +01:00
|
|
|
else
|
2020-07-20 21:11:32 +01:00
|
|
|
sector = _device.ReadSectorsTag((ulong)file.extents[i].start + _mddf.mddf_block + _volumePrefix,
|
|
|
|
|
(uint)file.extents[i].length, SectorTagType.AppleSectorTag);
|
2016-07-22 02:18:53 +01:00
|
|
|
|
|
|
|
|
Array.Copy(sector, 0, temp, offset, sector.Length);
|
|
|
|
|
offset += sector.Length;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-27 02:35:29 +01:00
|
|
|
if(!tags)
|
2016-07-22 02:18:53 +01:00
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
if(_fileSizeCache.TryGetValue(fileId, out int realSize))
|
2018-06-22 08:08:38 +01:00
|
|
|
if(realSize > temp.Length)
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.ErrorWriteLine("File {0} gets truncated.", fileId);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-07-27 14:34:29 +01:00
|
|
|
buf = temp;
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
_fileCache.Add(fileId, buf);
|
2016-07-22 02:18:53 +01:00
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
else
|
|
|
|
|
buf = temp;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
|
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-28 22:25:26 +01:00
|
|
|
Errno LookupFileId(string path, out short fileId, out bool isDir)
|
2016-07-22 02:18:53 +01:00
|
|
|
{
|
|
|
|
|
fileId = 0;
|
2018-06-22 08:08:38 +01:00
|
|
|
isDir = false;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(!_mounted)
|
2020-02-29 18:03:35 +00:00
|
|
|
return Errno.AccessDenied;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
string[] pathElements = path.Split(new[]
|
|
|
|
|
{
|
|
|
|
|
'/'
|
|
|
|
|
}, StringSplitOptions.RemoveEmptyEntries);
|
2016-07-22 02:18:53 +01:00
|
|
|
|
|
|
|
|
if(pathElements.Length == 0)
|
|
|
|
|
{
|
2016-07-31 01:53:47 +01:00
|
|
|
fileId = DIRID_ROOT;
|
2018-06-22 08:08:38 +01:00
|
|
|
isDir = true;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-07-22 02:18:53 +01:00
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-31 01:53:47 +01:00
|
|
|
// Only V3 supports subdirectories
|
2020-02-29 18:03:35 +00:00
|
|
|
if(pathElements.Length > 1 &&
|
2020-07-20 21:11:32 +01:00
|
|
|
_mddf.fsversion != LISA_V3)
|
2020-02-29 18:03:35 +00:00
|
|
|
return Errno.NotSupported;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(_debug && pathElements.Length == 1)
|
2016-07-22 02:18:53 +01:00
|
|
|
{
|
2016-07-28 22:25:26 +01:00
|
|
|
if(string.Compare(pathElements[0], "$MDDF", StringComparison.InvariantCulture) == 0)
|
2016-07-22 02:18:53 +01:00
|
|
|
{
|
|
|
|
|
fileId = (short)FILEID_MDDF;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-07-22 02:18:53 +01:00
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-28 22:25:26 +01:00
|
|
|
if(string.Compare(pathElements[0], "$Boot", StringComparison.InvariantCulture) == 0)
|
2016-07-22 02:18:53 +01:00
|
|
|
{
|
|
|
|
|
fileId = FILEID_BOOT_SIGNED;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-07-22 02:18:53 +01:00
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-28 22:25:26 +01:00
|
|
|
if(string.Compare(pathElements[0], "$Loader", StringComparison.InvariantCulture) == 0)
|
2016-07-22 02:18:53 +01:00
|
|
|
{
|
|
|
|
|
fileId = FILEID_LOADER_SIGNED;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-07-22 02:18:53 +01:00
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-28 22:25:26 +01:00
|
|
|
if(string.Compare(pathElements[0], "$Bitmap", StringComparison.InvariantCulture) == 0)
|
2016-07-22 02:18:53 +01:00
|
|
|
{
|
|
|
|
|
fileId = (short)FILEID_BITMAP;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-07-22 02:18:53 +01:00
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-28 22:25:26 +01:00
|
|
|
if(string.Compare(pathElements[0], "$S-Record", StringComparison.InvariantCulture) == 0)
|
2016-07-22 02:18:53 +01:00
|
|
|
{
|
|
|
|
|
fileId = (short)FILEID_SRECORD;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-07-22 02:18:53 +01:00
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-28 22:25:26 +01:00
|
|
|
if(string.Compare(pathElements[0], "$", StringComparison.InvariantCulture) == 0)
|
2016-07-22 02:18:53 +01:00
|
|
|
{
|
2016-07-31 01:53:47 +01:00
|
|
|
fileId = DIRID_ROOT;
|
2018-06-22 08:08:38 +01:00
|
|
|
isDir = true;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-07-22 02:18:53 +01:00
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-31 01:53:47 +01:00
|
|
|
for(int lvl = 0; lvl < pathElements.Length; lvl++)
|
|
|
|
|
{
|
2016-08-01 19:05:06 +01:00
|
|
|
string wantedFilename = pathElements[0].Replace('-', '/');
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
foreach(CatalogEntry entry in _catalogCache)
|
2016-07-31 01:53:47 +01:00
|
|
|
{
|
2017-12-26 08:01:40 +00:00
|
|
|
string filename = StringHandlers.CToString(entry.filename, Encoding);
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2016-07-31 01:53:47 +01:00
|
|
|
// LisaOS is case insensitive
|
2017-12-21 06:06:19 +00:00
|
|
|
if(string.Compare(wantedFilename, filename, StringComparison.InvariantCultureIgnoreCase) != 0 ||
|
2020-02-29 18:03:35 +00:00
|
|
|
entry.parentID != fileId)
|
|
|
|
|
continue;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
fileId = entry.fileID;
|
2018-06-22 08:08:38 +01:00
|
|
|
isDir = entry.fileType == 0x01;
|
2016-07-31 01:53:47 +01:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
// Not last path element, and it's not a directory
|
2020-02-29 18:03:35 +00:00
|
|
|
if(lvl != pathElements.Length - 1 &&
|
|
|
|
|
!isDir)
|
|
|
|
|
return Errno.NotDirectory;
|
2017-12-21 06:06:19 +00:00
|
|
|
|
|
|
|
|
// Arrived last path element
|
2020-02-29 18:03:35 +00:00
|
|
|
if(lvl == pathElements.Length - 1)
|
|
|
|
|
return Errno.NoError;
|
2016-07-22 02:18:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Errno.NoSuchFile;
|
2016-07-21 17:36:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|