Skip to content

Commit

Permalink
add basic file comparison tool
Browse files Browse the repository at this point in the history
  • Loading branch information
NobodysNightmare committed Jan 16, 2014
1 parent 5bd0876 commit c46139e
Show file tree
Hide file tree
Showing 4 changed files with 283 additions and 0 deletions.
55 changes: 55 additions & 0 deletions FileCheck/FileCheck.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{07E74F34-9E8B-4CF7-B879-92DB5A088A30}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>FileCheck</RootNamespace>
<AssemblyName>FileCheck</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
180 changes: 180 additions & 0 deletions FileCheck/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace FileCheck
{
class Program
{
private const string HashFileExtension = ".sha1";

static void Main(string[] args)
{
if (args.Length < 2)
{
PrintHelp();
return;
}

string command = args[0].ToLower();
var files = GetFiles(args.Skip(1).ToArray());

switch (command)
{
case "create":
CreateHashes(files.Where(file => file.Extension != HashFileExtension));
break;
case "verify":
VerifyHashes(files.Where(file => file.Extension == HashFileExtension));
break;
default:
Console.WriteLine("Unknown command '{0}'", command);
PrintHelp();
break;
}
}

private static void PrintHelp()
{
Console.WriteLine("Usage:");
Console.WriteLine(" FileCheck create <file>");
Console.WriteLine(" FileCheck create [-r] <directory>");
Console.WriteLine("");
Console.WriteLine(" FileCheck verify <file>");
Console.WriteLine(" FileCheck verify [-r] <directory>");
}

private static IEnumerable<FileInfo> GetFiles(string[] args)
{
string path = args.Last();
bool recurse = false;
if (args.Length > 1 && args[0] == "-r")
{
recurse = true;
}

if (Directory.Exists(path))
{
var directory = new DirectoryInfo(path);
foreach (FileInfo file in EnumerateDirectory(directory, recurse))
{
yield return file;
}
}
else if (File.Exists(path))
{
yield return new FileInfo(path);
}
else
{
Console.WriteLine("Could not find the path '{0}'", path);
}
}

private static IEnumerable<FileInfo> EnumerateDirectory(DirectoryInfo directory, bool recurse)
{
if (recurse)
{
foreach (DirectoryInfo subDirectory in directory.EnumerateDirectories())
{
foreach (FileInfo file in EnumerateDirectory(subDirectory, true))
{
yield return file;
}
}
}

foreach (FileInfo file in directory.EnumerateFiles())
{
yield return file;
}
}

private static void CreateHashes(IEnumerable<FileInfo> files)
{
Console.WriteLine("Generating Checksums...");
foreach (var file in files)
{
Console.Write("... {0}", file.FullName);
GenerateChecksum(file);
Console.WriteLine("\rOK ");
}
}

private static void GenerateChecksum(FileInfo file)
{
SHA1 hashAlgorithm = SHA1.Create();
byte[] hash;
using (var inStream = file.OpenRead())
using (var outStream = new FileStream(file.FullName + HashFileExtension, FileMode.Create))
{
hash = hashAlgorithm.ComputeHash(inStream);
outStream.Write(hash, 0, hash.Length);
outStream.Flush();
}
}

private static void VerifyHashes(IEnumerable<FileInfo> hashFiles)
{
Console.WriteLine("Verifying checksums...");
foreach (var hashFile in hashFiles)
{
string checkedFileName = hashFile.FullName.Substring(0, hashFile.FullName.Length - HashFileExtension.Length);
Console.Write("... {0}", checkedFileName);

FileInfo checkedFile = new FileInfo(checkedFileName);
if (!checkedFile.Exists)
{
Console.WriteLine("\rMISS");
}
else
{
if (VerifyFileHash(checkedFile, hashFile))
{
Console.WriteLine("\rOK ");
}
else
{
Console.WriteLine("\rFAIL");
}
}
}
}

private static bool VerifyFileHash(FileInfo checkedFile, FileInfo hashFile)
{
byte[] fileHash;
using (var fileStream = checkedFile.OpenRead())
{
SHA1 hashAlgorithm = SHA1.Create();
fileHash = hashAlgorithm.ComputeHash(fileStream);
}

byte[] referenceHash;
using (var fileStream = hashFile.OpenRead())
using (var reader = new BinaryReader(fileStream))
{
referenceHash = reader.ReadBytes(fileHash.Length);
}

return ByteArrayCompare(fileHash, referenceHash);
}

private static bool ByteArrayCompare(byte[] a1, byte[] a2)
{
if (a1.Length != a2.Length)
{
return false;
}

for (int i = 0; i < a1.Length; i++)
if (a1[i] != a2[i])
return false;

return true;
}
}
}
36 changes: 36 additions & 0 deletions FileCheck/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("FileCheck")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FileCheck")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("ae6133b6-90a0-4414-9078-c85105c3318f")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
12 changes: 12 additions & 0 deletions FileUtils.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DriveKeepAlive", "DriveKeep
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileBloater", "FileBloater\FileBloater.csproj", "{72C18893-8ED3-4776-980E-4A4802EE76C7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileCheck", "FileCheck\FileCheck.csproj", "{07E74F34-9E8B-4CF7-B879-92DB5A088A30}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -59,6 +61,16 @@ Global
{72C18893-8ED3-4776-980E-4A4802EE76C7}.Release|Mixed Platforms.Build.0 = Release|x86
{72C18893-8ED3-4776-980E-4A4802EE76C7}.Release|x86.ActiveCfg = Release|x86
{72C18893-8ED3-4776-980E-4A4802EE76C7}.Release|x86.Build.0 = Release|x86
{07E74F34-9E8B-4CF7-B879-92DB5A088A30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{07E74F34-9E8B-4CF7-B879-92DB5A088A30}.Debug|Any CPU.Build.0 = Debug|Any CPU
{07E74F34-9E8B-4CF7-B879-92DB5A088A30}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{07E74F34-9E8B-4CF7-B879-92DB5A088A30}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{07E74F34-9E8B-4CF7-B879-92DB5A088A30}.Debug|x86.ActiveCfg = Debug|Any CPU
{07E74F34-9E8B-4CF7-B879-92DB5A088A30}.Release|Any CPU.ActiveCfg = Release|Any CPU
{07E74F34-9E8B-4CF7-B879-92DB5A088A30}.Release|Any CPU.Build.0 = Release|Any CPU
{07E74F34-9E8B-4CF7-B879-92DB5A088A30}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{07E74F34-9E8B-4CF7-B879-92DB5A088A30}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{07E74F34-9E8B-4CF7-B879-92DB5A088A30}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit c46139e

Please sign in to comment.