From a0d2f9cc12dec6d8ef06048e977e5b5ef9c8a09d Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Sat, 23 Nov 2019 19:13:32 +0100 Subject: [PATCH] Use a smarter copy --- go.mod | 1 + go.sum | 4 ++ pkg/helpers/file.go | 94 ++------------------------------------------- 3 files changed, 9 insertions(+), 90 deletions(-) diff --git a/go.mod b/go.mod index fa29bacc..e2a6856b 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/mudler/docker-companion v0.4.6-0.20191110154655-b8b364100616 github.com/onsi/ginkgo v1.10.1 github.com/onsi/gomega v1.7.0 + github.com/otiai10/copy v1.0.2 github.com/pelletier/go-toml v1.6.0 // indirect github.com/philopon/go-toposort v0.0.0-20170620085441-9be86dbd762f github.com/pkg/errors v0.8.1 diff --git a/go.sum b/go.sum index 32ba4c45..c89e93a3 100644 --- a/go.sum +++ b/go.sum @@ -191,6 +191,10 @@ github.com/opencontainers/runc v0.1.1 h1:GlxAyO6x8rfZYN9Tt0Kti5a/cP41iuiO2yYT0IJ github.com/opencontainers/runc v0.1.1/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59PVA73FjuZG0U= github.com/opencontainers/runtime-spec v1.0.1 h1:wY4pOY8fBdSIvs9+IDHC55thBuEulhzfSgKeC1yFvzQ= github.com/opencontainers/runtime-spec v1.0.1/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= +github.com/otiai10/copy v1.0.2 h1:DDNipYy6RkIkjMwy+AWzgKiNTyj2RUI9yEMeETEpVyc= +github.com/otiai10/copy v1.0.2/go.mod h1:c7RpqBkwMom4bYTSkLSym4VSJz/XtncWRAj/J4PEIMY= +github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE= +github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml v1.6.0 h1:aetoXYr0Tv7xRU/V4B4IZJ2QcbtMUFoNb3ORp7TzIK4= github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys= diff --git a/pkg/helpers/file.go b/pkg/helpers/file.go index 8c6df65e..e45c6135 100644 --- a/pkg/helpers/file.go +++ b/pkg/helpers/file.go @@ -16,11 +16,11 @@ package helpers import ( - "fmt" - "io" "io/ioutil" "os" "path/filepath" + + copy "github.com/otiai10/copy" ) // Exists reports whether the named file or directory exists. @@ -57,44 +57,7 @@ func ensureDir(fileName string) { // of the source file. The file mode will be copied from the source and // the copied data is synced/flushed to stable storage. func CopyFile(src, dst string) (err error) { - in, err := os.Open(src) - if err != nil { - return - } - defer in.Close() - - ensureDir(dst) // FIXME: Breaks permissions - - out, err := os.Create(dst) - if err != nil { - return - } - defer func() { - if e := out.Close(); e != nil { - err = e - } - }() - - _, err = io.Copy(out, in) - if err != nil { - return - } - - err = out.Sync() - if err != nil { - return - } - - si, err := os.Stat(src) - if err != nil { - return - } - err = os.Chmod(dst, si.Mode()) // FIXME: Needs owners copy as well. - if err != nil { - return - } - - return + return copy.Copy(src, dst) } func IsDirectory(path string) (bool, error) { @@ -111,54 +74,5 @@ func IsDirectory(path string) (bool, error) { func CopyDir(src string, dst string) (err error) { src = filepath.Clean(src) dst = filepath.Clean(dst) - - si, err := os.Stat(src) - if err != nil { - return err - } - if !si.IsDir() { - return fmt.Errorf("source is not a directory") - } - - _, err = os.Stat(dst) - if err != nil && !os.IsNotExist(err) { - return - } - if err == nil { - // return fmt.Errorf("destination already exists") - } - - err = os.MkdirAll(dst, si.Mode()) - if err != nil { - return - } - - entries, err := ioutil.ReadDir(src) - if err != nil { - return - } - - for _, entry := range entries { - srcPath := filepath.Join(src, entry.Name()) - dstPath := filepath.Join(dst, entry.Name()) - - if entry.IsDir() { - err = CopyDir(srcPath, dstPath) - if err != nil { - return - } - } else { - // Skip symlinks. - if entry.Mode()&os.ModeSymlink != 0 { - continue - } - - err = CopyFile(srcPath, dstPath) - if err != nil { - return - } - } - } - - return + return copy.Copy(src, dst) }