2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2016-07-27 02:35:29 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : ExtractFiles.cs
|
2016-07-28 18:13:49 +01:00
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
2016-07-27 02:35:29 +01:00
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// Component : Verbs.
|
2016-07-27 02:35:29 +01:00
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// Implements the 'extract-files' verb.
|
2016-07-27 02:35:29 +01:00
|
|
|
//
|
|
|
|
|
// --[ 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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-01-03 17:51:30 +00:00
|
|
|
// Copyright © 2011-2020 Natalia Portillo
|
2016-07-27 02:35:29 +01:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-01-02 04:09:39 +00:00
|
|
|
using System.CommandLine;
|
|
|
|
|
using System.CommandLine.Invocation;
|
2017-12-19 19:33:46 +00:00
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.CommonTypes;
|
|
|
|
|
using Aaru.CommonTypes.Enums;
|
|
|
|
|
using Aaru.CommonTypes.Interfaces;
|
|
|
|
|
using Aaru.CommonTypes.Structs;
|
|
|
|
|
using Aaru.Console;
|
|
|
|
|
using Aaru.Core;
|
|
|
|
|
using FileAttributes = Aaru.CommonTypes.Structs.FileAttributes;
|
|
|
|
|
|
|
|
|
|
namespace Aaru.Commands.Filesystem
|
2016-07-27 02:35:29 +01:00
|
|
|
{
|
2020-01-02 04:09:39 +00:00
|
|
|
internal class ExtractFilesCommand : Command
|
2016-07-27 02:35:29 +01:00
|
|
|
{
|
2020-01-03 18:24:44 +00:00
|
|
|
public ExtractFilesCommand() : base("extract", "Extracts all files in disc image.")
|
2016-07-27 02:35:29 +01:00
|
|
|
{
|
2020-01-02 04:09:39 +00:00
|
|
|
Add(new Option(new[]
|
2019-01-05 16:59:23 +00:00
|
|
|
{
|
2020-01-02 04:09:39 +00:00
|
|
|
"--encoding", "-e"
|
|
|
|
|
}, "Name of character encoding to use.")
|
2019-01-05 16:59:23 +00:00
|
|
|
{
|
2020-01-02 04:09:39 +00:00
|
|
|
Argument = new Argument<string>(() => null), Required = false
|
|
|
|
|
});
|
2019-01-05 16:59:23 +00:00
|
|
|
|
2020-01-02 04:09:39 +00:00
|
|
|
Add(new Option(new[]
|
|
|
|
|
{
|
|
|
|
|
"--options", "-O"
|
|
|
|
|
}, "Comma separated name=value pairs of options to pass to filesystem plugin.")
|
|
|
|
|
{
|
|
|
|
|
Argument = new Argument<string>(() => null), Required = false
|
|
|
|
|
});
|
2019-01-05 16:59:23 +00:00
|
|
|
|
2020-01-02 04:09:39 +00:00
|
|
|
Add(new Option(new[]
|
|
|
|
|
{
|
|
|
|
|
"--xattrs", "-x"
|
|
|
|
|
}, "Extract extended attributes if present.")
|
|
|
|
|
{
|
|
|
|
|
Argument = new Argument<bool>(() => false), Required = false
|
|
|
|
|
});
|
2019-01-05 16:59:23 +00:00
|
|
|
|
2020-01-02 04:09:39 +00:00
|
|
|
Add(new Option(new[]
|
|
|
|
|
{
|
|
|
|
|
"--namespace", "-n"
|
|
|
|
|
}, "Namespace to use for filenames.")
|
|
|
|
|
{
|
|
|
|
|
Argument = new Argument<string>(() => null), Required = false
|
|
|
|
|
});
|
2019-01-05 16:59:23 +00:00
|
|
|
|
2020-01-02 04:09:39 +00:00
|
|
|
AddArgument(new Argument<string>
|
2019-01-05 16:59:23 +00:00
|
|
|
{
|
2020-01-02 04:09:39 +00:00
|
|
|
Arity = ArgumentArity.ExactlyOne, Description = "Disc image path", Name = "image-path"
|
|
|
|
|
});
|
2019-01-05 16:59:23 +00:00
|
|
|
|
2020-01-02 04:09:39 +00:00
|
|
|
AddArgument(new Argument<string>
|
2019-01-05 16:59:23 +00:00
|
|
|
{
|
2020-01-02 04:09:39 +00:00
|
|
|
Arity = ArgumentArity.ExactlyOne,
|
|
|
|
|
Description = "Directory where extracted files will be created. Will abort if it exists",
|
|
|
|
|
Name = "output-dir"
|
|
|
|
|
});
|
2019-01-05 16:59:23 +00:00
|
|
|
|
2020-01-02 04:09:39 +00:00
|
|
|
Handler = CommandHandler.Create(GetType().GetMethod(nameof(Invoke)));
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 17:41:19 +00:00
|
|
|
public static int Invoke(bool debug, bool verbose, string encoding, bool xattrs, string imagePath,
|
|
|
|
|
string @namespace, string outputDir, string options)
|
2020-01-02 04:09:39 +00:00
|
|
|
{
|
|
|
|
|
MainClass.PrintCopyright();
|
|
|
|
|
|
|
|
|
|
if(debug)
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.DebugWriteLineEvent += System.Console.Error.WriteLine;
|
2020-01-02 04:09:39 +00:00
|
|
|
|
|
|
|
|
if(verbose)
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.VerboseWriteLineEvent += System.Console.WriteLine;
|
2020-01-02 04:09:39 +00:00
|
|
|
|
|
|
|
|
Statistics.AddCommand("extract-files");
|
2019-01-05 16:59:23 +00:00
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.DebugWriteLine("Extract-Files command", "--debug={0}", debug);
|
|
|
|
|
AaruConsole.DebugWriteLine("Extract-Files command", "--encoding={0}", encoding);
|
|
|
|
|
AaruConsole.DebugWriteLine("Extract-Files command", "--input={0}", imagePath);
|
|
|
|
|
AaruConsole.DebugWriteLine("Extract-Files command", "--options={0}", options);
|
|
|
|
|
AaruConsole.DebugWriteLine("Extract-Files command", "--output={0}", outputDir);
|
|
|
|
|
AaruConsole.DebugWriteLine("Extract-Files command", "--verbose={0}", verbose);
|
|
|
|
|
AaruConsole.DebugWriteLine("Extract-Files command", "--xattrs={0}", xattrs);
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2020-01-02 04:09:39 +00:00
|
|
|
var filtersList = new FiltersList();
|
|
|
|
|
IFilter inputFilter = filtersList.GetFilter(imagePath);
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
Dictionary<string, string> parsedOptions = Core.Options.Parse(options);
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.DebugWriteLine("Extract-Files command", "Parsed options:");
|
2020-01-02 04:09:39 +00:00
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
foreach(KeyValuePair<string, string> parsedOption in parsedOptions)
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.DebugWriteLine("Extract-Files command", "{0} = {1}", parsedOption.Key, parsedOption.Value);
|
2020-01-02 04:09:39 +00:00
|
|
|
|
|
|
|
|
parsedOptions.Add("debug", debug.ToString());
|
2018-06-22 08:08:38 +01:00
|
|
|
|
2016-09-05 17:37:31 +01:00
|
|
|
if(inputFilter == null)
|
2016-07-27 02:35:29 +01:00
|
|
|
{
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.ErrorWriteLine("Cannot open specified file.");
|
2020-01-02 04:09:39 +00:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
return (int)ErrorNumber.CannotOpenFile;
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-02 04:09:39 +00:00
|
|
|
Encoding encodingClass = null;
|
2017-10-12 23:54:02 +01:00
|
|
|
|
2020-01-02 04:09:39 +00:00
|
|
|
if(encoding != null)
|
2017-10-12 23:54:02 +01:00
|
|
|
try
|
|
|
|
|
{
|
2020-01-02 04:09:39 +00:00
|
|
|
encodingClass = Claunia.Encoding.Encoding.GetEncoding(encoding);
|
|
|
|
|
|
|
|
|
|
if(verbose)
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.VerboseWriteLine("Using encoding for {0}.", encodingClass.EncodingName);
|
2017-10-12 23:54:02 +01:00
|
|
|
}
|
|
|
|
|
catch(ArgumentException)
|
|
|
|
|
{
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.ErrorWriteLine("Specified encoding is not supported.");
|
2020-01-02 04:09:39 +00:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
return (int)ErrorNumber.EncodingUnknown;
|
2017-10-12 23:54:02 +01:00
|
|
|
}
|
|
|
|
|
|
2018-07-20 22:53:46 +01:00
|
|
|
PluginBase plugins = GetPluginBase.Instance;
|
2016-07-27 02:35:29 +01:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
IMediaImage imageFormat = ImageFormat.Detect(inputFilter);
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
if(imageFormat == null)
|
2016-07-27 02:35:29 +01:00
|
|
|
{
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.WriteLine("Image format not identified, not proceeding with analysis.");
|
2020-01-02 04:09:39 +00:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
return (int)ErrorNumber.UnrecognizedFormat;
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
2017-12-21 02:57:32 +00:00
|
|
|
|
2020-01-02 04:09:39 +00:00
|
|
|
if(verbose)
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.VerboseWriteLine("Image format identified by {0} ({1}).", imageFormat.Name,
|
2020-02-29 18:03:35 +00:00
|
|
|
imageFormat.Id);
|
2020-01-02 04:09:39 +00:00
|
|
|
else
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.WriteLine("Image format identified by {0}.", imageFormat.Name);
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2020-01-02 04:09:39 +00:00
|
|
|
if(outputDir == null)
|
2019-10-05 13:49:21 +02:00
|
|
|
{
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.WriteLine("Output directory missing.");
|
2020-01-02 04:09:39 +00:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
return (int)ErrorNumber.MissingArgument;
|
2019-10-05 13:49:21 +02:00
|
|
|
}
|
|
|
|
|
|
2020-01-02 04:09:39 +00:00
|
|
|
if(Directory.Exists(outputDir) ||
|
|
|
|
|
File.Exists(outputDir))
|
2016-07-27 02:35:29 +01:00
|
|
|
{
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.ErrorWriteLine("Destination exists, aborting.");
|
2020-01-02 04:09:39 +00:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
return (int)ErrorNumber.DestinationExists;
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-05 16:59:23 +00:00
|
|
|
Directory.CreateDirectory(outputDir);
|
2016-07-27 02:35:29 +01:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-12-28 19:56:36 +00:00
|
|
|
if(!imageFormat.Open(inputFilter))
|
2016-07-27 02:35:29 +01:00
|
|
|
{
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.WriteLine("Unable to open image format");
|
|
|
|
|
AaruConsole.WriteLine("No error given");
|
2020-01-02 04:09:39 +00:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
return (int)ErrorNumber.CannotOpenFormat;
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.DebugWriteLine("Extract-Files command", "Correctly opened image file.");
|
2020-01-02 04:09:39 +00:00
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.DebugWriteLine("Extract-Files command", "Image without headers is {0} bytes.",
|
2020-02-29 18:03:35 +00:00
|
|
|
imageFormat.Info.ImageSize);
|
2020-01-02 04:09:39 +00:00
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.DebugWriteLine("Extract-Files command", "Image has {0} sectors.",
|
2020-02-29 18:03:35 +00:00
|
|
|
imageFormat.Info.Sectors);
|
2020-01-02 04:09:39 +00:00
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.DebugWriteLine("Extract-Files command", "Image identifies disk type as {0}.",
|
2020-02-29 18:03:35 +00:00
|
|
|
imageFormat.Info.MediaType);
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2019-01-05 16:59:23 +00:00
|
|
|
Statistics.AddMediaFormat(imageFormat.Format);
|
|
|
|
|
Statistics.AddMedia(imageFormat.Info.MediaType, false);
|
|
|
|
|
Statistics.AddFilter(inputFilter.Name);
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.ErrorWriteLine("Unable to open image format");
|
|
|
|
|
AaruConsole.ErrorWriteLine("Error: {0}", ex.Message);
|
2020-01-02 04:09:39 +00:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
return (int)ErrorNumber.CannotOpenFormat;
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
List<Partition> partitions = Core.Partitions.GetAll(imageFormat);
|
|
|
|
|
Core.Partitions.AddSchemesToStats(partitions);
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2017-12-27 23:55:20 +00:00
|
|
|
List<string> idPlugins;
|
2017-12-26 07:23:09 +00:00
|
|
|
IReadOnlyFilesystem plugin;
|
2017-12-27 23:55:20 +00:00
|
|
|
Errno error;
|
2020-01-02 04:09:39 +00:00
|
|
|
|
|
|
|
|
if(partitions.Count == 0)
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.DebugWriteLine("Extract-Files command", "No partitions found");
|
2016-07-27 02:35:29 +01:00
|
|
|
else
|
|
|
|
|
{
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.WriteLine("{0} partitions found.", partitions.Count);
|
2016-07-27 02:35:29 +01:00
|
|
|
|
|
|
|
|
for(int i = 0; i < partitions.Count; i++)
|
|
|
|
|
{
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.WriteLine();
|
|
|
|
|
AaruConsole.WriteLine("Partition {0}:", partitions[i].Sequence);
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.WriteLine("Identifying filesystem on partition");
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
Core.Filesystems.Identify(imageFormat, out idPlugins, partitions[i]);
|
2020-01-02 04:09:39 +00:00
|
|
|
|
|
|
|
|
if(idPlugins.Count == 0)
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.WriteLine("Filesystem not identified");
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(idPlugins.Count > 1)
|
2016-07-27 02:35:29 +01:00
|
|
|
{
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.WriteLine($"Identified by {idPlugins.Count} plugins");
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
foreach(string pluginName in idPlugins)
|
2017-12-26 07:23:09 +00:00
|
|
|
if(plugins.ReadOnlyFilesystems.TryGetValue(pluginName, out plugin))
|
2016-07-27 02:35:29 +01:00
|
|
|
{
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.WriteLine($"As identified by {plugin.Name}.");
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2020-01-02 04:09:39 +00:00
|
|
|
var fs = (IReadOnlyFilesystem)plugin.
|
|
|
|
|
GetType().GetConstructor(Type.EmptyTypes)?.
|
|
|
|
|
Invoke(new object[]
|
2020-02-29 18:03:35 +00:00
|
|
|
{});
|
2020-01-02 04:09:39 +00:00
|
|
|
|
|
|
|
|
error = fs.Mount(imageFormat, partitions[i], encodingClass, parsedOptions,
|
|
|
|
|
@namespace);
|
|
|
|
|
|
2016-07-27 02:35:29 +01:00
|
|
|
if(error == Errno.NoError)
|
|
|
|
|
{
|
2019-04-14 12:23:40 +01:00
|
|
|
string volumeName =
|
2020-01-02 04:09:39 +00:00
|
|
|
string.IsNullOrEmpty(fs.XmlFsType.VolumeName) ? "NO NAME"
|
2019-04-14 12:23:40 +01:00
|
|
|
: fs.XmlFsType.VolumeName;
|
2017-12-07 02:30:54 +00:00
|
|
|
|
2020-01-02 04:09:39 +00:00
|
|
|
ExtractFilesInDir("/", fs, volumeName, outputDir, xattrs);
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2019-01-05 16:59:23 +00:00
|
|
|
Statistics.AddFilesystem(fs.XmlFsType.Type);
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
|
|
|
|
else
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.ErrorWriteLine("Unable to mount device, error {0}",
|
2020-02-29 18:03:35 +00:00
|
|
|
error.ToString());
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-12-26 07:23:09 +00:00
|
|
|
plugins.ReadOnlyFilesystems.TryGetValue(idPlugins[0], out plugin);
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.WriteLine($"Identified by {plugin.Name}.");
|
2020-01-02 04:09:39 +00:00
|
|
|
|
|
|
|
|
var fs = (IReadOnlyFilesystem)plugin.
|
|
|
|
|
GetType().GetConstructor(Type.EmptyTypes)?.Invoke(new object[]
|
2020-02-29 18:03:35 +00:00
|
|
|
{});
|
2020-01-02 04:09:39 +00:00
|
|
|
|
|
|
|
|
error = fs.Mount(imageFormat, partitions[i], encodingClass, parsedOptions, @namespace);
|
|
|
|
|
|
2016-07-27 02:35:29 +01:00
|
|
|
if(error == Errno.NoError)
|
|
|
|
|
{
|
2020-01-02 04:09:39 +00:00
|
|
|
string volumeName = string.IsNullOrEmpty(fs.XmlFsType.VolumeName) ? "NO NAME"
|
2019-04-14 12:23:40 +01:00
|
|
|
: fs.XmlFsType.VolumeName;
|
2017-12-07 02:30:54 +00:00
|
|
|
|
2020-01-02 04:09:39 +00:00
|
|
|
ExtractFilesInDir("/", fs, volumeName, outputDir, xattrs);
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2019-01-05 16:59:23 +00:00
|
|
|
Statistics.AddFilesystem(fs.XmlFsType.Type);
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
2020-01-02 04:09:39 +00:00
|
|
|
else
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.ErrorWriteLine("Unable to mount device, error {0}", error.ToString());
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-14 12:23:40 +01:00
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.ErrorWriteLine($"Error reading file: {ex.Message}");
|
|
|
|
|
AaruConsole.DebugWriteLine("Extract-Files command", ex.StackTrace);
|
2020-01-02 04:09:39 +00:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
return (int)ErrorNumber.UnexpectedException;
|
2019-04-14 12:23:40 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
return (int)ErrorNumber.NoError;
|
2019-04-14 12:23:40 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-02 04:09:39 +00:00
|
|
|
static void ExtractFilesInDir(string path, IReadOnlyFilesystem fs, string volumeName, string outputDir,
|
|
|
|
|
bool doXattrs)
|
2019-04-14 12:23:40 +01:00
|
|
|
{
|
2020-01-02 04:09:39 +00:00
|
|
|
if(path.StartsWith("/"))
|
|
|
|
|
path = path.Substring(1);
|
2019-04-14 13:03:45 +01:00
|
|
|
|
2019-04-14 12:23:40 +01:00
|
|
|
Errno error = fs.ReadDir(path, out List<string> directory);
|
2020-01-02 04:09:39 +00:00
|
|
|
|
2019-04-14 12:23:40 +01:00
|
|
|
if(error != Errno.NoError)
|
|
|
|
|
{
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.ErrorWriteLine("Error {0} reading root directory {0}", error.ToString());
|
2020-01-02 04:09:39 +00:00
|
|
|
|
2019-04-14 12:23:40 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach(string entry in directory)
|
|
|
|
|
{
|
2019-04-14 13:03:45 +01:00
|
|
|
error = fs.Stat(path + "/" + entry, out FileEntryInfo stat);
|
2020-01-02 04:09:39 +00:00
|
|
|
|
2019-04-14 12:23:40 +01:00
|
|
|
if(error == Errno.NoError)
|
|
|
|
|
{
|
2019-04-14 13:03:45 +01:00
|
|
|
string outputPath;
|
|
|
|
|
|
|
|
|
|
if(stat.Attributes.HasFlag(FileAttributes.Directory))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(Path.Combine(outputDir, fs.XmlFsType.Type, volumeName));
|
|
|
|
|
|
|
|
|
|
outputPath = Path.Combine(outputDir, fs.XmlFsType.Type, volumeName, path, entry);
|
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(outputPath);
|
|
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.WriteLine("Created subdirectory at {0}", outputPath);
|
2019-04-14 13:03:45 +01:00
|
|
|
|
2020-01-02 04:09:39 +00:00
|
|
|
ExtractFilesInDir(path + "/" + entry, fs, volumeName, outputDir, doXattrs);
|
2019-04-14 13:03:45 +01:00
|
|
|
|
2020-01-02 04:09:39 +00:00
|
|
|
var di = new DirectoryInfo(outputPath);
|
2019-04-14 13:03:45 +01:00
|
|
|
|
|
|
|
|
#pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
|
2019-04-22 23:06:47 +01:00
|
|
|
try
|
|
|
|
|
{
|
2020-01-02 04:09:39 +00:00
|
|
|
if(stat.CreationTimeUtc.HasValue)
|
|
|
|
|
di.CreationTimeUtc = stat.CreationTimeUtc.Value;
|
2019-04-22 23:06:47 +01:00
|
|
|
}
|
2019-04-14 13:03:45 +01:00
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-22 23:06:47 +01:00
|
|
|
try
|
|
|
|
|
{
|
2020-01-02 04:09:39 +00:00
|
|
|
if(stat.LastWriteTimeUtc.HasValue)
|
|
|
|
|
di.LastWriteTimeUtc = stat.LastWriteTimeUtc.Value;
|
2019-04-22 23:06:47 +01:00
|
|
|
}
|
2019-04-14 13:03:45 +01:00
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-22 23:06:47 +01:00
|
|
|
try
|
|
|
|
|
{
|
2020-01-02 04:09:39 +00:00
|
|
|
if(stat.AccessTimeUtc.HasValue)
|
|
|
|
|
di.LastAccessTimeUtc = stat.AccessTimeUtc.Value;
|
2019-04-22 23:06:47 +01:00
|
|
|
}
|
2019-04-14 13:03:45 +01:00
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
|
|
|
|
#pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-14 12:23:40 +01:00
|
|
|
FileStream outputFile;
|
2020-01-02 04:09:39 +00:00
|
|
|
|
|
|
|
|
if(doXattrs)
|
2019-04-14 12:23:40 +01:00
|
|
|
{
|
2019-04-14 13:03:45 +01:00
|
|
|
error = fs.ListXAttr(path + "/" + entry, out List<string> xattrs);
|
2020-01-02 04:09:39 +00:00
|
|
|
|
2016-07-27 02:35:29 +01:00
|
|
|
if(error == Errno.NoError)
|
2019-04-14 12:23:40 +01:00
|
|
|
foreach(string xattr in xattrs)
|
2016-07-27 02:35:29 +01:00
|
|
|
{
|
2019-04-14 12:23:40 +01:00
|
|
|
byte[] xattrBuf = new byte[0];
|
2019-04-14 13:03:45 +01:00
|
|
|
error = fs.GetXattr(path + "/" + entry, xattr, ref xattrBuf);
|
2020-01-02 04:09:39 +00:00
|
|
|
|
|
|
|
|
if(error != Errno.NoError)
|
|
|
|
|
continue;
|
2016-08-26 01:43:15 +01:00
|
|
|
|
2019-04-14 12:23:40 +01:00
|
|
|
Directory.CreateDirectory(Path.Combine(outputDir, fs.XmlFsType.Type, volumeName,
|
|
|
|
|
".xattrs", xattr));
|
|
|
|
|
|
|
|
|
|
outputPath = Path.Combine(outputDir, fs.XmlFsType.Type, volumeName, ".xattrs", xattr,
|
2019-04-14 13:03:45 +01:00
|
|
|
path, entry);
|
2019-04-14 12:23:40 +01:00
|
|
|
|
|
|
|
|
if(!File.Exists(outputPath))
|
2016-07-27 02:35:29 +01:00
|
|
|
{
|
2019-04-14 12:23:40 +01:00
|
|
|
outputFile = new FileStream(outputPath, FileMode.CreateNew, FileAccess.ReadWrite,
|
|
|
|
|
FileShare.None);
|
2020-01-02 04:09:39 +00:00
|
|
|
|
2019-04-14 12:23:40 +01:00
|
|
|
outputFile.Write(xattrBuf, 0, xattrBuf.Length);
|
|
|
|
|
outputFile.Close();
|
2020-01-02 04:09:39 +00:00
|
|
|
var fi = new FileInfo(outputPath);
|
2019-04-14 12:23:40 +01:00
|
|
|
#pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
|
2019-04-22 23:06:47 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if(stat.CreationTimeUtc.HasValue)
|
|
|
|
|
fi.CreationTimeUtc = stat.CreationTimeUtc.Value;
|
|
|
|
|
}
|
2019-04-14 12:23:40 +01:00
|
|
|
catch
|
2016-07-27 02:35:29 +01:00
|
|
|
{
|
2019-04-14 12:23:40 +01:00
|
|
|
// ignored
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
2019-04-22 23:06:47 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if(stat.LastWriteTimeUtc.HasValue)
|
|
|
|
|
fi.LastWriteTimeUtc = stat.LastWriteTimeUtc.Value;
|
|
|
|
|
}
|
2019-04-14 12:23:40 +01:00
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2019-04-22 23:06:47 +01:00
|
|
|
try
|
|
|
|
|
{
|
2020-01-02 04:09:39 +00:00
|
|
|
if(stat.AccessTimeUtc.HasValue)
|
|
|
|
|
fi.LastAccessTimeUtc = stat.AccessTimeUtc.Value;
|
2019-04-22 23:06:47 +01:00
|
|
|
}
|
2019-04-14 12:23:40 +01:00
|
|
|
catch
|
2016-07-27 02:35:29 +01:00
|
|
|
{
|
2019-04-14 12:23:40 +01:00
|
|
|
// ignored
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
2019-04-14 12:23:40 +01:00
|
|
|
#pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.WriteLine("Written {0} bytes of xattr {1} from file {2} to {3}",
|
2020-02-29 18:03:35 +00:00
|
|
|
xattrBuf.Length, xattr, entry, outputPath);
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
2019-04-14 12:23:40 +01:00
|
|
|
else
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.ErrorWriteLine("Cannot write xattr {0} for {1}, output exists", xattr,
|
2020-02-29 18:03:35 +00:00
|
|
|
entry);
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
2019-04-14 12:23:40 +01:00
|
|
|
}
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2019-04-14 12:23:40 +01:00
|
|
|
Directory.CreateDirectory(Path.Combine(outputDir, fs.XmlFsType.Type, volumeName));
|
|
|
|
|
|
2019-04-14 13:03:45 +01:00
|
|
|
outputPath = Path.Combine(outputDir, fs.XmlFsType.Type, volumeName, path, entry);
|
2019-04-14 12:23:40 +01:00
|
|
|
|
|
|
|
|
if(!File.Exists(outputPath))
|
|
|
|
|
{
|
|
|
|
|
byte[] outBuf = new byte[0];
|
|
|
|
|
|
2019-04-14 13:03:45 +01:00
|
|
|
error = fs.Read(path + "/" + entry, 0, stat.Length, ref outBuf);
|
2019-04-14 12:23:40 +01:00
|
|
|
|
|
|
|
|
if(error == Errno.NoError)
|
|
|
|
|
{
|
|
|
|
|
outputFile = new FileStream(outputPath, FileMode.CreateNew, FileAccess.ReadWrite,
|
|
|
|
|
FileShare.None);
|
2020-01-02 04:09:39 +00:00
|
|
|
|
2019-04-14 12:23:40 +01:00
|
|
|
outputFile.Write(outBuf, 0, outBuf.Length);
|
|
|
|
|
outputFile.Close();
|
2020-01-02 04:09:39 +00:00
|
|
|
var fi = new FileInfo(outputPath);
|
2019-04-14 12:23:40 +01:00
|
|
|
#pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
|
2019-04-22 23:06:47 +01:00
|
|
|
try
|
|
|
|
|
{
|
2020-01-02 04:09:39 +00:00
|
|
|
if(stat.CreationTimeUtc.HasValue)
|
|
|
|
|
fi.CreationTimeUtc = stat.CreationTimeUtc.Value;
|
2019-04-22 23:06:47 +01:00
|
|
|
}
|
2019-04-14 12:23:40 +01:00
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-22 23:06:47 +01:00
|
|
|
try
|
|
|
|
|
{
|
2020-01-02 04:09:39 +00:00
|
|
|
if(stat.LastWriteTimeUtc.HasValue)
|
|
|
|
|
fi.LastWriteTimeUtc = stat.LastWriteTimeUtc.Value;
|
2019-04-22 23:06:47 +01:00
|
|
|
}
|
2019-04-14 12:23:40 +01:00
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-22 23:06:47 +01:00
|
|
|
try
|
|
|
|
|
{
|
2020-01-02 04:09:39 +00:00
|
|
|
if(stat.AccessTimeUtc.HasValue)
|
|
|
|
|
fi.LastAccessTimeUtc = stat.AccessTimeUtc.Value;
|
2019-04-22 23:06:47 +01:00
|
|
|
}
|
2019-04-14 12:23:40 +01:00
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
|
|
|
|
#pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.WriteLine("Written {0} bytes of file {1} to {2}", outBuf.Length, entry,
|
2020-02-29 18:03:35 +00:00
|
|
|
outputPath);
|
2019-04-14 12:23:40 +01:00
|
|
|
}
|
2020-01-02 04:09:39 +00:00
|
|
|
else
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.ErrorWriteLine("Error {0} reading file {1}", error, entry);
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
2020-01-02 04:09:39 +00:00
|
|
|
else
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.ErrorWriteLine("Cannot write file {0}, output exists", entry);
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
2020-01-02 04:09:39 +00:00
|
|
|
else
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.ErrorWriteLine("Error reading file {0}", entry);
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-02 04:09:39 +00:00
|
|
|
}
|