From 6d2880907bf862ceeeb9686f9614478e9fb5d0c9 Mon Sep 17 00:00:00 2001 From: ProfessionalUwU Date: Fri, 23 Jun 2023 22:34:00 +0200 Subject: [PATCH 01/14] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index de78ccb..1d40e9a 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Checksums every file under the current directory Clone the project ```bash -git clone http://192.168.0.69:3000/ProfessionalUwU/chksum.git +git clone https://gitea.hopeless-cloud.xyz/ProfessionalUwU/chksum.git ``` Go to the project directory From 951068c7505379999ac9186217deb6616433c44d Mon Sep 17 00:00:00 2001 From: ProfessionalUwU Date: Fri, 23 Jun 2023 22:38:32 +0200 Subject: [PATCH 02/14] Add SQlite package --- chksum.csproj | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/chksum.csproj b/chksum.csproj index b19ce67..8090b43 100644 --- a/chksum.csproj +++ b/chksum.csproj @@ -11,4 +11,8 @@ enable + + + + From 846b983caab9089262600824b5b6c3094cc7fdb6 Mon Sep 17 00:00:00 2001 From: ProfessionalUwU Date: Sat, 24 Jun 2023 01:02:03 +0200 Subject: [PATCH 03/14] Major changes Now using an SQlite database to store the file hash plus a bunch of other information --- Program.cs | 8 +++++++- chksum.cs | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/Program.cs b/Program.cs index d0873c6..4f3ba64 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,4 @@ -public class Program { +public class Program { static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Red; @@ -11,6 +11,8 @@ return; } + Chksum.getBaseDir(); + Console.ForegroundColor = ConsoleColor.Green; switch (args[0]) { case "checksum": @@ -46,6 +48,9 @@ Chksum.compareChecksums(); break; + case "createDB": + Chksum.initializeDB(); + break; case "help": PrintAvailableOptions(); break; @@ -63,6 +68,7 @@ "countmd5", "deletemd5", "compareChecksums", + "createDB", "help" }; diff --git a/chksum.cs b/chksum.cs index 06af796..7cb4265 100644 --- a/chksum.cs +++ b/chksum.cs @@ -1,7 +1,10 @@ +using Microsoft.Data.Sqlite; + // Go into folder // Check if any file is in there // If there is a file. Calculate md5sum > filename.md5 // If there is no file. Repeat + public class Chksum { // int getDirectoryCount() { @@ -19,6 +22,30 @@ public class Chksum { // return parentFolder; // } + public static string DatabaseRoot { get; set; } + public static void getBaseDir() { + DatabaseRoot = AppDomain.CurrentDomain.BaseDirectory; + } + + public static void initializeDB() { + using (var connection = new SqliteConnection("Data Source=chksum.db")) { + connection.Open(); + + var command = connection.CreateCommand(); + command.CommandText = + @" + CREATE TABLE file ( + filehash TEXT NOT NULL PRIMARY KEY, + filename TEXT NOT NULL, + pathtofile TEXT NOT NULL, + artist TEXT, + playbacklength INTEGER + ); + "; + command.ExecuteNonQuery(); + } + } + private static string CalculateMD5(string filename) { using (var md5 = System.Security.Cryptography.MD5.Create()) { using (var stream = File.OpenRead(filename)) { @@ -36,10 +63,24 @@ public class Chksum { FileInfo[] files = dir.GetFiles(); foreach (FileInfo file in files) { string fileName = file.Name; - string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName); - string checksumFile = Directory.GetCurrentDirectory() + "/" + fileNameWithoutExtension + ".md5"; - File.AppendAllText(checksumFile, CalculateMD5(fileName) + " " + fileName); - Console.WriteLine(checksumFile); + string absolutePathToFile = Path.GetFullPath(fileName); + string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile); + string fileHash = CalculateMD5(fileName); + + using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) { + connection.Open(); + + var command = connection.CreateCommand(); + command.CommandText = + @" + INSERT INTO file (filehash, filename, pathtofile) + VALUES ($filehash, $filename, $pathtofile) + "; + command.Parameters.AddWithValue("$filehash", fileHash); + command.Parameters.AddWithValue("$filename", fileName); + command.Parameters.AddWithValue("$pathtofile", pathToFile); + command.ExecuteNonQuery(); + } } } doTheThing(); From 5a3becb3b48eb9e249399696a75246920cda5dd5 Mon Sep 17 00:00:00 2001 From: ProfessionalUwU Date: Sun, 25 Jun 2023 02:45:33 +0200 Subject: [PATCH 04/14] Major additons Converted to new project structure Check if db already exists Add vacuum for db cleanup Check if filehash already exits and write info to console --- .editorconfig | 2 +- justfile | 33 ++++- src/Chksum.Tests/Chksum.Tests.csproj | 25 ++++ src/Chksum.Tests/Usings.cs | 2 + src/Chksum.Tests/doTheThingTest.cs | 8 ++ chksum.csproj => src/Chksum/Chksum.csproj | 4 + Program.cs => src/Chksum/Program.cs | 17 ++- chksum.cs => src/Chksum/chksum.cs | 132 +++++++++++++------- debugchksum.cs => src/Chksum/debugchksum.cs | 0 src/chksum.sln | 28 +++++ 10 files changed, 199 insertions(+), 52 deletions(-) create mode 100644 src/Chksum.Tests/Chksum.Tests.csproj create mode 100644 src/Chksum.Tests/Usings.cs create mode 100644 src/Chksum.Tests/doTheThingTest.cs rename chksum.csproj => src/Chksum/Chksum.csproj (83%) rename Program.cs => src/Chksum/Program.cs (88%) rename chksum.cs => src/Chksum/chksum.cs (58%) rename debugchksum.cs => src/Chksum/debugchksum.cs (100%) create mode 100644 src/chksum.sln diff --git a/.editorconfig b/.editorconfig index 8b0b175..d793869 100644 --- a/.editorconfig +++ b/.editorconfig @@ -10,7 +10,7 @@ indent_style = space tab_width = 4 # Naming Conventions -dotnet_naming_style.pascal_case_style.capitalization = pascal_case +dotnet_naming_style.camel_case.capitalization = camel_case # New line preferences csharp_new_line_before_open_brace = none diff --git a/justfile b/justfile index d9be799..8ad837f 100644 --- a/justfile +++ b/justfile @@ -1,2 +1,31 @@ -publish: - @dotnet publish --configuration Release chksum.csproj +default: + @just --list + +project_name := `printf '%s\n' "${PWD##*/}"` +uppercase_project_name := capitalize(project_name) + +setup: + @mkdir src + @dotnet new sln --name src/{{project_name}} + @dotnet new classlib -o src/{{uppercase_project_name}} + @dotnet new xunit -o src/{{uppercase_project_name}}.Tests + @dotnet sln add src/{{uppercase_project_name}}/{{uppercase_project_name}}.csproj + @dotnet sln add src/{{uppercase_project_name}}.Tests/{{uppercase_project_name}}.Tests.csproj + @dotnet add src/{{uppercase_project_name}}/{{uppercase_project_name}}.csproj reference src/{{uppercase_project_name}}.Tests/{{uppercase_project_name}}.Tests.csproj + +run: + @dotnet run + +build: + @dotnet build src/{{uppercase_project_name}}/{{uppercase_project_name}}.csproj + @dotnet build src/{{uppercase_project_name}}.Tests/{{uppercase_project_name}}.Tests.csproj + +publish: format + @dotnet publish --configuration Release src/{{uppercase_project_name}}/{{uppercase_project_name}}.csproj + +format: + @dotnet format src/{{uppercase_project_name}} + @dotnet format src/{{uppercase_project_name}}.Tests + +test: build + @dotnet test src/{{uppercase_project_name}}.Tests diff --git a/src/Chksum.Tests/Chksum.Tests.csproj b/src/Chksum.Tests/Chksum.Tests.csproj new file mode 100644 index 0000000..10cd649 --- /dev/null +++ b/src/Chksum.Tests/Chksum.Tests.csproj @@ -0,0 +1,25 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + diff --git a/src/Chksum.Tests/Usings.cs b/src/Chksum.Tests/Usings.cs new file mode 100644 index 0000000..7fef4b0 --- /dev/null +++ b/src/Chksum.Tests/Usings.cs @@ -0,0 +1,2 @@ +global using Xunit; +global using FluentAssertions; \ No newline at end of file diff --git a/src/Chksum.Tests/doTheThingTest.cs b/src/Chksum.Tests/doTheThingTest.cs new file mode 100644 index 0000000..94ae38e --- /dev/null +++ b/src/Chksum.Tests/doTheThingTest.cs @@ -0,0 +1,8 @@ +namespace Chksum.Tests; + +public class doTheThingTest { + [Fact] + public void doTheThing_willNotThrowAnException() { + + } +} \ No newline at end of file diff --git a/chksum.csproj b/src/Chksum/Chksum.csproj similarity index 83% rename from chksum.csproj rename to src/Chksum/Chksum.csproj index 8090b43..6db87ee 100644 --- a/chksum.csproj +++ b/src/Chksum/Chksum.csproj @@ -1,5 +1,9 @@ + + + + Exe net7.0 diff --git a/Program.cs b/src/Chksum/Program.cs similarity index 88% rename from Program.cs rename to src/Chksum/Program.cs index 4f3ba64..15e4b64 100644 --- a/Program.cs +++ b/src/Chksum/Program.cs @@ -1,3 +1,6 @@ +using System; +using Chksum.Utils; + public class Program { static void Main(string[] args) { @@ -11,7 +14,9 @@ public class Program { return; } - Chksum.getBaseDir(); + ChksumUtils utils = new ChksumUtils(); + + utils.getBaseDir(); Console.ForegroundColor = ConsoleColor.Green; switch (args[0]) { @@ -19,7 +24,7 @@ public class Program { Console.WriteLine("Starting the checksum process."); Console.ResetColor(); - Chksum.doTheThing(); + utils.doTheThing(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Checksum process finished"); @@ -28,7 +33,7 @@ public class Program { Console.WriteLine("Counting md5 checksum files."); Console.ResetColor(); - Chksum.countAllMd5Checksums(); + utils.countAllMd5Checksums(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Finished counting all md5 checksum files."); @@ -37,7 +42,7 @@ public class Program { Console.WriteLine("Deleting all md5 checksum files."); Console.ResetColor(); - Chksum.deleteAllMd5Checksums(); + utils.deleteAllMd5Checksums(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Deleted all md5 checksum files."); @@ -46,10 +51,10 @@ public class Program { Console.WriteLine("Comparing all md5 checksum files. If there is none, creating one."); Console.ResetColor(); - Chksum.compareChecksums(); + utils.compareChecksums(); break; case "createDB": - Chksum.initializeDB(); + utils.initializeDB(); break; case "help": PrintAvailableOptions(); diff --git a/chksum.cs b/src/Chksum/chksum.cs similarity index 58% rename from chksum.cs rename to src/Chksum/chksum.cs index 7cb4265..4aa4d03 100644 --- a/chksum.cs +++ b/src/Chksum/chksum.cs @@ -1,3 +1,5 @@ +using System; +using System.IO; using Microsoft.Data.Sqlite; // Go into folder @@ -5,14 +7,15 @@ using Microsoft.Data.Sqlite; // If there is a file. Calculate md5sum > filename.md5 // If there is no file. Repeat -public class Chksum { - +namespace Chksum.Utils; +public class ChksumUtils { + // int getDirectoryCount() { // int folderCount = Directory.GetDirectories(Directory.GetCurrentDirectory()).Length; // Get folder count in current directory // return folderCount; // } - private static int getFileCount() { + private int getFileCount() { int fileCount = Directory.GetFiles(Directory.GetCurrentDirectory()).Length; // Get file count in current directory return fileCount; } @@ -22,31 +25,44 @@ public class Chksum { // return parentFolder; // } - public static string DatabaseRoot { get; set; } - public static void getBaseDir() { + public string DatabaseRoot { get; set; } + public void getBaseDir() { DatabaseRoot = AppDomain.CurrentDomain.BaseDirectory; } - - public static void initializeDB() { - using (var connection = new SqliteConnection("Data Source=chksum.db")) { - connection.Open(); + public void initializeDB() { + if (!File.Exists("chksum.db")) { + using (var connection = new SqliteConnection("Data Source=chksum.db")) { + connection.Open(); + + var command = connection.CreateCommand(); + command.CommandText = + @" + CREATE TABLE file ( + filehash TEXT NOT NULL PRIMARY KEY, + filename TEXT NOT NULL, + pathtofile TEXT NOT NULL, + artist TEXT, + playbacklength INTEGER + ); + "; + command.ExecuteNonQuery(); + } + } + } + + public void cleanDB() { + using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db")) { var command = connection.CreateCommand(); command.CommandText = @" - CREATE TABLE file ( - filehash TEXT NOT NULL PRIMARY KEY, - filename TEXT NOT NULL, - pathtofile TEXT NOT NULL, - artist TEXT, - playbacklength INTEGER - ); + vacuum; "; command.ExecuteNonQuery(); } } - private static string CalculateMD5(string filename) { + private string CalculateMD5(string filename) { using (var md5 = System.Security.Cryptography.MD5.Create()) { using (var stream = File.OpenRead(filename)) { var hash = md5.ComputeHash(stream); @@ -55,49 +71,79 @@ public class Chksum { } } - public static void doTheThing() { - foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) { - Directory.SetCurrentDirectory(directory); // Set new root - if (getFileCount() >= 1) { - DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory()); - FileInfo[] files = dir.GetFiles(); - foreach (FileInfo file in files) { - string fileName = file.Name; - string absolutePathToFile = Path.GetFullPath(fileName); - string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile); - string fileHash = CalculateMD5(fileName); + public void doTheThing() { + foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) { + Directory.SetCurrentDirectory(directory); // Set new root + if (getFileCount() >= 1) { + DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory()); + FileInfo[] files = dir.GetFiles(); + foreach (FileInfo file in files) { + string fileName = file.Name; + string absolutePathToFile = Path.GetFullPath(fileName); + string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile); + string fileHash = CalculateMD5(fileName); - using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) { - connection.Open(); + if (checkIfFileAlreadyExists(fileHash, fileName) == false) { + connection.Open(); - var command = connection.CreateCommand(); - command.CommandText = - @" + var command = connection.CreateCommand(); + command.CommandText = + @" INSERT INTO file (filehash, filename, pathtofile) VALUES ($filehash, $filename, $pathtofile) - "; - command.Parameters.AddWithValue("$filehash", fileHash); - command.Parameters.AddWithValue("$filename", fileName); - command.Parameters.AddWithValue("$pathtofile", pathToFile); - command.ExecuteNonQuery(); + "; + command.Parameters.AddWithValue("$filehash", fileHash); + command.Parameters.AddWithValue("$filename", fileName); + command.Parameters.AddWithValue("$pathtofile", pathToFile); + command.ExecuteNonQuery(); + } } } + doTheThing(); } - doTheThing(); + } + + private bool checkIfFileAlreadyExists(string fileHash, string fileName) { + string filehash = string.Empty; + string filename = string.Empty; + + using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) { + connection.Open(); + + var command = connection.CreateCommand(); + command.CommandText = + @" + SELECT filehash, filename FROM file WHERE filehash = $filehash + "; + command.Parameters.AddWithValue("$filehash", fileHash); + + using (var reader = command.ExecuteReader()) { + while (reader.Read()) { + filehash = reader.GetString(0); + filename = reader.GetString(1); + } + } + } + + if (fileHash == filehash) { + Console.WriteLine($"Duplicate files found: {fileName} with the hash {fileHash} is identical to {filename} with the hash {filehash}"); + return true; + } else { + return false; } } - private static int getTotalFileCount() { + private int getTotalFileCount() { int totalFileCount = Directory.GetFiles(Directory.GetCurrentDirectory(), "*", SearchOption.AllDirectories).Length; return totalFileCount - 1; // Remove the program from the totalFileCount } - public static void countAllMd5Checksums() { + public void countAllMd5Checksums() { int totalMD5FileCount = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.md5", SearchOption.AllDirectories).Length; Console.WriteLine("There are " + totalMD5FileCount + " md5 checksums"); } - public static void deleteAllMd5Checksums() { + public void deleteAllMd5Checksums() { foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) { Directory.SetCurrentDirectory(directory); // Set new root if (getFileCount() >= 1) { @@ -115,7 +161,7 @@ public class Chksum { } } - public static void compareChecksums() { + public void compareChecksums() { foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) { Directory.SetCurrentDirectory(directory); // Set new root if (getFileCount() >= 1) { diff --git a/debugchksum.cs b/src/Chksum/debugchksum.cs similarity index 100% rename from debugchksum.cs rename to src/Chksum/debugchksum.cs diff --git a/src/chksum.sln b/src/chksum.sln new file mode 100644 index 0000000..b5f119a --- /dev/null +++ b/src/chksum.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chksum", "Chksum\Chksum.csproj", "{BBC56294-03CF-42E0-A838-75AF41EEE32B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chksum.Tests", "Chksum.Tests\Chksum.Tests.csproj", "{239727BC-7124-4985-A6F3-2700295AA06F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BBC56294-03CF-42E0-A838-75AF41EEE32B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BBC56294-03CF-42E0-A838-75AF41EEE32B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BBC56294-03CF-42E0-A838-75AF41EEE32B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BBC56294-03CF-42E0-A838-75AF41EEE32B}.Release|Any CPU.Build.0 = Release|Any CPU + {239727BC-7124-4985-A6F3-2700295AA06F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {239727BC-7124-4985-A6F3-2700295AA06F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {239727BC-7124-4985-A6F3-2700295AA06F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {239727BC-7124-4985-A6F3-2700295AA06F}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal From 64995434ababc74547e24f2e8399d9d58153fb7f Mon Sep 17 00:00:00 2001 From: ProfessionalUwU Date: Sun, 25 Jun 2023 03:28:34 +0200 Subject: [PATCH 05/14] Check if file moved If the file moved the path will be updated --- src/Chksum/chksum.cs | 57 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/src/Chksum/chksum.cs b/src/Chksum/chksum.cs index 4aa4d03..bdb8c05 100644 --- a/src/Chksum/chksum.cs +++ b/src/Chksum/chksum.cs @@ -83,15 +83,15 @@ public class ChksumUtils { string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile); string fileHash = CalculateMD5(fileName); - if (checkIfFileAlreadyExists(fileHash, fileName) == false) { + if (checkIfFileMovedAndUpdatePathToFile(fileHash, fileName, pathToFile) == false && checkIfFileAlreadyExists(fileHash, fileName) == false) { connection.Open(); var command = connection.CreateCommand(); command.CommandText = @" - INSERT INTO file (filehash, filename, pathtofile) - VALUES ($filehash, $filename, $pathtofile) - "; + INSERT INTO file (filehash, filename, pathtofile) + VALUES ($filehash, $filename, $pathtofile) + "; command.Parameters.AddWithValue("$filehash", fileHash); command.Parameters.AddWithValue("$filename", fileName); command.Parameters.AddWithValue("$pathtofile", pathToFile); @@ -103,9 +103,9 @@ public class ChksumUtils { } } - private bool checkIfFileAlreadyExists(string fileHash, string fileName) { + private bool checkIfFileAlreadyExists(string fileHash, string pathToFile) { string filehash = string.Empty; - string filename = string.Empty; + string pathtofile = string.Empty; using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) { connection.Open(); @@ -113,26 +113,65 @@ public class ChksumUtils { var command = connection.CreateCommand(); command.CommandText = @" - SELECT filehash, filename FROM file WHERE filehash = $filehash + SELECT filehash, pathtofile FROM file WHERE filehash = $filehash "; command.Parameters.AddWithValue("$filehash", fileHash); using (var reader = command.ExecuteReader()) { while (reader.Read()) { filehash = reader.GetString(0); - filename = reader.GetString(1); + pathtofile = reader.GetString(1); } } } if (fileHash == filehash) { - Console.WriteLine($"Duplicate files found: {fileName} with the hash {fileHash} is identical to {filename} with the hash {filehash}"); + Console.WriteLine($"Duplicate files found: {pathToFile} with the hash {fileHash} is identical to {pathtofile} with the hash {filehash}"); return true; } else { return false; } } + private bool checkIfFileMovedAndUpdatePathToFile(string fileHash, string fileName, string pathToFile) { + string pathtofile = string.Empty; + + using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) { + connection.Open(); + + var command = connection.CreateCommand(); + command.CommandText = + @" + SELECT pathtofile FROM file WHERE filehash = $filehash + "; + command.Parameters.AddWithValue("$filehash", fileHash); + + using (var reader = command.ExecuteReader()) { + while (reader.Read()) { + pathtofile = reader.GetString(0); + } + } + + if (pathToFile != pathtofile && pathtofile != "") { + var command2 = connection.CreateCommand(); + command2.CommandText = + @" + UPDATE file + SET pathtofile = $newpathtofile + WHERE filehash = $filehash + "; + command2.Parameters.AddWithValue("$newpathtofile", pathToFile); + command2.Parameters.AddWithValue("$filehash", fileHash); + command2.ExecuteNonQuery(); + + Console.WriteLine($"File moved: {fileName} was previously at {pathtofile} but is now at {pathToFile}"); + return true; + } else { + return false; + } + } + } + private int getTotalFileCount() { int totalFileCount = Directory.GetFiles(Directory.GetCurrentDirectory(), "*", SearchOption.AllDirectories).Length; return totalFileCount - 1; // Remove the program from the totalFileCount From b05ddd5e18760dabcd79434917332d4c83363814 Mon Sep 17 00:00:00 2001 From: ProfessionalUwU Date: Sun, 25 Jun 2023 03:32:43 +0200 Subject: [PATCH 06/14] Fix formatting --- src/Chksum/chksum.cs | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Chksum/chksum.cs b/src/Chksum/chksum.cs index bdb8c05..d951feb 100644 --- a/src/Chksum/chksum.cs +++ b/src/Chksum/chksum.cs @@ -73,34 +73,34 @@ public class ChksumUtils { public void doTheThing() { foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) { - Directory.SetCurrentDirectory(directory); // Set new root - if (getFileCount() >= 1) { - DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory()); - FileInfo[] files = dir.GetFiles(); - foreach (FileInfo file in files) { - string fileName = file.Name; - string absolutePathToFile = Path.GetFullPath(fileName); - string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile); - string fileHash = CalculateMD5(fileName); + Directory.SetCurrentDirectory(directory); // Set new root + if (getFileCount() >= 1) { + DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory()); + FileInfo[] files = dir.GetFiles(); + foreach (FileInfo file in files) { + string fileName = file.Name; + string absolutePathToFile = Path.GetFullPath(fileName); + string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile); + string fileHash = CalculateMD5(fileName); - if (checkIfFileMovedAndUpdatePathToFile(fileHash, fileName, pathToFile) == false && checkIfFileAlreadyExists(fileHash, fileName) == false) { - connection.Open(); + if (checkIfFileMovedAndUpdatePathToFile(fileHash, fileName, pathToFile) == false && checkIfFileAlreadyExists(fileHash, fileName) == false) { + connection.Open(); - var command = connection.CreateCommand(); - command.CommandText = - @" - INSERT INTO file (filehash, filename, pathtofile) - VALUES ($filehash, $filename, $pathtofile) + var command = connection.CreateCommand(); + command.CommandText = + @" + INSERT INTO file (filehash, filename, pathtofile) + VALUES ($filehash, $filename, $pathtofile) "; - command.Parameters.AddWithValue("$filehash", fileHash); - command.Parameters.AddWithValue("$filename", fileName); - command.Parameters.AddWithValue("$pathtofile", pathToFile); - command.ExecuteNonQuery(); - } + command.Parameters.AddWithValue("$filehash", fileHash); + command.Parameters.AddWithValue("$filename", fileName); + command.Parameters.AddWithValue("$pathtofile", pathToFile); + command.ExecuteNonQuery(); } } - doTheThing(); } + doTheThing(); + } } private bool checkIfFileAlreadyExists(string fileHash, string pathToFile) { From 132894e924d825f6974bbf99fe65714430377d79 Mon Sep 17 00:00:00 2001 From: ProfessionalUwU Date: Sun, 25 Jun 2023 16:48:43 +0200 Subject: [PATCH 07/14] Minor fixes --- README.md | 19 +++++++++++++++---- justfile | 2 +- src/Chksum/Program.cs | 1 - src/Chksum/chksum.cs | 8 -------- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 1d40e9a..7cdb8a5 100644 --- a/README.md +++ b/README.md @@ -25,22 +25,33 @@ pacman -S dotnet-runtime dotnet-sdk Build project ```bash -dotnet build chksum.csproj +just build ``` Publish project ```bash -dotnet publish --configuration Release chksum.csproj +just publish ``` Go to the publish folder ```bash -cd 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 ```bash -./chksum +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 \ No newline at end of file diff --git a/justfile b/justfile index 8ad837f..6b0b79a 100644 --- a/justfile +++ b/justfile @@ -20,7 +20,7 @@ build: @dotnet build src/{{uppercase_project_name}}/{{uppercase_project_name}}.csproj @dotnet build src/{{uppercase_project_name}}.Tests/{{uppercase_project_name}}.Tests.csproj -publish: format +publish: @dotnet publish --configuration Release src/{{uppercase_project_name}}/{{uppercase_project_name}}.csproj format: diff --git a/src/Chksum/Program.cs b/src/Chksum/Program.cs index 15e4b64..67459a6 100644 --- a/src/Chksum/Program.cs +++ b/src/Chksum/Program.cs @@ -1,4 +1,3 @@ -using System; using Chksum.Utils; public class Program { diff --git a/src/Chksum/chksum.cs b/src/Chksum/chksum.cs index d951feb..9cf007e 100644 --- a/src/Chksum/chksum.cs +++ b/src/Chksum/chksum.cs @@ -1,12 +1,4 @@ -using System; -using System.IO; using Microsoft.Data.Sqlite; - -// Go into folder -// Check if any file is in there -// If there is a file. Calculate md5sum > filename.md5 -// If there is no file. Repeat - namespace Chksum.Utils; public class ChksumUtils { From c51e02fa05bc3f60fccf6f5a1458e8be6750e381 Mon Sep 17 00:00:00 2001 From: ProfessionalUwU Date: Sun, 25 Jun 2023 21:12:42 +0200 Subject: [PATCH 08/14] Add library Embed the library into the executable and extract at runtime Cleanup after executing the program --- .gitignore | 1 + src/Chksum/Chksum.csproj | 1 + src/Chksum/Program.cs | 4 ++++ src/Chksum/chksum.cs | 22 +++++++++++++++++++++- 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index e68f0aa..7576ac2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .vscode/ obj/ bin/ +Libraries/ diff --git a/src/Chksum/Chksum.csproj b/src/Chksum/Chksum.csproj index 6db87ee..fc84e00 100644 --- a/src/Chksum/Chksum.csproj +++ b/src/Chksum/Chksum.csproj @@ -2,6 +2,7 @@ + diff --git a/src/Chksum/Program.cs b/src/Chksum/Program.cs index 67459a6..dde1d7c 100644 --- a/src/Chksum/Program.cs +++ b/src/Chksum/Program.cs @@ -17,6 +17,8 @@ public class Program { utils.getBaseDir(); + utils.ExtractEmbeddedLibrary(); + Console.ForegroundColor = ConsoleColor.Green; switch (args[0]) { case "checksum": @@ -64,6 +66,8 @@ public class Program { PrintAvailableOptions(); break; } + + utils.cleanup(); } static void PrintAvailableOptions() { diff --git a/src/Chksum/chksum.cs b/src/Chksum/chksum.cs index 9cf007e..dfc5c07 100644 --- a/src/Chksum/chksum.cs +++ b/src/Chksum/chksum.cs @@ -1,3 +1,4 @@ +using System.Reflection; using Microsoft.Data.Sqlite; namespace Chksum.Utils; public class ChksumUtils { @@ -17,11 +18,30 @@ public class ChksumUtils { // return parentFolder; // } - public string DatabaseRoot { get; set; } + public string DatabaseRoot { get; set; } = string.Empty; public void getBaseDir() { DatabaseRoot = AppDomain.CurrentDomain.BaseDirectory; } + public string libraryPath { get; set; } = string.Empty; + public void ExtractEmbeddedLibrary() { + libraryPath = Path.Combine(DatabaseRoot, "libe_sqlite3.so"); + + using (Stream? resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Chksum.Libraries.libe_sqlite3.so")) { + if (resourceStream != null) { + byte[] buffer = new byte[resourceStream.Length]; + resourceStream.Read(buffer, 0, buffer.Length); + File.WriteAllBytes(libraryPath, buffer); + } else { + throw new Exception(libraryPath + " could not be loaded"); + } + } + } + + public void cleanup() { + File.Delete(libraryPath); + } + public void initializeDB() { if (!File.Exists("chksum.db")) { using (var connection = new SqliteConnection("Data Source=chksum.db")) { From 8aec6497818fa7e3b10a2848056f49b024227fa4 Mon Sep 17 00:00:00 2001 From: AustrianToast Date: Sun, 25 Jun 2023 22:37:50 +0200 Subject: [PATCH 09/14] code cleanup removed unused methods changed output of methods --- src/Chksum/Program.cs | 23 +--------- src/Chksum/chksum.cs | 103 +++++++++++++++--------------------------- 2 files changed, 37 insertions(+), 89 deletions(-) diff --git a/src/Chksum/Program.cs b/src/Chksum/Program.cs index dde1d7c..770272d 100644 --- a/src/Chksum/Program.cs +++ b/src/Chksum/Program.cs @@ -30,24 +30,6 @@ public class Program { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Checksum process finished"); break; - case "countmd5": - Console.WriteLine("Counting md5 checksum files."); - Console.ResetColor(); - - utils.countAllMd5Checksums(); - - Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine("Finished counting all md5 checksum files."); - break; - case "deletemd5": - Console.WriteLine("Deleting all md5 checksum files."); - Console.ResetColor(); - - utils.deleteAllMd5Checksums(); - - Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine("Deleted all md5 checksum files."); - break; case "compareChecksums": Console.WriteLine("Comparing all md5 checksum files. If there is none, creating one."); Console.ResetColor(); @@ -73,16 +55,13 @@ public class Program { static void PrintAvailableOptions() { String[] options = { "checksum", - "countmd5", - "deletemd5", "compareChecksums", "createDB", "help" }; Console.ResetColor(); - Console.WriteLine("usage: chksum [option]"); - Console.WriteLine("Here is a list of all available options:"); + Console.WriteLine("usage: chksum [option] \nHere is a list of all available options:"); foreach (String option in options) { Console.WriteLine("\t" + option); } diff --git a/src/Chksum/chksum.cs b/src/Chksum/chksum.cs index dfc5c07..b8bb9a0 100644 --- a/src/Chksum/chksum.cs +++ b/src/Chksum/chksum.cs @@ -3,21 +3,11 @@ using Microsoft.Data.Sqlite; namespace Chksum.Utils; public class ChksumUtils { - // int getDirectoryCount() { - // int folderCount = Directory.GetDirectories(Directory.GetCurrentDirectory()).Length; // Get folder count in current directory - // return folderCount; - // } - private int getFileCount() { int fileCount = Directory.GetFiles(Directory.GetCurrentDirectory()).Length; // Get file count in current directory return fileCount; } - // string getParentFolder() { - // string parentFolder = Directory.GetParent(Directory.GetCurrentDirectory()).ToString(); // Get parent folder of current directory - // return parentFolder; - // } - public string DatabaseRoot { get; set; } = string.Empty; public void getBaseDir() { DatabaseRoot = AppDomain.CurrentDomain.BaseDirectory; @@ -38,28 +28,26 @@ public class ChksumUtils { } } - public void cleanup() { - File.Delete(libraryPath); - } - public void initializeDB() { - if (!File.Exists("chksum.db")) { - using (var connection = new SqliteConnection("Data Source=chksum.db")) { - connection.Open(); + if (File.Exists("chksum.db")) { + return; + } - var command = connection.CreateCommand(); - command.CommandText = - @" - CREATE TABLE file ( - filehash TEXT NOT NULL PRIMARY KEY, - filename TEXT NOT NULL, - pathtofile TEXT NOT NULL, - artist TEXT, - playbacklength INTEGER - ); - "; - command.ExecuteNonQuery(); - } + using (var connection = new SqliteConnection("Data Source=chksum.db")) { + connection.Open(); + + var command = connection.CreateCommand(); + command.CommandText = + @" + CREATE TABLE file ( + filehash TEXT NOT NULL PRIMARY KEY, + filename TEXT NOT NULL, + pathtofile TEXT NOT NULL, + artist TEXT, + playbacklength INTEGER + ); + "; + command.ExecuteNonQuery(); } } @@ -84,7 +72,8 @@ public class ChksumUtils { } public void doTheThing() { - foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) { + foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) + using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) { Directory.SetCurrentDirectory(directory); // Set new root if (getFileCount() >= 1) { DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory()); @@ -118,6 +107,7 @@ public class ChksumUtils { private bool checkIfFileAlreadyExists(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")) { connection.Open(); @@ -138,15 +128,17 @@ public class ChksumUtils { } if (fileHash == filehash) { - Console.WriteLine($"Duplicate files found: {pathToFile} with the hash {fileHash} is identical to {pathtofile} with the hash {filehash}"); - return true; - } else { - return false; + Console.WriteLine("Duplicate files found:"); + Console.WriteLine($"\toriginal\t{pathToFile}"); + Console.WriteLine($"\tduplicate\t{pathtofile}\n"); + doesExist = true; } + return doesExist; } private bool checkIfFileMovedAndUpdatePathToFile(string fileHash, string fileName, string pathToFile) { string pathtofile = string.Empty; + bool wasMoved = false; using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) { connection.Open(); @@ -176,39 +168,12 @@ public class ChksumUtils { command2.Parameters.AddWithValue("$filehash", fileHash); command2.ExecuteNonQuery(); - Console.WriteLine($"File moved: {fileName} was previously at {pathtofile} but is now at {pathToFile}"); - return true; - } else { - return false; + Console.WriteLine("File moved:"); + Console.WriteLine($"\tfrom\t{pathToFile}"); + Console.WriteLine($"\tto \t{pathtofile}\n"); + wasMoved = true; } - } - } - - private int getTotalFileCount() { - int totalFileCount = Directory.GetFiles(Directory.GetCurrentDirectory(), "*", SearchOption.AllDirectories).Length; - return totalFileCount - 1; // Remove the program from the totalFileCount - } - - public void countAllMd5Checksums() { - int totalMD5FileCount = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.md5", SearchOption.AllDirectories).Length; - Console.WriteLine("There are " + totalMD5FileCount + " md5 checksums"); - } - - public void deleteAllMd5Checksums() { - foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) { - Directory.SetCurrentDirectory(directory); // Set new root - if (getFileCount() >= 1) { - DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory()); - FileInfo[] files = dir.GetFiles(); - foreach (FileInfo file in files) { - string fileName = file.Name; - string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName); - string checksumFile = Directory.GetCurrentDirectory() + "/" + fileNameWithoutExtension + ".md5"; - File.Delete(checksumFile); - Console.WriteLine("Deleted " + checksumFile); - } - } - deleteAllMd5Checksums(); + return wasMoved; } } @@ -246,4 +211,8 @@ public class ChksumUtils { compareChecksums(); } } + + public void cleanup() { + File.Delete(libraryPath); + } } \ No newline at end of file From 2d42842fb21cdc41b45d1333b8621d614524e13a Mon Sep 17 00:00:00 2001 From: ProfessionalUwU Date: Mon, 26 Jun 2023 12:58:05 +0200 Subject: [PATCH 10/14] Minor tweaks --- README.md | 15 ++------------- src/Chksum/chksum.cs | 11 ++++------- 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 7cdb8a5..489a5da 100644 --- a/README.md +++ b/README.md @@ -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 \ No newline at end of file +./Chksum +``` \ No newline at end of file diff --git a/src/Chksum/chksum.cs b/src/Chksum/chksum.cs index b8bb9a0..cdcb54d 100644 --- a/src/Chksum/chksum.cs +++ b/src/Chksum/chksum.cs @@ -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,7 @@ public class ChksumUtils { } } - public void compareChecksums() { + public void compareChecksums() { // reuse for database comparison foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) { Directory.SetCurrentDirectory(directory); // Set new root if (getFileCount() >= 1) { From b899c4c5b611799e3802098845308302bda7308f Mon Sep 17 00:00:00 2001 From: ProfessionalUwU Date: Mon, 26 Jun 2023 15:59:06 +0200 Subject: [PATCH 11/14] Add option to check for deleted files --- src/Chksum/Program.cs | 5 +++++ src/Chksum/chksum.cs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/Chksum/Program.cs b/src/Chksum/Program.cs index 770272d..8b88d55 100644 --- a/src/Chksum/Program.cs +++ b/src/Chksum/Program.cs @@ -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" }; diff --git a/src/Chksum/chksum.cs b/src/Chksum/chksum.cs index cdcb54d..421bdff 100644 --- a/src/Chksum/chksum.cs +++ b/src/Chksum/chksum.cs @@ -174,6 +174,40 @@ public class ChksumUtils { } } + 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 From 6fbc53fa53d15dfd5192342a3a97f7ebc28f74d6 Mon Sep 17 00:00:00 2001 From: ProfessionalUwU Date: Mon, 26 Jun 2023 16:25:42 +0200 Subject: [PATCH 12/14] Add option to compare databases --- src/Chksum/Program.cs | 7 ++-- src/Chksum/chksum.cs | 92 +++++++++++++++++++++++++++++-------------- 2 files changed, 66 insertions(+), 33 deletions(-) diff --git a/src/Chksum/Program.cs b/src/Chksum/Program.cs index 8b88d55..613c886 100644 --- a/src/Chksum/Program.cs +++ b/src/Chksum/Program.cs @@ -8,7 +8,7 @@ public class Program { Console.WriteLine("Please specify an option."); PrintAvailableOptions(); return; - } else if (args.Length > 1) { + } else if (args.Length > 1 && args[0] != "compareDatabases") { Console.WriteLine("Too many options."); return; } @@ -30,11 +30,10 @@ public class Program { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Checksum process finished"); break; - case "compareChecksums": - Console.WriteLine("Comparing all md5 checksum files. If there is none, creating one."); + case "compareDatabases": Console.ResetColor(); - utils.compareChecksums(); + utils.compareDatabases(args[1]); break; case "createDB": utils.initializeDB(); diff --git a/src/Chksum/chksum.cs b/src/Chksum/chksum.cs index 421bdff..8b165a9 100644 --- a/src/Chksum/chksum.cs +++ b/src/Chksum/chksum.cs @@ -208,38 +208,72 @@ public class ChksumUtils { } } - public void compareChecksums() { // reuse for database comparison - foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) { - Directory.SetCurrentDirectory(directory); // Set new root - if (getFileCount() >= 1) { - DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory()); - FileInfo[] files = dir.GetFiles(); - // files.ToList().ForEach(i => Console.WriteLine(i.ToString())); // Print all files in files array - foreach (FileInfo file in files) { - string fileName = file.Name; - string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName); - string checksumFile = Directory.GetCurrentDirectory() + "/" + fileNameWithoutExtension + ".md5"; - string fileMd5Checksum = fileNameWithoutExtension + ".md5"; - if (File.Exists(fileMd5Checksum)) { - string newFileChecksum = CalculateMD5(fileName) + " " + fileName; - string existingFileChecksum = File.ReadAllText(fileMd5Checksum); - string newFileName = newFileChecksum.Substring(34); - string existingFileName = existingFileChecksum.Substring(34); - if (newFileChecksum.Equals(existingFileChecksum)) { - Console.WriteLine(newFileName + " and " + existingFileName + " are the same."); - } else { - Console.WriteLine(newFileName + " and " + existingFileName + " are not the same."); - Console.WriteLine("The checksum of " + newFileName + " is " + newFileChecksum); - Console.WriteLine("The checksum of the already exting file " + existingFileName + " is " + existingFileChecksum); - // TODO Tell the user to check which file is the correct one - } - } else { - File.AppendAllText(checksumFile, CalculateMD5(fileName) + " " + fileName); - Console.WriteLine("Calculated checksum for: " + checksumFile); + public void compareDatabases(string filePathToOtherDatabase) { + List filehashesOfOriginDatabase = new List(); + using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadOnly")) { + 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); + filehashesOfOriginDatabase.Add(filehash); + } + } + } + + List filehashesOfRemoteDatabase = new List(); + using (var connection = new SqliteConnection("Data Source=" + filePathToOtherDatabase + ";Mode=ReadOnly")) { + 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); + filehashesOfRemoteDatabase.Add(filehash); + } + } + } + + List filesThatDoNotExistsInTheRemote = filehashesOfOriginDatabase.Except(filehashesOfRemoteDatabase).ToList(); + //List 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(); } } From 944b61a1ace562a703a8ca439b865ae69ae7b355 Mon Sep 17 00:00:00 2001 From: ProfessionalUwU Date: Mon, 26 Jun 2023 17:21:56 +0200 Subject: [PATCH 13/14] Refactor compareDatabases --- src/Chksum/chksum.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Chksum/chksum.cs b/src/Chksum/chksum.cs index 8b165a9..38fe66e 100644 --- a/src/Chksum/chksum.cs +++ b/src/Chksum/chksum.cs @@ -208,7 +208,7 @@ public class ChksumUtils { } } - public void compareDatabases(string filePathToOtherDatabase) { + private List getFilehashesOfOriginDatabase() { List filehashesOfOriginDatabase = new List(); using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadOnly")) { string filehash = string.Empty; @@ -229,6 +229,10 @@ public class ChksumUtils { } } + return filehashesOfOriginDatabase; + } + + private List getFilehashesOfRemoteDatabase(string filePathToOtherDatabase) { List filehashesOfRemoteDatabase = new List(); using (var connection = new SqliteConnection("Data Source=" + filePathToOtherDatabase + ";Mode=ReadOnly")) { string filehash = string.Empty; @@ -249,7 +253,11 @@ public class ChksumUtils { } } - List filesThatDoNotExistsInTheRemote = filehashesOfOriginDatabase.Except(filehashesOfRemoteDatabase).ToList(); + return filehashesOfRemoteDatabase; + } + + public void compareDatabases(string filePathToOtherDatabase) { + List filesThatDoNotExistsInTheRemote = getFilehashesOfOriginDatabase().Except(getFilehashesOfRemoteDatabase(filePathToOtherDatabase)).ToList(); //List filesThatDoNotExistsInTheOrigin = filehashesOfRemoteDatabase.Except(filehashesOfOriginDatabase).ToList(); foreach (string file in filesThatDoNotExistsInTheRemote) { From 836b850f3f7ec262b30ab1ea37285df03ab36a66 Mon Sep 17 00:00:00 2001 From: ProfessionalUwU Date: Mon, 26 Jun 2023 18:20:47 +0200 Subject: [PATCH 14/14] Refactor getFilehashesFromDatabase --- src/Chksum/chksum.cs | 37 +++++++------------------------------ 1 file changed, 7 insertions(+), 30 deletions(-) diff --git a/src/Chksum/chksum.cs b/src/Chksum/chksum.cs index 38fe66e..8f0f1a6 100644 --- a/src/Chksum/chksum.cs +++ b/src/Chksum/chksum.cs @@ -208,9 +208,10 @@ public class ChksumUtils { } } - private List getFilehashesOfOriginDatabase() { - List filehashesOfOriginDatabase = new List(); - using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadOnly")) { + private List getFilehashesFromDatabase(string connectionString) { + List filehashesFromDatabase = new List(); + + using (var connection = new SqliteConnection(connectionString)) { string filehash = string.Empty; connection.Open(); @@ -224,40 +225,16 @@ public class ChksumUtils { using (var reader = selectCommand.ExecuteReader()) { while (reader.Read()) { filehash = reader.GetString(0); - filehashesOfOriginDatabase.Add(filehash); + filehashesFromDatabase.Add(filehash); } } } - return filehashesOfOriginDatabase; - } - - private List getFilehashesOfRemoteDatabase(string filePathToOtherDatabase) { - List filehashesOfRemoteDatabase = new List(); - using (var connection = new SqliteConnection("Data Source=" + filePathToOtherDatabase + ";Mode=ReadOnly")) { - 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); - filehashesOfRemoteDatabase.Add(filehash); - } - } - } - - return filehashesOfRemoteDatabase; + return filehashesFromDatabase; } public void compareDatabases(string filePathToOtherDatabase) { - List filesThatDoNotExistsInTheRemote = getFilehashesOfOriginDatabase().Except(getFilehashesOfRemoteDatabase(filePathToOtherDatabase)).ToList(); + List filesThatDoNotExistsInTheRemote = getFilehashesFromDatabase("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadOnly").Except(getFilehashesFromDatabase("Data Source=" + filePathToOtherDatabase + ";Mode=ReadOnly")).ToList(); //List filesThatDoNotExistsInTheOrigin = filehashesOfRemoteDatabase.Except(filehashesOfOriginDatabase).ToList(); foreach (string file in filesThatDoNotExistsInTheRemote) {