Replaced static field with optional parameter to switch verbose debugging off.

Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
This commit is contained in:
Konstantina Chremmou 2022-05-04 21:49:25 +01:00
parent a98d516bbe
commit 3a59c2490c
3 changed files with 34 additions and 22 deletions

View File

@ -73,12 +73,19 @@ namespace CommandLib
private readonly XXHash64 _xxHash = new XXHash64(); 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) _quiet = quiet;
Console.WriteLine(x); }
private void Debug(string x)
{
if (_quiet)
return;
Console.WriteLine(x);
} }
private string checksum_sha1(byte[] data) private string checksum_sha1(byte[] data)
@ -108,7 +115,7 @@ namespace CommandLib
return hex(_xxHash.ComputeHash(data)); 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(); Hashtable table = new Hashtable();
@ -130,14 +137,14 @@ namespace CommandLib
value = v.Value; value = v.Value;
} }
debug(String.Format("adding {0} = {1}", name, value)); Debug(String.Format("adding {0} = {1}", name, value));
table.Add(name, value); table.Add(name, value);
} }
return table; return table;
} }
private static void compare_tables(Hashtable recomputed, Hashtable original) private void compare_tables(Hashtable recomputed, Hashtable original)
{ {
foreach (DictionaryEntry x in recomputed) foreach (DictionaryEntry x in recomputed)
{ {
@ -149,7 +156,7 @@ namespace CommandLib
} }
else else
{ {
debug(String.Format("{0} hash OK", (string)x.Key)); Debug(String.Format("{0} hash OK", (string)x.Key));
} }
} }
} }
@ -209,13 +216,13 @@ namespace CommandLib
while (!cancelling()) while (!cancelling())
{ {
Header header_data = nextHeader(input, output, callback); 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); byte[] bytes_data = nextData(input, output, callback, header_data.file_size);
if (header_data.file_name.Equals("ova.xml")) 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")) else if (header_data.file_name.EndsWith("checksum.xml"))
{ {
@ -234,7 +241,7 @@ namespace CommandLib
if (!csum.Equals(csum_compare)) if (!csum.Equals(csum_compare))
throw new BlockChecksumFailed(header_data.file_name, csum, 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); recomputed_checksums.Add(header_data.file_name, csum);
nextData(input, output, callback, header_checksum.paddingLength()); // Eat the padding for the checksum file nextData(input, output, callback, header_checksum.paddingLength()); // Eat the padding for the checksum file
@ -245,7 +252,7 @@ namespace CommandLib
} }
catch (EndOfArchive) catch (EndOfArchive)
{ {
debug("EOF"); Debug("EOF");
if (original_checksums != null) if (original_checksums != null)
compare_tables(recomputed_checksums, original_checksums); compare_tables(recomputed_checksums, original_checksums);
} }

View File

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

View File

@ -62,7 +62,7 @@
<None Include="app.config" /> <None Include="app.config" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <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. <!-- 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. Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild"> <Target Name="BeforeBuild">