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