2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-29 05:13:09 +00:00
restic/backend/generic.go

71 lines
1.6 KiB
Go
Raw Normal View History

2014-09-23 20:39:12 +00:00
package backend
2016-01-26 20:49:33 +00:00
import "errors"
2014-09-23 20:39:12 +00:00
2016-01-26 20:49:33 +00:00
// ErrNoIDPrefixFound is returned by Find() when no ID for the given prefix
// could be found.
var ErrNoIDPrefixFound = errors.New("no ID found")
2016-01-26 20:49:33 +00:00
// ErrMultipleIDMatches is returned by Find() when multiple IDs with the given
// prefix are found.
var ErrMultipleIDMatches = errors.New("multiple IDs with prefix found")
2015-03-28 10:50:23 +00:00
// Find loads the list of all blobs of type t and searches for names which
// start with prefix. If none is found, nil and ErrNoIDPrefixFound is returned.
// If more than one is found, nil and ErrMultipleIDMatches is returned.
func Find(be Lister, t Type, prefix string) (string, error) {
done := make(chan struct{})
defer close(done)
2015-03-28 10:50:23 +00:00
match := ""
// TODO: optimize by sorting list etc.
2015-03-28 10:50:23 +00:00
for name := range be.List(t, done) {
if prefix == name[:len(prefix)] {
if match == "" {
match = name
} else {
2015-03-28 10:50:23 +00:00
return "", ErrMultipleIDMatches
}
}
}
2015-03-28 10:50:23 +00:00
if match != "" {
return match, nil
}
2015-03-28 10:50:23 +00:00
return "", ErrNoIDPrefixFound
}
2016-01-26 20:49:33 +00:00
const minPrefixLength = 8
// PrefixLength returns the number of bytes required so that all prefixes of
2015-03-28 10:50:23 +00:00
// all names of type t are unique.
2014-12-21 16:02:49 +00:00
func PrefixLength(be Lister, t Type) (int, error) {
2015-03-28 10:50:23 +00:00
done := make(chan struct{})
defer close(done)
// load all IDs of the given type
2015-03-28 10:50:23 +00:00
list := make([]string, 0, 100)
for name := range be.List(t, done) {
list = append(list, name)
}
// select prefixes of length l, test if the last one is the same as the current one
outer:
2016-01-26 20:49:33 +00:00
for l := minPrefixLength; l < IDSize; l++ {
2015-03-28 10:50:23 +00:00
var last string
2015-03-28 10:50:23 +00:00
for _, name := range list {
if last == name[:l] {
continue outer
}
2015-03-28 10:50:23 +00:00
last = name[:l]
}
return l, nil
}
return IDSize, nil
}