Compare commits

..

2 Commits

Author SHA1 Message Date
b899c4c5b6
Add option to check for deleted files 2023-06-26 15:59:06 +02:00
2d42842fb2
Minor tweaks 2023-06-26 12:58:05 +02:00
3 changed files with 45 additions and 20 deletions

View File

@ -39,19 +39,8 @@ Go to the publish folder
cd src/Chksum/bin/Release/net7.0/linux-x64/publish
```
Copy the libe_sqlite3.so to your /usr/local/lib or /usr/lib
```bash
cp libe_sqlite3.so /usr/local/lib
```
Run executable
```bash
LD_LIBRARY_PATH=/usr/local/lib ./Chksum
```
Info
LD_LIBRARY_PATH=/usr/local/lib is needed to tell the executable where the library is located
Alternately you can put the libe_sqlite3.so into the same folder as the executable
./Chksum
```

View File

@ -39,6 +39,10 @@ public class Program {
case "createDB":
utils.initializeDB();
break;
case "checkIfFileWasDeleted":
Console.ResetColor();
utils.checkIfFileWasDeleted();
break;
case "help":
PrintAvailableOptions();
break;
@ -57,6 +61,7 @@ public class Program {
"checksum",
"compareChecksums",
"createDB",
"checkIfFileWasDeleted",
"help"
};

View File

@ -84,7 +84,7 @@ public class ChksumUtils {
string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile);
string fileHash = CalculateMD5(fileName);
if (checkIfFileMovedAndUpdatePathToFile(fileHash, fileName, pathToFile) == false && checkIfFileAlreadyExists(fileHash, fileName) == false) {
if (checkIfFileMovedAndUpdatePathToFile(fileHash, fileName, pathToFile) == false && checkIfFileAlreadyExistsInDatabase(fileHash, fileName) == false) {
connection.Open();
var command = connection.CreateCommand();
@ -104,12 +104,12 @@ public class ChksumUtils {
}
}
private bool checkIfFileAlreadyExists(string fileHash, string pathToFile) {
private bool checkIfFileAlreadyExistsInDatabase(string fileHash, string pathToFile) {
string filehash = string.Empty;
string pathtofile = string.Empty;
bool doesExist = false;
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadOnly")) {
connection.Open();
var command = connection.CreateCommand();
@ -128,9 +128,6 @@ public class ChksumUtils {
}
if (fileHash == filehash) {
Console.WriteLine("Duplicate files found:");
Console.WriteLine($"\toriginal\t{pathToFile}");
Console.WriteLine($"\tduplicate\t{pathtofile}\n");
doesExist = true;
}
return doesExist;
@ -177,7 +174,41 @@ public class ChksumUtils {
}
}
public void compareChecksums() {
public void checkIfFileWasDeleted() {
string pathToFile = string.Empty;
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
connection.Open();
var selectCommand = connection.CreateCommand();
selectCommand.CommandText =
@"
Select pathtofile FROM file
";
using (var reader = selectCommand.ExecuteReader()) {
while (reader.Read()) {
pathToFile = reader.GetString(0);
if (!File.Exists(pathToFile)) {
var deleteCommand = connection.CreateCommand();
deleteCommand.CommandText =
@"
DELETE FROM file
WHERE pathtofile = $pathtofile
";
deleteCommand.Parameters.AddWithValue("$pathtofile", pathToFile);
deleteCommand.ExecuteNonQuery();
Console.WriteLine("File deleted:");
Console.WriteLine($"\t{pathToFile}\n");
}
}
}
}
}
public void compareChecksums() { // reuse for database comparison
foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) {
Directory.SetCurrentDirectory(directory); // Set new root
if (getFileCount() >= 1) {