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 dragonfly freebsd linux netbsd openbsd solaris
15 // File represents an open file descriptor.
20 // file is the real representation of *File.
21 // The extra level of indirection ensures that no clients of os
22 // can overwrite this data, which could cause the finalizer
23 // to close the wrong file descriptor.
27 dirinfo
*dirInfo
// nil unless directory being read
28 nepipe
int32 // number of consecutive EPIPE in Write
31 // Fd returns the integer Unix file descriptor referencing the open file.
32 func (f
*File
) Fd() uintptr {
39 // NewFile returns a new File with the given file descriptor and name.
40 func NewFile(fd
uintptr, name
string) *File
{
45 f
:= &File
{&file
{fd
: fdi
, name
: name
}}
46 runtime
.SetFinalizer(f
.file
, (*file
).close)
50 // Auxiliary information if the File describes a directory
52 buf
[]byte // buffer for directory I/O
53 dir
*syscall
.DIR
// from opendir
56 func epipecheck(file
*File
, e error
) {
57 if e
== syscall
.EPIPE
{
58 if atomic
.AddInt32(&file
.nepipe
, 1) >= 10 {
62 atomic
.StoreInt32(&file
.nepipe
, 0)
66 // DevNull is the name of the operating system's ``null device.''
67 // On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
68 const DevNull
= "/dev/null"
70 // OpenFile is the generalized open call; most users will use Open
71 // or Create instead. It opens the named file with specified flag
72 // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
73 // methods on the returned File can be used for I/O.
74 // If there is an error, it will be of type *PathError.
75 func OpenFile(name
string, flag
int, perm FileMode
) (file
*File
, err error
) {
76 r
, e
:= syscall
.Open(name
, flag|syscall
.O_CLOEXEC
, syscallMode(perm
))
78 return nil, &PathError
{"open", name
, e
}
81 // There's a race here with fork/exec, which we are
82 // content to live with. See ../syscall/exec_unix.go.
83 // On OS X 10.6, the O_CLOEXEC flag is not respected.
84 // On OS X 10.7, the O_CLOEXEC flag works.
85 // Without a cheap & reliable way to detect 10.6 vs 10.7 at
86 // runtime, we just always call syscall.CloseOnExec on Darwin.
87 // Once >=10.7 is prevalent, this extra call can removed.
88 if syscall
.O_CLOEXEC
== 0 || runtime
.GOOS
== "darwin" { // O_CLOEXEC not supported
89 syscall
.CloseOnExec(r
)
92 return NewFile(uintptr(r
), name
), nil
95 // Close closes the File, rendering it unusable for I/O.
96 // It returns an error, if any.
97 func (f
*File
) Close() error
{
101 return f
.file
.close()
104 func (file
*file
) close() error
{
105 if file
== nil || file
.fd
< 0 {
106 return syscall
.EINVAL
109 if e
:= syscall
.Close(file
.fd
); e
!= nil {
110 err
= &PathError
{"close", file
.name
, e
}
113 if file
.dirinfo
!= nil {
114 syscall
.Entersyscall()
115 i
:= libc_closedir(file
.dirinfo
.dir
)
116 errno
:= syscall
.GetErrno()
117 syscall
.Exitsyscall()
119 if i
< 0 && err
== nil {
120 err
= &PathError
{"closedir", file
.name
, errno
}
124 file
.fd
= -1 // so it can't be closed again
126 // no need for a finalizer anymore
127 runtime
.SetFinalizer(file
, nil)
131 // Stat returns the FileInfo structure describing file.
132 // If there is an error, it will be of type *PathError.
133 func (f
*File
) Stat() (fi FileInfo
, err error
) {
135 return nil, ErrInvalid
137 var stat syscall
.Stat_t
138 err
= syscall
.Fstat(f
.fd
, &stat
)
140 return nil, &PathError
{"stat", f
.name
, err
}
142 return fileInfoFromStat(&stat
, f
.name
), nil
145 // Stat returns a FileInfo describing the named file.
146 // If there is an error, it will be of type *PathError.
147 func Stat(name
string) (fi FileInfo
, err error
) {
148 var stat syscall
.Stat_t
149 err
= syscall
.Stat(name
, &stat
)
151 return nil, &PathError
{"stat", name
, err
}
153 return fileInfoFromStat(&stat
, name
), nil
156 // Lstat returns a FileInfo describing the named file.
157 // If the file is a symbolic link, the returned FileInfo
158 // describes the symbolic link. Lstat makes no attempt to follow the link.
159 // If there is an error, it will be of type *PathError.
160 func Lstat(name
string) (fi FileInfo
, err error
) {
161 var stat syscall
.Stat_t
162 err
= syscall
.Lstat(name
, &stat
)
164 return nil, &PathError
{"lstat", name
, err
}
166 return fileInfoFromStat(&stat
, name
), nil
169 func (f
*File
) readdir(n
int) (fi
[]FileInfo
, err error
) {
174 names
, err
:= f
.Readdirnames(n
)
175 fi
= make([]FileInfo
, 0, len(names
))
176 for _
, filename
:= range names
{
177 fip
, lerr
:= lstat(dirname
+ "/" + filename
)
178 if IsNotExist(lerr
) {
179 // File disappeared between readdir + stat.
180 // Just treat it as if it didn't exist.
191 // read reads up to len(b) bytes from the File.
192 // It returns the number of bytes read and an error, if any.
193 func (f
*File
) read(b
[]byte) (n
int, err error
) {
194 return syscall
.Read(f
.fd
, b
)
197 // pread reads len(b) bytes from the File starting at byte offset off.
198 // It returns the number of bytes read and the error, if any.
199 // EOF is signaled by a zero count with err set to 0.
200 func (f
*File
) pread(b
[]byte, off
int64) (n
int, err error
) {
201 return syscall
.Pread(f
.fd
, b
, off
)
204 // write writes len(b) bytes to the File.
205 // It returns the number of bytes written and an error, if any.
206 func (f
*File
) write(b
[]byte) (n
int, err error
) {
208 m
, err
:= syscall
.Write(f
.fd
, b
)
211 // If the syscall wrote some data but not all (short write)
212 // or it returned EINTR, then assume it stopped early for
213 // reasons that are uninteresting to the caller, and try again.
214 if 0 < m
&& m
< len(b
) || err
== syscall
.EINTR
{
223 // pwrite writes len(b) bytes to the File starting at byte offset off.
224 // It returns the number of bytes written and an error, if any.
225 func (f
*File
) pwrite(b
[]byte, off
int64) (n
int, err error
) {
226 return syscall
.Pwrite(f
.fd
, b
, off
)
229 // seek sets the offset for the next Read or Write on file to offset, interpreted
230 // according to whence: 0 means relative to the origin of the file, 1 means
231 // relative to the current offset, and 2 means relative to the end.
232 // It returns the new offset and an error, if any.
233 func (f
*File
) seek(offset
int64, whence
int) (ret
int64, err error
) {
234 return syscall
.Seek(f
.fd
, offset
, whence
)
237 // Truncate changes the size of the named file.
238 // If the file is a symbolic link, it changes the size of the link's target.
239 // If there is an error, it will be of type *PathError.
240 func Truncate(name
string, size
int64) error
{
241 if e
:= syscall
.Truncate(name
, size
); e
!= nil {
242 return &PathError
{"truncate", name
, e
}
247 // Remove removes the named file or directory.
248 // If there is an error, it will be of type *PathError.
249 func Remove(name
string) error
{
250 // System call interface forces us to know
251 // whether name is a file or directory.
252 // Try both: it is cheaper on average than
253 // doing a Stat plus the right one.
254 e
:= syscall
.Unlink(name
)
258 e1
:= syscall
.Rmdir(name
)
263 // Both failed: figure out which error to return.
264 // OS X and Linux differ on whether unlink(dir)
265 // returns EISDIR, so can't use that. However,
266 // both agree that rmdir(file) returns ENOTDIR,
267 // so we can use that to decide which error is real.
268 // Rmdir might also return ENOTDIR if given a bad
269 // file path, like /etc/passwd/foo, but in that case,
270 // both errors will be ENOTDIR, so it's okay to
271 // use the error from unlink.
272 if e1
!= syscall
.ENOTDIR
{
275 return &PathError
{"remove", name
, e
}
278 // basename removes trailing slashes and the leading directory name from path name
279 func basename(name
string) string {
281 // Remove trailing slashes
282 for ; i
> 0 && name
[i
] == '/'; i
-- {
285 // Remove leading directory name
286 for i
--; i
>= 0; i
-- {
296 // TempDir returns the default directory to use for temporary files.
297 func TempDir() string {
298 dir
:= Getenv("TMPDIR")