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.
14 var ErrPlan9 = errors.New("unimplemented on Plan 9")
16 // File represents an open file descriptor.
21 // file is the real representation of *File.
22 // The extra level of indirection ensures that no clients of os
23 // can overwrite this data, which could cause the finalizer
24 // to close the wrong file descriptor.
28 dirinfo *dirInfo // nil unless directory being read
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 [syscall.STATMAX]byte // buffer for directory I/O
53 nbuf int // length of buf; return value from Read
54 bufp int // location of next record in buf.
57 func epipecheck(file *File, e error) {
60 // DevNull is the name of the operating system's ``null device.''
61 // On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
62 const DevNull = "/dev/null"
64 // syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
65 func syscallMode(i FileMode) (o uint32) {
67 if i&ModeAppend != 0 {
70 if i&ModeExclusive != 0 {
73 if i&ModeTemporary != 0 {
79 // OpenFile is the generalized open call; most users will use Open
80 // or Create instead. It opens the named file with specified flag
81 // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
82 // methods on the returned File can be used for I/O.
83 // If there is an error, it will be of type *PathError.
84 func OpenFile(name string, flag int, perm FileMode) (file *File, err error) {
94 if flag&O_CREATE == O_CREATE {
95 flag = flag & ^O_CREATE
98 if flag&O_EXCL == O_EXCL {
101 if flag&O_TRUNC == O_TRUNC {
104 // O_APPEND is emulated on Plan 9
105 if flag&O_APPEND == O_APPEND {
106 flag = flag &^ O_APPEND
110 syscall.ForkLock.RLock()
111 if (create && trunc) || excl {
112 fd, e = syscall.Create(name, flag, syscallMode(perm))
114 fd, e = syscall.Open(name, flag)
115 if e != nil && create {
117 fd, e1 = syscall.Create(name, flag, syscallMode(perm))
123 syscall.ForkLock.RUnlock()
126 return nil, &PathError{"open", name, e}
130 if _, e = syscall.Seek(fd, 0, SEEK_END); e != nil {
131 return nil, &PathError{"seek", name, e}
135 return NewFile(uintptr(fd), name), nil
138 // Close closes the File, rendering it unusable for I/O.
139 // It returns an error, if any.
140 func (file *File) Close() error {
141 return file.file.close()
144 func (file *file) close() error {
145 if file == nil || file.fd < 0 {
149 syscall.ForkLock.RLock()
150 if e := syscall.Close(file.fd); e != nil {
151 err = &PathError{"close", file.name, e}
153 syscall.ForkLock.RUnlock()
154 file.fd = -1 // so it can't be closed again
156 // no need for a finalizer anymore
157 runtime.SetFinalizer(file, nil)
161 // Stat returns the FileInfo structure describing file.
162 // It returns the FileInfo and an error, if any.
163 func (f *File) Stat() (FileInfo, error) {
168 return fileInfoFromStat(d), nil
171 // Truncate changes the size of the file.
172 // It does not change the I/O offset.
173 func (f *File) Truncate(size int64) error {
177 d.Length = uint64(size)
179 if e := syscall.Fwstat(f.fd, pdir(nil, &d)); e != nil {
180 return &PathError{"truncate", f.name, e}
185 const chmodMask = uint32(syscall.DMAPPEND | syscall.DMEXCL | syscall.DMTMP | ModePerm)
187 // Chmod changes the mode of the file to mode.
188 // If there is an error, it will be of type *PathError.
189 func (f *File) Chmod(mode FileMode) error {
192 odir, e := dirstat(f)
194 return &PathError{"chmod", f.name, e}
197 d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask
198 if e := syscall.Fwstat(f.fd, pdir(nil, &d)); e != nil {
199 return &PathError{"chmod", f.name, e}
204 // Sync commits the current contents of the file to stable storage.
205 // Typically, this means flushing the file system's in-memory copy
206 // of recently written data to disk.
207 func (f *File) Sync() (err error) {
215 if e := syscall.Fwstat(f.fd, pdir(nil, &d)); e != nil {
216 return NewSyscallError("fsync", e)
221 // read reads up to len(b) bytes from the File.
222 // It returns the number of bytes read and an error, if any.
223 func (f *File) read(b []byte) (n int, err error) {
224 return syscall.Read(f.fd, b)
227 // pread reads len(b) bytes from the File starting at byte offset off.
228 // It returns the number of bytes read and the error, if any.
229 // EOF is signaled by a zero count with err set to nil.
230 func (f *File) pread(b []byte, off int64) (n int, err error) {
231 return syscall.Pread(f.fd, b, off)
234 // write writes len(b) bytes to the File.
235 // It returns the number of bytes written and an error, if any.
236 func (f *File) write(b []byte) (n int, err error) {
237 return syscall.Write(f.fd, b)
240 // pwrite writes len(b) bytes to the File starting at byte offset off.
241 // It returns the number of bytes written and an error, if any.
242 func (f *File) pwrite(b []byte, off int64) (n int, err error) {
243 return syscall.Pwrite(f.fd, b, off)
246 // seek sets the offset for the next Read or Write on file to offset, interpreted
247 // according to whence: 0 means relative to the origin of the file, 1 means
248 // relative to the current offset, and 2 means relative to the end.
249 // It returns the new offset and an error, if any.
250 func (f *File) seek(offset int64, whence int) (ret int64, err error) {
251 return syscall.Seek(f.fd, offset, whence)
254 // Truncate changes the size of the named file.
255 // If the file is a symbolic link, it changes the size of the link's target.
256 // If there is an error, it will be of type *PathError.
257 func Truncate(name string, size int64) error {
261 d.Length = uint64(size)
263 if e := syscall.Wstat(name, pdir(nil, &d)); e != nil {
264 return &PathError{"truncate", name, e}
269 // Remove removes the named file or directory.
270 // If there is an error, it will be of type *PathError.
271 func Remove(name string) error {
272 if e := syscall.Remove(name); e != nil {
273 return &PathError{"remove", name, e}
278 // Rename renames a file.
279 func Rename(oldname, newname string) error {
285 if e := syscall.Wstat(oldname, pdir(nil, &d)); e != nil {
286 return &PathError{"rename", oldname, e}
291 // Chmod changes the mode of the named file to mode.
292 // If there is an error, it will be of type *PathError.
293 func Chmod(name string, mode FileMode) error {
296 odir, e := dirstat(name)
298 return &PathError{"chmod", name, e}
301 d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask
302 if e := syscall.Wstat(name, pdir(nil, &d)); e != nil {
303 return &PathError{"chmod", name, e}
308 // Chtimes changes the access and modification times of the named
309 // file, similar to the Unix utime() or utimes() functions.
311 // The underlying filesystem may truncate or round the values to a
312 // less precise time unit.
313 func Chtimes(name string, atime time.Time, mtime time.Time) error {
317 d.Atime = uint32(atime.Unix())
318 d.Mtime = uint32(mtime.Unix())
320 if e := syscall.Wstat(name, pdir(nil, &d)); e != nil {
321 return &PathError{"chtimes", name, e}
326 func Pipe() (r *File, w *File, err error) {
329 syscall.ForkLock.RLock()
330 if e := syscall.Pipe(p[0:]); e != nil {
331 syscall.ForkLock.RUnlock()
332 return nil, nil, NewSyscallError("pipe", e)
334 syscall.ForkLock.RUnlock()
336 return NewFile(uintptr(p[0]), "|0"), NewFile(uintptr(p[1]), "|1"), nil
339 // not supported on Plan 9
341 // Link creates a hard link.
342 // If there is an error, it will be of type *LinkError.
343 func Link(oldname, newname string) error {
344 return &LinkError{"link", oldname, newname, ErrPlan9}
347 // Symlink creates newname as a symbolic link to oldname.
348 // If there is an error, it will be of type *LinkError.
349 func Symlink(oldname, newname string) error {
350 return &LinkError{"symlink", oldname, newname, ErrPlan9}
353 func Readlink(name string) (string, error) {
357 func Chown(name string, uid, gid int) error {
361 func Lchown(name string, uid, gid int) error {
365 func (f *File) Chown(uid, gid int) error {
369 // TempDir returns the default directory to use for temporary files.
370 func TempDir() string {