pack: Handle small files

This commit is contained in:
Alexander Neumann 2017-06-08 20:40:12 +02:00
parent 92c0aa3854
commit eb767ab15f
1 changed files with 26 additions and 0 deletions

View File

@ -189,9 +189,26 @@ func readHeaderLength(rd io.ReaderAt, size int64) (uint32, error) {
const maxHeaderSize = 16 * 1024 * 1024
// we require at least one entry in the header, and one blob for a pack file
var minFileSize = entrySize + crypto.Extension
// 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) {
if size == 0 {
err := InvalidFileError{
Message: "file is empty",
}
return nil, errors.Wrap(err, "readHeader")
}
if size < int64(minFileSize) {
err := InvalidFileError{
Message: "file is too small",
}
return nil, errors.Wrap(err, "readHeader")
}
hl, err := readHeaderLength(rd, size)
if err != nil {
return nil, err
@ -218,6 +235,15 @@ func readHeader(rd io.ReaderAt, size int64) ([]byte, error) {
return buf, nil
}
// 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
}
// List returns the list of entries found in a pack file.
func List(k *crypto.Key, rd io.ReaderAt, size int64) (entries []restic.Blob, err error) {
buf, err := readHeader(rd, size)