Procházet zdrojové kódy

Implement set last write and access time (#1194)

Wojciech Nagórski před 2 roky
rodič
revize
5803ada7cf

+ 128 - 0
src/Renci.SshNet.IntegrationTests/SftpTests.cs

@@ -6137,6 +6137,134 @@ namespace Renci.SshNet.IntegrationTests
             }
         }
 
+        [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<object[]> GetSftpUploadFileFileStreamData()
         {
             yield return new object[] { 0 };

+ 12 - 8
src/Renci.SshNet/SftpClient.cs

@@ -1801,10 +1801,11 @@ namespace Renci.SshNet
         /// </summary>
         /// <param name="path">The file for which to set the access date and time information.</param>
         /// <param name="lastAccessTime">A <see cref="DateTime"/> containing the value to set for the last access date and time of path. This value is expressed in local time.</param>
-        [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);
         }
 
         /// <summary>
@@ -1812,10 +1813,11 @@ namespace Renci.SshNet
         /// </summary>
         /// <param name="path">The file for which to set the access date and time information.</param>
         /// <param name="lastAccessTimeUtc">A <see cref="DateTime"/> containing the value to set for the last access date and time of path. This value is expressed in UTC time.</param>
-        [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);
         }
 
         /// <summary>
@@ -1823,10 +1825,11 @@ namespace Renci.SshNet
         /// </summary>
         /// <param name="path">The file for which to set the date and time information.</param>
         /// <param name="lastWriteTime">A <see cref="DateTime"/> containing the value to set for the last write date and time of path. This value is expressed in local time.</param>
-        [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);
         }
 
         /// <summary>
@@ -1834,10 +1837,11 @@ namespace Renci.SshNet
         /// </summary>
         /// <param name="path">The file for which to set the date and time information.</param>
         /// <param name="lastWriteTimeUtc">A <see cref="DateTime"/> containing the value to set for the last write date and time of path. This value is expressed in UTC time.</param>
-        [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);
         }
 
         /// <summary>