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  	"syscall"
    15  	_ "unsafe" // for go:linkname
    16  )
    17  
    18  const _UTIME_OMIT = unix.UTIME_OMIT
    19  
    20  // fixLongPath is a noop on non-Windows platforms.
    21  func fixLongPath(path string) string {
    22  	return path
    23  }
    24  
    25  func rename(oldname, newname string) error {
    26  	fi, err := Lstat(newname)
    27  	if err == nil && fi.IsDir() {
    28  		// There are two independent errors this function can return:
    29  		// one for a bad oldname, and one for a bad newname.
    30  		// At this point we've determined the newname is bad.
    31  		// But just in case oldname is also bad, prioritize returning
    32  		// the oldname error because that's what we did historically.
    33  		// However, if the old name and new name are not the same, yet
    34  		// they refer to the same file, it implies a case-only
    35  		// rename on a case-insensitive filesystem, which is ok.
    36  		if ofi, err := Lstat(oldname); err != nil {
    37  			if pe, ok := err.(*PathError); ok {
    38  				err = pe.Err
    39  			}
    40  			return &LinkError{"rename", oldname, newname, err}
    41  		} else if newname == oldname || !SameFile(fi, ofi) {
    42  			return &LinkError{"rename", oldname, newname, syscall.EEXIST}
    43  		}
    44  	}
    45  	err = ignoringEINTR(func() error {
    46  		return syscall.Rename(oldname, newname)
    47  	})
    48  	if err != nil {
    49  		return &LinkError{"rename", oldname, newname, err}
    50  	}
    51  	return nil
    52  }
    53  
    54  // file is the real representation of *File.
    55  // The extra level of indirection ensures that no clients of os
    56  // can overwrite this data, which could cause the finalizer
    57  // to close the wrong file descriptor.
    58  type file struct {
    59  	pfd         poll.FD
    60  	name        string
    61  	dirinfo     *dirInfo // nil unless directory being read
    62  	nonblock    bool     // whether we set nonblocking mode
    63  	stdoutOrErr bool     // whether this is stdout or stderr
    64  	appendMode  bool     // whether file is opened for appending
    65  }
    66  
    67  // Fd returns the integer Unix file descriptor referencing the open file.
    68  // If f is closed, the file descriptor becomes invalid.
    69  // If f is garbage collected, a finalizer may close the file descriptor,
    70  // making it invalid; see runtime.SetFinalizer for more information on when
    71  // a finalizer might be run. On Unix systems this will cause the SetDeadline
    72  // methods to stop working.
    73  // Because file descriptors can be reused, the returned file descriptor may
    74  // only be closed through the Close method of f, or by its finalizer during
    75  // garbage collection. Otherwise, during garbage collection the finalizer
    76  // may close an unrelated file descriptor with the same (reused) number.
    77  //
    78  // As an alternative, see the f.SyscallConn method.
    79  func (f *File) Fd() uintptr {
    80  	if f == nil {
    81  		return ^(uintptr(0))
    82  	}
    83  
    84  	// If we put the file descriptor into nonblocking mode,
    85  	// then set it to blocking mode before we return it,
    86  	// because historically we have always returned a descriptor
    87  	// opened in blocking mode. The File will continue to work,
    88  	// but any blocking operation will tie up a thread.
    89  	if f.nonblock {
    90  		f.pfd.SetBlocking()
    91  	}
    92  
    93  	return uintptr(f.pfd.Sysfd)
    94  }
    95  
    96  // NewFile returns a new File with the given file descriptor and
    97  // name. The returned value will be nil if fd is not a valid file
    98  // descriptor. On Unix systems, if the file descriptor is in
    99  // non-blocking mode, NewFile will attempt to return a pollable File
   100  // (one for which the SetDeadline methods work).
   101  //
   102  // After passing it to NewFile, fd may become invalid under the same
   103  // conditions described in the comments of the Fd method, and the same
   104  // constraints apply.
   105  func NewFile(fd uintptr, name string) *File {
   106  	fdi := int(fd)
   107  	if fdi < 0 {
   108  		return nil
   109  	}
   110  
   111  	kind := kindNewFile
   112  	appendMode := false
   113  	if flags, err := unix.Fcntl(fdi, syscall.F_GETFL, 0); err == nil {
   114  		if unix.HasNonblockFlag(flags) {
   115  			kind = kindNonBlock
   116  		}
   117  		appendMode = flags&syscall.O_APPEND != 0
   118  	}
   119  	f := newFile(fdi, name, kind)
   120  	f.appendMode = appendMode
   121  	return f
   122  }
   123  
   124  // net_newUnixFile is a hidden entry point called by net.conn.File.
   125  // This is used so that a nonblocking network connection will become
   126  // blocking if code calls the Fd method. We don't want that for direct
   127  // calls to NewFile: passing a nonblocking descriptor to NewFile should
   128  // remain nonblocking if you get it back using Fd. But for net.conn.File
   129  // the call to NewFile is hidden from the user. Historically in that case
   130  // the Fd method has returned a blocking descriptor, and we want to
   131  // retain that behavior because existing code expects it and depends on it.
   132  //
   133  //go:linkname net_newUnixFile net.newUnixFile
   134  func net_newUnixFile(fd int, name string) *File {
   135  	if fd < 0 {
   136  		panic("invalid FD")
   137  	}
   138  
   139  	f := newFile(fd, name, kindNonBlock)
   140  	f.nonblock = true // tell Fd to return blocking descriptor
   141  	return f
   142  }
   143  
   144  // newFileKind describes the kind of file to newFile.
   145  type newFileKind int
   146  
   147  const (
   148  	// kindNewFile means that the descriptor was passed to us via NewFile.
   149  	kindNewFile newFileKind = iota
   150  	// kindOpenFile means that the descriptor was opened using
   151  	// Open, Create, or OpenFile (without O_NONBLOCK).
   152  	kindOpenFile
   153  	// kindPipe means that the descriptor was opened using Pipe.
   154  	kindPipe
   155  	// kindNonBlock means that the descriptor is already in
   156  	// non-blocking mode.
   157  	kindNonBlock
   158  	// kindNoPoll means that we should not put the descriptor into
   159  	// non-blocking mode, because we know it is not a pipe or FIFO.
   160  	// Used by openFdAt for directories.
   161  	kindNoPoll
   162  )
   163  
   164  // newFile is like NewFile, but if called from OpenFile or Pipe
   165  // (as passed in the kind parameter) it tries to add the file to
   166  // the runtime poller.
   167  func newFile(fd int, name string, kind newFileKind) *File {
   168  	f := &File{&file{
   169  		pfd: poll.FD{
   170  			Sysfd:         fd,
   171  			IsStream:      true,
   172  			ZeroReadIsEOF: true,
   173  		},
   174  		name:        name,
   175  		stdoutOrErr: fd == 1 || fd == 2,
   176  	}}
   177  
   178  	pollable := kind == kindOpenFile || kind == kindPipe || kind == kindNonBlock
   179  
   180  	// If the caller passed a non-blocking filedes (kindNonBlock),
   181  	// we assume they know what they are doing so we allow it to be
   182  	// used with kqueue.
   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  			// Don't try to use kqueue with regular files on *BSDs.
   192  			// On FreeBSD a regular file is always
   193  			// reported as ready for writing.
   194  			// On Dragonfly, NetBSD and OpenBSD the fd is signaled
   195  			// only once as ready (both read and write).
   196  			// Issue 19093.
   197  			// Also don't add directories to the netpoller.
   198  			if err == nil && (typ == syscall.S_IFREG || typ == syscall.S_IFDIR) {
   199  				pollable = false
   200  			}
   201  
   202  			// In addition to the behavior described above for regular files,
   203  			// on Darwin, kqueue does not work properly with fifos:
   204  			// closing the last writer does not cause a kqueue event
   205  			// for any readers. See issue #24164.
   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  		if kind == kindNonBlock {
   215  			// The descriptor is already in non-blocking mode.
   216  			// We only set f.nonblock if we put the file into
   217  			// non-blocking mode.
   218  		} else if err := syscall.SetNonblock(fd, true); err == nil {
   219  			f.nonblock = true
   220  			clearNonBlock = true
   221  		} else {
   222  			pollable = false
   223  		}
   224  	}
   225  
   226  	// An error here indicates a failure to register
   227  	// with the netpoll system. That can happen for
   228  	// a file descriptor that is not supported by
   229  	// epoll/kqueue; for example, disk files on
   230  	// Linux systems. We assume that any real error
   231  	// will show up in later I/O.
   232  	// We do restore the blocking behavior if it was set by us.
   233  	if pollErr := f.pfd.Init("file", pollable); pollErr != nil && clearNonBlock {
   234  		if err := syscall.SetNonblock(fd, false); err == nil {
   235  			f.nonblock = false
   236  		}
   237  	}
   238  
   239  	runtime.SetFinalizer(f.file, (*file).close)
   240  	return f
   241  }
   242  
   243  func sigpipe() // implemented in package runtime
   244  
   245  // epipecheck raises SIGPIPE if we get an EPIPE error on standard
   246  // output or standard error. See the SIGPIPE docs in os/signal, and
   247  // issue 11845.
   248  func epipecheck(file *File, e error) {
   249  	if e == syscall.EPIPE && file.stdoutOrErr {
   250  		sigpipe()
   251  	}
   252  }
   253  
   254  // DevNull is the name of the operating system's “null device.”
   255  // On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
   256  const DevNull = "/dev/null"
   257  
   258  // openFileNolog is the Unix implementation of OpenFile.
   259  // Changes here should be reflected in openFdAt, if relevant.
   260  func openFileNolog(name string, flag int, perm FileMode) (*File, error) {
   261  	setSticky := false
   262  	if !supportsCreateWithStickyBit && flag&O_CREATE != 0 && perm&ModeSticky != 0 {
   263  		if _, err := Stat(name); IsNotExist(err) {
   264  			setSticky = true
   265  		}
   266  	}
   267  
   268  	var r int
   269  	var s poll.SysFile
   270  	for {
   271  		var e error
   272  		r, s, e = open(name, flag|syscall.O_CLOEXEC, syscallMode(perm))
   273  		if e == nil {
   274  			break
   275  		}
   276  
   277  		// We have to check EINTR here, per issues 11180 and 39237.
   278  		if e == syscall.EINTR {
   279  			continue
   280  		}
   281  
   282  		return nil, &PathError{Op: "open", Path: name, Err: e}
   283  	}
   284  
   285  	// open(2) itself won't handle the sticky bit on *BSD and Solaris
   286  	if setSticky {
   287  		setStickyBit(name)
   288  	}
   289  
   290  	// There's a race here with fork/exec, which we are
   291  	// content to live with. See ../syscall/exec_unix.go.
   292  	if !supportsCloseOnExec {
   293  		syscall.CloseOnExec(r)
   294  	}
   295  
   296  	kind := kindOpenFile
   297  	if unix.HasNonblockFlag(flag) {
   298  		kind = kindNonBlock
   299  	}
   300  
   301  	f := newFile(r, name, kind)
   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 file.dirinfo != nil {
   311  		file.dirinfo.close()
   312  		file.dirinfo = nil
   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 f.dirinfo != 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  		f.dirinfo.close()
   336  		f.dirinfo = nil
   337  	}
   338  	ret, err = f.pfd.Seek(offset, whence)
   339  	runtime.KeepAlive(f)
   340  	return ret, err
   341  }
   342  
   343  // Truncate changes the size of the named file.
   344  // If the file is a symbolic link, it changes the size of the link's target.
   345  // If there is an error, it will be of type *PathError.
   346  func Truncate(name string, size int64) error {
   347  	e := ignoringEINTR(func() error {
   348  		return syscall.Truncate(name, size)
   349  	})
   350  	if e != nil {
   351  		return &PathError{Op: "truncate", Path: name, Err: e}
   352  	}
   353  	return nil
   354  }
   355  
   356  // Remove removes the named file or (empty) directory.
   357  // If there is an error, it will be of type *PathError.
   358  func Remove(name string) error {
   359  	// System call interface forces us to know
   360  	// whether name is a file or directory.
   361  	// Try both: it is cheaper on average than
   362  	// doing a Stat plus the right one.
   363  	e := ignoringEINTR(func() error {
   364  		return syscall.Unlink(name)
   365  	})
   366  	if e == nil {
   367  		return nil
   368  	}
   369  	e1 := ignoringEINTR(func() error {
   370  		return syscall.Rmdir(name)
   371  	})
   372  	if e1 == nil {
   373  		return nil
   374  	}
   375  
   376  	// Both failed: figure out which error to return.
   377  	// OS X and Linux differ on whether unlink(dir)
   378  	// returns EISDIR, so can't use that. However,
   379  	// both agree that rmdir(file) returns ENOTDIR,
   380  	// so we can use that to decide which error is real.
   381  	// Rmdir might also return ENOTDIR if given a bad
   382  	// file path, like /etc/passwd/foo, but in that case,
   383  	// both errors will be ENOTDIR, so it's okay to
   384  	// use the error from unlink.
   385  	if e1 != syscall.ENOTDIR {
   386  		e = e1
   387  	}
   388  	return &PathError{Op: "remove", Path: name, Err: e}
   389  }
   390  
   391  func tempDir() string {
   392  	dir := Getenv("TMPDIR")
   393  	if dir == "" {
   394  		if runtime.GOOS == "android" {
   395  			dir = "/data/local/tmp"
   396  		} else {
   397  			dir = "/tmp"
   398  		}
   399  	}
   400  	return dir
   401  }
   402  
   403  // Link creates newname as a hard link to the oldname file.
   404  // If there is an error, it will be of type *LinkError.
   405  func Link(oldname, newname string) error {
   406  	e := ignoringEINTR(func() error {
   407  		return syscall.Link(oldname, newname)
   408  	})
   409  	if e != nil {
   410  		return &LinkError{"link", oldname, newname, e}
   411  	}
   412  	return nil
   413  }
   414  
   415  // Symlink creates newname as a symbolic link to oldname.
   416  // On Windows, a symlink to a non-existent oldname creates a file symlink;
   417  // if oldname is later created as a directory the symlink will not work.
   418  // If there is an error, it will be of type *LinkError.
   419  func Symlink(oldname, newname string) error {
   420  	e := ignoringEINTR(func() error {
   421  		return syscall.Symlink(oldname, newname)
   422  	})
   423  	if e != nil {
   424  		return &LinkError{"symlink", oldname, newname, e}
   425  	}
   426  	return nil
   427  }
   428  
   429  func readlink(name string) (string, error) {
   430  	for len := 128; ; len *= 2 {
   431  		b := make([]byte, len)
   432  		var (
   433  			n int
   434  			e error
   435  		)
   436  		for {
   437  			n, e = fixCount(syscall.Readlink(name, b))
   438  			if e != syscall.EINTR {
   439  				break
   440  			}
   441  		}
   442  		// buffer too small
   443  		if (runtime.GOOS == "aix" || runtime.GOOS == "wasip1") && e == syscall.ERANGE {
   444  			continue
   445  		}
   446  		if e != nil {
   447  			return "", &PathError{Op: "readlink", Path: name, Err: e}
   448  		}
   449  		if n < len {
   450  			return string(b[0:n]), nil
   451  		}
   452  	}
   453  }
   454  
   455  type unixDirent struct {
   456  	parent string
   457  	name   string
   458  	typ    FileMode
   459  	info   FileInfo
   460  }
   461  
   462  func (d *unixDirent) Name() string   { return d.name }
   463  func (d *unixDirent) IsDir() bool    { return d.typ.IsDir() }
   464  func (d *unixDirent) Type() FileMode { return d.typ }
   465  
   466  func (d *unixDirent) Info() (FileInfo, error) {
   467  	if d.info != nil {
   468  		return d.info, nil
   469  	}
   470  	return lstat(d.parent + "/" + d.name)
   471  }
   472  
   473  func (d *unixDirent) String() string {
   474  	return fs.FormatDirEntry(d)
   475  }
   476  
   477  func newUnixDirent(parent, name string, typ FileMode) (DirEntry, error) {
   478  	ude := &unixDirent{
   479  		parent: parent,
   480  		name:   name,
   481  		typ:    typ,
   482  	}
   483  	if typ != ^FileMode(0) && !testingForceReadDirLstat {
   484  		return ude, nil
   485  	}
   486  
   487  	info, err := lstat(parent + "/" + name)
   488  	if err != nil {
   489  		return nil, err
   490  	}
   491  
   492  	ude.typ = info.Mode().Type()
   493  	ude.info = info
   494  	return ude, nil
   495  }
   496  

View as plain text