diff --git a/src/Chksum/Program.cs b/src/Chksum/Program.cs index 8ce3406..fcf3c39 100644 --- a/src/Chksum/Program.cs +++ b/src/Chksum/Program.cs @@ -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(); - - try { - if (args[1] == "MD5") { - utils.doTheThing(args[1]); - } - int bufferSize = int.Parse(args[2]); - utils.doTheThing(args[1], bufferSize); + var hashAlgo = args[1].ToLower(); + + 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); } } diff --git a/src/Chksum/chksum.cs b/src/Chksum/chksum.cs index b3279ca..a3895f6 100644 --- a/src/Chksum/chksum.cs +++ b/src/Chksum/chksum.cs @@ -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 fileHashesMurmur; Dictionary 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")) { + 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(); } } \ No newline at end of file