From ed98a51109e8dea8f341177b84b14878891a61dc Mon Sep 17 00:00:00 2001 From: Kendall Bennett Date: Sat, 12 Mar 2022 15:40:01 -0500 Subject: [PATCH] Set the timestamp when the file is uploaded, and clear the milliseconds value which will not be passwed over the wire. --- src/Renci.SshNet/SftpClient.cs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Renci.SshNet/SftpClient.cs b/src/Renci.SshNet/SftpClient.cs index d33e21816..67e4df95b 100644 --- a/src/Renci.SshNet/SftpClient.cs +++ b/src/Renci.SshNet/SftpClient.cs @@ -2110,6 +2110,8 @@ private IEnumerable 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) @@ -2117,7 +2119,8 @@ private IEnumerable InternalSynchronizeDirectories(string sourcePath, 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 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 InternalSynchronizeDirectories(string sourcePath, return uploadedFiles; } + /// + /// Utility function to clear the millisecond value for a DateTime stamp + /// + /// Date to adjust + /// Adjusted DateTime value + private static DateTime ClearMilliseconds( + DateTime date) + { + return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Kind); + } + #endregion ///