Files
Aaru/Aaru.Filesystems/AppleMFS/Xattr.cs

203 lines
8.0 KiB
C#
Raw Normal View History

2017-05-19 20:28:49 +01:00
// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Xattr.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Apple Macintosh File System plugin.
//
// --[ Description ] ----------------------------------------------------------
//
2016-08-01 00:43:31 +01:00
// Methods to handle Apple Macintosh File System extended attributes
// (Finder Info, Resource Fork, etc.)
//
// --[ 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-12-31 23:08:23 +00:00
// Copyright © 2011-2021 Natalia Portillo
// ****************************************************************************/
using System;
2017-12-19 19:33:46 +00:00
using System.Collections.Generic;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Structs;
using Aaru.Helpers;
2020-07-20 15:43:52 +01:00
namespace Aaru.Filesystems
{
// Information from Inside Macintosh Volume II
2020-07-22 13:20:25 +01:00
public sealed partial class AppleMFS
{
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-16 04:42:14 +01:00
public ErrorNumber ListXAttr(string path, out List<string> xattrs)
{
xattrs = null;
2020-07-20 21:11:32 +01:00
if(!_mounted)
2021-09-16 04:42:14 +01:00
return ErrorNumber.AccessDenied;
string[] pathElements = path.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);
if(pathElements.Length != 1)
2021-09-16 04:42:14 +01:00
return ErrorNumber.NotSupported;
path = pathElements[0];
xattrs = new List<string>();
2020-07-20 21:11:32 +01:00
if(_debug)
if(string.Compare(path, "$", StringComparison.InvariantCulture) == 0 ||
string.Compare(path, "$Bitmap", StringComparison.InvariantCulture) == 0 ||
string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0 ||
string.Compare(path, "$MDB", StringComparison.InvariantCulture) == 0)
{
2020-07-20 21:11:32 +01:00
if(_device.Info.ReadableSectorTags.Contains(SectorTagType.AppleSectorTag))
xattrs.Add("com.apple.macintosh.tags");
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoError;
}
2020-07-20 21:11:32 +01:00
if(!_filenameToId.TryGetValue(path.ToLowerInvariant(), out uint fileId))
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoSuchFile;
2020-07-20 21:11:32 +01:00
if(!_idToEntry.TryGetValue(fileId, out FileEntry entry))
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoSuchFile;
if(entry.flRLgLen > 0)
{
xattrs.Add("com.apple.ResourceFork");
2020-07-20 21:11:32 +01:00
if(_debug && _device.Info.ReadableSectorTags.Contains(SectorTagType.AppleSectorTag))
xattrs.Add("com.apple.ResourceFork.tags");
}
xattrs.Add("com.apple.FinderInfo");
2020-07-20 21:11:32 +01:00
if(_debug &&
_device.Info.ReadableSectorTags.Contains(SectorTagType.AppleSectorTag) &&
entry.flLgLen > 0)
xattrs.Add("com.apple.macintosh.tags");
xattrs.Sort();
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoError;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-16 04:42:14 +01:00
public ErrorNumber GetXattr(string path, string xattr, ref byte[] buf)
{
2020-07-20 21:11:32 +01:00
if(!_mounted)
2021-09-16 04:42:14 +01:00
return ErrorNumber.AccessDenied;
string[] pathElements = path.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);
if(pathElements.Length != 1)
2021-09-16 04:42:14 +01:00
return ErrorNumber.NotSupported;
path = pathElements[0];
2020-07-20 21:11:32 +01:00
if(_debug)
if(string.Compare(path, "$", StringComparison.InvariantCulture) == 0 ||
string.Compare(path, "$Bitmap", StringComparison.InvariantCulture) == 0 ||
string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0 ||
string.Compare(path, "$MDB", StringComparison.InvariantCulture) == 0)
2020-07-20 21:11:32 +01:00
if(_device.Info.ReadableSectorTags.Contains(SectorTagType.AppleSectorTag) &&
string.Compare(xattr, "com.apple.macintosh.tags", StringComparison.InvariantCulture) == 0)
{
if(string.Compare(path, "$", StringComparison.InvariantCulture) == 0)
{
2020-07-20 21:11:32 +01:00
buf = new byte[_directoryTags.Length];
Array.Copy(_directoryTags, 0, buf, 0, buf.Length);
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoError;
}
if(string.Compare(path, "$Bitmap", StringComparison.InvariantCulture) == 0)
{
2020-07-20 21:11:32 +01:00
buf = new byte[_bitmapTags.Length];
Array.Copy(_bitmapTags, 0, buf, 0, buf.Length);
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoError;
}
if(string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0)
{
2020-07-20 21:11:32 +01:00
buf = new byte[_bootTags.Length];
Array.Copy(_bootTags, 0, buf, 0, buf.Length);
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoError;
}
if(string.Compare(path, "$MDB", StringComparison.InvariantCulture) == 0)
{
2020-07-20 21:11:32 +01:00
buf = new byte[_mdbTags.Length];
Array.Copy(_mdbTags, 0, buf, 0, buf.Length);
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoError;
}
}
2018-06-22 08:08:38 +01:00
else
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoSuchExtendedAttribute;
2021-09-16 04:42:14 +01:00
ErrorNumber error;
2020-07-20 21:11:32 +01:00
if(!_filenameToId.TryGetValue(path.ToLowerInvariant(), out uint fileId))
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoSuchFile;
2020-07-20 21:11:32 +01:00
if(!_idToEntry.TryGetValue(fileId, out FileEntry entry))
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoSuchFile;
2018-06-22 08:08:38 +01:00
if(entry.flRLgLen > 0 &&
2017-12-19 20:33:03 +00:00
string.Compare(xattr, "com.apple.ResourceFork", StringComparison.InvariantCulture) == 0)
{
error = ReadFile(path, out buf, true, false);
return error;
}
2018-06-22 08:08:38 +01:00
if(entry.flRLgLen > 0 &&
2017-12-19 20:33:03 +00:00
string.Compare(xattr, "com.apple.ResourceFork.tags", StringComparison.InvariantCulture) == 0)
{
error = ReadFile(path, out buf, true, true);
return error;
}
if(string.Compare(xattr, "com.apple.FinderInfo", StringComparison.InvariantCulture) == 0)
{
buf = Marshal.StructureToByteArrayBigEndian(entry.flUsrWds);
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoError;
}
2020-07-20 21:11:32 +01:00
if(!_debug ||
!_device.Info.ReadableSectorTags.Contains(SectorTagType.AppleSectorTag) ||
string.Compare(xattr, "com.apple.macintosh.tags", StringComparison.InvariantCulture) != 0)
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoSuchExtendedAttribute;
error = ReadFile(path, out buf, false, true);
return error;
}
}
2017-12-19 20:33:03 +00:00
}