1
0
mirror of https://github.com/restic/restic.git synced 2024-06-27 07:55:08 +02:00
restic/internal/restic/readerat.go

42 lines
974 B
Go
Raw Normal View History

2016-08-31 22:39:36 +02:00
package restic
2016-08-25 21:49:00 +02:00
import (
2017-06-03 17:39:57 +02:00
"context"
2016-08-25 21:49:00 +02:00
"io"
2017-07-23 14:21:03 +02:00
"github.com/restic/restic/internal/debug"
2017-09-24 23:10:26 +02:00
"github.com/restic/restic/internal/errors"
2016-08-25 21:49:00 +02:00
)
type backendReaderAt struct {
be Backend
h Handle
}
func (brd backendReaderAt) ReadAt(p []byte, offset int64) (n int, err error) {
2017-06-04 11:16:55 +02:00
return ReadAt(context.TODO(), brd.be, brd.h, offset, p)
2016-08-25 21:49:00 +02:00
}
// ReaderAt returns an io.ReaderAt for a file in the backend.
func ReaderAt(be Backend, h Handle) io.ReaderAt {
return backendReaderAt{be: be, h: h}
}
// ReadAt reads from the backend handle h at the given position.
2017-06-04 11:16:55 +02:00
func ReadAt(ctx context.Context, be Backend, h Handle, offset int64, p []byte) (n int, err error) {
2017-01-24 09:03:33 +01:00
debug.Log("ReadAt(%v) at %v, len %v", h, offset, len(p))
err = be.Load(ctx, h, len(p), offset, func(rd io.Reader) (ierr error) {
n, ierr = io.ReadFull(rd, p)
return ierr
})
if err != nil {
return 0, err
}
2017-01-24 09:03:33 +01:00
debug.Log("ReadAt(%v) ReadFull returned %v bytes", h, n)
2017-09-24 23:10:26 +02:00
return n, errors.Wrapf(err, "ReadFull(%v)", h)
}