Merge pull request #3067 from kc284/xva-verify

xva_verify: some unused code removal; added cli switch.
This commit is contained in:
Konstantina Chremmou 2022-12-14 00:59:48 +00:00 committed by GitHub
commit e74232dc15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 93 deletions

View File

@ -73,12 +73,19 @@ namespace CommandLib
private readonly XXHash64 _xxHash = new XXHash64();
public static bool verbose_debugging = false;
private readonly bool _quiet;
private static void debug(string x)
public Export(bool quiet = false)
{
if (verbose_debugging)
Console.WriteLine(x);
_quiet = quiet;
}
private void Debug(string x)
{
if (_quiet)
return;
Console.WriteLine(x);
}
private string checksum_sha1(byte[] data)
@ -108,7 +115,7 @@ namespace CommandLib
return hex(_xxHash.ComputeHash(data));
}
private static Hashtable parse_checksum_table(string checksum_xml)
private Hashtable parse_checksum_table(string checksum_xml)
{
Hashtable table = new Hashtable();
@ -130,14 +137,14 @@ namespace CommandLib
value = v.Value;
}
debug(String.Format("adding {0} = {1}", name, value));
Debug(String.Format("adding {0} = {1}", name, value));
table.Add(name, value);
}
return table;
}
private static void compare_tables(Hashtable recomputed, Hashtable original)
private void compare_tables(Hashtable recomputed, Hashtable original)
{
foreach (DictionaryEntry x in recomputed)
{
@ -149,7 +156,7 @@ namespace CommandLib
}
else
{
debug(String.Format("{0} hash OK", (string)x.Key));
Debug(String.Format("{0} hash OK", (string)x.Key));
}
}
}
@ -162,20 +169,16 @@ namespace CommandLib
return new string(chars);
}
public delegate void verifyCallback(uint read);
public delegate bool cancellingCallback();
/// <summary>
/// 'input' is the source of the export data, if 'output' is not null then
/// a perfect copy should be echoed there.
/// </summary>
public void verify(Stream input, Stream output, cancellingCallback cancelling)
public void verify(Stream input, Stream output, Func<bool> cancelling)
{
verify(input, output, cancelling, null);
}
private Header nextHeader(Stream input, Stream output, verifyCallback callback)
private Header nextHeader(Stream input, Stream output, Action<uint> callback)
{
// Interperate the next bytes from the stream as a Tar header
byte[] bytes = nextData(input, output, callback, Header.length);
@ -194,7 +197,7 @@ namespace CommandLib
return new Header(bytes);
}
private byte[] nextData(Stream input, Stream output, verifyCallback callback, uint size)
private byte[] nextData(Stream input, Stream output, Action<uint> callback, uint size)
{
// Returns the next given number of bytes from the input
byte[] bytes = IO.unmarshal_n(input, size);
@ -203,7 +206,7 @@ namespace CommandLib
return bytes;
}
public void verify(Stream input, Stream output, cancellingCallback cancelling, verifyCallback callback)
public void verify(Stream input, Stream output, Func<bool> cancelling, Action<uint> callback)
{
Hashtable recomputed_checksums = new Hashtable();
Hashtable original_checksums = null;
@ -213,13 +216,13 @@ namespace CommandLib
while (!cancelling())
{
Header header_data = nextHeader(input, output, callback);
debug(header_data.ToString());
Debug(header_data.ToString());
byte[] bytes_data = nextData(input, output, callback, header_data.file_size);
if (header_data.file_name.Equals("ova.xml"))
{
debug("Skipping ova.xml");
Debug("Skipping ova.xml");
}
else if (header_data.file_name.EndsWith("checksum.xml"))
{
@ -238,7 +241,7 @@ namespace CommandLib
if (!csum.Equals(csum_compare))
throw new BlockChecksumFailed(header_data.file_name, csum, csum_compare);
debug(String.Format(" has checksum: {0}", csum));
Debug(String.Format(" has checksum: {0}", csum));
recomputed_checksums.Add(header_data.file_name, csum);
nextData(input, output, callback, header_checksum.paddingLength()); // Eat the padding for the checksum file
@ -249,7 +252,7 @@ namespace CommandLib
}
catch (EndOfArchive)
{
debug("EOF");
Debug("EOF");
if (original_checksums != null)
compare_tables(recomputed_checksums, original_checksums);
}

View File

@ -58,17 +58,5 @@ namespace CommandLib
unmarshal_n_into(stream, n, buffer);
return buffer;
}
public static void skip(Stream stream, uint n)
{
byte[] buffer = new byte[63356];
while (n > 0)
{
uint toread = (uint)buffer.Length;
if (n < toread) toread = n;
unmarshal_n_into(stream, toread, buffer);
n -= toread;
}
}
}
}

View File

