From d4da735091986868015369e01c63794af9cc9b84 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Mon, 6 Jul 2020 09:49:20 -0400 Subject: io/fs: move FileInfo, FileMode, PathError, ErrInvalid, ... from os to io/fs First step of creating the new io/fs package. For #41190. Change-Id: I1339b1abdd533b0f1deab283628088b2f706fb5b Reviewed-on: https://go-review.googlesource.com/c/go/+/243906 Trust: Russ Cox Run-TryBot: Russ Cox Run-TryBot: Emmanuel Odeke TryBot-Result: Go Bot Reviewed-by: Rob Pike Reviewed-by: Emmanuel Odeke --- src/os/error.go | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) (limited to 'src/os/error.go') diff --git a/src/os/error.go b/src/os/error.go index 875cc9711f..7cd9f22bfb 100644 --- a/src/os/error.go +++ b/src/os/error.go @@ -7,6 +7,7 @@ package os import ( "internal/oserror" "internal/poll" + "io/fs" ) // Portable analogs of some common system call errors. @@ -16,20 +17,17 @@ import ( var ( // ErrInvalid indicates an invalid argument. // Methods on File will return this error when the receiver is nil. - ErrInvalid = errInvalid() // "invalid argument" + ErrInvalid = fs.ErrInvalid // "invalid argument" + + ErrPermission = fs.ErrPermission // "permission denied" + ErrExist = fs.ErrExist // "file already exists" + ErrNotExist = fs.ErrNotExist // "file does not exist" + ErrClosed = fs.ErrClosed // "file already closed" - ErrPermission = errPermission() // "permission denied" - ErrExist = errExist() // "file already exists" - ErrNotExist = errNotExist() // "file does not exist" - ErrClosed = errClosed() // "file already closed" ErrNoDeadline = errNoDeadline() // "file type does not support deadline" ErrDeadlineExceeded = errDeadlineExceeded() // "i/o timeout" ) -func errInvalid() error { return oserror.ErrInvalid } -func errPermission() error { return oserror.ErrPermission } -func errExist() error { return oserror.ErrExist } -func errNotExist() error { return oserror.ErrNotExist } func errClosed() error { return oserror.ErrClosed } func errNoDeadline() error { return poll.ErrNoDeadline } @@ -47,21 +45,7 @@ type timeout interface { } // PathError records an error and the operation and file path that caused it. -type PathError struct { - Op string - Path string - Err error -} - -func (e *PathError) Error() string { return e.Op + " " + e.Path + ": " + e.Err.Error() } - -func (e *PathError) Unwrap() error { return e.Err } - -// Timeout reports whether this error represents a timeout. -func (e *PathError) Timeout() bool { - t, ok := e.Err.(timeout) - return ok && t.Timeout() -} +type PathError = fs.PathError // SyscallError records an error from a specific system call. type SyscallError struct { -- cgit v1.3 From b641f0dcf48aa748aa8d3db1e332b77044b48e59 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Tue, 10 Nov 2020 07:51:59 -0800 Subject: os: clarify that IsExist and friends do not use errors.Is Fixes #41122 Change-Id: Ie5cb0b19ac461d321520b1ebfc493a0ca22232a7 Reviewed-on: https://go-review.googlesource.com/c/go/+/268897 Trust: Ian Lance Taylor Reviewed-by: Rob Pike Reviewed-by: Brad Fitzpatrick --- src/os/error.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/os/error.go') diff --git a/src/os/error.go b/src/os/error.go index 7cd9f22bfb..704a6fb29e 100644 --- a/src/os/error.go +++ b/src/os/error.go @@ -76,6 +76,9 @@ func NewSyscallError(syscall string, err error) error { // IsExist returns a boolean indicating whether the error is known to report // that a file or directory already exists. It is satisfied by ErrExist as // well as some syscall errors. +// +// This function predates errors.Is. It only supports errors returned by +// the os package. New code should use errors.Is(err, os.ErrExist). func IsExist(err error) bool { return underlyingErrorIs(err, ErrExist) } @@ -83,6 +86,9 @@ func IsExist(err error) bool { // IsNotExist returns a boolean indicating whether the error is known to // report that a file or directory does not exist. It is satisfied by // ErrNotExist as well as some syscall errors. +// +// This function predates errors.Is. It only supports errors returned by +// the os package. New code should use errors.Is(err, os.ErrNotExist). func IsNotExist(err error) bool { return underlyingErrorIs(err, ErrNotExist) } @@ -90,12 +96,21 @@ func IsNotExist(err error) bool { // IsPermission returns a boolean indicating whether the error is known to // report that permission is denied. It is satisfied by ErrPermission as well // as some syscall errors. +// +// This function predates errors.Is. It only supports errors returned by +// the os package. New code should use errors.Is(err, os.ErrPermission). func IsPermission(err error) bool { return underlyingErrorIs(err, ErrPermission) } // IsTimeout returns a boolean indicating whether the error is known // to report that a timeout occurred. +// +// This function predates errors.Is, and the notion of whether an +// error indicates a timeout can be ambiguous. For example, the Unix +// error EWOULDBLOCK sometimes indicates a timeout and sometimes does not. +// New code should use errors.Is with a value appropriate to the call +// returning the error, such as os.ErrDeadlineExceeded. func IsTimeout(err error) bool { terr, ok := underlyingError(err).(timeout) return ok && terr.Timeout() -- cgit v1.3