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