Debian Codegraph @

src/os/file_unix.go

raw
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build unix || (js && wasm) || wasip1
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" // for go:linkname
17 )
18
19 const _UTIME_OMIT = unix.UTIME_OMIT
20
21 // fixLongPath is a noop on non-Windows platforms.
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 // There are two independent errors this function can return:
30 // one for a bad oldname, and one for a bad newname.
31 // At this point we've determined the newname is bad.
32 // But just in case oldname is also bad, prioritize returning
33 // the oldname error because that's what we did historically.
34 // However, if the old name and new name are not the same, yet
35 // they refer to the same file, it implies a case-only
36 // rename on a case-insensitive filesystem, which is ok.
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 // file is the real representation of *File.
56 // The extra level of indirection ensures that no clients of os
57 // can overwrite this data, which could cause the finalizer
58 // to close the wrong file descriptor.
59 type file struct {
60 pfd poll.FD
61 name string
62 dirinfo atomic.Pointer[dirInfo] // nil unless directory being read
63 nonblock bool // whether we set nonblocking mode
64 stdoutOrErr bool // whether this is stdout or stderr
65 appendMode bool // whether file is opened for appending
66 inRoot bool // whether file is opened in a Root
67 }
68
69 // fd is the Unix implementation of Fd.
70 func (f *File) fd() uintptr {
71 if f == nil {
72 return ^(uintptr(0))
73 }
74
75 // If we put the file descriptor into nonblocking mode,
76 // then set it to blocking mode before we return it,
77 // because historically we have always returned a descriptor
78 // opened in blocking mode. The File will continue to work,
79 // but any blocking operation will tie up a thread.
80 if f.nonblock {
81 f.pfd.SetBlocking()
82 }
83
84 return uintptr(f.pfd.Sysfd)
85 }
86
87 // newFileFromNewFile is called by [NewFile].
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 // net_newUnixFile is a hidden entry point called by net.conn.File.
104 // This is used so that a nonblocking network connection will become
105 // blocking if code calls the Fd method. We don't want that for direct
106 // calls to NewFile: passing a nonblocking descriptor to NewFile should
107 // remain nonblocking if you get it back using Fd. But for net.conn.File
108 // the call to NewFile is hidden from the user. Historically in that case
109 // the Fd method has returned a blocking descriptor, and we want to
110 // retain that behavior because existing code expects it and depends on it.
111 //
112 //go:linkname net_newUnixFile net.newUnixFile
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 // newFileKind describes the kind of file to newFile.
122 type newFileKind int
123
124 const (
125 // kindNewFile means that the descriptor was passed to us via NewFile.
126 kindNewFile newFileKind = iota
127 // kindOpenFile means that the descriptor was opened using
128 // Open, Create, or OpenFile.
129 kindOpenFile
130 // kindPipe means that the descriptor was opened using Pipe.
131 kindPipe
132 // kindSock means that the descriptor is a network file descriptor
133 // that was created from net package and was opened using net_newUnixFile.
134 kindSock
135 // kindNoPoll means that we should not put the descriptor into
136 // non-blocking mode, because we know it is not a pipe or FIFO.
137 // Used by openDirAt and openDirNolog for directories.
138 kindNoPoll
139 )
140
141 // newFile is like NewFile, but if called from OpenFile or Pipe
142 // (as passed in the kind parameter) it tries to add the file to
143 // the runtime poller.
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 // Things like regular files and FIFOs in kqueue on *BSD/Darwin
158 // may not work properly (or accurately according to its manual).
159 // As a result, we should avoid adding those to the kqueue-based
160 // netpoller. Check out #19093, #24164, and #66239 for more contexts.
161 //
162 // If the fd was passed to us via any path other than OpenFile,
163 // we assume those callers know what they were doing, so we won't
164 // perform this check and allow it to be added to the kqueue.
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 // Don't try to use kqueue with regular files on *BSDs.
174 // On FreeBSD a regular file is always
175 // reported as ready for writing.
176 // On Dragonfly, NetBSD and OpenBSD the fd is signaled
177 // only once as ready (both read and write).
178 // Issue 19093.
179 // Also don't add directories to the netpoller.
180 if err == nil && (typ == syscall.S_IFREG || typ == syscall.S_IFDIR) {
181 pollable = false
182 }
183
184 // In addition to the behavior described above for regular files,
185 // on Darwin, kqueue does not work properly with fifos:
186 // closing the last writer does not cause a kqueue event
187 // for any readers. See issue #24164.
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 // The descriptor is already in non-blocking mode.
197 // We only set f.nonblock if we put the file into
198 // non-blocking mode.
199 if nonBlocking {
200 // See the comments on net_newUnixFile.
201 if kind == kindSock {
202 f.nonblock = true // tell Fd to return blocking descriptor
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 // An error here indicates a failure to register
213 // with the netpoll system. That can happen for
214 // a file descriptor that is not supported by
215 // epoll/kqueue; for example, disk files on
216 // Linux systems. We assume that any real error
217 // will show up in later I/O.
218 // We do restore the blocking behavior if it was set by us.
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 runtime.SetFinalizer(f.file, (*file).close)
226 return f
227 }
228
229 func sigpipe() // implemented in package runtime
230
231 // epipecheck raises SIGPIPE if we get an EPIPE error on standard
232 // output or standard error. See the SIGPIPE docs in os/signal, and
233 // issue 11845.
234 func epipecheck(file *File, e error) {
235 if e == syscall.EPIPE && file.stdoutOrErr {
236 sigpipe()
237 }
238 }
239
240 // DevNull is the name of the operating system's “null device.”
241 // On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
242 const DevNull = "/dev/null"
243
244 // openFileNolog is the Unix implementation of OpenFile.
245 // Changes here should be reflected in openDirAt and openDirNolog, if relevant.
246 func openFileNolog(name string, flag int, perm FileMode) (*File, error) {
247 setSticky := false
248 if !supportsCreateWithStickyBit && flag&O_CREATE != 0 && perm&ModeSticky != 0 {
249 if _, err := Stat(name); IsNotExist(err) {
250 setSticky = true
251 }
252 }
253
254 var (
255 r int
256 s poll.SysFile
257 e error
258 )
259 // We have to check EINTR here, per issues 11180 and 39237.
260 ignoringEINTR(func() error {
261 r, s, e = open(name, flag|syscall.O_CLOEXEC, syscallMode(perm))
262 return e
263 })
264 if e != nil {
265 return nil, &PathError{Op: "open", Path: name, Err: e}
266 }
267
268 // open(2) itself won't handle the sticky bit on *BSD and Solaris
269 if setSticky {
270 setStickyBit(name)
271 }
272
273 // There's a race here with fork/exec, which we are
274 // content to live with. See ../syscall/exec_unix.go.
275 if !supportsCloseOnExec {
276 syscall.CloseOnExec(r)
277 }
278
279 f := newFile(r, name, kindOpenFile, unix.HasNonblockFlag(flag))
280 f.pfd.SysFile = s
281 return f, nil
282 }
283
284 func openDirNolog(name string) (*File, error) {
285 var (
286 r int
287 s poll.SysFile
288 e error
289 )
290 ignoringEINTR(func() error {
291 r, s, e = open(name, O_RDONLY|syscall.O_CLOEXEC|syscall.O_DIRECTORY, 0)
292 return e
293 })
294 if e != nil {
295 return nil, &PathError{Op: "open", Path: name, Err: e}
296 }
297
298 if !supportsCloseOnExec {
299 syscall.CloseOnExec(r)
300 }
301
302 f := newFile(r, name, kindNoPoll, false)
303 f.pfd.SysFile = s
304 return f, nil
305 }
306
307 func (file *file) close() error {
308 if file == nil {
309 return syscall.EINVAL
310 }
311 if info := file.dirinfo.Swap(nil); info != nil {
312 info.close()
313 }
314 var err error
315 if e := file.pfd.Close(); e != nil {
316 if e == poll.ErrFileClosing {
317 e = ErrClosed
318 }
319 err = &PathError{Op: "close", Path: file.name, Err: e}
320 }
321
322 // no need for a finalizer anymore
323 runtime.SetFinalizer(file, nil)
324 return err
325 }
326
327 // seek sets the offset for the next Read or Write on file to offset, interpreted
328 // according to whence: 0 means relative to the origin of the file, 1 means
329 // relative to the current offset, and 2 means relative to the end.
330 // It returns the new offset and an error, if any.
331 func (f *File) seek(offset int64, whence int) (ret int64, err error) {
332 if info := f.dirinfo.Swap(nil); info != nil {
333 // Free cached dirinfo, so we allocate a new one if we
334 // access this file as a directory again. See #35767 and #37161.
335 info.close()
336 }
337 ret, err = f.pfd.Seek(offset, whence)
338 runtime.KeepAlive(f)
339 return ret, err
340 }
341
342 // Truncate changes the size of the named file.
343 // If the file is a symbolic link, it changes the size of the link's target.
344 // If there is an error, it will be of type [*PathError].
345 func Truncate(name string, size int64) error {
346 e := ignoringEINTR(func() error {
347 return syscall.Truncate(name, size)
348 })
349 if e != nil {
350 return &PathError{Op: "truncate", Path: name, Err: e}
351 }
352 return nil
353 }
354
355 // Remove removes the named file or (empty) directory.
356 // If there is an error, it will be of type [*PathError].
357 func Remove(name string) error {
358 // System call interface forces us to know
359 // whether name is a file or directory.
360 // Try both: it is cheaper on average than
361 // doing a Stat plus the right one.
362 e := ignoringEINTR(func() error {
363 return syscall.Unlink(name)
364 })
365 if e == nil {
366 return nil
367 }
368 e1 := ignoringEINTR(func() error {
369 return syscall.Rmdir(name)
370 })
371 if e1 == nil {
372 return nil
373 }
374
375 // Both failed: figure out which error to return.
376 // OS X and Linux differ on whether unlink(dir)
377 // returns EISDIR, so can't use that. However,
378 // both agree that rmdir(file) returns ENOTDIR,
379 // so we can use that to decide which error is real.
380 // Rmdir might also return ENOTDIR if given a bad
381 // file path, like /etc/passwd/foo, but in that case,
382 // both errors will be ENOTDIR, so it's okay to
383 // use the error from unlink.
384 if e1 != syscall.ENOTDIR {
385 e = e1
386 }
387 return &PathError{Op: "remove", Path: name, Err: e}
388 }
389
390 func tempDir() string {
391 dir := Getenv("TMPDIR")
392 if dir == "" {
393 if runtime.GOOS == "android" {
394 dir = "/data/local/tmp"
395 } else {
396 dir = "/tmp"
397 }
398 }
399 return dir
400 }
401
402 // Link creates newname as a hard link to the oldname file.
403 // If there is an error, it will be of type *LinkError.
404 func Link(oldname, newname string) error {
405 e := ignoringEINTR(func() error {
406 return syscall.Link(oldname, newname)
407 })
408 if e != nil {
409 return &LinkError{"link", oldname, newname, e}
410 }
411 return nil
412 }
413
414 // Symlink creates newname as a symbolic link to oldname.
415 // On Windows, a symlink to a non-existent oldname creates a file symlink;
416 // if oldname is later created as a directory the symlink will not work.
417 // If there is an error, it will be of type *LinkError.
418 func Symlink(oldname, newname string) error {
419 e := ignoringEINTR(func() error {
420 return syscall.Symlink(oldname, newname)
421 })
422 if e != nil {
423 return &LinkError{"symlink", oldname, newname, e}
424 }
425 return nil
426 }
427
428 func readlink(name string) (string, error) {
429 for len := 128; ; len *= 2 {
430 b := make([]byte, len)
431 n, err := ignoringEINTR2(func() (int, error) {
432 return fixCount(syscall.Readlink(name, b))
433 })
434 // buffer too small
435 if (runtime.GOOS == "aix" || runtime.GOOS == "wasip1") && err == syscall.ERANGE {
436 continue
437 }
438 if err != nil {
439 return "", &PathError{Op: "readlink", Path: name, Err: err}
440 }
441 if n < len {
442 return string(b[0:n]), nil
443 }
444 }
445 }
446
447 type unixDirent struct {
448 parent string
449 name string
450 typ FileMode
451 info FileInfo
452 }
453
454 func (d *unixDirent) Name() string { return d.name }
455 func (d *unixDirent) IsDir() bool { return d.typ.IsDir() }
456 func (d *unixDirent) Type() FileMode { return d.typ }
457
458 func (d *unixDirent) Info() (FileInfo, error) {
459 if d.info != nil {
460 return d.info, nil
461 }
462 return Lstat(d.parent + "/" + d.name)
463 }
464
465 func (d *unixDirent) String() string {
466 return fs.FormatDirEntry(d)
467 }
468
469 func newUnixDirent(parent *File, name string, typ FileMode) (DirEntry, error) {
470 ude := &unixDirent{
471 parent: parent.name,
472 name: name,
473 typ: typ,
474 }
475 // When the parent file was opened in a Root,
476 // we cannot use a lazy lstat to load the FileInfo.
477 // Use lstatat here.
478 if typ != ^FileMode(0) && !parent.inRoot {
479 return ude, nil
480 }
481
482 info, err := parent.lstatat(name)
483 if err != nil {
484 return nil, err
485 }
486
487 ude.typ = info.Mode().Type()
488 ude.info = info
489 return ude, nil
490 }