Compare commits
4 Commits
v1.0.0
...
7f6f4c5253
Author | SHA1 | Date | |
---|---|---|---|
7f6f4c5253
|
|||
e12117fba8
|
|||
763cde4e2d
|
|||
531a4676e9
|
@ -1,11 +1,19 @@
|
|||||||
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,27 +70,34 @@ public class ChksumUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private string CalculateMD5(string filename) {
|
private Dictionary<string, string> CalculateChecksums(string[] filenames) {
|
||||||
using (var md5 = System.Security.Cryptography.MD5.Create()) {
|
Dictionary<string, string> checksums = new Dictionary<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.Add(filename, checksum);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return 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) {
|
Dictionary<string, string> fileHashes = CalculateChecksums(indexFiles());
|
||||||
DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
|
foreach (var file in fileHashes) {
|
||||||
FileInfo[] files = dir.GetFiles();
|
string absolutePathToFile = file.Key;
|
||||||
foreach (FileInfo file in files) {
|
string fileName = Path.GetFileName(absolutePathToFile);
|
||||||
string fileName = file.Name;
|
|
||||||
string absolutePathToFile = Path.GetFullPath(fileName);
|
|
||||||
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();
|
connection.Open();
|
||||||
@ -100,7 +115,6 @@ public class ChksumUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
doTheThing();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user