diff --git a/Renci.SshClient/Renci.SshNet.Tests/SftpClientTests/ListDirectoryTest.cs b/Renci.SshClient/Renci.SshNet.Tests/SftpClientTests/ListDirectoryTest.cs index 0def7763..ff5d9551 100644 --- a/Renci.SshClient/Renci.SshNet.Tests/SftpClientTests/ListDirectoryTest.cs +++ b/Renci.SshClient/Renci.SshNet.Tests/SftpClientTests/ListDirectoryTest.cs @@ -98,6 +98,27 @@ namespace Renci.SshNet.Tests.SftpClientTests } } + [TestMethod] + [TestCategory("Sftp")] + public void Test_Sftp_ListDirectory_Empty() + { + using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD)) + { + sftp.Connect(); + + var files = sftp.ListDirectory(string.Empty); + + Assert.IsTrue(files.Count() > 0); + + foreach (var file in files) + { + Debug.WriteLine(file.FullName); + } + + sftp.Disconnect(); + } + } + [TestMethod] [TestCategory("Sftp")] public void Test_Sftp_ListDirectory_HugeDirectory() diff --git a/Renci.SshClient/Renci.SshNet/BaseClient.cs b/Renci.SshClient/Renci.SshNet/BaseClient.cs index af6a8b44..d8d55a1d 100644 --- a/Renci.SshClient/Renci.SshNet/BaseClient.cs +++ b/Renci.SshClient/Renci.SshNet/BaseClient.cs @@ -111,6 +111,9 @@ namespace Renci.SshNet /// public void Disconnect() { + if (!this.IsConnected) + return; + this.OnDisconnecting(); this.Dispose(); diff --git a/Renci.SshClient/Renci.SshNet/Session.cs b/Renci.SshClient/Renci.SshNet/Session.cs index c335ad91..5af8838c 100644 --- a/Renci.SshClient/Renci.SshNet/Session.cs +++ b/Renci.SshClient/Renci.SshNet/Session.cs @@ -141,12 +141,14 @@ namespace Renci.SshNet } } + private bool _isDisconnectMessageSent; + private uint _nextChannelNumber; /// /// Gets the next channel number. /// /// The next channel number. - public uint NextChannelNumber + internal uint NextChannelNumber { get { @@ -805,13 +807,15 @@ namespace Renci.SshNet private void SendDisconnect(DisconnectReason reasonCode, string message) { + // If disconnect message was sent already dont send it again + if (this._isDisconnectMessageSent) + return; + var disconnectMessage = new DisconnectMessage(reasonCode, message); this.SendMessage(disconnectMessage); - // Handle disconnect message as if it was sent by the server - // TODO: Review this code and may be handle it separately - this.HandleMessage(disconnectMessage); + this._isDisconnectMessageSent = true; } /// @@ -1583,12 +1587,11 @@ namespace Renci.SshNet { this.ErrorOccured(this, new ErrorEventArgs(exp)); } - var disconnectReason = DisconnectReason.ByApplication; - if (connectionException != null) - disconnectReason = connectionException.DisconnectReason; - - this.SendDisconnect(disconnectReason, exp.ToString()); + if (connectionException != null && connectionException.DisconnectReason != DisconnectReason.ConnectionLost) + { + this.SendDisconnect(connectionException.DisconnectReason, exp.ToString()); + } } #region IDisposable Members @@ -1622,26 +1625,11 @@ namespace Renci.SshNet if (this._socket != null) { - lock (this._socket) - { - // TODO: Not sure if it makes sense to do locking in Dispose but just want to check it for now as possible reason, need to remove later. + // If socket still open try to send disconnect message to the server + this.SendDisconnect(DisconnectReason.ByApplication, "Connection terminated by the client."); - if (this._socket != null) - { - try - { - // If socket still open try to send disconnect message to the server - this.SendMessage(new DisconnectMessage(DisconnectReason.ByApplication, "Connection terminated by the client.")); - } - catch (Exception) - { - // Do nothing as this is not required to send disconnect message correctly. - } - - this._socket.Dispose(); - this._socket = null; - } - } + this._socket.Dispose(); + this._socket = null; } if (this._messageListener != null) diff --git a/Renci.SshClient/Renci.SshNet/Sftp/ListDirectoryCommand.cs b/Renci.SshClient/Renci.SshNet/Sftp/ListDirectoryCommand.cs index 7a5d4abd..d4c9c87c 100644 --- a/Renci.SshClient/Renci.SshNet/Sftp/ListDirectoryCommand.cs +++ b/Renci.SshClient/Renci.SshNet/Sftp/ListDirectoryCommand.cs @@ -47,7 +47,7 @@ namespace Renci.SshNet.Sftp base.OnName(files); var seperator = "/"; - if (this._path[this._path.Length - 1] == '/') + if (!string.IsNullOrEmpty(this._path) && this._path[this._path.Length - 1] == '/') seperator = string.Empty; var sftpFiles = from f in files diff --git a/Renci.SshClient/Renci.SshNet/Sftp/SftpSession.cs b/Renci.SshClient/Renci.SshNet/Sftp/SftpSession.cs index bae968d1..68610edb 100644 --- a/Renci.SshClient/Renci.SshNet/Sftp/SftpSession.cs +++ b/Renci.SshClient/Renci.SshNet/Sftp/SftpSession.cs @@ -113,7 +113,7 @@ namespace Renci.SshNet.Sftp { var fullPath = path; - if (path[0] != '/' && this.WorkingDirectory != null) + if (!string.IsNullOrEmpty(path) && path[0] != '/' && this.WorkingDirectory != null) { if (this.WorkingDirectory[this.WorkingDirectory.Length - 1] == '/') {