feature/multi-threading #2
@ -1,11 +1,20 @@
|
|||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Security.Cryptography;
|
||||||
using Microsoft.Data.Sqlite;
|
using Microsoft.Data.Sqlite;
|
||||||
namespace Chksum.Utils;
|
namespace Chksum.Utils;
|
||||||
public class ChksumUtils {
|
public class ChksumUtils {
|
||||||
|
|
||||||
private int getFileCount() {
|
private int getTotalFileCount() {
|
||||||
int fileCount = Directory.GetFiles(Directory.GetCurrentDirectory()).Length; // Get file count in current directory
|
int totalFileCount = Directory.GetFiles(Directory.GetCurrentDirectory(), "*", SearchOption.AllDirectories).Length;
|
||||||
return fileCount;
|
return totalFileCount - 3; // Remove the program, datbase and library from the totalFileCount
|
||||||
|
}
|
||||||
|
|
||||||
|
private string[] indexFiles() {
|
||||||
|
string[] indexedFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*", SearchOption.AllDirectories);
|
||||||
|
string[] filesToExclude = { "Chksum", "chksum.db", "libe_sqlite3.so" };
|
||||||
|
indexedFiles = indexedFiles.Where(file => !filesToExclude.Contains(Path.GetFileName(file))).ToArray();
|
||||||
|
return indexedFiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string DatabaseRoot { get; set; } = string.Empty;
|
public string DatabaseRoot { get; set; } = string.Empty;
|
||||||
@ -62,31 +71,40 @@ public class ChksumUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private string CalculateMD5(string filename) {
|
private Dictionary<string, string> CalculateChecksums(string[] filenames) {
|
||||||
using (var md5 = System.Security.Cryptography.MD5.Create()) {
|
ConcurrentDictionary<string, string> checksums = new ConcurrentDictionary<string, string>();
|
||||||
|
|
||||||
|
Parallel.ForEach(filenames, (filename, state) => {
|
||||||
|
using (var md5 = MD5.Create()) {
|
||||||
using (var stream = File.OpenRead(filename)) {
|
using (var stream = File.OpenRead(filename)) {
|
||||||
var hash = md5.ComputeHash(stream);
|
var hash = md5.ComputeHash(stream);
|
||||||
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
|
var checksum = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
|
||||||
|
|
||||||
|
lock (checksums) {
|
||||||
|
checksums.TryAdd(filename, checksum);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return new Dictionary<string, string>(checksums);
|
||||||
|
}
|
||||||
|
|
||||||
public void doTheThing() {
|
public void doTheThing() {
|
||||||
foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory()))
|
|
||||||
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
|
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
|
||||||
Directory.SetCurrentDirectory(directory); // Set new root
|
if (getTotalFileCount() < 1) {
|
||||||
if (getFileCount() >= 1) {
|
return;
|
||||||
DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
|
}
|
||||||
FileInfo[] files = dir.GetFiles();
|
connection.Open();
|
||||||
foreach (FileInfo file in files) {
|
Dictionary<string, string> fileHashes = CalculateChecksums(indexFiles());
|
||||||
string fileName = file.Name;
|
|
||||||
string absolutePathToFile = Path.GetFullPath(fileName);
|
foreach (var file in fileHashes) {
|
||||||
|
string absolutePathToFile = file.Key;
|
||||||
|
string fileName = Path.GetFileName(absolutePathToFile);
|
||||||
string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile);
|
string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile);
|
||||||
string fileHash = CalculateMD5(fileName);
|
string fileHash = file.Value;
|
||||||
|
|
||||||
if (checkIfFileMovedAndUpdatePathToFile(fileHash, fileName, pathToFile) == false && checkIfFileAlreadyExistsInDatabase(fileHash, fileName) == false) {
|
if (checkIfFileMovedAndUpdatePathToFile(fileHash, fileName, pathToFile) == false && checkIfFileAlreadyExistsInDatabase(fileHash, fileName) == false) {
|
||||||
connection.Open();
|
|
||||||
|
|
||||||
var command = connection.CreateCommand();
|
var command = connection.CreateCommand();
|
||||||
command.CommandText =
|
command.CommandText =
|
||||||
@"
|
@"
|
||||||
@ -100,8 +118,6 @@ public class ChksumUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
doTheThing();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool checkIfFileAlreadyExistsInDatabase(string fileHash, string pathToFile) {
|
private bool checkIfFileAlreadyExistsInDatabase(string fileHash, string pathToFile) {
|
||||||
@ -165,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() {
|
||||||
@ -190,7 +206,9 @@ 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)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
var deleteCommand = connection.CreateCommand();
|
var deleteCommand = connection.CreateCommand();
|
||||||
deleteCommand.CommandText =
|
deleteCommand.CommandText =
|
||||||
@"
|
@"
|
||||||
@ -206,7 +224,6 @@ public class ChksumUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private List<string> getFilehashesFromDatabase(string connectionString) {
|
private List<string> getFilehashesFromDatabase(string connectionString) {
|
||||||
List<string> filehashesFromDatabase = new List<string>();
|
List<string> filehashesFromDatabase = new List<string>();
|
||||||
@ -235,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")) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user