1
0
mirror of https://github.com/golang/go synced 2024-11-19 21:04:43 -07:00

os: use FileMode instead of uint32 in various functions

Fixes #2733

R=chickencha, ality, rsc
CC=golang-dev
https://golang.org/cl/5553064
This commit is contained in:
Brad Fitzpatrick 2012-01-19 15:45:18 -08:00
parent ab2ea94c60
commit 6454a3eb15
8 changed files with 33 additions and 17 deletions

View File

@ -618,7 +618,7 @@ func (b *builder) build(a *action) error {
// install is the action for installing a single package or executable. // install is the action for installing a single package or executable.
func (b *builder) install(a *action) error { func (b *builder) install(a *action) error {
a1 := a.deps[0] a1 := a.deps[0]
perm := uint32(0666) perm := os.FileMode(0666)
if a1.link { if a1.link {
perm = 0777 perm = 0777
} }
@ -697,7 +697,7 @@ func removeByRenaming(name string) error {
} }
// copyFile is like 'cp src dst'. // 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 { if buildN || buildX {
b.showcmd("", "cp %s %s", src, dst) b.showcmd("", "cp %s %s", src, dst)
if buildN { if buildN {

View File

@ -151,7 +151,7 @@ func main() {
changed[o.Dst] = 1 changed[o.Dst] = 1
} }
if o.Mode != 0 { 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) undoRevert(o.Dst)
changed[o.Dst] = 1 changed[o.Dst] = 1
} }
@ -189,7 +189,7 @@ func makeParent(name string) {
// Copy of os.MkdirAll but adds to undo log after // Copy of os.MkdirAll but adds to undo log after
// creating a directory. // creating a directory.
func mkdirAll(path string, perm uint32) error { func mkdirAll(path string, perm os.FileMode) error {
dir, err := os.Lstat(path) dir, err := os.Lstat(path)
if err == nil { if err == nil {
if dir.IsDir() { if dir.IsDir() {

View File

@ -50,7 +50,7 @@ func ReadFile(filename string) ([]byte, error) {
// WriteFile writes data to a file named by filename. // WriteFile writes data to a file named by filename.
// If the file does not exist, WriteFile creates it with permissions perm; // If the file does not exist, WriteFile creates it with permissions perm;
// otherwise WriteFile truncates it before writing. // 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) f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
if err != nil { if err != nil {
return err return err

View File

@ -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. // Mkdir creates a new directory with the specified name and permission bits.
// It returns an error, if any. // It returns an error, if any.
func Mkdir(name string, perm uint32) error { func Mkdir(name string, perm FileMode) error {
e := syscall.Mkdir(name, perm) e := syscall.Mkdir(name, syscallMode(perm))
if e != nil { if e != nil {
return &PathError{"mkdir", name, e} return &PathError{"mkdir", name, e}
} }

View File

@ -81,18 +81,34 @@ func Rename(oldname, newname string) error {
return nil 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. // 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. // If the file is a symbolic link, it changes the mode of the link's target.
func Chmod(name string, mode uint32) error { func Chmod(name string, mode FileMode) error {
if e := syscall.Chmod(name, mode); e != nil { if e := syscall.Chmod(name, syscallMode(mode)); e != nil {
return &PathError{"chmod", name, e} return &PathError{"chmod", name, e}
} }
return nil return nil
} }
// Chmod changes the mode of the file to mode. // Chmod changes the mode of the file to mode.
func (f *File) Chmod(mode uint32) error { func (f *File) Chmod(mode FileMode) error {
if e := syscall.Fchmod(f.fd, mode); e != nil { if e := syscall.Fchmod(f.fd, syscallMode(mode)); e != nil {
return &PathError{"chmod", f.name, e} return &PathError{"chmod", f.name, e}
} }
return nil return nil

View File

@ -61,8 +61,8 @@ const DevNull = "/dev/null"
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
// methods on the returned File can be used for I/O. // methods on the returned File can be used for I/O.
// It returns the File and an error, if any. // 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) {
r, e := syscall.Open(name, flag|syscall.O_CLOEXEC, perm) r, e := syscall.Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm))
if e != nil { if e != nil {
return nil, &PathError{"open", name, e} return nil, &PathError{"open", name, e}
} }

View File

@ -57,8 +57,8 @@ const DevNull = "NUL"
func (f *file) isdir() bool { return f != nil && f.dirinfo != nil } func (f *file) isdir() bool { return f != nil && f.dirinfo != nil }
func openFile(name string, flag int, perm uint32) (file *File, err error) { func openFile(name string, flag int, perm FileMode) (file *File, err error) {
r, e := syscall.Open(name, flag|syscall.O_CLOEXEC, perm) r, e := syscall.Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm))
if e != nil { if e != nil {
return nil, &PathError{"open", name, e} return nil, &PathError{"open", name, e}
} }
@ -88,7 +88,7 @@ func openDir(name string) (file *File, err error) {
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
// methods on the returned File can be used for I/O. // methods on the returned File can be used for I/O.
// It returns the File and an error, if any. // 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 == "" { if name == "" {
return nil, &PathError{"open", name, syscall.ENOENT} return nil, &PathError{"open", name, syscall.ENOENT}
} }

View File

@ -13,7 +13,7 @@ import "io"
// directories that MkdirAll creates. // directories that MkdirAll creates.
// If path is already a directory, MkdirAll does nothing // If path is already a directory, MkdirAll does nothing
// and returns nil. // 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. // If path exists, stop with success or error.
dir, err := Stat(path) dir, err := Stat(path)
if err == nil { if err == nil {