forked from TrueCloudLab/restic
Improve error message when sftp fails
Also add a prefix for all errors written to stderr by the client
This commit is contained in:
parent
22bde5b277
commit
83924d0864
1 changed files with 15 additions and 3 deletions
|
@ -1,6 +1,7 @@
|
||||||
package sftp
|
package sftp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -36,8 +37,18 @@ func startClient(program string, args ...string) (*SFTP, error) {
|
||||||
// command. This assumes that passwordless login is correctly configured.
|
// command. This assumes that passwordless login is correctly configured.
|
||||||
cmd := exec.Command(program, args...)
|
cmd := exec.Command(program, args...)
|
||||||
|
|
||||||
// send errors from ssh to stderr
|
// prefix the errors with the program name
|
||||||
cmd.Stderr = os.Stderr
|
stderr, err := cmd.StderrPipe()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
sc := bufio.NewScanner(stderr)
|
||||||
|
for sc.Scan() {
|
||||||
|
fmt.Fprintf(os.Stderr, "subprocess %v: %v\n", program, sc.Text())
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// ignore signals sent to the parent (e.g. SIGINT)
|
// ignore signals sent to the parent (e.g. SIGINT)
|
||||||
cmd.SysProcAttr = ignoreSigIntProcAttr()
|
cmd.SysProcAttr = ignoreSigIntProcAttr()
|
||||||
|
@ -68,7 +79,7 @@ func startClient(program string, args ...string) (*SFTP, error) {
|
||||||
// open the SFTP session
|
// open the SFTP session
|
||||||
client, err := sftp.NewClientPipe(rd, wr)
|
client, err := sftp.NewClientPipe(rd, wr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("unable to start the sftp session, error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &SFTP{c: client, cmd: cmd, result: ch}, nil
|
return &SFTP{c: client, cmd: cmd, result: ch}, nil
|
||||||
|
@ -107,6 +118,7 @@ func Open(dir string, program string, args ...string) (*SFTP, error) {
|
||||||
debug.Log("sftp.Open", "open backend with program %v, %v at %v", program, args, dir)
|
debug.Log("sftp.Open", "open backend with program %v, %v at %v", program, args, dir)
|
||||||
sftp, err := startClient(program, args...)
|
sftp, err := startClient(program, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
debug.Log("sftp.Open", "unable to start program: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue