* bb-reorder.c (better_edge_p): Fix handling of uninitialized
[official-gcc.git] / libgo / go / os / file_posix.go
blob6634112efce6cafb2d7b41c1247aa949fabcfe0a
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // +build aix darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
7 package os
9 import (
10 "syscall"
11 "time"
14 func sigpipe() // implemented in package runtime
16 // Readlink returns the destination of the named symbolic link.
17 // If there is an error, it will be of type *PathError.
18 func Readlink(name string) (string, error) {
19 for len := 128; ; len *= 2 {
20 b := make([]byte, len)
21 n, e := fixCount(syscall.Readlink(fixLongPath(name), b))
22 // buffer too small
23 if e == syscall.ERANGE {
24 continue
26 if e != nil {
27 return "", &PathError{"readlink", name, e}
29 if n < len {
30 return string(b[0:n]), nil
35 // syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
36 func syscallMode(i FileMode) (o uint32) {
37 o |= uint32(i.Perm())
38 if i&ModeSetuid != 0 {
39 o |= syscall.S_ISUID
41 if i&ModeSetgid != 0 {
42 o |= syscall.S_ISGID
44 if i&ModeSticky != 0 {
45 o |= syscall.S_ISVTX
47 // No mapping for Go's ModeTemporary (plan9 only).
48 return
51 // Chmod changes the mode of the named file to mode.
52 // If the file is a symbolic link, it changes the mode of the link's target.
53 // If there is an error, it will be of type *PathError.
54 func Chmod(name string, mode FileMode) error {
55 if e := syscall.Chmod(name, syscallMode(mode)); e != nil {
56 return &PathError{"chmod", name, e}
58 return nil
61 // Chmod changes the mode of the file to mode.
62 // If there is an error, it will be of type *PathError.
63 func (f *File) Chmod(mode FileMode) error {
64 if err := f.checkValid("chmod"); err != nil {
65 return err
67 if e := syscall.Fchmod(f.fd, syscallMode(mode)); e != nil {
68 return &PathError{"chmod", f.name, e}
70 return nil
73 // Chown changes the numeric uid and gid of the named file.
74 // If the file is a symbolic link, it changes the uid and gid of the link's target.
75 // If there is an error, it will be of type *PathError.
76 func Chown(name string, uid, gid int) error {
77 if e := syscall.Chown(name, uid, gid); e != nil {
78 return &PathError{"chown", name, e}
80 return nil
83 // Lchown changes the numeric uid and gid of the named file.
84 // If the file is a symbolic link, it changes the uid and gid of the link itself.
85 // If there is an error, it will be of type *PathError.
86 func Lchown(name string, uid, gid int) error {
87 if e := syscall.Lchown(name, uid, gid); e != nil {
88 return &PathError{"lchown", name, e}
90 return nil
93 // Chown changes the numeric uid and gid of the named file.
94 // If there is an error, it will be of type *PathError.
95 func (f *File) Chown(uid, gid int) error {
96 if err := f.checkValid("chown"); err != nil {
97 return err
99 if e := syscall.Fchown(f.fd, uid, gid); e != nil {
100 return &PathError{"chown", f.name, e}
102 return nil
105 // Truncate changes the size of the file.
106 // It does not change the I/O offset.
107 // If there is an error, it will be of type *PathError.
108 func (f *File) Truncate(size int64) error {
109 if err := f.checkValid("truncate"); err != nil {
110 return err
112 if e := syscall.Ftruncate(f.fd, size); e != nil {
113 return &PathError{"truncate", f.name, e}
115 return nil
118 // Sync commits the current contents of the file to stable storage.
119 // Typically, this means flushing the file system's in-memory copy
120 // of recently written data to disk.
121 func (f *File) Sync() error {
122 if err := f.checkValid("sync"); err != nil {
123 return err
125 if e := syscall.Fsync(f.fd); e != nil {
126 return &PathError{"sync", f.name, e}
128 return nil
131 // Chtimes changes the access and modification times of the named
132 // file, similar to the Unix utime() or utimes() functions.
134 // The underlying filesystem may truncate or round the values to a
135 // less precise time unit.
136 // If there is an error, it will be of type *PathError.
137 func Chtimes(name string, atime time.Time, mtime time.Time) error {
138 var utimes [2]syscall.Timespec
139 utimes[0] = syscall.NsecToTimespec(atime.UnixNano())
140 utimes[1] = syscall.NsecToTimespec(mtime.UnixNano())
141 if e := syscall.UtimesNano(fixLongPath(name), utimes[0:]); e != nil {
142 return &PathError{"chtimes", name, e}
144 return nil