Skip to content

Commit

Permalink
feat: Preserve original file permissions & Default copy permissions c… (
Browse files Browse the repository at this point in the history
  • Loading branch information
wlynxg authored Sep 19, 2023
1 parent 42de1c1 commit 7c9eefe
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion os/gfile/gfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const (
DefaultPermOpen = os.FileMode(0666)

// DefaultPermCopy is the default perm for file/folder copy.
DefaultPermCopy = os.FileMode(0777)
DefaultPermCopy = os.FileMode(0755)
)

var (
Expand Down
18 changes: 16 additions & 2 deletions os/gfile/gfile_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ import (

// CopyOption is the option for Copy* functions.
type CopyOption struct {
Sync bool // Auto call file sync after source file content copied to target file.
Mode os.FileMode // Destination created file mode. The default file mode is DefaultPermCopy.
// Auto call file sync after source file content copied to target file.
Sync bool

// Preserve the mode of the original file to the target file.
// If true, the Mode attribute will make no sense.
PreserveMode bool

// Destination created file mode.
// The default file mode is DefaultPermCopy if PreserveMode is false.
Mode os.FileMode
}

// Copy file/directory from `src` to `dst`.
Expand Down Expand Up @@ -162,6 +170,9 @@ func CopyFile(src, dst string, option ...CopyOption) (err error) {
return
}
}
if usedOption.PreserveMode {
usedOption.Mode = srcStat.Mode().Perm()
}
if err = Chmod(dst, usedOption.Mode); err != nil {
return
}
Expand Down Expand Up @@ -192,6 +203,9 @@ func CopyDir(src string, dst string, option ...CopyOption) (err error) {
if !si.IsDir() {
return gerror.NewCode(gcode.CodeInvalidParameter, "source is not a directory")
}
if usedOption.PreserveMode {
usedOption.Mode = si.Mode().Perm()
}
if !Exists(dst) {
if err = os.MkdirAll(dst, usedOption.Mode); err != nil {
err = gerror.Wrapf(
Expand Down
2 changes: 1 addition & 1 deletion os/gfile/gfile_z_exmaple_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func ExampleChmod() {

// Output:
// -rw-r--r--
// -rwxrwxrwx
// -rwxr-xr-x
}

func ExampleAbs() {
Expand Down
36 changes: 36 additions & 0 deletions os/gfile/gfile_z_unit_copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package gfile_test

import (
"os"
"testing"

"github.com/gogf/gf/v2/os/gfile"
Expand Down Expand Up @@ -100,6 +101,41 @@ func Test_CopyFile(t *testing.T) {
t.Assert(gfile.GetContents(src), srcContent)
t.Assert(gfile.GetContents(dst), srcContent)
})
// Set mode
gtest.C(t, func(t *gtest.T) {
var (
src = "/testfile_copyfile1.txt"
dst = "/testfile_copyfile2.txt"
dstMode = os.FileMode(0600)
)
t.AssertNil(createTestFile(src, ""))
defer delTestFiles(src)

t.Assert(gfile.CopyFile(testpath()+src, testpath()+dst, gfile.CopyOption{Mode: dstMode}), nil)
defer delTestFiles(dst)

dstStat, err := gfile.Stat(testpath() + dst)
t.AssertNil(err)
t.Assert(dstStat.Mode().Perm(), dstMode)
})
// Preserve src file's mode
gtest.C(t, func(t *gtest.T) {
var (
src = "/testfile_copyfile1.txt"
dst = "/testfile_copyfile2.txt"
)
t.AssertNil(createTestFile(src, ""))
defer delTestFiles(src)

t.Assert(gfile.CopyFile(testpath()+src, testpath()+dst, gfile.CopyOption{PreserveMode: true}), nil)
defer delTestFiles(dst)

srcStat, err := gfile.Stat(testpath() + src)
t.AssertNil(err)
dstStat, err := gfile.Stat(testpath() + dst)
t.AssertNil(err)
t.Assert(srcStat.Mode().Perm(), dstStat.Mode().Perm())
})
}

func Test_CopyDir(t *testing.T) {
Expand Down

0 comments on commit 7c9eefe

Please sign in to comment.