Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hash only mode for compairing files. Fixed verbose logging. #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions KuduSync.NET/FileInfoBaseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,14 @@ public static string ComputeSha1(this FileInfoBase file)
return BitConverter.ToString(sha1.ComputeHash(fileStream));
}
}

public static string ComputeSha256(this FileInfoBase file)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given non-security related scenario, isn't the existing Sha1 method sufficient?

{
using (var fileStream = file.OpenRead())
{
var sha256 = new SHA256Managed();
return BitConverter.ToString(sha256.ComputeHash(fileStream));
}
}
}
}
61 changes: 45 additions & 16 deletions KuduSync.NET/KuduSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ public KuduSync(KuduSyncOptions options, Logger logger)
{
_to = Path.Combine(_to, _targetSubFolder);
}

if (_options.HashOnly)
{
_logger.Log("Hash only mode ON");
}

if (_options.Verbose)
{
_logger.Log("Verbose logging ON");
}

}

private bool TryCleanupToBeDeletedDirectory()
Expand Down Expand Up @@ -121,13 +132,12 @@ public void Run()
_nextManifest.SaveManifestFile();

TryCleanupToBeDeletedDirectory();

_logger.Log("Kudusync.NET Complete");
}

private void SmartCopy(string sourcePath,
string destinationPath,
string targetSubFolder,
DirectoryInfoBase sourceDirectory,
DirectoryInfoBase destinationDirectory)
//Main method
private void SmartCopy(string sourcePath,string destinationPath,string targetSubFolder,DirectoryInfoBase sourceDirectory,DirectoryInfoBase destinationDirectory)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid unnecessary formatting changes that don't directly relate to the PR

