1
- using System ;
1
+ #nullable enable
2
+ using System ;
2
3
using System . Collections . Generic ;
4
+ using System . Diagnostics ;
3
5
using System . Diagnostics . CodeAnalysis ;
4
6
using System . Globalization ;
5
7
using System . IO ;
@@ -127,12 +129,12 @@ public IRemotePathTransformation RemotePathTransformation
127
129
/// <summary>
128
130
/// Occurs when downloading file.
129
131
/// </summary>
130
- public event EventHandler < ScpDownloadEventArgs > Downloading ;
132
+ public event EventHandler < ScpDownloadEventArgs > ? Downloading ;
131
133
132
134
/// <summary>
133
135
/// Occurs when uploading file.
134
136
/// </summary>
135
- public event EventHandler < ScpUploadEventArgs > Uploading ;
137
+ public event EventHandler < ScpUploadEventArgs > ? Uploading ;
136
138
137
139
/// <summary>
138
140
/// Initializes a new instance of the <see cref="ScpClient"/> class.
@@ -246,8 +248,14 @@ internal ScpClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServ
246
248
/// <exception cref="ArgumentException"><paramref name="path"/> is a zero-length <see cref="string"/>.</exception>
247
249
/// <exception cref="ScpException">A directory with the specified path exists on the remote host.</exception>
248
250
/// <exception cref="SshException">The secure copy execution request was rejected by the server.</exception>
251
+ /// <exception cref="SshConnectionException">Client is not connected.</exception>
249
252
public void Upload ( Stream source , string path )
250
253
{
254
+ if ( Session is null )
255
+ {
256
+ throw new SshConnectionException ( "Client not connected." ) ;
257
+ }
258
+
251
259
var posixPath = PosixPath . CreateAbsoluteOrRelativeFilePath ( path ) ;
252
260
253
261
using ( var input = ServiceFactory . CreatePipeStream ( ) )
@@ -280,13 +288,19 @@ public void Upload(Stream source, string path)
280
288
/// <exception cref="ArgumentException"><paramref name="path"/> is a zero-length <see cref="string"/>.</exception>
281
289
/// <exception cref="ScpException">A directory with the specified path exists on the remote host.</exception>
282
290
/// <exception cref="SshException">The secure copy execution request was rejected by the server.</exception>
291
+ /// <exception cref="SshConnectionException">Client is not connected.</exception>
283
292
public void Upload ( FileInfo fileInfo , string path )
284
293
{
285
294
if ( fileInfo is null )
286
295
{
287
296
throw new ArgumentNullException ( nameof ( fileInfo ) ) ;
288
297
}
289
298
299
+ if ( Session is null )
300
+ {
301
+ throw new SshConnectionException ( "Client not connected." ) ;
302
+ }
303
+
290
304
var posixPath = PosixPath . CreateAbsoluteOrRelativeFilePath ( path ) ;
291
305
292
306
using ( var input = ServiceFactory . CreatePipeStream ( ) )
@@ -323,6 +337,7 @@ public void Upload(FileInfo fileInfo, string path)
323
337
/// <exception cref="ArgumentException"><paramref name="path"/> is a zero-length string.</exception>
324
338
/// <exception cref="ScpException"><paramref name="path"/> does not exist on the remote host, is not a directory or the user does not have the required permission.</exception>
325
339
/// <exception cref="SshException">The secure copy execution request was rejected by the server.</exception>
340
+ /// <exception cref="SshConnectionException">Client is not connected.</exception>
326
341
public void Upload ( DirectoryInfo directoryInfo , string path )
327
342
{
328
343
if ( directoryInfo is null )
@@ -340,6 +355,11 @@ public void Upload(DirectoryInfo directoryInfo, string path)
340
355
throw new ArgumentException ( "The path cannot be a zero-length string." , nameof ( path ) ) ;
341
356
}
342
357
358
+ if ( Session is null )
359
+ {
360
+ throw new SshConnectionException ( "Client not connected." ) ;
361
+ }
362
+
343
363
using ( var input = ServiceFactory . CreatePipeStream ( ) )
344
364
using ( var channel = Session . CreateChannelSession ( ) )
345
365
{
@@ -371,6 +391,7 @@ public void Upload(DirectoryInfo directoryInfo, string path)
371
391
/// <exception cref="ArgumentException"><paramref name="filename"/> is <see langword="null"/> or empty.</exception>
372
392
/// <exception cref="ScpException"><paramref name="filename"/> exists on the remote host, and is not a regular file.</exception>
373
393
/// <exception cref="SshException">The secure copy execution request was rejected by the server.</exception>
394
+ /// <exception cref="SshConnectionException">Client is not connected.</exception>
374
395
public void Download ( string filename , FileInfo fileInfo )
375
396
{
376
397
if ( string . IsNullOrEmpty ( filename ) )
@@ -383,6 +404,11 @@ public void Download(string filename, FileInfo fileInfo)
383
404
throw new ArgumentNullException ( nameof ( fileInfo ) ) ;
384
405
}
385
406
407
+ if ( Session is null )
408
+ {
409
+ throw new SshConnectionException ( "Client not connected." ) ;
410
+ }
411
+
386
412
using ( var input = ServiceFactory . CreatePipeStream ( ) )
387
413
using ( var channel = Session . CreateChannelSession ( ) )
388
414
{
@@ -411,6 +437,7 @@ public void Download(string filename, FileInfo fileInfo)
411
437
/// <exception cref="ArgumentNullException"><paramref name="directoryInfo"/> is <see langword="null"/>.</exception>
412
438
/// <exception cref="ScpException">File or directory with the specified path does not exist on the remote host.</exception>
413
439
/// <exception cref="SshException">The secure copy execution request was rejected by the server.</exception>
440
+ /// <exception cref="SshConnectionException">Client is not connected.</exception>
414
441
public void Download ( string directoryName , DirectoryInfo directoryInfo )
415
442
{
416
443
if ( string . IsNullOrEmpty ( directoryName ) )
@@ -423,6 +450,11 @@ public void Download(string directoryName, DirectoryInfo directoryInfo)
423
450
throw new ArgumentNullException ( nameof ( directoryInfo ) ) ;
424
451
}
425
452
453
+ if ( Session is null )
454
+ {
455
+ throw new SshConnectionException ( "Client not connected." ) ;
456
+ }
457
+
426
458
using ( var input = ServiceFactory . CreatePipeStream ( ) )
427
459
using ( var channel = Session . CreateChannelSession ( ) )
428
460
{
@@ -451,6 +483,7 @@ public void Download(string directoryName, DirectoryInfo directoryInfo)
451
483
/// <exception cref="ArgumentNullException"><paramref name="destination"/> is <see langword="null"/>.</exception>
452
484
/// <exception cref="ScpException"><paramref name="filename"/> exists on the remote host, and is not a regular file.</exception>
453
485
/// <exception cref="SshException">The secure copy execution request was rejected by the server.</exception>
486
+ /// <exception cref="SshConnectionException">Client is not connected.</exception>
454
487
public void Download ( string filename , Stream destination )
455
488
{
456
489
if ( string . IsNullOrWhiteSpace ( filename ) )
@@ -463,6 +496,11 @@ public void Download(string filename, Stream destination)
463
496
throw new ArgumentNullException ( nameof ( destination ) ) ;
464
497
}
465
498
499
+ if ( Session is null )
500
+ {
501
+ throw new SshConnectionException ( "Client not connected." ) ;
502
+ }
503
+
466
504
using ( var input = ServiceFactory . CreatePipeStream ( ) )
467
505
using ( var channel = Session . CreateChannelSession ( ) )
468
506
{
@@ -767,13 +805,17 @@ private void InternalDownload(IChannelSession channel, Stream input, FileSystemI
767
805
768
806
directoryCounter -- ;
769
807
770
- currentDirectoryFullName = new DirectoryInfo ( currentDirectoryFullName ) . Parent . FullName ;
771
-
772
808
if ( directoryCounter == 0 )
773
809
{
774
810
break ;
775
811
}
776
812
813
+ var currentDirectoryParent = new DirectoryInfo ( currentDirectoryFullName ) . Parent ;
814
+
815
+ Debug . Assert ( currentDirectoryParent is not null , $ "Should be { directoryCounter . ToString ( CultureInfo . InvariantCulture ) } levels deeper than { startDirectoryFullName } .") ;
816
+
817
+ currentDirectoryFullName = currentDirectoryParent . FullName ;
818
+
777
819
continue ;
778
820
}
779
821
@@ -795,7 +837,7 @@ private void InternalDownload(IChannelSession channel, Stream input, FileSystemI
795
837
else
796
838
{
797
839
// Don't create directory for first level
798
- newDirectoryInfo = fileSystemInfo as DirectoryInfo ;
840
+ newDirectoryInfo = ( DirectoryInfo ) fileSystemInfo ;
799
841
}
800
842
801
843
directoryCounter ++ ;
0 commit comments