Skip to content

Commit

Permalink
Set the timestamp when the file is uploaded, and clear the millisecon…
Browse files Browse the repository at this point in the history
…ds value which will not be passwed over the wire.
  • Loading branch information
kendallb committed Mar 12, 2022
1 parent bc99ada commit ed98a51
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/Renci.SshNet/SftpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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>
Expand Down

0 comments on commit ed98a51

Please sign in to comment.