tstor/pkg/ctxio/readerat.go
royalcat 5591f145a9
All checks were successful
docker / build-docker (linux/amd64) (push) Successful in 2m17s
docker / build-docker (linux/386) (push) Successful in 2m22s
docker / build-docker (linux/arm64/v8) (push) Successful in 8m12s
docker / build-docker (linux/arm64) (push) Successful in 8m22s
docker / build-docker (linux/arm/v7) (push) Successful in 8m48s
no cache archive reader
2024-04-17 11:36:14 +03:00

47 lines
1,002 B
Go

package ctxio
import (
"context"
"sync"
)
type ReaderReaderAtWrapper struct {
mu sync.Mutex
rat ReaderAt
offset int64
}
func NewReaderReaderAtWrapper(rat ReaderAt) *ReaderReaderAtWrapper {
return &ReaderReaderAtWrapper{
rat: rat,
}
}
var _ Reader = (*ReaderReaderAtWrapper)(nil)
var _ ReaderAt = (*ReaderReaderAtWrapper)(nil)
var _ Closer = (*ReaderReaderAtWrapper)(nil)
// Read implements Reader.
func (r *ReaderReaderAtWrapper) Read(ctx context.Context, p []byte) (n int, err error) {
r.mu.Lock()
defer r.mu.Unlock()
n, err = r.rat.ReadAt(ctx, p, r.offset)
r.offset += int64(n)
return n, err
}
// ReadAt implements ReaderAt.
func (r *ReaderReaderAtWrapper) ReadAt(ctx context.Context, p []byte, off int64) (n int, err error) {
return r.rat.ReadAt(ctx, p, off)
}
// Close implements Closer.
func (r *ReaderReaderAtWrapper) Close(ctx context.Context) (err error) {
if c, ok := r.rat.(Closer); ok {
err = c.Close(ctx)
if err != nil {
return err
}
}
return nil
}