Compare commits
8 Commits
8aec649781
...
v1.0.0
Author | SHA1 | Date | |
---|---|---|---|
bc42dd542a
|
|||
836b850f3f
|
|||
944b61a1ac
|
|||
6fbc53fa53
|
|||
b899c4c5b6
|
|||
2d42842fb2
|
|||
f99ca8bb26
|
|||
a6c994fa65
|
15
README.md
15
README.md
@ -39,19 +39,8 @@ Go to the publish folder
|
|||||||
cd src/Chksum/bin/Release/net7.0/linux-x64/publish
|
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
|
Run executable
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
LD_LIBRARY_PATH=/usr/local/lib ./Chksum
|
./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
|
|
@ -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,15 +30,18 @@ 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();
|
||||||
break;
|
break;
|
||||||
|
case "checkIfFileWasDeleted":
|
||||||
|
Console.ResetColor();
|
||||||
|
utils.checkIfFileWasDeleted();
|
||||||
|
break;
|
||||||
case "help":
|
case "help":
|
||||||
PrintAvailableOptions();
|
PrintAvailableOptions();
|
||||||
break;
|
break;
|
||||||
@ -57,6 +60,7 @@ public class Program {
|
|||||||
"checksum",
|
"checksum",
|
||||||
"compareChecksums",
|
"compareChecksums",
|
||||||
"createDB",
|
"createDB",
|
||||||
|
"checkIfFileWasDeleted",
|
||||||
"help"
|
"help"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -84,7 +84,7 @@ public class ChksumUtils {
|
|||||||
string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile);
|
string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile);
|
||||||
string fileHash = CalculateMD5(fileName);
|
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();
|
connection.Open();
|
||||||
|
|
||||||
var command = connection.CreateCommand();
|
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 filehash = string.Empty;
|
||||||
string pathtofile = string.Empty;
|
string pathtofile = string.Empty;
|
||||||
bool doesExist = false;
|
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();
|
connection.Open();
|
||||||
|
|
||||||
var command = connection.CreateCommand();
|
var command = connection.CreateCommand();
|
||||||
@ -128,9 +128,6 @@ public class ChksumUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (fileHash == filehash) {
|
if (fileHash == filehash) {
|
||||||
Console.WriteLine("Duplicate files found:");
|
|
||||||
Console.WriteLine($"\toriginal\t{pathToFile}");
|
|
||||||
Console.WriteLine($"\tduplicate\t{pathtofile}\n");
|
|
||||||
doesExist = true;
|
doesExist = true;
|
||||||
}
|
}
|
||||||
return doesExist;
|
return doesExist;
|
||||||
@ -177,38 +174,91 @@ public class ChksumUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void compareChecksums() {
|
public void checkIfFileWasDeleted() {
|
||||||
foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) {
|
string pathToFile = string.Empty;
|
||||||
Directory.SetCurrentDirectory(directory); // Set new root
|
|
||||||
if (getFileCount() >= 1) {
|
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
|
||||||
DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
|
connection.Open();
|
||||||
FileInfo[] files = dir.GetFiles();
|
|
||||||
// files.ToList().ForEach(i => Console.WriteLine(i.ToString())); // Print all files in files array
|
var selectCommand = connection.CreateCommand();
|
||||||
foreach (FileInfo file in files) {
|
selectCommand.CommandText =
|
||||||
string fileName = file.Name;
|
@"
|
||||||
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
|
Select pathtofile FROM file
|
||||||
string checksumFile = Directory.GetCurrentDirectory() + "/" + fileNameWithoutExtension + ".md5";
|
";
|
||||||
string fileMd5Checksum = fileNameWithoutExtension + ".md5";
|
|
||||||
if (File.Exists(fileMd5Checksum)) {
|
using (var reader = selectCommand.ExecuteReader()) {
|
||||||
string newFileChecksum = CalculateMD5(fileName) + " " + fileName;
|
while (reader.Read()) {
|
||||||
string existingFileChecksum = File.ReadAllText(fileMd5Checksum);
|
pathToFile = reader.GetString(0);
|
||||||
string newFileName = newFileChecksum.Substring(34);
|
|
||||||
string existingFileName = existingFileChecksum.Substring(34);
|
if (!File.Exists(pathToFile)) {
|
||||||
if (newFileChecksum.Equals(existingFileChecksum)) {
|
var deleteCommand = connection.CreateCommand();
|
||||||
Console.WriteLine(newFileName + " and " + existingFileName + " are the same.");
|
deleteCommand.CommandText =
|
||||||
} else {
|
@"
|
||||||
Console.WriteLine(newFileName + " and " + existingFileName + " are not the same.");
|
DELETE FROM file
|
||||||
Console.WriteLine("The checksum of " + newFileName + " is " + newFileChecksum);
|
WHERE pathtofile = $pathtofile
|
||||||
Console.WriteLine("The checksum of the already exting file " + existingFileName + " is " + existingFileChecksum);
|
";
|
||||||
// TODO Tell the user to check which file is the correct one
|
deleteCommand.Parameters.AddWithValue("$pathtofile", pathToFile);
|
||||||
}
|
deleteCommand.ExecuteNonQuery();
|
||||||
} else {
|
|
||||||
File.AppendAllText(checksumFile, CalculateMD5(fileName) + " " + fileName);
|
Console.WriteLine("File deleted:");
|
||||||
Console.WriteLine("Calculated checksum for: " + checksumFile);
|
Console.WriteLine($"\t{pathToFile}\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<string> getFilehashesFromDatabase(string connectionString) {
|
||||||
|
List<string> filehashesFromDatabase = new List<string>();
|
||||||
|
|
||||||
|
using (var connection = new SqliteConnection(connectionString)) {
|
||||||
|
string filehash = string.Empty;
|
||||||
|
|
||||||
|
connection.Open();
|
||||||
|
|
||||||
|
var selectCommand = connection.CreateCommand();
|
||||||
|
selectCommand.CommandText =
|
||||||
|
@"
|
||||||
|
Select filehash FROM file
|
||||||
|
";
|
||||||
|
|
||||||
|
using (var reader = selectCommand.ExecuteReader()) {
|
||||||
|
while (reader.Read()) {
|
||||||
|
filehash = reader.GetString(0);
|
||||||
|
filehashesFromDatabase.Add(filehash);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return filehashesFromDatabase;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void compareDatabases(string filePathToOtherDatabase) {
|
||||||
|
List<string> filesThatDoNotExistsInTheRemote = getFilehashesFromDatabase("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadOnly").Except(getFilehashesFromDatabase("Data Source=" + filePathToOtherDatabase + ";Mode=ReadOnly")).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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user