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
This commit is contained in:
parent
846b983caa
commit
5a3becb3b4
@ -10,7 +10,7 @@ indent_style = space
|
|||||||
tab_width = 4
|
tab_width = 4
|
||||||
|
|
||||||
# Naming Conventions
|
# Naming Conventions
|
||||||
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
|
dotnet_naming_style.camel_case.capitalization = camel_case
|
||||||
|
|
||||||
# New line preferences
|
# New line preferences
|
||||||
csharp_new_line_before_open_brace = none
|
csharp_new_line_before_open_brace = none
|
||||||
|
33
justfile
33
justfile
@ -1,2 +1,31 @@
|
|||||||
publish:
|
default:
|
||||||
@dotnet publish --configuration Release chksum.csproj
|
@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
|
||||||
|
25
src/Chksum.Tests/Chksum.Tests.csproj
Normal file
25
src/Chksum.Tests/Chksum.Tests.csproj
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net7.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="FluentAssertions" Version="6.11.0" />
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
|
||||||
|
<PackageReference Include="xunit" Version="2.4.2" />
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="coverlet.collector" Version="3.1.2">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
2
src/Chksum.Tests/Usings.cs
Normal file
2
src/Chksum.Tests/Usings.cs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
global using Xunit;
|
||||||
|
global using FluentAssertions;
|
8
src/Chksum.Tests/doTheThingTest.cs
Normal file
8
src/Chksum.Tests/doTheThingTest.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
namespace Chksum.Tests;
|
||||||
|
|
||||||
|
public class doTheThingTest {
|
||||||
|
[Fact]
|
||||||
|
public void doTheThing_willNotThrowAnException() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,9 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Chksum.Tests\Chksum.Tests.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net7.0</TargetFramework>
|
<TargetFramework>net7.0</TargetFramework>
|
@ -1,3 +1,6 @@
|
|||||||
|
using System;
|
||||||
|
using Chksum.Utils;
|
||||||
|
|
||||||
public class Program {
|
public class Program {
|
||||||
static void Main(string[] args) {
|
static void Main(string[] args) {
|
||||||
|
|
||||||
@ -11,7 +14,9 @@ public class Program {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Chksum.getBaseDir();
|
ChksumUtils utils = new ChksumUtils();
|
||||||
|
|
||||||
|
utils.getBaseDir();
|
||||||
|
|
||||||
Console.ForegroundColor = ConsoleColor.Green;
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
switch (args[0]) {
|
switch (args[0]) {
|
||||||
@ -19,7 +24,7 @@ public class Program {
|
|||||||
Console.WriteLine("Starting the checksum process.");
|
Console.WriteLine("Starting the checksum process.");
|
||||||
Console.ResetColor();
|
Console.ResetColor();
|
||||||
|
|
||||||
Chksum.doTheThing();
|
utils.doTheThing();
|
||||||
|
|
||||||
Console.ForegroundColor = ConsoleColor.Green;
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
Console.WriteLine("Checksum process finished");
|
Console.WriteLine("Checksum process finished");
|
||||||
@ -28,7 +33,7 @@ public class Program {
|
|||||||
Console.WriteLine("Counting md5 checksum files.");
|
Console.WriteLine("Counting md5 checksum files.");
|
||||||
Console.ResetColor();
|
Console.ResetColor();
|
||||||
|
|
||||||
Chksum.countAllMd5Checksums();
|
utils.countAllMd5Checksums();
|
||||||
|
|
||||||
Console.ForegroundColor = ConsoleColor.Green;
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
Console.WriteLine("Finished counting all md5 checksum files.");
|
Console.WriteLine("Finished counting all md5 checksum files.");
|
||||||
@ -37,7 +42,7 @@ public class Program {
|
|||||||
Console.WriteLine("Deleting all md5 checksum files.");
|
Console.WriteLine("Deleting all md5 checksum files.");
|
||||||
Console.ResetColor();
|
Console.ResetColor();
|
||||||
|
|
||||||
Chksum.deleteAllMd5Checksums();
|
utils.deleteAllMd5Checksums();
|
||||||
|
|
||||||
Console.ForegroundColor = ConsoleColor.Green;
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
Console.WriteLine("Deleted all md5 checksum files.");
|
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.WriteLine("Comparing all md5 checksum files. If there is none, creating one.");
|
||||||
Console.ResetColor();
|
Console.ResetColor();
|
||||||
|
|
||||||
Chksum.compareChecksums();
|
utils.compareChecksums();
|
||||||
break;
|
break;
|
||||||
case "createDB":
|
case "createDB":
|
||||||
Chksum.initializeDB();
|
utils.initializeDB();
|
||||||
break;
|
break;
|
||||||
case "help":
|
case "help":
|
||||||
PrintAvailableOptions();
|
PrintAvailableOptions();
|
@ -1,3 +1,5 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
using Microsoft.Data.Sqlite;
|
using Microsoft.Data.Sqlite;
|
||||||
|
|
||||||
// Go into folder
|
// Go into folder
|
||||||
@ -5,14 +7,15 @@ using Microsoft.Data.Sqlite;
|
|||||||
// If there is a file. Calculate md5sum > filename.md5
|
// If there is a file. Calculate md5sum > filename.md5
|
||||||
// If there is no file. Repeat
|
// If there is no file. Repeat
|
||||||
|
|
||||||
public class Chksum {
|
namespace Chksum.Utils;
|
||||||
|
public class ChksumUtils {
|
||||||
|
|
||||||
// int getDirectoryCount() {
|
// int getDirectoryCount() {
|
||||||
// int folderCount = Directory.GetDirectories(Directory.GetCurrentDirectory()).Length; // Get folder count in current directory
|
// int folderCount = Directory.GetDirectories(Directory.GetCurrentDirectory()).Length; // Get folder count in current directory
|
||||||
// return folderCount;
|
// return folderCount;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
private static int getFileCount() {
|
private int getFileCount() {
|
||||||
int fileCount = Directory.GetFiles(Directory.GetCurrentDirectory()).Length; // Get file count in current directory
|
int fileCount = Directory.GetFiles(Directory.GetCurrentDirectory()).Length; // Get file count in current directory
|
||||||
return fileCount;
|
return fileCount;
|
||||||
}
|
}
|
||||||
@ -22,12 +25,13 @@ public class Chksum {
|
|||||||
// return parentFolder;
|
// return parentFolder;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
public static string DatabaseRoot { get; set; }
|
public string DatabaseRoot { get; set; }
|
||||||
public static void getBaseDir() {
|
public void getBaseDir() {
|
||||||
DatabaseRoot = AppDomain.CurrentDomain.BaseDirectory;
|
DatabaseRoot = AppDomain.CurrentDomain.BaseDirectory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void initializeDB() {
|
public void initializeDB() {
|
||||||
|
if (!File.Exists("chksum.db")) {
|
||||||
using (var connection = new SqliteConnection("Data Source=chksum.db")) {
|
using (var connection = new SqliteConnection("Data Source=chksum.db")) {
|
||||||
connection.Open();
|
connection.Open();
|
||||||
|
|
||||||
@ -45,8 +49,20 @@ public class Chksum {
|
|||||||
command.ExecuteNonQuery();
|
command.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static string CalculateMD5(string filename) {
|
public void cleanDB() {
|
||||||
|
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db")) {
|
||||||
|
var command = connection.CreateCommand();
|
||||||
|
command.CommandText =
|
||||||
|
@"
|
||||||
|
vacuum;
|
||||||
|
";
|
||||||
|
command.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private string CalculateMD5(string filename) {
|
||||||
using (var md5 = System.Security.Cryptography.MD5.Create()) {
|
using (var md5 = System.Security.Cryptography.MD5.Create()) {
|
||||||
using (var stream = File.OpenRead(filename)) {
|
using (var stream = File.OpenRead(filename)) {
|
||||||
var hash = md5.ComputeHash(stream);
|
var hash = md5.ComputeHash(stream);
|
||||||
@ -55,8 +71,8 @@ public class Chksum {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void doTheThing() {
|
public void doTheThing() {
|
||||||
foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) {
|
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
|
Directory.SetCurrentDirectory(directory); // Set new root
|
||||||
if (getFileCount() >= 1) {
|
if (getFileCount() >= 1) {
|
||||||
DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
|
DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
|
||||||
@ -67,7 +83,7 @@ public class Chksum {
|
|||||||
string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile);
|
string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile);
|
||||||
string fileHash = CalculateMD5(fileName);
|
string fileHash = CalculateMD5(fileName);
|
||||||
|
|
||||||
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
|
if (checkIfFileAlreadyExists(fileHash, fileName) == false) {
|
||||||
connection.Open();
|
connection.Open();
|
||||||
|
|
||||||
var command = connection.CreateCommand();
|
var command = connection.CreateCommand();
|
||||||
@ -87,17 +103,47 @@ public class Chksum {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int getTotalFileCount() {
|
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 int getTotalFileCount() {
|
||||||
int totalFileCount = Directory.GetFiles(Directory.GetCurrentDirectory(), "*", SearchOption.AllDirectories).Length;
|
int totalFileCount = Directory.GetFiles(Directory.GetCurrentDirectory(), "*", SearchOption.AllDirectories).Length;
|
||||||
return totalFileCount - 1; // Remove the program from the totalFileCount
|
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;
|
int totalMD5FileCount = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.md5", SearchOption.AllDirectories).Length;
|
||||||
Console.WriteLine("There are " + totalMD5FileCount + " md5 checksums");
|
Console.WriteLine("There are " + totalMD5FileCount + " md5 checksums");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void deleteAllMd5Checksums() {
|
public void deleteAllMd5Checksums() {
|
||||||
foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) {
|
foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) {
|
||||||
Directory.SetCurrentDirectory(directory); // Set new root
|
Directory.SetCurrentDirectory(directory); // Set new root
|
||||||
if (getFileCount() >= 1) {
|
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())) {
|
foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) {
|
||||||
Directory.SetCurrentDirectory(directory); // Set new root
|
Directory.SetCurrentDirectory(directory); // Set new root
|
||||||
if (getFileCount() >= 1) {
|
if (getFileCount() >= 1) {
|
28
src/chksum.sln
Normal file
28
src/chksum.sln
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user