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