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