mirror of
				https://github.com/restic/restic.git
				synced 2025-11-04 05:20:52 +00:00 
			
		
		
		
	local: stricter handling of short files
This commit is contained in:
		@@ -2,6 +2,7 @@ package local
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"hash"
 | 
			
		||||
	"io"
 | 
			
		||||
	"os"
 | 
			
		||||
@@ -30,6 +31,8 @@ type Local struct {
 | 
			
		||||
// ensure statically that *Local implements backend.Backend.
 | 
			
		||||
var _ backend.Backend = &Local{}
 | 
			
		||||
 | 
			
		||||
var errTooShort = fmt.Errorf("file is too short")
 | 
			
		||||
 | 
			
		||||
func NewFactory() location.Factory {
 | 
			
		||||
	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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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.
 | 
			
		||||
func (b *Local) Save(_ context.Context, h backend.Handle, rd backend.RewindReader) (err error) {
 | 
			
		||||
	finalname := b.Filename(h)
 | 
			
		||||
@@ -219,6 +226,18 @@ func (b *Local) openReader(_ context.Context, h backend.Handle, length int, offs
 | 
			
		||||
		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 {
 | 
			
		||||
		_, err = f.Seek(offset, 0)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user