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

os/Chown #4213

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions src/os/file_anyos.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ func Chmod(name string, mode FileMode) error {
return nil
}

// Chown changes the numeric uid and gid of the named file.
// If the file is a symbolic link, it changes the uid and gid of the link's target.
// A uid or gid of -1 means to not change that value.
// If there is an error, it will be of type *PathError.
func Chown(name string, uid, gid int) error {
e := ignoringEINTR(func() error {
return syscall.Chown(name, uid, gid)
})
if e != nil {
return &PathError{Op: "chown", Path: name, Err: e}
}
return nil
}

// ignoringEINTR makes a function call and repeats it if it returns an
// EINTR error. This appears to be required even though we install all
// signal handlers with SA_RESTART: see #22838, #38033, #38836, #40846.
Expand Down
37 changes: 37 additions & 0 deletions src/os/os_chmod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ package os_test
import (
. "os"
"runtime"
"syscall"
"testing"
)

func TestChmod(t *testing.T) {
// Chmod
f := newFile("TestChmod", t)
defer Remove(f.Name())
defer f.Close()
Expand All @@ -28,4 +30,39 @@ func TestChmod(t *testing.T) {
t.Fatalf("chmod %s %#o: %s", f.Name(), fm, err)
}
checkMode(t, f.Name(), fm)

}

// Since testing syscalls requires a static, predictable environment that has to be controlled
// by the CI, we don't test for success but for failures and verify that the error messages are as expected.
// EACCES is returned when the user does not have the required permissions to change the ownership of the file
// ENOENT is returned when the file does not exist
// ENOTDIR is returned when the file is not a directory
func TestChownErr(t *testing.T) {
if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
t.Skip("skipping on " + runtime.GOOS)
}

var (
TEST_UID_ROOT = 0
TEST_GID_ROOT = 0
)

f := newFile("TestChown", t)
leongross marked this conversation as resolved.
Show resolved Hide resolved
defer Remove(f.Name())
defer f.Close()

// EACCES
if err := Chown(f.Name(), TEST_UID_ROOT, TEST_GID_ROOT); err != nil {
if err.(syscall.Errno) != syscall.EPERM {
t.Fatalf("chown(%s, uid=%v, gid=%v): got '%v', want %v", f.Name(), TEST_UID_ROOT, TEST_GID_ROOT, err, syscall.EPERM)
}
}

// ENOENT
if err := Chown("invalid", Geteuid(), Getgid()); err != nil {
if err.(syscall.Errno) != syscall.ENOENT {
t.Fatalf("chown(%s, uid=%v, gid=%v): got '%v', want %v", f.Name(), Geteuid(), Getegid(), err, syscall.ENOENT)
}
}
}
14 changes: 14 additions & 0 deletions src/syscall/syscall_libc.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ func Unlink(path string) (err error) {
return
}

func Chown(path string, uid, gid int) (err error) {
data := cstring(path)
fail := int(libc_chown(&data[0], uid, gid))
if fail < 0 {
err = getErrno()
}
return
}

func Faccessat(dirfd int, path string, mode uint32, flags int) (err error)

func Kill(pid int, sig Signal) (err error) {
Expand Down Expand Up @@ -406,6 +415,11 @@ func libc_chdir(pathname *byte) int32
//export chmod
func libc_chmod(pathname *byte, mode uint32) int32

// int chown(const char *pathname, uid_t owner, gid_t group);
//
//export chown
func libc_chown(pathname *byte, owner, group int) int32

// int mkdir(const char *pathname, mode_t mode);
//
//export mkdir
Expand Down