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 // +build darwin freebsd linux netbsd openbsd
14 // File represents an open file descriptor.
19 // file is the real representation of *File.
20 // The extra level of indirection ensures that no clients of os
21 // can overwrite this data, which could cause the finalizer
22 // to close the wrong file descriptor.
26 dirinfo *dirInfo // nil unless directory being read
27 nepipe int // number of consecutive EPIPE in Write
30 // Fd returns the integer Unix file descriptor referencing the open file.
31 func (f *File) Fd() int {
38 // NewFile returns a new File with the given file descriptor and name.
39 func NewFile(fd int, name string) *File {
43 f := &File{&file{fd: fd, name: name}}
44 runtime.SetFinalizer(f.file, (*file).close)
48 // Auxiliary information if the File describes a directory
50 buf []byte // buffer for directory I/O
51 dir *syscall.DIR // from opendir
54 // DevNull is the name of the operating system's ``null device.''
55 // On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
56 const DevNull = "/dev/null"
58 // OpenFile is the generalized open call; most users will use Open
59 // or Create instead. It opens the named file with specified flag
60 // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
61 // methods on the returned File can be used for I/O.
62 // It returns the File and an error, if any.
63 func OpenFile(name string, flag int, perm FileMode) (file *File, err error) {
64 r, e := syscall.Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm))
66 return nil, &PathError{"open", name, e}
69 // There's a race here with fork/exec, which we are
70 // content to live with. See ../syscall/exec_unix.go.
71 // On OS X 10.6, the O_CLOEXEC flag is not respected.
72 // On OS X 10.7, the O_CLOEXEC flag works.
73 // Without a cheap & reliable way to detect 10.6 vs 10.7 at
74 // runtime, we just always call syscall.CloseOnExec on Darwin.
75 // Once >=10.7 is prevalent, this extra call can removed.
76 if syscall.O_CLOEXEC == 0 || runtime.GOOS == "darwin" { // O_CLOEXEC not supported
77 syscall.CloseOnExec(r)
80 return NewFile(r, name), nil
83 // Close closes the File, rendering it unusable for I/O.
84 // It returns an error, if any.
85 func (f *File) Close() error {
89 func (file *file) close() error {
90 if file == nil || file.fd < 0 {
94 if e := syscall.Close(file.fd); e != nil {
95 err = &PathError{"close", file.name, e}
98 if file.dirinfo != nil {
99 if libc_closedir(file.dirinfo.dir) < 0 && err == nil {
100 err = &PathError{"closedir", file.name, syscall.GetErrno()}
104 file.fd = -1 // so it can't be closed again
106 // no need for a finalizer anymore
107 runtime.SetFinalizer(file, nil)
111 // Stat returns the FileInfo structure describing file.
112 // It returns the FileInfo and an error, if any.
113 func (f *File) Stat() (fi FileInfo, err error) {
114 var stat syscall.Stat_t
115 err = syscall.Fstat(f.fd, &stat)
117 return nil, &PathError{"stat", f.name, err}
119 return fileInfoFromStat(&stat, f.name), nil
122 // Stat returns a FileInfo describing the named file and an error, if any.
123 // If name names a valid symbolic link, the returned FileInfo describes
124 // the file pointed at by the link and has fi.FollowedSymlink set to true.
125 // If name names an invalid symbolic link, the returned FileInfo describes
126 // the link itself and has fi.FollowedSymlink set to false.
127 func Stat(name string) (fi FileInfo, err error) {
128 var stat syscall.Stat_t
129 err = syscall.Stat(name, &stat)
131 return nil, &PathError{"stat", name, err}
133 return fileInfoFromStat(&stat, name), nil
136 // Lstat returns a FileInfo describing the named file and an
137 // error, if any. If the file is a symbolic link, the returned FileInfo
138 // describes the symbolic link. Lstat makes no attempt to follow the link.
139 func Lstat(name string) (fi FileInfo, err error) {
140 var stat syscall.Stat_t
141 err = syscall.Lstat(name, &stat)
143 return nil, &PathError{"lstat", name, err}
145 return fileInfoFromStat(&stat, name), nil
148 func (f *File) readdir(n int) (fi []FileInfo, err error) {
154 names, err := f.Readdirnames(n)
155 fi = make([]FileInfo, len(names))
156 for i, filename := range names {
157 fip, err := Lstat(dirname + filename)
161 fi[i] = &FileStat{name: filename}
167 // read reads up to len(b) bytes from the File.
168 // It returns the number of bytes read and an error, if any.
169 func (f *File) read(b []byte) (n int, err error) {
170 return syscall.Read(f.fd, b)
173 // pread reads len(b) bytes from the File starting at byte offset off.
174 // It returns the number of bytes read and the error, if any.
175 // EOF is signaled by a zero count with err set to 0.
176 func (f *File) pread(b []byte, off int64) (n int, err error) {
177 return syscall.Pread(f.fd, b, off)
180 // write writes len(b) bytes to the File.
181 // It returns the number of bytes written and an error, if any.
182 func (f *File) write(b []byte) (n int, err error) {
183 return syscall.Write(f.fd, b)
186 // pwrite writes len(b) bytes to the File starting at byte offset off.
187 // It returns the number of bytes written and an error, if any.
188 func (f *File) pwrite(b []byte, off int64) (n int, err error) {
189 return syscall.Pwrite(f.fd, b, off)
192 // seek sets the offset for the next Read or Write on file to offset, interpreted
193 // according to whence: 0 means relative to the origin of the file, 1 means
194 // relative to the current offset, and 2 means relative to the end.
195 // It returns the new offset and an error, if any.
196 func (f *File) seek(offset int64, whence int) (ret int64, err error) {
197 return syscall.Seek(f.fd, offset, whence)
200 // Truncate changes the size of the named file.
201 // If the file is a symbolic link, it changes the size of the link's target.
202 func Truncate(name string, size int64) error {
203 if e := syscall.Truncate(name, size); e != nil {
204 return &PathError{"truncate", name, e}
209 // Remove removes the named file or directory.
210 func Remove(name string) error {
211 // System call interface forces us to know
212 // whether name is a file or directory.
213 // Try both: it is cheaper on average than
214 // doing a Stat plus the right one.
215 e := syscall.Unlink(name)
219 e1 := syscall.Rmdir(name)
224 // Both failed: figure out which error to return.
225 // OS X and Linux differ on whether unlink(dir)
226 // returns EISDIR, so can't use that. However,
227 // both agree that rmdir(file) returns ENOTDIR,
228 // so we can use that to decide which error is real.
229 // Rmdir might also return ENOTDIR if given a bad
230 // file path, like /etc/passwd/foo, but in that case,
231 // both errors will be ENOTDIR, so it's okay to
232 // use the error from unlink.
233 if e1 != syscall.ENOTDIR {
236 return &PathError{"remove", name, e}
239 // basename removes trailing slashes and the leading directory name from path name
240 func basename(name string) string {
242 // Remove trailing slashes
243 for ; i > 0 && name[i] == '/'; i-- {
246 // Remove leading directory name
247 for i--; i >= 0; i-- {
257 // Pipe returns a connected pair of Files; reads from r return bytes written to w.
258 // It returns the files and an error, if any.
259 func Pipe() (r *File, w *File, err error) {
262 // See ../syscall/exec.go for description of lock.
263 syscall.ForkLock.RLock()
264 e := syscall.Pipe(p[0:])
266 syscall.ForkLock.RUnlock()
267 return nil, nil, NewSyscallError("pipe", e)
269 syscall.CloseOnExec(p[0])
270 syscall.CloseOnExec(p[1])
271 syscall.ForkLock.RUnlock()
273 return NewFile(p[0], "|0"), NewFile(p[1], "|1"), nil
276 // TempDir returns the default directory to use for temporary files.
277 func TempDir() string {
278 dir := Getenv("TMPDIR")