1 // Copyright 2011 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.
13 // File represents an open file descriptor.
18 // file is the real representation of *File.
19 // The extra level of indirection ensures that no clients of os
20 // can overwrite this data, which could cause the finalizer
21 // to close the wrong file descriptor.
25 dirinfo *dirInfo // nil unless directory being read
28 // Fd returns the integer Unix file descriptor referencing the open file.
29 func (f *File) Fd() uintptr {
36 // NewFile returns a new File with the given file descriptor and name.
37 func NewFile(fd uintptr, name string) *File {
42 f := &File{&file{fd: fdi, name: name}}
43 runtime.SetFinalizer(f.file, (*file).close)
47 // Auxiliary information if the File describes a directory
49 buf [syscall.STATMAX]byte // buffer for directory I/O
50 nbuf int // length of buf; return value from Read
51 bufp int // location of next record in buf.
54 func epipecheck(file *File, e error) {
57 // DevNull is the name of the operating system's ``null device.''
58 // On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
59 const DevNull = "/dev/null"
61 // syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
62 func syscallMode(i FileMode) (o uint32) {
64 if i&ModeAppend != 0 {
67 if i&ModeExclusive != 0 {
70 if i&ModeTemporary != 0 {
76 // OpenFile is the generalized open call; most users will use Open
77 // or Create instead. It opens the named file with specified flag
78 // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
79 // methods on the returned File can be used for I/O.
80 // If there is an error, it will be of type *PathError.
81 func OpenFile(name string, flag int, perm FileMode) (file *File, err error) {
91 if flag&O_CREATE == O_CREATE {
92 flag = flag & ^O_CREATE
95 if flag&O_EXCL == O_EXCL {
98 if flag&O_TRUNC == O_TRUNC {
101 // O_APPEND is emulated on Plan 9
102 if flag&O_APPEND == O_APPEND {
103 flag = flag &^ O_APPEND
107 syscall.ForkLock.RLock()
108 if (create && trunc) || excl {
109 fd, e = syscall.Create(name, flag, syscallMode(perm))
111 fd, e = syscall.Open(name, flag)
112 if e != nil && create {
114 fd, e1 = syscall.Create(name, flag, syscallMode(perm))
120 syscall.ForkLock.RUnlock()
123 return nil, &PathError{"open", name, e}
127 if _, e = syscall.Seek(fd, 0, SEEK_END); e != nil {
128 return nil, &PathError{"seek", name, e}
132 return NewFile(uintptr(fd), name), nil
135 // Close closes the File, rendering it unusable for I/O.
136 // It returns an error, if any.
137 func (f *File) Close() error {
138 return f.file.close()
141 func (file *file) close() error {
142 if file == nil || file.fd < 0 {
146 syscall.ForkLock.RLock()
147 if e := syscall.Close(file.fd); e != nil {
148 err = &PathError{"close", file.name, e}
150 syscall.ForkLock.RUnlock()
151 file.fd = -1 // so it can't be closed again
153 // no need for a finalizer anymore
154 runtime.SetFinalizer(file, nil)
158 // Stat returns the FileInfo structure describing file.
159 // If there is an error, it will be of type *PathError.
160 func (f *File) Stat() (fi FileInfo, err error) {
165 return fileInfoFromStat(d), nil
168 // Truncate changes the size of the file.
169 // It does not change the I/O offset.
170 // If there is an error, it will be of type *PathError.
171 func (f *File) Truncate(size int64) error {
177 var buf [syscall.STATFIXLEN]byte
178 n, err := d.Marshal(buf[:])
180 return &PathError{"truncate", f.name, err}
182 if err = syscall.Fwstat(f.fd, buf[:n]); err != nil {
183 return &PathError{"truncate", f.name, err}
188 const chmodMask = uint32(syscall.DMAPPEND | syscall.DMEXCL | syscall.DMTMP | ModePerm)
190 // Chmod changes the mode of the file to mode.
191 // If there is an error, it will be of type *PathError.
192 func (f *File) Chmod(mode FileMode) error {
195 odir, e := dirstat(f)
197 return &PathError{"chmod", f.name, e}
200 d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask
202 var buf [syscall.STATFIXLEN]byte
203 n, err := d.Marshal(buf[:])
205 return &PathError{"chmod", f.name, err}
207 if err = syscall.Fwstat(f.fd, buf[:n]); err != nil {
208 return &PathError{"chmod", f.name, err}
213 // Sync commits the current contents of the file to stable storage.
214 // Typically, this means flushing the file system's in-memory copy
215 // of recently written data to disk.
216 func (f *File) Sync() (err error) {
223 var buf [syscall.STATFIXLEN]byte
224 n, err := d.Marshal(buf[:])
226 return NewSyscallError("fsync", err)
228 if err = syscall.Fwstat(f.fd, buf[:n]); err != nil {
229 return NewSyscallError("fsync", err)
234 // read reads up to len(b) bytes from the File.
235 // It returns the number of bytes read and an error, if any.
236 func (f *File) read(b []byte) (n int, err error) {
237 return syscall.Read(f.fd, b)
240 // pread reads len(b) bytes from the File starting at byte offset off.
241 // It returns the number of bytes read and the error, if any.
242 // EOF is signaled by a zero count with err set to nil.
243 func (f *File) pread(b []byte, off int64) (n int, err error) {
244 return syscall.Pread(f.fd, b, off)
247 // write writes len(b) bytes to the File.
248 // It returns the number of bytes written and an error, if any.
249 func (f *File) write(b []byte) (n int, err error) {
250 return syscall.Write(f.fd, b)
253 // pwrite writes len(b) bytes to the File starting at byte offset off.
254 // It returns the number of bytes written and an error, if any.
255 func (f *File) pwrite(b []byte, off int64) (n int, err error) {
256 return syscall.Pwrite(f.fd, b, off)
259 // seek sets the offset for the next Read or Write on file to offset, interpreted
260 // according to whence: 0 means relative to the origin of the file, 1 means
261 // relative to the current offset, and 2 means relative to the end.
262 // It returns the new offset and an error, if any.
263 func (f *File) seek(offset int64, whence int) (ret int64, err error) {
264 return syscall.Seek(f.fd, offset, whence)
267 // Truncate changes the size of the named file.
268 // If the file is a symbolic link, it changes the size of the link's target.
269 // If there is an error, it will be of type *PathError.
270 func Truncate(name string, size int64) error {
276 var buf [syscall.STATFIXLEN]byte
277 n, err := d.Marshal(buf[:])
279 return &PathError{"truncate", name, err}
281 if err = syscall.Wstat(name, buf[:n]); err != nil {
282 return &PathError{"truncate", name, err}
287 // Remove removes the named file or directory.
288 // If there is an error, it will be of type *PathError.
289 func Remove(name string) error {
290 if e := syscall.Remove(name); e != nil {
291 return &PathError{"remove", name, e}
296 // Rename renames a file.
297 func Rename(oldname, newname string) error {
303 var buf [syscall.STATFIXLEN]byte
304 n, err := d.Marshal(buf[:])
306 return &PathError{"rename", oldname, err}
308 if err = syscall.Wstat(oldname, buf[:n]); err != nil {
309 return &PathError{"rename", oldname, err}
314 // Chmod changes the mode of the named file to mode.
315 // If the file is a symbolic link, it changes the mode of the link's target.
316 // If there is an error, it will be of type *PathError.
317 func Chmod(name string, mode FileMode) error {
320 odir, e := dirstat(name)
322 return &PathError{"chmod", name, e}
325 d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask
327 var buf [syscall.STATFIXLEN]byte
328 n, err := d.Marshal(buf[:])
330 return &PathError{"chmod", name, err}
332 if err = syscall.Wstat(name, buf[:n]); err != nil {
333 return &PathError{"chmod", name, err}
338 // Chtimes changes the access and modification times of the named
339 // file, similar to the Unix utime() or utimes() functions.
341 // The underlying filesystem may truncate or round the values to a
342 // less precise time unit.
343 // If there is an error, it will be of type *PathError.
344 func Chtimes(name string, atime time.Time, mtime time.Time) error {
348 d.Atime = uint32(atime.Unix())
349 d.Mtime = uint32(mtime.Unix())
351 var buf [syscall.STATFIXLEN]byte
352 n, err := d.Marshal(buf[:])
354 return &PathError{"chtimes", name, err}
356 if err = syscall.Wstat(name, buf[:n]); err != nil {
357 return &PathError{"chtimes", name, err}
362 // Pipe returns a connected pair of Files; reads from r return bytes
363 // written to w. It returns the files and an error, if any.
364 func Pipe() (r *File, w *File, err error) {
367 syscall.ForkLock.RLock()
368 if e := syscall.Pipe(p[0:]); e != nil {
369 syscall.ForkLock.RUnlock()
370 return nil, nil, NewSyscallError("pipe", e)
372 syscall.ForkLock.RUnlock()
374 return NewFile(uintptr(p[0]), "|0"), NewFile(uintptr(p[1]), "|1"), nil
377 // not supported on Plan 9
379 // Link creates newname as a hard link to the oldname file.
380 // If there is an error, it will be of type *LinkError.
381 func Link(oldname, newname string) error {
382 return &LinkError{"link", oldname, newname, syscall.EPLAN9}
385 // Symlink creates newname as a symbolic link to oldname.
386 // If there is an error, it will be of type *LinkError.
387 func Symlink(oldname, newname string) error {
388 return &LinkError{"symlink", oldname, newname, syscall.EPLAN9}
391 // Readlink returns the destination of the named symbolic link.
392 // If there is an error, it will be of type *PathError.
393 func Readlink(name string) (string, error) {
394 return "", &PathError{"readlink", name, syscall.EPLAN9}
397 // Chown changes the numeric uid and gid of the named file.
398 // If the file is a symbolic link, it changes the uid and gid of the link's target.
399 // If there is an error, it will be of type *PathError.
400 func Chown(name string, uid, gid int) error {
401 return &PathError{"chown", name, syscall.EPLAN9}
404 // Lchown changes the numeric uid and gid of the named file.
405 // If the file is a symbolic link, it changes the uid and gid of the link itself.
406 // If there is an error, it will be of type *PathError.
407 func Lchown(name string, uid, gid int) error {
408 return &PathError{"lchown", name, syscall.EPLAN9}
411 // Chown changes the numeric uid and gid of the named file.
412 // If there is an error, it will be of type *PathError.
413 func (f *File) Chown(uid, gid int) error {
414 return &PathError{"chown", f.name, syscall.EPLAN9}
417 // TempDir returns the default directory to use for temporary files.
418 func TempDir() string {