2015-11-18 19:20:25 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2021-08-22 13:10:00 +00:00
|
|
|
"bufio"
|
2017-06-04 09:16:55 +00:00
|
|
|
"context"
|
2020-12-19 11:39:48 +00:00
|
|
|
"io"
|
2021-08-22 13:10:00 +00:00
|
|
|
"io/ioutil"
|
2016-03-06 11:26:25 +00:00
|
|
|
"os"
|
2022-05-09 20:43:26 +00:00
|
|
|
"runtime"
|
2015-11-18 19:20:25 +00:00
|
|
|
"sync"
|
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/hashing"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-07-23 12:21:03 +00:00
|
|
|
|
|
|
|
"github.com/restic/restic/internal/crypto"
|
|
|
|
"github.com/restic/restic/internal/debug"
|
|
|
|
"github.com/restic/restic/internal/fs"
|
|
|
|
"github.com/restic/restic/internal/pack"
|
2020-03-19 10:27:19 +00:00
|
|
|
|
|
|
|
"github.com/minio/sha256-simd"
|
2015-11-18 19:20:25 +00:00
|
|
|
)
|
|
|
|
|
2017-01-22 16:53:00 +00:00
|
|
|
// Packer holds a pack.Packer together with a hash writer.
|
|
|
|
type Packer struct {
|
|
|
|
*pack.Packer
|
|
|
|
tmpfile *os.File
|
2021-08-22 13:10:00 +00:00
|
|
|
bufWr *bufio.Writer
|
2017-01-22 16:53:00 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 19:20:25 +00:00
|
|
|
// packerManager keeps a list of open packs and creates new on demand.
|
|
|
|
type packerManager struct {
|
2021-08-22 13:10:00 +00:00
|
|
|
tpe restic.BlobType
|
|
|
|
key *crypto.Key
|
|
|
|
queueFn func(ctx context.Context, t restic.BlobType, p *Packer) error
|
2021-08-07 20:52:05 +00:00
|
|
|
|
2022-07-02 21:30:26 +00:00
|
|
|
pm sync.Mutex
|
|
|
|
packer *Packer
|
|
|
|
packSize uint
|
2015-11-18 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
2016-03-06 13:20:48 +00:00
|
|
|
// newPackerManager returns an new packer manager which writes temporary files
|
2016-03-06 11:26:25 +00:00
|
|
|
// to a temporary directory
|
2022-07-02 21:30:26 +00:00
|
|
|
func newPackerManager(key *crypto.Key, tpe restic.BlobType, packSize uint, queueFn func(ctx context.Context, t restic.BlobType, p *Packer) error) *packerManager {
|
2016-03-06 12:14:06 +00:00
|
|
|
return &packerManager{
|
2022-07-02 21:30:26 +00:00
|
|
|
tpe: tpe,
|
|
|
|
key: key,
|
|
|
|
queueFn: queueFn,
|
|
|
|
packSize: packSize,
|
2021-08-07 20:52:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *packerManager) Flush(ctx context.Context) error {
|
|
|
|
r.pm.Lock()
|
|
|
|
defer r.pm.Unlock()
|
|
|
|
|
2021-08-22 13:10:00 +00:00
|
|
|
if r.packer != nil {
|
|
|
|
debug.Log("manually flushing pending pack")
|
|
|
|
err := r.queueFn(ctx, r.tpe, r.packer)
|
2021-08-07 20:52:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-22 13:10:00 +00:00
|
|
|
r.packer = nil
|
2021-08-07 20:52:05 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *packerManager) SaveBlob(ctx context.Context, t restic.BlobType, id restic.ID, ciphertext []byte, uncompressedLength int) (int, error) {
|
2021-08-22 13:10:00 +00:00
|
|
|
r.pm.Lock()
|
|
|
|
defer r.pm.Unlock()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
packer := r.packer
|
|
|
|
if r.packer == nil {
|
|
|
|
packer, err = r.newPacker()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-08-07 20:52:05 +00:00
|
|
|
}
|
2021-08-22 13:10:00 +00:00
|
|
|
// remember packer
|
|
|
|
r.packer = packer
|
2021-08-07 20:52:05 +00:00
|
|
|
|
|
|
|
// save ciphertext
|
2021-08-22 13:10:00 +00:00
|
|
|
// Add only appends bytes in memory to avoid being a scaling bottleneck
|
2021-08-07 20:52:05 +00:00
|
|
|
size, err := packer.Add(t, id, ciphertext, uncompressedLength)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2022-04-30 22:05:20 +00:00
|
|
|
// if the pack and header is not full enough, put back to the list
|
|
|
|
if packer.Size() < r.packSize && !packer.HeaderFull() {
|
2021-08-07 20:52:05 +00:00
|
|
|
debug.Log("pack is not full enough (%d bytes)", packer.Size())
|
|
|
|
return size, nil
|
2016-03-06 11:26:25 +00:00
|
|
|
}
|
2021-08-22 13:10:00 +00:00
|
|
|
// forget full packer
|
|
|
|
r.packer = nil
|
2021-08-07 20:52:05 +00:00
|
|
|
|
2021-08-22 13:10:00 +00:00
|
|
|
// call while holding lock to prevent findPacker from creating new packers if the uploaders are busy
|
2021-08-07 20:52:05 +00:00
|
|
|
// else write the pack to the backend
|
|
|
|
err = r.queueFn(ctx, t, packer)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return size + packer.HeaderOverhead(), nil
|
2016-03-06 11:26:25 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 19:20:25 +00:00
|
|
|
// findPacker returns a packer for a new blob of size bytes. Either a new one is
|
|
|
|
// created or one is returned that already has some blobs.
|
2021-08-22 13:10:00 +00:00
|
|
|
func (r *packerManager) newPacker() (packer *Packer, err error) {
|
2017-07-16 18:16:02 +00:00
|
|
|
debug.Log("create new pack")
|
2017-05-10 17:48:22 +00:00
|
|
|
tmpfile, err := fs.TempFile("", "restic-temp-pack-")
|
2016-03-06 11:26:25 +00:00
|
|
|
if err != nil {
|
2017-05-10 17:48:22 +00:00
|
|
|
return nil, errors.Wrap(err, "fs.TempFile")
|
2016-03-06 11:26:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-22 13:10:00 +00:00
|
|
|
bufWr := bufio.NewWriter(tmpfile)
|
|
|
|
p := pack.NewPacker(r.key, bufWr)
|
2017-01-22 16:53:00 +00:00
|
|
|
packer = &Packer{
|
|
|
|
Packer: p,
|
|
|
|
tmpfile: tmpfile,
|
2021-08-22 13:10:00 +00:00
|
|
|
bufWr: bufWr,
|
2017-01-22 16:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return packer, nil
|
2015-11-18 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// savePacker stores p in the backend.
|
2021-08-07 20:52:05 +00:00
|
|
|
func (r *Repository) savePacker(ctx context.Context, t restic.BlobType, p *Packer) error {
|
2017-07-16 18:24:37 +00:00
|
|
|
debug.Log("save packer for %v with %d blobs (%d bytes)\n", t, p.Packer.Count(), p.Packer.Size())
|
2021-08-07 20:52:05 +00:00
|
|
|
err := p.Packer.Finalize()
|
2015-11-18 19:20:25 +00:00
|
|
|
if err != nil {
|
2021-08-07 20:52:05 +00:00
|
|
|
return err
|
2015-11-18 19:20:25 +00:00
|
|
|
}
|
2021-08-22 13:10:00 +00:00
|
|
|
err = p.bufWr.Flush()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate sha256 hash in a second pass
|
|
|
|
var rd io.Reader
|
|
|
|
rd, err = restic.NewFileReader(p.tmpfile, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
beHasher := r.be.Hasher()
|
|
|
|
var beHr *hashing.Reader
|
|
|
|
if beHasher != nil {
|
|
|
|
beHr = hashing.NewReader(rd, beHasher)
|
|
|
|
rd = beHr
|
|
|
|
}
|
2015-11-18 19:20:25 +00:00
|
|
|
|
2021-08-22 13:10:00 +00:00
|
|
|
hr := hashing.NewReader(rd, sha256.New())
|
|
|
|
_, err = io.Copy(ioutil.Discard, hr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
id := restic.IDFromHash(hr.Sum(nil))
|
2020-07-28 08:13:11 +00:00
|
|
|
h := restic.Handle{Type: restic.PackFile, Name: id.String(),
|
|
|
|
ContainedBlobType: t}
|
2020-12-19 11:39:48 +00:00
|
|
|
var beHash []byte
|
2021-08-22 13:10:00 +00:00
|
|
|
if beHr != nil {
|
|
|
|
beHash = beHr.Sum(nil)
|
2020-12-19 11:39:48 +00:00
|
|
|
}
|
2021-08-22 13:10:00 +00:00
|
|
|
rrd, err := restic.NewFileReader(p.tmpfile, beHash)
|
2018-03-03 13:20:54 +00:00
|
|
|
if err != nil {
|
2021-08-07 20:52:05 +00:00
|
|
|
return err
|
2018-03-03 13:20:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-22 13:10:00 +00:00
|
|
|
err = r.be.Save(ctx, h, rrd)
|
2015-11-18 19:20:25 +00:00
|
|
|
if err != nil {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("Save(%v) error: %v", h, err)
|
2021-08-07 20:52:05 +00:00
|
|
|
return err
|
2015-11-18 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("saved as %v", h)
|
2015-11-18 19:20:25 +00:00
|
|
|
|
2017-01-23 17:45:15 +00:00
|
|
|
err = p.tmpfile.Close()
|
2017-01-22 16:53:00 +00:00
|
|
|
if err != nil {
|
2021-08-07 20:52:05 +00:00
|
|
|
return errors.Wrap(err, "close tempfile")
|
2017-01-22 16:53:00 +00:00
|
|
|
}
|
|
|
|
|
2022-05-09 20:43:26 +00:00
|
|
|
// on windows the tempfile is automatically deleted on close
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
err = fs.RemoveIfExists(p.tmpfile.Name())
|
|
|
|
if err != nil {
|
2021-08-07 20:52:05 +00:00
|
|
|
return errors.Wrap(err, "Remove")
|
2022-05-09 20:43:26 +00:00
|
|
|
}
|
2016-03-06 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 19:20:25 +00:00
|
|
|
// update blobs in the index
|
2020-06-06 20:20:44 +00:00
|
|
|
debug.Log(" updating blobs %v to pack %v", p.Packer.Blobs(), id)
|
|
|
|
r.idx.StorePack(id, p.Packer.Blobs())
|
2015-11-18 19:20:25 +00:00
|
|
|
|
2020-06-06 20:20:44 +00:00
|
|
|
// Save index if full
|
2020-06-12 07:24:38 +00:00
|
|
|
if r.noAutoIndexUpdate {
|
2021-08-07 20:52:05 +00:00
|
|
|
return nil
|
2020-06-12 07:24:38 +00:00
|
|
|
}
|
2021-08-07 20:52:05 +00:00
|
|
|
return r.idx.SaveFullIndex(ctx, r)
|
2015-11-18 19:20:25 +00:00
|
|
|
}
|