Source file src/os/file_unix.go

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

View as plain text