make sure ReadState returns an error

ReadState would hide any errors, assuming that it was an empty state.
This can mask errors on Windows, where the OS enforces read locks on the
state file.
This commit is contained in:
James Bardin 2018-03-19 18:15:54 -04:00
parent e10a7917e6
commit 8fb8b2cffc
1 changed files with 13 additions and 4 deletions

View File

@ -9,6 +9,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"os"
"reflect" "reflect"
"sort" "sort"
"strconv" "strconv"
@ -1876,13 +1877,21 @@ var ErrNoState = errors.New("no state")
// ReadState reads a state structure out of a reader in the format that // ReadState reads a state structure out of a reader in the format that
// was written by WriteState. // was written by WriteState.
func ReadState(src io.Reader) (*State, error) { func ReadState(src io.Reader) (*State, error) {
buf := bufio.NewReader(src) // check for a nil file specifically, since that produces a platform
if _, err := buf.Peek(1); err != nil { // specific error if we try to use it in a bufio.Reader.
// the error is either io.EOF or "invalid argument", and both are from if f, ok := src.(*os.File); ok && f == nil {
// an empty state.
return nil, ErrNoState return nil, ErrNoState
} }
buf := bufio.NewReader(src)
if _, err := buf.Peek(1); err != nil {
if err == io.EOF {
return nil, ErrNoState
}
return nil, err
}
if err := testForV0State(buf); err != nil { if err := testForV0State(buf); err != nil {
return nil, err return nil, err
} }