diff --git a/components/cluster/command/import.go b/components/cluster/command/import.go index c26c2fb220..4063fd6cb4 100644 --- a/components/cluster/command/import.go +++ b/components/cluster/command/import.go @@ -116,10 +116,10 @@ func newImportCmd() *cobra.Command { srcKeyPathPub := srcKeyPathPriv + ".pub" dstKeyPathPriv := spec.ClusterPath(clsName, "ssh", "id_rsa") dstKeyPathPub := dstKeyPathPriv + ".pub" - if err = tiuputils.CopyFile(srcKeyPathPriv, dstKeyPathPriv); err != nil { + if err = tiuputils.Copy(srcKeyPathPriv, dstKeyPathPriv); err != nil { return err } - if err = tiuputils.CopyFile(srcKeyPathPub, dstKeyPathPub); err != nil { + if err = tiuputils.Copy(srcKeyPathPub, dstKeyPathPub); err != nil { return err } diff --git a/components/err/error.go b/components/err/error.go index 24b14c3a65..38963cd317 100644 --- a/components/err/error.go +++ b/components/err/error.go @@ -55,7 +55,7 @@ func main() { type errorSpec struct { Code string `toml:"code" json:"code"` - Error string `toml:"error" json:"error"` + Error string `toml:"error" json:"error"` Description string `toml:"description" json:"description"` Tags []string `toml:"tags" json:"tags"` Workaround string `toml:"workaround" json:"workaround"` diff --git a/pkg/cluster/manager.go b/pkg/cluster/manager.go index f5a090fbde..a682c2eb3e 100644 --- a/pkg/cluster/manager.go +++ b/pkg/cluster/manager.go @@ -1801,7 +1801,7 @@ func overwritePatch(specManager *spec.SpecManager, clusterName, comp, packagePat tg := specManager.Path(clusterName, spec.PatchDirName, comp+"-"+checksum[:7]+".tar.gz") if !utils.IsExist(tg) { - if err := utils.CopyFile(packagePath, tg); err != nil { + if err := utils.Copy(packagePath, tg); err != nil { return err } } diff --git a/pkg/localdata/profile.go b/pkg/localdata/profile.go index 5afdaa6ede..2c1b1084e4 100644 --- a/pkg/localdata/profile.go +++ b/pkg/localdata/profile.go @@ -386,7 +386,7 @@ func (p *Profile) ResetMirror(addr, root string) error { fmt.Printf("WARN: adding root certificate via internet: %s\n", root) fmt.Printf("You can revoke this by remove %s\n", localRoot) } - _ = utils.CopyFile(p.Path("bin", "root.json"), localRoot) + _ = utils.Copy(p.Path("bin", "root.json"), localRoot) } if err := os.RemoveAll(p.Path(ManifestParentDir)); err != nil { diff --git a/pkg/utils/ioutil.go b/pkg/utils/ioutil.go index 3b8d2a3373..8a5327d78f 100644 --- a/pkg/utils/ioutil.go +++ b/pkg/utils/ioutil.go @@ -19,7 +19,6 @@ import ( "compress/gzip" "crypto/sha1" "encoding/hex" - "fmt" "io" "os" "path" @@ -149,7 +148,13 @@ func Copy(src, dst string) error { if err != nil { return err } - return out.Close() + + err = out.Close() + if err != nil { + return err + } + + return os.Chmod(dst, fi.Mode()) } // Move moves a file from src to dst, this is done by copying the file and then @@ -173,29 +178,6 @@ func CreateDir(path string) error { return nil } -// CopyFile copies a file from src to dst -func CopyFile(src, dst string) error { - in, err := os.Open(src) - if err != nil { - return err - } - defer in.Close() - - if IsExist(dst) { - return fmt.Errorf("destination path %s already exist", dst) - } - out, err := os.Create(dst) - if err != nil { - return err - } - defer out.Close() - - if _, err = io.Copy(out, in); err != nil { - return err - } - return nil -} - // Checksum returns the sha1 sum of target file func Checksum(file string) (string, error) { tarball, err := os.OpenFile(file, os.O_RDONLY, 0) diff --git a/pkg/utils/ioutil_test.go b/pkg/utils/ioutil_test.go index 4cba2b121d..e3aeed6bd0 100644 --- a/pkg/utils/ioutil_test.go +++ b/pkg/utils/ioutil_test.go @@ -68,3 +68,21 @@ func (s *TestIOUtilSuite) TestUntar(c *C) { c.Assert(err, IsNil) c.Assert(IsExist(path.Join(currentDir(), "testdata", "parent", "child", "content")), IsTrue) } + +func (s *TestIOUtilSuite) TestCopy(c *C) { + c.Assert(Copy(path.Join(currentDir(), "testdata", "test.tar.gz"), "/tmp/not-exists/test.tar.gz"), NotNil) + c.Assert(Copy(path.Join(currentDir(), "testdata", "test.tar.gz"), "/tmp/test.tar.gz"), IsNil) + fi, err := os.Stat(path.Join(currentDir(), "testdata", "test.tar.gz")) + c.Assert(err, IsNil) + fii, err := os.Stat("/tmp/test.tar.gz") + c.Assert(err, IsNil) + c.Assert(fi.Mode(), Equals, fii.Mode()) + + c.Assert(os.Chmod("/tmp/test.tar.gz", 0777), IsNil) + c.Assert(Copy(path.Join(currentDir(), "testdata", "test.tar.gz"), "/tmp/test.tar.gz"), IsNil) + fi, err = os.Stat(path.Join(currentDir(), "testdata", "test.tar.gz")) + c.Assert(err, IsNil) + fii, err = os.Stat("/tmp/test.tar.gz") + c.Assert(err, IsNil) + c.Assert(fi.Mode(), Equals, fii.Mode()) +}