index: deprecate legacy index format

This commit is contained in:
Michael Eischer 2024-03-09 18:20:52 +01:00
parent 396a61a992
commit 1a8bf358f1
4 changed files with 13 additions and 2 deletions

View File

@ -5,11 +5,13 @@ var Flag = New()
// flag names are written in kebab-case // flag names are written in kebab-case
const ( const (
ExampleFeature FlagName = "example-feature" ExampleFeature FlagName = "example-feature"
DeprecateLegacyIndex FlagName = "deprecate-legacy-index"
) )
func init() { func init() {
Flag.SetFlags(map[FlagName]FlagDesc{ Flag.SetFlags(map[FlagName]FlagDesc{
ExampleFeature: {Type: Alpha, Description: "just for testing"}, ExampleFeature: {Type: Alpha, Description: "just for testing"},
DeprecateLegacyIndex: {Type: Beta, Description: "disable support for index format used by restic 0.1.0. Use `restic repair index` to update the index if necessary."},
}) })
} }

View File

@ -3,12 +3,14 @@ package index
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"sync" "sync"
"time" "time"
"github.com/restic/restic/internal/crypto" "github.com/restic/restic/internal/crypto"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/feature"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
@ -515,6 +517,10 @@ func DecodeIndex(buf []byte, id restic.ID) (idx *Index, oldFormat bool, err erro
debug.Log("Error %v", err) debug.Log("Error %v", err)
if isErrOldIndex(err) { if isErrOldIndex(err) {
if feature.Flag.Enabled(feature.DeprecateLegacyIndex) {
return nil, false, fmt.Errorf("index seems to use the legacy format. update it using `restic repair index`")
}
debug.Log("index is probably old format, trying that") debug.Log("index is probably old format, trying that")
idx, err = decodeOldIndex(buf) idx, err = decodeOldIndex(buf)
return idx, err == nil, err return idx, err == nil, err

View File

@ -8,6 +8,7 @@ import (
"sync" "sync"
"testing" "testing"
"github.com/restic/restic/internal/feature"
"github.com/restic/restic/internal/index" "github.com/restic/restic/internal/index"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test" rtest "github.com/restic/restic/internal/test"
@ -427,6 +428,8 @@ func BenchmarkEncodeIndex(b *testing.B) {
} }
func TestIndexUnserializeOld(t *testing.T) { func TestIndexUnserializeOld(t *testing.T) {
defer feature.TestSetFlag(t, feature.Flag, feature.DeprecateLegacyIndex, false)()
idx, oldFormat, err := index.DecodeIndex(docOldExample, restic.NewRandomID()) idx, oldFormat, err := index.DecodeIndex(docOldExample, restic.NewRandomID())
rtest.OK(t, err) rtest.OK(t, err)
rtest.Assert(t, oldFormat, "old index format recognized as new format") rtest.Assert(t, oldFormat, "old index format recognized as new format")