1
0
mirror of https://github.com/restic/restic.git synced 2025-08-11 05:37:45 +00:00
Files
.github
changelog
cmd
contrib
doc
docker
helpers
internal
archiver
backend
azure
b2
dryrun
gs
local
location
mem
rclone
rest
s3
sftp
swift
test
testdata
backend_error.go
backend_retry.go
backend_retry_test.go
doc.go
foreground.go
foreground_sysv.go
foreground_test.go
foreground_unix.go
foreground_windows.go
http_transport.go
layout.go
layout_default.go
layout_rest.go
layout_s3legacy.go
layout_test.go
paths.go
semaphore.go
shell_split.go
shell_split_test.go
utils.go
utils_test.go
bloblru
cache
checker
crypto
debug
dump
errors
filter
fs
fuse
hashing
limiter
migrations
mock
options
pack
repository
restic
restorer
selfupdate
test
textfile
ui
walker
.gitignore
.golangci.yml
CHANGELOG.md
CONTRIBUTING.md
GOVERNANCE.md
LICENSE
Makefile
README.md
VERSION
build.go
doc.go
go.mod
go.sum
restic/internal/backend/utils.go
2021-01-30 20:19:47 +01:00

60 lines
1.6 KiB
Go

package backend
import (
"bytes"
"context"
"io"
"github.com/restic/restic/internal/restic"
)
// LoadAll reads all data stored in the backend for the handle into the given
// buffer, which is truncated. If the buffer is not large enough or nil, a new
// one is allocated.
func LoadAll(ctx context.Context, buf []byte, be restic.Backend, h restic.Handle) ([]byte, error) {
err := be.Load(ctx, h, 0, 0, func(rd io.Reader) error {
// make sure this is idempotent, in case an error occurs this function may be called multiple times!
wr := bytes.NewBuffer(buf[:0])
_, cerr := io.Copy(wr, rd)
if cerr != nil {
return cerr
}
buf = wr.Bytes()
return nil
})
if err != nil {
return nil, err
}
return buf, nil
}
// LimitedReadCloser wraps io.LimitedReader and exposes the Close() method.
type LimitedReadCloser struct {
io.Closer
io.LimitedReader
}
// LimitReadCloser returns a new reader wraps r in an io.LimitedReader, but also
// exposes the Close() method.
func LimitReadCloser(r io.ReadCloser, n int64) *LimitedReadCloser {
return &LimitedReadCloser{Closer: r, LimitedReader: io.LimitedReader{R: r, N: n}}
}
// DefaultLoad implements Backend.Load using lower-level openReader func
func DefaultLoad(ctx context.Context, h restic.Handle, length int, offset int64,
openReader func(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error),
fn func(rd io.Reader) error) error {
rd, err := openReader(ctx, h, length, offset)
if err != nil {
return err
}
err = fn(rd)
if err != nil {
_ = rd.Close() // ignore secondary errors closing the reader
return err
}
return rd.Close()
}