2015-04-26 13:36:49 +00:00
|
|
|
package pack
|
|
|
|
|
|
|
|
import (
|
2016-01-24 18:30:14 +00:00
|
|
|
"bytes"
|
2015-04-26 13:36:49 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
|
|
|
"github.com/restic/restic/internal/errors"
|
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"
|
2015-04-26 13:36:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Packer is used to create a new Pack.
|
|
|
|
type Packer struct {
|
2016-08-31 20:39:36 +00:00
|
|
|
blobs []restic.Blob
|
2015-04-26 13:36:49 +00:00
|
|
|
|
|
|
|
bytes uint
|
|
|
|
k *crypto.Key
|
2016-03-06 11:26:25 +00:00
|
|
|
wr io.Writer
|
2015-04-26 13:36:49 +00:00
|
|
|
|
|
|
|
m sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPacker returns a new Packer that can be used to pack blobs
|
2016-03-06 11:26:25 +00:00
|
|
|
// together. If wr is nil, a bytes.Buffer is used.
|
|
|
|
func NewPacker(k *crypto.Key, wr io.Writer) *Packer {
|
2016-03-06 11:34:23 +00:00
|
|
|
if wr == nil {
|
|
|
|
wr = bytes.NewBuffer(nil)
|
|
|
|
}
|
2016-03-06 11:26:25 +00:00
|
|
|
return &Packer{k: k, wr: wr}
|
2015-04-26 13:36:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add saves the data read from rd as a new blob to the packer. Returned is the
|
|
|
|
// number of bytes written to the pack.
|
2016-08-31 18:58:57 +00:00
|
|
|
func (p *Packer) Add(t restic.BlobType, id restic.ID, data []byte) (int, error) {
|
2015-04-26 13:36:49 +00:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
2016-08-31 20:39:36 +00:00
|
|
|
c := restic.Blob{Type: t, ID: id}
|
2015-04-26 13:36:49 +00:00
|
|
|
|
2016-03-06 11:26:25 +00:00
|
|
|
n, err := p.wr.Write(data)
|
2015-08-08 11:47:08 +00:00
|
|
|
c.Length = uint(n)
|
2015-04-26 13:36:49 +00:00
|
|
|
c.Offset = p.bytes
|
|
|
|
p.bytes += uint(n)
|
|
|
|
p.blobs = append(p.blobs, c)
|
|
|
|
|
2016-08-29 20:16:58 +00:00
|
|
|
return n, errors.Wrap(err, "Write")
|
2015-04-26 13:36:49 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 19:37:59 +00:00
|
|
|
var entrySize = uint(binary.Size(restic.BlobType(0)) + binary.Size(uint32(0)) + len(restic.ID{}))
|
2015-04-26 13:36:49 +00:00
|
|
|
|
|
|
|
// headerEntry is used with encoding/binary to read and write header entries
|
|
|
|
type headerEntry struct {
|
2016-08-04 16:40:31 +00:00
|
|
|
Type uint8
|
2015-04-26 13:36:49 +00:00
|
|
|
Length uint32
|
2016-09-01 19:37:59 +00:00
|
|
|
ID restic.ID
|
2015-04-26 13:36:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finalize writes the header for all added blobs and finalizes the pack.
|
2020-03-09 13:11:25 +00:00
|
|
|
// Returned are the number of bytes written, including the header.
|
2016-03-06 11:26:25 +00:00
|
|
|
func (p *Packer) Finalize() (uint, error) {
|
2015-04-26 13:36:49 +00:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
2016-01-24 18:30:14 +00:00
|
|
|
bytesWritten := p.bytes
|
2015-04-26 13:36:49 +00:00
|
|
|
|
2016-01-24 18:30:14 +00:00
|
|
|
hdrBuf := bytes.NewBuffer(nil)
|
|
|
|
bytesHeader, err := p.writeHeader(hdrBuf)
|
2015-04-29 22:36:36 +00:00
|
|
|
if err != nil {
|
2016-03-06 11:26:25 +00:00
|
|
|
return 0, err
|
2015-04-26 13:36:49 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 10:33:57 +00:00
|
|
|
encryptedHeader := make([]byte, 0, hdrBuf.Len()+p.k.Overhead()+p.k.NonceSize())
|
|
|
|
nonce := crypto.NewRandomNonce()
|
|
|
|
encryptedHeader = append(encryptedHeader, nonce...)
|
|
|
|
encryptedHeader = p.k.Seal(encryptedHeader, nonce, hdrBuf.Bytes(), nil)
|
2015-04-29 22:41:11 +00:00
|
|
|
|
2016-01-24 18:30:14 +00:00
|
|
|
// append the header
|
2016-03-06 11:26:25 +00:00
|
|
|
n, err := p.wr.Write(encryptedHeader)
|
2015-04-26 13:36:49 +00:00
|
|
|
if err != nil {
|
2016-08-29 20:16:58 +00:00
|
|
|
return 0, errors.Wrap(err, "Write")
|
2016-01-24 18:30:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 11:57:05 +00:00
|
|
|
hdrBytes := restic.CiphertextLength(int(bytesHeader))
|
|
|
|
if n != hdrBytes {
|
2016-03-06 11:26:25 +00:00
|
|
|
return 0, errors.New("wrong number of bytes written")
|
2015-04-26 13:36:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 11:57:05 +00:00
|
|
|
bytesWritten += uint(hdrBytes)
|
2015-04-26 13:36:49 +00:00
|
|
|
|
|
|
|
// write length
|
2017-01-13 11:57:05 +00:00
|
|
|
err = binary.Write(p.wr, binary.LittleEndian, uint32(restic.CiphertextLength(len(p.blobs)*int(entrySize))))
|
2015-04-26 13:36:49 +00:00
|
|
|
if err != nil {
|
2016-08-29 20:16:58 +00:00
|
|
|
return 0, errors.Wrap(err, "binary.Write")
|
2015-04-26 13:36:49 +00:00
|
|
|
}
|
2015-04-29 22:19:56 +00:00
|
|
|
bytesWritten += uint(binary.Size(uint32(0)))
|
2015-04-26 13:36:49 +00:00
|
|
|
|
2015-04-29 22:06:08 +00:00
|
|
|
p.bytes = uint(bytesWritten)
|
2016-03-06 11:26:25 +00:00
|
|
|
return bytesWritten, nil
|
2015-04-26 13:36:49 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 22:36:36 +00:00
|
|
|
// writeHeader constructs and writes the header to wr.
|
|
|
|
func (p *Packer) writeHeader(wr io.Writer) (bytesWritten uint, err error) {
|
|
|
|
for _, b := range p.blobs {
|
|
|
|
entry := headerEntry{
|
2015-08-08 11:47:08 +00:00
|
|
|
Length: uint32(b.Length),
|
2015-07-25 15:05:45 +00:00
|
|
|
ID: b.ID,
|
2015-04-29 22:36:36 +00:00
|
|
|
}
|
|
|
|
|
2016-08-04 16:40:31 +00:00
|
|
|
switch b.Type {
|
2016-08-31 18:58:57 +00:00
|
|
|
case restic.DataBlob:
|
2016-08-04 16:40:31 +00:00
|
|
|
entry.Type = 0
|
2016-08-31 18:58:57 +00:00
|
|
|
case restic.TreeBlob:
|
2016-08-04 16:40:31 +00:00
|
|
|
entry.Type = 1
|
|
|
|
default:
|
2016-08-21 15:48:36 +00:00
|
|
|
return 0, errors.Errorf("invalid blob type %v", b.Type)
|
2016-08-04 16:40:31 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 22:36:36 +00:00
|
|
|
err := binary.Write(wr, binary.LittleEndian, entry)
|
|
|
|
if err != nil {
|
2016-08-29 20:16:58 +00:00
|
|
|
return bytesWritten, errors.Wrap(err, "binary.Write")
|
2015-04-29 22:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bytesWritten += entrySize
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-26 13:36:49 +00:00
|
|
|
// Size returns the number of bytes written so far.
|
|
|
|
func (p *Packer) Size() uint {
|
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
|
|
|
return p.bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count returns the number of blobs in this packer.
|
|
|
|
func (p *Packer) Count() int {
|
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
|
|
|
return len(p.blobs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blobs returns the slice of blobs that have been written.
|
2016-08-31 20:39:36 +00:00
|
|
|
func (p *Packer) Blobs() []restic.Blob {
|
2015-04-26 13:36:49 +00:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
|
|
|
return p.blobs
|
|
|
|
}
|
|
|
|
|
2016-03-06 11:26:25 +00:00
|
|
|
// Writer return the underlying writer.
|
|
|
|
func (p *Packer) Writer() io.Writer {
|
|
|
|
return p.wr
|
|
|
|
}
|
|
|
|
|
2015-04-26 13:36:49 +00:00
|
|
|
func (p *Packer) String() string {
|
|
|
|
return fmt.Sprintf("<Packer %d blobs, %d bytes>", len(p.blobs), p.bytes)
|
|
|
|
}
|
|
|
|
|
2018-02-21 23:22:18 +00:00
|
|
|
var (
|
|
|
|
// size of the header-length field at the end of the file
|
|
|
|
headerLengthSize = binary.Size(uint32(0))
|
|
|
|
// we require at least one entry in the header, and one blob for a pack file
|
|
|
|
minFileSize = entrySize + crypto.Extension + uint(headerLengthSize)
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
maxHeaderSize = 16 * 1024 * 1024
|
|
|
|
// number of header enries to download as part of header-length request
|
|
|
|
eagerEntries = 15
|
|
|
|
)
|
|
|
|
|
2018-02-22 17:37:10 +00:00
|
|
|
// readRecords reads up to max records from the underlying ReaderAt, returning
|
|
|
|
// the raw header, the total number of records in the header, and any error.
|
|
|
|
// If the header contains fewer than max entries, the header is truncated to
|
|
|
|
// the appropriate size.
|
|
|
|
func readRecords(rd io.ReaderAt, size int64, max int) ([]byte, int, error) {
|
2018-02-21 23:22:18 +00:00
|
|
|
var bufsize int
|
2018-02-22 17:37:10 +00:00
|
|
|
bufsize += max * int(entrySize)
|
2018-02-21 23:22:18 +00:00
|
|
|
bufsize += crypto.Extension
|
|
|
|
bufsize += headerLengthSize
|
|
|
|
|
|
|
|
if bufsize > int(size) {
|
|
|
|
bufsize = int(size)
|
|
|
|
}
|
2016-08-25 19:51:07 +00:00
|
|
|
|
2018-02-21 23:22:18 +00:00
|
|
|
b := make([]byte, bufsize)
|
|
|
|
off := size - int64(bufsize)
|
|
|
|
if _, err := rd.ReadAt(b, off); err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2018-02-22 17:37:10 +00:00
|
|
|
hlen := binary.LittleEndian.Uint32(b[len(b)-headerLengthSize:])
|
2018-02-21 23:22:18 +00:00
|
|
|
b = b[:len(b)-headerLengthSize]
|
2018-02-22 17:37:10 +00:00
|
|
|
debug.Log("header length: %v", hlen)
|
2018-02-21 23:22:18 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
switch {
|
2018-02-22 17:37:10 +00:00
|
|
|
case hlen == 0:
|
2018-02-21 23:22:18 +00:00
|
|
|
err = InvalidFileError{Message: "header length is zero"}
|
2018-02-22 17:37:10 +00:00
|
|
|
case hlen < crypto.Extension:
|
2018-02-21 23:22:18 +00:00
|
|
|
err = InvalidFileError{Message: "header length is too small"}
|
2018-02-22 17:37:10 +00:00
|
|
|
case (hlen-crypto.Extension)%uint32(entrySize) != 0:
|
2018-02-21 23:22:18 +00:00
|
|
|
err = InvalidFileError{Message: "header length is invalid"}
|
2018-02-22 17:37:10 +00:00
|
|
|
case int64(hlen) > size-int64(headerLengthSize):
|
2018-02-21 23:22:18 +00:00
|
|
|
err = InvalidFileError{Message: "header is larger than file"}
|
2018-02-22 17:37:10 +00:00
|
|
|
case int64(hlen) > maxHeaderSize:
|
2018-02-21 23:22:18 +00:00
|
|
|
err = InvalidFileError{Message: "header is larger than maxHeaderSize"}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, errors.Wrap(err, "readHeader")
|
|
|
|
}
|
2018-02-21 02:28:37 +00:00
|
|
|
|
2018-02-22 17:37:10 +00:00
|
|
|
total := (int(hlen) - crypto.Extension) / int(entrySize)
|
|
|
|
if total < max {
|
|
|
|
// truncate to the beginning of the pack header
|
|
|
|
b = b[len(b)-int(hlen):]
|
2018-02-21 23:22:18 +00:00
|
|
|
}
|
2017-06-08 18:40:12 +00:00
|
|
|
|
2018-02-22 17:37:10 +00:00
|
|
|
return b, total, nil
|
2018-02-21 23:22:18 +00:00
|
|
|
}
|
2018-01-23 23:51:22 +00:00
|
|
|
|
2016-08-25 19:51:07 +00:00
|
|
|
// readHeader reads the header at the end of rd. size is the length of the
|
|
|
|
// whole data accessible in rd.
|
|
|
|
func readHeader(rd io.ReaderAt, size int64) ([]byte, error) {
|
2017-06-08 19:04:07 +00:00
|
|
|
debug.Log("size: %v", size)
|
2017-06-08 18:40:12 +00:00
|
|
|
if size < int64(minFileSize) {
|
2017-06-08 19:04:07 +00:00
|
|
|
err := InvalidFileError{Message: "file is too small"}
|
2017-06-08 18:40:12 +00:00
|
|
|
return nil, errors.Wrap(err, "readHeader")
|
|
|
|
}
|
|
|
|
|
2018-01-23 23:51:22 +00:00
|
|
|
// assuming extra request is significantly slower than extra bytes download,
|
|
|
|
// eagerly download eagerEntries header entries as part of header-length request.
|
|
|
|
// only make second request if actual number of entries is greater than eagerEntries
|
|
|
|
|
2018-02-21 23:22:18 +00:00
|
|
|
b, c, err := readRecords(rd, size, eagerEntries)
|
2016-08-07 11:12:52 +00:00
|
|
|
if err != nil {
|
2016-08-25 19:51:07 +00:00
|
|
|
return nil, err
|
2016-08-07 11:12:52 +00:00
|
|
|
}
|
2018-02-21 23:22:18 +00:00
|
|
|
if c <= eagerEntries {
|
|
|
|
// eager read sufficed, return what we got
|
|
|
|
return b, nil
|
2016-08-23 20:21:29 +00:00
|
|
|
}
|
2018-02-21 23:22:18 +00:00
|
|
|
b, _, err = readRecords(rd, size, c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-08-07 11:12:52 +00:00
|
|
|
}
|
2018-02-21 23:22:18 +00:00
|
|
|
return b, nil
|
2016-08-25 19:51:07 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 18:40:12 +00:00
|
|
|
// InvalidFileError is return when a file is found that is not a pack file.
|
|
|
|
type InvalidFileError struct {
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e InvalidFileError) Error() string {
|
|
|
|
return e.Message
|
|
|
|
}
|
|
|
|
|
2016-08-25 19:51:07 +00:00
|
|
|
// List returns the list of entries found in a pack file.
|
2016-08-31 20:39:36 +00:00
|
|
|
func List(k *crypto.Key, rd io.ReaderAt, size int64) (entries []restic.Blob, err error) {
|
2016-08-25 19:51:07 +00:00
|
|
|
buf, err := readHeader(rd, size)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-26 13:36:49 +00:00
|
|
|
|
2017-10-29 10:33:57 +00:00
|
|
|
if len(buf) < k.NonceSize()+k.Overhead() {
|
|
|
|
return nil, errors.New("invalid header, too small")
|
|
|
|
}
|
|
|
|
|
|
|
|
nonce, buf := buf[:k.NonceSize()], buf[k.NonceSize():]
|
|
|
|
buf, err = k.Open(buf[:0], nonce, buf, nil)
|
2015-04-26 13:36:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-03 11:34:04 +00:00
|
|
|
hdrRd := bytes.NewReader(buf)
|
2016-08-07 12:50:24 +00:00
|
|
|
|
2017-10-29 10:33:57 +00:00
|
|
|
entries = make([]restic.Blob, 0, uint(len(buf))/entrySize)
|
2017-01-15 14:27:58 +00:00
|
|
|
|
2015-10-25 17:07:51 +00:00
|
|
|
pos := uint(0)
|
|
|
|
for {
|
|
|
|
e := headerEntry{}
|
2016-08-25 19:51:07 +00:00
|
|
|
err = binary.Read(hdrRd, binary.LittleEndian, &e)
|
2016-08-29 17:18:57 +00:00
|
|
|
if errors.Cause(err) == io.EOF {
|
2015-10-25 17:07:51 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2016-08-29 20:16:58 +00:00
|
|
|
return nil, errors.Wrap(err, "binary.Read")
|
2015-04-26 13:36:49 +00:00
|
|
|
}
|
2015-10-25 17:07:51 +00:00
|
|
|
|
2016-08-31 20:39:36 +00:00
|
|
|
entry := restic.Blob{
|
2015-10-25 17:07:51 +00:00
|
|
|
Length: uint(e.Length),
|
|
|
|
ID: e.ID,
|
|
|
|
Offset: pos,
|
2016-08-04 16:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch e.Type {
|
|
|
|
case 0:
|
2016-08-31 18:58:57 +00:00
|
|
|
entry.Type = restic.DataBlob
|
2016-08-04 16:40:31 +00:00
|
|
|
case 1:
|
2016-08-31 18:58:57 +00:00
|
|
|
entry.Type = restic.TreeBlob
|
2016-08-04 16:40:31 +00:00
|
|
|
default:
|
2016-08-21 15:48:36 +00:00
|
|
|
return nil, errors.Errorf("invalid type %d", e.Type)
|
2016-08-04 16:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
entries = append(entries, entry)
|
2015-10-25 17:07:51 +00:00
|
|
|
|
|
|
|
pos += uint(e.Length)
|
2015-04-26 13:36:49 +00:00
|
|
|
}
|
|
|
|
|
2016-08-25 19:08:16 +00:00
|
|
|
return entries, nil
|
2015-04-26 13:36:49 +00:00
|
|
|
}
|