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-05-01 04:17:32 +01:00
|
|
|
// Copyright © 2011-2024 Natalia Portillo
|
2020-03-11 21:56:55 +00:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
2019-05-10 23:36:49 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2022-12-15 22:21:07 +00:00
|
|
|
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.Console;
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
namespace Aaru.Core;
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
public sealed partial class Sidecar
|
2019-05-10 23:36:49 +01:00
|
|
|
{
|
2022-12-15 22:21:07 +00:00
|
|
|
FilesystemContents Files(IReadOnlyFilesystem filesystem)
|
2019-05-10 23:36:49 +01:00
|
|
|
{
|
2022-12-15 22:21:07 +00:00
|
|
|
var contents = new FilesystemContents();
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-12-21 20:03:24 +00:00
|
|
|
ErrorNumber ret = filesystem.OpenDir("/", out IDirNode node);
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(ret != ErrorNumber.NoError) return null;
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2024-05-01 04:39:38 +01:00
|
|
|
List<Directory> directories = [];
|
|
|
|
|
List<ContentsFile> files = [];
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2023-10-04 17:34:40 +01:00
|
|
|
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)
|
2019-05-10 23:36:49 +01:00
|
|
|
{
|
2023-10-03 17:17:16 +01:00
|
|
|
AaruConsole.DebugWriteLine(MODULE_NAME, Localization.Core.Cannot_stat_0, dirent);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-05-10 23:36:49 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
files.Add(SidecarFile(filesystem, "", dirent, stat));
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-12-21 20:03:24 +00:00
|
|
|
filesystem.CloseDir(node);
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(files.Count > 0) contents.Files = files.OrderBy(f => f.Name).ToList();
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(directories.Count > 0) contents.Directories = directories.OrderBy(d => d.Name).ToList();
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return contents;
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-12-15 22:21:07 +00:00
|
|
|
Directory SidecarDirectory(IReadOnlyFilesystem filesystem, string path, string filename, FileEntryInfo stat)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2022-12-15 22:21:07 +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
|
|
|
|
|
};
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-12-21 20:03:24 +00:00
|
|
|
ErrorNumber ret = filesystem.OpenDir(path + "/" + filename, out IDirNode node);
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(ret != ErrorNumber.NoError) return null;
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2024-05-01 04:39:38 +01:00
|
|
|
List<Directory> directories = [];
|
|
|
|
|
List<ContentsFile> files = [];
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2023-10-04 17:34:40 +01:00
|
|
|
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);
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
if(ret != ErrorNumber.NoError)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2023-10-03 17:17:16 +01:00
|
|
|
AaruConsole.DebugWriteLine(MODULE_NAME, Localization.Core.Cannot_stat_0, dirent);
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(entryStat.Attributes.HasFlag(FileAttributes.Directory))
|
2019-05-10 23:36:49 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
directories.Add(SidecarDirectory(filesystem, path + "/" + filename, dirent, entryStat));
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
files.Add(SidecarFile(filesystem, path + "/" + filename, dirent, entryStat));
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(files.Count > 0) directory.Files = files.OrderBy(f => f.Name).ToList();
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(directories.Count > 0) directory.Directories = directories.OrderBy(d => d.Name).ToList();
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return directory;
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-12-15 22:21:07 +00:00
|
|
|
ContentsFile SidecarFile(IReadOnlyFilesystem filesystem, string path, string filename, FileEntryInfo stat)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
|
|
|
|
var fileChkWorker = new Checksum();
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-12-15 22:21:07 +00:00
|
|
|
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
|
|
|
|
|
};
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-12-19 11:03:51 +00:00
|
|
|
byte[] data = null;
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(stat.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
long position = 0;
|
2022-11-23 16:06:46 +00:00
|
|
|
UpdateStatus(string.Format(Localization.Core.Hashing_file_0_1, path, filename));
|
2022-03-06 13:29:38 +00:00
|
|
|
InitProgress2();
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-12-19 11:03:51 +00:00
|
|
|
ErrorNumber error = filesystem.OpenFile(path + "/" + filename, out IFileNode fileNode);
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-12-19 11:03:51 +00:00
|
|
|
if(error == ErrorNumber.NoError)
|
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
data = new byte[1048576];
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-12-19 11:03:51 +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
|
|
|
|
2022-12-19 11:03:51 +00:00
|
|
|
// TODO: Better error handling
|
|
|
|
|
filesystem.ReadFile(fileNode, 1048576, data, out _);
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-12-19 11:03:51 +00:00
|
|
|
UpdateProgress2(Localization.Core.Hashing_file_byte_0_of_1, position, stat.Length);
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-12-19 11:03:51 +00:00
|
|
|
fileChkWorker.Update(data);
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-12-19 11:03:51 +00:00
|
|
|
position += 1048576;
|
|
|
|
|
}
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-12-19 11:03:51 +00:00
|
|
|
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);
|
|
|
|
|
}
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
EndProgress2();
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-12-15 22:21:07 +00:00
|
|
|
file.Checksums = fileChkWorker.End();
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
file.Checksums = _emptyChecksums;
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
ErrorNumber ret = filesystem.ListXAttr(path + "/" + filename, out List<string> xattrs);
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(ret != ErrorNumber.NoError) return file;
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2024-05-01 04:39:38 +01:00
|
|
|
List<ExtendedAttribute> xattrTypes = [];
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
foreach(string xattr in xattrs)
|
|
|
|
|
{
|
|
|
|
|
ret = filesystem.GetXattr(path + "/" + filename, xattr, ref data);
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(ret != ErrorNumber.NoError) continue;
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
var xattrChkWorker = new Checksum();
|
|
|
|
|
xattrChkWorker.Update(data);
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-12-15 22:21:07 +00:00
|
|
|
xattrTypes.Add(new ExtendedAttribute
|
2019-05-10 23:36:49 +01:00
|
|
|
{
|
2022-12-15 22:21:07 +00:00
|
|
|
Checksums = xattrChkWorker.End(),
|
|
|
|
|
Length = (ulong)data.Length,
|
|
|
|
|
Name = xattr
|
2022-03-06 13:29:38 +00:00
|
|
|
});
|
|
|
|
|
}
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(xattrTypes.Count > 0) file.ExtendedAttributes = xattrTypes.OrderBy(x => x.Name).ToList();
|
2019-05-10 23:36:49 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return file;
|
2019-05-10 23:36:49 +01:00
|
|
|
}
|
|
|
|
|
}
|