diff options
Diffstat (limited to 'src/pkg/os/file_plan9.go')
-rw-r--r-- | src/pkg/os/file_plan9.go | 144 |
1 files changed, 100 insertions, 44 deletions
diff --git a/src/pkg/os/file_plan9.go b/src/pkg/os/file_plan9.go index cb0e9ef92..d6d39a899 100644 --- a/src/pkg/os/file_plan9.go +++ b/src/pkg/os/file_plan9.go @@ -5,14 +5,11 @@ package os import ( - "errors" "runtime" "syscall" "time" ) -var ErrPlan9 = errors.New("unimplemented on Plan 9") - // File represents an open file descriptor. type File struct { *file @@ -107,7 +104,6 @@ func OpenFile(name string, flag int, perm FileMode) (file *File, err error) { append = true } - syscall.ForkLock.RLock() if (create && trunc) || excl { fd, e = syscall.Create(name, flag, syscallMode(perm)) } else { @@ -120,7 +116,6 @@ func OpenFile(name string, flag int, perm FileMode) (file *File, err error) { } } } - syscall.ForkLock.RUnlock() if e != nil { return nil, &PathError{"open", name, e} @@ -137,8 +132,8 @@ func OpenFile(name string, flag int, perm FileMode) (file *File, err error) { // Close closes the File, rendering it unusable for I/O. // It returns an error, if any. -func (file *File) Close() error { - return file.file.close() +func (f *File) Close() error { + return f.file.close() } func (file *file) close() error { @@ -159,8 +154,8 @@ func (file *file) close() error { } // Stat returns the FileInfo structure describing file. -// It returns the FileInfo and an error, if any. -func (f *File) Stat() (FileInfo, error) { +// If there is an error, it will be of type *PathError. +func (f *File) Stat() (fi FileInfo, err error) { d, err := dirstat(f) if err != nil { return nil, err @@ -170,14 +165,20 @@ func (f *File) Stat() (FileInfo, error) { // Truncate changes the size of the file. // It does not change the I/O offset. +// If there is an error, it will be of type *PathError. func (f *File) Truncate(size int64) error { - var d Dir - d.Null() + var d syscall.Dir - d.Length = uint64(size) + d.Null() + d.Length = size - if e := syscall.Fwstat(f.fd, pdir(nil, &d)); e != nil { - return &PathError{"truncate", f.name, e} + var buf [syscall.STATFIXLEN]byte + n, err := d.Marshal(buf[:]) + if err != nil { + return &PathError{"truncate", f.name, err} + } + if err = syscall.Fwstat(f.fd, buf[:n]); err != nil { + return &PathError{"truncate", f.name, err} } return nil } @@ -187,7 +188,7 @@ const chmodMask = uint32(syscall.DMAPPEND | syscall.DMEXCL | syscall.DMTMP | Mod // Chmod changes the mode of the file to mode. // If there is an error, it will be of type *PathError. func (f *File) Chmod(mode FileMode) error { - var d Dir + var d syscall.Dir odir, e := dirstat(f) if e != nil { @@ -195,8 +196,14 @@ func (f *File) Chmod(mode FileMode) error { } d.Null() d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask - if e := syscall.Fwstat(f.fd, pdir(nil, &d)); e != nil { - return &PathError{"chmod", f.name, e} + + var buf [syscall.STATFIXLEN]byte + n, err := d.Marshal(buf[:]) + if err != nil { + return &PathError{"chmod", f.name, err} + } + if err = syscall.Fwstat(f.fd, buf[:n]); err != nil { + return &PathError{"chmod", f.name, err} } return nil } @@ -208,12 +215,16 @@ func (f *File) Sync() (err error) { if f == nil { return ErrInvalid } - - var d Dir + var d syscall.Dir d.Null() - if e := syscall.Fwstat(f.fd, pdir(nil, &d)); e != nil { - return NewSyscallError("fsync", e) + var buf [syscall.STATFIXLEN]byte + n, err := d.Marshal(buf[:]) + if err != nil { + return NewSyscallError("fsync", err) + } + if err = syscall.Fwstat(f.fd, buf[:n]); err != nil { + return NewSyscallError("fsync", err) } return nil } @@ -233,13 +244,23 @@ func (f *File) pread(b []byte, off int64) (n int, err error) { // write writes len(b) bytes to the File. // It returns the number of bytes written and an error, if any. +// Since Plan 9 preserves message boundaries, never allow +// a zero-byte write. func (f *File) write(b []byte) (n int, err error) { + if len(b) == 0 { + return 0, nil + } return syscall.Write(f.fd, b) } // pwrite writes len(b) bytes to the File starting at byte offset off. // It returns the number of bytes written and an error, if any. +// Since Plan 9 preserves message boundaries, never allow +// a zero-byte write. func (f *File) pwrite(b []byte, off int64) (n int, err error) { + if len(b) == 0 { + return 0, nil + } return syscall.Pwrite(f.fd, b, off) } @@ -255,13 +276,18 @@ func (f *File) seek(offset int64, whence int) (ret int64, err error) { // If the file is a symbolic link, it changes the size of the link's target. // If there is an error, it will be of type *PathError. func Truncate(name string, size int64) error { - var d Dir - d.Null() + var d syscall.Dir - d.Length = uint64(size) + d.Null() + d.Length = size - if e := syscall.Wstat(name, pdir(nil, &d)); e != nil { - return &PathError{"truncate", name, e} + var buf [syscall.STATFIXLEN]byte + n, err := d.Marshal(buf[:]) + if err != nil { + return &PathError{"truncate", name, err} + } + if err = syscall.Wstat(name, buf[:n]); err != nil { + return &PathError{"truncate", name, err} } return nil } @@ -277,21 +303,27 @@ func Remove(name string) error { // Rename renames a file. func Rename(oldname, newname string) error { - var d Dir - d.Null() + var d syscall.Dir + d.Null() d.Name = newname - if e := syscall.Wstat(oldname, pdir(nil, &d)); e != nil { - return &PathError{"rename", oldname, e} + buf := make([]byte, syscall.STATFIXLEN+len(d.Name)) + n, err := d.Marshal(buf[:]) + if err != nil { + return &PathError{"rename", oldname, err} + } + if err = syscall.Wstat(oldname, buf[:n]); err != nil { + return &PathError{"rename", oldname, err} } return nil } // Chmod changes the mode of the named file to mode. +// If the file is a symbolic link, it changes the mode of the link's target. // If there is an error, it will be of type *PathError. func Chmod(name string, mode FileMode) error { - var d Dir + var d syscall.Dir odir, e := dirstat(name) if e != nil { @@ -299,8 +331,14 @@ func Chmod(name string, mode FileMode) error { } d.Null() d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask - if e := syscall.Wstat(name, pdir(nil, &d)); e != nil { - return &PathError{"chmod", name, e} + + var buf [syscall.STATFIXLEN]byte + n, err := d.Marshal(buf[:]) + if err != nil { + return &PathError{"chmod", name, err} + } + if err = syscall.Wstat(name, buf[:n]); err != nil { + return &PathError{"chmod", name, err} } return nil } @@ -310,19 +348,27 @@ func Chmod(name string, mode FileMode) error { // // The underlying filesystem may truncate or round the values to a // less precise time unit. +// If there is an error, it will be of type *PathError. func Chtimes(name string, atime time.Time, mtime time.Time) error { - var d Dir - d.Null() + var d syscall.Dir + d.Null() d.Atime = uint32(atime.Unix()) d.Mtime = uint32(mtime.Unix()) - if e := syscall.Wstat(name, pdir(nil, &d)); e != nil { - return &PathError{"chtimes", name, e} + var buf [syscall.STATFIXLEN]byte + n, err := d.Marshal(buf[:]) + if err != nil { + return &PathError{"chtimes", name, err} + } + if err = syscall.Wstat(name, buf[:n]); err != nil { + return &PathError{"chtimes", name, err} } return nil } +// Pipe returns a connected pair of Files; reads from r return bytes +// written to w. It returns the files and an error, if any. func Pipe() (r *File, w *File, err error) { var p [2]int @@ -338,32 +384,42 @@ func Pipe() (r *File, w *File, err error) { // not supported on Plan 9 -// Link creates a hard link. +// Link creates newname as a hard link to the oldname file. // If there is an error, it will be of type *LinkError. func Link(oldname, newname string) error { - return &LinkError{"link", oldname, newname, ErrPlan9} + return &LinkError{"link", oldname, newname, syscall.EPLAN9} } // Symlink creates newname as a symbolic link to oldname. // If there is an error, it will be of type *LinkError. func Symlink(oldname, newname string) error { - return &LinkError{"symlink", oldname, newname, ErrPlan9} + return &LinkError{"symlink", oldname, newname, syscall.EPLAN9} } +// Readlink returns the destination of the named symbolic link. +// If there is an error, it will be of type *PathError. func Readlink(name string) (string, error) { - return "", ErrPlan9 + return "", &PathError{"readlink", name, syscall.EPLAN9} } +// Chown changes the numeric uid and gid of the named file. +// If the file is a symbolic link, it changes the uid and gid of the link's target. +// If there is an error, it will be of type *PathError. func Chown(name string, uid, gid int) error { - return ErrPlan9 + return &PathError{"chown", name, syscall.EPLAN9} } +// Lchown changes the numeric uid and gid of the named file. +// If the file is a symbolic link, it changes the uid and gid of the link itself. +// If there is an error, it will be of type *PathError. func Lchown(name string, uid, gid int) error { - return ErrPlan9 + return &PathError{"lchown", name, syscall.EPLAN9} } +// Chown changes the numeric uid and gid of the named file. +// If there is an error, it will be of type *PathError. func (f *File) Chown(uid, gid int) error { - return ErrPlan9 + return &PathError{"chown", f.name, syscall.EPLAN9} } // TempDir returns the default directory to use for temporary files. |