2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2016-07-27 02:35:29 +01:00
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// 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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2018-12-29 17:34:38 +00:00
|
|
|
// Copyright © 2011-2019 Natalia Portillo
|
2016-07-27 02:35:29 +01:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2017-12-19 19:33:46 +00:00
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using DiscImageChef.CommonTypes;
|
2019-01-05 19:50:56 +00:00
|
|
|
using DiscImageChef.CommonTypes.Enums;
|
2018-06-25 19:08:16 +01:00
|
|
|
using DiscImageChef.CommonTypes.Interfaces;
|
|
|
|
|
using DiscImageChef.CommonTypes.Structs;
|
2016-07-27 02:35:29 +01:00
|
|
|
using DiscImageChef.Console;
|
2017-12-19 19:33:46 +00:00
|
|
|
using DiscImageChef.Core;
|
2019-01-05 16:59:23 +00:00
|
|
|
using Mono.Options;
|
2016-07-27 02:35:29 +01:00
|
|
|
|
|
|
|
|
namespace DiscImageChef.Commands
|
|
|
|
|
{
|
2019-01-05 16:59:23 +00:00
|
|
|
class ExtractFilesCommand : Command
|
2016-07-27 02:35:29 +01:00
|
|
|
{
|
2019-01-05 16:59:23 +00:00
|
|
|
string encodingName;
|
|
|
|
|
bool extractXattrs;
|
|
|
|
|
string inputFile;
|
|
|
|
|
string outputDir;
|
|
|
|
|
string pluginOptions;
|
|
|
|
|
bool showHelp;
|
|
|
|
|
|
|
|
|
|
public ExtractFilesCommand() : base("extract-files", "Extracts all files in disc image.")
|
2016-07-27 02:35:29 +01:00
|
|
|
{
|
2019-01-05 16:59:23 +00:00
|
|
|
Options = new OptionSet
|
|
|
|
|
{
|
|
|
|
|
$"{MainClass.AssemblyTitle} {MainClass.AssemblyVersion?.InformationalVersion}",
|
|
|
|
|
$"{MainClass.AssemblyCopyright}",
|
|
|
|
|
"",
|
|
|
|
|
$"usage: DiscImageChef {Name} [OPTIONS] imagefile",
|
|
|
|
|
"",
|
|
|
|
|
Help,
|
|
|
|
|
{"encoding|e=", "Name of character encoding to use.", s => encodingName = s},
|
|
|
|
|
{
|
|
|
|
|
"options|O=", "Comma separated name=value pairs of options to pass to filesystem plugin.",
|
|
|
|
|
s => pluginOptions = s
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"output|o=", "Directory where extracted files will be created. Will abort if it exists.",
|
|
|
|
|
s => outputDir = s
|
|
|
|
|
},
|
|
|
|
|
{"xattrs|x", "Extract extended attributes if present.", b => extractXattrs = b != null},
|
|
|
|
|
{"help|h|?", "Show this message and exit.", v => showHelp = v != null}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int Invoke(IEnumerable<string> arguments)
|
|
|
|
|
{
|
|
|
|
|
List<string> extra = Options.Parse(arguments);
|
|
|
|
|
|
|
|
|
|
if(showHelp)
|
|
|
|
|
{
|
|
|
|
|
Options.WriteOptionDescriptions(CommandSet.Out);
|
2019-01-05 19:50:56 +00:00
|
|
|
return (int)ErrorNumber.HelpRequested;
|
2019-01-05 16:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MainClass.PrintCopyright();
|
|
|
|
|
if(MainClass.Debug) DicConsole.DebugWriteLineEvent += System.Console.Error.WriteLine;
|
|
|
|
|
if(MainClass.Verbose) DicConsole.VerboseWriteLineEvent += System.Console.WriteLine;
|
2019-01-05 20:21:57 +00:00
|
|
|
Statistics.AddCommand("extract-files");
|
2019-01-05 16:59:23 +00:00
|
|
|
|
|
|
|
|
if(extra.Count > 1)
|
|
|
|
|
{
|
|
|
|
|
DicConsole.ErrorWriteLine("Too many arguments.");
|
2019-01-05 19:50:56 +00:00
|
|
|
return (int)ErrorNumber.UnexpectedArgumentCount;
|
2019-01-05 16:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(extra.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
DicConsole.ErrorWriteLine("Missing input image.");
|
2019-01-05 19:50:56 +00:00
|
|
|
return (int)ErrorNumber.MissingArgument;
|
2019-01-05 16:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inputFile = extra[0];
|
|
|
|
|
|
|
|
|
|
DicConsole.DebugWriteLine("Extract-Files command", "--debug={0}", MainClass.Debug);
|
|
|
|
|
DicConsole.DebugWriteLine("Extract-Files command", "--encoding={0}", encodingName);
|
|
|
|
|
DicConsole.DebugWriteLine("Extract-Files command", "--input={0}", inputFile);
|
|
|
|
|
DicConsole.DebugWriteLine("Extract-Files command", "--options={0}", pluginOptions);
|
|
|
|
|
DicConsole.DebugWriteLine("Extract-Files command", "--output={0}", outputDir);
|
|
|
|
|
DicConsole.DebugWriteLine("Extract-Files command", "--verbose={0}", MainClass.Verbose);
|
|
|
|
|
DicConsole.DebugWriteLine("Extract-Files command", "--xattrs={0}", extractXattrs);
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
FiltersList filtersList = new FiltersList();
|
2019-01-05 16:59:23 +00:00
|
|
|
IFilter inputFilter = filtersList.GetFilter(inputFile);
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2019-01-05 16:59:23 +00:00
|
|
|
Dictionary<string, string> parsedOptions = Core.Options.Parse(pluginOptions);
|
2018-01-17 19:10:46 +00:00
|
|
|
DicConsole.DebugWriteLine("Extract-Files command", "Parsed options:");
|
2018-06-22 08:08:38 +01:00
|
|
|
foreach(KeyValuePair<string, string> parsedOption in parsedOptions)
|
2018-01-17 19:10:46 +00:00
|
|
|
DicConsole.DebugWriteLine("Extract-Files command", "{0} = {1}", parsedOption.Key, parsedOption.Value);
|
2019-01-05 16:59:23 +00:00
|
|
|
parsedOptions.Add("debug", MainClass.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
|
|
|
{
|
2016-09-05 17:37:31 +01:00
|
|
|
DicConsole.ErrorWriteLine("Cannot open specified file.");
|
2019-01-05 19:50:56 +00:00
|
|
|
return (int)ErrorNumber.CannotOpenFile;
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
2017-10-12 23:54:02 +01:00
|
|
|
Encoding encoding = null;
|
|
|
|
|
|
2019-01-05 16:59:23 +00:00
|
|
|
if(encodingName != null)
|
2017-10-12 23:54:02 +01:00
|
|
|
try
|
|
|
|
|
{
|
2019-01-05 16:59:23 +00:00
|
|
|
encoding = Claunia.Encoding.Encoding.GetEncoding(encodingName);
|
|
|
|
|
if(MainClass.Verbose) DicConsole.VerboseWriteLine("Using encoding for {0}.", encoding.EncodingName);
|
2017-10-12 23:54:02 +01:00
|
|
|
}
|
|
|
|
|
catch(ArgumentException)
|
|
|
|
|
{
|
|
|
|
|
DicConsole.ErrorWriteLine("Specified encoding is not supported.");
|
2019-01-05 19:50:56 +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
|
|
|
{
|
|
|
|
|
DicConsole.WriteLine("Image format not identified, not proceeding with analysis.");
|
2019-01-05 19:50:56 +00:00
|
|
|
return (int)ErrorNumber.UnrecognizedFormat;
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
2017-12-21 02:57:32 +00:00
|
|
|
|
2019-01-05 16:59:23 +00:00
|
|
|
if(MainClass.Verbose)
|
2017-12-21 02:57:32 +00:00
|
|
|
DicConsole.VerboseWriteLine("Image format identified by {0} ({1}).", imageFormat.Name,
|
2017-12-26 06:05:12 +00:00
|
|
|
imageFormat.Id);
|
2017-12-21 02:57:32 +00:00
|
|
|
else DicConsole.WriteLine("Image format identified by {0}.", imageFormat.Name);
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2019-01-05 16:59:23 +00:00
|
|
|
if(Directory.Exists(outputDir) || File.Exists(outputDir))
|
2016-07-27 02:35:29 +01:00
|
|
|
{
|
|
|
|
|
DicConsole.ErrorWriteLine("Destination exists, aborting.");
|
2019-01-05 19:50:56 +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
|
|
|
{
|
|
|
|
|
DicConsole.WriteLine("Unable to open image format");
|
|
|
|
|
DicConsole.WriteLine("No error given");
|
2019-01-05 19:50:56 +00:00
|
|
|
return (int)ErrorNumber.CannotOpenFormat;
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DicConsole.DebugWriteLine("Extract-Files command", "Correctly opened image file.");
|
2017-12-19 20:33:03 +00:00
|
|
|
DicConsole.DebugWriteLine("Extract-Files command", "Image without headers is {0} bytes.",
|
2017-12-26 06:05:12 +00:00
|
|
|
imageFormat.Info.ImageSize);
|
2017-12-19 20:33:03 +00:00
|
|
|
DicConsole.DebugWriteLine("Extract-Files command", "Image has {0} sectors.",
|
2017-12-26 06:05:12 +00:00
|
|
|
imageFormat.Info.Sectors);
|
2017-12-19 20:33:03 +00:00
|
|
|
DicConsole.DebugWriteLine("Extract-Files command", "Image identifies disk type as {0}.",
|
2017-12-26 06:05:12 +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)
|
|
|
|
|
{
|
|
|
|
|
DicConsole.ErrorWriteLine("Unable to open image format");
|
|
|
|
|
DicConsole.ErrorWriteLine("Error: {0}", ex.Message);
|
2019-01-05 19:50:56 +00:00
|
|
|
return (int)ErrorNumber.CannotOpenFormat;
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-20 17:15:26 +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;
|
2017-12-19 20:33:03 +00:00
|
|
|
if(partitions.Count == 0) DicConsole.DebugWriteLine("Extract-Files command", "No partitions found");
|
2016-07-27 02:35:29 +01:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DicConsole.WriteLine("{0} partitions found.", partitions.Count);
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < partitions.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
DicConsole.WriteLine();
|
2017-07-19 16:37:11 +01:00
|
|
|
DicConsole.WriteLine("Partition {0}:", partitions[i].Sequence);
|
2016-07-27 02:35:29 +01:00
|
|
|
|
|
|
|
|
DicConsole.WriteLine("Identifying filesystem on partition");
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
Core.Filesystems.Identify(imageFormat, out idPlugins, partitions[i]);
|
2017-12-27 23:55:20 +00:00
|
|
|
if(idPlugins.Count == 0) DicConsole.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
|
|
|
{
|
2017-12-21 17:58:51 +00:00
|
|
|
DicConsole.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
|
|
|
{
|
2017-12-21 17:58:51 +00:00
|
|
|
DicConsole.WriteLine($"As identified by {plugin.Name}.");
|
2017-12-26 07:23:09 +00:00
|
|
|
IReadOnlyFilesystem fs = (IReadOnlyFilesystem)plugin
|
2017-12-27 23:55:20 +00:00
|
|
|
.GetType()
|
|
|
|
|
.GetConstructor(Type.EmptyTypes)
|
|
|
|
|
?.Invoke(new object[] { });
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2018-01-17 19:10:46 +00:00
|
|
|
error = fs.Mount(imageFormat, partitions[i], encoding, parsedOptions);
|
2016-07-27 02:35:29 +01:00
|
|
|
if(error == Errno.NoError)
|
|
|
|
|
{
|
2019-04-14 12:23:40 +01:00
|
|
|
string volumeName =
|
|
|
|
|
string.IsNullOrEmpty(fs.XmlFsType.VolumeName)
|
|
|
|
|
? "NO NAME"
|
|
|
|
|
: fs.XmlFsType.VolumeName;
|
2017-12-07 02:30:54 +00:00
|
|
|
|
2019-04-14 12:23:40 +01:00
|
|
|
ExtractFilesInDir("/", fs, volumeName);
|
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
|
2017-12-19 20:33:03 +00:00
|
|
|
DicConsole.ErrorWriteLine("Unable to mount device, error {0}",
|
|
|
|
|
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);
|
2017-12-21 17:58:51 +00:00
|
|
|
DicConsole.WriteLine($"Identified by {plugin.Name}.");
|
2017-12-26 07:23:09 +00:00
|
|
|
IReadOnlyFilesystem fs = (IReadOnlyFilesystem)plugin
|
2017-12-27 23:55:20 +00:00
|
|
|
.GetType().GetConstructor(Type.EmptyTypes)
|
|
|
|
|
?.Invoke(new object[] { });
|
2018-01-17 19:10:46 +00:00
|
|
|
error = fs.Mount(imageFormat, partitions[i], encoding, parsedOptions);
|
2016-07-27 02:35:29 +01:00
|
|
|
if(error == Errno.NoError)
|
|
|
|
|
{
|
2019-04-14 12:23:40 +01:00
|
|
|
string volumeName = string.IsNullOrEmpty(fs.XmlFsType.VolumeName)
|
|
|
|
|
? "NO NAME"
|
|
|
|
|
: fs.XmlFsType.VolumeName;
|
2017-12-07 02:30:54 +00:00
|
|
|
|
2019-04-14 12:23:40 +01:00
|
|
|
ExtractFilesInDir("/", fs, volumeName);
|
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
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
else DicConsole.ErrorWriteLine("Unable to mount device, error {0}", error.ToString());
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-19 16:31:08 +01:00
|
|
|
Partition wholePart = new Partition
|
|
|
|
|
{
|
2017-12-27 23:55:20 +00:00
|
|
|
Name = "Whole device",
|
2017-12-26 06:05:12 +00:00
|
|
|
Length = imageFormat.Info.Sectors,
|
2017-12-27 23:55:20 +00:00
|
|
|
Size = imageFormat.Info.Sectors * imageFormat.Info.SectorSize
|
2017-07-19 16:31:08 +01:00
|
|
|
};
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
Core.Filesystems.Identify(imageFormat, out idPlugins, wholePart);
|
2017-12-27 23:55:20 +00:00
|
|
|
if(idPlugins.Count == 0) DicConsole.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
|
|
|
{
|
2017-12-21 17:58:51 +00:00
|
|
|
DicConsole.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
|
|
|
{
|
2017-12-21 17:58:51 +00:00
|
|
|
DicConsole.WriteLine($"As identified by {plugin.Name}.");
|
2017-12-26 07:23:09 +00:00
|
|
|
IReadOnlyFilesystem fs = (IReadOnlyFilesystem)plugin
|
2017-12-27 23:55:20 +00:00
|
|
|
.GetType().GetConstructor(Type.EmptyTypes)
|
|
|
|
|
?.Invoke(new object[] { });
|
2018-01-17 19:10:46 +00:00
|
|
|
error = fs.Mount(imageFormat, wholePart, encoding, parsedOptions);
|
2016-07-27 02:35:29 +01:00
|
|
|
if(error == Errno.NoError)
|
|
|
|
|
{
|
2019-04-14 12:23:40 +01:00
|
|
|
string volumeName = string.IsNullOrEmpty(fs.XmlFsType.VolumeName)
|
|
|
|
|
? "NO NAME"
|
|
|
|
|
: fs.XmlFsType.VolumeName;
|
2017-12-07 02:30:54 +00:00
|
|
|
|
2019-04-14 12:23:40 +01:00
|
|
|
ExtractFilesInDir("/", fs, volumeName);
|
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
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
else DicConsole.ErrorWriteLine("Unable to mount device, error {0}", 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);
|
2017-12-21 17:58:51 +00:00
|
|
|
DicConsole.WriteLine($"Identified by {plugin.Name}.");
|
2017-12-27 23:55:20 +00:00
|
|
|
IReadOnlyFilesystem fs =
|
|
|
|
|
(IReadOnlyFilesystem)plugin.GetType().GetConstructor(Type.EmptyTypes)?.Invoke(new object[] { });
|
2018-01-17 19:10:46 +00:00
|
|
|
error = fs.Mount(imageFormat, wholePart, encoding, parsedOptions);
|
2016-07-27 02:35:29 +01:00
|
|
|
if(error == Errno.NoError)
|
|
|
|
|
{
|
2019-04-14 12:23:40 +01:00
|
|
|
string volumeName = string.IsNullOrEmpty(fs.XmlFsType.VolumeName)
|
|
|
|
|
? "NO NAME"
|
|
|
|
|
: fs.XmlFsType.VolumeName;
|
|
|
|
|
|
|
|
|
|
ExtractFilesInDir("/", fs, volumeName);
|
|
|
|
|
|
|
|
|
|
Statistics.AddFilesystem(fs.XmlFsType.Type);
|
|
|
|
|
}
|
|
|
|
|
else DicConsole.ErrorWriteLine("Unable to mount device, error {0}", error.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
DicConsole.ErrorWriteLine($"Error reading file: {ex.Message}");
|
|
|
|
|
DicConsole.DebugWriteLine("Extract-Files command", ex.StackTrace);
|
|
|
|
|
return (int)ErrorNumber.UnexpectedException;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (int)ErrorNumber.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExtractFilesInDir(string path, IReadOnlyFilesystem fs, string volumeName)
|
|
|
|
|
{
|
|
|
|
|
Errno error = fs.ReadDir(path, out List<string> directory);
|
|
|
|
|
if(error != Errno.NoError)
|
|
|
|
|
{
|
|
|
|
|
DicConsole.ErrorWriteLine("Error {0} reading root directory {0}", error.ToString());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach(string entry in directory)
|
|
|
|
|
{
|
|
|
|
|
error = fs.Stat(entry, out FileEntryInfo stat);
|
|
|
|
|
if(error == Errno.NoError)
|
|
|
|
|
{
|
|
|
|
|
string outputPath;
|
|
|
|
|
FileStream outputFile;
|
|
|
|
|
if(extractXattrs)
|
|
|
|
|
{
|
|
|
|
|
error = fs.ListXAttr(entry, out List<string> xattrs);
|
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];
|
|
|
|
|
error = fs.GetXattr(entry, xattr, ref xattrBuf);
|
|
|
|
|
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,
|
|
|
|
|
entry);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
outputFile.Write(xattrBuf, 0, xattrBuf.Length);
|
|
|
|
|
outputFile.Close();
|
|
|
|
|
FileInfo fi = new FileInfo(outputPath);
|
|
|
|
|
#pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
|
|
|
|
|
try { fi.CreationTimeUtc = stat.CreationTimeUtc; }
|
|
|
|
|
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
|
|
|
try { fi.LastWriteTimeUtc = stat.LastWriteTimeUtc; }
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
2016-07-27 02:35:29 +01:00
|
|
|
|
2019-04-14 12:23:40 +01:00
|
|
|
try { fi.LastAccessTimeUtc = stat.AccessTimeUtc; }
|
|
|
|
|
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
|
|
|
|
|
DicConsole.WriteLine("Written {0} bytes of xattr {1} from file {2} to {3}",
|
|
|
|
|
xattrBuf.Length, xattr, entry, outputPath);
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
2019-04-14 12:23:40 +01:00
|
|
|
else
|
|
|
|
|
DicConsole.ErrorWriteLine("Cannot write xattr {0} for {1}, output exists", xattr,
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
|
|
outputPath = Path.Combine(outputDir, fs.XmlFsType.Type, volumeName, entry);
|
|
|
|
|
|
|
|
|
|
if(!File.Exists(outputPath))
|
|
|
|
|
{
|
|
|
|
|
byte[] outBuf = new byte[0];
|
|
|
|
|
|
|
|
|
|
error = fs.Read(entry, 0, stat.Length, ref outBuf);
|
|
|
|
|
|
|
|
|
|
if(error == Errno.NoError)
|
|
|
|
|
{
|
|
|
|
|
outputFile = new FileStream(outputPath, FileMode.CreateNew, FileAccess.ReadWrite,
|
|
|
|
|
FileShare.None);
|
|
|
|
|
outputFile.Write(outBuf, 0, outBuf.Length);
|
|
|
|
|
outputFile.Close();
|
|
|
|
|
FileInfo fi = new FileInfo(outputPath);
|
|
|
|
|
#pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
|
|
|
|
|
try { fi.CreationTimeUtc = stat.CreationTimeUtc; }
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try { fi.LastWriteTimeUtc = stat.LastWriteTimeUtc; }
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try { fi.LastAccessTimeUtc = stat.AccessTimeUtc; }
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
|
|
|
|
#pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
|
|
|
|
|
DicConsole.WriteLine("Written {0} bytes of file {1} to {2}", outBuf.Length, entry,
|
|
|
|
|
outputPath);
|
|
|
|
|
}
|
|
|
|
|
else DicConsole.ErrorWriteLine("Error {0} reading file {1}", error, entry);
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
2019-04-14 12:23:40 +01:00
|
|
|
else DicConsole.ErrorWriteLine("Cannot write file {0}, output exists", entry);
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
2019-04-14 12:23:40 +01:00
|
|
|
else DicConsole.ErrorWriteLine("Error reading file {0}", entry);
|
2016-07-27 02:35:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|