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() uintptr {
38 // NewFile returns a new File with the given file descriptor and name.
39 func NewFile(fd uintptr, name string) *File {
44 f := &File{&file{fd: fdi, name: name}}
45 runtime.SetFinalizer(f.file, (*file).close)
49 // Auxiliary information if the File describes a directory
51 buf []byte // buffer for directory I/O
52 dir *syscall.DIR // from opendir
55 // DevNull is the name of the operating system's ``null device.''
56 // On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
57 const DevNull = "/dev/null"
59 // OpenFile is the generalized open call; most users will use Open
60 // or Create instead. It opens the named file with specified flag
61 // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
62 // methods on the returned File can be used for I/O.
63 // If there is an error, it will be of type *PathError.
64 func OpenFile(name string, flag int, perm FileMode) (file *File, err error) {
65 r, e := syscall.Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm))
67 return nil, &PathError{"open", name, e}
70 // There's a race here with fork/exec, which we are
71 // content to live with. See ../syscall/exec_unix.go.
72 // On OS X 10.6, the O_CLOEXEC flag is not respected.
73 // On OS X 10.7, the O_CLOEXEC flag works.
74 // Without a cheap & reliable way to detect 10.6 vs 10.7 at
75 // runtime, we just always call syscall.CloseOnExec on Darwin.
76 // Once >=10.7 is prevalent, this extra call can removed.
77 if syscall.O_CLOEXEC == 0 || runtime.GOOS == "darwin" { // O_CLOEXEC not supported
78 syscall.CloseOnExec(r)
81 return NewFile(uintptr(r), name), nil
84 // Close closes the File, rendering it unusable for I/O.
85 // It returns an error, if any.
86 func (f *File) Close() error {
90 func (file *file) close() error {
91 if file == nil || file.fd < 0 {
95 if e := syscall.Close(file.fd); e != nil {
96 err = &PathError{"close", file.name, e}
99 if file.dirinfo != nil {
100 if libc_closedir(file.dirinfo.dir) < 0 && err == nil {
101 err = &PathError{"closedir", file.name, syscall.GetErrno()}
105 file.fd = -1 // so it can't be closed again
107 // no need for a finalizer anymore
108 runtime.SetFinalizer(file, nil)
112 // Stat returns the FileInfo structure describing file.
113 // If there is an error, it will be of type *PathError.
114 func (f *File) Stat() (fi FileInfo, err error) {
115 var stat syscall.Stat_t
116 err = syscall.Fstat(f.fd, &stat)
118 return nil, &PathError{"stat", f.name, err}
120 return fileInfoFromStat(&stat, f.name), nil
123 // Stat returns a FileInfo describing the named file.
124 // If there is an error, it will be of type *PathError.
125 func Stat(name string) (fi FileInfo, err error) {
126 var stat syscall.Stat_t
127 err = syscall.Stat(name, &stat)
129 return nil, &PathError{"stat", name, err}
131 return fileInfoFromStat(&stat, name), nil
134 // Lstat returns a FileInfo describing the named file.
135 // If the file is a symbolic link, the returned FileInfo
136 // describes the symbolic link. Lstat makes no attempt to follow the link.
137 // If there is an error, it will be of type *PathError.
138 func Lstat(name string) (fi FileInfo, err error) {
139 var stat syscall.Stat_t
140 err = syscall.Lstat(name, &stat)
142 return nil, &PathError{"lstat", name, err}
144 return fileInfoFromStat(&stat, name), nil
147 func (f *File) readdir(n int) (fi []FileInfo, err error) {
153 names, err := f.Readdirnames(n)
154 fi = make([]FileInfo, len(names))
155 for i, filename := range names {
156 fip, err := Lstat(dirname + filename)
160 fi[i] = &fileStat{name: filename}
166 // read reads up to len(b) bytes from the File.
167 // It returns the number of bytes read and an error, if any.
168 func (f *File) read(b []byte) (n int, err error) {
169 return syscall.Read(f.fd, b)
172 // pread reads len(b) bytes from the File starting at byte offset off.
173 // It returns the number of bytes read and the error, if any.
174 // EOF is signaled by a zero count with err set to 0.
175 func (f *File) pread(b []byte, off int64) (n int, err error) {
176 return syscall.Pread(f.fd, b, off)
179 // write writes len(b) bytes to the File.
180 // It returns the number of bytes written and an error, if any.
181 func (f *File) write(b []byte) (n int, err error) {
182 return syscall.Write(f.fd, b)
185 // pwrite writes len(b) bytes to the File starting at byte offset off.
186 // It returns the number of bytes written and an error, if any.
187 func (f *File) pwrite(b []byte, off int64) (n int, err error) {
188 return syscall.Pwrite(f.fd, b, off)
191 // seek sets the offset for the next Read or Write on file to offset, interpreted
192 // according to whence: 0 means relative to the origin of the file, 1 means
193 // relative to the current offset, and 2 means relative to the end.
194 // It returns the new offset and an error, if any.
195 func (f *File) seek(offset int64, whence int) (ret int64, err error) {
196 return syscall.Seek(f.fd, offset, whence)
199 // Truncate changes the size of the named file.
200 // If the file is a symbolic link, it changes the size of the link's target.
201 // If there is an error, it will be of type *PathError.
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 // If there is an error, it will be of type *PathError.
211 func Remove(name string) error {
212 // System call interface forces us to know
213 // whether name is a file or directory.
214 // Try both: it is cheaper on average than
215 // doing a Stat plus the right one.
216 e := syscall.Unlink(name)
220 e1 := syscall.Rmdir(name)
225 // Both failed: figure out which error to return.
226 // OS X and Linux differ on whether unlink(dir)
227 // returns EISDIR, so can't use that. However,
228 // both agree that rmdir(file) returns ENOTDIR,
229 // so we can use that to decide which error is real.
230 // Rmdir might also return ENOTDIR if given a bad
231 // file path, like /etc/passwd/foo, but in that case,
232 // both errors will be ENOTDIR, so it's okay to
233 // use the error from unlink.
234 if e1 != syscall.ENOTDIR {
237 return &PathError{"remove", name, e}
240 // basename removes trailing slashes and the leading directory name from path name
241 func basename(name string) string {
243 // Remove trailing slashes
244 for ; i > 0 && name[i] == '/'; i-- {
247 // Remove leading directory name
248 for i--; i >= 0; i-- {
258 // Pipe returns a connected pair of Files; reads from r return bytes written to w.
259 // It returns the files and an error, if any.
260 func Pipe() (r *File, w *File, err error) {
263 // See ../syscall/exec.go for description of lock.
264 syscall.ForkLock.RLock()
265 e := syscall.Pipe(p[0:])
267 syscall.ForkLock.RUnlock()
268 return nil, nil, NewSyscallError("pipe", e)
270 syscall.CloseOnExec(p[0])
271 syscall.CloseOnExec(p[1])
272 syscall.ForkLock.RUnlock()
274 return NewFile(uintptr(p[0]), "|0"), NewFile(uintptr(p[1]), "|1"), nil
277 // TempDir returns the default directory to use for temporary files.
278 func TempDir() string {
279 dir := Getenv("TMPDIR")