blob: f332bc83462825645bf3016f4d2372028fbc6623 [file] [log] [blame]
Rob Pikec80b06a2008-09-11 13:03:46 -07001// 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
Nigel Tao6a186d32011-04-20 09:57:05 +10005// Package os provides a platform-independent interface to operating system
Rob Pikebe0f6fe2012-02-09 16:55:36 +11006// 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
Rob Pikeefb28b22012-02-17 14:30:25 +110010// will include the failing file name when printed and will be of type
11// *PathError, which may be unpacked for more information.
Robert Griesemer465b9c32012-10-30 13:38:01 -070012//
Rob Pike34e07252011-06-14 11:49:33 +100013// The os interface is intended to be uniform across all operating systems.
14// Features not generally available appear in the system-specific package syscall.
Rob Pikeefb28b22012-02-17 14:30:25 +110015//
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
Sanjay Menakuru27e07a22012-02-24 22:42:16 +110028// Write take their byte counts from the length of the argument slice.
Rob Pikeefb28b22012-02-17 14:30:25 +110029//
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//
Rob Pikec80b06a2008-09-11 13:03:46 -070037package os
38
Russ Cox91ceda52009-02-15 19:35:52 -080039import (
Russ Cox08a073a2011-11-01 21:49:08 -040040 "io"
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080041 "syscall"
Russ Cox91ceda52009-02-15 19:35:52 -080042)
Rob Pikec80b06a2008-09-11 13:03:46 -070043
Rob Pike333cdd82009-03-07 16:56:44 -080044// Name returns the name of the file as presented to Open.
Brad Fitzpatrick744fb522011-12-01 11:23:39 -080045func (f *File) Name() string { return f.name }
Rob Pike704bc9d2009-02-06 17:54:26 -080046
Russ Cox7a706fb2009-03-11 12:51:10 -070047// Stdin, Stdout, and Stderr are open Files pointing to the standard input,
Rob Pike333cdd82009-03-07 16:56:44 -080048// standard output, and standard error file descriptors.
Russ Cox839a6842009-01-20 14:40:40 -080049var (
Brad Fitzpatrick4152b432012-02-10 14:16:15 +110050 Stdin = NewFile(uintptr(syscall.Stdin), "/dev/stdin")
51 Stdout = NewFile(uintptr(syscall.Stdout), "/dev/stdout")
52 Stderr = NewFile(uintptr(syscall.Stderr), "/dev/stderr")
Rob Pikec80b06a2008-09-11 13:03:46 -070053)
54
Rob Pike333cdd82009-03-07 16:56:44 -080055// Flags to Open wrapping those of the underlying system. Not all flags
56// may be implemented on a given system.
Russ Cox839a6842009-01-20 14:40:40 -080057const (
Brad Fitzpatrick4152b432012-02-10 14:16:15 +110058 O_RDONLY int = syscall.O_RDONLY // open the file read-only.
59 O_WRONLY int = syscall.O_WRONLY // open the file write-only.
60 O_RDWR int = syscall.O_RDWR // open the file read-write.
61 O_APPEND int = syscall.O_APPEND // append data to the file when writing.
62 O_CREATE int = syscall.O_CREAT // create a new file if none exists.
63 O_EXCL int = syscall.O_EXCL // used with O_CREATE, file must not exist
64 O_SYNC int = syscall.O_SYNC // open for synchronous I/O.
65 O_TRUNC int = syscall.O_TRUNC // if possible, truncate file when opened.
Rob Pike5ea76492008-09-11 15:09:10 -070066)
67
Brad Fitzpatrick2be13a82011-04-04 13:53:52 -070068// Seek whence values.
69const (
70 SEEK_SET int = 0 // seek relative to the origin of the file
71 SEEK_CUR int = 1 // seek relative to the current offset
72 SEEK_END int = 2 // seek relative to the end
73)
74
Fazlul Shahriar441538e2012-02-20 12:31:24 +110075// LinkError records an error during a link or symlink or rename
76// system call and the paths that caused it.
77type LinkError struct {
78 Op string
79 Old string
80 New string
81 Err error
82}
83
84func (e *LinkError) Error() string {
85 return e.Op + " " + e.Old + " " + e.New + ": " + e.Err.Error()
86}
87
Russ Cox7a706fb2009-03-11 12:51:10 -070088// Read reads up to len(b) bytes from the File.
Russ Cox08a073a2011-11-01 21:49:08 -040089// It returns the number of bytes read and an error, if any.
90// EOF is signaled by a zero count with err set to io.EOF.
Brad Fitzpatrick744fb522011-12-01 11:23:39 -080091func (f *File) Read(b []byte) (n int, err error) {
92 if f == nil {
Rob Pike56069f02012-02-17 10:04:29 +110093 return 0, ErrInvalid
Rob Pikec80b06a2008-09-11 13:03:46 -070094 }
Brad Fitzpatrick744fb522011-12-01 11:23:39 -080095 n, e := f.read(b)
Russ Cox9e0fec92009-06-01 22:14:39 -070096 if n < 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -080097 n = 0
Robert Griesemer23c8faa2008-11-14 15:13:29 -080098 }
Russ Coxc017a822011-11-13 22:44:52 -050099 if n == 0 && len(b) > 0 && e == nil {
Russ Cox08a073a2011-11-01 21:49:08 -0400100 return 0, io.EOF
Russ Cox64684cc2009-06-22 13:26:13 -0700101 }
Russ Coxc017a822011-11-13 22:44:52 -0500102 if e != nil {
Brad Fitzpatrick744fb522011-12-01 11:23:39 -0800103 err = &PathError{"read", f.name, e}
Russ Coxa0bcaf42009-06-25 20:24:55 -0700104 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800105 return n, err
Rob Pikec80b06a2008-09-11 13:03:46 -0700106}
107
Russ Coxf7e92c52009-08-27 18:36:45 -0700108// ReadAt reads len(b) bytes from the File starting at byte offset off.
Russ Cox08a073a2011-11-01 21:49:08 -0400109// It returns the number of bytes read and the error, if any.
Russ Coxc69d6342011-11-22 12:22:28 -0500110// ReadAt always returns a non-nil error when n < len(b).
111// At end of file, that error is io.EOF.
Brad Fitzpatrick744fb522011-12-01 11:23:39 -0800112func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
113 if f == nil {
Rob Pike56069f02012-02-17 10:04:29 +1100114 return 0, ErrInvalid
Russ Coxf7e92c52009-08-27 18:36:45 -0700115 }
116 for len(b) > 0 {
Brad Fitzpatrick744fb522011-12-01 11:23:39 -0800117 m, e := f.pread(b, off)
Russ Coxc017a822011-11-13 22:44:52 -0500118 if m == 0 && e == nil {
Russ Cox08a073a2011-11-01 21:49:08 -0400119 return n, io.EOF
Rob Pike4e201c72009-11-19 11:51:23 -0800120 }
Russ Coxc017a822011-11-13 22:44:52 -0500121 if e != nil {
Brad Fitzpatrick744fb522011-12-01 11:23:39 -0800122 err = &PathError{"read", f.name, e}
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800123 break
Russ Coxf7e92c52009-08-27 18:36:45 -0700124 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800125 n += m
126 b = b[m:]
127 off += int64(m)
Russ Coxf7e92c52009-08-27 18:36:45 -0700128 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800129 return
Russ Coxf7e92c52009-08-27 18:36:45 -0700130}
131
Russ Cox7a706fb2009-03-11 12:51:10 -0700132// Write writes len(b) bytes to the File.
Russ Cox08a073a2011-11-01 21:49:08 -0400133// It returns the number of bytes written and an error, if any.
134// Write returns a non-nil error when n != len(b).
Brad Fitzpatrick744fb522011-12-01 11:23:39 -0800135func (f *File) Write(b []byte) (n int, err error) {
136 if f == nil {
Rob Pike56069f02012-02-17 10:04:29 +1100137 return 0, ErrInvalid
Rob Pikec80b06a2008-09-11 13:03:46 -0700138 }
Brad Fitzpatrick744fb522011-12-01 11:23:39 -0800139 n, e := f.write(b)
Russ Cox9e0fec92009-06-01 22:14:39 -0700140 if n < 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800141 n = 0
Robert Griesemer23c8faa2008-11-14 15:13:29 -0800142 }
Shenghou Maaa0ae752013-12-09 23:25:13 -0500143 if n != len(b) {
144 err = io.ErrShortWrite
145 }
Yuval Pavel Zholkoverc256f0a2011-04-02 14:28:58 -0700146
Brad Fitzpatrick744fb522011-12-01 11:23:39 -0800147 epipecheck(f, e)
Yuval Pavel Zholkoverc256f0a2011-04-02 14:28:58 -0700148
Russ Coxc017a822011-11-13 22:44:52 -0500149 if e != nil {
Brad Fitzpatrick744fb522011-12-01 11:23:39 -0800150 err = &PathError{"write", f.name, e}
Russ Coxa0bcaf42009-06-25 20:24:55 -0700151 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800152 return n, err
Rob Pikec80b06a2008-09-11 13:03:46 -0700153}
154
Russ Coxf7e92c52009-08-27 18:36:45 -0700155// WriteAt writes len(b) bytes to the File starting at byte offset off.
Russ Cox08a073a2011-11-01 21:49:08 -0400156// It returns the number of bytes written and an error, if any.
157// WriteAt returns a non-nil error when n != len(b).
Brad Fitzpatrick744fb522011-12-01 11:23:39 -0800158func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
159 if f == nil {
Rob Pike56069f02012-02-17 10:04:29 +1100160 return 0, ErrInvalid
Russ Coxf7e92c52009-08-27 18:36:45 -0700161 }
162 for len(b) > 0 {
Brad Fitzpatrick744fb522011-12-01 11:23:39 -0800163 m, e := f.pwrite(b, off)
Russ Coxc017a822011-11-13 22:44:52 -0500164 if e != nil {
Brad Fitzpatrick744fb522011-12-01 11:23:39 -0800165 err = &PathError{"write", f.name, e}
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800166 break
Russ Coxf7e92c52009-08-27 18:36:45 -0700167 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800168 n += m
169 b = b[m:]
170 off += int64(m)
Russ Coxf7e92c52009-08-27 18:36:45 -0700171 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800172 return
Russ Coxf7e92c52009-08-27 18:36:45 -0700173}
174
Russ Cox7a706fb2009-03-11 12:51:10 -0700175// Seek sets the offset for the next Read or Write on file to offset, interpreted
Rob Pike333cdd82009-03-07 16:56:44 -0800176// according to whence: 0 means relative to the origin of the file, 1 means
177// relative to the current offset, and 2 means relative to the end.
Russ Cox08a073a2011-11-01 21:49:08 -0400178// It returns the new offset and an error, if any.
Brad Fitzpatrick744fb522011-12-01 11:23:39 -0800179func (f *File) Seek(offset int64, whence int) (ret int64, err error) {
Rob Pike4cb086b2013-08-20 14:33:03 +1000180 if f == nil {
181 return 0, ErrInvalid
182 }
Brad Fitzpatrick744fb522011-12-01 11:23:39 -0800183 r, e := f.seek(offset, whence)
184 if e == nil && f.dirinfo != nil && r != 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800185 e = syscall.EISDIR
Rob Piked94c5ab2009-02-10 11:27:45 -0800186 }
Russ Coxc017a822011-11-13 22:44:52 -0500187 if e != nil {
Brad Fitzpatrick744fb522011-12-01 11:23:39 -0800188 return 0, &PathError{"seek", f.name, e}
Rob Piked94c5ab2009-02-10 11:27:45 -0800189 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800190 return r, nil
Rob Piked94c5ab2009-02-10 11:27:45 -0800191}
192
Rob Pike333cdd82009-03-07 16:56:44 -0800193// WriteString is like Write, but writes the contents of string s rather than
Nigel Taoeb7d5692012-10-22 16:26:47 +1100194// a slice of bytes.
Brad Fitzpatrick744fb522011-12-01 11:23:39 -0800195func (f *File) WriteString(s string) (ret int, err error) {
196 if f == nil {
Rob Pike56069f02012-02-17 10:04:29 +1100197 return 0, ErrInvalid
Rob Pikec80b06a2008-09-11 13:03:46 -0700198 }
Brad Fitzpatrick744fb522011-12-01 11:23:39 -0800199 return f.Write([]byte(s))
Rob Pikec80b06a2008-09-11 13:03:46 -0700200}
Russ Cox5267db32008-09-26 14:31:17 -0700201
Rob Pike333cdd82009-03-07 16:56:44 -0800202// Mkdir creates a new directory with the specified name and permission bits.
Rob Pikebe0f6fe2012-02-09 16:55:36 +1100203// If there is an error, it will be of type *PathError.
Brad Fitzpatrick6454a3e2012-01-19 15:45:18 -0800204func Mkdir(name string, perm FileMode) error {
205 e := syscall.Mkdir(name, syscallMode(perm))
Kato Kazuyoshi62629022014-12-16 08:22:17 -0800206
Russ Coxc017a822011-11-13 22:44:52 -0500207 if e != nil {
208 return &PathError{"mkdir", name, e}
Russ Coxa0bcaf42009-06-25 20:24:55 -0700209 }
Kato Kazuyoshi9c0b1452014-12-22 21:05:07 -0800210
211 // mkdir(2) itself won't handle the sticky bit on *BSD and Solaris
212 if !supportsCreateWithStickyBit && perm&ModeSticky != 0 {
213 Chmod(name, perm)
214 }
215
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800216 return nil
Cary Hullf9608402008-10-09 00:15:37 -0700217}
Rob Pike704bc9d2009-02-06 17:54:26 -0800218
Russ Cox61ba1602009-04-07 00:40:36 -0700219// Chdir changes the current working directory to the named directory.
Rob Pikebe0f6fe2012-02-09 16:55:36 +1100220// If there is an error, it will be of type *PathError.
Russ Cox08a073a2011-11-01 21:49:08 -0400221func Chdir(dir string) error {
Russ Coxc017a822011-11-13 22:44:52 -0500222 if e := syscall.Chdir(dir); e != nil {
223 return &PathError{"chdir", dir, e}
Russ Coxa0bcaf42009-06-25 20:24:55 -0700224 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800225 return nil
Russ Cox61ba1602009-04-07 00:40:36 -0700226}
227
Russ Cox23c81f72009-05-18 10:49:34 -0700228// Chdir changes the current working directory to the file,
229// which must be a directory.
Rob Pikebe0f6fe2012-02-09 16:55:36 +1100230// If there is an error, it will be of type *PathError.
Russ Cox08a073a2011-11-01 21:49:08 -0400231func (f *File) Chdir() error {
Rob Pike4cb086b2013-08-20 14:33:03 +1000232 if f == nil {
233 return ErrInvalid
234 }
Russ Coxc017a822011-11-13 22:44:52 -0500235 if e := syscall.Fchdir(f.fd); e != nil {
236 return &PathError{"chdir", f.name, e}
Russ Coxa0bcaf42009-06-25 20:24:55 -0700237 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800238 return nil
Russ Cox23c81f72009-05-18 10:49:34 -0700239}
Rob Pike8a90fd32011-04-04 23:42:14 -0700240
241// Open opens the named file for reading. If successful, methods on
242// the returned file can be used for reading; the associated file
243// descriptor has mode O_RDONLY.
Rob Pikebe0f6fe2012-02-09 16:55:36 +1100244// If there is an error, it will be of type *PathError.
Russ Cox08a073a2011-11-01 21:49:08 -0400245func Open(name string) (file *File, err error) {
Rob Pike8a90fd32011-04-04 23:42:14 -0700246 return OpenFile(name, O_RDONLY, 0)
247}
248
249// Create creates the named file mode 0666 (before umask), truncating
250// it if it already exists. If successful, methods on the returned
251// File can be used for I/O; the associated file descriptor has mode
252// O_RDWR.
Rob Pikebe0f6fe2012-02-09 16:55:36 +1100253// If there is an error, it will be of type *PathError.
Russ Cox08a073a2011-11-01 21:49:08 -0400254func Create(name string) (file *File, err error) {
Rob Pike8a90fd32011-04-04 23:42:14 -0700255 return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666)
256}
Brad Fitzpatrick83788042013-08-08 11:13:00 -0700257
258// lstat is overridden in tests.
259var lstat = Lstat
Shenghou Maa4b66b72013-12-09 23:46:21 -0500260
261// Rename renames (moves) a file. OS-specific restrictions might apply.
262func Rename(oldpath, newpath string) error {
263 return rename(oldpath, newpath)
264}
Russ Coxa62da202014-10-28 15:00:13 -0400265
266// Many functions in package syscall return a count of -1 instead of 0.
267// Using fixCount(call()) instead of call() corrects the count.
268func fixCount(n int, err error) (int, error) {
269 if n < 0 {
270 n = 0
271 }
272 return n, err
273}