Skip to content

Commit

Permalink
podman save use named pipe
Browse files Browse the repository at this point in the history
podman save uses named pipe as output path, not directly using /dev/stdout.
fix containers#7017

Signed-off-by: Qi Wang <qiwan@redhat.com>
  • Loading branch information
QiWang19 committed Aug 4, 2020
1 parent d4cf3c5 commit 9549ffd
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions cmd/podman/images/save.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package images

import (
"bufio"
"context"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"syscall"

"github.com/containers/podman/v2/libpod/define"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh/terminal"

"github.com/containers/podman/v2/cmd/podman/parse"
"github.com/containers/podman/v2/cmd/podman/registry"
Expand All @@ -14,7 +21,6 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/crypto/ssh/terminal"
)

var validFormats = []string{define.OCIManifestDir, define.OCIArchive, define.V2s2ManifestDir, define.V2s2Archive}
Expand Down Expand Up @@ -95,7 +101,39 @@ func save(cmd *cobra.Command, args []string) error {
if terminal.IsTerminal(int(fi.Fd())) {
return errors.Errorf("refusing to save to terminal. Use -o flag or redirect")
}
saveOpts.Output = "/dev/stdout"
pipeDir, err := ioutil.TempDir(os.TempDir(), "pipeDir")
if err != nil {
return err
}
saveOpts.Output = filepath.Join(pipeDir, "saveio")
err = syscall.Mkfifo(saveOpts.Output, 0600)
if err != nil {
return errors.Wrapf(err, "error creating named pipe")
}
defer os.RemoveAll(pipeDir)
errc := make(chan error)
fpipe, err := os.OpenFile(saveOpts.Output, os.O_RDWR, os.ModeNamedPipe)
if err != nil {
return err
}
defer fpipe.Close()
go func() {
reader := bufio.NewReader(fpipe)
_, err = io.Copy(os.Stdout, reader)
errc <- err
if err != nil {
logrus.Errorf("error copying data to /dev/stdout, %v", err)
return
}
}()
select {
case err := <-errc:
if err != nil {
return err
}
default:
break
}
}
if err := parse.ValidateFileName(saveOpts.Output); err != nil {
return err
Expand Down

0 comments on commit 9549ffd

Please sign in to comment.