| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Diagnostics.CodeAnalysis; |
| | | 4 | | using System.Globalization; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Net; |
| | | 7 | | using System.Runtime.CompilerServices; |
| | | 8 | | using System.Text; |
| | | 9 | | using System.Threading; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | |
| | | 12 | | using Renci.SshNet.Abstractions; |
| | | 13 | | using Renci.SshNet.Common; |
| | | 14 | | using Renci.SshNet.Sftp; |
| | | 15 | | |
| | | 16 | | namespace Renci.SshNet |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Implementation of the SSH File Transfer Protocol (SFTP) over SSH. |
| | | 20 | | /// </summary> |
| | | 21 | | public class SftpClient : BaseClient, ISftpClient |
| | | 22 | | { |
| | 2 | 23 | | private static readonly Encoding Utf8NoBOM = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInv |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Holds the <see cref="ISftpSession"/> instance that is used to communicate to the |
| | | 27 | | /// SFTP server. |
| | | 28 | | /// </summary> |
| | | 29 | | private ISftpSession _sftpSession; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Holds the operation timeout. |
| | | 33 | | /// </summary> |
| | | 34 | | private int _operationTimeout; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Holds the size of the buffer. |
| | | 38 | | /// </summary> |
| | | 39 | | private uint _bufferSize; |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets or sets the operation timeout. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <value> |
| | | 45 | | /// The timeout to wait until an operation completes. The default value is negative |
| | | 46 | | /// one (-1) milliseconds, which indicates an infinite timeout period. |
| | | 47 | | /// </value> |
| | | 48 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 49 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> represents a value that is less than |
| | | 50 | | public TimeSpan OperationTimeout |
| | | 51 | | { |
| | | 52 | | get |
| | 15 | 53 | | { |
| | 15 | 54 | | CheckDisposed(); |
| | | 55 | | |
| | 12 | 56 | | return TimeSpan.FromMilliseconds(_operationTimeout); |
| | 12 | 57 | | } |
| | | 58 | | set |
| | 110 | 59 | | { |
| | 110 | 60 | | CheckDisposed(); |
| | | 61 | | |
| | 107 | 62 | | var timeoutInMilliseconds = value.TotalMilliseconds; |
| | 107 | 63 | | if (timeoutInMilliseconds is < -1d or > int.MaxValue) |
| | 6 | 64 | | { |
| | 6 | 65 | | throw new ArgumentOutOfRangeException(nameof(value), "The timeout must represent a value between -1 |
| | | 66 | | } |
| | | 67 | | |
| | 101 | 68 | | _operationTimeout = (int) timeoutInMilliseconds; |
| | 101 | 69 | | } |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Gets or sets the maximum size of the buffer in bytes. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <value> |
| | | 76 | | /// The size of the buffer. The default buffer size is 32768 bytes (32 KB). |
| | | 77 | | /// </value> |
| | | 78 | | /// <remarks> |
| | | 79 | | /// <para> |
| | | 80 | | /// For write operations, this limits the size of the payload for |
| | | 81 | | /// individual SSH_FXP_WRITE messages. The actual size is always |
| | | 82 | | /// capped at the maximum packet size supported by the peer |
| | | 83 | | /// (minus the size of protocol fields). |
| | | 84 | | /// </para> |
| | | 85 | | /// <para> |
| | | 86 | | /// For read operations, this controls the size of the payload which |
| | | 87 | | /// is requested from the peer in a SSH_FXP_READ message. The peer |
| | | 88 | | /// will send the requested number of bytes in a SSH_FXP_DATA message, |
| | | 89 | | /// possibly split over multiple SSH_MSG_CHANNEL_DATA messages. |
| | | 90 | | /// </para> |
| | | 91 | | /// <para> |
| | | 92 | | /// To optimize the size of the SSH packets sent by the peer, |
| | | 93 | | /// the actual requested size will take into account the size of the |
| | | 94 | | /// SSH_FXP_DATA protocol fields. |
| | | 95 | | /// </para> |
| | | 96 | | /// <para> |
| | | 97 | | /// The size of the each individual SSH_FXP_DATA message is limited to the |
| | | 98 | | /// local maximum packet size of the channel, which is set to <c>64 KB</c> |
| | | 99 | | /// for SSH.NET. However, the peer can limit this even further. |
| | | 100 | | /// </para> |
| | | 101 | | /// </remarks> |
| | | 102 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 103 | | public uint BufferSize |
| | | 104 | | { |
| | | 105 | | get |
| | 7 | 106 | | { |
| | 7 | 107 | | CheckDisposed(); |
| | 7 | 108 | | return _bufferSize; |
| | 7 | 109 | | } |
| | | 110 | | set |
| | 5 | 111 | | { |
| | 5 | 112 | | CheckDisposed(); |
| | 5 | 113 | | _bufferSize = value; |
| | 5 | 114 | | } |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Gets remote working directory. |
| | | 119 | | /// </summary> |
| | | 120 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 121 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 122 | | public string WorkingDirectory |
| | | 123 | | { |
| | | 124 | | get |
| | 80 | 125 | | { |
| | 80 | 126 | | CheckDisposed(); |
| | | 127 | | |
| | 80 | 128 | | if (_sftpSession is null) |
| | 0 | 129 | | { |
| | 0 | 130 | | throw new SshConnectionException("Client not connected."); |
| | | 131 | | } |
| | | 132 | | |
| | 80 | 133 | | return _sftpSession.WorkingDirectory; |
| | 80 | 134 | | } |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Gets sftp protocol version. |
| | | 139 | | /// </summary> |
| | | 140 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 141 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 142 | | public int ProtocolVersion |
| | | 143 | | { |
| | | 144 | | get |
| | 0 | 145 | | { |
| | 0 | 146 | | CheckDisposed(); |
| | | 147 | | |
| | 0 | 148 | | if (_sftpSession is null) |
| | 0 | 149 | | { |
| | 0 | 150 | | throw new SshConnectionException("Client not connected."); |
| | | 151 | | } |
| | | 152 | | |
| | 0 | 153 | | return (int) _sftpSession.ProtocolVersion; |
| | 0 | 154 | | } |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Gets the current SFTP session. |
| | | 159 | | /// </summary> |
| | | 160 | | /// <value> |
| | | 161 | | /// The current SFTP session. |
| | | 162 | | /// </value> |
| | | 163 | | internal ISftpSession SftpSession |
| | | 164 | | { |
| | 9 | 165 | | get { return _sftpSession; } |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | #region Constructors |
| | | 169 | | |
| | | 170 | | /// <summary> |
| | | 171 | | /// Initializes a new instance of the <see cref="SftpClient"/> class. |
| | | 172 | | /// </summary> |
| | | 173 | | /// <param name="connectionInfo">The connection info.</param> |
| | | 174 | | /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <see langword="null"/>.</except |
| | | 175 | | public SftpClient(ConnectionInfo connectionInfo) |
| | 319 | 176 | | : this(connectionInfo, ownsConnectionInfo: false) |
| | 319 | 177 | | { |
| | 319 | 178 | | } |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Initializes a new instance of the <see cref="SftpClient"/> class. |
| | | 182 | | /// </summary> |
| | | 183 | | /// <param name="host">Connection host.</param> |
| | | 184 | | /// <param name="port">Connection port.</param> |
| | | 185 | | /// <param name="username">Authentication username.</param> |
| | | 186 | | /// <param name="password">Authentication password.</param> |
| | | 187 | | /// <exception cref="ArgumentNullException"><paramref name="password"/> is <see langword="null"/>.</exception> |
| | | 188 | | /// <exception cref="ArgumentException"><paramref name="host"/> is invalid. <para>-or-</para> <paramref name="us |
| | | 189 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="IPEndPoint.Mi |
| | | 190 | | [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in |
| | | 191 | | public SftpClient(string host, int port, string username, string password) |
| | 69 | 192 | | : this(new PasswordConnectionInfo(host, port, username, password), ownsConnectionInfo: true) |
| | 69 | 193 | | { |
| | 69 | 194 | | } |
| | | 195 | | |
| | | 196 | | /// <summary> |
| | | 197 | | /// Initializes a new instance of the <see cref="SftpClient"/> class. |
| | | 198 | | /// </summary> |
| | | 199 | | /// <param name="host">Connection host.</param> |
| | | 200 | | /// <param name="username">Authentication username.</param> |
| | | 201 | | /// <param name="password">Authentication password.</param> |
| | | 202 | | /// <exception cref="ArgumentNullException"><paramref name="password"/> is <see langword="null"/>.</exception> |
| | | 203 | | /// <exception cref="ArgumentException"><paramref name="host"/> is invalid. <para>-or-</para> <paramref name="us |
| | | 204 | | public SftpClient(string host, string username, string password) |
| | 15 | 205 | | : this(host, ConnectionInfo.DefaultPort, username, password) |
| | 15 | 206 | | { |
| | 15 | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Initializes a new instance of the <see cref="SftpClient"/> class. |
| | | 211 | | /// </summary> |
| | | 212 | | /// <param name="host">Connection host.</param> |
| | | 213 | | /// <param name="port">Connection port.</param> |
| | | 214 | | /// <param name="username">Authentication username.</param> |
| | | 215 | | /// <param name="keyFiles">Authentication private key file(s) .</param> |
| | | 216 | | /// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is <see langword="null"/>.</exception> |
| | | 217 | | /// <exception cref="ArgumentException"><paramref name="host"/> is invalid. <para>-or-</para> <paramref name="us |
| | | 218 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="IPEndPoint.Mi |
| | | 219 | | [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in |
| | | 220 | | public SftpClient(string host, int port, string username, params IPrivateKeySource[] keyFiles) |
| | 0 | 221 | | : this(new PrivateKeyConnectionInfo(host, port, username, keyFiles), ownsConnectionInfo: true) |
| | 0 | 222 | | { |
| | 0 | 223 | | } |
| | | 224 | | |
| | | 225 | | /// <summary> |
| | | 226 | | /// Initializes a new instance of the <see cref="SftpClient"/> class. |
| | | 227 | | /// </summary> |
| | | 228 | | /// <param name="host">Connection host.</param> |
| | | 229 | | /// <param name="username">Authentication username.</param> |
| | | 230 | | /// <param name="keyFiles">Authentication private key file(s) .</param> |
| | | 231 | | /// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is <see langword="null"/>.</exception> |
| | | 232 | | /// <exception cref="ArgumentException"><paramref name="host"/> is invalid. <para>-or-</para> <paramref name="us |
| | | 233 | | public SftpClient(string host, string username, params IPrivateKeySource[] keyFiles) |
| | 0 | 234 | | : this(host, ConnectionInfo.DefaultPort, username, keyFiles) |
| | 0 | 235 | | { |
| | 0 | 236 | | } |
| | | 237 | | |
| | | 238 | | /// <summary> |
| | | 239 | | /// Initializes a new instance of the <see cref="SftpClient"/> class. |
| | | 240 | | /// </summary> |
| | | 241 | | /// <param name="connectionInfo">The connection info.</param> |
| | | 242 | | /// <param name="ownsConnectionInfo">Specified whether this instance owns the connection info.</param> |
| | | 243 | | /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <see langword="null"/>.</except |
| | | 244 | | /// <remarks> |
| | | 245 | | /// If <paramref name="ownsConnectionInfo"/> is <see langword="true"/>, the connection info will be disposed whe |
| | | 246 | | /// instance is disposed. |
| | | 247 | | /// </remarks> |
| | | 248 | | private SftpClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo) |
| | 388 | 249 | | : this(connectionInfo, ownsConnectionInfo, new ServiceFactory()) |
| | 388 | 250 | | { |
| | 388 | 251 | | } |
| | | 252 | | |
| | | 253 | | /// <summary> |
| | | 254 | | /// Initializes a new instance of the <see cref="SftpClient"/> class. |
| | | 255 | | /// </summary> |
| | | 256 | | /// <param name="connectionInfo">The connection info.</param> |
| | | 257 | | /// <param name="ownsConnectionInfo">Specified whether this instance owns the connection info.</param> |
| | | 258 | | /// <param name="serviceFactory">The factory to use for creating new services.</param> |
| | | 259 | | /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <see langword="null"/>.</except |
| | | 260 | | /// <exception cref="ArgumentNullException"><paramref name="serviceFactory"/> is <see langword="null"/>.</except |
| | | 261 | | /// <remarks> |
| | | 262 | | /// If <paramref name="ownsConnectionInfo"/> is <see langword="true"/>, the connection info will be disposed whe |
| | | 263 | | /// instance is disposed. |
| | | 264 | | /// </remarks> |
| | | 265 | | internal SftpClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory) |
| | 493 | 266 | | : base(connectionInfo, ownsConnectionInfo, serviceFactory) |
| | 493 | 267 | | { |
| | 493 | 268 | | _operationTimeout = SshNet.Session.Infinite; |
| | 493 | 269 | | _bufferSize = 1024 * 32; |
| | 493 | 270 | | } |
| | | 271 | | |
| | | 272 | | #endregion Constructors |
| | | 273 | | |
| | | 274 | | /// <summary> |
| | | 275 | | /// Changes remote directory to path. |
| | | 276 | | /// </summary> |
| | | 277 | | /// <param name="path">New directory path.</param> |
| | | 278 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 279 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 280 | | /// <exception cref="SftpPermissionDeniedException">Permission to change directory denied by remote host. <para> |
| | | 281 | | /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</excep |
| | | 282 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the rem |
| | | 283 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 284 | | public void ChangeDirectory(string path) |
| | 21 | 285 | | { |
| | 21 | 286 | | CheckDisposed(); |
| | | 287 | | |
| | 21 | 288 | | if (path is null) |
| | 1 | 289 | | { |
| | 1 | 290 | | throw new ArgumentNullException(nameof(path)); |
| | | 291 | | } |
| | | 292 | | |
| | 20 | 293 | | if (_sftpSession is null) |
| | 0 | 294 | | { |
| | 0 | 295 | | throw new SshConnectionException("Client not connected."); |
| | | 296 | | } |
| | | 297 | | |
| | 20 | 298 | | _sftpSession.ChangeDirectory(path); |
| | 15 | 299 | | } |
| | | 300 | | |
| | | 301 | | /// <summary> |
| | | 302 | | /// Changes permissions of file(s) to specified mode. |
| | | 303 | | /// </summary> |
| | | 304 | | /// <param name="path">File(s) path, may match multiple files.</param> |
| | | 305 | | /// <param name="mode">The mode.</param> |
| | | 306 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 307 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 308 | | /// <exception cref="SftpPermissionDeniedException">Permission to change permission on the path(s) was denied by |
| | | 309 | | /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</excep |
| | | 310 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the rem |
| | | 311 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 312 | | public void ChangePermissions(string path, short mode) |
| | 1 | 313 | | { |
| | 1 | 314 | | var file = Get(path); |
| | 1 | 315 | | file.SetPermissions(mode); |
| | 1 | 316 | | } |
| | | 317 | | |
| | | 318 | | /// <summary> |
| | | 319 | | /// Creates remote directory specified by path. |
| | | 320 | | /// </summary> |
| | | 321 | | /// <param name="path">Directory path to create.</param> |
| | | 322 | | /// <exception cref="ArgumentException"><paramref name="path"/> is <see langword="null"/> or contains only white |
| | | 323 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 324 | | /// <exception cref="SftpPermissionDeniedException">Permission to create the directory was denied by the remote |
| | | 325 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the rem |
| | | 326 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 327 | | public void CreateDirectory(string path) |
| | 10037 | 328 | | { |
| | 10037 | 329 | | CheckDisposed(); |
| | | 330 | | |
| | 10037 | 331 | | if (string.IsNullOrWhiteSpace(path)) |
| | 0 | 332 | | { |
| | 0 | 333 | | throw new ArgumentException(path); |
| | | 334 | | } |
| | | 335 | | |
| | 10037 | 336 | | if (_sftpSession is null) |
| | 0 | 337 | | { |
| | 0 | 338 | | throw new SshConnectionException("Client not connected."); |
| | | 339 | | } |
| | | 340 | | |
| | 10037 | 341 | | var fullPath = _sftpSession.GetCanonicalPath(path); |
| | | 342 | | |
| | 10037 | 343 | | _sftpSession.RequestMkDir(fullPath); |
| | 10034 | 344 | | } |
| | | 345 | | |
| | | 346 | | /// <summary> |
| | | 347 | | /// Deletes remote directory specified by path. |
| | | 348 | | /// </summary> |
| | | 349 | | /// <param name="path">Directory to be deleted path.</param> |
| | | 350 | | /// <exception cref="ArgumentException"><paramref name="path"/> is <see langword="null"/> or contains only white |
| | | 351 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 352 | | /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</excep |
| | | 353 | | /// <exception cref="SftpPermissionDeniedException">Permission to delete the directory was denied by the remote |
| | | 354 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the rem |
| | | 355 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 356 | | public void DeleteDirectory(string path) |
| | 42 | 357 | | { |
| | 42 | 358 | | CheckDisposed(); |
| | | 359 | | |
| | 42 | 360 | | if (string.IsNullOrWhiteSpace(path)) |
| | 1 | 361 | | { |
| | 1 | 362 | | throw new ArgumentException("path"); |
| | | 363 | | } |
| | | 364 | | |
| | 41 | 365 | | if (_sftpSession is null) |
| | 3 | 366 | | { |
| | 3 | 367 | | throw new SshConnectionException("Client not connected."); |
| | | 368 | | } |
| | | 369 | | |
| | 38 | 370 | | var fullPath = _sftpSession.GetCanonicalPath(path); |
| | | 371 | | |
| | 38 | 372 | | _sftpSession.RequestRmDir(fullPath); |
| | 36 | 373 | | } |
| | | 374 | | |
| | | 375 | | /// <summary> |
| | | 376 | | /// Deletes remote file specified by path. |
| | | 377 | | /// </summary> |
| | | 378 | | /// <param name="path">File to be deleted path.</param> |
| | | 379 | | /// <exception cref="ArgumentException"><paramref name="path"/> is <see langword="null"/> or contains only white |
| | | 380 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 381 | | /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</excep |
| | | 382 | | /// <exception cref="SftpPermissionDeniedException">Permission to delete the file was denied by the remote host. |
| | | 383 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the rem |
| | | 384 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 385 | | public void DeleteFile(string path) |
| | 155 | 386 | | { |
| | 155 | 387 | | CheckDisposed(); |
| | | 388 | | |
| | 155 | 389 | | if (string.IsNullOrWhiteSpace(path)) |
| | 3 | 390 | | { |
| | 3 | 391 | | throw new ArgumentException("path"); |
| | | 392 | | } |
| | | 393 | | |
| | 152 | 394 | | if (_sftpSession is null) |
| | 0 | 395 | | { |
| | 0 | 396 | | throw new SshConnectionException("Client not connected."); |
| | | 397 | | } |
| | | 398 | | |
| | 152 | 399 | | var fullPath = _sftpSession.GetCanonicalPath(path); |
| | | 400 | | |
| | 152 | 401 | | _sftpSession.RequestRemove(fullPath); |
| | 152 | 402 | | } |
| | | 403 | | |
| | | 404 | | /// <summary> |
| | | 405 | | /// Asynchronously deletes remote file specified by path. |
| | | 406 | | /// </summary> |
| | | 407 | | /// <param name="path">File to be deleted path.</param> |
| | | 408 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param> |
| | | 409 | | /// <returns>A <see cref="Task"/> that represents the asynchronous delete operation.</returns> |
| | | 410 | | /// <exception cref="ArgumentException"><paramref name="path"/> is <see langword="null"/> or contains only white |
| | | 411 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 412 | | /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</excep |
| | | 413 | | /// <exception cref="SftpPermissionDeniedException">Permission to delete the file was denied by the remote host. |
| | | 414 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the rem |
| | | 415 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 416 | | public async Task DeleteFileAsync(string path, CancellationToken cancellationToken) |
| | 3 | 417 | | { |
| | 3 | 418 | | CheckDisposed(); |
| | | 419 | | |
| | 3 | 420 | | if (string.IsNullOrWhiteSpace(path)) |
| | 3 | 421 | | { |
| | 3 | 422 | | throw new ArgumentException("path"); |
| | | 423 | | } |
| | | 424 | | |
| | 0 | 425 | | if (_sftpSession is null) |
| | 0 | 426 | | { |
| | 0 | 427 | | throw new SshConnectionException("Client not connected."); |
| | | 428 | | } |
| | | 429 | | |
| | 0 | 430 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 431 | | |
| | 0 | 432 | | var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false); |
| | 0 | 433 | | await _sftpSession.RequestRemoveAsync(fullPath, cancellationToken).ConfigureAwait(false); |
| | 0 | 434 | | } |
| | | 435 | | |
| | | 436 | | /// <summary> |
| | | 437 | | /// Renames remote file from old path to new path. |
| | | 438 | | /// </summary> |
| | | 439 | | /// <param name="oldPath">Path to the old file location.</param> |
| | | 440 | | /// <param name="newPath">Path to the new file location.</param> |
| | | 441 | | /// <exception cref="ArgumentNullException"><paramref name="oldPath"/> is <see langword="null"/>. <para>-or-</pa |
| | | 442 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 443 | | /// <exception cref="SftpPermissionDeniedException">Permission to rename the file was denied by the remote host. |
| | | 444 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the rem |
| | | 445 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 446 | | public void RenameFile(string oldPath, string newPath) |
| | 2 | 447 | | { |
| | 2 | 448 | | RenameFile(oldPath, newPath, isPosix: false); |
| | 1 | 449 | | } |
| | | 450 | | |
| | | 451 | | /// <summary> |
| | | 452 | | /// Renames remote file from old path to new path. |
| | | 453 | | /// </summary> |
| | | 454 | | /// <param name="oldPath">Path to the old file location.</param> |
| | | 455 | | /// <param name="newPath">Path to the new file location.</param> |
| | | 456 | | /// <param name="isPosix">if set to <see langword="true"/> then perform a posix rename.</param> |
| | | 457 | | /// <exception cref="ArgumentNullException"><paramref name="oldPath" /> is <see langword="null"/>. <para>-or-</p |
| | | 458 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 459 | | /// <exception cref="SftpPermissionDeniedException">Permission to rename the file was denied by the remote host. |
| | | 460 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the re |
| | | 461 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 462 | | public void RenameFile(string oldPath, string newPath, bool isPosix) |
| | 2 | 463 | | { |
| | 2 | 464 | | CheckDisposed(); |
| | | 465 | | |
| | 2 | 466 | | if (oldPath is null) |
| | 1 | 467 | | { |
| | 1 | 468 | | throw new ArgumentNullException(nameof(oldPath)); |
| | | 469 | | } |
| | | 470 | | |
| | 1 | 471 | | if (newPath is null) |
| | 0 | 472 | | { |
| | 0 | 473 | | throw new ArgumentNullException(nameof(newPath)); |
| | | 474 | | } |
| | | 475 | | |
| | 1 | 476 | | if (_sftpSession is null) |
| | 0 | 477 | | { |
| | 0 | 478 | | throw new SshConnectionException("Client not connected."); |
| | | 479 | | } |
| | | 480 | | |
| | 1 | 481 | | var oldFullPath = _sftpSession.GetCanonicalPath(oldPath); |
| | | 482 | | |
| | 1 | 483 | | var newFullPath = _sftpSession.GetCanonicalPath(newPath); |
| | | 484 | | |
| | 1 | 485 | | if (isPosix) |
| | 0 | 486 | | { |
| | 0 | 487 | | _sftpSession.RequestPosixRename(oldFullPath, newFullPath); |
| | 0 | 488 | | } |
| | | 489 | | else |
| | 1 | 490 | | { |
| | 1 | 491 | | _sftpSession.RequestRename(oldFullPath, newFullPath); |
| | 1 | 492 | | } |
| | 1 | 493 | | } |
| | | 494 | | |
| | | 495 | | /// <summary> |
| | | 496 | | /// Asynchronously renames remote file from old path to new path. |
| | | 497 | | /// </summary> |
| | | 498 | | /// <param name="oldPath">Path to the old file location.</param> |
| | | 499 | | /// <param name="newPath">Path to the new file location.</param> |
| | | 500 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param> |
| | | 501 | | /// <returns>A <see cref="Task"/> that represents the asynchronous rename operation.</returns> |
| | | 502 | | /// <exception cref="ArgumentNullException"><paramref name="oldPath"/> is <see langword="null"/>. <para>-or-</pa |
| | | 503 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 504 | | /// <exception cref="SftpPermissionDeniedException">Permission to rename the file was denied by the remote host. |
| | | 505 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the rem |
| | | 506 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 507 | | public async Task RenameFileAsync(string oldPath, string newPath, CancellationToken cancellationToken) |
| | 2 | 508 | | { |
| | 2 | 509 | | CheckDisposed(); |
| | | 510 | | |
| | 2 | 511 | | if (oldPath is null) |
| | 1 | 512 | | { |
| | 1 | 513 | | throw new ArgumentNullException(nameof(oldPath)); |
| | | 514 | | } |
| | | 515 | | |
| | 1 | 516 | | if (newPath is null) |
| | 0 | 517 | | { |
| | 0 | 518 | | throw new ArgumentNullException(nameof(newPath)); |
| | | 519 | | } |
| | | 520 | | |
| | 1 | 521 | | if (_sftpSession is null) |
| | 0 | 522 | | { |
| | 0 | 523 | | throw new SshConnectionException("Client not connected."); |
| | | 524 | | } |
| | | 525 | | |
| | 1 | 526 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 527 | | |
| | 1 | 528 | | var oldFullPath = await _sftpSession.GetCanonicalPathAsync(oldPath, cancellationToken).ConfigureAwait(false) |
| | 1 | 529 | | var newFullPath = await _sftpSession.GetCanonicalPathAsync(newPath, cancellationToken).ConfigureAwait(false) |
| | 1 | 530 | | await _sftpSession.RequestRenameAsync(oldFullPath, newFullPath, cancellationToken).ConfigureAwait(false); |
| | 1 | 531 | | } |
| | | 532 | | |
| | | 533 | | /// <summary> |
| | | 534 | | /// Creates a symbolic link from old path to new path. |
| | | 535 | | /// </summary> |
| | | 536 | | /// <param name="path">The old path.</param> |
| | | 537 | | /// <param name="linkPath">The new path.</param> |
| | | 538 | | /// <exception cref="ArgumentException"><paramref name="path"/> is <see langword="null"/>. <para>-or-</para> <pa |
| | | 539 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 540 | | /// <exception cref="SftpPermissionDeniedException">Permission to create the symbolic link was denied by the rem |
| | | 541 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the rem |
| | | 542 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 543 | | public void SymbolicLink(string path, string linkPath) |
| | 0 | 544 | | { |
| | 0 | 545 | | CheckDisposed(); |
| | | 546 | | |
| | 0 | 547 | | if (string.IsNullOrWhiteSpace(path)) |
| | 0 | 548 | | { |
| | 0 | 549 | | throw new ArgumentException("path"); |
| | | 550 | | } |
| | | 551 | | |
| | 0 | 552 | | if (string.IsNullOrWhiteSpace(linkPath)) |
| | 0 | 553 | | { |
| | 0 | 554 | | throw new ArgumentException("linkPath"); |
| | | 555 | | } |
| | | 556 | | |
| | 0 | 557 | | if (_sftpSession is null) |
| | 0 | 558 | | { |
| | 0 | 559 | | throw new SshConnectionException("Client not connected."); |
| | | 560 | | } |
| | | 561 | | |
| | 0 | 562 | | var fullPath = _sftpSession.GetCanonicalPath(path); |
| | | 563 | | |
| | 0 | 564 | | var linkFullPath = _sftpSession.GetCanonicalPath(linkPath); |
| | | 565 | | |
| | 0 | 566 | | _sftpSession.RequestSymLink(fullPath, linkFullPath); |
| | 0 | 567 | | } |
| | | 568 | | |
| | | 569 | | /// <summary> |
| | | 570 | | /// Retrieves list of files in remote directory. |
| | | 571 | | /// </summary> |
| | | 572 | | /// <param name="path">The path.</param> |
| | | 573 | | /// <param name="listCallback">The list callback.</param> |
| | | 574 | | /// <returns> |
| | | 575 | | /// A list of files. |
| | | 576 | | /// </returns> |
| | | 577 | | /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception> |
| | | 578 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 579 | | /// <exception cref="SftpPermissionDeniedException">Permission to list the contents of the directory was denied |
| | | 580 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the re |
| | | 581 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 582 | | public IEnumerable<ISftpFile> ListDirectory(string path, Action<int> listCallback = null) |
| | 16 | 583 | | { |
| | 16 | 584 | | CheckDisposed(); |
| | | 585 | | |
| | 16 | 586 | | return InternalListDirectory(path, listCallback); |
| | 7 | 587 | | } |
| | | 588 | | |
| | | 589 | | /// <summary> |
| | | 590 | | /// Asynchronously enumerates the files in remote directory. |
| | | 591 | | /// </summary> |
| | | 592 | | /// <param name="path">The path.</param> |
| | | 593 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param> |
| | | 594 | | /// <returns> |
| | | 595 | | /// An <see cref="IAsyncEnumerable{T}"/> of <see cref="ISftpFile"/> that represents the asynchronous enumeration |
| | | 596 | | /// The enumeration contains an async stream of <see cref="ISftpFile"/> for the files in the directory specified |
| | | 597 | | /// </returns> |
| | | 598 | | /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception> |
| | | 599 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 600 | | /// <exception cref="SftpPermissionDeniedException">Permission to list the contents of the directory was denied |
| | | 601 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the re |
| | | 602 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 603 | | public async IAsyncEnumerable<ISftpFile> ListDirectoryAsync(string path, [EnumeratorCancellation] CancellationTo |
| | 5 | 604 | | { |
| | 5 | 605 | | CheckDisposed(); |
| | | 606 | | |
| | 5 | 607 | | if (path is null) |
| | 0 | 608 | | { |
| | 0 | 609 | | throw new ArgumentNullException(nameof(path)); |
| | | 610 | | } |
| | | 611 | | |
| | 5 | 612 | | if (_sftpSession is null) |
| | 3 | 613 | | { |
| | 3 | 614 | | throw new SshConnectionException("Client not connected."); |
| | | 615 | | } |
| | | 616 | | |
| | 2 | 617 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 618 | | |
| | 2 | 619 | | var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false); |
| | | 620 | | |
| | 2 | 621 | | var handle = await _sftpSession.RequestOpenDirAsync(fullPath, cancellationToken).ConfigureAwait(false); |
| | | 622 | | try |
| | 2 | 623 | | { |
| | 2 | 624 | | var basePath = (fullPath[fullPath.Length - 1] == '/') ? |
| | 2 | 625 | | fullPath : |
| | 2 | 626 | | fullPath + '/'; |
| | | 627 | | |
| | 4 | 628 | | while (true) |
| | 4 | 629 | | { |
| | 4 | 630 | | var files = await _sftpSession.RequestReadDirAsync(handle, cancellationToken).ConfigureAwait(false); |
| | 4 | 631 | | if (files is null) |
| | 2 | 632 | | { |
| | 2 | 633 | | break; |
| | | 634 | | } |
| | | 635 | | |
| | 86 | 636 | | foreach (var file in files) |
| | 40 | 637 | | { |
| | 40 | 638 | | yield return new SftpFile(_sftpSession, basePath + file.Key, file.Value); |
| | 40 | 639 | | } |
| | 2 | 640 | | } |
| | 2 | 641 | | } |
| | | 642 | | finally |
| | 2 | 643 | | { |
| | 2 | 644 | | await _sftpSession.RequestCloseAsync(handle, cancellationToken).ConfigureAwait(false); |
| | 2 | 645 | | } |
| | 2 | 646 | | } |
| | | 647 | | |
| | | 648 | | /// <summary> |
| | | 649 | | /// Begins an asynchronous operation of retrieving list of files in remote directory. |
| | | 650 | | /// </summary> |
| | | 651 | | /// <param name="path">The path.</param> |
| | | 652 | | /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</par |
| | | 653 | | /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request fro |
| | | 654 | | /// <param name="listCallback">The list callback.</param> |
| | | 655 | | /// <returns> |
| | | 656 | | /// An <see cref="IAsyncResult" /> that references the asynchronous operation. |
| | | 657 | | /// </returns> |
| | | 658 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 659 | | public IAsyncResult BeginListDirectory(string path, AsyncCallback asyncCallback, object state, Action<int> listC |
| | 5 | 660 | | { |
| | 5 | 661 | | CheckDisposed(); |
| | | 662 | | |
| | 5 | 663 | | var asyncResult = new SftpListDirectoryAsyncResult(asyncCallback, state); |
| | | 664 | | |
| | 5 | 665 | | ThreadAbstraction.ExecuteThread(() => |
| | 5 | 666 | | { |
| | 5 | 667 | | try |
| | 5 | 668 | | { |
| | 5 | 669 | | var result = InternalListDirectory(path, count => |
| | 3 | 670 | | { |
| | 3 | 671 | | asyncResult.Update(count); |
| | 5 | 672 | | |
| | 3 | 673 | | listCallback?.Invoke(count); |
| | 8 | 674 | | }); |
| | 5 | 675 | | |
| | 3 | 676 | | asyncResult.SetAsCompleted(result, completedSynchronously: false); |
| | 3 | 677 | | } |
| | 1 | 678 | | catch (Exception exp) |
| | 1 | 679 | | { |
| | 1 | 680 | | asyncResult.SetAsCompleted(exp, completedSynchronously: false); |
| | 1 | 681 | | } |
| | 9 | 682 | | }); |
| | | 683 | | |
| | 5 | 684 | | return asyncResult; |
| | 5 | 685 | | } |
| | | 686 | | |
| | | 687 | | /// <summary> |
| | | 688 | | /// Ends an asynchronous operation of retrieving list of files in remote directory. |
| | | 689 | | /// </summary> |
| | | 690 | | /// <param name="asyncResult">The pending asynchronous SFTP request.</param> |
| | | 691 | | /// <returns> |
| | | 692 | | /// A list of files. |
| | | 693 | | /// </returns> |
| | | 694 | | /// <exception cref="ArgumentException">The <see cref="IAsyncResult"/> object did not come from the correspondin |
| | | 695 | | public IEnumerable<ISftpFile> EndListDirectory(IAsyncResult asyncResult) |
| | 4 | 696 | | { |
| | 4 | 697 | | if (asyncResult is not SftpListDirectoryAsyncResult ar || ar.EndInvokeCalled) |
| | 1 | 698 | | { |
| | 1 | 699 | | throw new ArgumentException("Either the IAsyncResult object did not come from the corresponding async me |
| | | 700 | | } |
| | | 701 | | |
| | | 702 | | // Wait for operation to complete, then return result or throw exception |
| | 3 | 703 | | return ar.EndInvoke(); |
| | 2 | 704 | | } |
| | | 705 | | |
| | | 706 | | /// <summary> |
| | | 707 | | /// Gets reference to remote file or directory. |
| | | 708 | | /// </summary> |
| | | 709 | | /// <param name="path">The path.</param> |
| | | 710 | | /// <returns> |
| | | 711 | | /// A reference to <see cref="ISftpFile"/> file object. |
| | | 712 | | /// </returns> |
| | | 713 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 714 | | /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</excep |
| | | 715 | | /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception> |
| | | 716 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 717 | | public ISftpFile Get(string path) |
| | 52 | 718 | | { |
| | 52 | 719 | | CheckDisposed(); |
| | | 720 | | |
| | 52 | 721 | | if (path is null) |
| | 1 | 722 | | { |
| | 1 | 723 | | throw new ArgumentNullException(nameof(path)); |
| | | 724 | | } |
| | | 725 | | |
| | 51 | 726 | | if (_sftpSession is null) |
| | 0 | 727 | | { |
| | 0 | 728 | | throw new SshConnectionException("Client not connected."); |
| | | 729 | | } |
| | | 730 | | |
| | 51 | 731 | | var fullPath = _sftpSession.GetCanonicalPath(path); |
| | | 732 | | |
| | 51 | 733 | | var attributes = _sftpSession.RequestLStat(fullPath); |
| | | 734 | | |
| | 50 | 735 | | return new SftpFile(_sftpSession, fullPath, attributes); |
| | 50 | 736 | | } |
| | | 737 | | |
| | | 738 | | /// <summary> |
| | | 739 | | /// Checks whether file or directory exists. |
| | | 740 | | /// </summary> |
| | | 741 | | /// <param name="path">The path.</param> |
| | | 742 | | /// <returns> |
| | | 743 | | /// <see langword="true"/> if directory or file exists; otherwise <see langword="false"/>. |
| | | 744 | | /// </returns> |
| | | 745 | | /// <exception cref="ArgumentException"><paramref name="path"/> is <see langword="null"/> or contains only white |
| | | 746 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 747 | | /// <exception cref="SftpPermissionDeniedException">Permission to perform the operation was denied by the remote |
| | | 748 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message"/> is the message from the rem |
| | | 749 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 750 | | public bool Exists(string path) |
| | 439 | 751 | | { |
| | 439 | 752 | | CheckDisposed(); |
| | | 753 | | |
| | 439 | 754 | | if (string.IsNullOrWhiteSpace(path)) |
| | 0 | 755 | | { |
| | 0 | 756 | | throw new ArgumentException("path"); |
| | | 757 | | } |
| | | 758 | | |
| | 439 | 759 | | if (_sftpSession is null) |
| | 0 | 760 | | { |
| | 0 | 761 | | throw new SshConnectionException("Client not connected."); |
| | | 762 | | } |
| | | 763 | | |
| | 439 | 764 | | var fullPath = _sftpSession.GetCanonicalPath(path); |
| | | 765 | | |
| | | 766 | | /* |
| | | 767 | | * Using SSH_FXP_REALPATH is not an alternative as the SFTP specification has not always |
| | | 768 | | * been clear on how the server should respond when the specified path is not present on |
| | | 769 | | * the server: |
| | | 770 | | * |
| | | 771 | | * SSH 1 to 4: |
| | | 772 | | * No mention of how the server should respond if the path is not present on the server. |
| | | 773 | | * |
| | | 774 | | * SSH 5: |
| | | 775 | | * The server SHOULD fail the request if the path is not present on the server. |
| | | 776 | | * |
| | | 777 | | * SSH 6: |
| | | 778 | | * Draft 06: The server SHOULD fail the request if the path is not present on the server. |
| | | 779 | | * Draft 07 to 13: The server MUST NOT fail the request if the path does not exist. |
| | | 780 | | * |
| | | 781 | | * Note that SSH 6 (draft 06 and forward) allows for more control options, but we |
| | | 782 | | * currently only support up to v3. |
| | | 783 | | */ |
| | | 784 | | |
| | | 785 | | try |
| | 439 | 786 | | { |
| | 439 | 787 | | _ = _sftpSession.RequestLStat(fullPath); |
| | 185 | 788 | | return true; |
| | | 789 | | } |
| | 254 | 790 | | catch (SftpPathNotFoundException) |
| | 254 | 791 | | { |
| | 254 | 792 | | return false; |
| | | 793 | | } |
| | 439 | 794 | | } |
| | | 795 | | |
| | | 796 | | /// <summary> |
| | | 797 | | /// Downloads remote file specified by the path into the stream. |
| | | 798 | | /// </summary> |
| | | 799 | | /// <param name="path">File to download.</param> |
| | | 800 | | /// <param name="output">Stream to write the file into.</param> |
| | | 801 | | /// <param name="downloadCallback">The download callback.</param> |
| | | 802 | | /// <exception cref="ArgumentNullException"><paramref name="output" /> is <see langword="null"/>.</exception> |
| | | 803 | | /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whit |
| | | 804 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 805 | | /// <exception cref="SftpPermissionDeniedException">Permission to perform the operation was denied by the remote |
| | | 806 | | /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</excep |
| | | 807 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the re |
| | | 808 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 809 | | /// <remarks> |
| | | 810 | | /// Method calls made by this method to <paramref name="output" />, may under certain conditions result in excep |
| | | 811 | | /// </remarks> |
| | | 812 | | public void DownloadFile(string path, Stream output, Action<ulong> downloadCallback = null) |
| | 38 | 813 | | { |
| | 38 | 814 | | CheckDisposed(); |
| | | 815 | | |
| | 38 | 816 | | InternalDownloadFile(path, output, asyncResult: null, downloadCallback); |
| | 35 | 817 | | } |
| | | 818 | | |
| | | 819 | | /// <summary> |
| | | 820 | | /// Begins an asynchronous file downloading into the stream. |
| | | 821 | | /// </summary> |
| | | 822 | | /// <param name="path">The path.</param> |
| | | 823 | | /// <param name="output">The output.</param> |
| | | 824 | | /// <returns> |
| | | 825 | | /// An <see cref="IAsyncResult" /> that references the asynchronous operation. |
| | | 826 | | /// </returns> |
| | | 827 | | /// <exception cref="ArgumentNullException"><paramref name="output" /> is <see langword="null"/>.</exception> |
| | | 828 | | /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whit |
| | | 829 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 830 | | /// <exception cref="SftpPermissionDeniedException">Permission to perform the operation was denied by the remote |
| | | 831 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the re |
| | | 832 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 833 | | /// <remarks> |
| | | 834 | | /// Method calls made by this method to <paramref name="output" />, may under certain conditions result in excep |
| | | 835 | | /// </remarks> |
| | | 836 | | public IAsyncResult BeginDownloadFile(string path, Stream output) |
| | 1 | 837 | | { |
| | 1 | 838 | | return BeginDownloadFile(path, output, asyncCallback: null, state: null); |
| | 1 | 839 | | } |
| | | 840 | | |
| | | 841 | | /// <summary> |
| | | 842 | | /// Begins an asynchronous file downloading into the stream. |
| | | 843 | | /// </summary> |
| | | 844 | | /// <param name="path">The path.</param> |
| | | 845 | | /// <param name="output">The output.</param> |
| | | 846 | | /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</par |
| | | 847 | | /// <returns> |
| | | 848 | | /// An <see cref="IAsyncResult" /> that references the asynchronous operation. |
| | | 849 | | /// </returns> |
| | | 850 | | /// <exception cref="ArgumentNullException"><paramref name="output" /> is <see langword="null"/>.</exception> |
| | | 851 | | /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whit |
| | | 852 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 853 | | /// <exception cref="SftpPermissionDeniedException">Permission to perform the operation was denied by the remote |
| | | 854 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the re |
| | | 855 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 856 | | /// <remarks> |
| | | 857 | | /// Method calls made by this method to <paramref name="output" />, may under certain conditions result in excep |
| | | 858 | | /// </remarks> |
| | | 859 | | public IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback asyncCallback) |
| | 0 | 860 | | { |
| | 0 | 861 | | return BeginDownloadFile(path, output, asyncCallback, state: null); |
| | 0 | 862 | | } |
| | | 863 | | |
| | | 864 | | /// <summary> |
| | | 865 | | /// Begins an asynchronous file downloading into the stream. |
| | | 866 | | /// </summary> |
| | | 867 | | /// <param name="path">The path.</param> |
| | | 868 | | /// <param name="output">The output.</param> |
| | | 869 | | /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</par |
| | | 870 | | /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request fro |
| | | 871 | | /// <param name="downloadCallback">The download callback.</param> |
| | | 872 | | /// <returns> |
| | | 873 | | /// An <see cref="IAsyncResult" /> that references the asynchronous operation. |
| | | 874 | | /// </returns> |
| | | 875 | | /// <exception cref="ArgumentNullException"><paramref name="output" /> is <see langword="null"/>.</exception> |
| | | 876 | | /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whit |
| | | 877 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 878 | | /// <remarks> |
| | | 879 | | /// Method calls made by this method to <paramref name="output" />, may under certain conditions result in excep |
| | | 880 | | /// </remarks> |
| | | 881 | | public IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback asyncCallback, object state, Act |
| | 16 | 882 | | { |
| | 16 | 883 | | CheckDisposed(); |
| | | 884 | | |
| | 16 | 885 | | if (string.IsNullOrWhiteSpace(path)) |
| | 2 | 886 | | { |
| | 2 | 887 | | throw new ArgumentException("path"); |
| | | 888 | | } |
| | | 889 | | |
| | 14 | 890 | | if (output is null) |
| | 1 | 891 | | { |
| | 1 | 892 | | throw new ArgumentNullException(nameof(output)); |
| | | 893 | | } |
| | | 894 | | |
| | 13 | 895 | | var asyncResult = new SftpDownloadAsyncResult(asyncCallback, state); |
| | | 896 | | |
| | 13 | 897 | | ThreadAbstraction.ExecuteThread(() => |
| | 13 | 898 | | { |
| | 13 | 899 | | try |
| | 13 | 900 | | { |
| | 13 | 901 | | InternalDownloadFile(path, output, asyncResult, offset => |
| | 1643 | 902 | | { |
| | 1643 | 903 | | asyncResult.Update(offset); |
| | 13 | 904 | | |
| | 1643 | 905 | | downloadCallback?.Invoke(offset); |
| | 1656 | 906 | | }); |
| | 13 | 907 | | |
| | 11 | 908 | | asyncResult.SetAsCompleted(exception: null, completedSynchronously: false); |
| | 11 | 909 | | } |
| | 2 | 910 | | catch (Exception exp) |
| | 2 | 911 | | { |
| | 2 | 912 | | asyncResult.SetAsCompleted(exp, completedSynchronously: false); |
| | 2 | 913 | | } |
| | 26 | 914 | | }); |
| | | 915 | | |
| | 13 | 916 | | return asyncResult; |
| | 13 | 917 | | } |
| | | 918 | | |
| | | 919 | | /// <summary> |
| | | 920 | | /// Ends an asynchronous file downloading into the stream. |
| | | 921 | | /// </summary> |
| | | 922 | | /// <param name="asyncResult">The pending asynchronous SFTP request.</param> |
| | | 923 | | /// <exception cref="ArgumentException">The <see cref="IAsyncResult"/> object did not come from the correspondin |
| | | 924 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 925 | | /// <exception cref="SftpPermissionDeniedException">Permission to perform the operation was denied by the remote |
| | | 926 | | /// <exception cref="SftpPathNotFoundException">The path was not found on the remote host.</exception> |
| | | 927 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the re |
| | | 928 | | public void EndDownloadFile(IAsyncResult asyncResult) |
| | 13 | 929 | | { |
| | 13 | 930 | | if (asyncResult is not SftpDownloadAsyncResult ar || ar.EndInvokeCalled) |
| | 1 | 931 | | { |
| | 1 | 932 | | throw new ArgumentException("Either the IAsyncResult object did not come from the corresponding async me |
| | | 933 | | } |
| | | 934 | | |
| | | 935 | | // Wait for operation to complete, then return result or throw exception |
| | 12 | 936 | | ar.EndInvoke(); |
| | 11 | 937 | | } |
| | | 938 | | |
| | | 939 | | /// <summary> |
| | | 940 | | /// Uploads stream into remote file. |
| | | 941 | | /// </summary> |
| | | 942 | | /// <param name="input">Data input stream.</param> |
| | | 943 | | /// <param name="path">Remote file path.</param> |
| | | 944 | | /// <param name="uploadCallback">The upload callback.</param> |
| | | 945 | | /// <exception cref="ArgumentNullException"><paramref name="input" /> is <see langword="null"/>.</exception> |
| | | 946 | | /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whit |
| | | 947 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 948 | | /// <exception cref="SftpPermissionDeniedException">Permission to upload the file was denied by the remote host. |
| | | 949 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the re |
| | | 950 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 951 | | /// <remarks> |
| | | 952 | | /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in except |
| | | 953 | | /// </remarks> |
| | | 954 | | public void UploadFile(Stream input, string path, Action<ulong> uploadCallback = null) |
| | 34 | 955 | | { |
| | 34 | 956 | | UploadFile(input, path, canOverride: true, uploadCallback); |
| | 33 | 957 | | } |
| | | 958 | | |
| | | 959 | | /// <summary> |
| | | 960 | | /// Uploads stream into remote file. |
| | | 961 | | /// </summary> |
| | | 962 | | /// <param name="input">Data input stream.</param> |
| | | 963 | | /// <param name="path">Remote file path.</param> |
| | | 964 | | /// <param name="canOverride">if set to <see langword="true"/> then existing file will be overwritten.</param> |
| | | 965 | | /// <param name="uploadCallback">The upload callback.</param> |
| | | 966 | | /// <exception cref="ArgumentNullException"><paramref name="input" /> is <see langword="null"/>.</exception> |
| | | 967 | | /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whit |
| | | 968 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 969 | | /// <exception cref="SftpPermissionDeniedException">Permission to upload the file was denied by the remote host. |
| | | 970 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the re |
| | | 971 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 972 | | /// <remarks> |
| | | 973 | | /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in except |
| | | 974 | | /// </remarks> |
| | | 975 | | public void UploadFile(Stream input, string path, bool canOverride, Action<ulong> uploadCallback = null) |
| | 34 | 976 | | { |
| | 34 | 977 | | CheckDisposed(); |
| | | 978 | | |
| | 34 | 979 | | var flags = Flags.Write | Flags.Truncate; |
| | | 980 | | |
| | 34 | 981 | | if (canOverride) |
| | 34 | 982 | | { |
| | 34 | 983 | | flags |= Flags.CreateNewOrOpen; |
| | 34 | 984 | | } |
| | | 985 | | else |
| | 0 | 986 | | { |
| | 0 | 987 | | flags |= Flags.CreateNew; |
| | 0 | 988 | | } |
| | | 989 | | |
| | 34 | 990 | | InternalUploadFile(input, path, flags, asyncResult: null, uploadCallback); |
| | 33 | 991 | | } |
| | | 992 | | |
| | | 993 | | /// <summary> |
| | | 994 | | /// Begins an asynchronous uploading the stream into remote file. |
| | | 995 | | /// </summary> |
| | | 996 | | /// <param name="input">Data input stream.</param> |
| | | 997 | | /// <param name="path">Remote file path.</param> |
| | | 998 | | /// <returns> |
| | | 999 | | /// An <see cref="IAsyncResult" /> that references the asynchronous operation. |
| | | 1000 | | /// </returns> |
| | | 1001 | | /// <exception cref="ArgumentNullException"><paramref name="input" /> is <see langword="null"/>.</exception> |
| | | 1002 | | /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whit |
| | | 1003 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1004 | | /// <exception cref="SftpPermissionDeniedException">Permission to list the contents of the directory was denied |
| | | 1005 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the re |
| | | 1006 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1007 | | /// <remarks> |
| | | 1008 | | /// <para> |
| | | 1009 | | /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in except |
| | | 1010 | | /// </para> |
| | | 1011 | | /// <para> |
| | | 1012 | | /// If the remote file already exists, it is overwritten and truncated. |
| | | 1013 | | /// </para> |
| | | 1014 | | /// </remarks> |
| | | 1015 | | public IAsyncResult BeginUploadFile(Stream input, string path) |
| | 3 | 1016 | | { |
| | 3 | 1017 | | return BeginUploadFile(input, path, canOverride: true, asyncCallback: null, state: null); |
| | 3 | 1018 | | } |
| | | 1019 | | |
| | | 1020 | | /// <summary> |
| | | 1021 | | /// Begins an asynchronous uploading the stream into remote file. |
| | | 1022 | | /// </summary> |
| | | 1023 | | /// <param name="input">Data input stream.</param> |
| | | 1024 | | /// <param name="path">Remote file path.</param> |
| | | 1025 | | /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</par |
| | | 1026 | | /// <returns> |
| | | 1027 | | /// An <see cref="IAsyncResult" /> that references the asynchronous operation. |
| | | 1028 | | /// </returns> |
| | | 1029 | | /// <exception cref="ArgumentNullException"><paramref name="input" /> is <see langword="null"/>.</exception> |
| | | 1030 | | /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whit |
| | | 1031 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1032 | | /// <exception cref="SftpPermissionDeniedException">Permission to list the contents of the directory was denied |
| | | 1033 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the re |
| | | 1034 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1035 | | /// <remarks> |
| | | 1036 | | /// <para> |
| | | 1037 | | /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in except |
| | | 1038 | | /// </para> |
| | | 1039 | | /// <para> |
| | | 1040 | | /// If the remote file already exists, it is overwritten and truncated. |
| | | 1041 | | /// </para> |
| | | 1042 | | /// </remarks> |
| | | 1043 | | public IAsyncResult BeginUploadFile(Stream input, string path, AsyncCallback asyncCallback) |
| | 1 | 1044 | | { |
| | 1 | 1045 | | return BeginUploadFile(input, path, canOverride: true, asyncCallback, state: null); |
| | 1 | 1046 | | } |
| | | 1047 | | |
| | | 1048 | | /// <summary> |
| | | 1049 | | /// Begins an asynchronous uploading the stream into remote file. |
| | | 1050 | | /// </summary> |
| | | 1051 | | /// <param name="input">Data input stream.</param> |
| | | 1052 | | /// <param name="path">Remote file path.</param> |
| | | 1053 | | /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</par |
| | | 1054 | | /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request fro |
| | | 1055 | | /// <param name="uploadCallback">The upload callback.</param> |
| | | 1056 | | /// <returns> |
| | | 1057 | | /// An <see cref="IAsyncResult" /> that references the asynchronous operation. |
| | | 1058 | | /// </returns> |
| | | 1059 | | /// <exception cref="ArgumentNullException"><paramref name="input" /> is <see langword="null"/>.</exception> |
| | | 1060 | | /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whit |
| | | 1061 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1062 | | /// <exception cref="SftpPermissionDeniedException">Permission to list the contents of the directory was denied |
| | | 1063 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the re |
| | | 1064 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1065 | | /// <remarks> |
| | | 1066 | | /// <para> |
| | | 1067 | | /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in except |
| | | 1068 | | /// </para> |
| | | 1069 | | /// <para> |
| | | 1070 | | /// If the remote file already exists, it is overwritten and truncated. |
| | | 1071 | | /// </para> |
| | | 1072 | | /// </remarks> |
| | | 1073 | | public IAsyncResult BeginUploadFile(Stream input, string path, AsyncCallback asyncCallback, object state, Action |
| | 15 | 1074 | | { |
| | 15 | 1075 | | return BeginUploadFile(input, path, canOverride: true, asyncCallback, state, uploadCallback); |
| | 12 | 1076 | | } |
| | | 1077 | | |
| | | 1078 | | /// <summary> |
| | | 1079 | | /// Begins an asynchronous uploading the stream into remote file. |
| | | 1080 | | /// </summary> |
| | | 1081 | | /// <param name="input">Data input stream.</param> |
| | | 1082 | | /// <param name="path">Remote file path.</param> |
| | | 1083 | | /// <param name="canOverride">Specified whether an existing file can be overwritten.</param> |
| | | 1084 | | /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</par |
| | | 1085 | | /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request fro |
| | | 1086 | | /// <param name="uploadCallback">The upload callback.</param> |
| | | 1087 | | /// <returns> |
| | | 1088 | | /// An <see cref="IAsyncResult" /> that references the asynchronous operation. |
| | | 1089 | | /// </returns> |
| | | 1090 | | /// <exception cref="ArgumentNullException"><paramref name="input" /> is <see langword="null"/>.</exception> |
| | | 1091 | | /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains only whit |
| | | 1092 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1093 | | /// <remarks> |
| | | 1094 | | /// <para> |
| | | 1095 | | /// Method calls made by this method to <paramref name="input" />, may under certain conditions result in except |
| | | 1096 | | /// </para> |
| | | 1097 | | /// <para> |
| | | 1098 | | /// When <paramref name="path"/> refers to an existing file, set <paramref name="canOverride"/> to <see langword |
| | | 1099 | | /// If <paramref name="canOverride"/> is <see langword="false"/>, the upload will fail and <see cref="EndUploadF |
| | | 1100 | | /// <see cref="SshException"/>. |
| | | 1101 | | /// </para> |
| | | 1102 | | /// </remarks> |
| | | 1103 | | public IAsyncResult BeginUploadFile(Stream input, string path, bool canOverride, AsyncCallback asyncCallback, ob |
| | 25 | 1104 | | { |
| | 25 | 1105 | | CheckDisposed(); |
| | | 1106 | | |
| | 25 | 1107 | | if (input is null) |
| | 1 | 1108 | | { |
| | 1 | 1109 | | throw new ArgumentNullException(nameof(input)); |
| | | 1110 | | } |
| | | 1111 | | |
| | 24 | 1112 | | if (string.IsNullOrWhiteSpace(path)) |
| | 2 | 1113 | | { |
| | 2 | 1114 | | throw new ArgumentException("path"); |
| | | 1115 | | } |
| | | 1116 | | |
| | 22 | 1117 | | var flags = Flags.Write | Flags.Truncate; |
| | | 1118 | | |
| | 22 | 1119 | | if (canOverride) |
| | 19 | 1120 | | { |
| | 19 | 1121 | | flags |= Flags.CreateNewOrOpen; |
| | 19 | 1122 | | } |
| | | 1123 | | else |
| | 3 | 1124 | | { |
| | 3 | 1125 | | flags |= Flags.CreateNew; |
| | 3 | 1126 | | } |
| | | 1127 | | |
| | 22 | 1128 | | var asyncResult = new SftpUploadAsyncResult(asyncCallback, state); |
| | | 1129 | | |
| | 22 | 1130 | | ThreadAbstraction.ExecuteThread(() => |
| | 22 | 1131 | | { |
| | 22 | 1132 | | try |
| | 22 | 1133 | | { |
| | 22 | 1134 | | InternalUploadFile(input, path, flags, asyncResult, offset => |
| | 1649 | 1135 | | { |
| | 1649 | 1136 | | asyncResult.Update(offset); |
| | 1649 | 1137 | | uploadCallback?.Invoke(offset); |
| | 1671 | 1138 | | }); |
| | 22 | 1139 | | |
| | 17 | 1140 | | asyncResult.SetAsCompleted(exception: null, completedSynchronously: false); |
| | 17 | 1141 | | } |
| | 5 | 1142 | | catch (Exception exp) |
| | 5 | 1143 | | { |
| | 5 | 1144 | | asyncResult.SetAsCompleted(exception: exp, completedSynchronously: false); |
| | 5 | 1145 | | } |
| | 44 | 1146 | | }); |
| | | 1147 | | |
| | 22 | 1148 | | return asyncResult; |
| | 22 | 1149 | | } |
| | | 1150 | | |
| | | 1151 | | /// <summary> |
| | | 1152 | | /// Ends an asynchronous uploading the stream into remote file. |
| | | 1153 | | /// </summary> |
| | | 1154 | | /// <param name="asyncResult">The pending asynchronous SFTP request.</param> |
| | | 1155 | | /// <exception cref="ArgumentException">The <see cref="IAsyncResult"/> object did not come from the correspondin |
| | | 1156 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1157 | | /// <exception cref="SftpPathNotFoundException">The directory of the file was not found on the remote host.</exc |
| | | 1158 | | /// <exception cref="SftpPermissionDeniedException">Permission to upload the file was denied by the remote host. |
| | | 1159 | | /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the re |
| | | 1160 | | public void EndUploadFile(IAsyncResult asyncResult) |
| | 21 | 1161 | | { |
| | 21 | 1162 | | if (asyncResult is not SftpUploadAsyncResult ar || ar.EndInvokeCalled) |
| | 1 | 1163 | | { |
| | 1 | 1164 | | throw new ArgumentException("Either the IAsyncResult object did not come from the corresponding async me |
| | | 1165 | | } |
| | | 1166 | | |
| | | 1167 | | // Wait for operation to complete, then return result or throw exception |
| | 20 | 1168 | | ar.EndInvoke(); |
| | 16 | 1169 | | } |
| | | 1170 | | |
| | | 1171 | | /// <summary> |
| | | 1172 | | /// Gets status using statvfs@openssh.com request. |
| | | 1173 | | /// </summary> |
| | | 1174 | | /// <param name="path">The path.</param> |
| | | 1175 | | /// <returns> |
| | | 1176 | | /// A <see cref="SftpFileSytemInformation"/> instance that contains file status information. |
| | | 1177 | | /// </returns> |
| | | 1178 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1179 | | /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception> |
| | | 1180 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1181 | | public SftpFileSytemInformation GetStatus(string path) |
| | 0 | 1182 | | { |
| | 0 | 1183 | | CheckDisposed(); |
| | | 1184 | | |
| | 0 | 1185 | | if (path is null) |
| | 0 | 1186 | | { |
| | 0 | 1187 | | throw new ArgumentNullException(nameof(path)); |
| | | 1188 | | } |
| | | 1189 | | |
| | 0 | 1190 | | if (_sftpSession is null) |
| | 0 | 1191 | | { |
| | 0 | 1192 | | throw new SshConnectionException("Client not connected."); |
| | | 1193 | | } |
| | | 1194 | | |
| | 0 | 1195 | | var fullPath = _sftpSession.GetCanonicalPath(path); |
| | | 1196 | | |
| | 0 | 1197 | | return _sftpSession.RequestStatVfs(fullPath); |
| | 0 | 1198 | | } |
| | | 1199 | | |
| | | 1200 | | /// <summary> |
| | | 1201 | | /// Asynchronously gets status using statvfs@openssh.com request. |
| | | 1202 | | /// </summary> |
| | | 1203 | | /// <param name="path">The path.</param> |
| | | 1204 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param> |
| | | 1205 | | /// <returns> |
| | | 1206 | | /// A <see cref="Task{SftpFileSytemInformation}"/> that represents the status operation. |
| | | 1207 | | /// The task result contains the <see cref="SftpFileSytemInformation"/> instance that contains file status infor |
| | | 1208 | | /// </returns> |
| | | 1209 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1210 | | /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception> |
| | | 1211 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1212 | | public async Task<SftpFileSytemInformation> GetStatusAsync(string path, CancellationToken cancellationToken) |
| | 0 | 1213 | | { |
| | 0 | 1214 | | CheckDisposed(); |
| | | 1215 | | |
| | 0 | 1216 | | if (path is null) |
| | 0 | 1217 | | { |
| | 0 | 1218 | | throw new ArgumentNullException(nameof(path)); |
| | | 1219 | | } |
| | | 1220 | | |
| | 0 | 1221 | | if (_sftpSession is null) |
| | 0 | 1222 | | { |
| | 0 | 1223 | | throw new SshConnectionException("Client not connected."); |
| | | 1224 | | } |
| | | 1225 | | |
| | 0 | 1226 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 1227 | | |
| | 0 | 1228 | | var fullPath = await _sftpSession.GetCanonicalPathAsync(path, cancellationToken).ConfigureAwait(false); |
| | 0 | 1229 | | return await _sftpSession.RequestStatVfsAsync(fullPath, cancellationToken).ConfigureAwait(false); |
| | 0 | 1230 | | } |
| | | 1231 | | |
| | | 1232 | | #region File Methods |
| | | 1233 | | |
| | | 1234 | | /// <summary> |
| | | 1235 | | /// Appends lines to a file, creating the file if it does not already exist. |
| | | 1236 | | /// </summary> |
| | | 1237 | | /// <param name="path">The file to append the lines to. The file is created if it does not already exist.</param |
| | | 1238 | | /// <param name="contents">The lines to append to the file.</param> |
| | | 1239 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>. <para>-or-</para> |
| | | 1240 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1241 | | /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on |
| | | 1242 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1243 | | /// <remarks> |
| | | 1244 | | /// The characters are written to the file using UTF-8 encoding without a byte-order mark (BOM). |
| | | 1245 | | /// </remarks> |
| | | 1246 | | public void AppendAllLines(string path, IEnumerable<string> contents) |
| | 3 | 1247 | | { |
| | 3 | 1248 | | CheckDisposed(); |
| | | 1249 | | |
| | 3 | 1250 | | if (contents is null) |
| | 0 | 1251 | | { |
| | 0 | 1252 | | throw new ArgumentNullException(nameof(contents)); |
| | | 1253 | | } |
| | | 1254 | | |
| | 3 | 1255 | | using (var stream = AppendText(path)) |
| | 2 | 1256 | | { |
| | 18 | 1257 | | foreach (var line in contents) |
| | 6 | 1258 | | { |
| | 6 | 1259 | | stream.WriteLine(line); |
| | 6 | 1260 | | } |
| | 2 | 1261 | | } |
| | 2 | 1262 | | } |
| | | 1263 | | |
| | | 1264 | | /// <summary> |
| | | 1265 | | /// Appends lines to a file by using a specified encoding, creating the file if it does not already exist. |
| | | 1266 | | /// </summary> |
| | | 1267 | | /// <param name="path">The file to append the lines to. The file is created if it does not already exist.</param |
| | | 1268 | | /// <param name="contents">The lines to append to the file.</param> |
| | | 1269 | | /// <param name="encoding">The character encoding to use.</param> |
| | | 1270 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>. <para>-or-</para> |
| | | 1271 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1272 | | /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on |
| | | 1273 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1274 | | public void AppendAllLines(string path, IEnumerable<string> contents, Encoding encoding) |
| | 3 | 1275 | | { |
| | 3 | 1276 | | CheckDisposed(); |
| | | 1277 | | |
| | 3 | 1278 | | if (contents is null) |
| | 0 | 1279 | | { |
| | 0 | 1280 | | throw new ArgumentNullException(nameof(contents)); |
| | | 1281 | | } |
| | | 1282 | | |
| | 3 | 1283 | | using (var stream = AppendText(path, encoding)) |
| | 2 | 1284 | | { |
| | 18 | 1285 | | foreach (var line in contents) |
| | 6 | 1286 | | { |
| | 6 | 1287 | | stream.WriteLine(line); |
| | 6 | 1288 | | } |
| | 2 | 1289 | | } |
| | 2 | 1290 | | } |
| | | 1291 | | |
| | | 1292 | | /// <summary> |
| | | 1293 | | /// Appends the specified string to the file, creating the file if it does not already exist. |
| | | 1294 | | /// </summary> |
| | | 1295 | | /// <param name="path">The file to append the specified string to.</param> |
| | | 1296 | | /// <param name="contents">The string to append to the file.</param> |
| | | 1297 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>. <para>-or-</para> |
| | | 1298 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1299 | | /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on |
| | | 1300 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1301 | | /// <remarks> |
| | | 1302 | | /// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM). |
| | | 1303 | | /// </remarks> |
| | | 1304 | | public void AppendAllText(string path, string contents) |
| | 3 | 1305 | | { |
| | 3 | 1306 | | using (var stream = AppendText(path)) |
| | 2 | 1307 | | { |
| | 2 | 1308 | | stream.Write(contents); |
| | 2 | 1309 | | } |
| | 2 | 1310 | | } |
| | | 1311 | | |
| | | 1312 | | /// <summary> |
| | | 1313 | | /// Appends the specified string to the file, creating the file if it does not already exist. |
| | | 1314 | | /// </summary> |
| | | 1315 | | /// <param name="path">The file to append the specified string to.</param> |
| | | 1316 | | /// <param name="contents">The string to append to the file.</param> |
| | | 1317 | | /// <param name="encoding">The character encoding to use.</param> |
| | | 1318 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>. <para>-or-</para> |
| | | 1319 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1320 | | /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on |
| | | 1321 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1322 | | public void AppendAllText(string path, string contents, Encoding encoding) |
| | 3 | 1323 | | { |
| | 3 | 1324 | | using (var stream = AppendText(path, encoding)) |
| | 2 | 1325 | | { |
| | 2 | 1326 | | stream.Write(contents); |
| | 2 | 1327 | | } |
| | 2 | 1328 | | } |
| | | 1329 | | |
| | | 1330 | | /// <summary> |
| | | 1331 | | /// Creates a <see cref="StreamWriter"/> that appends UTF-8 encoded text to the specified file, |
| | | 1332 | | /// creating the file if it does not already exist. |
| | | 1333 | | /// </summary> |
| | | 1334 | | /// <param name="path">The path to the file to append to.</param> |
| | | 1335 | | /// <returns> |
| | | 1336 | | /// A <see cref="StreamWriter"/> that appends text to a file using UTF-8 encoding without a |
| | | 1337 | | /// Byte-Order Mark (BOM). |
| | | 1338 | | /// </returns> |
| | | 1339 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1340 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1341 | | /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on |
| | | 1342 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1343 | | public StreamWriter AppendText(string path) |
| | 12 | 1344 | | { |
| | 12 | 1345 | | return AppendText(path, Utf8NoBOM); |
| | 9 | 1346 | | } |
| | | 1347 | | |
| | | 1348 | | /// <summary> |
| | | 1349 | | /// Creates a <see cref="StreamWriter"/> that appends text to a file using the specified |
| | | 1350 | | /// encoding, creating the file if it does not already exist. |
| | | 1351 | | /// </summary> |
| | | 1352 | | /// <param name="path">The path to the file to append to.</param> |
| | | 1353 | | /// <param name="encoding">The character encoding to use.</param> |
| | | 1354 | | /// <returns> |
| | | 1355 | | /// A <see cref="StreamWriter"/> that appends text to a file using the specified encoding. |
| | | 1356 | | /// </returns> |
| | | 1357 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>. <para>-or-</para> |
| | | 1358 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1359 | | /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on |
| | | 1360 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1361 | | public StreamWriter AppendText(string path, Encoding encoding) |
| | 24 | 1362 | | { |
| | 24 | 1363 | | CheckDisposed(); |
| | | 1364 | | |
| | 24 | 1365 | | if (encoding is null) |
| | 0 | 1366 | | { |
| | 0 | 1367 | | throw new ArgumentNullException(nameof(encoding)); |
| | | 1368 | | } |
| | | 1369 | | |
| | 24 | 1370 | | return new StreamWriter(new SftpFileStream(_sftpSession, path, FileMode.Append, FileAccess.Write, (int) _buf |
| | 18 | 1371 | | } |
| | | 1372 | | |
| | | 1373 | | /// <summary> |
| | | 1374 | | /// Creates or overwrites a file in the specified path. |
| | | 1375 | | /// </summary> |
| | | 1376 | | /// <param name="path">The path and name of the file to create.</param> |
| | | 1377 | | /// <returns> |
| | | 1378 | | /// A <see cref="SftpFileStream"/> that provides read/write access to the file specified in path. |
| | | 1379 | | /// </returns> |
| | | 1380 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1381 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1382 | | /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on |
| | | 1383 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1384 | | /// <remarks> |
| | | 1385 | | /// If the target file already exists, it is first truncated to zero bytes. |
| | | 1386 | | /// </remarks> |
| | | 1387 | | public SftpFileStream Create(string path) |
| | 5 | 1388 | | { |
| | 5 | 1389 | | CheckDisposed(); |
| | | 1390 | | |
| | 5 | 1391 | | return new SftpFileStream(_sftpSession, path, FileMode.Create, FileAccess.ReadWrite, (int) _bufferSize); |
| | 4 | 1392 | | } |
| | | 1393 | | |
| | | 1394 | | /// <summary> |
| | | 1395 | | /// Creates or overwrites the specified file. |
| | | 1396 | | /// </summary> |
| | | 1397 | | /// <param name="path">The path and name of the file to create.</param> |
| | | 1398 | | /// <param name="bufferSize">The maximum number of bytes buffered for reads and writes to the file.</param> |
| | | 1399 | | /// <returns> |
| | | 1400 | | /// A <see cref="SftpFileStream"/> that provides read/write access to the file specified in path. |
| | | 1401 | | /// </returns> |
| | | 1402 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1403 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1404 | | /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on |
| | | 1405 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1406 | | /// <remarks> |
| | | 1407 | | /// If the target file already exists, it is first truncated to zero bytes. |
| | | 1408 | | /// </remarks> |
| | | 1409 | | public SftpFileStream Create(string path, int bufferSize) |
| | 0 | 1410 | | { |
| | 0 | 1411 | | CheckDisposed(); |
| | | 1412 | | |
| | 0 | 1413 | | return new SftpFileStream(_sftpSession, path, FileMode.Create, FileAccess.ReadWrite, bufferSize); |
| | 0 | 1414 | | } |
| | | 1415 | | |
| | | 1416 | | /// <summary> |
| | | 1417 | | /// Creates or opens a file for writing UTF-8 encoded text. |
| | | 1418 | | /// </summary> |
| | | 1419 | | /// <param name="path">The file to be opened for writing.</param> |
| | | 1420 | | /// <returns> |
| | | 1421 | | /// A <see cref="StreamWriter"/> that writes text to a file using UTF-8 encoding without |
| | | 1422 | | /// a Byte-Order Mark (BOM). |
| | | 1423 | | /// </returns> |
| | | 1424 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1425 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1426 | | /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on |
| | | 1427 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1428 | | /// <remarks> |
| | | 1429 | | /// <para> |
| | | 1430 | | /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. |
| | | 1431 | | /// </para> |
| | | 1432 | | /// <para> |
| | | 1433 | | /// If the target file does not exist, it is created. |
| | | 1434 | | /// </para> |
| | | 1435 | | /// </remarks> |
| | | 1436 | | public StreamWriter CreateText(string path) |
| | 21 | 1437 | | { |
| | 21 | 1438 | | return CreateText(path, Utf8NoBOM); |
| | 19 | 1439 | | } |
| | | 1440 | | |
| | | 1441 | | /// <summary> |
| | | 1442 | | /// Creates or opens a file for writing text using the specified encoding. |
| | | 1443 | | /// </summary> |
| | | 1444 | | /// <param name="path">The file to be opened for writing.</param> |
| | | 1445 | | /// <param name="encoding">The character encoding to use.</param> |
| | | 1446 | | /// <returns> |
| | | 1447 | | /// A <see cref="StreamWriter"/> that writes to a file using the specified encoding. |
| | | 1448 | | /// </returns> |
| | | 1449 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1450 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1451 | | /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on |
| | | 1452 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1453 | | /// <remarks> |
| | | 1454 | | /// <para> |
| | | 1455 | | /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. |
| | | 1456 | | /// </para> |
| | | 1457 | | /// <para> |
| | | 1458 | | /// If the target file does not exist, it is created. |
| | | 1459 | | /// </para> |
| | | 1460 | | /// </remarks> |
| | | 1461 | | public StreamWriter CreateText(string path, Encoding encoding) |
| | 54 | 1462 | | { |
| | 54 | 1463 | | CheckDisposed(); |
| | | 1464 | | |
| | 54 | 1465 | | return new StreamWriter(OpenWrite(path), encoding); |
| | 46 | 1466 | | } |
| | | 1467 | | |
| | | 1468 | | /// <summary> |
| | | 1469 | | /// Deletes the specified file or directory. |
| | | 1470 | | /// </summary> |
| | | 1471 | | /// <param name="path">The name of the file or directory to be deleted. Wildcard characters are not supported.</ |
| | | 1472 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1473 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1474 | | /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</excep |
| | | 1475 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1476 | | public void Delete(string path) |
| | 10 | 1477 | | { |
| | 10 | 1478 | | var file = Get(path); |
| | 10 | 1479 | | file.Delete(); |
| | 10 | 1480 | | } |
| | | 1481 | | |
| | | 1482 | | /// <summary> |
| | | 1483 | | /// Returns the date and time the specified file or directory was last accessed. |
| | | 1484 | | /// </summary> |
| | | 1485 | | /// <param name="path">The file or directory for which to obtain access date and time information.</param> |
| | | 1486 | | /// <returns> |
| | | 1487 | | /// A <see cref="DateTime"/> structure set to the date and time that the specified file or directory was last ac |
| | | 1488 | | /// This value is expressed in local time. |
| | | 1489 | | /// </returns> |
| | | 1490 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1491 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1492 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1493 | | public DateTime GetLastAccessTime(string path) |
| | 4 | 1494 | | { |
| | 4 | 1495 | | var file = Get(path); |
| | 4 | 1496 | | return file.LastAccessTime; |
| | 4 | 1497 | | } |
| | | 1498 | | |
| | | 1499 | | /// <summary> |
| | | 1500 | | /// Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was las |
| | | 1501 | | /// </summary> |
| | | 1502 | | /// <param name="path">The file or directory for which to obtain access date and time information.</param> |
| | | 1503 | | /// <returns> |
| | | 1504 | | /// A <see cref="DateTime"/> structure set to the date and time that the specified file or directory was last ac |
| | | 1505 | | /// This value is expressed in UTC time. |
| | | 1506 | | /// </returns> |
| | | 1507 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1508 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1509 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1510 | | public DateTime GetLastAccessTimeUtc(string path) |
| | 2 | 1511 | | { |
| | 2 | 1512 | | var lastAccessTime = GetLastAccessTime(path); |
| | 2 | 1513 | | return lastAccessTime.ToUniversalTime(); |
| | 2 | 1514 | | } |
| | | 1515 | | |
| | | 1516 | | /// <summary> |
| | | 1517 | | /// Returns the date and time the specified file or directory was last written to. |
| | | 1518 | | /// </summary> |
| | | 1519 | | /// <param name="path">The file or directory for which to obtain write date and time information.</param> |
| | | 1520 | | /// <returns> |
| | | 1521 | | /// A <see cref="DateTime"/> structure set to the date and time that the specified file or directory was last wr |
| | | 1522 | | /// This value is expressed in local time. |
| | | 1523 | | /// </returns> |
| | | 1524 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1525 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1526 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1527 | | public DateTime GetLastWriteTime(string path) |
| | 4 | 1528 | | { |
| | 4 | 1529 | | var file = Get(path); |
| | 4 | 1530 | | return file.LastWriteTime; |
| | 4 | 1531 | | } |
| | | 1532 | | |
| | | 1533 | | /// <summary> |
| | | 1534 | | /// Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was las |
| | | 1535 | | /// </summary> |
| | | 1536 | | /// <param name="path">The file or directory for which to obtain write date and time information.</param> |
| | | 1537 | | /// <returns> |
| | | 1538 | | /// A <see cref="DateTime"/> structure set to the date and time that the specified file or directory was last wr |
| | | 1539 | | /// This value is expressed in UTC time. |
| | | 1540 | | /// </returns> |
| | | 1541 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1542 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1543 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1544 | | public DateTime GetLastWriteTimeUtc(string path) |
| | 2 | 1545 | | { |
| | 2 | 1546 | | var lastWriteTime = GetLastWriteTime(path); |
| | 2 | 1547 | | return lastWriteTime.ToUniversalTime(); |
| | 2 | 1548 | | } |
| | | 1549 | | |
| | | 1550 | | /// <summary> |
| | | 1551 | | /// Opens a <see cref="SftpFileStream"/> on the specified path with read/write access. |
| | | 1552 | | /// </summary> |
| | | 1553 | | /// <param name="path">The file to open.</param> |
| | | 1554 | | /// <param name="mode">A <see cref="FileMode"/> value that specifies whether a file is created if one does not e |
| | | 1555 | | /// <returns> |
| | | 1556 | | /// An unshared <see cref="SftpFileStream"/> that provides access to the specified file, with the specified mode |
| | | 1557 | | /// </returns> |
| | | 1558 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1559 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1560 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1561 | | public SftpFileStream Open(string path, FileMode mode) |
| | 3 | 1562 | | { |
| | 3 | 1563 | | return Open(path, mode, FileAccess.ReadWrite); |
| | 3 | 1564 | | } |
| | | 1565 | | |
| | | 1566 | | /// <summary> |
| | | 1567 | | /// Opens a <see cref="SftpFileStream"/> on the specified path, with the specified mode and access. |
| | | 1568 | | /// </summary> |
| | | 1569 | | /// <param name="path">The file to open.</param> |
| | | 1570 | | /// <param name="mode">A <see cref="FileMode"/> value that specifies whether a file is created if one does not e |
| | | 1571 | | /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on |
| | | 1572 | | /// <returns> |
| | | 1573 | | /// An unshared <see cref="SftpFileStream"/> that provides access to the specified file, with the specified mode |
| | | 1574 | | /// </returns> |
| | | 1575 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1576 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1577 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1578 | | public SftpFileStream Open(string path, FileMode mode, FileAccess access) |
| | 154 | 1579 | | { |
| | 154 | 1580 | | CheckDisposed(); |
| | | 1581 | | |
| | 154 | 1582 | | return new SftpFileStream(_sftpSession, path, mode, access, (int) _bufferSize); |
| | 144 | 1583 | | } |
| | | 1584 | | |
| | | 1585 | | /// <summary> |
| | | 1586 | | /// Asynchronously opens a <see cref="SftpFileStream"/> on the specified path, with the specified mode and acces |
| | | 1587 | | /// </summary> |
| | | 1588 | | /// <param name="path">The file to open.</param> |
| | | 1589 | | /// <param name="mode">A <see cref="FileMode"/> value that specifies whether a file is created if one does not e |
| | | 1590 | | /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on |
| | | 1591 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param> |
| | | 1592 | | /// <returns> |
| | | 1593 | | /// A <see cref="Task{SftpFileStream}"/> that represents the asynchronous open operation. |
| | | 1594 | | /// The task result contains the <see cref="SftpFileStream"/> that provides access to the specified file, with t |
| | | 1595 | | /// </returns> |
| | | 1596 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1597 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1598 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1599 | | public Task<SftpFileStream> OpenAsync(string path, FileMode mode, FileAccess access, CancellationToken cancellat |
| | 1 | 1600 | | { |
| | 1 | 1601 | | CheckDisposed(); |
| | | 1602 | | |
| | 1 | 1603 | | if (path is null) |
| | 0 | 1604 | | { |
| | 0 | 1605 | | throw new ArgumentNullException(nameof(path)); |
| | | 1606 | | } |
| | | 1607 | | |
| | 1 | 1608 | | if (_sftpSession is null) |
| | 0 | 1609 | | { |
| | 0 | 1610 | | throw new SshConnectionException("Client not connected."); |
| | | 1611 | | } |
| | | 1612 | | |
| | 1 | 1613 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 1614 | | |
| | 1 | 1615 | | return SftpFileStream.OpenAsync(_sftpSession, path, mode, access, (int) _bufferSize, cancellationToken); |
| | 1 | 1616 | | } |
| | | 1617 | | |
| | | 1618 | | /// <summary> |
| | | 1619 | | /// Opens an existing file for reading. |
| | | 1620 | | /// </summary> |
| | | 1621 | | /// <param name="path">The file to be opened for reading.</param> |
| | | 1622 | | /// <returns> |
| | | 1623 | | /// A read-only <see cref="SftpFileStream"/> on the specified path. |
| | | 1624 | | /// </returns> |
| | | 1625 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1626 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1627 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1628 | | public SftpFileStream OpenRead(string path) |
| | 116 | 1629 | | { |
| | 116 | 1630 | | return Open(path, FileMode.Open, FileAccess.Read); |
| | 109 | 1631 | | } |
| | | 1632 | | |
| | | 1633 | | /// <summary> |
| | | 1634 | | /// Opens an existing UTF-8 encoded text file for reading. |
| | | 1635 | | /// </summary> |
| | | 1636 | | /// <param name="path">The file to be opened for reading.</param> |
| | | 1637 | | /// <returns> |
| | | 1638 | | /// A <see cref="StreamReader"/> on the specified path. |
| | | 1639 | | /// </returns> |
| | | 1640 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1641 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1642 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1643 | | public StreamReader OpenText(string path) |
| | 0 | 1644 | | { |
| | 0 | 1645 | | return new StreamReader(OpenRead(path), Encoding.UTF8); |
| | 0 | 1646 | | } |
| | | 1647 | | |
| | | 1648 | | /// <summary> |
| | | 1649 | | /// Opens a file for writing. |
| | | 1650 | | /// </summary> |
| | | 1651 | | /// <param name="path">The file to be opened for writing.</param> |
| | | 1652 | | /// <returns> |
| | | 1653 | | /// An unshared <see cref="SftpFileStream"/> object on the specified path with <see cref="FileAccess.Write"/> ac |
| | | 1654 | | /// </returns> |
| | | 1655 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1656 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1657 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1658 | | /// <remarks> |
| | | 1659 | | /// If the file does not exist, it is created. |
| | | 1660 | | /// </remarks> |
| | | 1661 | | public SftpFileStream OpenWrite(string path) |
| | 105 | 1662 | | { |
| | 105 | 1663 | | CheckDisposed(); |
| | | 1664 | | |
| | 105 | 1665 | | return new SftpFileStream(_sftpSession, path, FileMode.OpenOrCreate, FileAccess.Write, (int) _bufferSize); |
| | 96 | 1666 | | } |
| | | 1667 | | |
| | | 1668 | | /// <summary> |
| | | 1669 | | /// Opens a binary file, reads the contents of the file into a byte array, and closes the file. |
| | | 1670 | | /// </summary> |
| | | 1671 | | /// <param name="path">The file to open for reading.</param> |
| | | 1672 | | /// <returns> |
| | | 1673 | | /// A byte array containing the contents of the file. |
| | | 1674 | | /// </returns> |
| | | 1675 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1676 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1677 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1678 | | public byte[] ReadAllBytes(string path) |
| | 11 | 1679 | | { |
| | 11 | 1680 | | using (var stream = OpenRead(path)) |
| | 10 | 1681 | | { |
| | 10 | 1682 | | var buffer = new byte[stream.Length]; |
| | 10 | 1683 | | _ = stream.Read(buffer, 0, buffer.Length); |
| | 10 | 1684 | | return buffer; |
| | | 1685 | | } |
| | 10 | 1686 | | } |
| | | 1687 | | |
| | | 1688 | | /// <summary> |
| | | 1689 | | /// Opens a text file, reads all lines of the file using UTF-8 encoding, and closes the file. |
| | | 1690 | | /// </summary> |
| | | 1691 | | /// <param name="path">The file to open for reading.</param> |
| | | 1692 | | /// <returns> |
| | | 1693 | | /// A string array containing all lines of the file. |
| | | 1694 | | /// </returns> |
| | | 1695 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1696 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1697 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1698 | | public string[] ReadAllLines(string path) |
| | 4 | 1699 | | { |
| | 4 | 1700 | | return ReadAllLines(path, Encoding.UTF8); |
| | 2 | 1701 | | } |
| | | 1702 | | |
| | | 1703 | | /// <summary> |
| | | 1704 | | /// Opens a file, reads all lines of the file with the specified encoding, and closes the file. |
| | | 1705 | | /// </summary> |
| | | 1706 | | /// <param name="path">The file to open for reading.</param> |
| | | 1707 | | /// <param name="encoding">The encoding applied to the contents of the file.</param> |
| | | 1708 | | /// <returns> |
| | | 1709 | | /// A string array containing all lines of the file. |
| | | 1710 | | /// </returns> |
| | | 1711 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1712 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1713 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1714 | | public string[] ReadAllLines(string path, Encoding encoding) |
| | 8 | 1715 | | { |
| | | 1716 | | /* |
| | | 1717 | | * We use the default buffer size for StreamReader - which is 1024 bytes - and the configured buffer size |
| | | 1718 | | * for the SftpFileStream. We may want to revisit this later. |
| | | 1719 | | */ |
| | | 1720 | | |
| | 8 | 1721 | | var lines = new List<string>(); |
| | | 1722 | | |
| | 8 | 1723 | | using (var stream = new StreamReader(OpenRead(path), encoding)) |
| | 4 | 1724 | | { |
| | 20 | 1725 | | while (!stream.EndOfStream) |
| | 16 | 1726 | | { |
| | 16 | 1727 | | lines.Add(stream.ReadLine()); |
| | 16 | 1728 | | } |
| | 4 | 1729 | | } |
| | | 1730 | | |
| | 4 | 1731 | | return lines.ToArray(); |
| | 4 | 1732 | | } |
| | | 1733 | | |
| | | 1734 | | /// <summary> |
| | | 1735 | | /// Opens a text file, reads all lines of the file with the UTF-8 encoding, and closes the file. |
| | | 1736 | | /// </summary> |
| | | 1737 | | /// <param name="path">The file to open for reading.</param> |
| | | 1738 | | /// <returns> |
| | | 1739 | | /// A string containing all lines of the file. |
| | | 1740 | | /// </returns> |
| | | 1741 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1742 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1743 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1744 | | public string ReadAllText(string path) |
| | 9 | 1745 | | { |
| | 9 | 1746 | | return ReadAllText(path, Encoding.UTF8); |
| | 8 | 1747 | | } |
| | | 1748 | | |
| | | 1749 | | /// <summary> |
| | | 1750 | | /// Opens a file, reads all lines of the file with the specified encoding, and closes the file. |
| | | 1751 | | /// </summary> |
| | | 1752 | | /// <param name="path">The file to open for reading.</param> |
| | | 1753 | | /// <param name="encoding">The encoding applied to the contents of the file.</param> |
| | | 1754 | | /// <returns> |
| | | 1755 | | /// A string containing all lines of the file. |
| | | 1756 | | /// </returns> |
| | | 1757 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1758 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1759 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1760 | | public string ReadAllText(string path, Encoding encoding) |
| | 19 | 1761 | | { |
| | | 1762 | | /* |
| | | 1763 | | * We use the default buffer size for StreamReader - which is 1024 bytes - and the configured buffer size |
| | | 1764 | | * for the SftpFileStream. We may want to revisit this later. |
| | | 1765 | | */ |
| | | 1766 | | |
| | 19 | 1767 | | using (var stream = new StreamReader(OpenRead(path), encoding)) |
| | 17 | 1768 | | { |
| | 17 | 1769 | | return stream.ReadToEnd(); |
| | | 1770 | | } |
| | 17 | 1771 | | } |
| | | 1772 | | |
| | | 1773 | | /// <summary> |
| | | 1774 | | /// Reads the lines of a file with the UTF-8 encoding. |
| | | 1775 | | /// </summary> |
| | | 1776 | | /// <param name="path">The file to read.</param> |
| | | 1777 | | /// <returns> |
| | | 1778 | | /// The lines of the file. |
| | | 1779 | | /// </returns> |
| | | 1780 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1781 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1782 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1783 | | public IEnumerable<string> ReadLines(string path) |
| | 2 | 1784 | | { |
| | 2 | 1785 | | return ReadAllLines(path); |
| | 1 | 1786 | | } |
| | | 1787 | | |
| | | 1788 | | /// <summary> |
| | | 1789 | | /// Read the lines of a file that has a specified encoding. |
| | | 1790 | | /// </summary> |
| | | 1791 | | /// <param name="path">The file to read.</param> |
| | | 1792 | | /// <param name="encoding">The encoding that is applied to the contents of the file.</param> |
| | | 1793 | | /// <returns> |
| | | 1794 | | /// The lines of the file. |
| | | 1795 | | /// </returns> |
| | | 1796 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1797 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1798 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1799 | | public IEnumerable<string> ReadLines(string path, Encoding encoding) |
| | 2 | 1800 | | { |
| | 2 | 1801 | | return ReadAllLines(path, encoding); |
| | 1 | 1802 | | } |
| | | 1803 | | |
| | | 1804 | | /// <summary> |
| | | 1805 | | /// Sets the date and time the specified file was last accessed. |
| | | 1806 | | /// </summary> |
| | | 1807 | | /// <param name="path">The file for which to set the access date and time information.</param> |
| | | 1808 | | /// <param name="lastAccessTime">A <see cref="DateTime"/> containing the value to set for the last access date a |
| | | 1809 | | public void SetLastAccessTime(string path, DateTime lastAccessTime) |
| | 1 | 1810 | | { |
| | 1 | 1811 | | var attributes = GetAttributes(path); |
| | 1 | 1812 | | attributes.LastAccessTime = lastAccessTime; |
| | 1 | 1813 | | SetAttributes(path, attributes); |
| | 1 | 1814 | | } |
| | | 1815 | | |
| | | 1816 | | /// <summary> |
| | | 1817 | | /// Sets the date and time, in coordinated universal time (UTC), that the specified file was last accessed. |
| | | 1818 | | /// </summary> |
| | | 1819 | | /// <param name="path">The file for which to set the access date and time information.</param> |
| | | 1820 | | /// <param name="lastAccessTimeUtc">A <see cref="DateTime"/> containing the value to set for the last access dat |
| | | 1821 | | public void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc) |
| | 1 | 1822 | | { |
| | 1 | 1823 | | var attributes = GetAttributes(path); |
| | 1 | 1824 | | attributes.LastAccessTimeUtc = lastAccessTimeUtc; |
| | 1 | 1825 | | SetAttributes(path, attributes); |
| | 1 | 1826 | | } |
| | | 1827 | | |
| | | 1828 | | /// <summary> |
| | | 1829 | | /// Sets the date and time that the specified file was last written to. |
| | | 1830 | | /// </summary> |
| | | 1831 | | /// <param name="path">The file for which to set the date and time information.</param> |
| | | 1832 | | /// <param name="lastWriteTime">A <see cref="DateTime"/> containing the value to set for the last write date and |
| | | 1833 | | public void SetLastWriteTime(string path, DateTime lastWriteTime) |
| | 1 | 1834 | | { |
| | 1 | 1835 | | var attributes = GetAttributes(path); |
| | 1 | 1836 | | attributes.LastWriteTime = lastWriteTime; |
| | 1 | 1837 | | SetAttributes(path, attributes); |
| | 1 | 1838 | | } |
| | | 1839 | | |
| | | 1840 | | /// <summary> |
| | | 1841 | | /// Sets the date and time, in coordinated universal time (UTC), that the specified file was last written to. |
| | | 1842 | | /// </summary> |
| | | 1843 | | /// <param name="path">The file for which to set the date and time information.</param> |
| | | 1844 | | /// <param name="lastWriteTimeUtc">A <see cref="DateTime"/> containing the value to set for the last write date |
| | | 1845 | | public void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc) |
| | 1 | 1846 | | { |
| | 1 | 1847 | | var attributes = GetAttributes(path); |
| | 1 | 1848 | | attributes.LastWriteTimeUtc = lastWriteTimeUtc; |
| | 1 | 1849 | | SetAttributes(path, attributes); |
| | 1 | 1850 | | } |
| | | 1851 | | |
| | | 1852 | | /// <summary> |
| | | 1853 | | /// Writes the specified byte array to the specified file, and closes the file. |
| | | 1854 | | /// </summary> |
| | | 1855 | | /// <param name="path">The file to write to.</param> |
| | | 1856 | | /// <param name="bytes">The bytes to write to the file.</param> |
| | | 1857 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1858 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1859 | | /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on |
| | | 1860 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1861 | | /// <remarks> |
| | | 1862 | | /// <para> |
| | | 1863 | | /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. |
| | | 1864 | | /// </para> |
| | | 1865 | | /// <para> |
| | | 1866 | | /// If the target file does not exist, it is created. |
| | | 1867 | | /// </para> |
| | | 1868 | | /// </remarks> |
| | | 1869 | | public void WriteAllBytes(string path, byte[] bytes) |
| | 8 | 1870 | | { |
| | 8 | 1871 | | using (var stream = OpenWrite(path)) |
| | 7 | 1872 | | { |
| | 7 | 1873 | | stream.Write(bytes, 0, bytes.Length); |
| | 7 | 1874 | | } |
| | 7 | 1875 | | } |
| | | 1876 | | |
| | | 1877 | | /// <summary> |
| | | 1878 | | /// Writes a collection of strings to the file using the UTF-8 encoding, and closes the file. |
| | | 1879 | | /// </summary> |
| | | 1880 | | /// <param name="path">The file to write to.</param> |
| | | 1881 | | /// <param name="contents">The lines to write to the file.</param> |
| | | 1882 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1883 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1884 | | /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on |
| | | 1885 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1886 | | /// <remarks> |
| | | 1887 | | /// <para> |
| | | 1888 | | /// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM). |
| | | 1889 | | /// </para> |
| | | 1890 | | /// <para> |
| | | 1891 | | /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. |
| | | 1892 | | /// </para> |
| | | 1893 | | /// <para> |
| | | 1894 | | /// If the target file does not exist, it is created. |
| | | 1895 | | /// </para> |
| | | 1896 | | /// </remarks> |
| | | 1897 | | public void WriteAllLines(string path, IEnumerable<string> contents) |
| | 4 | 1898 | | { |
| | 4 | 1899 | | WriteAllLines(path, contents, Utf8NoBOM); |
| | 3 | 1900 | | } |
| | | 1901 | | |
| | | 1902 | | /// <summary> |
| | | 1903 | | /// Write the specified string array to the file using the UTF-8 encoding, and closes the file. |
| | | 1904 | | /// </summary> |
| | | 1905 | | /// <param name="path">The file to write to.</param> |
| | | 1906 | | /// <param name="contents">The string array to write to the file.</param> |
| | | 1907 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1908 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1909 | | /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on |
| | | 1910 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1911 | | /// <remarks> |
| | | 1912 | | /// <para> |
| | | 1913 | | /// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM). |
| | | 1914 | | /// </para> |
| | | 1915 | | /// <para> |
| | | 1916 | | /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. |
| | | 1917 | | /// </para> |
| | | 1918 | | /// <para> |
| | | 1919 | | /// If the target file does not exist, it is created. |
| | | 1920 | | /// </para> |
| | | 1921 | | /// </remarks> |
| | | 1922 | | public void WriteAllLines(string path, string[] contents) |
| | 4 | 1923 | | { |
| | 4 | 1924 | | WriteAllLines(path, contents, Utf8NoBOM); |
| | 3 | 1925 | | } |
| | | 1926 | | |
| | | 1927 | | /// <summary> |
| | | 1928 | | /// Writes a collection of strings to the file using the specified encoding, and closes the file. |
| | | 1929 | | /// </summary> |
| | | 1930 | | /// <param name="path">The file to write to.</param> |
| | | 1931 | | /// <param name="contents">The lines to write to the file.</param> |
| | | 1932 | | /// <param name="encoding">The character encoding to use.</param> |
| | | 1933 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1934 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1935 | | /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on |
| | | 1936 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1937 | | /// <remarks> |
| | | 1938 | | /// <para> |
| | | 1939 | | /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. |
| | | 1940 | | /// </para> |
| | | 1941 | | /// <para> |
| | | 1942 | | /// If the target file does not exist, it is created. |
| | | 1943 | | /// </para> |
| | | 1944 | | /// </remarks> |
| | | 1945 | | public void WriteAllLines(string path, IEnumerable<string> contents, Encoding encoding) |
| | 8 | 1946 | | { |
| | 8 | 1947 | | using (var stream = CreateText(path, encoding)) |
| | 6 | 1948 | | { |
| | 62 | 1949 | | foreach (var line in contents) |
| | 22 | 1950 | | { |
| | 22 | 1951 | | stream.WriteLine(line); |
| | 22 | 1952 | | } |
| | 6 | 1953 | | } |
| | 6 | 1954 | | } |
| | | 1955 | | |
| | | 1956 | | /// <summary> |
| | | 1957 | | /// Writes the specified string array to the file by using the specified encoding, and closes the file. |
| | | 1958 | | /// </summary> |
| | | 1959 | | /// <param name="path">The file to write to.</param> |
| | | 1960 | | /// <param name="contents">The string array to write to the file.</param> |
| | | 1961 | | /// <param name="encoding">An <see cref="Encoding"/> object that represents the character encoding applied to th |
| | | 1962 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1963 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1964 | | /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on |
| | | 1965 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1966 | | /// <remarks> |
| | | 1967 | | /// <para> |
| | | 1968 | | /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. |
| | | 1969 | | /// </para> |
| | | 1970 | | /// <para> |
| | | 1971 | | /// If the target file does not exist, it is created. |
| | | 1972 | | /// </para> |
| | | 1973 | | /// </remarks> |
| | | 1974 | | public void WriteAllLines(string path, string[] contents, Encoding encoding) |
| | 8 | 1975 | | { |
| | 8 | 1976 | | using (var stream = CreateText(path, encoding)) |
| | 6 | 1977 | | { |
| | 62 | 1978 | | foreach (var line in contents) |
| | 22 | 1979 | | { |
| | 22 | 1980 | | stream.WriteLine(line); |
| | 22 | 1981 | | } |
| | 6 | 1982 | | } |
| | 6 | 1983 | | } |
| | | 1984 | | |
| | | 1985 | | /// <summary> |
| | | 1986 | | /// Writes the specified string to the file using the UTF-8 encoding, and closes the file. |
| | | 1987 | | /// </summary> |
| | | 1988 | | /// <param name="path">The file to write to.</param> |
| | | 1989 | | /// <param name="contents">The string to write to the file.</param> |
| | | 1990 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 1991 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 1992 | | /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on |
| | | 1993 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 1994 | | /// <remarks> |
| | | 1995 | | /// <para> |
| | | 1996 | | /// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM). |
| | | 1997 | | /// </para> |
| | | 1998 | | /// <para> |
| | | 1999 | | /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. |
| | | 2000 | | /// </para> |
| | | 2001 | | /// <para> |
| | | 2002 | | /// If the target file does not exist, it is created. |
| | | 2003 | | /// </para> |
| | | 2004 | | /// </remarks> |
| | | 2005 | | public void WriteAllText(string path, string contents) |
| | 15 | 2006 | | { |
| | 15 | 2007 | | using (var stream = CreateText(path)) |
| | 14 | 2008 | | { |
| | 14 | 2009 | | stream.Write(contents); |
| | 14 | 2010 | | } |
| | 14 | 2011 | | } |
| | | 2012 | | |
| | | 2013 | | /// <summary> |
| | | 2014 | | /// Writes the specified string to the file using the specified encoding, and closes the file. |
| | | 2015 | | /// </summary> |
| | | 2016 | | /// <param name="path">The file to write to.</param> |
| | | 2017 | | /// <param name="contents">The string to write to the file.</param> |
| | | 2018 | | /// <param name="encoding">The encoding to apply to the string.</param> |
| | | 2019 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 2020 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 2021 | | /// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on |
| | | 2022 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 2023 | | /// <remarks> |
| | | 2024 | | /// <para> |
| | | 2025 | | /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. |
| | | 2026 | | /// </para> |
| | | 2027 | | /// <para> |
| | | 2028 | | /// If the target file does not exist, it is created. |
| | | 2029 | | /// </para> |
| | | 2030 | | /// </remarks> |
| | | 2031 | | public void WriteAllText(string path, string contents, Encoding encoding) |
| | 12 | 2032 | | { |
| | 12 | 2033 | | using (var stream = CreateText(path, encoding)) |
| | 11 | 2034 | | { |
| | 11 | 2035 | | stream.Write(contents); |
| | 11 | 2036 | | } |
| | 11 | 2037 | | } |
| | | 2038 | | |
| | | 2039 | | /// <summary> |
| | | 2040 | | /// Gets the <see cref="SftpFileAttributes"/> of the file on the path. |
| | | 2041 | | /// </summary> |
| | | 2042 | | /// <param name="path">The path to the file.</param> |
| | | 2043 | | /// <returns> |
| | | 2044 | | /// The <see cref="SftpFileAttributes"/> of the file on the path. |
| | | 2045 | | /// </returns> |
| | | 2046 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 2047 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 2048 | | /// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</excep |
| | | 2049 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 2050 | | public SftpFileAttributes GetAttributes(string path) |
| | 15 | 2051 | | { |
| | 15 | 2052 | | CheckDisposed(); |
| | | 2053 | | |
| | 15 | 2054 | | if (_sftpSession is null) |
| | 0 | 2055 | | { |
| | 0 | 2056 | | throw new SshConnectionException("Client not connected."); |
| | | 2057 | | } |
| | | 2058 | | |
| | 15 | 2059 | | var fullPath = _sftpSession.GetCanonicalPath(path); |
| | | 2060 | | |
| | 15 | 2061 | | return _sftpSession.RequestLStat(fullPath); |
| | 15 | 2062 | | } |
| | | 2063 | | |
| | | 2064 | | /// <summary> |
| | | 2065 | | /// Sets the specified <see cref="SftpFileAttributes"/> of the file on the specified path. |
| | | 2066 | | /// </summary> |
| | | 2067 | | /// <param name="path">The path to the file.</param> |
| | | 2068 | | /// <param name="fileAttributes">The desired <see cref="SftpFileAttributes"/>.</param> |
| | | 2069 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 2070 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 2071 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 2072 | | public void SetAttributes(string path, SftpFileAttributes fileAttributes) |
| | 4 | 2073 | | { |
| | 4 | 2074 | | CheckDisposed(); |
| | | 2075 | | |
| | 4 | 2076 | | if (_sftpSession is null) |
| | 0 | 2077 | | { |
| | 0 | 2078 | | throw new SshConnectionException("Client not connected."); |
| | | 2079 | | } |
| | | 2080 | | |
| | 4 | 2081 | | var fullPath = _sftpSession.GetCanonicalPath(path); |
| | | 2082 | | |
| | 4 | 2083 | | _sftpSession.RequestSetStat(fullPath, fileAttributes); |
| | 4 | 2084 | | } |
| | | 2085 | | |
| | | 2086 | | #endregion // File Methods |
| | | 2087 | | |
| | | 2088 | | #region SynchronizeDirectories |
| | | 2089 | | |
| | | 2090 | | /// <summary> |
| | | 2091 | | /// Synchronizes the directories. |
| | | 2092 | | /// </summary> |
| | | 2093 | | /// <param name="sourcePath">The source path.</param> |
| | | 2094 | | /// <param name="destinationPath">The destination path.</param> |
| | | 2095 | | /// <param name="searchPattern">The search pattern.</param> |
| | | 2096 | | /// <returns> |
| | | 2097 | | /// A list of uploaded files. |
| | | 2098 | | /// </returns> |
| | | 2099 | | /// <exception cref="ArgumentNullException"><paramref name="sourcePath"/> is <see langword="null"/>.</exception> |
| | | 2100 | | /// <exception cref="ArgumentException"><paramref name="destinationPath"/> is <see langword="null"/> or contains |
| | | 2101 | | /// <exception cref="SftpPathNotFoundException"><paramref name="destinationPath"/> was not found on the remote h |
| | | 2102 | | /// <exception cref="SshException">If a problem occurs while copying the file.</exception> |
| | | 2103 | | public IEnumerable<FileInfo> SynchronizeDirectories(string sourcePath, string destinationPath, string searchPatt |
| | 1 | 2104 | | { |
| | 1 | 2105 | | if (sourcePath is null) |
| | 0 | 2106 | | { |
| | 0 | 2107 | | throw new ArgumentNullException(nameof(sourcePath)); |
| | | 2108 | | } |
| | | 2109 | | |
| | 1 | 2110 | | if (string.IsNullOrWhiteSpace(destinationPath)) |
| | 0 | 2111 | | { |
| | 0 | 2112 | | throw new ArgumentException("destinationPath"); |
| | | 2113 | | } |
| | | 2114 | | |
| | 1 | 2115 | | return InternalSynchronizeDirectories(sourcePath, destinationPath, searchPattern, asynchResult: null); |
| | 1 | 2116 | | } |
| | | 2117 | | |
| | | 2118 | | /// <summary> |
| | | 2119 | | /// Begins the synchronize directories. |
| | | 2120 | | /// </summary> |
| | | 2121 | | /// <param name="sourcePath">The source path.</param> |
| | | 2122 | | /// <param name="destinationPath">The destination path.</param> |
| | | 2123 | | /// <param name="searchPattern">The search pattern.</param> |
| | | 2124 | | /// <param name="asyncCallback">The async callback.</param> |
| | | 2125 | | /// <param name="state">The state.</param> |
| | | 2126 | | /// <returns> |
| | | 2127 | | /// An <see cref="IAsyncResult" /> that represents the asynchronous directory synchronization. |
| | | 2128 | | /// </returns> |
| | | 2129 | | /// <exception cref="ArgumentNullException"><paramref name="sourcePath"/> is <see langword="null"/>.</exception> |
| | | 2130 | | /// <exception cref="ArgumentException"><paramref name="destinationPath"/> is <see langword="null"/> or contains |
| | | 2131 | | /// <exception cref="SshException">If a problem occurs while copying the file.</exception> |
| | | 2132 | | public IAsyncResult BeginSynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern, |
| | 1 | 2133 | | { |
| | 1 | 2134 | | if (sourcePath is null) |
| | 0 | 2135 | | { |
| | 0 | 2136 | | throw new ArgumentNullException(nameof(sourcePath)); |
| | | 2137 | | } |
| | | 2138 | | |
| | 1 | 2139 | | if (string.IsNullOrWhiteSpace(destinationPath)) |
| | 0 | 2140 | | { |
| | 0 | 2141 | | throw new ArgumentException("destDir"); |
| | | 2142 | | } |
| | | 2143 | | |
| | 1 | 2144 | | var asyncResult = new SftpSynchronizeDirectoriesAsyncResult(asyncCallback, state); |
| | | 2145 | | |
| | 1 | 2146 | | ThreadAbstraction.ExecuteThread(() => |
| | 1 | 2147 | | { |
| | 1 | 2148 | | try |
| | 1 | 2149 | | { |
| | 1 | 2150 | | var result = InternalSynchronizeDirectories(sourcePath, destinationPath, searchPattern, asyncRes |
| | 1 | 2151 | | |
| | 1 | 2152 | | asyncResult.SetAsCompleted(result, completedSynchronously: false); |
| | 1 | 2153 | | } |
| | 0 | 2154 | | catch (Exception exp) |
| | 0 | 2155 | | { |
| | 0 | 2156 | | asyncResult.SetAsCompleted(exp, completedSynchronously: false); |
| | 0 | 2157 | | } |
| | 2 | 2158 | | }); |
| | | 2159 | | |
| | 1 | 2160 | | return asyncResult; |
| | 1 | 2161 | | } |
| | | 2162 | | |
| | | 2163 | | /// <summary> |
| | | 2164 | | /// Ends the synchronize directories. |
| | | 2165 | | /// </summary> |
| | | 2166 | | /// <param name="asyncResult">The async result.</param> |
| | | 2167 | | /// <returns> |
| | | 2168 | | /// A list of uploaded files. |
| | | 2169 | | /// </returns> |
| | | 2170 | | /// <exception cref="ArgumentException">The <see cref="IAsyncResult"/> object did not come from the correspondin |
| | | 2171 | | /// <exception cref="SftpPathNotFoundException">The destination path was not found on the remote host.</exceptio |
| | | 2172 | | public IEnumerable<FileInfo> EndSynchronizeDirectories(IAsyncResult asyncResult) |
| | 1 | 2173 | | { |
| | 1 | 2174 | | if (asyncResult is not SftpSynchronizeDirectoriesAsyncResult ar || ar.EndInvokeCalled) |
| | 0 | 2175 | | { |
| | 0 | 2176 | | throw new ArgumentException("Either the IAsyncResult object did not come from the corresponding async me |
| | | 2177 | | } |
| | | 2178 | | |
| | | 2179 | | // Wait for operation to complete, then return result or throw exception |
| | 1 | 2180 | | return ar.EndInvoke(); |
| | 1 | 2181 | | } |
| | | 2182 | | |
| | | 2183 | | private List<FileInfo> InternalSynchronizeDirectories(string sourcePath, string destinationPath, string searchPa |
| | 2 | 2184 | | { |
| | 2 | 2185 | | if (!Directory.Exists(sourcePath)) |
| | 0 | 2186 | | { |
| | 0 | 2187 | | throw new FileNotFoundException(string.Format("Source directory not found: {0}", sourcePath)); |
| | | 2188 | | } |
| | | 2189 | | |
| | 2 | 2190 | | var uploadedFiles = new List<FileInfo>(); |
| | | 2191 | | |
| | 2 | 2192 | | var sourceDirectory = new DirectoryInfo(sourcePath); |
| | | 2193 | | |
| | 2 | 2194 | | using (var sourceFiles = sourceDirectory.EnumerateFiles(searchPattern).GetEnumerator()) |
| | 2 | 2195 | | { |
| | 2 | 2196 | | if (!sourceFiles.MoveNext()) |
| | 0 | 2197 | | { |
| | 0 | 2198 | | return uploadedFiles; |
| | | 2199 | | } |
| | | 2200 | | |
| | | 2201 | | #region Existing Files at The Destination |
| | | 2202 | | |
| | 2 | 2203 | | var destFiles = InternalListDirectory(destinationPath, listCallback: null); |
| | 2 | 2204 | | var destDict = new Dictionary<string, ISftpFile>(); |
| | 22 | 2205 | | foreach (var destFile in destFiles) |
| | 8 | 2206 | | { |
| | 8 | 2207 | | if (destFile.IsDirectory) |
| | 6 | 2208 | | { |
| | 6 | 2209 | | continue; |
| | | 2210 | | } |
| | | 2211 | | |
| | 2 | 2212 | | destDict.Add(destFile.Name, destFile); |
| | 2 | 2213 | | } |
| | | 2214 | | |
| | | 2215 | | #endregion |
| | | 2216 | | |
| | | 2217 | | #region Upload the difference |
| | | 2218 | | |
| | | 2219 | | const Flags uploadFlag = Flags.Write | Flags.Truncate | Flags.CreateNewOrOpen; |
| | | 2220 | | do |
| | 2 | 2221 | | { |
| | 2 | 2222 | | var localFile = sourceFiles.Current; |
| | 2 | 2223 | | if (localFile is null) |
| | 0 | 2224 | | { |
| | 0 | 2225 | | continue; |
| | | 2226 | | } |
| | | 2227 | | |
| | 2 | 2228 | | var isDifferent = true; |
| | 2 | 2229 | | if (destDict.TryGetValue(localFile.Name, out var remoteFile)) |
| | 0 | 2230 | | { |
| | | 2231 | | // File exists at the destination, use filesize to detect if there's a difference |
| | 0 | 2232 | | isDifferent = localFile.Length != remoteFile.Length; |
| | 0 | 2233 | | } |
| | | 2234 | | |
| | 2 | 2235 | | if (isDifferent) |
| | 2 | 2236 | | { |
| | 2 | 2237 | | var remoteFileName = string.Format(CultureInfo.InvariantCulture, @"{0}/{1}", destinationPath, lo |
| | | 2238 | | try |
| | 2 | 2239 | | { |
| | | 2240 | | #pragma warning disable CA2000 // Dispose objects before losing scope; false positive |
| | 2 | 2241 | | using (var file = File.OpenRead(localFile.FullName)) |
| | | 2242 | | #pragma warning restore CA2000 // Dispose objects before losing scope; false positive |
| | 2 | 2243 | | { |
| | 2 | 2244 | | InternalUploadFile(file, remoteFileName, uploadFlag, asyncResult: null, uploadCallback: |
| | 2 | 2245 | | } |
| | | 2246 | | |
| | 2 | 2247 | | uploadedFiles.Add(localFile); |
| | | 2248 | | |
| | 2 | 2249 | | asynchResult?.Update(uploadedFiles.Count); |
| | 2 | 2250 | | } |
| | 0 | 2251 | | catch (Exception ex) |
| | 0 | 2252 | | { |
| | 0 | 2253 | | throw new SshException($"Failed to upload {localFile.FullName} to {remoteFileName}", ex); |
| | | 2254 | | } |
| | 2 | 2255 | | } |
| | 2 | 2256 | | } |
| | 2 | 2257 | | while (sourceFiles.MoveNext()); |
| | 2 | 2258 | | } |
| | | 2259 | | |
| | | 2260 | | #endregion |
| | | 2261 | | |
| | 2 | 2262 | | return uploadedFiles; |
| | 2 | 2263 | | } |
| | | 2264 | | |
| | | 2265 | | #endregion |
| | | 2266 | | |
| | | 2267 | | /// <summary> |
| | | 2268 | | /// Internals the list directory. |
| | | 2269 | | /// </summary> |
| | | 2270 | | /// <param name="path">The path.</param> |
| | | 2271 | | /// <param name="listCallback">The list callback.</param> |
| | | 2272 | | /// <returns> |
| | | 2273 | | /// A list of files in the specfied directory. |
| | | 2274 | | /// </returns> |
| | | 2275 | | /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception> |
| | | 2276 | | /// <exception cref="SshConnectionException">Client not connected.</exception> |
| | | 2277 | | private List<ISftpFile> InternalListDirectory(string path, Action<int> listCallback) |
| | 23 | 2278 | | { |
| | 23 | 2279 | | if (path is null) |
| | 1 | 2280 | | { |
| | 1 | 2281 | | throw new ArgumentNullException(nameof(path)); |
| | | 2282 | | } |
| | | 2283 | | |
| | 22 | 2284 | | if (_sftpSession is null) |
| | 3 | 2285 | | { |
| | 3 | 2286 | | throw new SshConnectionException("Client not connected."); |
| | | 2287 | | } |
| | | 2288 | | |
| | 19 | 2289 | | var fullPath = _sftpSession.GetCanonicalPath(path); |
| | | 2290 | | |
| | 16 | 2291 | | var handle = _sftpSession.RequestOpenDir(fullPath); |
| | | 2292 | | |
| | 12 | 2293 | | var basePath = fullPath; |
| | | 2294 | | |
| | | 2295 | | #if NET || NETSTANDARD2_1_OR_GREATER |
| | 12 | 2296 | | if (!basePath.EndsWith('/')) |
| | | 2297 | | #else |
| | 0 | 2298 | | if (!basePath.EndsWith("/", StringComparison.Ordinal)) |
| | | 2299 | | #endif // NET || NETSTANDARD2_1_OR_GREATER |
| | 10 | 2300 | | { |
| | 10 | 2301 | | basePath = string.Format("{0}/", fullPath); |
| | 10 | 2302 | | } |
| | | 2303 | | |
| | 12 | 2304 | | var result = new List<ISftpFile>(); |
| | | 2305 | | |
| | 12 | 2306 | | var files = _sftpSession.RequestReadDir(handle); |
| | | 2307 | | |
| | 124 | 2308 | | while (files is not null) |
| | 112 | 2309 | | { |
| | 20694 | 2310 | | foreach (var f in files) |
| | 10179 | 2311 | | { |
| | 10179 | 2312 | | result.Add(new SftpFile(_sftpSession, |
| | 10179 | 2313 | | string.Format(CultureInfo.InvariantCulture, "{0}{1}", basePath, f.Key), |
| | 10179 | 2314 | | f.Value)); |
| | 10179 | 2315 | | } |
| | | 2316 | | |
| | | 2317 | | // Call callback to report number of files read |
| | 112 | 2318 | | if (listCallback is not null) |
| | 3 | 2319 | | { |
| | | 2320 | | // Execute callback on different thread |
| | 6 | 2321 | | ThreadAbstraction.ExecuteThread(() => listCallback(result.Count)); |
| | 3 | 2322 | | } |
| | | 2323 | | |
| | 112 | 2324 | | files = _sftpSession.RequestReadDir(handle); |
| | 112 | 2325 | | } |
| | | 2326 | | |
| | 12 | 2327 | | _sftpSession.RequestClose(handle); |
| | | 2328 | | |
| | 12 | 2329 | | return result; |
| | 12 | 2330 | | } |
| | | 2331 | | |
| | | 2332 | | /// <summary> |
| | | 2333 | | /// Internals the download file. |
| | | 2334 | | /// </summary> |
| | | 2335 | | /// <param name="path">The path.</param> |
| | | 2336 | | /// <param name="output">The output.</param> |
| | | 2337 | | /// <param name="asyncResult">An <see cref="IAsyncResult"/> that references the asynchronous request.</param> |
| | | 2338 | | /// <param name="downloadCallback">The download callback.</param> |
| | | 2339 | | /// <exception cref="ArgumentNullException"><paramref name="output" /> is <see langword="null"/>.</exception> |
| | | 2340 | | /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains whitespac |
| | | 2341 | | /// <exception cref="SshConnectionException">Client not connected.</exception> |
| | | 2342 | | private void InternalDownloadFile(string path, Stream output, SftpDownloadAsyncResult asyncResult, Action<ulong> |
| | 51 | 2343 | | { |
| | 51 | 2344 | | if (output is null) |
| | 0 | 2345 | | { |
| | 0 | 2346 | | throw new ArgumentNullException(nameof(output)); |
| | | 2347 | | } |
| | | 2348 | | |
| | 51 | 2349 | | if (string.IsNullOrWhiteSpace(path)) |
| | 0 | 2350 | | { |
| | 0 | 2351 | | throw new ArgumentException("path"); |
| | | 2352 | | } |
| | | 2353 | | |
| | 51 | 2354 | | if (_sftpSession is null) |
| | 1 | 2355 | | { |
| | 1 | 2356 | | throw new SshConnectionException("Client not connected."); |
| | | 2357 | | } |
| | | 2358 | | |
| | 50 | 2359 | | var fullPath = _sftpSession.GetCanonicalPath(path); |
| | | 2360 | | |
| | 50 | 2361 | | using (var fileReader = ServiceFactory.CreateSftpFileReader(fullPath, _sftpSession, _bufferSize)) |
| | 46 | 2362 | | { |
| | 46 | 2363 | | var totalBytesRead = 0UL; |
| | | 2364 | | |
| | 3693 | 2365 | | while (true) |
| | 3693 | 2366 | | { |
| | | 2367 | | // Cancel download |
| | 3693 | 2368 | | if (asyncResult is not null && asyncResult.IsDownloadCanceled) |
| | 0 | 2369 | | { |
| | 0 | 2370 | | break; |
| | | 2371 | | } |
| | | 2372 | | |
| | 3693 | 2373 | | var data = fileReader.Read(); |
| | 3693 | 2374 | | if (data.Length == 0) |
| | 46 | 2375 | | { |
| | 46 | 2376 | | break; |
| | | 2377 | | } |
| | | 2378 | | |
| | 3647 | 2379 | | output.Write(data, 0, data.Length); |
| | | 2380 | | |
| | 3647 | 2381 | | totalBytesRead += (ulong) data.Length; |
| | | 2382 | | |
| | 3647 | 2383 | | if (downloadCallback is not null) |
| | 1643 | 2384 | | { |
| | | 2385 | | // Copy offset to ensure it's not modified between now and execution of callback |
| | 1643 | 2386 | | var downloadOffset = totalBytesRead; |
| | | 2387 | | |
| | | 2388 | | // Execute callback on different thread |
| | 6572 | 2389 | | ThreadAbstraction.ExecuteThread(() => { downloadCallback(downloadOffset); }); |
| | 1643 | 2390 | | } |
| | 3647 | 2391 | | } |
| | 46 | 2392 | | } |
| | 46 | 2393 | | } |
| | | 2394 | | |
| | | 2395 | | /// <summary> |
| | | 2396 | | /// Internals the upload file. |
| | | 2397 | | /// </summary> |
| | | 2398 | | /// <param name="input">The input.</param> |
| | | 2399 | | /// <param name="path">The path.</param> |
| | | 2400 | | /// <param name="flags">The flags.</param> |
| | | 2401 | | /// <param name="asyncResult">An <see cref="IAsyncResult"/> that references the asynchronous request.</param> |
| | | 2402 | | /// <param name="uploadCallback">The upload callback.</param> |
| | | 2403 | | /// <exception cref="ArgumentNullException"><paramref name="input" /> is <see langword="null"/>.</exception> |
| | | 2404 | | /// <exception cref="ArgumentException"><paramref name="path" /> is <see langword="null"/> or contains whitespac |
| | | 2405 | | /// <exception cref="SshConnectionException">Client not connected.</exception> |
| | | 2406 | | private void InternalUploadFile(Stream input, string path, Flags flags, SftpUploadAsyncResult asyncResult, Actio |
| | 58 | 2407 | | { |
| | 58 | 2408 | | if (input is null) |
| | 0 | 2409 | | { |
| | 0 | 2410 | | throw new ArgumentNullException(nameof(input)); |
| | | 2411 | | } |
| | | 2412 | | |
| | 58 | 2413 | | if (string.IsNullOrWhiteSpace(path)) |
| | 0 | 2414 | | { |
| | 0 | 2415 | | throw new ArgumentException("path"); |
| | | 2416 | | } |
| | | 2417 | | |
| | 58 | 2418 | | if (_sftpSession is null) |
| | 1 | 2419 | | { |
| | 1 | 2420 | | throw new SshConnectionException("Client not connected."); |
| | | 2421 | | } |
| | | 2422 | | |
| | 57 | 2423 | | var fullPath = _sftpSession.GetCanonicalPath(path); |
| | | 2424 | | |
| | 57 | 2425 | | var handle = _sftpSession.RequestOpen(fullPath, flags); |
| | | 2426 | | |
| | 52 | 2427 | | ulong offset = 0; |
| | | 2428 | | |
| | | 2429 | | // create buffer of optimal length |
| | 52 | 2430 | | var buffer = new byte[_sftpSession.CalculateOptimalWriteLength(_bufferSize, handle)]; |
| | | 2431 | | |
| | 52 | 2432 | | var bytesRead = input.Read(buffer, 0, buffer.Length); |
| | 52 | 2433 | | var expectedResponses = 0; |
| | 52 | 2434 | | var responseReceivedWaitHandle = new AutoResetEvent(initialState: false); |
| | | 2435 | | |
| | | 2436 | | do |
| | 3823 | 2437 | | { |
| | | 2438 | | // Cancel upload |
| | 3823 | 2439 | | if (asyncResult is not null && asyncResult.IsUploadCanceled) |
| | 0 | 2440 | | { |
| | 0 | 2441 | | break; |
| | | 2442 | | } |
| | | 2443 | | |
| | 3823 | 2444 | | if (bytesRead > 0) |
| | 3741 | 2445 | | { |
| | 3741 | 2446 | | var writtenBytes = offset + (ulong) bytesRead; |
| | | 2447 | | |
| | 3741 | 2448 | | _sftpSession.RequestWrite(handle, offset, buffer, offset: 0, bytesRead, wait: null, s => |
| | 3741 | 2449 | | { |
| | 3741 | 2450 | | if (s.StatusCode == StatusCodes.Ok) |
| | 3741 | 2451 | | { |
| | 3741 | 2452 | | _ = Interlocked.Decrement(ref expectedResponses); |
| | 3741 | 2453 | | _ = responseReceivedWaitHandle.Set(); |
| | 3741 | 2454 | | |
| | 3741 | 2455 | | // Call callback to report number of bytes written |
| | 3741 | 2456 | | if (uploadCallback is not null) |
| | 1649 | 2457 | | { |
| | 3741 | 2458 | | // Execute callback on different thread |
| | 3298 | 2459 | | ThreadAbstraction.ExecuteThread(() => uploadCallback(writtenBytes)); |
| | 1649 | 2460 | | } |
| | 3741 | 2461 | | } |
| | 7482 | 2462 | | }); |
| | | 2463 | | |
| | 3741 | 2464 | | _ = Interlocked.Increment(ref expectedResponses); |
| | | 2465 | | |
| | 3741 | 2466 | | offset += (ulong) bytesRead; |
| | | 2467 | | |
| | 3741 | 2468 | | bytesRead = input.Read(buffer, 0, buffer.Length); |
| | 3741 | 2469 | | } |
| | 82 | 2470 | | else if (expectedResponses > 0) |
| | 75 | 2471 | | { |
| | | 2472 | | // Wait for expectedResponses to change |
| | 75 | 2473 | | _sftpSession.WaitOnHandle(responseReceivedWaitHandle, _operationTimeout); |
| | 75 | 2474 | | } |
| | 3823 | 2475 | | } |
| | 3823 | 2476 | | while (expectedResponses > 0 || bytesRead > 0); |
| | | 2477 | | |
| | 52 | 2478 | | _sftpSession.RequestClose(handle); |
| | 52 | 2479 | | responseReceivedWaitHandle.Dispose(); |
| | 52 | 2480 | | } |
| | | 2481 | | |
| | | 2482 | | /// <summary> |
| | | 2483 | | /// Called when client is connected to the server. |
| | | 2484 | | /// </summary> |
| | | 2485 | | protected override void OnConnected() |
| | 723 | 2486 | | { |
| | 723 | 2487 | | base.OnConnected(); |
| | | 2488 | | |
| | 723 | 2489 | | _sftpSession = CreateAndConnectToSftpSession(); |
| | 707 | 2490 | | } |
| | | 2491 | | |
| | | 2492 | | /// <summary> |
| | | 2493 | | /// Called when client is disconnecting from the server. |
| | | 2494 | | /// </summary> |
| | | 2495 | | protected override void OnDisconnecting() |
| | 787 | 2496 | | { |
| | 787 | 2497 | | base.OnDisconnecting(); |
| | | 2498 | | |
| | | 2499 | | // disconnect, dispose and dereference the SFTP session since we create a new SFTP session |
| | | 2500 | | // on each connect |
| | 787 | 2501 | | var sftpSession = _sftpSession; |
| | 787 | 2502 | | if (sftpSession is not null) |
| | 698 | 2503 | | { |
| | 698 | 2504 | | _sftpSession = null; |
| | 698 | 2505 | | sftpSession.Dispose(); |
| | 698 | 2506 | | } |
| | 787 | 2507 | | } |
| | | 2508 | | |
| | | 2509 | | /// <summary> |
| | | 2510 | | /// Releases unmanaged and - optionally - managed resources. |
| | | 2511 | | /// </summary> |
| | | 2512 | | /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langwor |
| | | 2513 | | protected override void Dispose(bool disposing) |
| | 521 | 2514 | | { |
| | 521 | 2515 | | base.Dispose(disposing); |
| | | 2516 | | |
| | 521 | 2517 | | if (disposing) |
| | 467 | 2518 | | { |
| | 467 | 2519 | | var sftpSession = _sftpSession; |
| | 467 | 2520 | | if (sftpSession is not null) |
| | 0 | 2521 | | { |
| | 0 | 2522 | | _sftpSession = null; |
| | 0 | 2523 | | sftpSession.Dispose(); |
| | 0 | 2524 | | } |
| | 467 | 2525 | | } |
| | 521 | 2526 | | } |
| | | 2527 | | |
| | | 2528 | | private ISftpSession CreateAndConnectToSftpSession() |
| | 723 | 2529 | | { |
| | 723 | 2530 | | var sftpSession = ServiceFactory.CreateSftpSession(Session, |
| | 723 | 2531 | | _operationTimeout, |
| | 723 | 2532 | | ConnectionInfo.Encoding, |
| | 723 | 2533 | | ServiceFactory.CreateSftpResponseFactory()); |
| | | 2534 | | try |
| | 723 | 2535 | | { |
| | 723 | 2536 | | sftpSession.Connect(); |
| | 707 | 2537 | | return sftpSession; |
| | | 2538 | | } |
| | 16 | 2539 | | catch |
| | 16 | 2540 | | { |
| | 16 | 2541 | | sftpSession.Dispose(); |
| | 16 | 2542 | | throw; |
| | | 2543 | | } |
| | 707 | 2544 | | } |
| | | 2545 | | } |
| | | 2546 | | } |