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.
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.
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.
16 // Here is a simple example, opening a file and reading some of it.
18 // file, err := os.Open("file.go") // For read access.
23 // If the open fails, the error string will be self-explanatory, like
25 // open file.go: no such file or directory
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.
30 // data := make([]byte, 100)
31 // count, err := file.Read(data)
35 // fmt.Printf("read %d bytes: %q\n", count, data[:count])
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.
45 "internal/safefilepath"
55 // Name returns the name of the file as presented to Open.
56 func (f *File) Name() string { return f.name }
58 // Stdin, Stdout, and Stderr are open Files pointing to the standard input,
59 // standard output, and standard error file descriptors.
61 // Note that the Go runtime writes to standard error for panics and crashes;
62 // closing Stderr may cause those messages to go elsewhere, perhaps
63 // to a file opened later.
65 Stdin = NewFile(uintptr(syscall.Stdin), "/dev/stdin")
66 Stdout = NewFile(uintptr(syscall.Stdout), "/dev/stdout")
67 Stderr = NewFile(uintptr(syscall.Stderr), "/dev/stderr")
70 // Flags to OpenFile wrapping those of the underlying system. Not all
71 // flags may be implemented on a given system.
73 // Exactly one of O_RDONLY, O_WRONLY, or O_RDWR must be specified.
74 O_RDONLY int = syscall.O_RDONLY // open the file read-only.
75 O_WRONLY int = syscall.O_WRONLY // open the file write-only.
76 O_RDWR int = syscall.O_RDWR // open the file read-write.
77 // The remaining values may be or'ed in to control behavior.
78 O_APPEND int = syscall.O_APPEND // append data to the file when writing.
79 O_CREATE int = syscall.O_CREAT // create a new file if none exists.
80 O_EXCL int = syscall.O_EXCL // used with O_CREATE, file must not exist.
81 O_SYNC int = syscall.O_SYNC // open for synchronous I/O.
82 O_TRUNC int = syscall.O_TRUNC // truncate regular writable file when opened.
85 // Seek whence values.
87 // Deprecated: Use io.SeekStart, io.SeekCurrent, and io.SeekEnd.
89 SEEK_SET int = 0 // seek relative to the origin of the file
90 SEEK_CUR int = 1 // seek relative to the current offset
91 SEEK_END int = 2 // seek relative to the end
94 // LinkError records an error during a link or symlink or rename
95 // system call and the paths that caused it.
96 type LinkError struct {
103 func (e *LinkError) Error() string {
104 return e.Op + " " + e.Old + " " + e.New + ": " + e.Err.Error()
107 func (e *LinkError) Unwrap() error {
111 // Read reads up to len(b) bytes from the File and stores them in b.
112 // It returns the number of bytes read and any error encountered.
113 // At end of file, Read returns 0, io.EOF.
114 func (f *File) Read(b []byte) (n int, err error) {
115 if err := f.checkValid("read"); err != nil {
119 return n, f.wrapErr("read", e)
122 // ReadAt reads len(b) bytes from the File starting at byte offset off.
123 // It returns the number of bytes read and the error, if any.
124 // ReadAt always returns a non-nil error when n < len(b).
125 // At end of file, that error is io.EOF.
126 func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
127 if err := f.checkValid("read"); err != nil {
132 return 0, &PathError{Op: "readat", Path: f.name, Err: errors.New("negative offset")}
136 m, e := f.pread(b, off)
138 err = f.wrapErr("read", e)
148 // ReadFrom implements io.ReaderFrom.
149 func (f *File) ReadFrom(r io.Reader) (n int64, err error) {
150 if err := f.checkValid("write"); err != nil {
153 n, handled, e := f.readFrom(r)
155 return genericReadFrom(f, r) // without wrapping
157 return n, f.wrapErr("write", e)
160 func genericReadFrom(f *File, r io.Reader) (int64, error) {
161 return io.Copy(fileWithoutReadFrom{f}, r)
164 // fileWithoutReadFrom implements all the methods of *File other
165 // than ReadFrom. This is used to permit ReadFrom to call io.Copy
166 // without leading to a recursive call to ReadFrom.
167 type fileWithoutReadFrom struct {
171 // This ReadFrom method hides the *File ReadFrom method.
172 func (fileWithoutReadFrom) ReadFrom(fileWithoutReadFrom) {
176 // Write writes len(b) bytes from b to the File.
177 // It returns the number of bytes written and an error, if any.
178 // Write returns a non-nil error when n != len(b).
179 func (f *File) Write(b []byte) (n int, err error) {
180 if err := f.checkValid("write"); err != nil {
188 err = io.ErrShortWrite
194 err = f.wrapErr("write", e)
200 var errWriteAtInAppendMode = errors.New("os: invalid use of WriteAt on file opened with O_APPEND")
202 // WriteAt writes len(b) bytes to the File starting at byte offset off.
203 // It returns the number of bytes written and an error, if any.
204 // WriteAt returns a non-nil error when n != len(b).
206 // If file was opened with the O_APPEND flag, WriteAt returns an error.
207 func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
208 if err := f.checkValid("write"); err != nil {
212 return 0, errWriteAtInAppendMode
216 return 0, &PathError{Op: "writeat", Path: f.name, Err: errors.New("negative offset")}
220 m, e := f.pwrite(b, off)
222 err = f.wrapErr("write", e)
232 // Seek sets the offset for the next Read or Write on file to offset, interpreted
233 // according to whence: 0 means relative to the origin of the file, 1 means
234 // relative to the current offset, and 2 means relative to the end.
235 // It returns the new offset and an error, if any.
236 // The behavior of Seek on a file opened with O_APPEND is not specified.
237 func (f *File) Seek(offset int64, whence int) (ret int64, err error) {
238 if err := f.checkValid("seek"); err != nil {
241 r, e := f.seek(offset, whence)
242 if e == nil && f.dirinfo != nil && r != 0 {
246 return 0, f.wrapErr("seek", e)
251 // WriteString is like Write, but writes the contents of string s rather than
253 func (f *File) WriteString(s string) (n int, err error) {
254 b := unsafe.Slice(unsafe.StringData(s), len(s))
258 // Mkdir creates a new directory with the specified name and permission
259 // bits (before umask).
260 // If there is an error, it will be of type *PathError.
261 func Mkdir(name string, perm FileMode) error {
262 longName := fixLongPath(name)
263 e := ignoringEINTR(func() error {
264 return syscall.Mkdir(longName, syscallMode(perm))
268 return &PathError{Op: "mkdir", Path: name, Err: e}
271 // mkdir(2) itself won't handle the sticky bit on *BSD and Solaris
272 if !supportsCreateWithStickyBit && perm&ModeSticky != 0 {
273 e = setStickyBit(name)
284 // setStickyBit adds ModeSticky to the permission bits of path, non atomic.
285 func setStickyBit(name string) error {
286 fi, err := Stat(name)
290 return Chmod(name, fi.Mode()|ModeSticky)
293 // Chdir changes the current working directory to the named directory.
294 // If there is an error, it will be of type *PathError.
295 func Chdir(dir string) error {
296 if e := syscall.Chdir(dir); e != nil {
297 testlog.Open(dir) // observe likely non-existent directory
298 return &PathError{Op: "chdir", Path: dir, Err: e}
300 if log := testlog.Logger(); log != nil {
309 // Open opens the named file for reading. If successful, methods on
310 // the returned file can be used for reading; the associated file
311 // descriptor has mode O_RDONLY.
312 // If there is an error, it will be of type *PathError.
313 func Open(name string) (*File, error) {
314 return OpenFile(name, O_RDONLY, 0)
317 // Create creates or truncates the named file. If the file already exists,
318 // it is truncated. If the file does not exist, it is created with mode 0666
319 // (before umask). If successful, methods on the returned File can
320 // be used for I/O; the associated file descriptor has mode O_RDWR.
321 // If there is an error, it will be of type *PathError.
322 func Create(name string) (*File, error) {
323 return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666)
326 // OpenFile is the generalized open call; most users will use Open
327 // or Create instead. It opens the named file with specified flag
328 // (O_RDONLY etc.). If the file does not exist, and the O_CREATE flag
329 // is passed, it is created with mode perm (before umask). If successful,
330 // methods on the returned File can be used for I/O.
331 // If there is an error, it will be of type *PathError.
332 func OpenFile(name string, flag int, perm FileMode) (*File, error) {
334 f, err := openFileNolog(name, flag, perm)
338 f.appendMode = flag&O_APPEND != 0
343 // lstat is overridden in tests.
346 // Rename renames (moves) oldpath to newpath.
347 // If newpath already exists and is not a directory, Rename replaces it.
348 // OS-specific restrictions may apply when oldpath and newpath are in different directories.
349 // Even within the same directory, on non-Unix platforms Rename is not an atomic operation.
350 // If there is an error, it will be of type *LinkError.
351 func Rename(oldpath, newpath string) error {
352 return rename(oldpath, newpath)
355 // Many functions in package syscall return a count of -1 instead of 0.
356 // Using fixCount(call()) instead of call() corrects the count.
357 func fixCount(n int, err error) (int, error) {
364 // checkWrapErr is the test hook to enable checking unexpected wrapped errors of poll.ErrFileClosing.
365 // It is set to true in the export_test.go for tests (including fuzz tests).
366 var checkWrapErr = false
368 // wrapErr wraps an error that occurred during an operation on an open file.
369 // It passes io.EOF through unchanged, otherwise converts
370 // poll.ErrFileClosing to ErrClosed and wraps the error in a PathError.
371 func (f *File) wrapErr(op string, err error) error {
372 if err == nil || err == io.EOF {
375 if err == poll.ErrFileClosing {
377 } else if checkWrapErr && errors.Is(err, poll.ErrFileClosing) {
378 panic("unexpected error wrapping poll.ErrFileClosing: " + err.Error())
380 return &PathError{Op: op, Path: f.name, Err: err}
383 // TempDir returns the default directory to use for temporary files.
385 // On Unix systems, it returns $TMPDIR if non-empty, else /tmp.
386 // On Windows, it uses GetTempPath, returning the first non-empty
387 // value from %TMP%, %TEMP%, %USERPROFILE%, or the Windows directory.
388 // On Plan 9, it returns /tmp.
390 // The directory is neither guaranteed to exist nor have accessible
392 func TempDir() string {
396 // UserCacheDir returns the default root directory to use for user-specific
397 // cached data. Users should create their own application-specific subdirectory
398 // within this one and use that.
400 // On Unix systems, it returns $XDG_CACHE_HOME as specified by
401 // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html if
402 // non-empty, else $HOME/.cache.
403 // On Darwin, it returns $HOME/Library/Caches.
404 // On Windows, it returns %LocalAppData%.
405 // On Plan 9, it returns $home/lib/cache.
407 // If the location cannot be determined (for example, $HOME is not defined),
408 // then it will return an error.
409 func UserCacheDir() (string, error) {
412 switch runtime.GOOS {
414 dir = Getenv("LocalAppData")
416 return "", errors.New("%LocalAppData% is not defined")
419 case "darwin", "ios":
422 return "", errors.New("$HOME is not defined")
424 dir += "/Library/Caches"
429 return "", errors.New("$home is not defined")
434 dir = Getenv("XDG_CACHE_HOME")
438 return "", errors.New("neither $XDG_CACHE_HOME nor $HOME are defined")
447 // UserConfigDir returns the default root directory to use for user-specific
448 // configuration data. Users should create their own application-specific
449 // subdirectory within this one and use that.
451 // On Unix systems, it returns $XDG_CONFIG_HOME as specified by
452 // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html if
453 // non-empty, else $HOME/.config.
454 // On Darwin, it returns $HOME/Library/Application Support.
455 // On Windows, it returns %AppData%.
456 // On Plan 9, it returns $home/lib.
458 // If the location cannot be determined (for example, $HOME is not defined),
459 // then it will return an error.
460 func UserConfigDir() (string, error) {
463 switch runtime.GOOS {
465 dir = Getenv("AppData")
467 return "", errors.New("%AppData% is not defined")
470 case "darwin", "ios":
473 return "", errors.New("$HOME is not defined")
475 dir += "/Library/Application Support"
480 return "", errors.New("$home is not defined")
485 dir = Getenv("XDG_CONFIG_HOME")
489 return "", errors.New("neither $XDG_CONFIG_HOME nor $HOME are defined")
498 // UserHomeDir returns the current user's home directory.
500 // On Unix, including macOS, it returns the $HOME environment variable.
501 // On Windows, it returns %USERPROFILE%.
502 // On Plan 9, it returns the $home environment variable.
504 // If the expected variable is not set in the environment, UserHomeDir
505 // returns either a platform-specific default value or a non-nil error.
506 func UserHomeDir() (string, error) {
507 env, enverr := "HOME", "$HOME"
508 switch runtime.GOOS {
510 env, enverr = "USERPROFILE", "%userprofile%"
512 env, enverr = "home", "$home"
514 if v := Getenv(env); v != "" {
517 // On some geese the home directory is not always defined.
518 switch runtime.GOOS {
520 return "/sdcard", nil
524 return "", errors.New(enverr + " is not defined")
527 // Chmod changes the mode of the named file to mode.
528 // If the file is a symbolic link, it changes the mode of the link's target.
529 // If there is an error, it will be of type *PathError.
531 // A different subset of the mode bits are used, depending on the
534 // On Unix, the mode's permission bits, ModeSetuid, ModeSetgid, and
535 // ModeSticky are used.
537 // On Windows, only the 0200 bit (owner writable) of mode is used; it
538 // controls whether the file's read-only attribute is set or cleared.
539 // The other bits are currently unused. For compatibility with Go 1.12
540 // and earlier, use a non-zero mode. Use mode 0400 for a read-only
541 // file and 0600 for a readable+writable file.
543 // On Plan 9, the mode's permission bits, ModeAppend, ModeExclusive,
544 // and ModeTemporary are used.
545 func Chmod(name string, mode FileMode) error { return chmod(name, mode) }
547 // Chmod changes the mode of the file to mode.
548 // If there is an error, it will be of type *PathError.
549 func (f *File) Chmod(mode FileMode) error { return f.chmod(mode) }
551 // SetDeadline sets the read and write deadlines for a File.
552 // It is equivalent to calling both SetReadDeadline and SetWriteDeadline.
554 // Only some kinds of files support setting a deadline. Calls to SetDeadline
555 // for files that do not support deadlines will return ErrNoDeadline.
556 // On most systems ordinary files do not support deadlines, but pipes do.
558 // A deadline is an absolute time after which I/O operations fail with an
559 // error instead of blocking. The deadline applies to all future and pending
560 // I/O, not just the immediately following call to Read or Write.
561 // After a deadline has been exceeded, the connection can be refreshed
562 // by setting a deadline in the future.
564 // If the deadline is exceeded a call to Read or Write or to other I/O
565 // methods will return an error that wraps ErrDeadlineExceeded.
566 // This can be tested using errors.Is(err, os.ErrDeadlineExceeded).
567 // That error implements the Timeout method, and calling the Timeout
568 // method will return true, but there are other possible errors for which
569 // the Timeout will return true even if the deadline has not been exceeded.
571 // An idle timeout can be implemented by repeatedly extending
572 // the deadline after successful Read or Write calls.
574 // A zero value for t means I/O operations will not time out.
575 func (f *File) SetDeadline(t time.Time) error {
576 return f.setDeadline(t)
579 // SetReadDeadline sets the deadline for future Read calls and any
580 // currently-blocked Read call.
581 // A zero value for t means Read will not time out.
582 // Not all files support setting deadlines; see SetDeadline.
583 func (f *File) SetReadDeadline(t time.Time) error {
584 return f.setReadDeadline(t)
587 // SetWriteDeadline sets the deadline for any future Write calls and any
588 // currently-blocked Write call.
589 // Even if Write times out, it may return n > 0, indicating that
590 // some of the data was successfully written.
591 // A zero value for t means Write will not time out.
592 // Not all files support setting deadlines; see SetDeadline.
593 func (f *File) SetWriteDeadline(t time.Time) error {
594 return f.setWriteDeadline(t)
597 // SyscallConn returns a raw file.
598 // This implements the syscall.Conn interface.
599 func (f *File) SyscallConn() (syscall.RawConn, error) {
600 if err := f.checkValid("SyscallConn"); err != nil {
606 // DirFS returns a file system (an fs.FS) for the tree of files rooted at the directory dir.
608 // Note that DirFS("/prefix") only guarantees that the Open calls it makes to the
609 // operating system will begin with "/prefix": DirFS("/prefix").Open("file") is the
610 // same as os.Open("/prefix/file"). So if /prefix/file is a symbolic link pointing outside
611 // the /prefix tree, then using DirFS does not stop the access any more than using
612 // os.Open does. Additionally, the root of the fs.FS returned for a relative path,
613 // DirFS("prefix"), will be affected by later calls to Chdir. DirFS is therefore not
614 // a general substitute for a chroot-style security mechanism when the directory tree
615 // contains arbitrary content.
617 // The directory dir must not be "".
619 // The result implements [io/fs.StatFS], [io/fs.ReadFileFS] and
620 // [io/fs.ReadDirFS].
621 func DirFS(dir string) fs.FS {
627 func (dir dirFS) Open(name string) (fs.File, error) {
628 fullname, err := dir.join(name)
630 return nil, &PathError{Op: "open", Path: name, Err: err}
632 f, err := Open(fullname)
634 // DirFS takes a string appropriate for GOOS,
635 // while the name argument here is always slash separated.
636 // dir.join will have mixed the two; undo that for
638 err.(*PathError).Path = name
644 // The ReadFile method calls the [ReadFile] function for the file
645 // with the given name in the directory. The function provides
646 // robust handling for small files and special file systems.
647 // Through this method, dirFS implements [io/fs.ReadFileFS].
648 func (dir dirFS) ReadFile(name string) ([]byte, error) {
649 fullname, err := dir.join(name)
651 return nil, &PathError{Op: "readfile", Path: name, Err: err}
653 return ReadFile(fullname)
656 // ReadDir reads the named directory, returning all its directory entries sorted
657 // by filename. Through this method, dirFS implements [io/fs.ReadDirFS].
658 func (dir dirFS) ReadDir(name string) ([]DirEntry, error) {
659 fullname, err := dir.join(name)
661 return nil, &PathError{Op: "readdir", Path: name, Err: err}
663 return ReadDir(fullname)
666 func (dir dirFS) Stat(name string) (fs.FileInfo, error) {
667 fullname, err := dir.join(name)
669 return nil, &PathError{Op: "stat", Path: name, Err: err}
671 f, err := Stat(fullname)
673 // See comment in dirFS.Open.
674 err.(*PathError).Path = name
680 // join returns the path for name in dir.
681 func (dir dirFS) join(name string) (string, error) {
683 return "", errors.New("os: DirFS with empty root")
685 if !fs.ValidPath(name) {
686 return "", ErrInvalid
688 name, err := safefilepath.FromFS(name)
690 return "", ErrInvalid
692 if IsPathSeparator(dir[len(dir)-1]) {
693 return string(dir) + name, nil
695 return string(dir) + string(PathSeparator) + name, nil
698 // ReadFile reads the named file and returns the contents.
699 // A successful call returns err == nil, not err == EOF.
700 // Because ReadFile reads the whole file, it does not treat an EOF from Read
701 // as an error to be reported.
702 func ReadFile(name string) ([]byte, error) {
710 if info, err := f.Stat(); err == nil {
711 size64 := info.Size()
712 if int64(int(size64)) == size64 {
716 size++ // one byte for final read at EOF
718 // If a file claims a small size, read at least 512 bytes.
719 // In particular, files in Linux's /proc claim size 0 but
720 // then do not work right if read in small pieces,
721 // so an initial read of 1 byte would not work correctly.
726 data := make([]byte, 0, size)
728 n, err := f.Read(data[len(data):cap(data)])
729 data = data[:len(data)+n]
737 if len(data) >= cap(data) {
738 d := append(data[:cap(data)], 0)
744 // WriteFile writes data to the named file, creating it if necessary.
745 // If the file does not exist, WriteFile creates it with permissions perm (before umask);
746 // otherwise WriteFile truncates it before writing, without changing permissions.
747 // Since WriteFile requires multiple system calls to complete, a failure mid-operation
748 // can leave the file in a partially written state.
749 func WriteFile(name string, data []byte, perm FileMode) error {
750 f, err := OpenFile(name, O_WRONLY|O_CREATE|O_TRUNC, perm)
754 _, err = f.Write(data)
755 if err1 := f.Close(); err1 != nil && err == nil {