Allow loading index with old format

This commit is contained in:
Alexander Neumann
2015-07-26 21:58:03 +02:00
parent 7944e8e323
commit 96f2165067
2 changed files with 44 additions and 3 deletions

View File

@@ -319,6 +319,19 @@ func (idx *Index) Dump(w io.Writer) error {
return nil
}
// isErrOldIndex returns true if the error may be caused by an old index
// format.
func isErrOldIndex(err error) bool {
if e, ok := err.(*json.UnmarshalTypeError); ok && e.Value == "array" {
return true
}
return false
}
// ErrOldIndexFormat means an index with the old format was detected.
var ErrOldIndexFormat = errors.New("index has old format")
// DecodeIndex loads and unserializes an index from rd.
func DecodeIndex(rd io.Reader) (*Index, backend.IDs, error) {
debug.Log("Index.DecodeIndex", "Start decoding index")
@@ -328,6 +341,12 @@ func DecodeIndex(rd io.Reader) (*Index, backend.IDs, error) {
err := dec.Decode(&idxJSON)
if err != nil {
debug.Log("Index.DecodeIndex", "Error %#v", err)
if isErrOldIndex(err) {
debug.Log("Index.DecodeIndex", "index is probably old format, trying that")
err = ErrOldIndexFormat
}
return nil, nil, err
}