2023-08-27 07:57:02 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2023-08-28 05:53:17 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2023-08-27 07:57:02 +00:00
|
|
|
"io"
|
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ReadCloserCommand wraps an exec.Cmd and its standard output to provide an
|
|
|
|
// io.ReadCloser that waits for the command to terminate on Close(), reporting
|
|
|
|
// any error in the command.Wait() function back to the Close() caller.
|
|
|
|
type ReadCloserCommand struct {
|
|
|
|
Cmd *exec.Cmd
|
|
|
|
Stdout io.ReadCloser
|
2023-08-28 05:53:17 +00:00
|
|
|
|
2023-08-29 07:19:17 +00:00
|
|
|
// We should call exec.Wait() once. waitHandled is taking care of storing
|
|
|
|
// whether we already called that function in Read() to avoid calling it
|
|
|
|
// again in Close().
|
|
|
|
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-08-28 05:53:17 +00:00
|
|
|
// Read populate the array with data from the process stdout.
|
|
|
|
func (fp *ReadCloserCommand) Read(p []byte) (int, error) {
|
2023-08-29 07:19:17 +00:00
|
|
|
if fp.alreadyClosedReadErr != nil {
|
|
|
|
return 0, fp.alreadyClosedReadErr
|
|
|
|
}
|
2023-08-28 05:53:17 +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) {
|
|
|
|
// Check if the command terminated successfully. If not, return the
|
2023-08-28 05:53:17 +00:00
|
|
|
// error.
|
2023-08-29 07:19:17 +00:00
|
|
|
fp.waitHandled = true
|
|
|
|
errw := fp.Cmd.Wait()
|
|
|
|
if errw != nil {
|
|
|
|
// If we have information about the exit code, let's use it in the
|
|
|
|
// error message. Otherwise, send the error message along.
|
|
|
|
// In any case, use a fatal error to abort the snapshot.
|
|
|
|
var err2 *exec.ExitError
|
|
|
|
if errors.As(errw, &err2) {
|
|
|
|
err = errors.Fatalf("command terminated with exit code %d", err2.ExitCode())
|
|
|
|
} else {
|
|
|
|
err = errors.Fatal(errw.Error())
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (fp *ReadCloserCommand) Close() error {
|
2023-08-29 07:19:17 +00:00
|
|
|
if fp.waitHandled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-27 07:57:02 +00:00
|
|
|
// No need to close fp.Stdout as Wait() closes all pipes.
|
2023-08-28 05:53:17 +00:00
|
|
|
err := fp.Cmd.Wait()
|
|
|
|
if err != nil {
|
|
|
|
// If we have information about the exit code, let's use it in the
|
|
|
|
// error message. Otherwise, send the error message along.
|
|
|
|
// In any case, use a fatal error to abort the snapshot.
|
|
|
|
var err2 *exec.ExitError
|
|
|
|
if errors.As(err, &err2) {
|
|
|
|
return errors.Fatalf("command terminated with exit code %d", err2.ExitCode())
|
|
|
|
}
|
|
|
|
return errors.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
return nil
|
2023-08-27 07:57:02 +00:00
|
|
|
}
|