diff --git a/cmd/restic/cmd_init.go b/cmd/restic/cmd_init.go index 842db2c84..7e15da584 100644 --- a/cmd/restic/cmd_init.go +++ b/cmd/restic/cmd_init.go @@ -87,7 +87,8 @@ func runInit(ctx context.Context, opts InitOptions, gopts global.Options, args [ } if !gopts.JSON { - printer.P("created restic repository %v at %s", s.Config().ID[:10], location.StripPassword(gopts.Backends, gopts.Repo)) + id := s.Config().ID + printer.P("created restic repository %v at %s", id.Str(), location.StripPassword(gopts.Backends, gopts.Repo)) if opts.CopyChunkerParameters && chunkerPolynomial != nil { printer.P(" with chunker parameters copied from secondary repository") } @@ -99,7 +100,7 @@ func runInit(ctx context.Context, opts InitOptions, gopts global.Options, args [ } else { status := initSuccess{ MessageType: "initialized", - ID: s.Config().ID, + ID: s.Config().ID.String(), Repository: location.StripPassword(gopts.Backends, gopts.Repo), } return json.NewEncoder(gopts.Term.OutputWriter()).Encode(status) diff --git a/cmd/restic/cmd_mount.go b/cmd/restic/cmd_mount.go index d47cbbb74..969e4898f 100644 --- a/cmd/restic/cmd_mount.go +++ b/cmd/restic/cmd_mount.go @@ -154,7 +154,7 @@ func runMount(ctx context.Context, opts MountOptions, gopts global.Options, args return err } - fuseMountName := fmt.Sprintf("restic:%s", repo.Config().ID[:10]) + fuseMountName := fmt.Sprintf("restic:%s", repo.Config().ID.String()[:10]) mountOptions := []systemFuse.MountOption{ systemFuse.ReadOnly(), diff --git a/cmd/restic/cmd_prune.go b/cmd/restic/cmd_prune.go index 7112c7bd2..0c24cc730 100644 --- a/cmd/restic/cmd_prune.go +++ b/cmd/restic/cmd_prune.go @@ -183,7 +183,7 @@ func runPrune(ctx context.Context, opts PruneOptions, gopts global.Options, term defer unlock() if opts.UnsafeNoSpaceRecovery != "" { - repoID := repo.Config().ID + repoID := repo.Config().ID.String() if opts.UnsafeNoSpaceRecovery != repoID { return errors.Fatalf("must pass id '%s' to --unsafe-recover-no-free-space", repoID) } diff --git a/internal/global/global.go b/internal/global/global.go index 40429c8a4..547f7da9e 100644 --- a/internal/global/global.go +++ b/internal/global/global.go @@ -410,19 +410,16 @@ func decryptRepository(ctx context.Context, s *repository.Repository, gopts *Opt // printRepositoryInfo displays the repository ID, version and compression level. func printRepositoryInfo(s *repository.Repository, gopts Options, printer restic.Printer) { id := s.Config().ID - if len(id) > 8 { - id = id[:8] - } extra := "" if s.Config().Version >= 2 { extra = ", compression level " + gopts.Compression.String() } - printer.PT("repository %v opened (version %v%s)", id, s.Config().Version, extra) + printer.PT("repository %v opened (version %v%s)", id.Str(), s.Config().Version, extra) } // setupCache creates a new cache and removes old cache directories if instructed to do so. func setupCache(s *repository.Repository, gopts Options, printer restic.Printer) error { - c, err := cache.New(s.Config().ID, gopts.CacheDir) + c, err := cache.New(s.Config().ID.String(), gopts.CacheDir) if err != nil { printer.E("unable to open cache: %v", err) return err diff --git a/internal/restic/config.go b/internal/restic/config.go index 4c6c97a08..06be974c2 100644 --- a/internal/restic/config.go +++ b/internal/restic/config.go @@ -15,7 +15,7 @@ import ( // Config contains the configuration for a repository. type Config struct { Version uint `json:"version"` - ID string `json:"id"` + ID ID `json:"id"` ChunkerPolynomial chunker.Pol `json:"chunker_polynomial"` } @@ -43,7 +43,7 @@ func CreateConfig(version uint, pol *chunker.Pol) (Config, error) { cfg.ChunkerPolynomial = *pol } - cfg.ID = NewRandomID().String() + cfg.ID = NewRandomID() cfg.Version = version debug.Log("New config: %#v", cfg) diff --git a/internal/restic/config_test.go b/internal/restic/config_test.go index 54d3db2fc..fed759b6e 100644 --- a/internal/restic/config_test.go +++ b/internal/restic/config_test.go @@ -63,3 +63,11 @@ func TestConfig(t *testing.T) { rtest.Assert(t, cfg1 == cfg2, "configs aren't equal: %v != %v", cfg1, cfg2) } + +func TestLoadConfigRejectsInvalidID(t *testing.T) { + load := func(tpe restic.FileType, id restic.ID) ([]byte, error) { + return []byte(`{"version":1,"id":"not-an-id","chunker_polynomial":{"a":1,"b":2}}`), nil + } + _, err := restic.LoadConfig(context.TODO(), loader{load}) + rtest.Assert(t, err != nil, "invalid repository id was accepted") +}