Skip to content

Commit

Permalink
Added the hash only option
Browse files Browse the repository at this point in the history
  • Loading branch information
TechieGuy12 committed Jun 17, 2022
1 parent 528694d commit 3a2b7d7
Showing 1 changed file with 89 additions and 28 deletions.
117 changes: 89 additions & 28 deletions FileVerification/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ static int Main(string[] args)
);
rootCommand.AddOption(threadsOption);

var getHashOnlyOption = new Option<bool>(
aliases: new string[] { "--hashonly", "-ho" },
description: "Generate and display the file hash."
);
rootCommand.AddOption(getHashOnlyOption);

var settingsFileOption = new Option<string>(
aliases: new string[] { "--settingsFile", "-sfi" },
description: "The name of the settings XML file."
Expand All @@ -70,11 +76,34 @@ static int Main(string[] args)
);
rootCommand.AddOption(settingsFolderOption);

rootCommand.SetHandler((fileOptionValue, algorithmOptionValue, hashOptionValue, threadsOptionValue, settingsFileOptionValue, settingsFolderOptionValue) =>
{
Run(fileOptionValue, algorithmOptionValue, hashOptionValue, threadsOptionValue, settingsFileOptionValue, settingsFolderOptionValue);
},
fileOption, algorithmOption, hashOption, threadsOption, settingsFileOption, settingsFolderOption);
rootCommand.SetHandler(
(
fileOptionValue,
algorithmOptionValue,
hashOptionValue,
getHashOnlyOptionValue,
threadsOptionValue,
settingsFileOptionValue,
settingsFolderOptionValue
) =>
{
Run(
fileOptionValue,
algorithmOptionValue,
hashOptionValue,
getHashOnlyOptionValue,
threadsOptionValue,
settingsFileOptionValue,
settingsFolderOptionValue);
},
fileOption,
algorithmOption,
hashOption,
getHashOnlyOption,
threadsOption,
settingsFileOption,
settingsFolderOption
);
return rootCommand.Invoke(args);
}

Expand All @@ -86,7 +115,14 @@ static int Main(string[] args)
/// <param name="settingsFile"></param>
/// <param name="settingsFolder"></param>
/// <returns></returns>
static int Run(string? file, HashAlgorithm? algorithm, string hashOption, int? threads, string? settingsFile, string? settingsFolder)
static int Run(
string? file,
HashAlgorithm? algorithm,
string hashOption,
bool hashOnlyOption,
int? threads,
string? settingsFile,
string? settingsFolder)
{
try
{
Expand All @@ -110,22 +146,31 @@ static int Run(string? file, HashAlgorithm? algorithm, string hashOption, int? t
threads = 1;
}

// Read the settings file if one was provided as an argument
Settings? settings = null;
if (!string.IsNullOrWhiteSpace(settingsFile) && !string.IsNullOrWhiteSpace(settingsFolder))
// Trim the double-quote from the path, since it can cause an
// issue if the path ends with a slash ('\'), because the code
// will interpret the slash and double-quote as an escape
// character for the double quote ('\"' to '"')
file = file.Trim('"');

// If the hash option has not been specified, or the hash only
// option is false then continue with cralwing the directory to
// generate and verify the hashes of the files
if (string.IsNullOrWhiteSpace(hashOption) && !hashOnlyOption)
{
ISettingsFile xmlFile = new XmlFile(settingsFolder, settingsFile);
settings = xmlFile.Read();
}
// Read the settings file if one was provided as an argument
Settings? settings = null;
if (!string.IsNullOrWhiteSpace(settingsFile) && !string.IsNullOrWhiteSpace(settingsFolder))
{
ISettingsFile xmlFile = new XmlFile(settingsFolder, settingsFile);
settings = xmlFile.Read();
}

Logger.WriteLine("--------------------------------------------------------------------------------");
Logger.WriteLine($"Folder/File: {file}");
Logger.WriteLine($"Hash Algorithm: {algorithm}");
Logger.WriteLine($"Threads: {threads}");
Logger.WriteLine("--------------------------------------------------------------------------------");
Logger.WriteLine("--------------------------------------------------------------------------------");
Logger.WriteLine($"Folder/File: {file}");
Logger.WriteLine($"Hash Algorithm: {algorithm}");
Logger.WriteLine($"Threads: {threads}");
Logger.WriteLine("--------------------------------------------------------------------------------");

if (string.IsNullOrWhiteSpace(hashOption))
{
PathInfo path = new PathInfo(file);
Stopwatch watch = new Stopwatch();
watch.Start();
Expand All @@ -142,7 +187,7 @@ static int Run(string? file, HashAlgorithm? algorithm, string hashOption, int? t
Logger.WriteLine("--------------------------------------------------------------------------------");
}

// If settings gdwere specified, then send the notifications
// If settings were specified, then send the notifications
if (settings != null)
{
settings.Send();
Expand All @@ -165,18 +210,34 @@ static int Run(string? file, HashAlgorithm? algorithm, string hashOption, int? t
return ERROR_NO_HASH;
}

int returnValue = string.Compare(fileHash, hashOption, true) == 0 ? SUCCESS : ERROR_HASH_NOT_MATCH;

if (returnValue == SUCCESS)
// If the hash only option was specified, then just display
// the hash of the file
if (hashOnlyOption)
{
Logger.WriteLine($"The file hash matches the hash '{hashOption}'");
Logger.WriteLine(fileHash);
return SUCCESS;
}
else

// The the hash option was specified, compare the file hash
// with the hash passed through the argument
if (!string.IsNullOrWhiteSpace(hashOption))
{
Logger.WriteLine($"The file hash '{fileHash}' does not match the hash '{hashOption}'");
}
return returnValue;
int returnValue = string.Compare(fileHash, hashOption, true) == 0 ? SUCCESS : ERROR_HASH_NOT_MATCH;

if (returnValue == SUCCESS)
{
Logger.WriteLine($"The file hash matches the hash '{hashOption}'");
}
else
{
Logger.WriteLine($"The file hash '{fileHash}' does not match the hash '{hashOption}'");
}

return returnValue;
}
}

return SUCCESS;
}
catch (Exception ex)
{
Expand Down

0 comments on commit 3a2b7d7

Please sign in to comment.