]> Cypherpunks repositories - gostls13.git/commitdiff
os: use FileMode instead of uint32 in various functions
authorBrad Fitzpatrick <bradfitz@golang.org>
Thu, 19 Jan 2012 23:45:18 +0000 (15:45 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 19 Jan 2012 23:45:18 +0000 (15:45 -0800)
Fixes #2733

R=chickencha, ality, rsc
CC=golang-dev
https://golang.org/cl/5553064

src/cmd/go/build.go
src/cmd/hgpatch/main.go
src/pkg/io/ioutil/ioutil.go
src/pkg/os/file.go
src/pkg/os/file_posix.go
src/pkg/os/file_unix.go
src/pkg/os/file_windows.go
src/pkg/os/path.go

index 77a64f406e316a10f85e31baa86a176ffbeb6ce2..23eacf970ec3e09fcdf037c8da6280a9c7915a61 100644 (file)
@@ -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
        }
@@ -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 {
index 05dc61eb957ab56307a6b36fc7e6d0e78b117898..1ecfa972df1ce929f56e9c706def238f8a2e0dda 100644 (file)
@@ -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
                }
@@ -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() {
index be7fa5f2bc8bc8da61afe778c4f2308251d998d5..65f4b3ac2e57725f2b08c89472823df1f4a8792d 100644 (file)
@@ -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
index 71845d3c9c93d833d25be13136acae0acf90e7cb..3efa650c6575666ac2aed54a6418a3ebced76005 100644 (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.
 // 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}
        }
index 8231ef4817c96ee5ccd4edccacc184ce34700ebd..86ac1cab2cbf4e370c7733dcd95ebb2df79256db 100644 (file)
@@ -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
index 01b72358f299b342e8544deaa9ccb49bc9368e8b..ae5e908339e6bfde540ba3a9dd66238b172bb544 100644 (file)
@@ -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}
        }
index f5aaca06e51431313be238f8c1aa96fa482dae71..7d39fdd2cd9b828e6063658d95039bb023cc8232 100644 (file)
@@ -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}
        }
@@ -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}
        }
index bc14a78318811d8d9e5678c0f1c6f5517b9209f6..e962f3e397b39d4d82b50cbab57d212ba0b76611 100644 (file)
@@ -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 {