2023-10-01 09:40:12 +00:00
|
|
|
package backend
|
2018-03-03 13:20:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-12-19 11:39:48 +00:00
|
|
|
"hash"
|
2018-03-03 13:20:54 +00:00
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RewindReader allows resetting the Reader to the beginning of the data.
|
|
|
|
type RewindReader interface {
|
|
|
|
io.Reader
|
|
|
|
|
|
|
|
// Rewind rewinds the reader so the same data can be read again from the
|
|
|
|
// start.
|
|
|
|
Rewind() error
|
|
|
|
|
|
|
|
// Length returns the number of bytes that can be read from the Reader
|
|
|
|
// after calling Rewind.
|
2018-03-04 09:40:42 +00:00
|
|
|
Length() int64
|
2020-12-19 11:39:48 +00:00
|
|
|
|
|
|
|
// Hash return a hash of the data if requested by the backed.
|
|
|
|
Hash() []byte
|
2018-03-03 13:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ByteReader implements a RewindReader for a byte slice.
|
|
|
|
type ByteReader struct {
|
|
|
|
*bytes.Reader
|
2020-12-19 11:39:48 +00:00
|
|
|
Len int64
|
|
|
|
hash []byte
|
2018-03-03 13:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rewind restarts the reader from the beginning of the data.
|
|
|
|
func (b *ByteReader) Rewind() error {
|
|
|
|
_, err := b.Reader.Seek(0, io.SeekStart)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Length returns the number of bytes read from the reader after Rewind is
|
|
|
|
// called.
|
2018-03-04 09:40:42 +00:00
|
|
|
func (b *ByteReader) Length() int64 {
|
2018-03-03 13:20:54 +00:00
|
|
|
return b.Len
|
|
|
|
}
|
|
|
|
|
2020-12-19 11:39:48 +00:00
|
|
|
// Hash return a hash of the data if requested by the backed.
|
|
|
|
func (b *ByteReader) Hash() []byte {
|
|
|
|
return b.hash
|
|
|
|
}
|
|
|
|
|
2018-03-03 13:20:54 +00:00
|
|
|
// statically ensure that *ByteReader implements RewindReader.
|
|
|
|
var _ RewindReader = &ByteReader{}
|
|
|
|
|
|
|
|
// NewByteReader prepares a ByteReader that can then be used to read buf.
|
2020-12-19 11:39:48 +00:00
|
|
|
func NewByteReader(buf []byte, hasher hash.Hash) *ByteReader {
|
|
|
|
var hash []byte
|
|
|
|
if hasher != nil {
|
|
|
|
// must never fail according to interface
|
2021-01-29 21:12:51 +00:00
|
|
|
_, err := hasher.Write(buf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-12-19 11:39:48 +00:00
|
|
|
hash = hasher.Sum(nil)
|
|
|
|
}
|
2018-03-03 13:20:54 +00:00
|
|
|
return &ByteReader{
|
|
|
|
Reader: bytes.NewReader(buf),
|
2018-03-04 09:40:42 +00:00
|
|
|
Len: int64(len(buf)),
|
2020-12-19 11:39:48 +00:00
|
|
|
hash: hash,
|
2018-03-03 13:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// statically ensure that *FileReader implements RewindReader.
|
|
|
|
var _ RewindReader = &FileReader{}
|
|
|
|
|
|
|
|
// FileReader implements a RewindReader for an open file.
|
|
|
|
type FileReader struct {
|
|
|
|
io.ReadSeeker
|
2020-12-19 11:39:48 +00:00
|
|
|
Len int64
|
|
|
|
hash []byte
|
2018-03-03 13:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rewind seeks to the beginning of the file.
|
|
|
|
func (f *FileReader) Rewind() error {
|
|
|
|
_, err := f.ReadSeeker.Seek(0, io.SeekStart)
|
|
|
|
return errors.Wrap(err, "Seek")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Length returns the length of the file.
|
2018-03-04 09:40:42 +00:00
|
|
|
func (f *FileReader) Length() int64 {
|
2018-03-03 13:20:54 +00:00
|
|
|
return f.Len
|
|
|
|
}
|
|
|
|
|
2020-12-19 11:39:48 +00:00
|
|
|
// Hash return a hash of the data if requested by the backed.
|
|
|
|
func (f *FileReader) Hash() []byte {
|
|
|
|
return f.hash
|
|
|
|
}
|
|
|
|
|
2018-03-03 13:20:54 +00:00
|
|
|
// NewFileReader wraps f in a *FileReader.
|
2020-12-19 11:39:48 +00:00
|
|
|
func NewFileReader(f io.ReadSeeker, hash []byte) (*FileReader, error) {
|
2018-03-03 13:20:54 +00:00
|
|
|
pos, err := f.Seek(0, io.SeekEnd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Seek")
|
|
|
|
}
|
|
|
|
|
|
|
|
fr := &FileReader{
|
|
|
|
ReadSeeker: f,
|
2018-03-04 09:40:42 +00:00
|
|
|
Len: pos,
|
2020-12-19 11:39:48 +00:00
|
|
|
hash: hash,
|
2018-03-03 13:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = fr.Rewind()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fr, nil
|
|
|
|
}
|