mirror of
https://github.com/containers/podman.git
synced 2025-06-25 12:20:42 +08:00
Merge pull request #2845 from QiWang19/cpdir
fix bug podman cp directory
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@ -207,6 +208,11 @@ func copy(src, destPath, dest string, idMappingOpts storage.IDMappingOptions, ch
|
|||||||
if !srcfi.IsDir() && !strings.HasSuffix(dest, string(os.PathSeparator)) {
|
if !srcfi.IsDir() && !strings.HasSuffix(dest, string(os.PathSeparator)) {
|
||||||
destdir = filepath.Dir(destPath)
|
destdir = filepath.Dir(destPath)
|
||||||
}
|
}
|
||||||
|
_, err = os.Stat(destdir)
|
||||||
|
if err != nil && !os.IsNotExist(err) {
|
||||||
|
return errors.Wrapf(err, "error checking directory %q", destdir)
|
||||||
|
}
|
||||||
|
destDirIsExist := (err == nil)
|
||||||
if err = os.MkdirAll(destdir, 0755); err != nil {
|
if err = os.MkdirAll(destdir, 0755); err != nil {
|
||||||
return errors.Wrapf(err, "error creating directory %q", destdir)
|
return errors.Wrapf(err, "error creating directory %q", destdir)
|
||||||
}
|
}
|
||||||
@ -219,6 +225,9 @@ func copy(src, destPath, dest string, idMappingOpts storage.IDMappingOptions, ch
|
|||||||
if srcfi.IsDir() {
|
if srcfi.IsDir() {
|
||||||
|
|
||||||
logrus.Debugf("copying %q to %q", srcPath+string(os.PathSeparator)+"*", dest+string(os.PathSeparator)+"*")
|
logrus.Debugf("copying %q to %q", srcPath+string(os.PathSeparator)+"*", dest+string(os.PathSeparator)+"*")
|
||||||
|
if destDirIsExist && !strings.HasSuffix(src, fmt.Sprintf("%s.", string(os.PathSeparator))) {
|
||||||
|
destPath = filepath.Join(destPath, filepath.Base(srcPath))
|
||||||
|
}
|
||||||
if err = copyWithTar(srcPath, destPath); err != nil {
|
if err = copyWithTar(srcPath, destPath); err != nil {
|
||||||
return errors.Wrapf(err, "error copying %q to %q", srcPath, dest)
|
return errors.Wrapf(err, "error copying %q to %q", srcPath, dest)
|
||||||
}
|
}
|
||||||
|
@ -112,4 +112,37 @@ var _ = Describe("Podman cp", func() {
|
|||||||
}
|
}
|
||||||
Expect(string(output)).To(Equal("copy from host to container directory"))
|
Expect(string(output)).To(Equal("copy from host to container directory"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("podman cp dir to dir", func() {
|
||||||
|
path, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
testDirPath := filepath.Join(path, "TestDir")
|
||||||
|
err = os.Mkdir(testDirPath, 0777)
|
||||||
|
if err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
session := podmanTest.Podman([]string{"create", ALPINE, "ls", "/foodir"})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(Equal(0))
|
||||||
|
name := session.OutputToString()
|
||||||
|
|
||||||
|
session = podmanTest.Podman([]string{"cp", testDirPath, name + ":/foodir"})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(Equal(0))
|
||||||
|
session = podmanTest.Podman([]string{"start", "-a", name})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(Equal(0))
|
||||||
|
Expect(len(session.OutputToStringArray())).To(Equal(0))
|
||||||
|
|
||||||
|
session = podmanTest.Podman([]string{"cp", testDirPath, name + ":/foodir"})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(Equal(0))
|
||||||
|
session = podmanTest.Podman([]string{"start", "-a", name})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(Equal(0))
|
||||||
|
Expect(session.OutputToString()).To(Equal("TestDir"))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user