| 1 |
/* |
| 2 |
* errors.go - Custom errors and error functions used by fscrypt |
| 3 |
* |
| 4 |
* Copyright 2017 Google Inc. |
| 5 |
* Author: Joe Richey ([email protected]) |
| 6 |
* |
| 7 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 8 |
* use this file except in compliance with the License. You may obtain a copy of |
| 9 |
* the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, software |
| 14 |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 |
* License for the specific language governing permissions and limitations under |
| 17 |
* the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
package util |
| 21 |
|
| 22 |
import ( |
| 23 |
"fmt" |
| 24 |
"io" |
| 25 |
"log" |
| 26 |
"os" |
| 27 |
|
| 28 |
"github.com/pkg/errors" |
| 29 |
) |
| 30 |
|
| 31 |
// ErrReader wraps an io.Reader, passing along calls to Read() until a read |
| 32 |
// fails. Then, the error is stored, and all subsequent calls to Read() do |
| 33 |
// nothing. This allows you to write code which has many subsequent reads and |
| 34 |
// do all of the error checking at the end. For example: |
| 35 |
// |
| 36 |
// r := NewErrReader(reader) |
| 37 |
// r.Read(foo) |
| 38 |
// r.Read(bar) |
| 39 |
// r.Read(baz) |
| 40 |
// if r.Err() != nil { |
| 41 |
// // Handle error |
| 42 |
// } |
| 43 |
// |
| 44 |
// Taken from https://blog.golang.org/errors-are-values by Rob Pike. |
| 45 |
type ErrReader struct { |
| 46 |
r io.Reader |
| 47 |
err error |
| 48 |
} |
| 49 |
|
| 50 |
// NewErrReader creates an ErrReader which wraps the provided reader. |
| 51 |
func NewErrReader(reader io.Reader) *ErrReader { |
| 52 |
return &ErrReader{r: reader, err: nil} |
| 53 |
} |
| 54 |
|
| 55 |
// Read runs ReadFull on the wrapped reader if no errors have occurred. |
| 56 |
// Otherwise, the previous error is just returned and no reads are attempted. |
| 57 |
func (e *ErrReader) Read(p []byte) (n int, err error) { |
| 58 |
if e.err == nil { |
| 59 |
n, e.err = io.ReadFull(e.r, p) |
| 60 |
} |
| 61 |
return n, e.err |
| 62 |
} |
| 63 |
|
| 64 |
// Err returns the first encountered err (or nil if no errors occurred). |
| 65 |
func (e *ErrReader) Err() error { |
| 66 |
return e.err |
| 67 |
} |
| 68 |
|
| 69 |
// ErrWriter works exactly like ErrReader, except with io.Writer. |
| 70 |
type ErrWriter struct { |
| 71 |
w io.Writer |
| 72 |
err error |
| 73 |
} |
| 74 |
|
| 75 |
// NewErrWriter creates an ErrWriter which wraps the provided writer. |
| 76 |
func NewErrWriter(writer io.Writer) *ErrWriter { |
| 77 |
return &ErrWriter{w: writer, err: nil} |
| 78 |
} |
| 79 |
|
| 80 |
// Write runs the wrapped writer's Write if no errors have occurred. Otherwise, |
| 81 |
// the previous error is just returned and no writes are attempted. |
| 82 |
func (e *ErrWriter) Write(p []byte) (n int, err error) { |
| 83 |
if e.err == nil { |
| 84 |
n, e.err = e.w.Write(p) |
| 85 |
} |
| 86 |
return n, e.err |
| 87 |
} |
| 88 |
|
| 89 |
// Err returns the first encountered err (or nil if no errors occurred). |
| 90 |
func (e *ErrWriter) Err() error { |
| 91 |
return e.err |
| 92 |
} |
| 93 |
|
| 94 |
// CheckValidLength returns an invalid length error if expected != actual |
| 95 |
func CheckValidLength(expected, actual int) error { |
| 96 |
if expected == actual { |
| 97 |
return nil |
| 98 |
} |
| 99 |
return fmt.Errorf("expected length of %d, got %d", expected, actual) |
| 100 |
} |
| 101 |
|
| 102 |
// SystemError is an error that should indicate something has gone wrong in the |
| 103 |
// underlying system (syscall failure, bad ioctl, etc...). |
| 104 |
type SystemError string |
| 105 |
|
| 106 |
func (s SystemError) Error() string { |
| 107 |
return "system error: " + string(s) |
| 108 |
} |
| 109 |
|
| 110 |
// NeverError panics if a non-nil error is passed in. It should be used to check |
| 111 |
// for logic errors, not to handle recoverable errors. |
| 112 |
func NeverError(err error) { |
| 113 |
if err != nil { |
| 114 |
log.Panicf("NeverError() check failed: %v", err) |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
var ( |
| 119 |
// testEnvVarName is the name of an environment variable that should be |
| 120 |
// set to an empty mountpoint. This is only used for integration tests. |
| 121 |
// If not set, integration tests are skipped. |
| 122 |
testEnvVarName = "TEST_FILESYSTEM_ROOT" |
| 123 |
// ErrSkipIntegration indicates integration tests shouldn't be run. |
| 124 |
ErrSkipIntegration = errors.New("skipping integration test") |
| 125 |
) |
| 126 |
|
| 127 |
// TestRoot returns a the root of a filesystem specified by testEnvVarName. This |
| 128 |
// function is only used for integration tests. |
| 129 |
func TestRoot() (string, error) { |
| 130 |
path := os.Getenv(testEnvVarName) |
| 131 |
if path == "" { |
| 132 |
return "", ErrSkipIntegration |
| 133 |
} |
| 134 |
return path, nil |
| 135 |
} |