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
Checksums every file under the current directory

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();
if (hashAlgo.Equals("md5")) {
utils.doTheThing(hashAlgo);
break;
}
try {
if (args[1] == "MD5") {
utils.doTheThing(args[1]);
}
int bufferSize = int.Parse(args[2]);
utils.doTheThing(args[1], bufferSize);
}
catch (FormatException) {
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,15 +250,18 @@ 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();
if (checkIfFileMovedAndUpdatePathToFile(fileHash, fileName, pathToFile) || checkIfFileAlreadyExistsInDatabase(fileHash, fileName)) {
continue;
}
var InsertCommand = connection.CreateCommand();
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.Information("All filehashes were successfully inserted into the database");
var keys = db.Execute("KEYS", "*");
@ -464,5 +465,6 @@ public class ChksumUtils {
public void cleanup() {
File.Delete(libraryPath);
logger.Information("Successfully deleted libe_sqlite3.so");
Console.ResetColor();
}
}