-
-
Notifications
You must be signed in to change notification settings - Fork 952
Added SshCommand.InputStream to allow writing to stdin of SshCommand #1293
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
WojciechNagorski
merged 12 commits into
sshnet:develop
from
realvizu:SshCommandInputStream
Feb 6, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8f1717a
Making all unit tests pass locally.
ed38b31
Added SshCommand.InputStream.
1883838
Added an integration test for SshCommand.InputStream.
5855bde
Merge branch 'develop' into SshCommandInputStream
WojciechNagorski 742e4bf
Reverting changes made to unit tests unrelated to this PR.
7ac41c1
Moved ChannelInputStream's EOF sending from Write to Dispose. Replace…
d03efe2
Merge branch 'SshCommandInputStream' of https://github.com/realvizu/S…
03781c1
Fixing review comments.
a0161a5
Merge branch 'develop' into SshCommandInputStream
WojciechNagorski 01733fb
Merge branch 'develop' into SshCommandInputStream
WojciechNagorski c4f8992
Merge branch 'develop' into SshCommandInputStream
WojciechNagorski f29213f
Fix build error after #1286
Rob-Hague File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
using System; | ||
using System.IO; | ||
|
||
using Renci.SshNet.Channels; | ||
|
||
namespace Renci.SshNet.Common | ||
{ | ||
/// <summary> | ||
/// ChannelInputStream is a one direction stream intended for channel data. | ||
/// </summary> | ||
internal sealed class ChannelInputStream : Stream | ||
{ | ||
/// <summary> | ||
/// Channel to send data to. | ||
/// </summary> | ||
private readonly IChannelSession _channel; | ||
|
||
/// <summary> | ||
/// Total bytes passed through the stream. | ||
/// </summary> | ||
private long _totalPosition; | ||
|
||
/// <summary> | ||
/// Indicates whether the current instance was disposed. | ||
/// </summary> | ||
private bool _isDisposed; | ||
|
||
internal ChannelInputStream(IChannelSession channel) | ||
{ | ||
_channel = channel; | ||
} | ||
|
||
/// <summary> | ||
/// When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device. | ||
/// </summary> | ||
/// <exception cref="IOException">An I/O error occurs.</exception> | ||
/// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception> | ||
/// <remarks> | ||
/// Once flushed, any subsequent read operations no longer block until requested bytes are available. Any write operation reactivates blocking | ||
/// reads. | ||
/// </remarks> | ||
public override void Flush() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// When overridden in a derived class, sets the position within the current stream. | ||
/// </summary> | ||
/// <returns> | ||
/// The new position within the current stream. | ||
/// </returns> | ||
/// <param name="offset">A byte offset relative to the origin parameter.</param> | ||
/// <param name="origin">A value of type <see cref="SeekOrigin"/> indicating the reference point used to obtain the new position.</param> | ||
/// <exception cref="NotSupportedException">The stream does not support seeking, such as if the stream is constructed from a pipe or console output.</exception> | ||
public override long Seek(long offset, SeekOrigin origin) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
/// <summary> | ||
/// When overridden in a derived class, sets the length of the current stream. | ||
/// </summary> | ||
/// <param name="value">The desired length of the current stream in bytes.</param> | ||
/// <exception cref="NotSupportedException">The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output.</exception> | ||
public override void SetLength(long value) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
/// <summary> | ||
/// When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read. | ||
/// </summary> | ||
/// <returns> | ||
/// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero if the stream is closed or end of the stream has been reached. | ||
/// </returns> | ||
/// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param> | ||
/// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param> | ||
/// <param name="count">The maximum number of bytes to be read from the current stream.</param> | ||
/// <exception cref="ArgumentException">The sum of offset and count is larger than the buffer length.</exception> | ||
/// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception> | ||
/// <exception cref="NotSupportedException">The stream does not support reading.</exception> | ||
/// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <c>null</c>.</exception> | ||
/// <exception cref="IOException">An I/O error occurs.</exception> | ||
/// <exception cref="ArgumentOutOfRangeException">offset or count is negative.</exception> | ||
public override int Read(byte[] buffer, int offset, int count) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
/// <summary> | ||
/// When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. | ||
/// </summary> | ||
/// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param> | ||
/// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param> | ||
/// <param name="count">The number of bytes to be written to the current stream.</param> | ||
/// <exception cref="IOException">An I/O error occurs.</exception> | ||
/// <exception cref="NotSupportedException">The stream does not support writing.</exception> | ||
/// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception> | ||
/// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <c>null</c>.</exception> | ||
/// <exception cref="ArgumentException">The sum of offset and count is greater than the buffer length.</exception> | ||
/// <exception cref="ArgumentOutOfRangeException">offset or count is negative.</exception> | ||
public override void Write(byte[] buffer, int offset, int count) | ||
{ | ||
if (buffer == null) | ||
{ | ||
throw new ArgumentNullException(nameof(buffer)); | ||
} | ||
|
||
if (offset + count > buffer.Length) | ||
{ | ||
throw new ArgumentException("The sum of offset and count is greater than the buffer length."); | ||
} | ||
|
||
if (offset < 0 || count < 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(offset), "offset or count is negative."); | ||
} | ||
|
||
if (_isDisposed) | ||
{ | ||
throw CreateObjectDisposedException(); | ||
} | ||
|
||
if (count == 0) | ||
{ | ||
return; | ||
} | ||
|
||
_channel.SendData(buffer, offset, count); | ||
_totalPosition += count; | ||
} | ||
|
||
/// <summary> | ||
/// Releases the unmanaged resources used by the Stream and optionally releases the managed resources. | ||
/// </summary> | ||
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> | ||
protected override void Dispose(bool disposing) | ||
{ | ||
if (!_isDisposed) | ||
{ | ||
_isDisposed = true; | ||
|
||
// Closing the InputStream requires sending EOF. | ||
if (disposing && _totalPosition > 0 && _channel?.IsOpen == true) | ||
{ | ||
_channel.SendEof(); | ||
} | ||
} | ||
|
||
base.Dispose(disposing); | ||
} | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether the current stream supports reading. | ||
/// </summary> | ||
/// <returns> | ||
/// true if the stream supports reading; otherwise, false. | ||
/// </returns> | ||
public override bool CanRead | ||
{ | ||
get { return false; } | ||
} | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether the current stream supports seeking. | ||
/// </summary> | ||
/// <returns> | ||
/// <c>true</c> if the stream supports seeking; otherwise, <c>false</c>. | ||
/// </returns> | ||
public override bool CanSeek | ||
{ | ||
get { return false; } | ||
} | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether the current stream supports writing. | ||
/// </summary> | ||
/// <returns> | ||
/// <c>true</c> if the stream supports writing; otherwise, <c>false</c>. | ||
/// </returns> | ||
public override bool CanWrite | ||
{ | ||
get { return true; } | ||
} | ||
|
||
/// <summary> | ||
/// Gets the length in bytes of the stream. | ||
/// </summary> | ||
/// <returns> | ||
/// A long value representing the length of the stream in bytes. | ||
/// </returns> | ||
/// <exception cref="NotSupportedException">A class derived from Stream does not support seeking.</exception> | ||
/// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception> | ||
public override long Length | ||
{ | ||
get { throw new NotSupportedException(); } | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the position within the current stream. | ||
/// </summary> | ||
/// <returns> | ||
/// The current position within the stream. | ||
/// </returns> | ||
/// <exception cref="NotSupportedException">The stream does not support seeking.</exception> | ||
public override long Position | ||
{ | ||
get { return _totalPosition; } | ||
set { throw new NotSupportedException(); } | ||
} | ||
|
||
private ObjectDisposedException CreateObjectDisposedException() | ||
{ | ||
return new ObjectDisposedException(GetType().FullName); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.