Black Lives Matter. Support the Equal Justice Initiative.

Source file src/os/file.go

Documentation: os

     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  // Package os provides a platform-independent interface to operating system
     6  // functionality. The design is Unix-like, although the error handling is
     7  // Go-like; failing calls return values of type error rather than error numbers.
     8  // Often, more information is available within the error. For example,
     9  // if a call that takes a file name fails, such as Open or Stat, the error
    10  // will include the failing file name when printed and will be of type
    11  // *PathError, which may be unpacked for more information.
    12  //
    13  // The os interface is intended to be uniform across all operating systems.
    14  // Features not generally available appear in the system-specific package syscall.
    15  //
    16  // Here is a simple example, opening a file and reading some of it.
    17  //
    18  //	file, err := os.Open("file.go") // For read access.
    19  //	if err != nil {
    20  //		log.Fatal(err)
    21  //	}
    22  //
    23  // If the open fails, the error string will be self-explanatory, like
    24  //
    25  //	open file.go: no such file or directory
    26  //
    27  // The file's data can then be read into a slice of bytes. Read and
    28  // Write take their byte counts from the length of the argument slice.
    29  //
    30  //	data := make([]byte, 100)
    31  //	count, err := file.Read(data)
    32  //	if err != nil {
    33  //		log.Fatal(err)
    34  //	}
    35  //	fmt.Printf("read %d bytes: %q\n", count, data[:count])
    36  //
    37  // Note: The maximum number of concurrent operations on a File may be limited by
    38  // the OS or the system. The number should be high, but exceeding it may degrade
    39  // performance or cause other issues.
    40  //
    41  package os
    42  
    43  import (
    44  	"errors"
    45  	"internal/poll"
    46  	"internal/testlog"
    47  	"io"
    48  	"runtime"
    49  	"syscall"
    50  	"time"
    51  )
    52  
    53  // Name returns the name of the file as presented to Open.
    54  func (f *File) Name() string { return f.name }
    55  
    56  // Stdin, Stdout, and Stderr are open Files pointing to the standard input,
    57  // standard output, and standard error file descriptors.
    58  //
    59  // Note that the Go runtime writes to standard error for panics and crashes;
    60  // closing Stderr may cause those messages to go elsewhere, perhaps
    61  // to a file opened later.
    62  var (
    63  	Stdin  = NewFile(uintptr(syscall.Stdin), "/dev/stdin")
    64  	Stdout = NewFile(uintptr(syscall.Stdout), "/dev/stdout")
    65  	Stderr = NewFile(uintptr(syscall.Stderr), "/dev/stderr")
    66  )
    67  
    68  // Flags to OpenFile wrapping those of the underlying system. Not all
    69  // flags may be implemented on a given system.
    70  const (
    71  	// Exactly one of O_RDONLY, O_WRONLY, or O_RDWR must be specified.
    72  	O_RDONLY int = syscall.O_RDONLY // open the file read-only.
    73  	O_WRONLY int = syscall.O_WRONLY // open the file write-only.
    74  	O_RDWR   int = syscall.O_RDWR   // open the file read-write.
    75  	// The remaining values may be or'ed in to control behavior.
    76  	O_APPEND int = syscall.O_APPEND // append data to the file when writing.
    77  	O_CREATE int = syscall.O_CREAT  // create a new file if none exists.
    78  	O_EXCL   int = syscall.O_EXCL   // used with O_CREATE, file must not exist.
    79  	O_SYNC   int = syscall.O_SYNC   // open for synchronous I/O.
    80  	O_TRUNC  int = syscall.O_TRUNC  // truncate regular writable file when opened.
    81  )
    82  
    83  // Seek whence values.
    84  //
    85  // Deprecated: Use io.SeekStart, io.SeekCurrent, and io.SeekEnd.
    86  const (
    87  	SEEK_SET int = 0 // seek relative to the origin of the file
    88  	SEEK_CUR int = 1 // seek relative to the current offset
    89  	SEEK_END int = 2 // seek relative to the end
    90  )
    91  
    92  // LinkError records an error during a link or symlink or rename
    93  // system call and the paths that caused it.
    94  type LinkError struct {
    95  	Op  string
    96  	Old string
    97  	New string
    98  	Err error
    99  }
   100  
   101  func (e *LinkError) Error() string {
   102  	return e.Op + " " + e.Old + " " + e.New + ": " + e.Err.Error()
   103  }
   104  
   105  func (e *LinkError) Unwrap() error {
   106  	return e.Err
   107  }
   108  
   109  // Read reads up to len(b) bytes from the File.
   110  // It returns the number of bytes read and any error encountered.
   111  // At end of file, Read returns 0, io.EOF.
   112  func (f *File) Read(b []byte) (n int, err error) {
   113  	if err := f.checkValid("read"); err != nil {
   114  		return 0, err
   115  	}
   116  	n, e := f.read(b)
   117  	return n, f.wrapErr("read", e)
   118  }
   119  
   120  // ReadAt reads len(b) bytes from the File starting at byte offset off.
   121  // It returns the number of bytes read and the error, if any.
   122  // ReadAt always returns a non-nil error when n < len(b).
   123  // At end of file, that error is io.EOF.
   124  func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
   125  	if err := f.checkValid("read"); err != nil {
   126  		return 0, err
   127  	}
   128  
   129  	if off < 0 {
   130  		return 0, &PathError{"readat", f.name, errors.New("negative offset")}
   131  	}
   132  
   133  	for len(b) > 0 {
   134  		m, e := f.pread(b, off)
   135  		if e != nil {
   136  			err = f.wrapErr("read", e)
   137  			break
   138  		}
   139  		n += m
   140  		b = b[m:]
   141  		off += int64(m)
   142  	}
   143  	return
   144  }
   145  
   146  // ReadFrom implements io.ReaderFrom.
   147  func (f *File) ReadFrom(r io.Reader) (n int64, err error) {
   148  	if err := f.checkValid("write"); err != nil {
   149  		return 0, err
   150  	}
   151  	n, handled, e := f.readFrom(r)
   152  	if !handled {
   153  		return genericReadFrom(f, r) // without wrapping
   154  	}
   155  	return n, f.wrapErr("write", e)
   156  }
   157  
   158  func genericReadFrom(f *File, r io.Reader) (int64, error) {
   159  	return io.Copy(onlyWriter{f}, r)
   160  }
   161  
   162  type onlyWriter struct {
   163  	io.Writer
   164  }
   165  
   166  // Write writes len(b) bytes to the File.
   167  // It returns the number of bytes written and an error, if any.
   168  // Write returns a non-nil error when n != len(b).
   169  func (f *File) Write(b []byte) (n int, err error) {
   170  	if err := f.checkValid("write"); err != nil {
   171  		return 0, err
   172  	}
   173  	n, e := f.write(b)
   174  	if n < 0 {
   175  		n = 0
   176  	}
   177  	if n != len(b) {
   178  		err = io.ErrShortWrite
   179  	}
   180  
   181  	epipecheck(f, e)
   182  
   183  	if e != nil {
   184  		err = f.wrapErr("write", e)
   185  	}
   186  
   187  	return n, err
   188  }
   189  
   190  var errWriteAtInAppendMode = errors.New("os: invalid use of WriteAt on file opened with O_APPEND")
   191  
   192  // WriteAt writes len(b) bytes to the File starting at byte offset off.
   193  // It returns the number of bytes written and an error, if any.
   194  // WriteAt returns a non-nil error when n != len(b).
   195  //
   196  // If file was opened with the O_APPEND flag, WriteAt returns an error.
   197  func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
   198  	if err := f.checkValid("write"); err != nil {
   199  		return 0, err
   200  	}
   201  	if f.appendMode {
   202  		return 0, errWriteAtInAppendMode
   203  	}
   204  
   205  	if off < 0 {
   206  		return 0, &PathError{"writeat", f.name, errors.New("negative offset")}
   207  	}
   208  
   209  	for len(b) > 0 {
   210  		m, e := f.pwrite(b, off)
   211  		if e != nil {
   212  			err = f.wrapErr("write", e)
   213  			break
   214  		}
   215  		n += m
   216  		b = b[m:]
   217  		off += int64(m)
   218  	}
   219  	return
   220  }
   221  
   222  // Seek sets the offset for the next Read or Write on file to offset, interpreted
   223  // according to whence: 0 means relative to the origin of the file, 1 means
   224  // relative to the current offset, and 2 means relative to the end.
   225  // It returns the new offset and an error, if any.
   226  // The behavior of Seek on a file opened with O_APPEND is not specified.
   227  //
   228  // If f is a directory, the behavior of Seek varies by operating
   229  // system; you can seek to the beginning of the directory on Unix-like
   230  // operating systems, but not on Windows.
   231  func (f *File) Seek(offset int64, whence int) (ret int64, err error) {
   232  	if err := f.checkValid("seek"); err != nil {
   233  		return 0, err
   234  	}
   235  	r, e := f.seek(offset, whence)
   236  	if e == nil && f.dirinfo != nil && r != 0 {
   237  		e = syscall.EISDIR
   238  	}
   239  	if e != nil {
   240  		return 0, f.wrapErr("seek", e)
   241  	}
   242  	return r, nil
   243  }
   244  
   245  // WriteString is like Write, but writes the contents of string s rather than
   246  // a slice of bytes.
   247  func (f *File) WriteString(s string) (n int, err error) {
   248  	return f.Write([]byte(s))
   249  }
   250  
   251  // Mkdir creates a new directory with the specified name and permission
   252  // bits (before umask).
   253  // If there is an error, it will be of type *PathError.
   254  func Mkdir(name string, perm FileMode) error {
   255  	if runtime.GOOS == "windows" && isWindowsNulName(name) {
   256  		return &PathError{"mkdir", name, syscall.ENOTDIR}
   257  	}
   258  	e := syscall.Mkdir(fixLongPath(name), syscallMode(perm))
   259  
   260  	if e != nil {
   261  		return &PathError{"mkdir", name, e}
   262  	}
   263  
   264  	// mkdir(2) itself won't handle the sticky bit on *BSD and Solaris
   265  	if !supportsCreateWithStickyBit && perm&ModeSticky != 0 {
   266  		e = setStickyBit(name)
   267  
   268  		if e != nil {
   269  			Remove(name)
   270  			return e
   271  		}
   272  	}
   273  
   274  	return nil
   275  }
   276  
   277  // setStickyBit adds ModeSticky to the permission bits of path, non atomic.
   278  func setStickyBit(name string) error {
   279  	fi, err := Stat(name)
   280  	if err != nil {
   281  		return err
   282  	}
   283  	return Chmod(name, fi.Mode()|ModeSticky)
   284  }
   285  
   286  // Chdir changes the current working directory to the named directory.
   287  // If there is an error, it will be of type *PathError.
   288  func Chdir(dir string) error {
   289  	if e := syscall.Chdir(dir); e != nil {
   290  		testlog.Open(dir) // observe likely non-existent directory
   291  		return &PathError{"chdir", dir, e}
   292  	}
   293  	if log := testlog.Logger(); log != nil {
   294  		wd, err := Getwd()
   295  		if err == nil {
   296  			log.Chdir(wd)
   297  		}
   298  	}
   299  	return nil
   300  }
   301  
   302  // Open opens the named file for reading. If successful, methods on
   303  // the returned file can be used for reading; the associated file
   304  // descriptor has mode O_RDONLY.
   305  // If there is an error, it will be of type *PathError.
   306  func Open(name string) (*File, error) {
   307  	return OpenFile(name, O_RDONLY, 0)
   308  }
   309  
   310  // Create creates or truncates the named file. If the file already exists,
   311  // it is truncated. If the file does not exist, it is created with mode 0666
   312  // (before umask). If successful, methods on the returned File can
   313  // be used for I/O; the associated file descriptor has mode O_RDWR.
   314  // If there is an error, it will be of type *PathError.
   315  func Create(name string) (*File, error) {
   316  	return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666)
   317  }
   318  
   319  // OpenFile is the generalized open call; most users will use Open
   320  // or Create instead. It opens the named file with specified flag
   321  // (O_RDONLY etc.). If the file does not exist, and the O_CREATE flag
   322  // is passed, it is created with mode perm (before umask). If successful,
   323  // methods on the returned File can be used for I/O.
   324  // If there is an error, it will be of type *PathError.
   325  func OpenFile(name string, flag int, perm FileMode) (*File, error) {
   326  	testlog.Open(name)
   327  	f, err := openFileNolog(name, flag, perm)
   328  	if err != nil {
   329  		return nil, err
   330  	}
   331  	f.appendMode = flag&O_APPEND != 0
   332  
   333  	return f, nil
   334  }
   335  
   336  // lstat is overridden in tests.
   337  var lstat = Lstat
   338  
   339  // Rename renames (moves) oldpath to newpath.
   340  // If newpath already exists and is not a directory, Rename replaces it.
   341  // OS-specific restrictions may apply when oldpath and newpath are in different directories.
   342  // If there is an error, it will be of type *LinkError.
   343  func Rename(oldpath, newpath string) error {
   344  	return rename(oldpath, newpath)
   345  }
   346  
   347  // Many functions in package syscall return a count of -1 instead of 0.
   348  // Using fixCount(call()) instead of call() corrects the count.
   349  func fixCount(n int, err error) (int, error) {
   350  	if n < 0 {
   351  		n = 0
   352  	}
   353  	return n, err
   354  }
   355  
   356  // wrapErr wraps an error that occurred during an operation on an open file.
   357  // It passes io.EOF through unchanged, otherwise converts
   358  // poll.ErrFileClosing to ErrClosed and wraps the error in a PathError.
   359  func (f *File) wrapErr(op string, err error) error {
   360  	if err == nil || err == io.EOF {
   361  		return err
   362  	}
   363  	if err == poll.ErrFileClosing {
   364  		err = ErrClosed
   365  	}
   366  	return &PathError{op, f.name, err}
   367  }
   368  
   369  // TempDir returns the default directory to use for temporary files.
   370  //
   371  // On Unix systems, it returns $TMPDIR if non-empty, else /tmp.
   372  // On Windows, it uses GetTempPath, returning the first non-empty
   373  // value from %TMP%, %TEMP%, %USERPROFILE%, or the Windows directory.
   374  // On Plan 9, it returns /tmp.
   375  //
   376  // The directory is neither guaranteed to exist nor have accessible
   377  // permissions.
   378  func TempDir() string {
   379  	return tempDir()
   380  }
   381  
   382  // UserCacheDir returns the default root directory to use for user-specific
   383  // cached data. Users should create their own application-specific subdirectory
   384  // within this one and use that.
   385  //
   386  // On Unix systems, it returns $XDG_CACHE_HOME as specified by
   387  // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html if
   388  // non-empty, else $HOME/.cache.
   389  // On Darwin, it returns $HOME/Library/Caches.
   390  // On Windows, it returns %LocalAppData%.
   391  // On Plan 9, it returns $home/lib/cache.
   392  //
   393  // If the location cannot be determined (for example, $HOME is not defined),
   394  // then it will return an error.
   395  func UserCacheDir() (string, error) {
   396  	var dir string
   397  
   398  	switch runtime.GOOS {
   399  	case "windows":
   400  		dir = Getenv("LocalAppData")
   401  		if dir == "" {
   402  			return "", errors.New("%LocalAppData% is not defined")
   403  		}
   404  
   405  	case "darwin":
   406  		dir = Getenv("HOME")
   407  		if dir == "" {
   408  			return "", errors.New("$HOME is not defined")
   409  		}
   410  		dir += "/Library/Caches"
   411  
   412  	case "plan9":
   413  		dir = Getenv("home")
   414  		if dir == "" {
   415  			return "", errors.New("$home is not defined")
   416  		}
   417  		dir += "/lib/cache"
   418  
   419  	default: // Unix
   420  		dir = Getenv("XDG_CACHE_HOME")
   421  		if dir == "" {
   422  			dir = Getenv("HOME")
   423  			if dir == "" {
   424  				return "", errors.New("neither $XDG_CACHE_HOME nor $HOME are defined")
   425  			}
   426  			dir += "/.cache"
   427  		}
   428  	}
   429  
   430  	return dir, nil
   431  }
   432  
   433  // UserConfigDir returns the default root directory to use for user-specific
   434  // configuration data. Users should create their own application-specific
   435  // subdirectory within this one and use that.
   436  //
   437  // On Unix systems, it returns $XDG_CONFIG_HOME as specified by
   438  // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html if
   439  // non-empty, else $HOME/.config.
   440  // On Darwin, it returns $HOME/Library/Application Support.
   441  // On Windows, it returns %AppData%.
   442  // On Plan 9, it returns $home/lib.
   443  //
   444  // If the location cannot be determined (for example, $HOME is not defined),
   445  // then it will return an error.
   446  func UserConfigDir() (string, error) {
   447  	var dir string
   448  
   449  	switch runtime.GOOS {
   450  	case "windows":
   451  		dir = Getenv("AppData")
   452  		if dir == "" {
   453  			return "", errors.New("%AppData% is not defined")
   454  		}
   455  
   456  	case "darwin":
   457  		dir = Getenv("HOME")
   458  		if dir == "" {
   459  			return "", errors.New("$HOME is not defined")
   460  		}
   461  		dir += "/Library/Application Support"
   462  
   463  	case "plan9":
   464  		dir = Getenv("home")
   465  		if dir == "" {
   466  			return "", errors.New("$home is not defined")
   467  		}
   468  		dir += "/lib"
   469  
   470  	default: // Unix
   471  		dir = Getenv("XDG_CONFIG_HOME")
   472  		if dir == "" {
   473  			dir = Getenv("HOME")
   474  			if dir == "" {
   475  				return "", errors.New("neither $XDG_CONFIG_HOME nor $HOME are defined")
   476  			}
   477  			dir += "/.config"
   478  		}
   479  	}
   480  
   481  	return dir, nil
   482  }
   483  
   484  // UserHomeDir returns the current user's home directory.
   485  //
   486  // On Unix, including macOS, it returns the $HOME environment variable.
   487  // On Windows, it returns %USERPROFILE%.
   488  // On Plan 9, it returns the $home environment variable.
   489  func UserHomeDir() (string, error) {
   490  	env, enverr := "HOME", "$HOME"
   491  	switch runtime.GOOS {
   492  	case "windows":
   493  		env, enverr = "USERPROFILE", "%userprofile%"
   494  	case "plan9":
   495  		env, enverr = "home", "$home"
   496  	}
   497  	if v := Getenv(env); v != "" {
   498  		return v, nil
   499  	}
   500  	// On some geese the home directory is not always defined.
   501  	switch runtime.GOOS {
   502  	case "android":
   503  		return "/sdcard", nil
   504  	case "darwin":
   505  		if runtime.GOARCH == "arm64" {
   506  			return "/", nil
   507  		}
   508  	}
   509  	return "", errors.New(enverr + " is not defined")
   510  }
   511  
   512  // Chmod changes the mode of the named file to mode.
   513  // If the file is a symbolic link, it changes the mode of the link's target.
   514  // If there is an error, it will be of type *PathError.
   515  //
   516  // A different subset of the mode bits are used, depending on the
   517  // operating system.
   518  //
   519  // On Unix, the mode's permission bits, ModeSetuid, ModeSetgid, and
   520  // ModeSticky are used.
   521  //
   522  // On Windows, only the 0200 bit (owner writable) of mode is used; it
   523  // controls whether the file's read-only attribute is set or cleared.
   524  // The other bits are currently unused. For compatibility with Go 1.12
   525  // and earlier, use a non-zero mode. Use mode 0400 for a read-only
   526  // file and 0600 for a readable+writable file.
   527  //
   528  // On Plan 9, the mode's permission bits, ModeAppend, ModeExclusive,
   529  // and ModeTemporary are used.
   530  func Chmod(name string, mode FileMode) error { return chmod(name, mode) }
   531  
   532  // Chmod changes the mode of the file to mode.
   533  // If there is an error, it will be of type *PathError.
   534  func (f *File) Chmod(mode FileMode) error { return f.chmod(mode) }
   535  
   536  // SetDeadline sets the read and write deadlines for a File.
   537  // It is equivalent to calling both SetReadDeadline and SetWriteDeadline.
   538  //
   539  // Only some kinds of files support setting a deadline. Calls to SetDeadline
   540  // for files that do not support deadlines will return ErrNoDeadline.
   541  // On most systems ordinary files do not support deadlines, but pipes do.
   542  //
   543  // A deadline is an absolute time after which I/O operations fail with an
   544  // error instead of blocking. The deadline applies to all future and pending
   545  // I/O, not just the immediately following call to Read or Write.
   546  // After a deadline has been exceeded, the connection can be refreshed
   547  // by setting a deadline in the future.
   548  //
   549  // If the deadline is exceeded a call to Read or Write or to other I/O
   550  // methods will return an error that wraps ErrDeadlineExceeded.
   551  // This can be tested using errors.Is(err, os.ErrDeadlineExceeded).
   552  // That error implements the Timeout method, and calling the Timeout
   553  // method will return true, but there are other possible errors for which
   554  // the Timeout will return true even if the deadline has not been exceeded.
   555  //
   556  // An idle timeout can be implemented by repeatedly extending
   557  // the deadline after successful Read or Write calls.
   558  //
   559  // A zero value for t means I/O operations will not time out.
   560  func (f *File) SetDeadline(t time.Time) error {
   561  	return f.setDeadline(t)
   562  }
   563  
   564  // SetReadDeadline sets the deadline for future Read calls and any
   565  // currently-blocked Read call.
   566  // A zero value for t means Read will not time out.
   567  // Not all files support setting deadlines; see SetDeadline.
   568  func (f *File) SetReadDeadline(t time.Time) error {
   569  	return f.setReadDeadline(t)
   570  }
   571  
   572  // SetWriteDeadline sets the deadline for any future Write calls and any
   573  // currently-blocked Write call.
   574  // Even if Write times out, it may return n > 0, indicating that
   575  // some of the data was successfully written.
   576  // A zero value for t means Write will not time out.
   577  // Not all files support setting deadlines; see SetDeadline.
   578  func (f *File) SetWriteDeadline(t time.Time) error {
   579  	return f.setWriteDeadline(t)
   580  }
   581  
   582  // SyscallConn returns a raw file.
   583  // This implements the syscall.Conn interface.
   584  func (f *File) SyscallConn() (syscall.RawConn, error) {
   585  	if err := f.checkValid("SyscallConn"); err != nil {
   586  		return nil, err
   587  	}
   588  	return newRawConn(f)
   589  }
   590  
   591  // isWindowsNulName reports whether name is os.DevNull ('NUL') on Windows.
   592  // True is returned if name is 'NUL' whatever the case.
   593  func isWindowsNulName(name string) bool {
   594  	if len(name) != 3 {
   595  		return false
   596  	}
   597  	if name[0] != 'n' && name[0] != 'N' {
   598  		return false
   599  	}
   600  	if name[1] != 'u' && name[1] != 'U' {
   601  		return false
   602  	}
   603  	if name[2] != 'l' && name[2] != 'L' {
   604  		return false
   605  	}
   606  	return true
   607  }
   608  

View as plain text