[1/7] Preprocessor cleanup
[official-gcc.git] / libgo / go / os / file_posix.go
blob9bd1cc7b98ee247ab3178fe0fca734d367d3c6f2
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 js,wasm 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 // See docs in file.go:Chmod.
52 func chmod(name string, mode FileMode) error {
53 if e := syscall.Chmod(fixLongPath(name), syscallMode(mode)); e != nil {
54 return &PathError{"chmod", name, e}
56 return nil
59 // See docs in file.go:(*File).Chmod.
60 func (f *File) chmod(mode FileMode) error {
61 if err := f.checkValid("chmod"); err != nil {
62 return err
64 if e := f.pfd.Fchmod(syscallMode(mode)); e != nil {
65 return f.wrapErr("chmod", e)
67 return nil
70 // Chown changes the numeric uid and gid of the named file.
71 // If the file is a symbolic link, it changes the uid and gid of the link's target.
72 // A uid or gid of -1 means to not change that value.
73 // If there is an error, it will be of type *PathError.
75 // On Windows or Plan 9, Chown always returns the syscall.EWINDOWS or
76 // EPLAN9 error, wrapped in *PathError.
77 func Chown(name string, uid, gid int) error {
78 if e := syscall.Chown(name, uid, gid); e != nil {
79 return &PathError{"chown", name, e}
81 return nil
84 // Lchown changes the numeric uid and gid of the named file.
85 // If the file is a symbolic link, it changes the uid and gid of the link itself.
86 // If there is an error, it will be of type *PathError.
88 // On Windows, it always returns the syscall.EWINDOWS error, wrapped
89 // in *PathError.
90 func Lchown(name string, uid, gid int) error {
91 if e := syscall.Lchown(name, uid, gid); e != nil {
92 return &PathError{"lchown", name, e}
94 return nil
97 // Chown changes the numeric uid and gid of the named file.
98 // If there is an error, it will be of type *PathError.
100 // On Windows, it always returns the syscall.EWINDOWS error, wrapped
101 // in *PathError.
102 func (f *File) Chown(uid, gid int) error {
103 if err := f.checkValid("chown"); err != nil {
104 return err
106 if e := f.pfd.Fchown(uid, gid); e != nil {
107 return f.wrapErr("chown", e)
109 return nil
112 // Truncate changes the size of the file.
113 // It does not change the I/O offset.
114 // If there is an error, it will be of type *PathError.
115 func (f *File) Truncate(size int64) error {
116 if err := f.checkValid("truncate"); err != nil {
117 return err
119 if e := f.pfd.Ftruncate(size); e != nil {
120 return f.wrapErr("truncate", e)
122 return nil
125 // Sync commits the current contents of the file to stable storage.
126 // Typically, this means flushing the file system's in-memory copy
127 // of recently written data to disk.
128 func (f *File) Sync() error {
129 if err := f.checkValid("sync"); err != nil {
130 return err
132 if e := f.pfd.Fsync(); e != nil {
133 return f.wrapErr("sync", e)
135 return nil
138 // Chtimes changes the access and modification times of the named
139 // file, similar to the Unix utime() or utimes() functions.
141 // The underlying filesystem may truncate or round the values to a
142 // less precise time unit.
143 // If there is an error, it will be of type *PathError.
144 func Chtimes(name string, atime time.Time, mtime time.Time) error {
145 var utimes [2]syscall.Timespec
146 utimes[0] = syscall.NsecToTimespec(atime.UnixNano())
147 utimes[1] = syscall.NsecToTimespec(mtime.UnixNano())
148 if e := syscall.UtimesNano(fixLongPath(name), utimes[0:]); e != nil {
149 return &PathError{"chtimes", name, e}
151 return nil
154 // Chdir changes the current working directory to the file,
155 // which must be a directory.
156 // If there is an error, it will be of type *PathError.
157 func (f *File) Chdir() error {
158 if err := f.checkValid("chdir"); err != nil {
159 return err
161 if e := f.pfd.Fchdir(); e != nil {
162 return f.wrapErr("chdir", e)
164 return nil
167 // setDeadline sets the read and write deadline.
168 func (f *File) setDeadline(t time.Time) error {
169 if err := f.checkValid("SetDeadline"); err != nil {
170 return err
172 return f.pfd.SetDeadline(t)
175 // setReadDeadline sets the read deadline.
176 func (f *File) setReadDeadline(t time.Time) error {
177 if err := f.checkValid("SetReadDeadline"); err != nil {
178 return err
180 return f.pfd.SetReadDeadline(t)
183 // setWriteDeadline sets the write deadline.
184 func (f *File) setWriteDeadline(t time.Time) error {
185 if err := f.checkValid("SetWriteDeadline"); err != nil {
186 return err
188 return f.pfd.SetWriteDeadline(t)
191 // checkValid checks whether f is valid for use.
192 // If not, it returns an appropriate error, perhaps incorporating the operation name op.
193 func (f *File) checkValid(op string) error {
194 if f == nil {
195 return ErrInvalid
197 return nil