diff --git a/Renci.SshClient/Renci.SshClient.Tests/Renci.SshClient.Tests.csproj b/Renci.SshClient/Renci.SshClient.Tests/Renci.SshClient.Tests.csproj index ec338e1d..64512d41 100644 --- a/Renci.SshClient/Renci.SshClient.Tests/Renci.SshClient.Tests.csproj +++ b/Renci.SshClient/Renci.SshClient.Tests/Renci.SshClient.Tests.csproj @@ -66,6 +66,7 @@ + diff --git a/Renci.SshClient/Renci.SshClient.Tests/SftpClientTests/GetTest.cs b/Renci.SshClient/Renci.SshClient.Tests/SftpClientTests/GetTest.cs new file mode 100644 index 00000000..de9ed59a --- /dev/null +++ b/Renci.SshClient/Renci.SshClient.Tests/SftpClientTests/GetTest.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); + } + } + + } +} diff --git a/Renci.SshClient/Renci.SshClient/Renci.SshClient.csproj b/Renci.SshClient/Renci.SshClient/Renci.SshClient.csproj index 7bcc4fa5..f32b1fea 100644 --- a/Renci.SshClient/Renci.SshClient/Renci.SshClient.csproj +++ b/Renci.SshClient/Renci.SshClient/Renci.SshClient.csproj @@ -126,6 +126,7 @@ + diff --git a/Renci.SshClient/Renci.SshClient/Sftp/StatusCommand.cs b/Renci.SshClient/Renci.SshClient/Sftp/StatusCommand.cs new file mode 100644 index 00000000..1e22666a --- /dev/null +++ b/Renci.SshClient/Renci.SshClient/Sftp/StatusCommand.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; + } + } + } +} diff --git a/Renci.SshClient/Renci.SshClient/SftpClient.cs b/Renci.SshClient/Renci.SshClient/SftpClient.cs index b51e969d..5c347a88 100644 --- a/Renci.SshClient/Renci.SshClient/SftpClient.cs +++ b/Renci.SshClient/Renci.SshClient/SftpClient.cs @@ -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; } + /// + /// Gets reference to remote file or directory. + /// + /// The path. + /// + 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; + } + /// /// Downloads remote file specified by the path into the stream. ///