2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-04 19:03:46 +00:00
restic/internal/backend/rclone/stdio_conn.go

75 lines
1.2 KiB
Go
Raw Normal View History

2018-03-13 21:30:51 +00:00
package rclone
import (
"net"
"os"
"os/exec"
"sync"
2018-03-13 21:30:51 +00:00
"github.com/restic/restic/internal/debug"
)
// StdioConn implements a net.Conn via stdin/stdout.
type StdioConn struct {
2018-03-30 07:52:46 +00:00
stdin *os.File
stdout *os.File
cmd *exec.Cmd
close sync.Once
2018-03-13 21:30:51 +00:00
}
func (s *StdioConn) Read(p []byte) (int, error) {
n, err := s.stdin.Read(p)
return n, err
}
func (s *StdioConn) Write(p []byte) (int, error) {
n, err := s.stdout.Write(p)
return n, err
}
// Close closes both streams.
func (s *StdioConn) Close() (err error) {
s.close.Do(func() {
debug.Log("close stdio connection")
var errs []error
2018-03-13 21:30:51 +00:00
for _, f := range []func() error{s.stdin.Close, s.stdout.Close} {
err := f()
if err != nil {
errs = append(errs, err)
}
2018-03-13 21:30:51 +00:00
}
if len(errs) > 0 {
err = errs[0]
}
})
return err
2018-03-13 21:30:51 +00:00
}
// LocalAddr returns nil.
func (s *StdioConn) LocalAddr() net.Addr {
return Addr{}
}
// RemoteAddr returns nil.
func (s *StdioConn) RemoteAddr() net.Addr {
return Addr{}
}
// make sure StdioConn implements net.Conn
var _ net.Conn = &StdioConn{}
// Addr implements net.Addr for stdin/stdout.
type Addr struct{}
// Network returns the network type as a string.
func (a Addr) Network() string {
return "stdio"
}
func (a Addr) String() string {
return "stdio"
}