@ -30,7 +30,6 @@
*/
using System;
using System.IO;
using System.Text;
namespace CommandLib
@ -63,10 +62,6 @@ namespace CommandLib
/// </summary>
class EndOfArchive : ApplicationException
{
public EndOfArchive()
{
}
public override string ToString()
{
return "End of tar archive";
@ -234,44 +229,5 @@ namespace CommandLib
throw new HeaderChecksumFailed(recomputed, chksum);
}
/// <summary>
/// Read a tar header from a stream
/// </summary>
public static Header fromStream(Stream input)
{
byte[] one = IO.unmarshal_n(input, length);
if (all_zeroes(one))
{
byte[] two = IO.unmarshal_n(input, length);
if (all_zeroes(two))
throw new EndOfArchive();
return new Header(two);
}
return new Header(one);
}
}
public class Archive
{
public static void list(Stream stream)
{
try
{
while (true)
{
Header x = Header.fromStream(stream);
Console.WriteLine(x);
IO.skip(stream, x.file_size);
IO.skip(stream, x.paddingLength());
}
}
catch (EndOfArchive)
{
Console.WriteLine("EOF");
}
}
}
}

View File

@ -110,7 +110,7 @@ namespace XenAdmin.Actions
log.DebugFormat("Exporting {0} to {1}", VM.Name(), _filename);
// The DownloadFile call will block, so we need a separate thread to poll for task status.
Thread taskThread = new Thread(progressPoll);
Thread taskThread = new Thread(ProgressPoll);
taskThread.Name = "Progress polling thread for ExportVmAction for " + VM.Name().Ellipsise(20);
taskThread.IsBackground = true;
taskThread.Start();
@ -156,7 +156,7 @@ namespace XenAdmin.Actions
int i = 0;
long filesize = new FileInfo(tmpFile).Length / 50; //Div by 50 to save doing the * 50 in the callback
Export.verifyCallback callback = size =>
void Callback(uint size)
{
read += size;
i++;
@ -168,7 +168,7 @@ namespace XenAdmin.Actions
PercentComplete = 50 + (int)(read / filesize);
i = 0;
}
};
}
try
{
@ -178,7 +178,7 @@ namespace XenAdmin.Actions
this.Description = Messages.ACTION_EXPORT_VERIFY;
export = new Export();
export.verify(fs, null, () => Cancelling, callback);
export.verify(fs, null, () => Cancelling, Callback);
}
}
catch (Exception e)
@ -246,8 +246,6 @@ namespace XenAdmin.Actions
}
}
private void HttpGet(string filename, Uri uri)
{
using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
@ -259,7 +257,7 @@ namespace XenAdmin.Actions
}
}
private void progressPoll()
private void ProgressPoll()
{
try
{

View File

@ -32,6 +32,7 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using CommandLib;
@ -41,30 +42,34 @@ namespace xva_verify
{
public static void Main(string[] args)
{
Export.verbose_debugging = true;
if (args.Length < 1 || args.Length > 2)
if (args.Length < 1 || args.Length > 3)
{
var sb = new StringBuilder();
sb.AppendLine();
sb.AppendLine("Usage").AppendLine();
sb.AppendLine(" xva_verify <archive> [<copy>]").AppendLine();
sb.AppendLine(" xva_verify <archive> [<copy> -q]").AppendLine();
sb.AppendLine("where").AppendLine();
sb.AppendLine(" <archive> The name of the archive file to verify. Use '-' to read from stdin.");
sb.AppendLine(" <copy> If specified, a copy of the archive file is created with this name.").AppendLine();
sb.AppendLine(" <copy> If specified, a copy of the archive file is created with this name.");
sb.AppendLine(" -q If specified, it switches off verbose debugging.");
sb.AppendLine();
Console.WriteLine(sb.ToString());
Environment.Exit(1);
}
bool quiet = args.Contains("-q");
var fileArgs = args.Where(a => a != "-q").ToArray();
try
{
string filename = args[0];
string filename = fileArgs[0];
Stream g = null;
if (args.Length == 2)
g = new FileStream(args[1], FileMode.Create);
if (fileArgs.Length > 1)
g = new FileStream(fileArgs[1], FileMode.Create);
Stream f = args[0].Equals("-")
Stream f = fileArgs[0].Equals("-")
? Console.OpenStandardInput()
: new FileStream(filename, FileMode.Open, FileAccess.Read);
@ -88,7 +93,7 @@ namespace xva_verify
}
}
new Export().verify(f, g, () => false);
new Export(quiet).verify(f, g, () => false);
}
catch(UnauthorizedAccessException)
{

View File

@ -62,7 +62,7 @@
<None Include="app.config" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)AddManifest.targets"/>
<Import Project="$(SolutionDir)AddManifest.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">