Files
Aaru/Aaru.Filesystems/FAT/Xattr.cs

244 lines
7.7 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// 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/>.
//
// ----------------------------------------------------------------------------
2020-01-03 17:51:30 +00:00
// Copyright © 2011-2020 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Structs;
using Aaru.Helpers;
2020-07-20 15:43:52 +01:00
namespace Aaru.Filesystems
{
public partial class FAT
{
Dictionary<string, Dictionary<string, byte[]>> eaCache;
2020-07-20 05:20:18 +01:00
/// <inheritdoc />
2019-04-26 00:54:51 +01:00
public Errno ListXAttr(string path, out List<string> xattrs)
{
xattrs = null;
2020-02-29 18:03:35 +00:00
if(!mounted)
return Errno.AccessDenied;
2019-04-26 00:54:51 +01:00
// No other xattr recognized yet
2020-02-29 18:03:35 +00:00
if(cachedEaData is null &&
!fat32)
return Errno.NotSupported;
2020-02-29 18:03:35 +00:00
if(path[0] == '/')
path = path.Substring(1);
if(eaCache.TryGetValue(path.ToLower(cultureInfo), out Dictionary<string, byte[]> eas))
{
xattrs = eas.Keys.ToList();
2020-02-29 18:03:35 +00:00
return Errno.NoError;
}
2019-04-28 11:57:54 +01:00
Errno err = GetFileEntry(path, out CompleteDirectoryEntry entry);
2020-02-29 18:03:35 +00:00
if(err != Errno.NoError ||
entry is null)
return err;
xattrs = new List<string>();
2019-04-28 11:57:54 +01:00
if(!fat32)
{
2020-02-29 18:03:35 +00:00
if(entry.Dirent.ea_handle == 0)
return Errno.NoError;
2019-04-28 11:57:54 +01:00
eas = GetEas(entry.Dirent.ea_handle);
}
else
{
2020-02-29 18:03:35 +00:00
if(entry.Fat32Ea.start_cluster == 0)
return Errno.NoError;
2019-04-28 11:57:54 +01:00
eas = GetEas(entry.Fat32Ea);
}
2020-02-29 18:03:35 +00:00
if(eas is null)
return Errno.NoError;
eaCache.Add(path.ToLower(cultureInfo), eas);
xattrs = eas.Keys.ToList();
2020-02-29 18:03:35 +00:00
return Errno.NoError;
}
2020-07-20 05:20:18 +01:00
/// <inheritdoc />
public Errno GetXattr(string path, string xattr, ref byte[] buf)
{
2020-02-29 18:03:35 +00:00
if(!mounted)
return Errno.AccessDenied;
Errno err = ListXAttr(path, out List<string> xattrs);
2020-02-29 18:03:35 +00:00
if(err != Errno.NoError)
return err;
2020-02-29 18:03:35 +00:00
if(path[0] == '/')
path = path.Substring(1);
2020-02-29 18:03:35 +00:00
if(!xattrs.Contains(xattr.ToLower(cultureInfo)))
return Errno.NoSuchExtendedAttribute;
if(!eaCache.TryGetValue(path.ToLower(cultureInfo), out Dictionary<string, byte[]> eas))
return Errno.InvalidArgument;
2020-02-29 18:03:35 +00:00
if(!eas.TryGetValue(xattr.ToLower(cultureInfo), out byte[] data))
return Errno.InvalidArgument;
buf = new byte[data.Length];
data.CopyTo(buf, 0);
return Errno.NoError;
}
2019-04-28 11:57:54 +01:00
Dictionary<string, byte[]> GetEas(DirectoryEntry entryFat32Ea)
{
2020-02-29 18:03:35 +00:00
var eaMs = new MemoryStream();
uint[] rootDirectoryClusters = GetClusters(entryFat32Ea.start_cluster);
2019-04-28 11:57:54 +01:00
foreach(uint cluster in rootDirectoryClusters)
{
2020-02-29 18:03:35 +00:00
byte[] buffer =
image.ReadSectors(firstClusterSector + (cluster * sectorsPerCluster), sectorsPerCluster);
2019-04-28 11:57:54 +01:00
eaMs.Write(buffer, 0, buffer.Length);
}
byte[] full = eaMs.ToArray();
ushort size = BitConverter.ToUInt16(full, 0);
byte[] eas = new byte[size];
Array.Copy(full, 0, eas, 0, size);
full = null;
eaMs.Close();
return GetEas(eas);
}
Dictionary<string, byte[]> GetEas(ushort eaHandle)
{
int aIndex = eaHandle >> 7;
2020-02-29 18:03:35 +00:00
// First 0x20 bytes are the magic number and unused words
2020-02-29 18:03:35 +00:00
ushort a = BitConverter.ToUInt16(cachedEaData, (aIndex * 2) + 0x20);
2020-02-29 18:03:35 +00:00
ushort b = BitConverter.ToUInt16(cachedEaData, (eaHandle * 2) + 0x200);
uint eaCluster = (uint)(a + b);
2020-02-29 18:03:35 +00:00
if(b == EA_UNUSED)
return null;
EaHeader header =
Marshal.ByteArrayToStructureLittleEndian<EaHeader>(cachedEaData, (int)(eaCluster * bytesPerCluster),
Marshal.SizeOf<EaHeader>());
2020-02-29 18:03:35 +00:00
if(header.magic != 0x4145)
return null;
uint eaLen = BitConverter.ToUInt32(cachedEaData,
(int)(eaCluster * bytesPerCluster) + Marshal.SizeOf<EaHeader>());
byte[] eaData = new byte[eaLen];
Array.Copy(cachedEaData, (int)(eaCluster * bytesPerCluster) + Marshal.SizeOf<EaHeader>(), eaData, 0, eaLen);
2019-04-28 11:57:54 +01:00
return GetEas(eaData);
}
Dictionary<string, byte[]> GetEas(byte[] eaData)
{
2020-02-29 18:03:35 +00:00
if(eaData is null ||
eaData.Length < 4)
return null;
2019-04-28 11:57:54 +01:00
Dictionary<string, byte[]> eas = new Dictionary<string, byte[]>();
2020-02-29 18:03:35 +00:00
if(debug)
eas.Add("com.microsoft.os2.fea", eaData);
int pos = 4;
2020-02-29 18:03:35 +00:00
while(pos < eaData.Length)
{
2020-07-20 07:47:12 +01:00
pos++; // Skip fEA
byte cbName = eaData[pos++];
ushort cbValue = BitConverter.ToUInt16(eaData, pos);
pos += 2;
string name = Encoding.ASCII.GetString(eaData, pos, cbName);
pos += cbName;
pos++;
byte[] data = new byte[cbValue];
Array.Copy(eaData, pos, data, 0, cbValue);
pos += cbValue;
// OS/2 System Attributes
if(name[0] == '.')
{
// This is WorkPlace System information so it's IBM
2020-02-29 18:03:35 +00:00
if(name == ".CLASSINFO")
name = "com.ibm.os2.classinfo";
else
name = "com.microsoft.os2" + name.ToLower();
}
eas.Add(name, data);
}
return eas;
2019-04-26 00:54:51 +01:00
}
void CacheEaData()
{
2020-02-29 18:03:35 +00:00
if(eaDirEntry.start_cluster == 0)
return;
2020-02-29 18:03:35 +00:00
var eaDataMs = new MemoryStream();
2020-02-29 18:03:35 +00:00
foreach(byte[] buffer in GetClusters(eaDirEntry.start_cluster).
Select(cluster => image.ReadSectors(firstClusterSector + (cluster * sectorsPerCluster),
sectorsPerCluster)))
eaDataMs.Write(buffer, 0, buffer.Length);
cachedEaData = eaDataMs.ToArray();
}
}
}