Compare commits

...

2 Commits
v2.0.0 ... main

Author SHA1 Message Date
61e25685e5
Add build badge
Signed-off-by: Gitea <gitea@hopeless-cloud.xyz>
2023-07-06 17:17:17 +00:00
90706e7796
code cleanup
made garbage code less garbage
2023-07-04 22:39:59 +02:00
3 changed files with 42 additions and 36 deletions

View File

@ -1,3 +1,5 @@
[![status-badge](https://woodpecker.hopeless-cloud.xyz/api/badges/ProfessionalUwU/chksum/status.svg)](https://woodpecker.hopeless-cloud.xyz/ProfessionalUwU/chksum)
# chksum # chksum
Checksums every file under the current directory Checksums every file under the current directory

View File

@ -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();
if (hashAlgo.Equals("md5")) {
utils.doTheThing(hashAlgo);
break;
}
try { try {
if (args[1] == "MD5") { var bufferSize = int.Parse(args[2]);
utils.doTheThing(args[1]); utils.doTheThing(hashAlgo, bufferSize);
} } catch (FormatException) {
int bufferSize = int.Parse(args[2]);
utils.doTheThing(args[1], 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);
} }
} }

View File

@ -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,15 +250,18 @@ 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")) {
foreach (var file in fileHashes) { connection.Open();
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(); var InsertCommand = connection.CreateCommand();
InsertCommand.CommandText = InsertCommand.CommandText =
@" @"
@ -273,7 +275,6 @@ public class ChksumUtils {
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.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");
var keys = db.Execute("KEYS", "*"); var keys = db.Execute("KEYS", "*");
@ -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();
} }
} }