Add Get function to get reference to SftpFile instnace that represents remote file or directory

This commit is contained in:
olegkap_cp
2011-05-05 18:26:29 +00:00
parent 99bddc38e7
commit 20fb009c84
5 changed files with 140 additions and 0 deletions
@@ -66,6 +66,7 @@
<Compile Include="SftpClientTests\CreateDirectoryTest.cs" />
<Compile Include="SftpClientTests\DeleteDirectoryTest.cs" />
<Compile Include="SftpClientTests\DeleteFileTest.cs" />
<Compile Include="SftpClientTests\GetTest.cs" />
<Compile Include="SftpClientTests\ListDirectoryTest.cs" />
<Compile Include="SftpClientTests\RenameFileTest.cs" />
<Compile Include="SftpClientTests\UploadDownloadFileTest.cs" />
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshClient.Tests.Properties;
using System.IO;
namespace Renci.SshClient.Tests.SftpClientTests
{
[TestClass]
public class GetTest
{
[TestInitialize()]
public void CleanCurrentFolder()
{
using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
client.Connect();
client.RunCommand("rm -rf *");
client.Disconnect();
}
}
[TestMethod]
[TestCategory("Sftp")]
public void Test_Get_Root_Directory()
{
using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
sftp.Connect();
var directory = sftp.Get("/");
Assert.AreEqual("/", directory.FullName);
Assert.IsTrue(directory.IsDirectory);
Assert.IsFalse(directory.IsRegularFile);
}
}
[TestMethod]
[TestCategory("Sftp")]
public void Test_Get_Invalid_Directory()
{
using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
sftp.Connect();
var directory = sftp.Get("/xyz");
Assert.IsNull(directory);
}
}
[TestMethod]
[TestCategory("Sftp")]
public void Test_Get_File()
{
using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
sftp.Connect();
sftp.UploadFile(new MemoryStream(), "abc.txt");
var file = sftp.Get("abc.txt");
Assert.AreEqual("/home/tester/abc.txt", file.FullName);
Assert.IsTrue(file.IsRegularFile);
Assert.IsFalse(file.IsDirectory);
}
}
}
}
@@ -126,6 +126,7 @@
<Compile Include="Security\Cryptography\TransformMode.cs" />
<Compile Include="Security\Cryptography\Ciphers\DesCipher.cs" />
<Compile Include="Sftp\SetFileStatusCommand.cs" />
<Compile Include="Sftp\StatusCommand.cs" />
<Compile Include="Sftp\SymbolicLinkCommand.cs" />
<Compile Include="Sftp\Messages\SftpRequestMessage.cs" />
<Compile Include="Messages\Transport\KeyExchangeDhGroupExchangeGroup.cs" />
@@ -0,0 +1,44 @@
using System.Collections.Generic;
using Renci.SshClient.Sftp.Messages;
namespace Renci.SshClient.Sftp
{
internal class StatusCommand : SftpCommand
{
private string _path;
public SftpFile File { get; private set; }
public StatusCommand(SftpSession sftpSession, string path)
: base(sftpSession)
{
this._path = path;
}
protected override void OnExecute()
{
this.SendStatMessage(this._path);
}
protected override void OnAttributes(SftpFileAttributes attributes)
{
base.OnAttributes(attributes);
this.File = new SftpFile(this.SftpSession, this._path, attributes);
this.CompleteExecution();
}
protected override void OnStatus(StatusCodes statusCode, string errorMessage, string language)
{
base.OnStatus(statusCode, errorMessage, language);
if (statusCode == StatusCodes.NoSuchFile)
{
this.CompleteExecution();
this.IsStatusHandled = true;
}
}
}
}
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using Renci.SshClient.Sftp;
@@ -291,6 +292,27 @@ namespace Renci.SshClient
return files;
}
/// <summary>
/// Gets reference to remote file or directory.
/// </summary>
/// <param name="path">The path.</param>
/// <returns></returns>
public SftpFile Get(string path)
{
// Ensure that connection is established.
this.EnsureConnection();
var fullPath = this._sftpSession.GetCanonicalPath(path);
var cmd = new StatusCommand(this._sftpSession, fullPath);
cmd.CommandTimeout = this.OperationTimeout;
cmd.Execute();
return cmd.File;
}
/// <summary>
/// Downloads remote file specified by the path into the stream.
/// </summary>