forked from TrueCloudLab/distribution
1d33874951
Go 1.13 and up enforce import paths to be versioned if a project contains a go.mod and has released v2 or up. The current v2.x branches (and releases) do not yet have a go.mod, and therefore are still allowed to be imported with a non-versioned import path (go modules add a `+incompatible` annotation in that case). However, now that this project has a `go.mod` file, incompatible import paths will not be accepted by go modules, and attempting to use code from this repository will fail. This patch uses `v3` for the import-paths (not `v2`), because changing import paths itself is a breaking change, which means that the next release should increment the "major" version to comply with SemVer (as go modules dictate). Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"io/ioutil"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver"
|
|
)
|
|
|
|
const (
|
|
maxBlobGetSize = 4 << 20
|
|
)
|
|
|
|
func getContent(ctx context.Context, driver driver.StorageDriver, p string) ([]byte, error) {
|
|
r, err := driver.Reader(ctx, p, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return readAllLimited(r, maxBlobGetSize)
|
|
}
|
|
|
|
func readAllLimited(r io.Reader, limit int64) ([]byte, error) {
|
|
r = limitReader(r, limit)
|
|
return ioutil.ReadAll(r)
|
|
}
|
|
|
|
// limitReader returns a new reader limited to n bytes. Unlike io.LimitReader,
|
|
// this returns an error when the limit reached.
|
|
func limitReader(r io.Reader, n int64) io.Reader {
|
|
return &limitedReader{r: r, n: n}
|
|
}
|
|
|
|
// limitedReader implements a reader that errors when the limit is reached.
|
|
//
|
|
// Partially cribbed from net/http.MaxBytesReader.
|
|
type limitedReader struct {
|
|
r io.Reader // underlying reader
|
|
n int64 // max bytes remaining
|
|
err error // sticky error
|
|
}
|
|
|
|
func (l *limitedReader) Read(p []byte) (n int, err error) {
|
|
if l.err != nil {
|
|
return 0, l.err
|
|
}
|
|
if len(p) == 0 {
|
|
return 0, nil
|
|
}
|
|
// If they asked for a 32KB byte read but only 5 bytes are
|
|
// remaining, no need to read 32KB. 6 bytes will answer the
|
|
// question of the whether we hit the limit or go past it.
|
|
if int64(len(p)) > l.n+1 {
|
|
p = p[:l.n+1]
|
|
}
|
|
n, err = l.r.Read(p)
|
|
|
|
if int64(n) <= l.n {
|
|
l.n -= int64(n)
|
|
l.err = err
|
|
return n, err
|
|
}
|
|
|
|
n = int(l.n)
|
|
l.n = 0
|
|
|
|
l.err = errors.New("storage: read exceeds limit")
|
|
return n, l.err
|
|
}
|