Skip to content

Commit

Permalink
os: use FileMode instead of uint32 in various functions
Browse files Browse the repository at this point in the history
Fixes #2733

R=chickencha, ality, rsc
CC=golang-dev
https://golang.org/cl/5553064
  • Loading branch information
bradfitz committed Jan 19, 2012
1 parent ab2ea94 commit 6454a3e
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/cmd/go/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ func (b *builder) build(a *action) error {
// install is the action for installing a single package or executable.
func (b *builder) install(a *action) error {
a1 := a.deps[0]
perm := uint32(0666)
perm := os.FileMode(0666)
if a1.link {
perm = 0777
}
Expand Down Expand Up @@ -697,7 +697,7 @@ func removeByRenaming(name string) error {
}

// copyFile is like 'cp src dst'.
func (b *builder) copyFile(dst, src string, perm uint32) error {
func (b *builder) copyFile(dst, src string, perm os.FileMode) error {
if buildN || buildX {
b.showcmd("", "cp %s %s", src, dst)
if buildN {
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/hgpatch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func main() {
changed[o.Dst] = 1
}
if o.Mode != 0 {
chk(os.Chmod(o.Dst, uint32(o.Mode&0755)))
chk(os.Chmod(o.Dst, os.FileMode(o.Mode&0755)))
undoRevert(o.Dst)
changed[o.Dst] = 1
}
Expand Down Expand Up @@ -189,7 +189,7 @@ func makeParent(name string) {

// Copy of os.MkdirAll but adds to undo log after
// creating a directory.
func mkdirAll(path string, perm uint32) error {
func mkdirAll(path string, perm os.FileMode) error {
dir, err := os.Lstat(path)
if err == nil {
if dir.IsDir() {
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/io/ioutil/ioutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func ReadFile(filename string) ([]byte, error) {
// WriteFile writes data to a file named by filename.
// If the file does not exist, WriteFile creates it with permissions perm;
// otherwise WriteFile truncates it before writing.
func WriteFile(filename string, data []byte, perm uint32) error {
func WriteFile(filename string, data []byte, perm os.FileMode) error {
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/os/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ func (f *File) WriteString(s string) (ret int, err error) {

// Mkdir creates a new directory with the specified name and permission bits.
// It returns an error, if any.
func Mkdir(name string, perm uint32) error {
e := syscall.Mkdir(name, perm)
func Mkdir(name string, perm FileMode) error {
e := syscall.Mkdir(name, syscallMode(perm))
if e != nil {
return &PathError{"mkdir", name, e}
}
Expand Down
24 changes: 20 additions & 4 deletions src/pkg/os/file_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,34 @@ func Rename(oldname, newname string) error {
return nil
}

// syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
func syscallMode(i FileMode) (o uint32) {
o |= uint32(i.Perm())
if i&ModeSetuid != 0 {
o |= syscall.S_ISUID
}
if i&ModeSetgid != 0 {
o |= syscall.S_ISGID
}
if i&ModeSticky != 0 {
o |= syscall.S_ISVTX
}
// No mapping for Go's ModeTemporary (plan9 only).
return
}

// Chmod changes the mode of the named file to mode.
// If the file is a symbolic link, it changes the mode of the link's target.
func Chmod(name string, mode uint32) error {
if e := syscall.Chmod(name, mode); e != nil {
func Chmod(name string, mode FileMode) error {
if e := syscall.Chmod(name, syscallMode(mode)); e != nil {
return &PathError{"chmod", name, e}
}
return nil
}

// Chmod changes the mode of the file to mode.
func (f *File) Chmod(mode uint32) error {
if e := syscall.Fchmod(f.fd, mode); e != nil {
func (f *File) Chmod(mode FileMode) error {
if e := syscall.Fchmod(f.fd, syscallMode(mode)); e != nil {
return &PathError{"chmod", f.name, e}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/os/file_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ const DevNull = "/dev/null"
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
// methods on the returned File can be used for I/O.
// It returns the File and an error, if any.
func OpenFile(name string, flag int, perm uint32) (file *File, err error) {
r, e := syscall.Open(name, flag|syscall.O_CLOEXEC, perm)
func OpenFile(name string, flag int, perm FileMode) (file *File, err error) {
r, e := syscall.Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm))
if e != nil {
return nil, &PathError{"open", name, e}
}
Expand Down
6 changes: 3 additions & 3 deletions src/pkg/os/file_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ const DevNull = "NUL"

func (f *file) isdir() bool { return f != nil && f.dirinfo != nil }

func openFile(name string, flag int, perm uint32) (file *File, err error) {
r, e := syscall.Open(name, flag|syscall.O_CLOEXEC, perm)
func openFile(name string, flag int, perm FileMode) (file *File, err error) {
r, e := syscall.Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm))
if e != nil {
return nil, &PathError{"open", name, e}
}
Expand Down Expand Up @@ -88,7 +88,7 @@ func openDir(name string) (file *File, err error) {
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
// methods on the returned File can be used for I/O.
// It returns the File and an error, if any.
func OpenFile(name string, flag int, perm uint32) (file *File, err error) {
func OpenFile(name string, flag int, perm FileMode) (file *File, err error) {
if name == "" {
return nil, &PathError{"open", name, syscall.ENOENT}
}
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/os/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import "io"
// directories that MkdirAll creates.
// If path is already a directory, MkdirAll does nothing
// and returns nil.
func MkdirAll(path string, perm uint32) error {
func MkdirAll(path string, perm FileMode) error {
// If path exists, stop with success or error.
dir, err := Stat(path)
if err == nil {
Expand Down

0 comments on commit 6454a3e

Please sign in to comment.