Files
Aaru/Aaru.Filesystems/ISO9660/File.cs

592 lines
21 KiB
C#
Raw Normal View History

2019-07-31 20:10:27 +01:00
// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
2019-07-31 20:10:27 +01:00
// ----------------------------------------------------------------------------
//
// Filename : File.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : ISO9660 filesystem plugin.
//
// --[ Description ] ----------------------------------------------------------
//
// Handles file and extents.
//
// --[ 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-01-03 17:51:30 +00:00
// Copyright © 2011-2020 Natalia Portillo
// In the loving memory of Facunda "Tata" Suárez Domínguez, R.I.P. 2019/07/24
2019-07-31 20:10:27 +01:00
// ****************************************************************************/
using System;
2019-07-19 16:34:43 +01:00
using System.Collections.Generic;
using System.Globalization;
2019-07-20 00:38:59 +01:00
using System.IO;
2019-07-19 16:34:43 +01:00
using System.Linq;
using System.Runtime.CompilerServices;
using Aaru.CommonTypes.Enums;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Structs;
using Aaru.Console;
2020-02-27 00:33:26 +00:00
using Aaru.Helpers;
using FileAttributes = Aaru.CommonTypes.Structs.FileAttributes;
2020-02-27 00:33:26 +00:00
namespace Aaru.Filesystems.ISO9660
{
public partial class ISO9660
{
2019-07-19 21:37:02 +01:00
public Errno MapBlock(string path, long fileBlock, out long deviceBlock)
{
deviceBlock = 0;
if(!mounted)
return Errno.AccessDenied;
2019-07-19 21:37:02 +01:00
Errno err = GetFileEntry(path, out DecodedDirectoryEntry entry);
if(err != Errno.NoError)
return err;
if(entry.Flags.HasFlag(FileFlags.Directory) &&
!debug)
return Errno.IsDirectory;
2019-07-19 21:37:02 +01:00
// TODO: Multi-extents
if(entry.Extents.Count > 1)
return Errno.NotImplemented;
deviceBlock = entry.Extents[0].extent + fileBlock;
2019-07-19 21:37:02 +01:00
return Errno.NoError;
}
2019-07-19 16:35:27 +01:00
public Errno GetAttributes(string path, out FileAttributes attributes)
{
attributes = new FileAttributes();
if(!mounted)
return Errno.AccessDenied;
2019-07-19 16:35:27 +01:00
Errno err = Stat(path, out FileEntryInfo stat);
if(err != Errno.NoError)
return err;
2019-07-19 16:35:27 +01:00
attributes = stat.Attributes;
return Errno.NoError;
}
2019-07-28 21:33:05 +01:00
// TODO: Resolve symbolic link
2019-07-20 00:38:59 +01:00
public Errno Read(string path, long offset, long size, ref byte[] buf)
{
buf = null;
if(!mounted)
return Errno.AccessDenied;
2019-07-20 00:38:59 +01:00
Errno err = GetFileEntry(path, out DecodedDirectoryEntry entry);
if(err != Errno.NoError)
return err;
if(entry.Flags.HasFlag(FileFlags.Directory) &&
!debug)
return Errno.IsDirectory;
2019-07-20 00:38:59 +01:00
if(entry.Extents is null)
return Errno.InvalidArgument;
if(entry.Size == 0)
2019-07-20 00:38:59 +01:00
{
buf = new byte[0];
2019-07-20 00:38:59 +01:00
return Errno.NoError;
}
if(offset >= (long)entry.Size)
return Errno.InvalidArgument;
if(size + offset >= (long)entry.Size)
size = (long)entry.Size - offset;
2019-07-31 04:33:31 +01:00
offset += entry.XattrLength;
if(entry.CdiSystemArea != null &&
entry.CdiSystemArea.Value.attributes.HasFlag(CdiAttributes.DigitalAudio) &&
entry.Extents.Count == 1)
{
try
{
long firstSector = offset / 2352;
long offsetInSector = offset % 2352;
long sizeInSectors = (size + offsetInSector) / 2352;
if((size + offsetInSector) % 2352 > 0)
sizeInSectors++;
2019-07-20 00:38:59 +01:00
byte[] buffer =
image.ReadSectorsLong((ulong)(entry.Extents[0].extent + firstSector), (uint)sizeInSectors);
buf = new byte[size];
Array.Copy(buffer, offsetInSector, buf, 0, size);
return Errno.NoError;
}
catch(Exception e)
{
AaruConsole.DebugWriteLine("ISO9660 plugin", "Exception reading CD-i audio file");
AaruConsole.DebugWriteLine("ISO9660 plugin", "{0}", e);
return Errno.InOutError;
}
}
2020-03-10 22:47:10 +00:00
buf = ReadWithExtents(offset, size, entry.Extents,
entry.XA?.signature == XA_MAGIC &&
entry.XA?.attributes.HasFlag(XaAttributes.Interleaved) == true,
entry.XA?.filenumber ?? 0);
2019-07-20 00:38:59 +01:00
return Errno.NoError;
}
2019-07-19 16:34:43 +01:00
public Errno Stat(string path, out FileEntryInfo stat)
{
stat = null;
if(!mounted)
return Errno.AccessDenied;
2019-07-19 16:34:43 +01:00
Errno err = GetFileEntry(path, out DecodedDirectoryEntry entry);
if(err != Errno.NoError)
return err;
2019-07-19 16:34:43 +01:00
stat = new FileEntryInfo
{
Attributes = new FileAttributes(), Blocks = (long)(entry.Size / 2048), // TODO: XA
BlockSize = 2048, Length = (long)entry.Size, Links = 1, LastWriteTimeUtc = entry.Timestamp
2019-07-19 16:34:43 +01:00
};
2020-04-18 20:49:46 +01:00
if(entry.Extents?.Count > 0)
stat.Inode = entry.Extents[0].extent;
if(entry.Size % 2048 > 0)
stat.Blocks++;
if(entry.Flags.HasFlag(FileFlags.Directory))
stat.Attributes |= FileAttributes.Directory;
2019-07-19 16:34:43 +01:00
if(entry.Flags.HasFlag(FileFlags.Hidden))
stat.Attributes |= FileAttributes.Hidden;
2019-07-19 16:34:43 +01:00
if(entry.FinderInfo != null)
{
AppleCommon.FInfo finderInfo = entry.FinderInfo.Value;
if(finderInfo.fdFlags.HasFlag(AppleCommon.FinderFlags.kIsAlias))
stat.Attributes |= FileAttributes.Alias;
if(finderInfo.fdFlags.HasFlag(AppleCommon.FinderFlags.kIsInvisible))
stat.Attributes |= FileAttributes.Hidden;
if(finderInfo.fdFlags.HasFlag(AppleCommon.FinderFlags.kHasBeenInited))
stat.Attributes |= FileAttributes.HasBeenInited;
if(finderInfo.fdFlags.HasFlag(AppleCommon.FinderFlags.kHasCustomIcon))
stat.Attributes |= FileAttributes.HasCustomIcon;
if(finderInfo.fdFlags.HasFlag(AppleCommon.FinderFlags.kHasNoINITs))
stat.Attributes |= FileAttributes.HasNoINITs;
if(finderInfo.fdFlags.HasFlag(AppleCommon.FinderFlags.kIsOnDesk))
stat.Attributes |= FileAttributes.IsOnDesk;
if(finderInfo.fdFlags.HasFlag(AppleCommon.FinderFlags.kIsShared))
stat.Attributes |= FileAttributes.Shared;
if(finderInfo.fdFlags.HasFlag(AppleCommon.FinderFlags.kIsStationery))
stat.Attributes |= FileAttributes.Stationery;
if(finderInfo.fdFlags.HasFlag(AppleCommon.FinderFlags.kHasBundle))
stat.Attributes |= FileAttributes.Bundle;
}
if(entry.AppleIcon != null)
stat.Attributes |= FileAttributes.HasCustomIcon;
2019-07-28 16:48:18 +01:00
if(entry.XA != null)
{
if(entry.XA.Value.attributes.HasFlag(XaAttributes.GroupExecute))
stat.Mode |= 8;
if(entry.XA.Value.attributes.HasFlag(XaAttributes.GroupRead))
stat.Mode |= 32;
if(entry.XA.Value.attributes.HasFlag(XaAttributes.OwnerExecute))
stat.Mode |= 64;
if(entry.XA.Value.attributes.HasFlag(XaAttributes.OwnerRead))
stat.Mode |= 256;
if(entry.XA.Value.attributes.HasFlag(XaAttributes.SystemExecute))
stat.Mode |= 1;
if(entry.XA.Value.attributes.HasFlag(XaAttributes.SystemRead))
stat.Mode |= 4;
2019-07-28 16:48:18 +01:00
stat.UID = entry.XA.Value.user;
stat.GID = entry.XA.Value.group;
stat.Inode = entry.XA.Value.filenumber;
}
2019-07-28 17:46:09 +01:00
if(entry.PosixAttributes != null)
{
stat.Mode = (uint?)entry.PosixAttributes.Value.st_mode & 0x0FFF;
2019-07-28 17:46:09 +01:00
if(entry.PosixAttributes.Value.st_mode.HasFlag(PosixMode.Block))
stat.Attributes |= FileAttributes.BlockDevice;
2019-07-28 17:46:09 +01:00
if(entry.PosixAttributes.Value.st_mode.HasFlag(PosixMode.Character))
stat.Attributes |= FileAttributes.CharDevice;
if(entry.PosixAttributes.Value.st_mode.HasFlag(PosixMode.Pipe))
stat.Attributes |= FileAttributes.Pipe;
2019-07-28 17:46:09 +01:00
if(entry.PosixAttributes.Value.st_mode.HasFlag(PosixMode.Socket))
stat.Attributes |= FileAttributes.Socket;
2019-07-28 17:46:09 +01:00
if(entry.PosixAttributes.Value.st_mode.HasFlag(PosixMode.Symlink))
stat.Attributes |= FileAttributes.Symlink;
2019-07-28 17:46:09 +01:00
stat.Links = entry.PosixAttributes.Value.st_nlink;
stat.UID = entry.PosixAttributes.Value.st_uid;
stat.GID = entry.PosixAttributes.Value.st_gid;
stat.Inode = entry.PosixAttributes.Value.st_ino;
}
else if(entry.PosixAttributesOld != null)
{
stat.Mode = (uint?)entry.PosixAttributesOld.Value.st_mode & 0x0FFF;
2019-07-28 17:46:09 +01:00
if(entry.PosixAttributesOld.Value.st_mode.HasFlag(PosixMode.Block))
stat.Attributes |= FileAttributes.BlockDevice;
2019-07-28 17:46:09 +01:00
if(entry.PosixAttributesOld.Value.st_mode.HasFlag(PosixMode.Character))
stat.Attributes |= FileAttributes.CharDevice;
2019-07-28 17:46:09 +01:00
if(entry.PosixAttributesOld.Value.st_mode.HasFlag(PosixMode.Pipe))
stat.Attributes |= FileAttributes.Pipe;
2019-07-28 17:46:09 +01:00
if(entry.PosixAttributesOld.Value.st_mode.HasFlag(PosixMode.Socket))
stat.Attributes |= FileAttributes.Socket;
2019-07-28 17:46:09 +01:00
if(entry.PosixAttributesOld.Value.st_mode.HasFlag(PosixMode.Symlink))
stat.Attributes |= FileAttributes.Symlink;
2019-07-28 17:46:09 +01:00
stat.Links = entry.PosixAttributesOld.Value.st_nlink;
stat.UID = entry.PosixAttributesOld.Value.st_uid;
stat.GID = entry.PosixAttributesOld.Value.st_gid;
}
2019-07-28 17:29:45 +01:00
if(entry.AmigaProtection != null)
{
if(entry.AmigaProtection.Value.Multiuser.HasFlag(AmigaMultiuser.GroupExec))
stat.Mode |= 8;
if(entry.AmigaProtection.Value.Multiuser.HasFlag(AmigaMultiuser.GroupRead))
stat.Mode |= 32;
if(entry.AmigaProtection.Value.Multiuser.HasFlag(AmigaMultiuser.GroupWrite))
stat.Mode |= 16;
if(entry.AmigaProtection.Value.Multiuser.HasFlag(AmigaMultiuser.OtherExec))
stat.Mode |= 1;
if(entry.AmigaProtection.Value.Multiuser.HasFlag(AmigaMultiuser.OtherRead))
stat.Mode |= 4;
if(entry.AmigaProtection.Value.Multiuser.HasFlag(AmigaMultiuser.OtherWrite))
stat.Mode |= 2;
if(entry.AmigaProtection.Value.Protection.HasFlag(AmigaAttributes.OwnerExec))
stat.Mode |= 64;
if(entry.AmigaProtection.Value.Protection.HasFlag(AmigaAttributes.OwnerRead))
stat.Mode |= 256;
if(entry.AmigaProtection.Value.Protection.HasFlag(AmigaAttributes.OwnerWrite))
stat.Mode |= 128;
2019-07-28 17:29:45 +01:00
if(entry.AmigaProtection.Value.Protection.HasFlag(AmigaAttributes.Archive))
stat.Attributes |= FileAttributes.Archive;
}
2019-07-28 17:54:40 +01:00
if(entry.PosixDeviceNumber != null)
stat.DeviceNo = (entry.PosixDeviceNumber.Value.dev_t_high << 32) +
entry.PosixDeviceNumber.Value.dev_t_low;
if(entry.RripModify != null)
stat.LastWriteTimeUtc = DecodeIsoDateTime(entry.RripModify);
2019-07-28 18:32:15 +01:00
if(entry.RripAccess != null)
stat.AccessTimeUtc = DecodeIsoDateTime(entry.RripAccess);
2019-07-28 18:32:15 +01:00
if(entry.RripAttributeChange != null)
stat.StatusChangeTimeUtc = DecodeIsoDateTime(entry.RripAttributeChange);
if(entry.RripBackup != null)
stat.BackupTimeUtc = DecodeIsoDateTime(entry.RripBackup);
2019-07-28 18:32:15 +01:00
if(entry.SymbolicLink != null)
stat.Attributes |= FileAttributes.Symlink;
2019-07-28 21:33:05 +01:00
if(entry.XattrLength == 0 ||
cdi ||
highSierra)
return Errno.NoError;
2019-07-31 17:44:51 +01:00
if(entry.CdiSystemArea != null)
{
stat.UID = entry.CdiSystemArea.Value.owner;
stat.GID = entry.CdiSystemArea.Value.group;
if(entry.CdiSystemArea.Value.attributes.HasFlag(CdiAttributes.GroupExecute))
2019-07-31 23:02:43 +01:00
stat.Mode |= 8;
if(entry.CdiSystemArea.Value.attributes.HasFlag(CdiAttributes.GroupRead))
2019-07-31 23:02:43 +01:00
stat.Mode |= 32;
if(entry.CdiSystemArea.Value.attributes.HasFlag(CdiAttributes.OtherExecute))
2019-07-31 23:02:43 +01:00
stat.Mode |= 1;
if(entry.CdiSystemArea.Value.attributes.HasFlag(CdiAttributes.OtherRead))
2019-07-31 23:02:43 +01:00
stat.Mode |= 4;
if(entry.CdiSystemArea.Value.attributes.HasFlag(CdiAttributes.OwnerExecute))
2019-07-31 23:02:43 +01:00
stat.Mode |= 64;
if(entry.CdiSystemArea.Value.attributes.HasFlag(CdiAttributes.OwnerRead))
2019-07-31 23:02:43 +01:00
stat.Mode |= 256;
2019-07-31 17:44:51 +01:00
}
byte[] ea = ReadSingleExtent(0, entry.XattrLength, entry.Extents[0].extent);
ExtendedAttributeRecord ear = Marshal.ByteArrayToStructureLittleEndian<ExtendedAttributeRecord>(ea);
stat.UID = ear.owner;
stat.GID = ear.group;
stat.Mode = 0;
if(ear.permissions.HasFlag(Permissions.GroupExecute))
stat.Mode |= 8;
if(ear.permissions.HasFlag(Permissions.GroupRead))
stat.Mode |= 32;
if(ear.permissions.HasFlag(Permissions.OwnerExecute))
stat.Mode |= 64;
if(ear.permissions.HasFlag(Permissions.OwnerRead))
stat.Mode |= 256;
if(ear.permissions.HasFlag(Permissions.OtherExecute))
stat.Mode |= 1;
if(ear.permissions.HasFlag(Permissions.OtherRead))
stat.Mode |= 4;
stat.CreationTimeUtc = DateHandlers.Iso9660ToDateTime(ear.creation_date);
stat.LastWriteTimeUtc = DateHandlers.Iso9660ToDateTime(ear.modification_date);
2019-07-19 16:34:43 +01:00
return Errno.NoError;
}
2019-07-28 21:33:05 +01:00
public Errno ReadLink(string path, out string dest)
{
dest = null;
Errno err = GetFileEntry(path, out DecodedDirectoryEntry entry);
if(err != Errno.NoError)
return err;
if(entry.SymbolicLink is null)
return Errno.InvalidArgument;
2019-07-28 21:33:05 +01:00
dest = entry.SymbolicLink;
return Errno.NoError;
}
2019-07-19 16:34:43 +01:00
Errno GetFileEntry(string path, out DecodedDirectoryEntry entry)
{
entry = null;
2019-07-31 19:53:47 +01:00
string cutPath = path.StartsWith("/", StringComparison.Ordinal)
2019-07-19 16:34:43 +01:00
? path.Substring(1).ToLower(CultureInfo.CurrentUICulture)
: path.ToLower(CultureInfo.CurrentUICulture);
string[] pieces = cutPath.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);
if(pieces.Length == 0)
return Errno.InvalidArgument;
2019-07-19 16:34:43 +01:00
string parentPath = string.Join("/", pieces, 0, pieces.Length - 1);
if(!directoryCache.TryGetValue(parentPath, out _))
{
Errno err = ReadDir(parentPath, out _);
if(err != Errno.NoError)
return err;
2019-07-19 16:34:43 +01:00
}
Dictionary<string, DecodedDirectoryEntry> parent;
if(pieces.Length == 1)
parent = rootDirectoryCache;
else if(!directoryCache.TryGetValue(parentPath, out parent))
return Errno.InvalidArgument;
2019-07-19 16:34:43 +01:00
KeyValuePair<string, DecodedDirectoryEntry> dirent =
2020-04-22 00:22:34 +01:00
parent.FirstOrDefault(t => t.Key.ToLower(CultureInfo.CurrentUICulture) == pieces[^1]);
2019-07-19 16:34:43 +01:00
if(string.IsNullOrEmpty(dirent.Key))
{
if(!joliet &&
2020-04-22 00:22:34 +01:00
!pieces[^1].EndsWith(";1", StringComparison.Ordinal))
{
dirent = parent.FirstOrDefault(t => t.Key.ToLower(CultureInfo.CurrentUICulture) ==
2020-04-22 00:22:34 +01:00
pieces[^1] + ";1");
if(string.IsNullOrEmpty(dirent.Key))
return Errno.NoSuchFile;
}
else
return Errno.NoSuchFile;
}
2019-07-19 16:34:43 +01:00
entry = dirent.Value;
2019-07-19 16:34:43 +01:00
return Errno.NoError;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2020-03-10 22:47:10 +00:00
byte[] ReadSingleExtent(long offset, long size, uint startingSector, bool interleaved = false,
byte fileNumber = 0) => ReadWithExtents(offset, size, new List<(uint extent, uint size)>
{
(startingSector, (uint)size)
}, interleaved, fileNumber);
// Cannot think how to make this faster, as we don't know the mode sector until it is read, but we have size in bytes
2020-03-10 22:47:10 +00:00
byte[] ReadWithExtents(long offset, long size, List<(uint extent, uint size)> extents, bool interleaved,
byte fileNumber)
{
var ms = new MemoryStream();
long currentFilePos = 0;
for(int i = 0; i < extents.Count; i++)
{
if(offset - currentFilePos >= extents[i].size)
{
currentFilePos += extents[i].size;
continue;
}
long leftExtentSize = extents[i].size;
uint currentExtentSector = 0;
while(leftExtentSize > 0)
{
2020-03-10 22:47:10 +00:00
byte[] sector = ReadSector(extents[i].extent + currentExtentSector, interleaved, fileNumber);
if(sector is null)
{
currentExtentSector++;
continue;
}
if(offset - currentFilePos > sector.Length)
{
currentExtentSector++;
leftExtentSize -= sector.Length;
currentFilePos += sector.Length;
continue;
}
if(offset - currentFilePos > 0)
ms.Write(sector, (int)(offset - currentFilePos),
(int)(sector.Length - (offset - currentFilePos)));
else
ms.Write(sector, 0, sector.Length);
currentExtentSector++;
leftExtentSize -= sector.Length;
currentFilePos += sector.Length;
if(ms.Length >= size)
break;
}
if(ms.Length >= size)
break;
}
if(ms.Length >= size)
ms.SetLength(size);
return ms.ToArray();
}
byte[] ReadSubheaderWithExtents(List<(uint extent, uint size)> extents, bool copy)
{
var ms = new MemoryStream();
for(int i = 0; i < extents.Count; i++)
{
long leftExtentSize = extents[i].size;
uint currentExtentSector = 0;
while(leftExtentSize > 0)
{
try
{
byte[] fullSector =
image.ReadSectorTag(extents[i].extent + currentExtentSector,
SectorTagType.CdSectorSubHeader);
ms.Write(fullSector, copy ? 0 : 4, 4);
}
catch
{
// Do nothing
}
currentExtentSector++;
leftExtentSize -= 2048;
}
}
return ms.ToArray();
}
}
}