2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-22 12:55:18 +00:00

restic: change Find to return ids

That way consumers no longer have to manually convert the returned name
to an id.
This commit is contained in:
Michael Eischer 2022-10-15 16:00:05 +02:00
parent 258b487d8f
commit 02634dce7a
6 changed files with 16 additions and 24 deletions

View File

@ -436,10 +436,7 @@ func runDebugExamine(ctx context.Context, gopts GlobalOptions, args []string) er
for _, name := range args { for _, name := range args {
id, err := restic.ParseID(name) id, err := restic.ParseID(name)
if err != nil { if err != nil {
name, err = restic.Find(ctx, repo.Backend(), restic.PackFile, name) id, err = restic.Find(ctx, repo.Backend(), restic.PackFile, name)
if err == nil {
id, err = restic.ParseID(name)
}
if err != nil { if err != nil {
Warnf("error: %v\n", err) Warnf("error: %v\n", err)
continue continue

View File

@ -236,7 +236,7 @@ func runKey(ctx context.Context, gopts GlobalOptions, args []string) error {
return err return err
} }
return deleteKey(ctx, repo, id) return deleteKey(ctx, repo, id.String())
case "passwd": case "passwd":
lock, ctx, err := lockRepoExclusive(ctx, repo) lock, ctx, err := lockRepoExclusive(ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)

View File

@ -119,7 +119,7 @@ func SearchKey(ctx context.Context, s *Repository, password string, maxKeys int,
id, err := restic.Find(ctx, s.Backend(), restic.KeyFile, keyHint) id, err := restic.Find(ctx, s.Backend(), restic.KeyFile, keyHint)
if err == nil { if err == nil {
key, err := OpenKey(ctx, s, id, password) key, err := OpenKey(ctx, s, id.String(), password)
if err == nil { if err == nil {
debug.Log("successfully opened hinted key %v", id) debug.Log("successfully opened hinted key %v", id)

View File

@ -26,23 +26,23 @@ func (e *NoIDByPrefixError) Error() string {
// Find loads the list of all files of type t and searches for names which // Find loads the list of all files of type t and searches for names which
// start with prefix. If none is found, nil and ErrNoIDPrefixFound is returned. // start with prefix. If none is found, nil and ErrNoIDPrefixFound is returned.
// If more than one is found, nil and ErrMultipleIDMatches is returned. // If more than one is found, nil and ErrMultipleIDMatches is returned.
func Find(ctx context.Context, be Lister, t FileType, prefix string) (string, error) { func Find(ctx context.Context, be Lister, t FileType, prefix string) (ID, error) {
match := "" match := ID{}
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
err := be.List(ctx, t, func(fi FileInfo) error { err := be.List(ctx, t, func(fi FileInfo) error {
// ignore filename which are not an id // ignore filename which are not an id
_, err := ParseID(fi.Name) id, err := ParseID(fi.Name)
if err != nil { if err != nil {
debug.Log("unable to parse %v as an ID", fi.Name) debug.Log("unable to parse %v as an ID", fi.Name)
return nil return nil
} }
if len(fi.Name) >= len(prefix) && prefix == fi.Name[:len(prefix)] { if len(fi.Name) >= len(prefix) && prefix == fi.Name[:len(prefix)] {
if match == "" { if match.IsNull() {
match = fi.Name match = id
} else { } else {
return &MultipleIDMatchesError{prefix} return &MultipleIDMatchesError{prefix}
} }
@ -52,12 +52,12 @@ func Find(ctx context.Context, be Lister, t FileType, prefix string) (string, er
}) })
if err != nil { if err != nil {
return "", err return ID{}, err
} }
if match != "" { if !match.IsNull() {
return match, nil return match, nil
} }
return "", &NoIDByPrefixError{prefix} return ID{}, &NoIDByPrefixError{prefix}
} }

View File

@ -43,7 +43,7 @@ func TestFind(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
expectedMatch := "20bdc1402a6fc9b633aaffffffffffffffffffffffffffffffffffffffffffff" expectedMatch := TestParseID("20bdc1402a6fc9b633aaffffffffffffffffffffffffffffffffffffffffffff")
if f != expectedMatch { if f != expectedMatch {
t.Errorf("Wrong match returned want %s, got %s", expectedMatch, f) t.Errorf("Wrong match returned want %s, got %s", expectedMatch, f)
} }
@ -52,7 +52,7 @@ func TestFind(t *testing.T) {
if _, ok := err.(*NoIDByPrefixError); !ok || !strings.Contains(err.Error(), "NotAPrefix") { if _, ok := err.(*NoIDByPrefixError); !ok || !strings.Contains(err.Error(), "NotAPrefix") {
t.Error("Expected no snapshots to be found.") t.Error("Expected no snapshots to be found.")
} }
if f != "" { if !f.IsNull() {
t.Errorf("Find should not return a match on error.") t.Errorf("Find should not return a match on error.")
} }
@ -62,7 +62,7 @@ func TestFind(t *testing.T) {
if _, ok := err.(*NoIDByPrefixError); !ok || !strings.Contains(err.Error(), extraLengthID) { if _, ok := err.(*NoIDByPrefixError); !ok || !strings.Contains(err.Error(), extraLengthID) {
t.Errorf("Wrong error %v for no snapshots matched", err) t.Errorf("Wrong error %v for no snapshots matched", err)
} }
if f != "" { if !f.IsNull() {
t.Errorf("Find should not return a match on error.") t.Errorf("Find should not return a match on error.")
} }
@ -71,7 +71,7 @@ func TestFind(t *testing.T) {
if _, ok := err.(*MultipleIDMatchesError); !ok { if _, ok := err.(*MultipleIDMatchesError); !ok {
t.Errorf("Wrong error %v for multiple snapshots", err) t.Errorf("Wrong error %v for multiple snapshots", err)
} }
if f != "" { if !f.IsNull() {
t.Errorf("Find should not return a match on error.") t.Errorf("Find should not return a match on error.")
} }
} }

View File

@ -77,12 +77,7 @@ func FindSnapshot(ctx context.Context, be Lister, loader LoaderUnpacked, s strin
id, err := ParseID(s) id, err := ParseID(s)
if err != nil { if err != nil {
// find snapshot id with prefix // find snapshot id with prefix
name, err := Find(ctx, be, SnapshotFile, s) id, err = Find(ctx, be, SnapshotFile, s)
if err != nil {
return nil, err
}
id, err = ParseID(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }