Win32-OpenSSH icon indicating copy to clipboard operation
Win32-OpenSSH copied to clipboard

`scp` locks the output file but it shouldn't

Open RobbyCBennett opened this issue 1 year ago • 3 comments
trafficstars

Prerequisites

  • [X] Write a descriptive title.
  • [X] Make sure you are able to repro it on the latest version
  • [X] Search the existing issues.

Steps to reproduce

  1. In CMD or PowerShell, download a big file into Windows with `scp
scp "hostname:big_input_file" big_output_file
  1. In another PowerShell terminal, while it's downloading, try to access it:
Get-Content big_output_file -TotalCount 2KB
  1. See the following error:
Get-Content: The process cannot access the file 'C:\Users\Robby\Desktop\big_output_file' because it is being used by another process.

Expected behavior

I'm trying to download a big file with `scp` and read it during the download in another program. This works with `scp` in WSL, but not in Windows.

Actual behavior

In Windows, I can't read from the file because it gets locked by `scp`.

Error details

Exception             :
    Type       : System.IO.IOException
    TargetSite :
        Name          : CreateFile
        DeclaringType : [Microsoft.Win32.SafeHandles.SafeFileHandle]
        MemberType    : Method
        Module        : System.Private.CoreLib.dll
    Message    : The process cannot access the file 'C:\Users\Robby\Desktop\big_output_file' because it is being
used by another process.
    Source     : System.Private.CoreLib
    HResult    : -2147024864
    StackTrace :
   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access,
FileShare share, FileOptions options)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare
share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share,
FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access,
FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at Microsoft.PowerShell.Commands.FileSystemContentReaderWriter.CreateStreams(String filePath, String streamName,
FileMode fileMode, FileAccess fileAccess, FileShare fileShare, Encoding fileEncoding)
   at Microsoft.PowerShell.Commands.FileSystemContentReaderWriter..ctor(String path, String streamName, FileMode mode,
FileAccess access, FileShare share, Encoding encoding, Boolean usingByteEncoding, Boolean waitForChanges,
CmdletProvider provider, Boolean isRawStream)
   at Microsoft.PowerShell.Commands.FileSystemProvider.GetContentReader(String path)
TargetObject          : C:\Users\Robby\Desktop\big_output_file.mkv
CategoryInfo          : ReadError: (C:\Users\Robby\Desktop\big_output_file:String) [Get-Content], IOException
FullyQualifiedErrorId : GetContentReaderIOError,Microsoft.PowerShell.Commands.GetContentCommand
InvocationInfo        :
    MyCommand        : Get-Content
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 15
    Line             : Get-Content $env:file -TotalCount 2KB
    Statement        : Get-Content $env:file -TotalCount 2KB
    PositionMessage  : At line:1 char:1
                       + Get-Content $env:file -TotalCount 2KB
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Get-Content
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :
      0
      1

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.3
PSEdition                      Core
GitCommitId                    7.4.3
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Version

OpenSSH_for_Windows_9.5p1, LibreSSL 3.8.2

Visuals

No response

RobbyCBennett avatar Jun 26 '24 00:06 RobbyCBennett

We don't believe this is a scp issue, we believe this is consistent behavior across Windows.

If you copy the same large file from one location to another on your computer, can you open and read the file in the new location while it is being copied?

maertendMSFT avatar Jul 01 '24 17:07 maertendMSFT

You are correct that the same behavior exists across Windows. I have the same issue with the copy command.

Just because it's consistent, it does not make it correct. There should be a way to disable the locking. If the desire is to make this tool more like Linux, then locking should be an opt-in feature. Regardless of whether it's opt-in or opt-out, there should be a way to disable locking.

RobbyCBennett avatar Jul 07 '24 17:07 RobbyCBennett

This usually comes down to the CreateFile API and whether the FILE_SHARE_READ and FILE_SHARE_WRITE flags were passed in. I don't recall if the fork is using CreateFile directly or using the POSIX-esque calls.

NoMoreFood avatar Jul 07 '24 21:07 NoMoreFood