{
if (IgnorePath(sourceDirectory))
{
Expand Down Expand Up @@ -165,6 +175,7 @@ private void SmartCopy(string sourcePath,

// Trim the start destinationFilePath
string previousPath = FileSystemHelpers.GetRelativePath(destinationPath, destFile.FullName);

if (!sourceFilesLookup.ContainsKey(destFile.Name) && DoesPathExistsInManifest(previousPath, targetSubFolder))
{
_logger.Log("Deleting file: '{0}'", previousPath);
Expand All @@ -184,26 +195,44 @@ private void SmartCopy(string sourcePath,
// if the file exists in the destination then only copy it again if it's
// last write time is different than the same file in the source (only if it changed)
FileInfoBase targetFile;
if (destFilesLookup.TryGetValue(sourceFile.Name, out targetFile) &&
sourceFile.LastWriteTimeUtc == targetFile.LastWriteTimeUtc)
{
continue;
}

var details = FileSystemHelpers.GetRelativePath(sourcePath, sourceFile.FullName) + (_options.CopyMetaData ? " " + ShorthandAttributes(sourceFile) : String.Empty);

string path = FileSystemHelpers.GetDestinationPath(sourcePath, destinationPath, sourceFile);

var details = FileSystemHelpers.GetRelativePath(sourcePath, sourceFile.FullName) + (_options.CopyMetaData ? " " + ShorthandAttributes(sourceFile) : String.Empty);
switch (_options.HashOnly)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use a simple 'if' statement instead of a switch since it's just bool?

{
case true: //behaviour added by JWC on 8/11/2016
if (destFilesLookup.TryGetValue(sourceFile.Name, out targetFile) && sourceFile.ComputeSha256() == targetFile.ComputeSha256()) //if destination contins file, and the hash matched
{
if(_options.Verbose)
_logger.Log("Hash match on {0}, will not copy", details);
//move to next iteration if file the same
continue;
}
break;
case false:
if (destFilesLookup.TryGetValue(sourceFile.Name, out targetFile) && sourceFile.LastWriteTimeUtc == targetFile.LastWriteTimeUtc) //if destination contains file, and the time matches
{
//move to next iteration if file the same
continue;
}
break;
}

//if file is not the same, the code below executes

if (sourceFile.IsWebConfig())
{
// If current file is web.config check the content sha1.
if (!destFilesLookup.TryGetValue(sourceFile.Name, out targetFile) ||
!sourceFile.ComputeSha1().Equals(targetFile.ComputeSha1()))
{
// If current file is web.config check the content sha256.

//if (!destFilesLookup.TryGetValue(sourceFile.Name, out targetFile) && !sourceFile.ComputeSha256().Equals(targetFile.ComputeSha256())) //if destination does not contain file OR the hash matches
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean up commented out code

//{
// Save the file path to copy later for copying web.config forces an appDomain
// restart right away without respecting waitChangeNotification
_filesToCopyLast.Add(Tuple.Create(sourceFile, path, details));
}
//}

continue;
}

Expand Down
7 changes: 5 additions & 2 deletions KuduSync.NET/KuduSyncOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@ public class KuduSyncOptions : CommandLineOptionsBase
[Option("q", "quiet", Required = false, HelpText = "No logging")]
public bool Quiet { get; set; }

[Option("v", "verbose", Required = false, HelpText = "Verbose logging with maximum number of output lines")]
public int? Verbose { get; set; }
[Option("v", "verbose", DefaultValue = false, Required = false, HelpText = "Verbose logging with maximum number of output lines")]
public bool Verbose { get; set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks like a breaking change, since it used to represent the number of lines. Might be best to handle logging changes in a separate PR and keep this one focused on hash


[Option("w", "whatIf", Required = false, HelpText = "Only log without actual copy/remove of files")]
public bool WhatIf { get; set; }

[Option("", "perf", Required = false, HelpText = "Print out the time it took to complete KuduSync operation")]
public bool Perf { get; set; }

[Option("h", "hashOnly", DefaultValue = false, Required = false, HelpText = "Compare files by contents only using SHA256")]
public bool HashOnly { get; set; }

[HelpOption]
public string GetUsage()
{
Expand Down
25 changes: 23 additions & 2 deletions KuduSync.NET/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.Remoting.Messaging;
using System.Text;

namespace KuduSync.NET
Expand All @@ -19,15 +21,34 @@ public class Logger : IDisposable
/// <param name="maxLogLines">sets the verbosity, 0 is verbose, less is quiet, more is the number of maximum log lines to write.</param>
public Logger(int maxLogLines)
{
var stream = Console.OpenStandardOutput();
_writer = new StreamWriter(stream);
Stream stream = Console.OpenStandardOutput();
_writer = new KuduSyncLogger(stream);
_maxLogLines = maxLogLines;
}


public class KuduSyncLogger : StreamWriter
{
public KuduSyncLogger(Stream stream): base(stream)
{


}

public override void WriteLine(string value)
{
Debug.WriteLine(value);
base.WriteLine(value);
}
}



public void Log(string format, params object[] args)
{
bool logged = false;


if (_maxLogLines == 0 || _logCounter < _maxLogLines)
{
_writer.WriteLine(format, args);
Expand Down
8 changes: 4 additions & 4 deletions KuduSync.NET/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static int Main(string[] args)
ICommandLineParser parser = new CommandLineParser();
if (parser.ParseArguments(args, kuduSyncOptions))
{
using (var logger = GetLogger(kuduSyncOptions))
using (Logger logger = GetLogger(kuduSyncOptions))
{
new KuduSync(kuduSyncOptions, logger).Run();
}
Expand Down Expand Up @@ -53,13 +53,13 @@ private static Logger GetLogger(KuduSyncOptions kuduSyncOptions)
{
maxLogLines = -1;
}
else if (kuduSyncOptions.Verbose != null)
else if (kuduSyncOptions.Verbose)
{
maxLogLines = kuduSyncOptions.Verbose.Value;
maxLogLines = int.MaxValue;
}
else
{
maxLogLines = 0;
maxLogLines = 100;
}

return new Logger(maxLogLines);
Expand Down