diff --git a/src/Renci.SshNet.IntegrationTests/SftpTests.cs b/src/Renci.SshNet.IntegrationTests/SftpTests.cs index dc316cfbc..a5a80bfef 100644 --- a/src/Renci.SshNet.IntegrationTests/SftpTests.cs +++ b/src/Renci.SshNet.IntegrationTests/SftpTests.cs @@ -6137,6 +6137,134 @@ public void Sftp_OpenRead() } } + [TestMethod] + public void Sftp_SetLastAccessTime() + { + var testFilePath = "/home/sshnet/test-file.txt"; + var testContent = "File"; + using var client = new SftpClient(_connectionInfoFactory.Create()); + client.Connect(); + + using var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(testContent)); + var currentTime = DateTime.Now; + + client.UploadFile(fileStream, testFilePath); + + try + { + var time = client.GetLastAccessTime(testFilePath); + Assert.AreEqual(currentTime.Year, time.Year); + Assert.AreEqual(currentTime.Month, time.Month); + Assert.AreEqual(currentTime.Day, time.Day); + + var newTime = new DateTime(1986, 03, 15, 01, 02, 03); + + client.SetLastAccessTime(testFilePath, newTime); + time = client.GetLastAccessTime(testFilePath); + Assert.AreEqual(newTime, time); + } + finally + { + client.DeleteFile(testFilePath); + } + } + + + [TestMethod] + public void Sftp_SetLastAccessTimeUtc() + { + var testFilePath = "/home/sshnet/test-file.txt"; + var testContent = "File"; + using var client = new SftpClient(_connectionInfoFactory.Create()); + client.Connect(); + + using var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(testContent)); + var currentTime = DateTime.UtcNow; + + client.UploadFile(fileStream, testFilePath); + try + { + var time = client.GetLastAccessTimeUtc(testFilePath); + Assert.AreEqual(currentTime.Year, time.Year); + Assert.AreEqual(currentTime.Month, time.Month); + Assert.AreEqual(currentTime.Day, time.Day); + + var newTime = new DateTime(1986, 03, 15, 01, 02, 03); + DateTime.SpecifyKind(newTime, DateTimeKind.Utc); + + client.SetLastAccessTimeUtc(testFilePath, newTime); + time = client.GetLastAccessTimeUtc(testFilePath); + Assert.AreEqual(newTime, time); + } + finally + { + client.DeleteFile(testFilePath); + } + } + + [TestMethod] + public void Sftp_SetLastWriteTime() + { + var testFilePath = "/home/sshnet/test-file.txt"; + var testContent = "File"; + using var client = new SftpClient(_connectionInfoFactory.Create()); + client.Connect(); + + using var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(testContent)); + var currentTime = DateTime.Now; + + client.UploadFile(fileStream, testFilePath); + try + { + var time = client.GetLastWriteTime(testFilePath); + Assert.AreEqual(currentTime.Year, time.Year); + Assert.AreEqual(currentTime.Month, time.Month); + Assert.AreEqual(currentTime.Day, time.Day); + + var newTime = new DateTime(1986, 03, 15, 01, 02, 03); + + client.SetLastWriteTime(testFilePath, newTime); + time = client.GetLastWriteTime(testFilePath); + Assert.AreEqual(newTime, time); + } + finally + { + client.DeleteFile(testFilePath); + } + } + + [TestMethod] + public void Sftp_SetLastWriteTimeUtc() + { + var testFilePath = "/home/sshnet/test-file.txt"; + var testContent = "File"; + using var client = new SftpClient(_connectionInfoFactory.Create()); + client.Connect(); + + using var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(testContent)); + var currentTime = DateTime.UtcNow; + + client.UploadFile(fileStream, testFilePath); + try + { + var time = client.GetLastWriteTimeUtc(testFilePath); + Assert.AreEqual(currentTime.Year, time.Year); + Assert.AreEqual(currentTime.Month, time.Month); + Assert.AreEqual(currentTime.Day, time.Day); + + var newTime = new DateTime(1986, 03, 15, 01, 02, 03); + DateTime.SpecifyKind(newTime, DateTimeKind.Utc); + + client.SetLastWriteTimeUtc(testFilePath, newTime); + time = client.GetLastWriteTimeUtc(testFilePath); + Assert.AreEqual(newTime, time); + } + finally + { + client.DeleteFile(testFilePath); + } + } + private static IEnumerable GetSftpUploadFileFileStreamData() { yield return new object[] { 0 }; diff --git a/src/Renci.SshNet/SftpClient.cs b/src/Renci.SshNet/SftpClient.cs index e6f9cb2d6..877fed092 100644 --- a/src/Renci.SshNet/SftpClient.cs +++ b/src/Renci.SshNet/SftpClient.cs @@ -1801,10 +1801,11 @@ public IEnumerable ReadLines(string path, Encoding encoding) /// /// The file for which to set the access date and time information. /// A containing the value to set for the last access date and time of path. This value is expressed in local time. - [Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")] public void SetLastAccessTime(string path, DateTime lastAccessTime) { - throw new NotImplementedException(); + var attributes = GetAttributes(path); + attributes.LastAccessTime = lastAccessTime; + SetAttributes(path, attributes); } /// @@ -1812,10 +1813,11 @@ public void SetLastAccessTime(string path, DateTime lastAccessTime) /// /// The file for which to set the access date and time information. /// A containing the value to set for the last access date and time of path. This value is expressed in UTC time. - [Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")] public void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc) { - throw new NotImplementedException(); + var attributes = GetAttributes(path); + attributes.LastAccessTimeUtc = lastAccessTimeUtc; + SetAttributes(path, attributes); } /// @@ -1823,10 +1825,11 @@ public void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc) /// /// The file for which to set the date and time information. /// A containing the value to set for the last write date and time of path. This value is expressed in local time. - [Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")] public void SetLastWriteTime(string path, DateTime lastWriteTime) { - throw new NotImplementedException(); + var attributes = GetAttributes(path); + attributes.LastWriteTime = lastWriteTime; + SetAttributes(path, attributes); } /// @@ -1834,10 +1837,11 @@ public void SetLastWriteTime(string path, DateTime lastWriteTime) /// /// The file for which to set the date and time information. /// A containing the value to set for the last write date and time of path. This value is expressed in UTC time. - [Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")] public void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc) { - throw new NotImplementedException(); + var attributes = GetAttributes(path); + attributes.LastWriteTimeUtc = lastWriteTimeUtc; + SetAttributes(path, attributes); } ///