use renameio to avoid “text file busy” errors

Before this commit, extracting a breakglass would fail when /tmp/breakglass*/sh
was busy because it was being run in a separate connection.
This commit is contained in:
Michael Stapelberg 2022-04-09 00:11:47 +02:00
parent 564a0eceaf
commit ef69007a43
3 changed files with 6 additions and 3 deletions

1
go.mod
View File

@ -5,6 +5,7 @@ go 1.17
require ( require (
github.com/gokrazy/gokrazy v0.0.0-20211024151958-b718dd90ae71 github.com/gokrazy/gokrazy v0.0.0-20211024151958-b718dd90ae71
github.com/gokrazy/internal v0.0.0-20210621162516-1b3b5687a06d github.com/gokrazy/internal v0.0.0-20210621162516-1b3b5687a06d
github.com/google/renameio/v2 v2.0.0
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf
github.com/kr/pty v1.1.8 github.com/kr/pty v1.1.8
github.com/pkg/sftp v1.13.0 github.com/pkg/sftp v1.13.0

2
go.sum
View File

@ -11,6 +11,8 @@ github.com/gokrazy/internal v0.0.0-20210621162516-1b3b5687a06d/go.mod h1:Gqv1x1D
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gopacket v1.1.16/go.mod h1:UCLx9mCmAwsVbn6qQl1WIEt2SO7Nd2fD0th1TBAsqBw= github.com/google/gopacket v1.1.16/go.mod h1:UCLx9mCmAwsVbn6qQl1WIEt2SO7Nd2fD0th1TBAsqBw=
github.com/google/renameio/v2 v2.0.0 h1:UifI23ZTGY8Tt29JbYFiuyIU3eX+RNFtUwefq9qAhxg=
github.com/google/renameio/v2 v2.0.0/go.mod h1:BtmJXm5YlszgC+TD4HOEEUFgkJP3nLxehU6hfe7jRt4=
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf h1:7+FW5aGwISbqUtkfmIpZJGRgNFg2ioYPvFaUxdqpDsg= github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf h1:7+FW5aGwISbqUtkfmIpZJGRgNFg2ioYPvFaUxdqpDsg=
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf/go.mod h1:RpwtwJQFrIEPstU94h88MWPXP2ektJZ8cZ0YntAmXiE= github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf/go.mod h1:RpwtwJQFrIEPstU94h88MWPXP2ektJZ8cZ0YntAmXiE=
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8= github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=

6
scp.go
View File

@ -11,6 +11,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/google/renameio/v2"
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
) )
@ -83,15 +84,14 @@ func scpSink(channel ssh.Channel, req *ssh.Request, cmdline []string) error {
continue // directory, dont try to OpenFile() it continue // directory, dont try to OpenFile() it
} }
mode := h.FileInfo().Mode() & os.ModePerm mode := h.FileInfo().Mode() & os.ModePerm
out, err := os.OpenFile(h.Name, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, mode) out, err := renameio.NewPendingFile(h.Name, renameio.WithStaticPermissions(mode))
if err != nil { if err != nil {
return err return err
} }
if _, err := io.Copy(out, tr); err != nil { if _, err := io.Copy(out, tr); err != nil {
out.Close()
return err return err
} }
if err := out.Close(); err != nil { if err := out.CloseAtomicallyReplace(); err != nil {
return err return err
} }
} }