Source file
src/os/file_unix.go
1
2
3
4
5
6
7 package os
8
9 import (
10 "internal/poll"
11 "internal/syscall/unix"
12 "runtime"
13 "syscall"
14 )
15
16
17 func fixLongPath(path string) string {
18 return path
19 }
20
21 func rename(oldname, newname string) error {
22 fi, err := Lstat(newname)
23 if err == nil && fi.IsDir() {
24
25
26
27
28
29
30
31
32 if ofi, err := Lstat(oldname); err != nil {
33 if pe, ok := err.(*PathError); ok {
34 err = pe.Err
35 }
36 return &LinkError{"rename", oldname, newname, err}
37 } else if newname == oldname || !SameFile(fi, ofi) {
38 return &LinkError{"rename", oldname, newname, syscall.EEXIST}
39 }
40 }
41 err = ignoringEINTR(func() error {
42 return syscall.Rename(oldname, newname)
43 })
44 if err != nil {
45 return &LinkError{"rename", oldname, newname, err}
46 }
47 return nil
48 }
49
50
51
52
53
54 type file struct {
55 pfd poll.FD
56 name string
57 dirinfo *dirInfo
58 nonblock bool
59 stdoutOrErr bool
60 appendMode bool
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75 func (f *File) Fd() uintptr {
76 if f == nil {
77 return ^(uintptr(0))
78 }
79
80
81
82
83
84
85 if f.nonblock {
86 f.pfd.SetBlocking()
87 }
88
89 return uintptr(f.pfd.Sysfd)
90 }
91
92
93
94
95
96
97
98
99
100
101 func NewFile(fd uintptr, name string) *File {
102 kind := kindNewFile
103 if nb, err := unix.IsNonblock(int(fd)); err == nil && nb {
104 kind = kindNonBlock
105 }
106 return newFile(fd, name, kind)
107 }
108
109
110 type newFileKind int
111
112 const (
113 kindNewFile newFileKind = iota
114 kindOpenFile
115 kindPipe
116 kindNonBlock
117 )
118
119
120
121
122 func newFile(fd uintptr, name string, kind newFileKind) *File {
123 fdi := int(fd)
124 if fdi < 0 {
125 return nil
126 }
127 f := &File{&file{
128 pfd: poll.FD{
129 Sysfd: fdi,
130 IsStream: true,
131 ZeroReadIsEOF: true,
132 },
133 name: name,
134 stdoutOrErr: fdi == 1 || fdi == 2,
135 }}
136
137 pollable := kind == kindOpenFile || kind == kindPipe || kind == kindNonBlock
138
139
140
141
142 if kind == kindOpenFile {
143 switch runtime.GOOS {
144 case "darwin", "ios", "dragonfly", "freebsd", "netbsd", "openbsd":
145 var st syscall.Stat_t
146 err := ignoringEINTR(func() error {
147 return syscall.Fstat(fdi, &st)
148 })
149 typ := st.Mode & syscall.S_IFMT
150
151
152
153
154
155
156
157 if err == nil && (typ == syscall.S_IFREG || typ == syscall.S_IFDIR) {
158 pollable = false
159 }
160
161
162
163
164
165 if (runtime.GOOS == "darwin" || runtime.GOOS == "ios") && typ == syscall.S_IFIFO {
166 pollable = false
167 }
168 }
169 }
170
171 if err := f.pfd.Init("file", pollable); err != nil {
172
173
174
175
176
177
178 } else if pollable {
179
180
181 if err := syscall.SetNonblock(fdi, true); err == nil {
182 f.nonblock = true
183 }
184 }
185
186 runtime.SetFinalizer(f.file, (*file).close)
187 return f
188 }
189
190
191
192
193 func epipecheck(file *File, e error) {
194 if e == syscall.EPIPE && file.stdoutOrErr {
195 sigpipe()
196 }
197 }
198
199
200
201 const DevNull = "/dev/null"
202
203
204
205 func openFileNolog(name string, flag int, perm FileMode) (*File, error) {
206 setSticky := false
207 if !supportsCreateWithStickyBit && flag&O_CREATE != 0 && perm&ModeSticky != 0 {
208 if _, err := Stat(name); IsNotExist(err) {
209 setSticky = true
210 }
211 }
212
213 var r int
214 for {
215 var e error
216 r, e = syscall.Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm))
217 if e == nil {
218 break
219 }
220
221
222 if e == syscall.EINTR {
223 continue
224 }
225
226 return nil, &PathError{Op: "open", Path: name, Err: e}
227 }
228
229
230 if setSticky {
231 setStickyBit(name)
232 }
233
234
235
236 if !supportsCloseOnExec {
237 syscall.CloseOnExec(r)
238 }
239
240 return newFile(uintptr(r), name, kindOpenFile), nil
241 }
242
243 func (file *file) close() error {
244 if file == nil {
245 return syscall.EINVAL
246 }
247 if file.dirinfo != nil {
248 file.dirinfo.close()
249 file.dirinfo = nil
250 }
251 var err error
252 if e := file.pfd.Close(); e != nil {
253 if e == poll.ErrFileClosing {
254 e = ErrClosed
255 }
256 err = &PathError{Op: "close", Path: file.name, Err: e}
257 }
258
259
260 runtime.SetFinalizer(file, nil)
261 return err
262 }
263
264
265
266
267
268 func (f *File) seek(offset int64, whence int) (ret int64, err error) {
269 if f.dirinfo != nil {
270
271
272 f.dirinfo.close()
273 f.dirinfo = nil
274 }
275 ret, err = f.pfd.Seek(offset, whence)
276 runtime.KeepAlive(f)
277 return ret, err
278 }
279
280
281
282
283 func Truncate(name string, size int64) error {
284 e := ignoringEINTR(func() error {
285 return syscall.Truncate(name, size)
286 })
287 if e != nil {
288 return &PathError{Op: "truncate", Path: name, Err: e}
289 }
290 return nil
291 }
292
293
294
295 func Remove(name string) error {
296
297
298
299
300 e := ignoringEINTR(func() error {
301 return syscall.Unlink(name)
302 })
303 if e == nil {
304 return nil
305 }
306 e1 := ignoringEINTR(func() error {
307 return syscall.Rmdir(name)
308 })
309 if e1 == nil {
310 return nil
311 }
312
313
314
315
316
317
318
319
320
321
322 if e1 != syscall.ENOTDIR {
323 e = e1
324 }
325 return &PathError{Op: "remove", Path: name, Err: e}
326 }
327
328 func tempDir() string {
329 dir := Getenv("TMPDIR")
330 if dir == "" {
331 if runtime.GOOS == "android" {
332 dir = "/data/local/tmp"
333 } else {
334 dir = "/tmp"
335 }
336 }
337 return dir
338 }
339
340
341
342 func Link(oldname, newname string) error {
343 e := ignoringEINTR(func() error {
344 return syscall.Link(oldname, newname)
345 })
346 if e != nil {
347 return &LinkError{"link", oldname, newname, e}
348 }
349 return nil
350 }
351
352
353
354
355
356 func Symlink(oldname, newname string) error {
357 e := ignoringEINTR(func() error {
358 return syscall.Symlink(oldname, newname)
359 })
360 if e != nil {
361 return &LinkError{"symlink", oldname, newname, e}
362 }
363 return nil
364 }
365
366
367
368 func Readlink(name string) (string, error) {
369 for len := 128; ; len *= 2 {
370 b := make([]byte, len)
371 var (
372 n int
373 e error
374 )
375 for {
376 n, e = fixCount(syscall.Readlink(name, b))
377 if e != syscall.EINTR {
378 break
379 }
380 }
381
382 if runtime.GOOS == "aix" && e == syscall.ERANGE {
383 continue
384 }
385 if e != nil {
386 return "", &PathError{Op: "readlink", Path: name, Err: e}
387 }
388 if n < len {
389 return string(b[0:n]), nil
390 }
391 }
392 }
393
394 type unixDirent struct {
395 parent string
396 name string
397 typ FileMode
398 info FileInfo
399 }
400
401 func (d *unixDirent) Name() string { return d.name }
402 func (d *unixDirent) IsDir() bool { return d.typ.IsDir() }
403 func (d *unixDirent) Type() FileMode { return d.typ }
404
405 func (d *unixDirent) Info() (FileInfo, error) {
406 if d.info != nil {
407 return d.info, nil
408 }
409 return lstat(d.parent + "/" + d.name)
410 }
411
412 func newUnixDirent(parent, name string, typ FileMode) (DirEntry, error) {
413 ude := &unixDirent{
414 parent: parent,
415 name: name,
416 typ: typ,
417 }
418 if typ != ^FileMode(0) && !testingForceReadDirLstat {
419 return ude, nil
420 }
421
422 info, err := lstat(parent + "/" + name)
423 if err != nil {
424 return nil, err
425 }
426
427 ude.typ = info.Mode().Type()
428 ude.info = info
429 return ude, nil
430 }
431
View as plain text