Compare commits
7 Commits
7f6f4c5253
...
v1.1.0
Author | SHA1 | Date | |
---|---|---|---|
1ee5114dab
|
|||
f403b77864
|
|||
42320ebf8d
|
|||
787721381d | |||
7d7e9bac6c | |||
2b4019d7cf | |||
be8180a60d |
@ -1,3 +1,4 @@
|
|||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using Microsoft.Data.Sqlite;
|
using Microsoft.Data.Sqlite;
|
||||||
@ -71,7 +72,7 @@ public class ChksumUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Dictionary<string, string> CalculateChecksums(string[] filenames) {
|
private Dictionary<string, string> CalculateChecksums(string[] filenames) {
|
||||||
Dictionary<string, string> checksums = new Dictionary<string, string>();
|
ConcurrentDictionary<string, string> checksums = new ConcurrentDictionary<string, string>();
|
||||||
|
|
||||||
Parallel.ForEach(filenames, (filename, state) => {
|
Parallel.ForEach(filenames, (filename, state) => {
|
||||||
using (var md5 = MD5.Create()) {
|
using (var md5 = MD5.Create()) {
|
||||||
@ -80,39 +81,40 @@ public class ChksumUtils {
|
|||||||
var checksum = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
|
var checksum = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
|
||||||
|
|
||||||
lock (checksums) {
|
lock (checksums) {
|
||||||
checksums.Add(filename, checksum);
|
checksums.TryAdd(filename, checksum);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return checksums;
|
return new Dictionary<string, string>(checksums);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void doTheThing() {
|
public void doTheThing() {
|
||||||
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
|
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
|
||||||
if (getTotalFileCount() >= 1) {
|
if (getTotalFileCount() < 1) {
|
||||||
Dictionary<string, string> fileHashes = CalculateChecksums(indexFiles());
|
return;
|
||||||
foreach (var file in fileHashes) {
|
}
|
||||||
string absolutePathToFile = file.Key;
|
connection.Open();
|
||||||
string fileName = Path.GetFileName(absolutePathToFile);
|
Dictionary<string, string> fileHashes = CalculateChecksums(indexFiles());
|
||||||
string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile);
|
|
||||||
string fileHash = file.Value;
|
foreach (var file in fileHashes) {
|
||||||
|
string absolutePathToFile = file.Key;
|
||||||
if (checkIfFileMovedAndUpdatePathToFile(fileHash, fileName, pathToFile) == false && checkIfFileAlreadyExistsInDatabase(fileHash, fileName) == false) {
|
string fileName = Path.GetFileName(absolutePathToFile);
|
||||||
connection.Open();
|
string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile);
|
||||||
|
string fileHash = file.Value;
|
||||||
var command = connection.CreateCommand();
|
|
||||||
command.CommandText =
|
if (checkIfFileMovedAndUpdatePathToFile(fileHash, fileName, pathToFile) == false && checkIfFileAlreadyExistsInDatabase(fileHash, fileName) == false) {
|
||||||
@"
|
var command = connection.CreateCommand();
|
||||||
INSERT INTO file (filehash, filename, pathtofile)
|
command.CommandText =
|
||||||
VALUES ($filehash, $filename, $pathtofile)
|
@"
|
||||||
";
|
INSERT INTO file (filehash, filename, pathtofile)
|
||||||
command.Parameters.AddWithValue("$filehash", fileHash);
|
VALUES ($filehash, $filename, $pathtofile)
|
||||||
command.Parameters.AddWithValue("$filename", fileName);
|
";
|
||||||
command.Parameters.AddWithValue("$pathtofile", pathToFile);
|
command.Parameters.AddWithValue("$filehash", fileHash);
|
||||||
command.ExecuteNonQuery();
|
command.Parameters.AddWithValue("$filename", fileName);
|
||||||
}
|
command.Parameters.AddWithValue("$pathtofile", pathToFile);
|
||||||
|
command.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -179,13 +181,13 @@ public class ChksumUtils {
|
|||||||
command2.Parameters.AddWithValue("$filehash", fileHash);
|
command2.Parameters.AddWithValue("$filehash", fileHash);
|
||||||
command2.ExecuteNonQuery();
|
command2.ExecuteNonQuery();
|
||||||
|
|
||||||
Console.WriteLine("File moved:");
|
Console.WriteLine("File moved or is a duplicate:");
|
||||||
Console.WriteLine($"\tfrom\t{pathToFile}");
|
Console.WriteLine($"\tfrom\t{pathToFile}");
|
||||||
Console.WriteLine($"\tto \t{pathtofile}\n");
|
Console.WriteLine($"\tto \t{pathtofile}\n");
|
||||||
wasMoved = true;
|
wasMoved = true;
|
||||||
}
|
}
|
||||||
return wasMoved;
|
|
||||||
}
|
}
|
||||||
|
return wasMoved;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void checkIfFileWasDeleted() {
|
public void checkIfFileWasDeleted() {
|
||||||
@ -204,19 +206,20 @@ public class ChksumUtils {
|
|||||||
while (reader.Read()) {
|
while (reader.Read()) {
|
||||||
pathToFile = reader.GetString(0);
|
pathToFile = reader.GetString(0);
|
||||||
|
|
||||||
if (!File.Exists(pathToFile)) {
|
if (File.Exists(pathToFile)) {
|
||||||
var deleteCommand = connection.CreateCommand();
|
continue;
|
||||||
deleteCommand.CommandText =
|
|
||||||
@"
|
|
||||||
DELETE FROM file
|
|
||||||
WHERE pathtofile = $pathtofile
|
|
||||||
";
|
|
||||||
deleteCommand.Parameters.AddWithValue("$pathtofile", pathToFile);
|
|
||||||
deleteCommand.ExecuteNonQuery();
|
|
||||||
|
|
||||||
Console.WriteLine("File deleted:");
|
|
||||||
Console.WriteLine($"\t{pathToFile}\n");
|
|
||||||
}
|
}
|
||||||
|
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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -249,7 +252,6 @@ public class ChksumUtils {
|
|||||||
|
|
||||||
public void compareDatabases(string filePathToOtherDatabase) {
|
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> 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) {
|
foreach (string file in filesThatDoNotExistsInTheRemote) {
|
||||||
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadOnly")) {
|
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadOnly")) {
|
||||||
|
Reference in New Issue
Block a user