2019-04-25 01:25:13 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2019-04-25 01:25:13 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : Xattr.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// Component : Microsoft FAT filesystem plugin.
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Methods to handle Microsoft FAT extended attributes.
|
|
|
|
|
//
|
|
|
|
|
// --[ 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-02-18 10:02:53 +00:00
|
|
|
// Copyright © 2011-2022 Natalia Portillo
|
2019-04-25 01:25:13 +01:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
2019-04-27 20:19:18 +01:00
|
|
|
using System;
|
2019-04-25 01:25:13 +01:00
|
|
|
using System.Collections.Generic;
|
2019-04-27 20:19:18 +01:00
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2021-09-16 04:42:14 +01:00
|
|
|
using Aaru.CommonTypes.Enums;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.Helpers;
|
2019-04-25 01:25:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
namespace Aaru.Filesystems;
|
|
|
|
|
|
|
|
|
|
public sealed partial class FAT
|
2019-04-25 01:25:13 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
Dictionary<string, Dictionary<string, byte[]>> _eaCache;
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ErrorNumber ListXAttr(string path, out List<string> xattrs)
|
2019-04-25 01:25:13 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
xattrs = null;
|
|
|
|
|
|
|
|
|
|
if(!_mounted)
|
|
|
|
|
return ErrorNumber.AccessDenied;
|
|
|
|
|
|
|
|
|
|
// No other xattr recognized yet
|
|
|
|
|
if(_cachedEaData is null &&
|
|
|
|
|
!_fat32)
|
|
|
|
|
return ErrorNumber.NotSupported;
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(path[0] == '/')
|
|
|
|
|
path = path.Substring(1);
|
|
|
|
|
|
|
|
|
|
if(_eaCache.TryGetValue(path.ToLower(_cultureInfo), out Dictionary<string, byte[]> eas))
|
2019-04-26 00:54:51 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
xattrs = eas.Keys.ToList();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
2019-04-26 00:54:51 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
ErrorNumber err = GetFileEntry(path, out CompleteDirectoryEntry entry);
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(err != ErrorNumber.NoError ||
|
|
|
|
|
entry is null)
|
|
|
|
|
return err;
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
xattrs = new List<string>();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!_fat32)
|
|
|
|
|
{
|
|
|
|
|
if(entry.Dirent.ea_handle == 0)
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.NoError;
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
eas = GetEas(entry.Dirent.ea_handle);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(entry.Fat32Ea.start_cluster == 0)
|
|
|
|
|
return ErrorNumber.NoError;
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
eas = GetEas(entry.Fat32Ea);
|
|
|
|
|
}
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(eas is null)
|
|
|
|
|
return ErrorNumber.NoError;
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
_eaCache.Add(path.ToLower(_cultureInfo), eas);
|
|
|
|
|
xattrs = eas.Keys.ToList();
|
2019-04-28 11:57:54 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ErrorNumber GetXattr(string path, string xattr, ref byte[] buf)
|
|
|
|
|
{
|
|
|
|
|
if(!_mounted)
|
|
|
|
|
return ErrorNumber.AccessDenied;
|
2019-04-27 22:21:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
ErrorNumber err = ListXAttr(path, out List<string> xattrs);
|
2019-04-27 22:21:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(err != ErrorNumber.NoError)
|
|
|
|
|
return err;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(path[0] == '/')
|
|
|
|
|
path = path.Substring(1);
|
2019-04-27 22:21:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!xattrs.Contains(xattr.ToLower(_cultureInfo)))
|
|
|
|
|
return ErrorNumber.NoSuchExtendedAttribute;
|
2019-04-27 22:21:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!_eaCache.TryGetValue(path.ToLower(_cultureInfo), out Dictionary<string, byte[]> eas))
|
|
|
|
|
return ErrorNumber.InvalidArgument;
|
2019-04-27 22:21:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!eas.TryGetValue(xattr.ToLower(_cultureInfo), out byte[] data))
|
|
|
|
|
return ErrorNumber.InvalidArgument;
|
2019-04-27 22:21:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
buf = new byte[data.Length];
|
|
|
|
|
data.CopyTo(buf, 0);
|
2019-04-27 22:21:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
2019-04-27 22:21:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
Dictionary<string, byte[]> GetEas(DirectoryEntry entryFat32Ea)
|
|
|
|
|
{
|
|
|
|
|
var eaMs = new MemoryStream();
|
|
|
|
|
uint[] rootDirectoryClusters = GetClusters(entryFat32Ea.start_cluster);
|
2019-04-27 22:21:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
foreach(uint cluster in rootDirectoryClusters)
|
|
|
|
|
{
|
|
|
|
|
ErrorNumber errno = _image.ReadSectors(_firstClusterSector + (cluster * _sectorsPerCluster),
|
|
|
|
|
_sectorsPerCluster, out byte[] buffer);
|
2019-04-27 22:21:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
return null;
|
2019-04-27 22:21:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
eaMs.Write(buffer, 0, buffer.Length);
|
2019-04-27 22:21:13 +01:00
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
byte[] full = eaMs.ToArray();
|
|
|
|
|
ushort size = BitConverter.ToUInt16(full, 0);
|
|
|
|
|
byte[] eas = new byte[size];
|
|
|
|
|
Array.Copy(full, 0, eas, 0, size);
|
2019-04-28 11:57:54 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
eaMs.Close();
|
2021-09-19 21:16:47 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return GetEas(eas);
|
|
|
|
|
}
|
2021-09-19 21:16:47 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
Dictionary<string, byte[]> GetEas(ushort eaHandle)
|
|
|
|
|
{
|
|
|
|
|
int aIndex = eaHandle >> 7;
|
2019-04-28 11:57:54 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// First 0x20 bytes are the magic number and unused words
|
|
|
|
|
ushort a = BitConverter.ToUInt16(_cachedEaData, (aIndex * 2) + 0x20);
|
2019-04-28 11:57:54 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
ushort b = BitConverter.ToUInt16(_cachedEaData, (eaHandle * 2) + 0x200);
|
2019-04-28 11:57:54 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
uint eaCluster = (uint)(a + b);
|
2019-04-28 11:57:54 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(b == EA_UNUSED)
|
|
|
|
|
return null;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
EaHeader header =
|
|
|
|
|
Marshal.ByteArrayToStructureLittleEndian<EaHeader>(_cachedEaData, (int)(eaCluster * _bytesPerCluster),
|
|
|
|
|
Marshal.SizeOf<EaHeader>());
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(header.magic != 0x4145)
|
|
|
|
|
return null;
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
uint eaLen = BitConverter.ToUInt32(_cachedEaData,
|
|
|
|
|
(int)(eaCluster * _bytesPerCluster) + Marshal.SizeOf<EaHeader>());
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
byte[] eaData = new byte[eaLen];
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
Array.Copy(_cachedEaData, (int)(eaCluster * _bytesPerCluster) + Marshal.SizeOf<EaHeader>(), eaData, 0,
|
|
|
|
|
eaLen);
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return GetEas(eaData);
|
|
|
|
|
}
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
Dictionary<string, byte[]> GetEas(byte[] eaData)
|
|
|
|
|
{
|
|
|
|
|
if(eaData is null ||
|
|
|
|
|
eaData.Length < 4)
|
|
|
|
|
return null;
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
Dictionary<string, byte[]> eas = new();
|
2020-07-20 21:11:32 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(_debug)
|
|
|
|
|
eas.Add("com.microsoft.os2.fea", eaData);
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
int pos = 4;
|
2019-04-28 11:57:54 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
while(pos < eaData.Length)
|
2019-04-28 11:57:54 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
pos++; // Skip fEA
|
|
|
|
|
byte cbName = eaData[pos++];
|
|
|
|
|
ushort cbValue = BitConverter.ToUInt16(eaData, pos);
|
|
|
|
|
pos += 2;
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
string name = Encoding.ASCII.GetString(eaData, pos, cbName);
|
|
|
|
|
pos += cbName;
|
|
|
|
|
pos++;
|
|
|
|
|
byte[] data = new byte[cbValue];
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
Array.Copy(eaData, pos, data, 0, cbValue);
|
|
|
|
|
pos += cbValue;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// OS/2 System Attributes
|
|
|
|
|
if(name[0] == '.')
|
2019-04-27 20:19:18 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
// This is WorkPlace System information so it's IBM
|
|
|
|
|
if(name == ".CLASSINFO")
|
|
|
|
|
name = "com.ibm.os2.classinfo";
|
|
|
|
|
else
|
|
|
|
|
name = "com.microsoft.os2" + name.ToLower();
|
2019-04-27 20:19:18 +01:00
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
eas.Add(name, data);
|
2019-04-26 00:54:51 +01:00
|
|
|
}
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return eas;
|
|
|
|
|
}
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
void CacheEaData()
|
|
|
|
|
{
|
|
|
|
|
if(_eaDirEntry.start_cluster == 0)
|
|
|
|
|
return;
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
var eaDataMs = new MemoryStream();
|
2021-09-19 21:16:47 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
foreach(uint cluster in GetClusters(_eaDirEntry.start_cluster))
|
|
|
|
|
{
|
|
|
|
|
ErrorNumber errno = _image.ReadSectors(_firstClusterSector + (cluster * _sectorsPerCluster),
|
|
|
|
|
_sectorsPerCluster, out byte[] buffer);
|
2021-09-19 21:16:47 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
break;
|
2019-04-27 20:19:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
eaDataMs.Write(buffer, 0, buffer.Length);
|
2019-04-27 20:19:18 +01:00
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
_cachedEaData = eaDataMs.ToArray();
|
2019-04-25 01:25:13 +01:00
|
|
|
}
|
|
|
|
|
}
|