2023-08-27 07:57:02 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2023-10-01 13:25:48 +00:00
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-08-27 07:57:02 +00:00
|
|
|
"io"
|
|
|
|
"os/exec"
|
2023-10-01 13:07:51 +00:00
|
|
|
|
|
|
|
"github.com/restic/restic/internal/errors"
|
2023-08-27 07:57:02 +00:00
|
|
|
)
|
|
|
|
|
2023-10-01 13:25:48 +00:00
|
|
|
// CommandReader wrap a command such that its standard output can be read using
|
|
|
|
// a io.ReadCloser. Close() waits for the command to terminate, reporting
|
|
|
|
// any error back to the caller.
|
2023-10-01 13:07:51 +00:00
|
|
|
type CommandReader struct {
|
|
|
|
cmd *exec.Cmd
|
|
|
|
stdout io.ReadCloser
|
2023-08-28 05:53:17 +00:00
|
|
|
|
2023-10-01 13:25:48 +00:00
|
|
|
// cmd.Wait() must only be called once. Prevent duplicate executions in
|
|
|
|
// Read() and Close().
|
2023-08-29 07:19:17 +00:00
|
|
|
waitHandled bool
|
|
|
|
|
|
|
|
// alreadyClosedReadErr is the error that we should return if we try to
|
|
|
|
// read the pipe again after closing. This works around a Read() call that
|
|
|
|
// is issued after a previous Read() with `io.EOF` (but some bytes were
|
|
|
|
// read in the past).
|
|
|
|
alreadyClosedReadErr error
|
2023-08-27 07:57:02 +00:00
|
|
|
}
|
|
|
|
|
2023-10-01 13:25:48 +00:00
|
|
|
func NewCommandReader(ctx context.Context, args []string, logOutput io.Writer) (*CommandReader, error) {
|
|
|
|
// Prepare command and stdout
|
|
|
|
command := exec.CommandContext(ctx, args[0], args[1:]...)
|
|
|
|
stdout, err := command.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to setup stdout pipe: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use a Go routine to handle the stderr to avoid deadlocks
|
|
|
|
stderr, err := command.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to setup stderr pipe: %w", err)
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
sc := bufio.NewScanner(stderr)
|
|
|
|
for sc.Scan() {
|
|
|
|
_, _ = fmt.Fprintf(logOutput, "subprocess %v: %v\n", command.Args[0], sc.Text())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := command.Start(); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to start command: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-10-01 13:07:51 +00:00
|
|
|
return &CommandReader{
|
2023-10-01 13:25:48 +00:00
|
|
|
cmd: command,
|
2023-10-01 13:07:51 +00:00
|
|
|
stdout: stdout,
|
2023-10-01 13:25:48 +00:00
|
|
|
}, nil
|
2023-10-01 13:07:51 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 05:53:17 +00:00
|
|
|
// Read populate the array with data from the process stdout.
|
2023-10-01 13:07:51 +00:00
|
|
|
func (fp *CommandReader) Read(p []byte) (int, error) {
|
2023-08-29 07:19:17 +00:00
|
|
|
if fp.alreadyClosedReadErr != nil {
|
|
|
|
return 0, fp.alreadyClosedReadErr
|
|
|
|
}
|
2023-10-01 13:07:51 +00:00
|
|
|
b, err := fp.stdout.Read(p)
|
2023-08-29 07:19:17 +00:00
|
|
|
|
|
|
|
// If the error is io.EOF, the program terminated. We need to check the
|
|
|
|
// exit code here because, if the program terminated with no output, the
|
|
|
|
// error in `Close()` is ignored.
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
fp.waitHandled = true
|
2023-10-01 13:07:51 +00:00
|
|
|
// check if the command terminated successfully, If not return the error.
|
|
|
|
if errw := fp.wait(); errw != nil {
|
|
|
|
err = errw
|
2023-08-29 07:19:17 +00:00
|
|
|
}
|
2023-08-28 05:53:17 +00:00
|
|
|
}
|
2023-08-29 07:19:17 +00:00
|
|
|
fp.alreadyClosedReadErr = err
|
2023-08-28 05:53:17 +00:00
|
|
|
return b, err
|
2023-08-27 07:57:02 +00:00
|
|
|
}
|
|
|
|
|
2023-10-01 13:07:51 +00:00
|
|
|
func (fp *CommandReader) wait() error {
|
|
|
|
err := fp.cmd.Wait()
|
2023-08-28 05:53:17 +00:00
|
|
|
if err != nil {
|
2023-10-01 13:25:48 +00:00
|
|
|
// Use a fatal error to abort the snapshot.
|
2023-10-01 14:20:45 +00:00
|
|
|
return errors.Fatal(fmt.Errorf("command failed: %w", err).Error())
|
2023-08-28 05:53:17 +00:00
|
|
|
}
|
|
|
|
return nil
|
2023-08-27 07:57:02 +00:00
|
|
|
}
|
2023-10-01 13:07:51 +00:00
|
|
|
|
|
|
|
func (fp *CommandReader) Close() error {
|
|
|
|
if fp.waitHandled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fp.wait()
|
|
|
|
}
|