From 659cd54837e663421bcb174b87810257612590bb Mon Sep 17 00:00:00 2001 From: Robert Hague Date: Mon, 3 Nov 2025 20:19:07 +0100 Subject: [PATCH] Truncate local file in ScpClient.Download Similar to #1686 but for the local side of SCP: opening the file should use Create not OpenWrite. closes #648 --- src/Renci.SshNet/ScpClient.cs | 2 +- test/Renci.SshNet.IntegrationTests/ScpTests.cs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Renci.SshNet/ScpClient.cs b/src/Renci.SshNet/ScpClient.cs index 18fc97591..2f0fd67f7 100644 --- a/src/Renci.SshNet/ScpClient.cs +++ b/src/Renci.SshNet/ScpClient.cs @@ -831,7 +831,7 @@ private void InternalDownload(IChannelSession channel, Stream input, FileSystemI fileInfo = new FileInfo(Path.Combine(currentDirectoryFullName, fileName)); } - using (var output = fileInfo.OpenWrite()) + using (var output = fileInfo.Open(FileMode.Create, FileAccess.Write)) { InternalDownload(channel, input, output, fileName, length); } diff --git a/test/Renci.SshNet.IntegrationTests/ScpTests.cs b/test/Renci.SshNet.IntegrationTests/ScpTests.cs index 8433bf704..fa432be82 100644 --- a/test/Renci.SshNet.IntegrationTests/ScpTests.cs +++ b/test/Renci.SshNet.IntegrationTests/ScpTests.cs @@ -679,7 +679,8 @@ public void Scp_Download_FileInfo_ExistingFile(IRemotePathTransformation remoteP } } - var fileInfo = new FileInfo(Path.GetTempFileName()); + // Create a local file larger than the remote file in order to test truncation. + var fileInfo = new FileInfo(CreateTempFile(size + 64)); try {