code cleanup

made garbage code less garbage
This commit is contained in:
AustrianToast 2023-07-04 22:39:59 +02:00
parent 7b00a87620
commit 90706e7796
No known key found for this signature in database
GPG Key ID: 5CD422268E489EB4
2 changed files with 40 additions and 36 deletions

View File

@ -19,20 +19,23 @@ public class Program {
utils.ExtractEmbeddedLibrary();
var option = args[0].ToLower();
Console.ForegroundColor = ConsoleColor.Green;
switch (args[0]) {
switch (option) {
case "checksum":
Console.WriteLine("Starting the checksum process.");
Console.ResetColor();
var hashAlgo = args[1].ToLower();
try {
if (args[1] == "MD5") {
utils.doTheThing(args[1]);
}
int bufferSize = int.Parse(args[2]);
utils.doTheThing(args[1], bufferSize);
if (hashAlgo.Equals("md5")) {
utils.doTheThing(hashAlgo);
break;
}
catch (FormatException) {
try {
var bufferSize = int.Parse(args[2]);
utils.doTheThing(hashAlgo, bufferSize);
} catch (FormatException) {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Buffer was not a valid integer value. Please specify a valid integer value for the buffer size");
Console.ResetColor();
@ -41,16 +44,16 @@ public class Program {
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Checksum process finished");
break;
case "saveToSqlite":
case "savetosqlite":
Console.ResetColor();
utils.saveToSqlite();
break;
case "compareDatabases":
case "comparedatabases":
Console.ResetColor();
utils.compareDatabases(args[1]);
break;
case "checkIfFileWasDeleted":
case "checkiffilewasdeleted":
Console.ResetColor();
utils.checkIfFileWasDeleted();
break;
@ -71,7 +74,6 @@ public class Program {
String[] options = {
"checksum - MD5, Murmur and XxHash - Default buffer size is 4096",
"compareDatabases",
"compareChecksums",
"saveToSqlite",
"checkIfFileWasDeleted",
"help"
@ -79,7 +81,7 @@ public class Program {
Console.ResetColor();
Console.WriteLine("usage: chksum [option] \nHere is a list of all available options:");
foreach (String option in options) {
foreach (var option in options) {
Console.WriteLine("\t" + option);
}
}

View File

@ -99,7 +99,7 @@ public class ChksumUtils {
}
private void UpdateProgressBar(int current, int total) {
int progress = (int)((double)current / total * 100);
var progress = (int)((double)current / total * 100);
string progressText = $"Progress: {progress}% [{current}/{total}]";
Console.Write("\r" + progressText.PadRight(Console.WindowWidth));
@ -199,7 +199,7 @@ public class ChksumUtils {
return hash;
}
public void doTheThing(string hashalgo, int bufferSize = 4096) {
public void doTheThing(string hashAlgo, int bufferSize = 4096) {
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
IDatabase db = redis.GetDatabase();
@ -214,22 +214,22 @@ public class ChksumUtils {
Dictionary<string, uint> fileHashesMurmur;
Dictionary<string, string> fileHashesMD5;
switch (hashalgo) {
case "MD5":
switch (hashAlgo) {
case "md5":
fileHashesMD5 = CalculateChecksums(indexFiles());
fileHashes = fileHashesMD5.ToDictionary(kv => kv.Key, kv => (object)kv.Value);
break;
case "Murmur":
case "murmur":
fileHashesMurmur = CalculateChecksumsWithMurmur(indexFiles(), bufferSize);
fileHashes = fileHashesMurmur.ToDictionary(kv => kv.Key, kv => (object)kv.Value);
break;
case "XxHash":
case "xxhash":
fileHashesXxHash3 = CalculateChecksumsWithXxHash3(indexFiles(), bufferSize);
fileHashes = fileHashesXxHash3.ToDictionary(kv => kv.Key, kv => (object)kv.Value);
break;
default:
logger.Error("No valid hash algorithm was selected");
throw new Exception($"{hashalgo} is not a valid option. Valid options are MD5, Murmur and XxHash");
throw new Exception($"{hashAlgo} is not a valid option. Valid options are MD5, Murmur and XxHash");
}
logger.Information("All files were checksummed");
@ -241,7 +241,6 @@ public class ChksumUtils {
}
public void saveToSqlite() {
initializeDB();
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
@ -251,27 +250,29 @@ public class ChksumUtils {
logger.Information("Retrived all values from redis");
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
foreach (var file in fileHashes) {
connection.Open();
foreach (var file in fileHashes) {
var absolutePathToFile = file.Name.ToString();
string fileName = Path.GetFileName(absolutePathToFile.ToString());
string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile.ToString());
var fileHash = file.Value.ToString();
if (!checkIfFileMovedAndUpdatePathToFile(fileHash, fileName, pathToFile) && !checkIfFileAlreadyExistsInDatabase(fileHash, fileName)) {
connection.Open();
var InsertCommand = connection.CreateCommand();
InsertCommand.CommandText =
@"
INSERT INTO file (filehash, filename, pathtofile)
VALUES ($filehash, $filename, $pathtofile)
";
InsertCommand.Parameters.AddWithValue("$filehash", fileHash);
InsertCommand.Parameters.AddWithValue("$filename", fileName);
InsertCommand.Parameters.AddWithValue("$pathtofile", pathToFile);
InsertCommand.ExecuteNonQuery();
logger.Verbose("{fileName} which is located at {pathToFile} relative to the database with the hash {fileHash} was successfully inserted into the database", fileName, pathToFile, fileHash);
if (checkIfFileMovedAndUpdatePathToFile(fileHash, fileName, pathToFile) || checkIfFileAlreadyExistsInDatabase(fileHash, fileName)) {
continue;
}
var InsertCommand = connection.CreateCommand();
InsertCommand.CommandText =
@"
INSERT INTO file (filehash, filename, pathtofile)
VALUES ($filehash, $filename, $pathtofile)
";
InsertCommand.Parameters.AddWithValue("$filehash", fileHash);
InsertCommand.Parameters.AddWithValue("$filename", fileName);
InsertCommand.Parameters.AddWithValue("$pathtofile", pathToFile);
InsertCommand.ExecuteNonQuery();
logger.Verbose("{fileName} which is located at {pathToFile} relative to the database with the hash {fileHash} was successfully inserted into the database", fileName, pathToFile, fileHash);
}
}
logger.Information("All filehashes were successfully inserted into the database");
@ -464,5 +465,6 @@ public class ChksumUtils {
public void cleanup() {
File.Delete(libraryPath);
logger.Information("Successfully deleted libe_sqlite3.so");
Console.ResetColor();
}
}