Skip to content

Commit

Permalink
Merge pull request #2532 from QiWang19/i2520
Browse files Browse the repository at this point in the history
add flag --extract tar file in podman cp
  • Loading branch information
openshift-merge-robot authored Mar 6, 2019
2 parents c6c0b54 + d9e1b2c commit ef71475
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/podman/cliconfig/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ type BuildValues struct {

type CpValues struct {
PodmanCommand
Extract bool
}
21 changes: 17 additions & 4 deletions cmd/podman/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ var (

func init() {
cpCommand.Command = _cpCommand
flags := cpCommand.Flags()
flags.BoolVar(&cpCommand.Extract, "extract", false, "Extract the tar file into the destination directory.")
rootCmd.AddCommand(cpCommand.Command)
}

Expand All @@ -61,10 +63,11 @@ func cpCmd(c *cliconfig.CpValues) error {
}
defer runtime.Shutdown(false)

return copyBetweenHostAndContainer(runtime, args[0], args[1])
extract := c.Flag("extract").Changed
return copyBetweenHostAndContainer(runtime, args[0], args[1], extract)
}

func copyBetweenHostAndContainer(runtime *libpod.Runtime, src string, dest string) error {
func copyBetweenHostAndContainer(runtime *libpod.Runtime, src string, dest string, extract bool) error {

srcCtr, srcPath := parsePath(runtime, src)
destCtr, destPath := parsePath(runtime, dest)
Expand Down Expand Up @@ -166,7 +169,7 @@ func copyBetweenHostAndContainer(runtime *libpod.Runtime, src string, dest strin

var lastError error
for _, src := range glob {
err := copy(src, destPath, dest, idMappingOpts, &containerOwner)
err := copy(src, destPath, dest, idMappingOpts, &containerOwner, extract)
if lastError != nil {
logrus.Error(lastError)
}
Expand Down Expand Up @@ -219,7 +222,7 @@ func getPathInfo(path string) (string, os.FileInfo, error) {
return path, srcfi, nil
}

func copy(src, destPath, dest string, idMappingOpts storage.IDMappingOptions, chownOpts *idtools.IDPair) error {
func copy(src, destPath, dest string, idMappingOpts storage.IDMappingOptions, chownOpts *idtools.IDPair, extract bool) error {
srcPath, err := filepath.EvalSymlinks(src)
if err != nil {
return errors.Wrapf(err, "error evaluating symlinks %q", srcPath)
Expand All @@ -240,6 +243,7 @@ func copy(src, destPath, dest string, idMappingOpts storage.IDMappingOptions, ch
// return functions for copying items
copyFileWithTar := chrootarchive.CopyFileWithTarAndChown(chownOpts, digest.Canonical.Digester().Hash(), idMappingOpts.UIDMap, idMappingOpts.GIDMap)
copyWithTar := chrootarchive.CopyWithTarAndChown(chownOpts, digest.Canonical.Digester().Hash(), idMappingOpts.UIDMap, idMappingOpts.GIDMap)
untarPath := chrootarchive.UntarPathAndChown(chownOpts, digest.Canonical.Digester().Hash(), idMappingOpts.UIDMap, idMappingOpts.GIDMap)

if srcfi.IsDir() {

Expand All @@ -263,6 +267,15 @@ func copy(src, destPath, dest string, idMappingOpts storage.IDMappingOptions, ch
destPath = filepath.Join(destPath, filepath.Base(srcPath))
}
}

if extract {
// We're extracting an archive into the destination directory.
logrus.Debugf("extracting contents of %q into %q", srcPath, destPath)
if err = untarPath(srcPath, destPath); err != nil {
return errors.Wrapf(err, "error extracting %q into %q", srcPath, destPath)
}
return nil
}
// Copy the file, preserving attributes.
logrus.Debugf("copying %q to %q", srcPath, destPath)
if err = copyFileWithTar(srcPath, destPath); err != nil {
Expand Down
10 changes: 10 additions & 0 deletions completions/bash/podman
Original file line number Diff line number Diff line change
Expand Up @@ -2328,6 +2328,15 @@ _podman_load() {
_complete_ "$options_with_args" "$boolean_options"
}

_podman_cp() {
local boolean_options="
--help
-h
--extract
"
_complete_ "$boolean_options"
}

_podman_login() {
local options_with_args="
--username
Expand Down Expand Up @@ -3015,6 +3024,7 @@ _podman_podman() {
build
commit
container
cp
create
diff
exec
Expand Down
7 changes: 7 additions & 0 deletions docs/podman-cp.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ You can also use : when specifying paths to a **SRC_PATH** or **DEST_PATH** on a
If you use a : in a local machine path, you must be explicit with a relative or absolute path, for example:
`/path/to/file:name.txt` or `./file:name.txt`

## OPTIONS

**--extract**

Extract the tar file into the destination directory. If the destination directory is not provided, extract the tar file into the root directory.

## ALTERNATIVES

Expand Down Expand Up @@ -100,5 +105,7 @@ podman cp containerID:/myapp/ /myapp/

podman cp containerID:/home/myuser/. /home/myuser/

podman cp --extract /home/myuser/myfiles.tar.gz containerID:/myfiles

## SEE ALSO
podman(1), podman-mount(1), podman-umount(1)

0 comments on commit ef71475

Please sign in to comment.