Skip to content

Commit

Permalink
Implement set last write and access time (sshnet#1194)
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechNagorski authored Sep 29, 2023
1 parent cd1151d commit 5803ada
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 8 deletions.
128 changes: 128 additions & 0 deletions src/Renci.SshNet.IntegrationTests/SftpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object[]> GetSftpUploadFileFileStreamData()
{
yield return new object[] { 0 };
Expand Down
20 changes: 12 additions & 8 deletions src/Renci.SshNet/SftpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1801,43 +1801,47 @@ public IEnumerable<string> ReadLines(string path, Encoding encoding)
/// </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>
/// Sets the date and time, in coordinated universal time (UTC), that the specified file was last accessed.
/// </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>
/// Sets the date and time that the specified file was last written to.
/// </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>
/// Sets the date and time, in coordinated universal time (UTC), that the specified file was last written to.
/// </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>
Expand Down

0 comments on commit 5803ada

Please sign in to comment.