Skip to content

Implement set last write and access time #1194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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