mirror of
https://github.com/containers/podman.git
synced 2025-06-24 11:28:24 +08:00
Merge pull request #9469 from vrothberg/cp-stdout
podman cp: /dev/std{in,out} fixes
This commit is contained in:
@ -121,7 +121,9 @@ func copyFromContainer(container string, containerPath string, hostPath string)
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isStdout := false
|
||||||
if hostPath == "-" {
|
if hostPath == "-" {
|
||||||
|
isStdout = true
|
||||||
hostPath = os.Stdout.Name()
|
hostPath = os.Stdout.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,10 +154,16 @@ func copyFromContainer(container string, containerPath string, hostPath string)
|
|||||||
hostBaseName = filepath.Base(hostInfo.LinkTarget)
|
hostBaseName = filepath.Base(hostInfo.LinkTarget)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !isStdout {
|
||||||
|
if err := validateFileInfo(hostInfo); err != nil {
|
||||||
|
return errors.Wrap(err, "invalid destination")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
reader, writer := io.Pipe()
|
reader, writer := io.Pipe()
|
||||||
hostCopy := func() error {
|
hostCopy := func() error {
|
||||||
defer reader.Close()
|
defer reader.Close()
|
||||||
if hostInfo.LinkTarget == os.Stdout.Name() {
|
if isStdout {
|
||||||
_, err := io.Copy(os.Stdout, reader)
|
_, err := io.Copy(os.Stdout, reader)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -223,8 +231,6 @@ func copyToContainer(container string, containerPath string, hostPath string) er
|
|||||||
if hostPath == "-" {
|
if hostPath == "-" {
|
||||||
hostPath = os.Stdin.Name()
|
hostPath = os.Stdin.Name()
|
||||||
isStdin = true
|
isStdin = true
|
||||||
} else if hostPath == os.Stdin.Name() {
|
|
||||||
isStdin = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure that host path exists.
|
// Make sure that host path exists.
|
||||||
@ -363,3 +369,12 @@ func containerParentDir(container string, containerPath string) (string, error)
|
|||||||
workDir = filepath.Join(workDir, containerPath)
|
workDir = filepath.Join(workDir, containerPath)
|
||||||
return filepath.Dir(workDir), nil
|
return filepath.Dir(workDir), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// validateFileInfo returns an error if the specified FileInfo doesn't point to
|
||||||
|
// a directory or a regular file.
|
||||||
|
func validateFileInfo(info *copy.FileInfo) error {
|
||||||
|
if info.Mode.IsDir() || info.Mode.IsRegular() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return errors.Errorf("%q must be a directory or a regular file", info.LinkTarget)
|
||||||
|
}
|
||||||
|
@ -475,9 +475,9 @@ load helpers
|
|||||||
run_podman exec cpcontainer rm -rf /tmp/$srcdir
|
run_podman exec cpcontainer rm -rf /tmp/$srcdir
|
||||||
|
|
||||||
# Now for "/dev/stdin".
|
# Now for "/dev/stdin".
|
||||||
|
# Note: while this works, the content ends up in Nirvana.
|
||||||
|
# Same for Docker.
|
||||||
run_podman cp /dev/stdin cpcontainer:/tmp < $tar_file
|
run_podman cp /dev/stdin cpcontainer:/tmp < $tar_file
|
||||||
run_podman exec cpcontainer cat /tmp/$srcdir/$rand_filename
|
|
||||||
is "$output" "$rand_content"
|
|
||||||
|
|
||||||
# Error checks below ...
|
# Error checks below ...
|
||||||
|
|
||||||
@ -487,11 +487,11 @@ load helpers
|
|||||||
|
|
||||||
# Destination must be a directory (on an existing file).
|
# Destination must be a directory (on an existing file).
|
||||||
run_podman exec cpcontainer touch /tmp/file.txt
|
run_podman exec cpcontainer touch /tmp/file.txt
|
||||||
run_podman 125 cp /dev/stdin cpcontainer:/tmp/file.txt < $tar_file
|
run_podman 125 cp - cpcontainer:/tmp/file.txt < $tar_file
|
||||||
is "$output" 'Error: destination must be a directory when copying from stdin'
|
is "$output" 'Error: destination must be a directory when copying from stdin'
|
||||||
|
|
||||||
# Destination must be a directory (on an absent path).
|
# Destination must be a directory (on an absent path).
|
||||||
run_podman 125 cp /dev/stdin cpcontainer:/tmp/IdoNotExist < $tar_file
|
run_podman 125 cp - cpcontainer:/tmp/IdoNotExist < $tar_file
|
||||||
is "$output" 'Error: destination must be a directory when copying from stdin'
|
is "$output" 'Error: destination must be a directory when copying from stdin'
|
||||||
|
|
||||||
run_podman rm -f cpcontainer
|
run_podman rm -f cpcontainer
|
||||||
@ -508,6 +508,10 @@ load helpers
|
|||||||
run_podman exec cpcontainer sh -c "echo '$rand_content' > /tmp/file.txt"
|
run_podman exec cpcontainer sh -c "echo '$rand_content' > /tmp/file.txt"
|
||||||
run_podman exec cpcontainer touch /tmp/empty.txt
|
run_podman exec cpcontainer touch /tmp/empty.txt
|
||||||
|
|
||||||
|
# Make sure that only "-" gets special treatment. "/dev/stdout"
|
||||||
|
run_podman 125 cp cpcontainer:/tmp/file.txt /dev/stdout
|
||||||
|
is "$output" 'Error: invalid destination: "/dev/stdout" must be a directory or a regular file'
|
||||||
|
|
||||||
# Copying from stdout will always compress. So let's copy the previously
|
# Copying from stdout will always compress. So let's copy the previously
|
||||||
# created file from the container via stdout, untar the archive and make
|
# created file from the container via stdout, untar the archive and make
|
||||||
# sure the file exists with the expected content.
|
# sure the file exists with the expected content.
|
||||||
|
Reference in New Issue
Block a user