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 openbsd
14 // File represents an open file descriptor.
18 dirinfo *dirInfo // nil unless directory being read
19 nepipe int // number of consecutive EPIPE in Write
22 // Fd returns the integer Unix file descriptor referencing the open file.
23 func (file *File) Fd() int {
30 // NewFile returns a new File with the given file descriptor and name.
31 func NewFile(fd int, name string) *File {
35 f := &File{fd: fd, name: name}
36 runtime.SetFinalizer(f, (*File).Close)
40 // Auxiliary information if the File describes a directory
42 buf []byte // buffer for directory I/O
43 dir *syscall.DIR // from opendir
46 // DevNull is the name of the operating system's ``null device.''
47 // On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
48 const DevNull = "/dev/null"
50 // OpenFile is the generalized open call; most users will use Open
51 // or Create instead. It opens the named file with specified flag
52 // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
53 // methods on the returned File can be used for I/O.
54 // It returns the File and an Error, if any.
55 func OpenFile(name string, flag int, perm uint32) (file *File, err Error) {
56 r, e := syscall.Open(name, flag|syscall.O_CLOEXEC, perm)
58 return nil, &PathError{"open", name, Errno(e)}
61 // There's a race here with fork/exec, which we are
62 // content to live with. See ../syscall/exec.go
63 if syscall.O_CLOEXEC == 0 { // O_CLOEXEC not supported
64 syscall.CloseOnExec(r)
67 return NewFile(r, name), nil
70 // Close closes the File, rendering it unusable for I/O.
71 // It returns an Error, if any.
72 func (file *File) Close() Error {
73 if file == nil || file.fd < 0 {
77 if e := syscall.Close(file.fd); e != 0 {
78 err = &PathError{"close", file.name, Errno(e)}
81 if file.dirinfo != nil {
82 if libc_closedir(file.dirinfo.dir) < 0 && err == nil {
83 err = &PathError{"closedir", file.name, Errno(syscall.GetErrno())}
87 file.fd = -1 // so it can't be closed again
89 // no need for a finalizer anymore
90 runtime.SetFinalizer(file, nil)
94 // Stat returns the FileInfo structure describing file.
95 // It returns the FileInfo and an error, if any.
96 func (file *File) Stat() (fi *FileInfo, err Error) {
97 var stat syscall.Stat_t
98 e := syscall.Fstat(file.fd, &stat)
100 return nil, &PathError{"stat", file.name, Errno(e)}
102 return fileInfoFromStat(file.name, new(FileInfo), &stat, &stat), nil
105 // Stat returns a FileInfo structure describing the named file and an error, if any.
106 // If name names a valid symbolic link, the returned FileInfo describes
107 // the file pointed at by the link and has fi.FollowedSymlink set to true.
108 // If name names an invalid symbolic link, the returned FileInfo describes
109 // the link itself and has fi.FollowedSymlink set to false.
110 func Stat(name string) (fi *FileInfo, err Error) {
111 var lstat, stat syscall.Stat_t
112 e := syscall.Lstat(name, &lstat)
114 return nil, &PathError{"stat", name, Errno(e)}
117 if lstat.Mode&syscall.S_IFMT == syscall.S_IFLNK {
118 e := syscall.Stat(name, &stat)
123 return fileInfoFromStat(name, new(FileInfo), &lstat, statp), nil
126 // Lstat returns the FileInfo structure describing the named file and an
127 // error, if any. If the file is a symbolic link, the returned FileInfo
128 // describes the symbolic link. Lstat makes no attempt to follow the link.
129 func Lstat(name string) (fi *FileInfo, err Error) {
130 var stat syscall.Stat_t
131 e := syscall.Lstat(name, &stat)
133 return nil, &PathError{"lstat", name, Errno(e)}
135 return fileInfoFromStat(name, new(FileInfo), &stat, &stat), nil
138 // Readdir reads the contents of the directory associated with file and
139 // returns an array of up to n FileInfo structures, as would be returned
140 // by Lstat, in directory order. Subsequent calls on the same file will yield
141 // further FileInfos.
143 // If n > 0, Readdir returns at most n FileInfo structures. In this case, if
144 // Readdir returns an empty slice, it will return a non-nil error
145 // explaining why. At the end of a directory, the error is os.EOF.
147 // If n <= 0, Readdir returns all the FileInfo from the directory in
148 // a single slice. In this case, if Readdir succeeds (reads all
149 // the way to the end of the directory), it returns the slice and a
150 // nil os.Error. If it encounters an error before the end of the
151 // directory, Readdir returns the FileInfo read until that point
152 // and a non-nil error.
153 func (file *File) Readdir(n int) (fi []FileInfo, err Error) {
159 names, err := file.Readdirnames(n)
160 fi = make([]FileInfo, len(names))
161 for i, filename := range names {
162 fip, err := Lstat(dirname + filename)
163 if fip == nil || err != nil {
164 fi[i].Name = filename // rest is already zeroed out
172 // read reads up to len(b) bytes from the File.
173 // It returns the number of bytes read and an error, if any.
174 func (f *File) read(b []byte) (n int, err int) {
175 return syscall.Read(f.fd, b)
178 // pread reads len(b) bytes from the File starting at byte offset off.
179 // It returns the number of bytes read and the error, if any.
180 // EOF is signaled by a zero count with err set to 0.
181 func (f *File) pread(b []byte, off int64) (n int, err int) {
182 return syscall.Pread(f.fd, b, off)
185 // write writes len(b) bytes to the File.
186 // It returns the number of bytes written and an error, if any.
187 func (f *File) write(b []byte) (n int, err int) {
188 return syscall.Write(f.fd, b)
191 // pwrite writes len(b) bytes to the File starting at byte offset off.
192 // It returns the number of bytes written and an error, if any.
193 func (f *File) pwrite(b []byte, off int64) (n int, err int) {
194 return syscall.Pwrite(f.fd, b, off)
197 // seek sets the offset for the next Read or Write on file to offset, interpreted
198 // according to whence: 0 means relative to the origin of the file, 1 means
199 // relative to the current offset, and 2 means relative to the end.
200 // It returns the new offset and an error, if any.
201 func (f *File) seek(offset int64, whence int) (ret int64, err int) {
202 return syscall.Seek(f.fd, offset, whence)
205 // Truncate changes the size of the named file.
206 // If the file is a symbolic link, it changes the size of the link's target.
207 func Truncate(name string, size int64) Error {
208 if e := syscall.Truncate(name, size); e != 0 {
209 return &PathError{"truncate", name, Errno(e)}
214 // basename removes trailing slashes and the leading directory name from path name
215 func basename(name string) string {
217 // Remove trailing slashes
218 for ; i > 0 && name[i] == '/'; i-- {
221 // Remove leading directory name
222 for i--; i >= 0; i-- {
232 // Pipe returns a connected pair of Files; reads from r return bytes written to w.
233 // It returns the files and an Error, if any.
234 func Pipe() (r *File, w *File, err Error) {
237 // See ../syscall/exec.go for description of lock.
238 syscall.ForkLock.RLock()
239 e := syscall.Pipe(p[0:])
241 syscall.ForkLock.RUnlock()
242 return nil, nil, NewSyscallError("pipe", e)
244 syscall.CloseOnExec(p[0])
245 syscall.CloseOnExec(p[1])
246 syscall.ForkLock.RUnlock()
248 return NewFile(p[0], "|0"), NewFile(p[1], "|1"), nil