...
Source file
src/os/file_posix.go
Documentation: os
1
2
3
4
5
6
7 package os
8
9 import (
10 "runtime"
11 "syscall"
12 "time"
13 )
14
15 func sigpipe()
16
17
18
19
20
21 func (f *File) Close() error {
22 if f == nil {
23 return ErrInvalid
24 }
25 return f.file.close()
26 }
27
28
29
30 func (f *File) read(b []byte) (n int, err error) {
31 n, err = f.pfd.Read(b)
32 runtime.KeepAlive(f)
33 return n, err
34 }
35
36
37
38
39 func (f *File) pread(b []byte, off int64) (n int, err error) {
40 n, err = f.pfd.Pread(b, off)
41 runtime.KeepAlive(f)
42 return n, err
43 }
44
45
46
47 func (f *File) write(b []byte) (n int, err error) {
48 n, err = f.pfd.Write(b)
49 runtime.KeepAlive(f)
50 return n, err
51 }
52
53
54
55 func (f *File) pwrite(b []byte, off int64) (n int, err error) {
56 n, err = f.pfd.Pwrite(b, off)
57 runtime.KeepAlive(f)
58 return n, err
59 }
60
61
62 func syscallMode(i FileMode) (o uint32) {
63 o |= uint32(i.Perm())
64 if i&ModeSetuid != 0 {
65 o |= syscall.S_ISUID
66 }
67 if i&ModeSetgid != 0 {
68 o |= syscall.S_ISGID
69 }
70 if i&ModeSticky != 0 {
71 o |= syscall.S_ISVTX
72 }
73
74 return
75 }
76
77
78 func chmod(name string, mode FileMode) error {
79 longName := fixLongPath(name)
80 e := ignoringEINTR(func() error {
81 return syscall.Chmod(longName, syscallMode(mode))
82 })
83 if e != nil {
84 return &PathError{Op: "chmod", Path: name, Err: e}
85 }
86 return nil
87 }
88
89
90 func (f *File) chmod(mode FileMode) error {
91 if err := f.checkValid("chmod"); err != nil {
92 return err
93 }
94 if e := f.pfd.Fchmod(syscallMode(mode)); e != nil {
95 return f.wrapErr("chmod", e)
96 }
97 return nil
98 }
99
100
101
102
103
104
105
106
107 func Chown(name string, uid, gid int) error {
108 e := ignoringEINTR(func() error {
109 return syscall.Chown(name, uid, gid)
110 })
111 if e != nil {
112 return &PathError{Op: "chown", Path: name, Err: e}
113 }
114 return nil
115 }
116
117
118
119
120
121
122
123 func Lchown(name string, uid, gid int) error {
124 e := ignoringEINTR(func() error {
125 return syscall.Lchown(name, uid, gid)
126 })
127 if e != nil {
128 return &PathError{Op: "lchown", Path: name, Err: e}
129 }
130 return nil
131 }
132
133
134
135
136
137
138 func (f *File) Chown(uid, gid int) error {
139 if err := f.checkValid("chown"); err != nil {
140 return err
141 }
142 if e := f.pfd.Fchown(uid, gid); e != nil {
143 return f.wrapErr("chown", e)
144 }
145 return nil
146 }
147
148
149
150
151 func (f *File) Truncate(size int64) error {
152 if err := f.checkValid("truncate"); err != nil {
153 return err
154 }
155 if e := f.pfd.Ftruncate(size); e != nil {
156 return f.wrapErr("truncate", e)
157 }
158 return nil
159 }
160
161
162
163
164 func (f *File) Sync() error {
165 if err := f.checkValid("sync"); err != nil {
166 return err
167 }
168 if e := f.pfd.Fsync(); e != nil {
169 return f.wrapErr("sync", e)
170 }
171 return nil
172 }
173
174
175
176
177
178
179
180 func Chtimes(name string, atime time.Time, mtime time.Time) error {
181 var utimes [2]syscall.Timespec
182 utimes[0] = syscall.NsecToTimespec(atime.UnixNano())
183 utimes[1] = syscall.NsecToTimespec(mtime.UnixNano())
184 if e := syscall.UtimesNano(fixLongPath(name), utimes[0:]); e != nil {
185 return &PathError{Op: "chtimes", Path: name, Err: e}
186 }
187 return nil
188 }
189
190
191
192
193 func (f *File) Chdir() error {
194 if err := f.checkValid("chdir"); err != nil {
195 return err
196 }
197 if e := f.pfd.Fchdir(); e != nil {
198 return f.wrapErr("chdir", e)
199 }
200 return nil
201 }
202
203
204 func (f *File) setDeadline(t time.Time) error {
205 if err := f.checkValid("SetDeadline"); err != nil {
206 return err
207 }
208 return f.pfd.SetDeadline(t)
209 }
210
211
212 func (f *File) setReadDeadline(t time.Time) error {
213 if err := f.checkValid("SetReadDeadline"); err != nil {
214 return err
215 }
216 return f.pfd.SetReadDeadline(t)
217 }
218
219
220 func (f *File) setWriteDeadline(t time.Time) error {
221 if err := f.checkValid("SetWriteDeadline"); err != nil {
222 return err
223 }
224 return f.pfd.SetWriteDeadline(t)
225 }
226
227
228
229 func (f *File) checkValid(op string) error {
230 if f == nil {
231 return ErrInvalid
232 }
233 return nil
234 }
235
236
237
238
239
240
241
242
243 func ignoringEINTR(fn func() error) error {
244 for {
245 err := fn()
246 if err != syscall.EINTR {
247 return err
248 }
249 }
250 }
251
View as plain text