Skip to content

Commit 5803ada

Browse files
Implement set last write and access time (#1194)
1 parent cd1151d commit 5803ada

File tree

2 files changed

+140
-8
lines changed

2 files changed

+140
-8
lines changed

src/Renci.SshNet.IntegrationTests/SftpTests.cs

+128
Original file line numberDiff line numberDiff line change
@@ -6137,6 +6137,134 @@ public void Sftp_OpenRead()
61376137
}
61386138
}
61396139

6140+
[TestMethod]
6141+
public void Sftp_SetLastAccessTime()
6142+
{
6143+
var testFilePath = "/home/sshnet/test-file.txt";
6144+
var testContent = "File";
6145+
using var client = new SftpClient(_connectionInfoFactory.Create());
6146+
client.Connect();
6147+
6148+
using var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(testContent));
6149+
var currentTime = DateTime.Now;
6150+
6151+
client.UploadFile(fileStream, testFilePath);
6152+
6153+
try
6154+
{
6155+
var time = client.GetLastAccessTime(testFilePath);
6156+
Assert.AreEqual(currentTime.Year, time.Year);
6157+
Assert.AreEqual(currentTime.Month, time.Month);
6158+
Assert.AreEqual(currentTime.Day, time.Day);
6159+
6160+
var newTime = new DateTime(1986, 03, 15, 01, 02, 03);
6161+
6162+
client.SetLastAccessTime(testFilePath, newTime);
6163+
time = client.GetLastAccessTime(testFilePath);
6164+
Assert.AreEqual(newTime, time);
6165+
}
6166+
finally
6167+
{
6168+
client.DeleteFile(testFilePath);
6169+
}
6170+
}
6171+
6172+
6173+
[TestMethod]
6174+
public void Sftp_SetLastAccessTimeUtc()
6175+
{
6176+
var testFilePath = "/home/sshnet/test-file.txt";
6177+
var testContent = "File";
6178+
using var client = new SftpClient(_connectionInfoFactory.Create());
6179+
client.Connect();
6180+
6181+
using var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(testContent));
6182+
var currentTime = DateTime.UtcNow;
6183+
6184+
client.UploadFile(fileStream, testFilePath);
6185+
try
6186+
{
6187+
var time = client.GetLastAccessTimeUtc(testFilePath);
6188+
Assert.AreEqual(currentTime.Year, time.Year);
6189+
Assert.AreEqual(currentTime.Month, time.Month);
6190+
Assert.AreEqual(currentTime.Day, time.Day);
6191+
6192+
var newTime = new DateTime(1986, 03, 15, 01, 02, 03);
6193+
DateTime.SpecifyKind(newTime, DateTimeKind.Utc);
6194+
6195+
client.SetLastAccessTimeUtc(testFilePath, newTime);
6196+
time = client.GetLastAccessTimeUtc(testFilePath);
6197+
Assert.AreEqual(newTime, time);
6198+
}
6199+
finally
6200+
{
6201+
client.DeleteFile(testFilePath);
6202+
}
6203+
}
6204+
6205+
[TestMethod]
6206+
public void Sftp_SetLastWriteTime()
6207+
{
6208+
var testFilePath = "/home/sshnet/test-file.txt";
6209+
var testContent = "File";
6210+
using var client = new SftpClient(_connectionInfoFactory.Create());
6211+
client.Connect();
6212+
6213+
using var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(testContent));
6214+
var currentTime = DateTime.Now;
6215+
6216+
client.UploadFile(fileStream, testFilePath);
6217+
try
6218+
{
6219+
var time = client.GetLastWriteTime(testFilePath);
6220+
Assert.AreEqual(currentTime.Year, time.Year);
6221+
Assert.AreEqual(currentTime.Month, time.Month);
6222+
Assert.AreEqual(currentTime.Day, time.Day);
6223+
6224+
var newTime = new DateTime(1986, 03, 15, 01, 02, 03);
6225+
6226+
client.SetLastWriteTime(testFilePath, newTime);
6227+
time = client.GetLastWriteTime(testFilePath);
6228+
Assert.AreEqual(newTime, time);
6229+
}
6230+
finally
6231+
{
6232+
client.DeleteFile(testFilePath);
6233+
}
6234+
}
6235+
6236+
[TestMethod]
6237+
public void Sftp_SetLastWriteTimeUtc()
6238+
{
6239+
var testFilePath = "/home/sshnet/test-file.txt";
6240+
var testContent = "File";
6241+
using var client = new SftpClient(_connectionInfoFactory.Create());
6242+
client.Connect();
6243+
6244+
using var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(testContent));
6245+
var currentTime = DateTime.UtcNow;
6246+
6247+
client.UploadFile(fileStream, testFilePath);
6248+
try
6249+
{
6250+
var time = client.GetLastWriteTimeUtc(testFilePath);
6251+
Assert.AreEqual(currentTime.Year, time.Year);
6252+
Assert.AreEqual(currentTime.Month, time.Month);
6253+
Assert.AreEqual(currentTime.Day, time.Day);
6254+
6255+
var newTime = new DateTime(1986, 03, 15, 01, 02, 03);
6256+
DateTime.SpecifyKind(newTime, DateTimeKind.Utc);
6257+
6258+
client.SetLastWriteTimeUtc(testFilePath, newTime);
6259+
time = client.GetLastWriteTimeUtc(testFilePath);
6260+
Assert.AreEqual(newTime, time);
6261+
}
6262+
finally
6263+
{
6264+
client.DeleteFile(testFilePath);
6265+
}
6266+
}
6267+
61406268
private static IEnumerable<object[]> GetSftpUploadFileFileStreamData()
61416269
{
61426270
yield return new object[] { 0 };

src/Renci.SshNet/SftpClient.cs

+12-8
Original file line numberDiff line numberDiff line change
@@ -1801,43 +1801,47 @@ public IEnumerable<string> ReadLines(string path, Encoding encoding)
18011801
/// </summary>
18021802
/// <param name="path">The file for which to set the access date and time information.</param>
18031803
/// <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>
1804-
[Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")]
18051804
public void SetLastAccessTime(string path, DateTime lastAccessTime)
18061805
{
1807-
throw new NotImplementedException();
1806+
var attributes = GetAttributes(path);
1807+
attributes.LastAccessTime = lastAccessTime;
1808+
SetAttributes(path, attributes);
18081809
}
18091810

18101811
/// <summary>
18111812
/// Sets the date and time, in coordinated universal time (UTC), that the specified file was last accessed.
18121813
/// </summary>
18131814
/// <param name="path">The file for which to set the access date and time information.</param>
18141815
/// <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>
1815-
[Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")]
18161816
public void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc)
18171817
{
1818-
throw new NotImplementedException();
1818+
var attributes = GetAttributes(path);
1819+
attributes.LastAccessTimeUtc = lastAccessTimeUtc;
1820+
SetAttributes(path, attributes);
18191821
}
18201822

18211823
/// <summary>
18221824
/// Sets the date and time that the specified file was last written to.
18231825
/// </summary>
18241826
/// <param name="path">The file for which to set the date and time information.</param>
18251827
/// <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>
1826-
[Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")]
18271828
public void SetLastWriteTime(string path, DateTime lastWriteTime)
18281829
{
1829-
throw new NotImplementedException();
1830+
var attributes = GetAttributes(path);
1831+
attributes.LastWriteTime = lastWriteTime;
1832+
SetAttributes(path, attributes);
18301833
}
18311834

18321835
/// <summary>
18331836
/// Sets the date and time, in coordinated universal time (UTC), that the specified file was last written to.
18341837
/// </summary>
18351838
/// <param name="path">The file for which to set the date and time information.</param>
18361839
/// <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>
1837-
[Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")]
18381840
public void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc)
18391841
{
1840-
throw new NotImplementedException();
1842+
var attributes = GetAttributes(path);
1843+
attributes.LastWriteTimeUtc = lastWriteTimeUtc;
1844+
SetAttributes(path, attributes);
18411845
}
18421846

18431847
/// <summary>

0 commit comments

Comments
 (0)