Version: v0.1.0 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2015 License: BSD-2-Clause Imports: 18 Imported by: 0



Package repository implements a restic repository on top of a backend.



View Source
const RepoVersion = 1

RepoVersion is the version that is written to the config when a repository is newly created with Init().


View Source
var (
	// ErrNoKeyFound is returned when no key for the repository could be decrypted.
	ErrNoKeyFound = errors.New("no key could be found")


func FilesInParallel

func FilesInParallel(repo backend.Lister, t backend.Type, n uint, f ParallelWorkFunc) error

FilesInParallel runs n workers of f in parallel, on the IDs that repo.List(t) yield. If f returns an error, the process is aborted and the first error is returned.


type Blob

type Blob struct {
	ID          *backend.ID `json:"id,omitempty"`
	Size        uint64      `json:"size,omitempty"`
	Storage     *backend.ID `json:"sid,omitempty"`   // encrypted ID
	StorageSize uint64      `json:"ssize,omitempty"` // encrypted Size

func (Blob) Compare

func (b Blob) Compare(other Blob) int

Compare compares two blobs by comparing the ID and the size. It returns -1, 0, or 1.

func (Blob) String

func (b Blob) String() string

func (Blob) Valid

func (b Blob) Valid() bool

type Blobs

type Blobs []Blob

type Config

type Config struct {
	Version           uint        `json:"version"`
	ID                string      `json:"id"`
	ChunkerPolynomial chunker.Pol `json:"chunker_polynomial"`

Config contains the configuration for a repository.

func CreateConfig

func CreateConfig(r JSONUnpackedSaver) (Config, error)

CreateConfig creates a config file with a randomly selected polynomial and ID and saves the config in the repository.

func LoadConfig

func LoadConfig(r JSONUnpackedLoader) (Config, error)

LoadConfig returns loads, checks and returns the config for a repository.

type Index

type Index struct {
	// contains filtered or unexported fields

Index holds a lookup table for id -> pack.

func DecodeIndex

func DecodeIndex(rd io.Reader) (*Index, error)

DecodeIndex loads and unserializes an index from rd.

func LoadIndex

func LoadIndex(repo *Repository, id string) (*Index, error)

LoadIndex loads the index id from backend and returns it.

func NewIndex

func NewIndex() *Index

NewIndex returns a new index.

func (*Index) Count

func (idx *Index) Count(t pack.BlobType) (n uint)

Count returns the number of blobs of type t in the index.

func (*Index) Dump

func (idx *Index) Dump(w io.Writer) error

Dump writes the pretty-printed JSON representation of the index to w.

func (*Index) Each

func (idx *Index) Each(done chan struct{}) <-chan PackedBlob

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.

func (*Index) Encode

func (idx *Index) Encode(w io.Writer) error

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 (*Index) Has

func (idx *Index) Has(id backend.ID) bool

Has returns true iff the id is listed in the index.

func (*Index) Lookup

func (idx *Index) Lookup(id backend.ID) (packID *backend.ID, tpe pack.BlobType, offset, length uint, err error)

Lookup returns the pack for the id.

func (*Index) LookupSize

func (idx *Index) LookupSize(id backend.ID) (cleartextLength uint, err error)

LookupSize returns the length of the cleartext content behind the given id

func (*Index) Merge

func (idx *Index) Merge(other *Index)

Merge loads all items from other into idx.

func (*Index) Remove

func (idx *Index) Remove(packID backend.ID)

Remove removes the pack ID from the index.

func (*Index) Store

func (idx *Index) Store(t pack.BlobType, id backend.ID, pack *backend.ID, offset, length uint)

Store remembers the id and pack in the index.

type JSONUnpackedLoader

type JSONUnpackedLoader interface {
	LoadJSONUnpacked(backend.Type, backend.ID, interface{}) error

JSONUnpackedLoader loads unpacked JSON.

type JSONUnpackedSaver

type JSONUnpackedSaver interface {
	SaveJSONUnpacked(backend.Type, interface{}) (backend.ID, error)

JSONUnpackedSaver saves unpacked JSON.

type Key

type Key struct {
	Created  time.Time `json:"created"`
	Username string    `json:"username"`
	Hostname string    `json:"hostname"`

	KDF  string `json:"kdf"`
	N    int    `json:"N"`
	R    int    `json:"r"`
	P    int    `json:"p"`
	Salt []byte `json:"salt"`
	Data []byte `json:"data"`
	// contains filtered or unexported fields

Key represents an encrypted master key for a repository.

func AddKey

func AddKey(s *Repository, password string, template *crypto.Key) (*Key, error)

AddKey adds a new key to an already existing repository.

func LoadKey

func LoadKey(s *Repository, name string) (*Key, error)

LoadKey loads a key from the backend.

func OpenKey

func OpenKey(s *Repository, name string, password string) (*Key, error)

OpenKey tries do decrypt the key specified by name with the given password.

func SearchKey

func SearchKey(s *Repository, password string) (*Key, error)

SearchKey tries to decrypt all keys in the backend with the given password. If none could be found, ErrNoKeyFound is returned.

func (Key) Name

func (k Key) Name() string

func (*Key) String

func (k *Key) String() string

func (*Key) Valid

func (k *Key) Valid() bool

Valid tests whether the mac and encryption keys are valid (i.e. not zero)

type PackedBlob

type PackedBlob struct {
	PackID backend.ID

PackedBlob is a blob already saved within a pack.

type ParallelWorkFunc

type ParallelWorkFunc func(id string, done <-chan struct{}) error

ParallelWorkFunc gets one file ID to work on. If an error is returned, processing stops. If done is closed, the function should return.

type Repository

type Repository struct {
	Config Config
	// contains filtered or unexported fields

Repository is used to access a repository in a backend.

func New

func New(be backend.Backend) *Repository

New returns a new repository with backend be.

func (*Repository) Backend

func (r *Repository) Backend() backend.Backend

Backend returns the backend for the repository.

func (*Repository) Close

func (r *Repository) Close() error

Close closes the repository by closing the backend.

func (*Repository) Count

func (r *Repository) Count(t backend.Type) (n uint)

Count returns the number of blobs of a given type in the backend.

func (*Repository) Decrypt

func (r *Repository) Decrypt(ciphertext []byte) ([]byte, error)

Decrypt authenticates and decrypts ciphertext and returns the plaintext.

func (*Repository) Delete

func (r *Repository) Delete() error

Delete calls backend.Delete() if implemented, and returns an error otherwise.

func (*Repository) Encrypt

func (r *Repository) Encrypt(ciphertext, plaintext []byte) ([]byte, error)

Encrypt encrypts and authenticates the plaintext and saves the result in ciphertext.

func (*Repository) Find

func (r *Repository) Find(t backend.Type, prefix string) (string, error)

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 (*Repository) Flush

func (r *Repository) Flush() error

Flush saves all remaining packs.

func (*Repository) Index

func (r *Repository) Index() *Index

Index returns the currently loaded Index.

func (*Repository) Init

func (r *Repository) Init(password string) error

Init creates a new master key with the supplied password, initializes and saves the repository config.

func (*Repository) Key

func (r *Repository) Key() *crypto.Key

Key returns the current master key.

func (*Repository) KeyName

func (r *Repository) KeyName() string

KeyName returns the name of the current key in the backend.

func (*Repository) List

func (r *Repository) List(t backend.Type, done <-chan struct{}) <-chan backend.ID

List returns a channel that yields all IDs of type t in the backend.

func (*Repository) LoadAndDecrypt

func (r *Repository) LoadAndDecrypt(t backend.Type, id backend.ID) ([]byte, error)

LoadAndDecrypt loads and decrypts data identified by t and id from the backend.

func (*Repository) LoadBlob

func (r *Repository) LoadBlob(t pack.BlobType, id backend.ID, plaintextBuf []byte) ([]byte, error)

LoadBlob tries to load and decrypt content identified by t and id from a pack from the backend, the result is stored in plaintextBuf, which must be large enough to hold the complete blob.

func (*Repository) LoadIndex

func (r *Repository) LoadIndex() error

LoadIndex loads all index files from the backend in parallel and merges them with the current index. The first error that occurred is returned.

func (*Repository) LoadJSONPack

func (r *Repository) LoadJSONPack(t pack.BlobType, id backend.ID, item interface{}) error

LoadJSONPack calls LoadBlob() to load a blob from the backend, decrypt the data and afterwards call json.Unmarshal on the item.

func (*Repository) LoadJSONUnpacked

func (r *Repository) LoadJSONUnpacked(t backend.Type, id backend.ID, item interface{}) error

LoadJSONUnpacked decrypts the data and afterwards calls json.Unmarshal on the item.

func (*Repository) LookupBlobSize

func (r *Repository) LookupBlobSize(id backend.ID) (uint, error)

LookupBlobSize returns the size of blob id.

func (*Repository) PrefixLength

func (r *Repository) PrefixLength(t backend.Type) (int, error)

PrefixLength returns the number of bytes required so that all prefixes of all IDs of type t are unique.

func (*Repository) SaveAndEncrypt

func (r *Repository) SaveAndEncrypt(t pack.BlobType, data []byte, id *backend.ID) (backend.ID, error)

SaveAndEncrypt encrypts data and stores it to the backend as type t. If data is small enough, it will be packed together with other small blobs.

func (*Repository) SaveFrom

func (r *Repository) SaveFrom(t pack.BlobType, id *backend.ID, length uint, rd io.Reader) error

SaveFrom encrypts data read from rd and stores it in a pack in the backend as type t.

func (*Repository) SaveIndex

func (r *Repository) SaveIndex() (backend.ID, error)

SaveIndex saves all new packs in the index in the backend, returned is the storage ID.

func (*Repository) SaveJSON

func (r *Repository) SaveJSON(t pack.BlobType, item interface{}) (backend.ID, error)

SaveJSON serialises item as JSON and encrypts and saves it in a pack in the backend as type t.

func (*Repository) SaveJSONUnpacked

func (r *Repository) SaveJSONUnpacked(t backend.Type, item interface{}) (backend.ID, error)

SaveJSONUnpacked serialises item as JSON and encrypts and saves it in the backend as type t, without a pack. It returns the storage hash.

func (*Repository) SearchKey

func (r *Repository) SearchKey(password string) error

SearchKey finds a key with the supplied password, afterwards the config is read and parsed.

func (*Repository) SetIndex

func (r *Repository) SetIndex(i *Index)

SetIndex instructs the repository to use the given index.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL