forked from TrueCloudLab/restic
local: stricter handling of short files
This commit is contained in:
parent
b4895ebd76
commit
e793c002ec
1 changed files with 19 additions and 0 deletions
|
@ -2,6 +2,7 @@ package local
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"hash"
|
"hash"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
@ -30,6 +31,8 @@ type Local struct {
|
||||||
// ensure statically that *Local implements backend.Backend.
|
// ensure statically that *Local implements backend.Backend.
|
||||||
var _ backend.Backend = &Local{}
|
var _ backend.Backend = &Local{}
|
||||||
|
|
||||||
|
var errTooShort = fmt.Errorf("file is too short")
|
||||||
|
|
||||||
func NewFactory() location.Factory {
|
func NewFactory() location.Factory {
|
||||||
return location.NewLimitedBackendFactory("local", ParseConfig, location.NoPassword, limiter.WrapBackendConstructor(Create), limiter.WrapBackendConstructor(Open))
|
return location.NewLimitedBackendFactory("local", ParseConfig, location.NoPassword, limiter.WrapBackendConstructor(Create), limiter.WrapBackendConstructor(Open))
|
||||||
}
|
}
|
||||||
|
@ -110,6 +113,10 @@ func (b *Local) IsNotExist(err error) bool {
|
||||||
return errors.Is(err, os.ErrNotExist)
|
return errors.Is(err, os.ErrNotExist)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Local) IsPermanentError(err error) bool {
|
||||||
|
return b.IsNotExist(err) || errors.Is(err, errTooShort) || errors.Is(err, os.ErrPermission)
|
||||||
|
}
|
||||||
|
|
||||||
// Save stores data in the backend at the handle.
|
// Save stores data in the backend at the handle.
|
||||||
func (b *Local) Save(_ context.Context, h backend.Handle, rd backend.RewindReader) (err error) {
|
func (b *Local) Save(_ context.Context, h backend.Handle, rd backend.RewindReader) (err error) {
|
||||||
finalname := b.Filename(h)
|
finalname := b.Filename(h)
|
||||||
|
@ -219,6 +226,18 @@ func (b *Local) openReader(_ context.Context, h backend.Handle, length int, offs
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fi, err := f.Stat()
|
||||||
|
if err != nil {
|
||||||
|
_ = f.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
size := fi.Size()
|
||||||
|
if size < offset+int64(length) {
|
||||||
|
_ = f.Close()
|
||||||
|
return nil, errTooShort
|
||||||
|
}
|
||||||
|
|
||||||
if offset > 0 {
|
if offset > 0 {
|
||||||
_, err = f.Seek(offset, 0)
|
_, err = f.Seek(offset, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue