2015-05-09 21:52:03 +00:00
|
|
|
package repository
|
2015-04-26 15:10:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-09-23 20:27:48 +00:00
|
|
|
"errors"
|
2015-04-26 15:44:38 +00:00
|
|
|
"fmt"
|
2015-04-26 15:10:31 +00:00
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/restic/restic/backend"
|
2015-07-19 13:16:16 +00:00
|
|
|
"github.com/restic/restic/crypto"
|
2015-04-26 15:10:31 +00:00
|
|
|
"github.com/restic/restic/debug"
|
|
|
|
"github.com/restic/restic/pack"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Index holds a lookup table for id -> pack.
|
|
|
|
type Index struct {
|
|
|
|
m sync.Mutex
|
2015-07-25 22:06:40 +00:00
|
|
|
pack map[backend.ID]indexEntry
|
2015-04-26 15:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type indexEntry struct {
|
|
|
|
tpe pack.BlobType
|
2015-07-25 15:05:45 +00:00
|
|
|
packID *backend.ID
|
2015-04-26 15:10:31 +00:00
|
|
|
offset uint
|
|
|
|
length uint
|
|
|
|
old bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewIndex returns a new index.
|
|
|
|
func NewIndex() *Index {
|
|
|
|
return &Index{
|
2015-07-25 22:06:40 +00:00
|
|
|
pack: make(map[backend.ID]indexEntry),
|
2015-04-26 15:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-25 15:05:45 +00:00
|
|
|
func (idx *Index) store(t pack.BlobType, id backend.ID, pack *backend.ID, offset, length uint, old bool) {
|
2015-07-25 22:06:40 +00:00
|
|
|
idx.pack[id] = indexEntry{
|
2015-04-26 15:10:31 +00:00
|
|
|
tpe: t,
|
|
|
|
packID: pack,
|
|
|
|
offset: offset,
|
|
|
|
length: length,
|
|
|
|
old: old,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 20:27:48 +00:00
|
|
|
// Store remembers the id and pack in the index. An existing entry will be
|
|
|
|
// silently overwritten.
|
2015-07-25 15:05:45 +00:00
|
|
|
func (idx *Index) Store(t pack.BlobType, id backend.ID, pack *backend.ID, offset, length uint) {
|
2015-04-26 15:10:31 +00:00
|
|
|
idx.m.Lock()
|
|
|
|
defer idx.m.Unlock()
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
debug.Log("Index.Store", "pack %v contains id %v (%v), offset %v, length %v",
|
|
|
|
pack.Str(), id.Str(), t, offset, length)
|
2015-04-26 15:10:31 +00:00
|
|
|
|
|
|
|
idx.store(t, id, pack, offset, length, false)
|
|
|
|
}
|
|
|
|
|
2015-09-23 20:27:48 +00:00
|
|
|
// StoreInProgress adds a preliminary index entry for a blob that is about to be
|
|
|
|
// saved. The entry must be updated using Store once the the blob has been
|
|
|
|
// written to a pack. Adding an preliminary index fails if there's an existing
|
|
|
|
// entry associated with the same id.
|
|
|
|
func (idx *Index) StoreInProgress(t pack.BlobType, id backend.ID) error {
|
|
|
|
idx.m.Lock()
|
|
|
|
defer idx.m.Unlock()
|
|
|
|
|
|
|
|
if _, hasID := idx.pack[id]; hasID {
|
|
|
|
errorMsg := fmt.Sprintf("index already contains id %v (%v)", id.Str(), t)
|
|
|
|
debug.Log("Index.StoreInProgress", errorMsg)
|
|
|
|
return errors.New(errorMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
idx.store(t, id, nil, 0, 0, false)
|
|
|
|
debug.Log("Index.StoreInProgress", "preliminary entry added for id %v (%v)",
|
|
|
|
id.Str(), t)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-26 15:10:31 +00:00
|
|
|
// Remove removes the pack ID from the index.
|
|
|
|
func (idx *Index) Remove(packID backend.ID) {
|
|
|
|
idx.m.Lock()
|
|
|
|
defer idx.m.Unlock()
|
|
|
|
|
|
|
|
debug.Log("Index.Remove", "id %v removed", packID.Str())
|
|
|
|
|
2015-07-25 22:06:40 +00:00
|
|
|
if _, ok := idx.pack[packID]; ok {
|
|
|
|
delete(idx.pack, packID)
|
2015-04-26 15:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup returns the pack for the id.
|
2015-07-25 15:05:45 +00:00
|
|
|
func (idx *Index) Lookup(id backend.ID) (packID *backend.ID, tpe pack.BlobType, offset, length uint, err error) {
|
2015-04-26 15:10:31 +00:00
|
|
|
idx.m.Lock()
|
|
|
|
defer idx.m.Unlock()
|
|
|
|
|
2015-07-25 22:06:40 +00:00
|
|
|
if p, ok := idx.pack[id]; ok {
|
2015-04-26 15:10:31 +00:00
|
|
|
debug.Log("Index.Lookup", "id %v found in pack %v at %d, length %d",
|
|
|
|
id.Str(), p.packID.Str(), p.offset, p.length)
|
|
|
|
return p.packID, p.tpe, p.offset, p.length, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
debug.Log("Index.Lookup", "id %v not found", id.Str())
|
2015-07-11 13:51:42 +00:00
|
|
|
return nil, pack.Data, 0, 0, fmt.Errorf("id %v not found in index", id)
|
2015-04-26 15:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Has returns true iff the id is listed in the index.
|
|
|
|
func (idx *Index) Has(id backend.ID) bool {
|
|
|
|
_, _, _, _, err := idx.Lookup(id)
|
|
|
|
if err == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-07-19 13:16:16 +00:00
|
|
|
// LookupSize returns the length of the cleartext content behind the
|
|
|
|
// given id
|
|
|
|
func (idx *Index) LookupSize(id backend.ID) (cleartextLength uint, err error) {
|
|
|
|
_, _, _, encryptedLength, err := idx.Lookup(id)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return encryptedLength - crypto.Extension, nil
|
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// Merge loads all items from other into idx.
|
|
|
|
func (idx *Index) Merge(other *Index) {
|
|
|
|
debug.Log("Index.Merge", "Merge index with %p", other)
|
|
|
|
idx.m.Lock()
|
|
|
|
defer idx.m.Unlock()
|
|
|
|
|
|
|
|
for k, v := range other.pack {
|
|
|
|
if _, ok := idx.pack[k]; ok {
|
2015-08-08 11:47:08 +00:00
|
|
|
debug.Log("Index.Merge", "index already has key %v, updating", k.Str())
|
2015-04-26 15:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
idx.pack[k] = v
|
|
|
|
}
|
|
|
|
debug.Log("Index.Merge", "done merging index")
|
|
|
|
}
|
|
|
|
|
2015-06-28 22:22:25 +00:00
|
|
|
// PackedBlob is a blob already saved within a pack.
|
|
|
|
type PackedBlob struct {
|
|
|
|
pack.Blob
|
|
|
|
PackID backend.ID
|
|
|
|
}
|
|
|
|
|
2015-04-26 15:10:31 +00:00
|
|
|
// Each returns a channel that yields all blobs known to the index. If done is
|
|
|
|
// closed, the background goroutine terminates. This blocks any modification of
|
|
|
|
// the index.
|
2015-06-28 22:22:25 +00:00
|
|
|
func (idx *Index) Each(done chan struct{}) <-chan PackedBlob {
|
2015-04-26 15:10:31 +00:00
|
|
|
idx.m.Lock()
|
|
|
|
|
2015-06-28 22:22:25 +00:00
|
|
|
ch := make(chan PackedBlob)
|
2015-04-26 15:10:31 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer idx.m.Unlock()
|
|
|
|
defer func() {
|
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
2015-07-25 22:06:40 +00:00
|
|
|
for id, blob := range idx.pack {
|
2015-04-26 15:10:31 +00:00
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return
|
2015-06-28 22:22:25 +00:00
|
|
|
case ch <- PackedBlob{
|
|
|
|
Blob: pack.Blob{
|
|
|
|
ID: id,
|
|
|
|
Offset: blob.offset,
|
|
|
|
Type: blob.tpe,
|
2015-08-08 11:47:08 +00:00
|
|
|
Length: blob.length,
|
2015-06-28 22:22:25 +00:00
|
|
|
},
|
2015-07-25 15:05:45 +00:00
|
|
|
PackID: *blob.packID,
|
2015-04-26 15:10:31 +00:00
|
|
|
}:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// Count returns the number of blobs of type t in the index.
|
|
|
|
func (idx *Index) Count(t pack.BlobType) (n uint) {
|
|
|
|
debug.Log("Index.Count", "counting blobs of type %v", t)
|
|
|
|
idx.m.Lock()
|
|
|
|
defer idx.m.Unlock()
|
|
|
|
|
|
|
|
for id, blob := range idx.pack {
|
|
|
|
if blob.tpe == t {
|
|
|
|
n++
|
2015-08-08 11:47:08 +00:00
|
|
|
debug.Log("Index.Count", " blob %v counted: %v", id.Str(), blob)
|
2015-04-26 15:44:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-26 15:10:31 +00:00
|
|
|
type packJSON struct {
|
2015-07-25 22:06:40 +00:00
|
|
|
ID backend.ID `json:"id"`
|
2015-04-26 15:10:31 +00:00
|
|
|
Blobs []blobJSON `json:"blobs"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type blobJSON struct {
|
2015-07-25 22:06:40 +00:00
|
|
|
ID backend.ID `json:"id"`
|
2015-04-26 15:10:31 +00:00
|
|
|
Type pack.BlobType `json:"type"`
|
|
|
|
Offset uint `json:"offset"`
|
|
|
|
Length uint `json:"length"`
|
|
|
|
}
|
|
|
|
|
2015-05-16 12:05:19 +00:00
|
|
|
// generatePackList returns a list of packs containing only the index entries
|
|
|
|
// that selsectFn returned true for. If selectFn is nil, the list contains all
|
|
|
|
// blobs in the index.
|
|
|
|
func (idx *Index) generatePackList(selectFn func(indexEntry) bool) ([]*packJSON, error) {
|
2015-04-26 15:10:31 +00:00
|
|
|
list := []*packJSON{}
|
2015-07-25 22:06:40 +00:00
|
|
|
packs := make(map[backend.ID]*packJSON)
|
2015-04-26 15:10:31 +00:00
|
|
|
|
|
|
|
for id, blob := range idx.pack {
|
2015-10-11 18:45:42 +00:00
|
|
|
if blob.packID == nil {
|
|
|
|
panic("nil pack id")
|
|
|
|
}
|
|
|
|
|
2015-05-16 12:05:19 +00:00
|
|
|
if selectFn != nil && !selectFn(blob) {
|
2015-04-26 15:10:31 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-10-11 17:25:02 +00:00
|
|
|
debug.Log("Index.generatePackList", "handle blob %v", id.Str())
|
2015-04-26 15:44:38 +00:00
|
|
|
|
2015-07-25 15:05:45 +00:00
|
|
|
if blob.packID.IsNull() {
|
2015-05-16 12:05:19 +00:00
|
|
|
debug.Log("Index.generatePackList", "blob %q has no packID! (type %v, offset %v, length %v)",
|
2015-08-08 11:47:08 +00:00
|
|
|
id.Str(), blob.tpe, blob.offset, blob.length)
|
2015-05-16 12:05:19 +00:00
|
|
|
return nil, fmt.Errorf("unable to serialize index: pack for blob %v hasn't been written yet", id)
|
2015-04-26 15:44:38 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:10:31 +00:00
|
|
|
// see if pack is already in map
|
2015-07-25 22:06:40 +00:00
|
|
|
p, ok := packs[*blob.packID]
|
2015-04-26 15:10:31 +00:00
|
|
|
if !ok {
|
|
|
|
// else create new pack
|
2015-07-25 22:06:40 +00:00
|
|
|
p = &packJSON{ID: *blob.packID}
|
2015-04-26 15:10:31 +00:00
|
|
|
|
|
|
|
// and append it to the list and map
|
|
|
|
list = append(list, p)
|
|
|
|
packs[p.ID] = p
|
|
|
|
}
|
|
|
|
|
|
|
|
// add blob
|
|
|
|
p.Blobs = append(p.Blobs, blobJSON{
|
|
|
|
ID: id,
|
|
|
|
Type: blob.tpe,
|
|
|
|
Offset: blob.offset,
|
|
|
|
Length: blob.length,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-16 12:05:19 +00:00
|
|
|
debug.Log("Index.generatePackList", "done")
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2015-07-25 22:40:00 +00:00
|
|
|
type jsonIndex struct {
|
|
|
|
Supersedes []backend.ID `json:"supersedes,omitempty"`
|
|
|
|
Packs []*packJSON `json:"packs"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonOldIndex []*packJSON
|
|
|
|
|
2015-05-16 12:05:19 +00:00
|
|
|
// encode writes the JSON serialization of the index filtered by selectFn to enc.
|
2015-07-25 22:40:00 +00:00
|
|
|
func (idx *Index) encode(w io.Writer, supersedes []backend.ID, selectFn func(indexEntry) bool) error {
|
2015-05-16 12:05:19 +00:00
|
|
|
list, err := idx.generatePackList(func(entry indexEntry) bool {
|
|
|
|
return !entry.old
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
debug.Log("Index.Encode", "done")
|
|
|
|
|
2015-04-26 15:10:31 +00:00
|
|
|
enc := json.NewEncoder(w)
|
2015-07-25 22:40:00 +00:00
|
|
|
idxJSON := jsonIndex{
|
|
|
|
Supersedes: supersedes,
|
|
|
|
Packs: list,
|
|
|
|
}
|
|
|
|
return enc.Encode(idxJSON)
|
2015-04-26 15:10:31 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 12:05:19 +00:00
|
|
|
// Encode writes the JSON serialization of the index to the writer w. This
|
|
|
|
// serialization only contains new blobs added via idx.Store(), not old ones
|
|
|
|
// introduced via DecodeIndex().
|
|
|
|
func (idx *Index) Encode(w io.Writer) error {
|
|
|
|
debug.Log("Index.Encode", "encoding index")
|
|
|
|
idx.m.Lock()
|
|
|
|
defer idx.m.Unlock()
|
|
|
|
|
2015-07-25 22:40:00 +00:00
|
|
|
return idx.encode(w, nil, func(e indexEntry) bool { return !e.old })
|
2015-05-16 12:05:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dump writes the pretty-printed JSON representation of the index to w.
|
|
|
|
func (idx *Index) Dump(w io.Writer) error {
|
|
|
|
debug.Log("Index.Dump", "dumping index")
|
|
|
|
idx.m.Lock()
|
|
|
|
defer idx.m.Unlock()
|
|
|
|
|
|
|
|
list, err := idx.generatePackList(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := json.MarshalIndent(list, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Write(append(buf, '\n'))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
debug.Log("Index.Dump", "done")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-26 19:58:03 +00:00
|
|
|
// 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")
|
|
|
|
|
2015-04-26 15:10:31 +00:00
|
|
|
// DecodeIndex loads and unserializes an index from rd.
|
2015-07-25 22:40:00 +00:00
|
|
|
func DecodeIndex(rd io.Reader) (*Index, backend.IDs, error) {
|
2015-04-26 15:44:38 +00:00
|
|
|
debug.Log("Index.DecodeIndex", "Start decoding index")
|
2015-07-25 22:40:00 +00:00
|
|
|
idxJSON := jsonIndex{}
|
|
|
|
|
|
|
|
dec := json.NewDecoder(rd)
|
|
|
|
err := dec.Decode(&idxJSON)
|
|
|
|
if err != nil {
|
|
|
|
debug.Log("Index.DecodeIndex", "Error %#v", err)
|
2015-07-26 19:58:03 +00:00
|
|
|
|
|
|
|
if isErrOldIndex(err) {
|
|
|
|
debug.Log("Index.DecodeIndex", "index is probably old format, trying that")
|
|
|
|
err = ErrOldIndexFormat
|
|
|
|
}
|
|
|
|
|
2015-07-25 22:40:00 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
idx := NewIndex()
|
|
|
|
for _, pack := range idxJSON.Packs {
|
|
|
|
for _, blob := range pack.Blobs {
|
|
|
|
idx.store(blob.Type, blob.ID, &pack.ID, blob.Offset, blob.Length, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug.Log("Index.DecodeIndex", "done")
|
|
|
|
return idx, idxJSON.Supersedes, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeOldIndex loads and unserializes an index in the old format from rd.
|
|
|
|
func DecodeOldIndex(rd io.Reader) (*Index, backend.IDs, error) {
|
|
|
|
debug.Log("Index.DecodeOldIndex", "Start decoding old index")
|
2015-04-26 15:10:31 +00:00
|
|
|
list := []*packJSON{}
|
|
|
|
|
|
|
|
dec := json.NewDecoder(rd)
|
|
|
|
err := dec.Decode(&list)
|
|
|
|
if err != nil {
|
2015-07-25 22:40:00 +00:00
|
|
|
debug.Log("Index.DecodeOldIndex", "Error %#v", err)
|
|
|
|
return nil, nil, err
|
2015-04-26 15:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
idx := NewIndex()
|
|
|
|
for _, pack := range list {
|
|
|
|
for _, blob := range pack.Blobs {
|
2015-07-25 22:06:40 +00:00
|
|
|
idx.store(blob.Type, blob.ID, &pack.ID, blob.Offset, blob.Length, true)
|
2015-04-26 15:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-25 22:40:00 +00:00
|
|
|
debug.Log("Index.DecodeOldIndex", "done")
|
|
|
|
return idx, backend.IDs{}, err
|
2015-04-26 15:10:31 +00:00
|
|
|
}
|