Rob Pike | c80b06a | 2008-09-11 13:03:46 -0700 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package os |
| 6 | |
Rob Pike | 56069f0 | 2012-02-17 10:04:29 +1100 | [diff] [blame] | 7 | import ( |
| 8 | "errors" |
| 9 | ) |
| 10 | |
| 11 | // Portable analogs of some common system call errors. |
| 12 | var ( |
| 13 | ErrInvalid = errors.New("invalid argument") |
| 14 | ErrPermission = errors.New("permission denied") |
| 15 | ErrExist = errors.New("file already exists") |
Anthony Martin | 566e0fe | 2012-02-18 07:44:38 +1100 | [diff] [blame] | 16 | ErrNotExist = errors.New("file does not exist") |
Rob Pike | 56069f0 | 2012-02-17 10:04:29 +1100 | [diff] [blame] | 17 | ) |
| 18 | |
Russ Cox | a0bcaf4 | 2009-06-25 20:24:55 -0700 | [diff] [blame] | 19 | // PathError records an error and the operation and file path that caused it. |
| 20 | type PathError struct { |
Russ Cox | 08a073a | 2011-11-01 21:49:08 -0400 | [diff] [blame] | 21 | Op string |
| 22 | Path string |
| 23 | Err error |
Russ Cox | a0bcaf4 | 2009-06-25 20:24:55 -0700 | [diff] [blame] | 24 | } |
| 25 | |
Russ Cox | 08a073a | 2011-11-01 21:49:08 -0400 | [diff] [blame] | 26 | func (e *PathError) Error() string { return e.Op + " " + e.Path + ": " + e.Err.Error() } |
Anthony Martin | 9f8c2c8 | 2012-02-14 14:22:34 -0500 | [diff] [blame] | 27 | |
| 28 | // SyscallError records an error from a specific system call. |
| 29 | type SyscallError struct { |
| 30 | Syscall string |
| 31 | Err error |
| 32 | } |
| 33 | |
| 34 | func (e *SyscallError) Error() string { return e.Syscall + ": " + e.Err.Error() } |
| 35 | |
| 36 | // NewSyscallError returns, as an error, a new SyscallError |
| 37 | // with the given system call name and error details. |
| 38 | // As a convenience, if err is nil, NewSyscallError returns nil. |
| 39 | func NewSyscallError(syscall string, err error) error { |
| 40 | if err == nil { |
| 41 | return nil |
| 42 | } |
| 43 | return &SyscallError{syscall, err} |
| 44 | } |
Shenghou Ma | 4ca59a0 | 2012-03-13 13:48:07 +0800 | [diff] [blame] | 45 | |
Rob Pike | abe384f | 2013-07-23 11:59:49 +1000 | [diff] [blame] | 46 | // IsExist returns a boolean indicating whether the error is known to report |
| 47 | // that a file or directory already exists. It is satisfied by ErrExist as |
| 48 | // well as some syscall errors. |
Shenghou Ma | 4ca59a0 | 2012-03-13 13:48:07 +0800 | [diff] [blame] | 49 | func IsExist(err error) bool { |
| 50 | return isExist(err) |
| 51 | } |
| 52 | |
Rob Pike | abe384f | 2013-07-23 11:59:49 +1000 | [diff] [blame] | 53 | // IsNotExist returns a boolean indicating whether the error is known to |
| 54 | // report that a file or directory does not exist. It is satisfied by |
| 55 | // ErrNotExist as well as some syscall errors. |
Shenghou Ma | 4ca59a0 | 2012-03-13 13:48:07 +0800 | [diff] [blame] | 56 | func IsNotExist(err error) bool { |
| 57 | return isNotExist(err) |
| 58 | } |
| 59 | |
Rob Pike | abe384f | 2013-07-23 11:59:49 +1000 | [diff] [blame] | 60 | // IsPermission returns a boolean indicating whether the error is known to |
| 61 | // report that permission is denied. It is satisfied by ErrPermission as well |
| 62 | // as some syscall errors. |
Shenghou Ma | 4ca59a0 | 2012-03-13 13:48:07 +0800 | [diff] [blame] | 63 | func IsPermission(err error) bool { |
| 64 | return isPermission(err) |
| 65 | } |