Posh-SSH icon indicating copy to clipboard operation
Posh-SSH copied to clipboard

Get-SCPItem overwrites bytes in local file instead of replacing the entire file

Open soul4soul opened this issue 5 years ago • 2 comments

When I use Get-SCPItem and overwrite a local file I expect the local file to be completely overwritten. What I'm finding is that the bytes are written to the existing file starting at offset 0. This can be reproduce by running Get-SCPItem to retrieve a file which is smaller in size then the local file it will overwrite.

Ubuntu-18.04-x64 can be used for the remote and host machine to reproduce. I have seen this issue using different remote machines. Edit: I can confirm this happens using Windows 10 as the host machine too.

  1. On remote machine make a file echo "abcdef" > a.txt
  2. On remote machine make another file echo "def" > b.txt
  3. On local machine run Get-SCPItem -Path "/root/a.txt" -Destination "./" -Computer localhost -Port 10022 -Force -PathType File
  4. On local machine run more a.txt, note output should be abcdef
  5. On local machine run Get-SCPItem -Path "/root/b.txt" -Destination "./" -Computer localhost -Port 10022 -Force -PathType File -NewName a.txt
  6. On local machine run more a.txt

At this point I would expect the local file named a.txt to contain def but instead it contains def\nef.

The use of -NewName parameter has no bearing on the issue. The remote file named a.txt can be modified in between the calls to Get-SCPItem to reproduce the issue.

image

soul4soul avatar Mar 31 '20 15:03 soul4soul

Sadly I don't own the library used by the module, moat I can do is that if the file exists to delete it and the re download.

darkoperator avatar Mar 31 '20 15:03 darkoperator

Alright, I reported this to SSH.NET as you have indicated its a problem with the library. The SCPClient Download function doesn't explicitly mention the behavior when a local file exists. I can code around it in my scripts but It feels right to have Posh-SSH or it's dependency handle it.

This could be something that is just always done in Posh-SSH. I could see this going behind a new switch too.

soul4soul avatar Mar 31 '20 21:03 soul4soul