code cleanup
made garbage code less garbage
This commit is contained in:
parent
7b00a87620
commit
90706e7796
@ -19,20 +19,23 @@ public class Program {
|
|||||||
|
|
||||||
utils.ExtractEmbeddedLibrary();
|
utils.ExtractEmbeddedLibrary();
|
||||||
|
|
||||||
|
var option = args[0].ToLower();
|
||||||
|
|
||||||
Console.ForegroundColor = ConsoleColor.Green;
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
switch (args[0]) {
|
switch (option) {
|
||||||
case "checksum":
|
case "checksum":
|
||||||
Console.WriteLine("Starting the checksum process.");
|
Console.WriteLine("Starting the checksum process.");
|
||||||
Console.ResetColor();
|
Console.ResetColor();
|
||||||
|
var hashAlgo = args[1].ToLower();
|
||||||
try {
|
|
||||||
if (args[1] == "MD5") {
|
if (hashAlgo.Equals("md5")) {
|
||||||
utils.doTheThing(args[1]);
|
utils.doTheThing(hashAlgo);
|
||||||
}
|
break;
|
||||||
int bufferSize = int.Parse(args[2]);
|
|
||||||
utils.doTheThing(args[1], bufferSize);
|
|
||||||
}
|
}
|
||||||
catch (FormatException) {
|
try {
|
||||||
|
var bufferSize = int.Parse(args[2]);
|
||||||
|
utils.doTheThing(hashAlgo, bufferSize);
|
||||||
|
} catch (FormatException) {
|
||||||
Console.ForegroundColor = ConsoleColor.Red;
|
Console.ForegroundColor = ConsoleColor.Red;
|
||||||
Console.WriteLine("Buffer was not a valid integer value. Please specify a valid integer value for the buffer size");
|
Console.WriteLine("Buffer was not a valid integer value. Please specify a valid integer value for the buffer size");
|
||||||
Console.ResetColor();
|
Console.ResetColor();
|
||||||
@ -41,16 +44,16 @@ public class Program {
|
|||||||
Console.ForegroundColor = ConsoleColor.Green;
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
Console.WriteLine("Checksum process finished");
|
Console.WriteLine("Checksum process finished");
|
||||||
break;
|
break;
|
||||||
case "saveToSqlite":
|
case "savetosqlite":
|
||||||
Console.ResetColor();
|
Console.ResetColor();
|
||||||
utils.saveToSqlite();
|
utils.saveToSqlite();
|
||||||
break;
|
break;
|
||||||
case "compareDatabases":
|
case "comparedatabases":
|
||||||
Console.ResetColor();
|
Console.ResetColor();
|
||||||
|
|
||||||
utils.compareDatabases(args[1]);
|
utils.compareDatabases(args[1]);
|
||||||
break;
|
break;
|
||||||
case "checkIfFileWasDeleted":
|
case "checkiffilewasdeleted":
|
||||||
Console.ResetColor();
|
Console.ResetColor();
|
||||||
utils.checkIfFileWasDeleted();
|
utils.checkIfFileWasDeleted();
|
||||||
break;
|
break;
|
||||||
@ -71,7 +74,6 @@ public class Program {
|
|||||||
String[] options = {
|
String[] options = {
|
||||||
"checksum - MD5, Murmur and XxHash - Default buffer size is 4096",
|
"checksum - MD5, Murmur and XxHash - Default buffer size is 4096",
|
||||||
"compareDatabases",
|
"compareDatabases",
|
||||||
"compareChecksums",
|
|
||||||
"saveToSqlite",
|
"saveToSqlite",
|
||||||
"checkIfFileWasDeleted",
|
"checkIfFileWasDeleted",
|
||||||
"help"
|
"help"
|
||||||
@ -79,7 +81,7 @@ public class Program {
|
|||||||
|
|
||||||
Console.ResetColor();
|
Console.ResetColor();
|
||||||
Console.WriteLine("usage: chksum [option] \nHere is a list of all available options:");
|
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);
|
Console.WriteLine("\t" + option);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -99,7 +99,7 @@ public class ChksumUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateProgressBar(int current, int total) {
|
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}]";
|
string progressText = $"Progress: {progress}% [{current}/{total}]";
|
||||||
|
|
||||||
Console.Write("\r" + progressText.PadRight(Console.WindowWidth));
|
Console.Write("\r" + progressText.PadRight(Console.WindowWidth));
|
||||||
@ -199,7 +199,7 @@ public class ChksumUtils {
|
|||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void doTheThing(string hashalgo, int bufferSize = 4096) {
|
public void doTheThing(string hashAlgo, int bufferSize = 4096) {
|
||||||
|
|
||||||
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
|
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
|
||||||
IDatabase db = redis.GetDatabase();
|
IDatabase db = redis.GetDatabase();
|
||||||
@ -214,22 +214,22 @@ public class ChksumUtils {
|
|||||||
Dictionary<string, uint> fileHashesMurmur;
|
Dictionary<string, uint> fileHashesMurmur;
|
||||||
Dictionary<string, string> fileHashesMD5;
|
Dictionary<string, string> fileHashesMD5;
|
||||||
|
|
||||||
switch (hashalgo) {
|
switch (hashAlgo) {
|
||||||
case "MD5":
|
case "md5":
|
||||||
fileHashesMD5 = CalculateChecksums(indexFiles());
|
fileHashesMD5 = CalculateChecksums(indexFiles());
|
||||||
fileHashes = fileHashesMD5.ToDictionary(kv => kv.Key, kv => (object)kv.Value);
|
fileHashes = fileHashesMD5.ToDictionary(kv => kv.Key, kv => (object)kv.Value);
|
||||||
break;
|
break;
|
||||||
case "Murmur":
|
case "murmur":
|
||||||
fileHashesMurmur = CalculateChecksumsWithMurmur(indexFiles(), bufferSize);
|
fileHashesMurmur = CalculateChecksumsWithMurmur(indexFiles(), bufferSize);
|
||||||
fileHashes = fileHashesMurmur.ToDictionary(kv => kv.Key, kv => (object)kv.Value);
|
fileHashes = fileHashesMurmur.ToDictionary(kv => kv.Key, kv => (object)kv.Value);
|
||||||
break;
|
break;
|
||||||
case "XxHash":
|
case "xxhash":
|
||||||
fileHashesXxHash3 = CalculateChecksumsWithXxHash3(indexFiles(), bufferSize);
|
fileHashesXxHash3 = CalculateChecksumsWithXxHash3(indexFiles(), bufferSize);
|
||||||
fileHashes = fileHashesXxHash3.ToDictionary(kv => kv.Key, kv => (object)kv.Value);
|
fileHashes = fileHashesXxHash3.ToDictionary(kv => kv.Key, kv => (object)kv.Value);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
logger.Error("No valid hash algorithm was selected");
|
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");
|
logger.Information("All files were checksummed");
|
||||||
@ -241,7 +241,6 @@ public class ChksumUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void saveToSqlite() {
|
public void saveToSqlite() {
|
||||||
|
|
||||||
initializeDB();
|
initializeDB();
|
||||||
|
|
||||||
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
|
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
|
||||||
@ -251,27 +250,29 @@ public class ChksumUtils {
|
|||||||
logger.Information("Retrived all values from redis");
|
logger.Information("Retrived all values from redis");
|
||||||
|
|
||||||
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
|
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
|
||||||
|
connection.Open();
|
||||||
|
|
||||||
foreach (var file in fileHashes) {
|
foreach (var file in fileHashes) {
|
||||||
|
|
||||||
var absolutePathToFile = file.Name.ToString();
|
var absolutePathToFile = file.Name.ToString();
|
||||||
string fileName = Path.GetFileName(absolutePathToFile.ToString());
|
string fileName = Path.GetFileName(absolutePathToFile.ToString());
|
||||||
string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile.ToString());
|
string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile.ToString());
|
||||||
var fileHash = file.Value.ToString();
|
var fileHash = file.Value.ToString();
|
||||||
|
|
||||||
if (!checkIfFileMovedAndUpdatePathToFile(fileHash, fileName, pathToFile) && !checkIfFileAlreadyExistsInDatabase(fileHash, fileName)) {
|
if (checkIfFileMovedAndUpdatePathToFile(fileHash, fileName, pathToFile) || checkIfFileAlreadyExistsInDatabase(fileHash, fileName)) {
|
||||||
connection.Open();
|
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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");
|
logger.Information("All filehashes were successfully inserted into the database");
|
||||||
@ -464,5 +465,6 @@ public class ChksumUtils {
|
|||||||
public void cleanup() {
|
public void cleanup() {
|
||||||
File.Delete(libraryPath);
|
File.Delete(libraryPath);
|
||||||
logger.Information("Successfully deleted libe_sqlite3.so");
|
logger.Information("Successfully deleted libe_sqlite3.so");
|
||||||
|
Console.ResetColor();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user