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 7, 2020
1 parent 51159e7 commit a650faf
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
22 changes: 18 additions & 4 deletions cmd/podman/images/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/containers/podman/v2/libpod/define"
"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 +15,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 @@ -85,7 +85,10 @@ func saveFlags(flags *pflag.FlagSet) {

func save(cmd *cobra.Command, args []string) error {
var (
tags []string
tags []string
pipePath string
cleanup func() error
err error
)
if cmd.Flag("compress").Changed && (saveOpts.Format != define.OCIManifestDir && saveOpts.Format != define.V2s2ManifestDir && saveOpts.Format == "") {
return errors.Errorf("--compress can only be set when --format is either 'oci-dir' or 'docker-dir'")
Expand All @@ -95,13 +98,24 @@ 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"
pipePath, cleanup, err = setupPipe()
if err != nil {
return err
}
saveOpts.Output = pipePath
}
if err := parse.ValidateFileName(saveOpts.Output); err != nil {
return err
}
if len(args) > 1 {
tags = args[1:]
}
return registry.ImageEngine().Save(context.Background(), args[0], tags, saveOpts)
err = registry.ImageEngine().Save(context.Background(), args[0], tags, saveOpts)
if err != nil {
return err
}
if cleanup != nil {
defer cleanup()
}
return nil
}
42 changes: 42 additions & 0 deletions cmd/podman/images/utils_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package images

import (
"io"
"io/ioutil"
"os"
"path/filepath"
"syscall"

"github.com/pkg/errors"
)

// setupPipe() for fixing https://github.com/containers/podman/issues/7017
// uses named pipe since containers/image EvalSymlinks fails with /dev/stdout
func setupPipe() (string, func() error, error) {
errc := make(chan error)
pipeDir, err := ioutil.TempDir(os.TempDir(), "pipeDir")
if err != nil {
return "", nil, err
}
pipePath := filepath.Join(pipeDir, "saveio")
err = syscall.Mkfifo(pipePath, 0600)
if err != nil {
os.RemoveAll(pipeDir)
return "", nil, errors.Wrapf(err, "error creating named pipe")
}
go func() {
fpipe, err := os.Open(pipePath)
if err != nil {
errc <- err
return
}
_, err = io.Copy(os.Stdout, fpipe)
fpipe.Close()
errc <- err
}()
return pipePath, func() error {
err := <-errc
os.RemoveAll(pipeDir)
return err
}, nil
}
7 changes: 7 additions & 0 deletions cmd/podman/images/utils_unsupported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build !linux

package images

func setupPipe() (string, func() error, error) {
return "/dev/stdout", nil, nil
}

0 comments on commit a650faf

Please sign in to comment.