feature/SQlite #1

Merged
ProfessionalUwU merged 14 commits from feature/SQlite into main 2023-06-26 18:22:28 +02:00
2 changed files with 66 additions and 33 deletions
Showing only changes of commit 6fbc53fa53 - Show all commits

View File

@ -8,7 +8,7 @@ public class Program {
Console.WriteLine("Please specify an option."); Console.WriteLine("Please specify an option.");
PrintAvailableOptions(); PrintAvailableOptions();
return; return;
} else if (args.Length > 1) { } else if (args.Length > 1 && args[0] != "compareDatabases") {
Console.WriteLine("Too many options."); Console.WriteLine("Too many options.");
return; return;
} }
@ -30,11 +30,10 @@ public class Program {
Console.ForegroundColor = ConsoleColor.Green; Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Checksum process finished"); Console.WriteLine("Checksum process finished");
break; break;
case "compareChecksums": case "compareDatabases":
Console.WriteLine("Comparing all md5 checksum files. If there is none, creating one.");
Console.ResetColor(); Console.ResetColor();
utils.compareChecksums(); utils.compareDatabases(args[1]);
break; break;
case "createDB": case "createDB":
utils.initializeDB(); utils.initializeDB();

View File

@ -208,38 +208,72 @@ public class ChksumUtils {
} }
} }
public void compareChecksums() { // reuse for database comparison public void compareDatabases(string filePathToOtherDatabase) {
foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) { List<string> filehashesOfOriginDatabase = new List<string>();
Directory.SetCurrentDirectory(directory); // Set new root using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadOnly")) {
if (getFileCount() >= 1) { string filehash = string.Empty;
DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
FileInfo[] files = dir.GetFiles(); connection.Open();
// files.ToList().ForEach(i => Console.WriteLine(i.ToString())); // Print all files in files array
foreach (FileInfo file in files) { var selectCommand = connection.CreateCommand();
string fileName = file.Name; selectCommand.CommandText =
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName); @"
string checksumFile = Directory.GetCurrentDirectory() + "/" + fileNameWithoutExtension + ".md5"; Select filehash FROM file
string fileMd5Checksum = fileNameWithoutExtension + ".md5"; ";
if (File.Exists(fileMd5Checksum)) {
string newFileChecksum = CalculateMD5(fileName) + " " + fileName; using (var reader = selectCommand.ExecuteReader()) {
string existingFileChecksum = File.ReadAllText(fileMd5Checksum); while (reader.Read()) {
string newFileName = newFileChecksum.Substring(34); filehash = reader.GetString(0);
string existingFileName = existingFileChecksum.Substring(34); filehashesOfOriginDatabase.Add(filehash);
if (newFileChecksum.Equals(existingFileChecksum)) { }
Console.WriteLine(newFileName + " and " + existingFileName + " are the same."); }
} else { }
Console.WriteLine(newFileName + " and " + existingFileName + " are not the same.");
Console.WriteLine("The checksum of " + newFileName + " is " + newFileChecksum); List<string> filehashesOfRemoteDatabase = new List<string>();
Console.WriteLine("The checksum of the already exting file " + existingFileName + " is " + existingFileChecksum); using (var connection = new SqliteConnection("Data Source=" + filePathToOtherDatabase + ";Mode=ReadOnly")) {
// TODO Tell the user to check which file is the correct one string filehash = string.Empty;
}
} else { connection.Open();
File.AppendAllText(checksumFile, CalculateMD5(fileName) + " " + fileName);
Console.WriteLine("Calculated checksum for: " + checksumFile); var selectCommand = connection.CreateCommand();
selectCommand.CommandText =
@"
Select filehash FROM file
";
using (var reader = selectCommand.ExecuteReader()) {
while (reader.Read()) {
filehash = reader.GetString(0);
filehashesOfRemoteDatabase.Add(filehash);
}
}
}
List<string> filesThatDoNotExistsInTheRemote = filehashesOfOriginDatabase.Except(filehashesOfRemoteDatabase).ToList();
//List<string> filesThatDoNotExistsInTheOrigin = filehashesOfRemoteDatabase.Except(filehashesOfOriginDatabase).ToList();
foreach (string file in filesThatDoNotExistsInTheRemote) {
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadOnly")) {
string filename = string.Empty;
connection.Open();
var selectCommand = connection.CreateCommand();
selectCommand.CommandText =
@"
Select filename FROM file WHERE filehash = $filehash
";
selectCommand.Parameters.AddWithValue("$filehash", file);
using (var reader = selectCommand.ExecuteReader()) {
while (reader.Read()) {
filename = reader.GetString(0);
Console.WriteLine("File not found in remote:");
Console.WriteLine($"\t{filename}\n");
} }
} }
} }
compareChecksums();
} }
} }