Skip to content

Commit

Permalink
Fix copy file without mode (#844)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucklove authored Oct 19, 2020
1 parent 6fc2fb9 commit 3dcd09b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 30 deletions.
4 changes: 2 additions & 2 deletions components/cluster/command/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion components/err/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/localdata/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 7 additions & 25 deletions pkg/utils/ioutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"compress/gzip"
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"os"
"path"
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions pkg/utils/ioutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit 3dcd09b

Please sign in to comment.