1
0
mirror of https://github.com/golang/go synced 2024-11-25 08:47:56 -07:00

os: talk about errors and PathError in the package documentation

Fixes #2383.

R=golang-dev, bradfitz, adg, rsc
CC=golang-dev
https://golang.org/cl/5641061
This commit is contained in:
Rob Pike 2012-02-09 16:55:36 +11:00
parent 1253c75cf5
commit be0f6febad
9 changed files with 54 additions and 18 deletions

View File

@ -12,6 +12,7 @@ import (
// StartProcess starts a new process with the program, arguments and attributes // StartProcess starts a new process with the program, arguments and attributes
// specified by name, argv and attr. // specified by name, argv and attr.
// If there is an error, it will be of type *PathError.
func StartProcess(name string, argv []string, attr *ProcAttr) (p *Process, err error) { func StartProcess(name string, argv []string, attr *ProcAttr) (p *Process, err error) {
sysattr := &syscall.ProcAttr{ sysattr := &syscall.ProcAttr{
Dir: attr.Dir, Dir: attr.Dir,
@ -75,6 +76,7 @@ func (p *Process) Kill() error {
// named binary, with arguments argv and environment envv. // named binary, with arguments argv and environment envv.
// If successful, Exec never returns. If it fails, it returns an error. // If successful, Exec never returns. If it fails, it returns an error.
// ForkExec is almost always a better way to execute a program. // ForkExec is almost always a better way to execute a program.
// If there is an error, it will be of type *PathError.
func Exec(name string, argv []string, envv []string) error { func Exec(name string, argv []string, envv []string) error {
e := syscall.Exec(name, argv, envv) e := syscall.Exec(name, argv, envv)
if e != nil { if e != nil {

View File

@ -26,6 +26,8 @@ func (sig UnixSignal) String() string {
// //
// StartProcess is a low-level interface. The os/exec package provides // StartProcess is a low-level interface. The os/exec package provides
// higher-level interfaces. // higher-level interfaces.
//
// If there is an error, it will be of type *PathError.
func StartProcess(name string, argv []string, attr *ProcAttr) (p *Process, err error) { func StartProcess(name string, argv []string, attr *ProcAttr) (p *Process, err error) {
sysattr := &syscall.ProcAttr{ sysattr := &syscall.ProcAttr{
Dir: attr.Dir, Dir: attr.Dir,
@ -57,6 +59,8 @@ func (p *Process) Kill() error {
// //
// To run a child process, see StartProcess (for a low-level interface) // To run a child process, see StartProcess (for a low-level interface)
// or the os/exec package (for higher-level interfaces). // or the os/exec package (for higher-level interfaces).
//
// If there is an error, it will be of type *PathError.
func Exec(name string, argv []string, envv []string) error { func Exec(name string, argv []string, envv []string) error {
if envv == nil { if envv == nil {
envv = Environ() envv = Environ()

View File

@ -3,7 +3,13 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Package os provides a platform-independent interface to operating system // Package os provides a platform-independent interface to operating system
// functionality. The design is Unix-like. // functionality. The design is Unix-like, although the error handling is
// Go-like; failing calls return values of type error rather than error numbers.
// Often, more information is available within the error. For example,
// if a call that takes a file name fails, such as Open or Stat, the error
// will include failing file name when printed and will be of type *PathError,
// which may be unpacked for more information.
//
// The os interface is intended to be uniform across all operating systems. // The os interface is intended to be uniform across all operating systems.
// Features not generally available appear in the system-specific package syscall. // Features not generally available appear in the system-specific package syscall.
package os package os
@ -157,7 +163,7 @@ func (f *File) WriteString(s string) (ret int, err error) {
} }
// Mkdir creates a new directory with the specified name and permission bits. // Mkdir creates a new directory with the specified name and permission bits.
// It returns an error, if any. // If there is an error, it will be of type *PathError.
func Mkdir(name string, perm FileMode) error { func Mkdir(name string, perm FileMode) error {
e := syscall.Mkdir(name, syscallMode(perm)) e := syscall.Mkdir(name, syscallMode(perm))
if e != nil { if e != nil {
@ -167,6 +173,7 @@ func Mkdir(name string, perm FileMode) error {
} }
// Chdir changes the current working directory to the named directory. // Chdir changes the current working directory to the named directory.
// If there is an error, it will be of type *PathError.
func Chdir(dir string) error { func Chdir(dir string) error {
if e := syscall.Chdir(dir); e != nil { if e := syscall.Chdir(dir); e != nil {
return &PathError{"chdir", dir, e} return &PathError{"chdir", dir, e}
@ -176,6 +183,7 @@ func Chdir(dir string) error {
// Chdir changes the current working directory to the file, // Chdir changes the current working directory to the file,
// which must be a directory. // which must be a directory.
// If there is an error, it will be of type *PathError.
func (f *File) Chdir() error { func (f *File) Chdir() error {
if e := syscall.Fchdir(f.fd); e != nil { if e := syscall.Fchdir(f.fd); e != nil {
return &PathError{"chdir", f.name, e} return &PathError{"chdir", f.name, e}
@ -186,7 +194,7 @@ func (f *File) Chdir() error {
// Open opens the named file for reading. If successful, methods on // Open opens the named file for reading. If successful, methods on
// the returned file can be used for reading; the associated file // the returned file can be used for reading; the associated file
// descriptor has mode O_RDONLY. // descriptor has mode O_RDONLY.
// It returns the File and an error, if any. // If there is an error, it will be of type *PathError.
func Open(name string) (file *File, err error) { func Open(name string) (file *File, err error) {
return OpenFile(name, O_RDONLY, 0) return OpenFile(name, O_RDONLY, 0)
} }
@ -195,7 +203,7 @@ func Open(name string) (file *File, err error) {
// it if it already exists. If successful, methods on the returned // it if it already exists. If successful, methods on the returned
// File can be used for I/O; the associated file descriptor has mode // File can be used for I/O; the associated file descriptor has mode
// O_RDWR. // O_RDWR.
// It returns the File and an error, if any. // If there is an error, it will be of type *PathError.
func Create(name string) (file *File, err error) { func Create(name string) (file *File, err error) {
return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666) return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666)
} }

View File

@ -76,7 +76,7 @@ func syscallMode(i FileMode) (o uint32) {
// or Create instead. It opens the named file with specified flag // or Create instead. It opens the named file with specified flag
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
// methods on the returned File can be used for I/O. // methods on the returned File can be used for I/O.
// It returns the File and an error, if any. // If there is an error, it will be of type *PathError.
func OpenFile(name string, flag int, perm FileMode) (file *File, err error) { func OpenFile(name string, flag int, perm FileMode) (file *File, err error) {
var ( var (
fd int fd int
@ -181,6 +181,7 @@ func (f *File) Truncate(size int64) error {
const chmodMask = uint32(syscall.DMAPPEND | syscall.DMEXCL | syscall.DMTMP | ModePerm) const chmodMask = uint32(syscall.DMAPPEND | syscall.DMEXCL | syscall.DMTMP | ModePerm)
// Chmod changes the mode of the file to mode. // 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 { func (f *File) Chmod(mode FileMode) error {
var d Dir var d Dir
@ -248,6 +249,7 @@ func (f *File) seek(offset int64, whence int) (ret int64, err error) {
// Truncate changes the size of the named file. // Truncate changes the size of the named file.
// If the file is a symbolic link, it changes the size of the link's target. // 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 { func Truncate(name string, size int64) error {
var d Dir var d Dir
d.Null() d.Null()
@ -261,6 +263,7 @@ func Truncate(name string, size int64) error {
} }
// Remove removes the named file or directory. // Remove removes the named file or directory.
// If there is an error, it will be of type *PathError.
func Remove(name string) error { func Remove(name string) error {
if e := syscall.Remove(name); e != nil { if e := syscall.Remove(name); e != nil {
return &PathError{"remove", name, e} return &PathError{"remove", name, e}
@ -269,6 +272,7 @@ func Remove(name string) error {
} }
// Rename renames a file. // Rename renames a file.
// If there is an error, it will be of type *PathError.
func Rename(oldname, newname string) error { func Rename(oldname, newname string) error {
var d Dir var d Dir
d.Null() d.Null()
@ -282,6 +286,7 @@ func Rename(oldname, newname string) error {
} }
// Chmod changes the mode of the named file to mode. // Chmod changes the mode of the named file to mode.
// If there is an error, it will be of type *PathError.
func Chmod(name string, mode FileMode) error { func Chmod(name string, mode FileMode) error {
var d Dir var d Dir

View File

@ -57,6 +57,7 @@ func Symlink(oldname, newname string) error {
// Readlink reads the contents of a symbolic link: the destination of // Readlink reads the contents of a symbolic link: the destination of
// the link. It returns the contents and an error, if any. // the link. It returns the contents and an error, if any.
// If there is an error, it will be of type *PathError.
func Readlink(name string) (string, error) { func Readlink(name string) (string, error) {
for len := 128; ; len *= 2 { for len := 128; ; len *= 2 {
b := make([]byte, len) b := make([]byte, len)
@ -99,6 +100,7 @@ func syscallMode(i FileMode) (o uint32) {
// Chmod changes the mode of the named file to mode. // 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 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 { func Chmod(name string, mode FileMode) error {
if e := syscall.Chmod(name, syscallMode(mode)); e != nil { if e := syscall.Chmod(name, syscallMode(mode)); e != nil {
return &PathError{"chmod", name, e} return &PathError{"chmod", name, e}
@ -107,6 +109,7 @@ func Chmod(name string, mode FileMode) error {
} }
// Chmod changes the mode of the file to mode. // 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 { func (f *File) Chmod(mode FileMode) error {
if e := syscall.Fchmod(f.fd, syscallMode(mode)); e != nil { if e := syscall.Fchmod(f.fd, syscallMode(mode)); e != nil {
return &PathError{"chmod", f.name, e} return &PathError{"chmod", f.name, e}
@ -116,6 +119,7 @@ func (f *File) Chmod(mode FileMode) error {
// Chown changes the numeric uid and gid of the named file. // 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 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 { func Chown(name string, uid, gid int) error {
if e := syscall.Chown(name, uid, gid); e != nil { if e := syscall.Chown(name, uid, gid); e != nil {
return &PathError{"chown", name, e} return &PathError{"chown", name, e}
@ -125,6 +129,7 @@ func Chown(name string, uid, gid int) error {
// Lchown changes the numeric uid and gid of the named file. // 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 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 { func Lchown(name string, uid, gid int) error {
if e := syscall.Lchown(name, uid, gid); e != nil { if e := syscall.Lchown(name, uid, gid); e != nil {
return &PathError{"lchown", name, e} return &PathError{"lchown", name, e}
@ -133,6 +138,7 @@ func Lchown(name string, uid, gid int) error {
} }
// Chown changes the numeric uid and gid of the named file. // 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 { func (f *File) Chown(uid, gid int) error {
if e := syscall.Fchown(f.fd, uid, gid); e != nil { if e := syscall.Fchown(f.fd, uid, gid); e != nil {
return &PathError{"chown", f.name, e} return &PathError{"chown", f.name, e}
@ -142,6 +148,7 @@ func (f *File) Chown(uid, gid int) error {
// Truncate changes the size of the file. // Truncate changes the size of the file.
// It does not change the I/O offset. // 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 { func (f *File) Truncate(size int64) error {
if e := syscall.Ftruncate(f.fd, size); e != nil { if e := syscall.Ftruncate(f.fd, size); e != nil {
return &PathError{"truncate", f.name, e} return &PathError{"truncate", f.name, e}
@ -167,6 +174,7 @@ func (f *File) Sync() (err error) {
// //
// The underlying filesystem may truncate or round the values to a // The underlying filesystem may truncate or round the values to a
// less precise time unit. // 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 { func Chtimes(name string, atime time.Time, mtime time.Time) error {
var utimes [2]syscall.Timeval var utimes [2]syscall.Timeval
atime_ns := atime.Unix()*1e9 + int64(atime.Nanosecond()) atime_ns := atime.Unix()*1e9 + int64(atime.Nanosecond())

View File

@ -60,7 +60,7 @@ const DevNull = "/dev/null"
// or Create instead. It opens the named file with specified flag // or Create instead. It opens the named file with specified flag
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
// methods on the returned File can be used for I/O. // methods on the returned File can be used for I/O.
// It returns the File and an error, if any. // If there is an error, it will be of type *PathError.
func OpenFile(name string, flag int, perm FileMode) (file *File, err error) { func OpenFile(name string, flag int, perm FileMode) (file *File, err error) {
r, e := syscall.Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm)) r, e := syscall.Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm))
if e != nil { if e != nil {
@ -103,7 +103,7 @@ func (file *file) close() error {
} }
// Stat returns the FileInfo structure describing file. // Stat returns the FileInfo structure describing file.
// It returns the FileInfo and an error, if any. // If there is an error, it will be of type *PathError.
func (f *File) Stat() (fi FileInfo, err error) { func (f *File) Stat() (fi FileInfo, err error) {
var stat syscall.Stat_t var stat syscall.Stat_t
err = syscall.Fstat(f.fd, &stat) err = syscall.Fstat(f.fd, &stat)
@ -113,11 +113,12 @@ func (f *File) Stat() (fi FileInfo, err error) {
return fileInfoFromStat(&stat, f.name), nil return fileInfoFromStat(&stat, f.name), nil
} }
// Stat returns a FileInfo describing the named file and an error, if any. // Stat returns a FileInfo describing the named file.
// If name names a valid symbolic link, the returned FileInfo describes // If name names a valid symbolic link, the returned FileInfo describes
// the file pointed at by the link and has fi.FollowedSymlink set to true. // the file pointed at by the link and has fi.FollowedSymlink set to true.
// If name names an invalid symbolic link, the returned FileInfo describes // If name names an invalid symbolic link, the returned FileInfo describes
// the link itself and has fi.FollowedSymlink set to false. // the link itself and has fi.FollowedSymlink set to false.
// If there is an error, it will be of type *PathError.
func Stat(name string) (fi FileInfo, err error) { func Stat(name string) (fi FileInfo, err error) {
var stat syscall.Stat_t var stat syscall.Stat_t
err = syscall.Stat(name, &stat) err = syscall.Stat(name, &stat)
@ -127,9 +128,10 @@ func Stat(name string) (fi FileInfo, err error) {
return fileInfoFromStat(&stat, name), nil return fileInfoFromStat(&stat, name), nil
} }
// Lstat returns a FileInfo describing the named file and an // Lstat returns a FileInfo describing the named file.
// error, if any. If the file is a symbolic link, the returned FileInfo // If the file is a symbolic link, the returned FileInfo
// describes the symbolic link. Lstat makes no attempt to follow the link. // describes the symbolic link. Lstat makes no attempt to follow the link.
// If there is an error, it will be of type *PathError.
func Lstat(name string) (fi FileInfo, err error) { func Lstat(name string) (fi FileInfo, err error) {
var stat syscall.Stat_t var stat syscall.Stat_t
err = syscall.Lstat(name, &stat) err = syscall.Lstat(name, &stat)
@ -193,6 +195,7 @@ func (f *File) seek(offset int64, whence int) (ret int64, err error) {
// Truncate changes the size of the named file. // Truncate changes the size of the named file.
// If the file is a symbolic link, it changes the size of the link's target. // 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 { func Truncate(name string, size int64) error {
if e := syscall.Truncate(name, size); e != nil { if e := syscall.Truncate(name, size); e != nil {
return &PathError{"truncate", name, e} return &PathError{"truncate", name, e}
@ -201,6 +204,7 @@ func Truncate(name string, size int64) error {
} }
// Remove removes the named file or directory. // Remove removes the named file or directory.
// If there is an error, it will be of type *PathError.
func Remove(name string) error { func Remove(name string) error {
// System call interface forces us to know // System call interface forces us to know
// whether name is a file or directory. // whether name is a file or directory.

View File

@ -87,7 +87,7 @@ func openDir(name string) (file *File, err error) {
// or Create instead. It opens the named file with specified flag // or Create instead. It opens the named file with specified flag
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
// methods on the returned File can be used for I/O. // methods on the returned File can be used for I/O.
// It returns the File and an error, if any. // If there is an error, it will be of type *PathError.
func OpenFile(name string, flag int, perm FileMode) (file *File, err error) { func OpenFile(name string, flag int, perm FileMode) (file *File, err error) {
if name == "" { if name == "" {
return nil, &PathError{"open", name, syscall.ENOENT} return nil, &PathError{"open", name, syscall.ENOENT}
@ -267,6 +267,7 @@ func Truncate(name string, size int64) error {
} }
// Remove removes the named file or directory. // Remove removes the named file or directory.
// If there is an error, it will be of type *PathError.
func Remove(name string) error { func Remove(name string) error {
p := &syscall.StringToUTF16(name)[0] p := &syscall.StringToUTF16(name)[0]

View File

@ -82,7 +82,8 @@ func dirstat(arg interface{}) (d *Dir, err error) {
return nil, &PathError{"stat", name, Ebadstat} return nil, &PathError{"stat", name, Ebadstat}
} }
// Stat returns a FileInfo structure describing the named file and an error, if any. // Stat returns a FileInfo structure describing the named file.
// If there is an error, it will be of type *PathError.
func Stat(name string) (FileInfo, error) { func Stat(name string) (FileInfo, error) {
d, err := dirstat(name) d, err := dirstat(name)
if err != nil { if err != nil {
@ -91,9 +92,10 @@ func Stat(name string) (FileInfo, error) {
return fileInfoFromStat(d), nil return fileInfoFromStat(d), nil
} }
// Lstat returns the FileInfo structure describing the named file and an // Lstat returns the FileInfo structure describing the named file.
// error, if any. If the file is a symbolic link (though Plan 9 does not have symbolic links), // If the file is a symbolic link (though Plan 9 does not have symbolic links),
// the returned FileInfo describes the symbolic link. Lstat makes no attempt to follow the link. // the returned FileInfo describes the symbolic link. Lstat makes no attempt to follow the link.
// If there is an error, it will be of type *PathError.
func Lstat(name string) (FileInfo, error) { func Lstat(name string) (FileInfo, error) {
return Stat(name) return Stat(name)
} }

View File

@ -11,7 +11,7 @@ import (
) )
// Stat returns the FileInfo structure describing file. // Stat returns the FileInfo structure describing file.
// It returns the FileInfo and an error, if any. // If there is an error, it will be of type *PathError.
func (file *File) Stat() (fi FileInfo, err error) { func (file *File) Stat() (fi FileInfo, err error) {
if file == nil || file.fd < 0 { if file == nil || file.fd < 0 {
return nil, EINVAL return nil, EINVAL
@ -28,11 +28,12 @@ func (file *File) Stat() (fi FileInfo, err error) {
return toFileInfo(basename(file.name), d.FileAttributes, d.FileSizeHigh, d.FileSizeLow, d.CreationTime, d.LastAccessTime, d.LastWriteTime), nil return toFileInfo(basename(file.name), d.FileAttributes, d.FileSizeHigh, d.FileSizeLow, d.CreationTime, d.LastAccessTime, d.LastWriteTime), nil
} }
// Stat returns a FileInfo structure describing the named file and an error, if any. // Stat returns a FileInfo structure describing the named file.
// If name names a valid symbolic link, the returned FileInfo describes // If name names a valid symbolic link, the returned FileInfo describes
// the file pointed at by the link and has fi.FollowedSymlink set to true. // the file pointed at by the link and has fi.FollowedSymlink set to true.
// If name names an invalid symbolic link, the returned FileInfo describes // If name names an invalid symbolic link, the returned FileInfo describes
// the link itself and has fi.FollowedSymlink set to false. // the link itself and has fi.FollowedSymlink set to false.
// If there is an error, it will be of type *PathError.
func Stat(name string) (fi FileInfo, err error) { func Stat(name string) (fi FileInfo, err error) {
if len(name) == 0 { if len(name) == 0 {
return nil, &PathError{"Stat", name, syscall.Errno(syscall.ERROR_PATH_NOT_FOUND)} return nil, &PathError{"Stat", name, syscall.Errno(syscall.ERROR_PATH_NOT_FOUND)}
@ -45,9 +46,10 @@ func Stat(name string) (fi FileInfo, err error) {
return toFileInfo(basename(name), d.FileAttributes, d.FileSizeHigh, d.FileSizeLow, d.CreationTime, d.LastAccessTime, d.LastWriteTime), nil return toFileInfo(basename(name), d.FileAttributes, d.FileSizeHigh, d.FileSizeLow, d.CreationTime, d.LastAccessTime, d.LastWriteTime), nil
} }
// Lstat returns the FileInfo structure describing the named file and an // Lstat returns the FileInfo structure describing the named file.
// error, if any. If the file is a symbolic link, the returned FileInfo // If the file is a symbolic link, the returned FileInfo
// describes the symbolic link. Lstat makes no attempt to follow the link. // describes the symbolic link. Lstat makes no attempt to follow the link.
// If there is an error, it will be of type *PathError.
func Lstat(name string) (fi FileInfo, err error) { func Lstat(name string) (fi FileInfo, err error) {
// No links on Windows // No links on Windows
return Stat(name) return Stat(name)