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

Use timestamps as well as file lengths to determine if files have changed during sync #933

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
Set the timestamp when the file is uploaded, and clear the millisecon…
…ds value which will not be passwed over the wire.
  • Loading branch information
kendallb committed Mar 12, 2022
commit ed98a51109e8dea8f341177b84b14878891a61dc
21 changes: 20 additions & 1 deletion src/Renci.SshNet/SftpClient.cs
Original file line number Diff line number Diff line change
@@ -2110,14 +2110,17 @@ private IEnumerable<FileInfo> InternalSynchronizeDirectories(string sourcePath,
const Flags uploadFlag = Flags.Write | Flags.Truncate | Flags.CreateNewOrOpen;
foreach (var localFile in sourceFiles)
{
// Clear the milliseconds from the timestamp, as that granularity will not carry over the wire with SFTP
var lastWriteTimeUtc = ClearMilliseconds(localFile.LastWriteTimeUtc);
var isDifferent = !destDict.ContainsKey(localFile.Name);

if (!isDifferent)
{
var temp = destDict[localFile.Name];
// TODO: Use md5 to detect a difference
//ltang: File exists at the destination => Using filesize to detect the difference
isDifferent = localFile.Length != temp.Length;
//kendallb: Use file length and timestamp to detect the difference
isDifferent = localFile.Length != temp.Length || lastWriteTimeUtc != localFile.LastWriteTimeUtc;
}

if (isDifferent)
@@ -2128,6 +2131,11 @@ private IEnumerable<FileInfo> InternalSynchronizeDirectories(string sourcePath,
using (var file = File.OpenRead(localFile.FullName))
{
InternalUploadFile(file, remoteFileName, uploadFlag, null, null);

// Set the timestamp to match on the uploaded file (the helper function is not implemented in the library)
var fileAttributes = GetAttributes(remoteFileName);
fileAttributes.LastWriteTimeUtc = lastWriteTimeUtc;
SetAttributes(remoteFileName, fileAttributes);
}

uploadedFiles.Add(localFile);
@@ -2149,6 +2157,17 @@ private IEnumerable<FileInfo> InternalSynchronizeDirectories(string sourcePath,
return uploadedFiles;
}

/// <summary>
/// Utility function to clear the millisecond value for a DateTime stamp
/// </summary>
/// <param name="date">Date to adjust</param>
/// <returns>Adjusted DateTime value</returns>
private static DateTime ClearMilliseconds(
DateTime date)
{
return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Kind);
}

#endregion

/// <summary>