mirror of
https://github.com/golang/go.git
synced 2025-11-01 09:10:57 +00:00
echo back context of call in error if likely to be useful.
For example, if os.Open("/etc/passwd", os.O_RDONLY)
fails with syscall.EPERM, it returns as the os.Error
&PathError{
Op: "open",
Path: "/etc/passwd"
Error: os.EPERM
}
which formats as
open /etc/passwd: permission denied
Not converted:
datafmt
go/...
google/...
regexp
tabwriter
template
R=r
DELTA=1153 (561 added, 156 deleted, 436 changed)
OCL=30738
CL=30781
74 lines
1.3 KiB
Go
74 lines
1.3 KiB
Go
// Copyright 2009 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package file
|
|
|
|
import (
|
|
"os";
|
|
"syscall";
|
|
)
|
|
|
|
type File struct {
|
|
fd int; // file descriptor number
|
|
name string; // file name at Open time
|
|
}
|
|
|
|
func newFile(fd int, name string) *File {
|
|
if fd < 0 {
|
|
return nil
|
|
}
|
|
return &File{fd, name}
|
|
}
|
|
|
|
var (
|
|
Stdin = newFile(0, "/dev/stdin");
|
|
Stdout = newFile(1, "/dev/stdout");
|
|
Stderr = newFile(2, "/dev/stderr");
|
|
)
|
|
|
|
func Open(name string, mode int, perm int) (file *File, err os.Error) {
|
|
r, e := syscall.Open(name, mode, perm);
|
|
if e != 0 {
|
|
err = os.Errno(e);
|
|
}
|
|
return newFile(r, name), err
|
|
}
|
|
|
|
func (file *File) Close() os.Error {
|
|
if file == nil {
|
|
return os.EINVAL
|
|
}
|
|
e := syscall.Close(file.fd);
|
|
file.fd = -1; // so it can't be closed again
|
|
if e != 0 {
|
|
return os.Errno(e);
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (file *File) Read(b []byte) (ret int, err os.Error) {
|
|
if file == nil {
|
|
return -1, os.EINVAL
|
|
}
|
|
r, e := syscall.Read(file.fd, b);
|
|
if e != 0 {
|
|
err = os.Errno(e);
|
|
}
|
|
return int(r), err
|
|
}
|
|
|
|
func (file *File) Write(b []byte) (ret int, err os.Error) {
|
|
if file == nil {
|
|
return -1, os.EINVAL
|
|
}
|
|
r, e := syscall.Write(file.fd, b);
|
|
if e != 0 {
|
|
err = os.Errno(e);
|
|
}
|
|
return int(r), err
|
|
}
|
|
|
|
func (file *File) String() string {
|
|
return file.name
|
|
}
|