| 1 |
/* |
| 2 |
Copyright 2017 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package io |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"errors" |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
"io/ioutil" |
| 25 |
) |
| 26 |
|
| 27 |
// ErrLimitReached means that the read limit is reached. |
| 28 |
var ErrLimitReached = errors.New("the read limit is reached") |
| 29 |
|
| 30 |
// ConsistentRead repeatedly reads a file until it gets the same content twice. |
| 31 |
// This is useful when reading files in /proc that are larger than page size |
| 32 |
// and kernel may modify them between individual read() syscalls. |
| 33 |
// It returns InconsistentReadError when it cannot get a consistent read in |
| 34 |
// given nr. of attempts. Caller should retry, kernel is probably under heavy |
| 35 |
// mount/unmount load. |
| 36 |
func ConsistentRead(filename string, attempts int) ([]byte, error) { |
| 37 |
return consistentReadSync(filename, attempts, nil) |
| 38 |
} |
| 39 |
|
| 40 |
// consistentReadSync is the main functionality of ConsistentRead but |
| 41 |
// introduces a sync callback that can be used by the tests to mutate the file |
| 42 |
// from which the test data is being read |
| 43 |
func consistentReadSync(filename string, attempts int, sync func(int)) ([]byte, error) { |
| 44 |
oldContent, err := ioutil.ReadFile(filename) |
| 45 |
if err != nil { |
| 46 |
return nil, err |
| 47 |
} |
| 48 |
for i := range attempts { |
| 49 |
if sync != nil { |
| 50 |
sync(i) |
| 51 |
} |
| 52 |
newContent, err := ioutil.ReadFile(filename) |
| 53 |
if err != nil { |
| 54 |
return nil, err |
| 55 |
} |
| 56 |
if bytes.Compare(oldContent, newContent) == 0 { |
| 57 |
return newContent, nil |
| 58 |
} |
| 59 |
// Files are different, continue reading |
| 60 |
oldContent = newContent |
| 61 |
} |
| 62 |
return nil, InconsistentReadError{filename, attempts} |
| 63 |
} |
| 64 |
|
| 65 |
// InconsistentReadError is returned from ConsistentRead when it cannot get |
| 66 |
// a consistent read in given nr. of attempts. Caller should retry, kernel is |
| 67 |
// probably under heavy mount/unmount load. |
| 68 |
type InconsistentReadError struct { |
| 69 |
filename string |
| 70 |
attempts int |
| 71 |
} |
| 72 |
|
| 73 |
func (i InconsistentReadError) Error() string { |
| 74 |
return fmt.Sprintf("could not get consistent content of %s after %d attempts", i.filename, i.attempts) |
| 75 |
} |
| 76 |
|
| 77 |
var _ error = InconsistentReadError{} |
| 78 |
|
| 79 |
func IsInconsistentReadError(err error) bool { |
| 80 |
if _, ok := err.(InconsistentReadError); ok { |
| 81 |
return true |
| 82 |
} |
| 83 |
return false |
| 84 |
} |
| 85 |
|
| 86 |
// ReadAtMost reads up to `limit` bytes from `r`, and reports an error |
| 87 |
// when `limit` bytes are read. |
| 88 |
func ReadAtMost(r io.Reader, limit int64) ([]byte, error) { |
| 89 |
limitedReader := &io.LimitedReader{R: r, N: limit} |
| 90 |
data, err := ioutil.ReadAll(limitedReader) |
| 91 |
if err != nil { |
| 92 |
return data, err |
| 93 |
} |
| 94 |
if limitedReader.N <= 0 { |
| 95 |
return data, ErrLimitReached |
| 96 |
} |
| 97 |
return data, nil |
| 98 |
} |