Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Preserve original file permissions & Default copy permissions c… #2969

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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