Files
Aaru/Aaru.Core/Sidecar/Files.cs

236 lines
8.0 KiB
C#
Raw Normal View History

2020-03-11 21:56:55 +00:00
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Files.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Core algorithms.
//
// --[ Description ] ----------------------------------------------------------
//
// Creates sidecar information of files contained in supported read-only
// filesystems.
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2024-12-19 10:45:18 +00:00
// Copyright © 2011-2025 Natalia Portillo
2020-03-11 21:56:55 +00:00
// ****************************************************************************/
using System.Collections.Generic;
using System.Linq;
using Aaru.CommonTypes.AaruMetadata;
2021-09-16 04:42:14 +01:00
using Aaru.CommonTypes.Enums;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Interfaces;
using Aaru.CommonTypes.Structs;
using Aaru.Logging;
2025-08-14 15:51:32 +01:00
using Directory = Aaru.CommonTypes.AaruMetadata.Directory;
using FileAttributes = Aaru.CommonTypes.Structs.FileAttributes;
namespace Aaru.Core;
2022-03-06 13:29:38 +00:00
public sealed partial class Sidecar
{
FilesystemContents Files(IReadOnlyFilesystem filesystem)
{
var contents = new FilesystemContents();
ErrorNumber ret = filesystem.OpenDir("/", out IDirNode node);
2024-05-01 04:05:22 +01:00
if(ret != ErrorNumber.NoError) return null;
2024-05-01 04:39:38 +01:00
List<Directory> directories = [];
List<ContentsFile> files = [];
while(filesystem.ReadDir(node, out string dirent) == ErrorNumber.NoError && dirent is not null)
2022-03-06 13:29:38 +00:00
{
ret = filesystem.Stat(dirent, out FileEntryInfo stat);
if(ret != ErrorNumber.NoError)
{
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME, Localization.Core.Cannot_stat_0, dirent);
2022-03-06 13:29:38 +00:00
continue;
}
2022-03-06 13:29:38 +00:00
if(stat.Attributes.HasFlag(FileAttributes.Directory))
{
directories.Add(SidecarDirectory(filesystem, "", dirent, stat));
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
continue;
}
2022-03-06 13:29:38 +00:00
files.Add(SidecarFile(filesystem, "", dirent, stat));
}
2020-02-29 18:03:35 +00:00
filesystem.CloseDir(node);
if(files.Count > 0) contents.Files = files.OrderBy(static f => f.Name).ToList();
if(directories.Count > 0) contents.Directories = directories.OrderBy(static d => d.Name).ToList();
2022-03-06 13:29:38 +00:00
return contents;
}
2020-02-29 18:03:35 +00:00
Directory SidecarDirectory(IReadOnlyFilesystem filesystem, string path, string filename, FileEntryInfo stat)
2022-03-06 13:29:38 +00:00
{
var directory = new Directory
{
AccessTime = stat.AccessTimeUtc,
Attributes = (ulong)stat.Attributes,
BackupTime = stat.BackupTimeUtc,
CreationTime = stat.CreationTimeUtc,
DeviceNumber = stat.DeviceNo,
Inode = stat.Inode,
LastWriteTime = stat.LastWriteTimeUtc,
Links = stat.Links,
Name = filename,
PosixGroupId = stat.GID,
PosixMode = stat.Mode,
PosixUserId = stat.UID,
StatusChangeTime = stat.StatusChangeTimeUtc
};
ErrorNumber ret = filesystem.OpenDir(path + "/" + filename, out IDirNode node);
2024-05-01 04:05:22 +01:00
if(ret != ErrorNumber.NoError) return null;
2024-05-01 04:39:38 +01:00
List<Directory> directories = [];
List<ContentsFile> files = [];
while(filesystem.ReadDir(node, out string dirent) == ErrorNumber.NoError && dirent is not null)
2022-03-06 13:29:38 +00:00
{
ret = filesystem.Stat(path + "/" + filename + "/" + dirent, out FileEntryInfo entryStat);
2021-09-16 04:42:14 +01:00
if(ret != ErrorNumber.NoError)
2022-03-06 13:29:38 +00:00
{
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME, Localization.Core.Cannot_stat_0, dirent);
2022-03-06 13:29:38 +00:00
continue;
}
2022-03-06 13:29:38 +00:00
if(entryStat.Attributes.HasFlag(FileAttributes.Directory))
{
2022-03-06 13:29:38 +00:00
directories.Add(SidecarDirectory(filesystem, path + "/" + filename, dirent, entryStat));
2022-03-06 13:29:38 +00:00
continue;
}
2022-03-06 13:29:38 +00:00
files.Add(SidecarFile(filesystem, path + "/" + filename, dirent, entryStat));
}
2020-02-29 18:03:35 +00:00
if(files.Count > 0) directory.Files = files.OrderBy(static f => f.Name).ToList();
if(directories.Count > 0) directory.Directories = directories.OrderBy(static d => d.Name).ToList();
2022-03-06 13:29:38 +00:00
return directory;
}
2020-02-29 18:03:35 +00:00
ContentsFile SidecarFile(IReadOnlyFilesystem filesystem, string path, string filename, FileEntryInfo stat)
2022-03-06 13:29:38 +00:00
{
var fileChkWorker = new Checksum();
var file = new ContentsFile
{
AccessTime = stat.AccessTimeUtc,
Attributes = (ulong)stat.Attributes,
BackupTime = stat.BackupTimeUtc,
CreationTime = stat.CreationTimeUtc,
DeviceNumber = stat.DeviceNo,
Inode = stat.Inode,
LastWriteTime = stat.LastWriteTimeUtc,
Length = (ulong)stat.Length,
Links = stat.Links,
Name = filename,
PosixGroupId = stat.GID,
PosixMode = stat.Mode,
PosixUserId = stat.UID,
StatusChangeTime = stat.StatusChangeTimeUtc
};
byte[] data = null;
2022-03-06 13:29:38 +00:00
if(stat.Length > 0)
{
long position = 0;
UpdateStatus(string.Format(Localization.Core.Hashing_file_0_1, path, filename));
2022-03-06 13:29:38 +00:00
InitProgress2();
ErrorNumber error = filesystem.OpenFile(path + "/" + filename, out IFileNode fileNode);
if(error == ErrorNumber.NoError)
{
2022-03-06 13:29:38 +00:00
data = new byte[1048576];
2020-02-29 18:03:35 +00:00
while(position < stat.Length - 1048576)
{
2024-05-01 04:05:22 +01:00
if(_aborted) return file;
2020-02-29 18:03:35 +00:00
// TODO: Better error handling
filesystem.ReadFile(fileNode, 1048576, data, out _);
UpdateProgress2(Localization.Core.Hashing_file_byte_0_of_1, position, stat.Length);
fileChkWorker.Update(data);
position += 1048576;
}
data = new byte[stat.Length - position];
filesystem.ReadFile(fileNode, data.Length, data, out _);
UpdateProgress2(Localization.Core.Hashing_file_byte_0_of_1, position, stat.Length);
fileChkWorker.Update(data);
filesystem.CloseFile(fileNode);
}
2022-03-06 13:29:38 +00:00
EndProgress2();
file.Checksums = fileChkWorker.End();
2022-03-06 13:29:38 +00:00
}
else
file.Checksums = _emptyChecksums;
2022-03-06 13:29:38 +00:00
ErrorNumber ret = filesystem.ListXAttr(path + "/" + filename, out List<string> xattrs);
2024-05-01 04:05:22 +01:00
if(ret != ErrorNumber.NoError) return file;
2024-05-01 04:39:38 +01:00
List<ExtendedAttribute> xattrTypes = [];
2022-03-06 13:29:38 +00:00
foreach(string xattr in xattrs)
{
ret = filesystem.GetXattr(path + "/" + filename, xattr, ref data);
2024-05-01 04:05:22 +01:00
if(ret != ErrorNumber.NoError) continue;
2022-03-06 13:29:38 +00:00
var xattrChkWorker = new Checksum();
xattrChkWorker.Update(data);
xattrTypes.Add(new ExtendedAttribute
{
Checksums = xattrChkWorker.End(),
Length = (ulong)data.Length,
Name = xattr
2022-03-06 13:29:38 +00:00
});
}
if(xattrTypes.Count > 0) file.ExtendedAttributes = xattrTypes.OrderBy(static x => x.Name).ToList();
2022-03-06 13:29:38 +00:00
return file;
}
}