From 3db899a3a53c59f9e4cc30c1ae63a6f188afda57 Mon Sep 17 00:00:00 2001 From: thegame4craft Date: Wed, 18 Jun 2025 23:20:08 +0200 Subject: [PATCH 1/2] scp: add flag to notify for uploading finished when uploaded an empty file --- src/Renci.SshNet/ScpClient.cs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Renci.SshNet/ScpClient.cs b/src/Renci.SshNet/ScpClient.cs index 56330f2ef..5150288a6 100644 --- a/src/Renci.SshNet/ScpClient.cs +++ b/src/Renci.SshNet/ScpClient.cs @@ -240,12 +240,13 @@ internal ScpClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServ /// /// The to upload. /// A relative or absolute path for the remote file. + /// Should the event be raised when the file is empty? /// is . /// is a zero-length . /// A directory with the specified path exists on the remote host. /// The secure copy execution request was rejected by the server. /// Client is not connected. - public void Upload(Stream source, string path) + public void Upload(Stream source, string path, bool notifyOnEmptyFile=false) { if (Session is null) { @@ -271,7 +272,7 @@ public void Upload(Stream source, string path) CheckReturnCode(input); UploadFileModeAndName(channel, input, source.Length, posixPath.File); - UploadFileContent(channel, input, source, posixPath.File); + UploadFileContent(channel, input, source, posixPath.File, notifyOnEmptyFile); } } @@ -280,13 +281,14 @@ public void Upload(Stream source, string path) /// /// The file system info. /// A relative or absolute path for the remote file. + /// Should the event be raised when the file is empty? /// is . /// is . /// is a zero-length . /// A directory with the specified path exists on the remote host. /// The secure copy execution request was rejected by the server. /// Client is not connected. - public void Upload(FileInfo fileInfo, string path) + public void Upload(FileInfo fileInfo, string path, bool notifyOnEmptyFile=false) { ThrowHelper.ThrowIfNull(fileInfo); @@ -317,7 +319,7 @@ public void Upload(FileInfo fileInfo, string path) { UploadTimes(channel, input, fileInfo); UploadFileModeAndName(channel, input, source.Length, posixPath.File); - UploadFileContent(channel, input, source, fileInfo.Name); + UploadFileContent(channel, input, source, fileInfo.Name, notifyOnEmptyFile); } } } @@ -327,13 +329,14 @@ public void Upload(FileInfo fileInfo, string path) /// /// The directory info. /// A relative or absolute path for the remote directory. + /// Should the event be raised when the file is empty? /// is . /// is . /// is a zero-length string. /// does not exist on the remote host, is not a directory or the user does not have the required permission. /// The secure copy execution request was rejected by the server. /// Client is not connected. - public void Upload(DirectoryInfo directoryInfo, string path) + public void Upload(DirectoryInfo directoryInfo, string path, bool notifyOnEmptyFile=false) { ThrowHelper.ThrowIfNull(directoryInfo); ThrowHelper.ThrowIfNullOrEmpty(path); @@ -362,7 +365,7 @@ public void Upload(DirectoryInfo directoryInfo, string path) CheckReturnCode(input); - UploadDirectoryContent(channel, input, directoryInfo); + UploadDirectoryContent(channel, input, directoryInfo, notifyOnEmptyFile); } } @@ -556,10 +559,11 @@ private void UploadFileModeAndName(IChannelSession channel, Stream input, long f /// A from which any feedback from the server can be read. /// The content to upload. /// The name of the remote file, without path, to which the content is uploaded. + /// Should the event be raised when the file is empty? /// /// is only used for raising the event. /// - private void UploadFileContent(IChannelSession channel, Stream input, Stream source, string remoteFileName) + private void UploadFileContent(IChannelSession channel, Stream input, Stream source, string remoteFileName, bool notifyOnEmptyFile) { var totalLength = source.Length; var buffer = new byte[BufferSize]; @@ -579,6 +583,11 @@ private void UploadFileContent(IChannelSession channel, Stream input, Stream sou read = source.Read(buffer, 0, buffer.Length); } + if (totalLength == 0 && totalRead == 0 && notifyOnEmptyFile) + { + RaiseUploadingEvent(remoteFileName, totalLength, totalRead); + } + SendSuccessConfirmation(channel); CheckReturnCode(input); } @@ -687,7 +696,8 @@ private void UploadTimes(IChannelSession channel, Stream input, FileSystemInfo f /// The channel to perform the upload in. /// A from which any feedback from the server can be read. /// The directory to upload. - private void UploadDirectoryContent(IChannelSession channel, Stream input, DirectoryInfo directoryInfo) + /// Should the event be raised when the file is empty? + private void UploadDirectoryContent(IChannelSession channel, Stream input, DirectoryInfo directoryInfo, bool notifyOnEmptyFile=false) { // Upload files var files = directoryInfo.GetFiles(); @@ -697,7 +707,7 @@ private void UploadDirectoryContent(IChannelSession channel, Stream input, Direc { UploadTimes(channel, input, file); UploadFileModeAndName(channel, input, source.Length, file.Name); - UploadFileContent(channel, input, source, file.Name); + UploadFileContent(channel, input, source, file.Name, notifyOnEmptyFile); } } @@ -707,7 +717,7 @@ private void UploadDirectoryContent(IChannelSession channel, Stream input, Direc { UploadTimes(channel, input, directory); UploadDirectoryModeAndName(channel, input, directory.Name); - UploadDirectoryContent(channel, input, directory); + UploadDirectoryContent(channel, input, directory, notifyOnEmptyFile); } // Mark upload of current directory complete From 153419793472f2d6d45d329035d1464bef2bbf88 Mon Sep 17 00:00:00 2001 From: thegame4craft Date: Thu, 19 Jun 2025 16:35:55 +0200 Subject: [PATCH 2/2] remove notifyOnEmptyFile Flag --- src/Renci.SshNet/ScpClient.cs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/Renci.SshNet/ScpClient.cs b/src/Renci.SshNet/ScpClient.cs index 5150288a6..18fc97591 100644 --- a/src/Renci.SshNet/ScpClient.cs +++ b/src/Renci.SshNet/ScpClient.cs @@ -240,13 +240,12 @@ internal ScpClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServ /// /// The to upload. /// A relative or absolute path for the remote file. - /// Should the event be raised when the file is empty? /// is . /// is a zero-length . /// A directory with the specified path exists on the remote host. /// The secure copy execution request was rejected by the server. /// Client is not connected. - public void Upload(Stream source, string path, bool notifyOnEmptyFile=false) + public void Upload(Stream source, string path) { if (Session is null) { @@ -272,7 +271,7 @@ public void Upload(Stream source, string path, bool notifyOnEmptyFile=false) CheckReturnCode(input); UploadFileModeAndName(channel, input, source.Length, posixPath.File); - UploadFileContent(channel, input, source, posixPath.File, notifyOnEmptyFile); + UploadFileContent(channel, input, source, posixPath.File); } } @@ -281,14 +280,13 @@ public void Upload(Stream source, string path, bool notifyOnEmptyFile=false) /// /// The file system info. /// A relative or absolute path for the remote file. - /// Should the event be raised when the file is empty? /// is . /// is . /// is a zero-length . /// A directory with the specified path exists on the remote host. /// The secure copy execution request was rejected by the server. /// Client is not connected. - public void Upload(FileInfo fileInfo, string path, bool notifyOnEmptyFile=false) + public void Upload(FileInfo fileInfo, string path) { ThrowHelper.ThrowIfNull(fileInfo); @@ -319,7 +317,7 @@ public void Upload(FileInfo fileInfo, string path, bool notifyOnEmptyFile=false) { UploadTimes(channel, input, fileInfo); UploadFileModeAndName(channel, input, source.Length, posixPath.File); - UploadFileContent(channel, input, source, fileInfo.Name, notifyOnEmptyFile); + UploadFileContent(channel, input, source, fileInfo.Name); } } } @@ -329,14 +327,13 @@ public void Upload(FileInfo fileInfo, string path, bool notifyOnEmptyFile=false) /// /// The directory info. /// A relative or absolute path for the remote directory. - /// Should the event be raised when the file is empty? /// is . /// is . /// is a zero-length string. /// does not exist on the remote host, is not a directory or the user does not have the required permission. /// The secure copy execution request was rejected by the server. /// Client is not connected. - public void Upload(DirectoryInfo directoryInfo, string path, bool notifyOnEmptyFile=false) + public void Upload(DirectoryInfo directoryInfo, string path) { ThrowHelper.ThrowIfNull(directoryInfo); ThrowHelper.ThrowIfNullOrEmpty(path); @@ -365,7 +362,7 @@ public void Upload(DirectoryInfo directoryInfo, string path, bool notifyOnEmptyF CheckReturnCode(input); - UploadDirectoryContent(channel, input, directoryInfo, notifyOnEmptyFile); + UploadDirectoryContent(channel, input, directoryInfo); } } @@ -559,11 +556,10 @@ private void UploadFileModeAndName(IChannelSession channel, Stream input, long f /// A from which any feedback from the server can be read. /// The content to upload. /// The name of the remote file, without path, to which the content is uploaded. - /// Should the event be raised when the file is empty? /// /// is only used for raising the event. /// - private void UploadFileContent(IChannelSession channel, Stream input, Stream source, string remoteFileName, bool notifyOnEmptyFile) + private void UploadFileContent(IChannelSession channel, Stream input, Stream source, string remoteFileName) { var totalLength = source.Length; var buffer = new byte[BufferSize]; @@ -583,7 +579,7 @@ private void UploadFileContent(IChannelSession channel, Stream input, Stream sou read = source.Read(buffer, 0, buffer.Length); } - if (totalLength == 0 && totalRead == 0 && notifyOnEmptyFile) + if (totalLength == 0 && totalRead == 0) { RaiseUploadingEvent(remoteFileName, totalLength, totalRead); } @@ -696,8 +692,7 @@ private void UploadTimes(IChannelSession channel, Stream input, FileSystemInfo f /// The channel to perform the upload in. /// A from which any feedback from the server can be read. /// The directory to upload. - /// Should the event be raised when the file is empty? - private void UploadDirectoryContent(IChannelSession channel, Stream input, DirectoryInfo directoryInfo, bool notifyOnEmptyFile=false) + private void UploadDirectoryContent(IChannelSession channel, Stream input, DirectoryInfo directoryInfo) { // Upload files var files = directoryInfo.GetFiles(); @@ -707,7 +702,7 @@ private void UploadDirectoryContent(IChannelSession channel, Stream input, Direc { UploadTimes(channel, input, file); UploadFileModeAndName(channel, input, source.Length, file.Name); - UploadFileContent(channel, input, source, file.Name, notifyOnEmptyFile); + UploadFileContent(channel, input, source, file.Name); } } @@ -717,7 +712,7 @@ private void UploadDirectoryContent(IChannelSession channel, Stream input, Direc { UploadTimes(channel, input, directory); UploadDirectoryModeAndName(channel, input, directory.Name); - UploadDirectoryContent(channel, input, directory, notifyOnEmptyFile); + UploadDirectoryContent(channel, input, directory); } // Mark upload of current directory complete