Skip to content

Commit ed98a51

Browse files
committed
Set the timestamp when the file is uploaded, and clear the milliseconds value which will not be passwed over the wire.
1 parent bc99ada commit ed98a51

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/Renci.SshNet/SftpClient.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2110,14 +2110,17 @@ private IEnumerable<FileInfo> InternalSynchronizeDirectories(string sourcePath,
21102110
const Flags uploadFlag = Flags.Write | Flags.Truncate | Flags.CreateNewOrOpen;
21112111
foreach (var localFile in sourceFiles)
21122112
{
2113+
// Clear the milliseconds from the timestamp, as that granularity will not carry over the wire with SFTP
2114+
var lastWriteTimeUtc = ClearMilliseconds(localFile.LastWriteTimeUtc);
21132115
var isDifferent = !destDict.ContainsKey(localFile.Name);
21142116

21152117
if (!isDifferent)
21162118
{
21172119
var temp = destDict[localFile.Name];
21182120
// TODO: Use md5 to detect a difference
21192121
//ltang: File exists at the destination => Using filesize to detect the difference
2120-
isDifferent = localFile.Length != temp.Length;
2122+
//kendallb: Use file length and timestamp to detect the difference
2123+
isDifferent = localFile.Length != temp.Length || lastWriteTimeUtc != localFile.LastWriteTimeUtc;
21212124
}
21222125

21232126
if (isDifferent)
@@ -2128,6 +2131,11 @@ private IEnumerable<FileInfo> InternalSynchronizeDirectories(string sourcePath,
21282131
using (var file = File.OpenRead(localFile.FullName))
21292132
{
21302133
InternalUploadFile(file, remoteFileName, uploadFlag, null, null);
2134+
2135+
// Set the timestamp to match on the uploaded file (the helper function is not implemented in the library)
2136+
var fileAttributes = GetAttributes(remoteFileName);
2137+
fileAttributes.LastWriteTimeUtc = lastWriteTimeUtc;
2138+
SetAttributes(remoteFileName, fileAttributes);
21312139
}
21322140

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

2160+
/// <summary>
2161+
/// Utility function to clear the millisecond value for a DateTime stamp
2162+
/// </summary>
2163+
/// <param name="date">Date to adjust</param>
2164+
/// <returns>Adjusted DateTime value</returns>
2165+
private static DateTime ClearMilliseconds(
2166+
DateTime date)
2167+
{
2168+
return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Kind);
2169+
}
2170+
21522171
#endregion
21532172

21542173
/// <summary>

0 commit comments

Comments
 (0)