package os
import (
"runtime"
"syscall"
)
type File struct {
fd int
name string
dirinfo *dirInfo
nepipe int
}
func (file *File) Fd() int { return file.fd }
func (file *File) Name() string { return file.name }
func NewFile(fd int, name string) *File {
if fd < 0 {
return nil
}
f := &File{fd, name, nil, 0}
runtime.SetFinalizer(f, (*File).Close)
return f
}
var (
Stdin = NewFile(syscall.Stdin, "/dev/stdin")
Stdout = NewFile(syscall.Stdout, "/dev/stdout")
Stderr = NewFile(syscall.Stderr, "/dev/stderr")
)
const (
O_RDONLY = syscall.O_RDONLY
O_WRONLY = syscall.O_WRONLY
O_RDWR = syscall.O_RDWR
O_APPEND = syscall.O_APPEND
O_ASYNC = syscall.O_ASYNC
O_CREAT = syscall.O_CREAT
O_EXCL = syscall.O_EXCL
O_NOCTTY = syscall.O_NOCTTY
O_NONBLOCK = syscall.O_NONBLOCK
O_NDELAY = O_NONBLOCK
O_SYNC = syscall.O_SYNC
O_TRUNC = syscall.O_TRUNC
O_CREATE = O_CREAT
)
type eofError int
func (eofError) String() string { return "EOF" }
var EOF Error = eofError(0)
func (file *File) Read(b []byte) (n int, err Error) {
if file == nil {
return 0, EINVAL
}
n, e := syscall.Read(file.fd, b)
if n < 0 {
n = 0
}
if n == 0 && e == 0 {
return 0, EOF
}
if e != 0 {
err = &PathError{"read", file.name, Errno(e)}
}
return n, err
}
func (file *File) ReadAt(b []byte, off int64) (n int, err Error) {
if file == nil {
return 0, EINVAL
}
for len(b) > 0 {
m, e := syscall.Pread(file.fd, b, off)
if m == 0 && e == 0 {
return n, EOF
}
if e != 0 {
err = &PathError{"read", file.name, Errno(e)}
break
}
n += m
b = b[m:]
off += int64(m)
}
return
}
func (file *File) Write(b []byte) (n int, err Error) {
if file == nil {
return 0, EINVAL
}
n, e := syscall.Write(file.fd, b)
if n < 0 {
n = 0
}
if e == syscall.EPIPE {
file.nepipe++
if file.nepipe >= 10 {
Exit(syscall.EPIPE)
}
} else {
file.nepipe = 0
}
if e != 0 {
err = &PathError{"write", file.name, Errno(e)}
}
return n, err
}
func (file *File) WriteAt(b []byte, off int64) (n int, err Error) {
if file == nil {
return 0, EINVAL
}
for len(b) > 0 {
m, e := syscall.Pwrite(file.fd, b, off)
if e != 0 {
err = &PathError{"write", file.name, Errno(e)}
break
}
n += m
b = b[m:]
off += int64(m)
}
return
}
func (file *File) Seek(offset int64, whence int) (ret int64, err Error) {
r, e := syscall.Seek(file.fd, offset, whence)
if e == 0 && file.dirinfo != nil && r != 0 {
e = syscall.EISDIR
}
if e != 0 {
return 0, &PathError{"seek", file.name, Errno(e)}
}
return r, nil
}
func (file *File) WriteString(s string) (ret int, err Error) {
if file == nil {
return 0, EINVAL
}
b := syscall.StringByteSlice(s)
b = b[0 : len(b)-1]
return file.Write(b)
}
func Pipe() (r *File, w *File, err Error) {
var p [2]int
syscall.ForkLock.RLock()
e := syscall.Pipe(p[0:])
if e != 0 {
syscall.ForkLock.RUnlock()
return nil, nil, NewSyscallError("pipe", e)
}
syscall.CloseOnExec(p[0])
syscall.CloseOnExec(p[1])
syscall.ForkLock.RUnlock()
return NewFile(p[0], "|0"), NewFile(p[1], "|1"), nil
}
func Mkdir(name string, perm int) Error {
e := syscall.Mkdir(name, perm)
if e != 0 {
return &PathError{"mkdir", name, Errno(e)}
}
return nil
}
func Stat(name string) (fi *FileInfo, err Error) {
var lstat, stat syscall.Stat_t
e := syscall.Lstat(name, &lstat)
if e != 0 {
return nil, &PathError{"stat", name, Errno(e)}
}
statp := &lstat
if lstat.Mode&syscall.S_IFMT == syscall.S_IFLNK {
e := syscall.Stat(name, &stat)
if e == 0 {
statp = &stat
}
}
return fileInfoFromStat(name, new(FileInfo), &lstat, statp), nil
}
func Lstat(name string) (fi *FileInfo, err Error) {
var stat syscall.Stat_t
e := syscall.Lstat(name, &stat)
if e != 0 {
return nil, &PathError{"lstat", name, Errno(e)}
}
return fileInfoFromStat(name, new(FileInfo), &stat, &stat), nil
}
func Chdir(dir string) Error {
if e := syscall.Chdir(dir); e != 0 {
return &PathError{"chdir", dir, Errno(e)}
}
return nil
}
func (f *File) Chdir() Error {
if e := syscall.Fchdir(f.fd); e != 0 {
return &PathError{"chdir", f.name, Errno(e)}
}
return nil
}
func Remove(name string) Error {
e := syscall.Unlink(name)
if e == 0 {
return nil
}
e1 := syscall.Rmdir(name)
if e1 == 0 {
return nil
}
if e1 != syscall.ENOTDIR {
e = e1
}
return &PathError{"remove", name, Errno(e)}
}
type LinkError struct {
Op string
Old string
New string
Error Error
}
func (e *LinkError) String() string {
return e.Op + " " + e.Old + " " + e.New + ": " + e.Error.String()
}
func Link(oldname, newname string) Error {
e := syscall.Link(oldname, newname)
if e != 0 {
return &LinkError{"link", oldname, newname, Errno(e)}
}
return nil
}
func Symlink(oldname, newname string) Error {
e := syscall.Symlink(oldname, newname)
if e != 0 {
return &LinkError{"symlink", oldname, newname, Errno(e)}
}
return nil
}
func Readlink(name string) (string, Error) {
for len := 128; ; len *= 2 {
b := make([]byte, len)
n, e := syscall.Readlink(name, b)
if e != 0 {
return "", &PathError{"readlink", name, Errno(e)}
}
if n < len {
return string(b[0:n]), nil
}
}
return "", nil
}
func Rename(oldname, newname string) Error {
e := syscall.Rename(oldname, newname)
if e != 0 {
return &LinkError{"rename", oldname, newname, Errno(e)}
}
return nil
}
func Chmod(name string, mode int) Error {
if e := syscall.Chmod(name, mode); e != 0 {
return &PathError{"chmod", name, Errno(e)}
}
return nil
}
func (f *File) Chmod(mode int) Error {
if e := syscall.Fchmod(f.fd, mode); e != 0 {
return &PathError{"chmod", f.name, Errno(e)}
}
return nil
}
func Chown(name string, uid, gid int) Error {
if e := syscall.Chown(name, uid, gid); e != 0 {
return &PathError{"chown", name, Errno(e)}
}
return nil
}
func Lchown(name string, uid, gid int) Error {
if e := syscall.Lchown(name, uid, gid); e != 0 {
return &PathError{"lchown", name, Errno(e)}
}
return nil
}
func (f *File) Chown(uid, gid int) Error {
if e := syscall.Fchown(f.fd, uid, gid); e != 0 {
return &PathError{"chown", f.name, Errno(e)}
}
return nil
}
func (f *File) Truncate(size int64) Error {
if e := syscall.Ftruncate(f.fd, size); e != 0 {
return &PathError{"truncate", f.name, Errno(e)}
}
return nil
}
func Chtimes(name string, atime_ns int64, mtime_ns int64) Error {
var utimes [2]syscall.Timeval
utimes[0] = syscall.NsecToTimeval(atime_ns)
utimes[1] = syscall.NsecToTimeval(mtime_ns)
if e := syscall.Utimes(name, utimes[0:]); e != 0 {
return &PathError{"chtimes", name, Errno(e)}
}
return nil
}