Skip to content

Commit f3a5807

Browse files
committed
fix
Signed-off-by: Sylvia Lei <lixlei@microsoft.com>
1 parent 596455f commit f3a5807

File tree

6 files changed

+29
-23
lines changed

6 files changed

+29
-23
lines changed

content.go

+15-9
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func resolve(ctx context.Context, target ReadOnlyTarget, proxy *cas.Proxy, refer
107107
}
108108

109109
// DefaultFetchOptions provides the default FetchOptions.
110-
var DefaultFetchOptions = FetchOptions{}
110+
var DefaultFetchOptions FetchOptions
111111

112112
// FetchOptions contains parameters for oras.Fetch.
113113
type FetchOptions struct {
@@ -150,32 +150,38 @@ func Fetch(ctx context.Context, target ReadOnlyTarget, reference string, opts Fe
150150

151151
// DefaultFetchBytesOptions provides the default FetchBytesOptions.
152152
var DefaultFetchBytesOptions = FetchBytesOptions{
153-
SizeLimit: int64(1 << 22), // 4 MiB
153+
MaxBytes: defaultMaxBytes,
154154
}
155155

156+
// defaultMaxBytes is the default value of MaxBytes.
157+
const defaultMaxBytes int64 = 4 * 1024 * 1024 // 4 MiB
158+
156159
// FetchBytesOptions contains parameters for oras.FetchBytes.
157160
type FetchBytesOptions struct {
158161
// FetchOptions contains parameters for fetching content.
159162
FetchOptions
160-
// SizeLimit limits the max size of the fetched content.
161-
// If SizeLimit is not specified, or the specified value is less than or
162-
// equal to 0, it will be considered as infinity.
163-
SizeLimit int64
163+
// MaxBytes limits the maximum size of the fetched content bytes.
164+
// If less than or equal to 0, a default (currently 4 MiB) is used.
165+
MaxBytes int64
164166
}
165167

166168
// FetchBytes fetches the content bytes identified by the reference.
167169
func FetchBytes(ctx context.Context, target ReadOnlyTarget, reference string, opts FetchBytesOptions) (ocispec.Descriptor, []byte, error) {
170+
if opts.MaxBytes <= 0 {
171+
opts.MaxBytes = defaultMaxBytes
172+
}
173+
168174
desc, rc, err := Fetch(ctx, target, reference, opts.FetchOptions)
169175
if err != nil {
170176
return ocispec.Descriptor{}, nil, err
171177
}
172178
defer rc.Close()
173179

174-
if opts.SizeLimit > 0 && desc.Size > opts.SizeLimit {
180+
if desc.Size > opts.MaxBytes {
175181
return ocispec.Descriptor{}, nil, fmt.Errorf(
176-
"content size %v exceeds max size limit %v: %w",
182+
"content size %v exceeds MaxBytes %v: %w",
177183
desc.Size,
178-
opts.SizeLimit,
184+
opts.MaxBytes,
179185
errdef.ErrSizeExceedsLimit)
180186
}
181187
bytes, err := content.ReadAll(rc, desc)

content_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -966,8 +966,8 @@ func TestFetchBytes_Memory(t *testing.T) {
966966
t.Fatalf("oras.FetchBytes() error = %v, wantErr %v", err, errdef.ErrNotFound)
967967
}
968968

969-
// test FetchBytes with SizeLimit = 1
970-
_, _, err = oras.FetchBytes(ctx, target, manifestTag, oras.FetchBytesOptions{SizeLimit: 1})
969+
// test FetchBytes with MaxBytes = 1
970+
_, _, err = oras.FetchBytes(ctx, target, manifestTag, oras.FetchBytesOptions{MaxBytes: 1})
971971
if !errors.Is(err, errdef.ErrSizeExceedsLimit) {
972972
t.Fatalf("oras.FetchBytes() error = %v, wantErr %v", err, errdef.ErrSizeExceedsLimit)
973973
}
@@ -994,7 +994,7 @@ func TestFetchBytes_Memory(t *testing.T) {
994994
t.Errorf("oras.FetchBytes() = %v, want %v", gotBytes, blobs[3])
995995
}
996996

997-
// test FetchBytes with TargetPlatform and SizeLimit = 1
997+
// test FetchBytes with TargetPlatform and MaxBytes = 1
998998
// should return size exceed error
999999
opts = oras.FetchBytesOptions{
10001000
FetchOptions: oras.FetchOptions{
@@ -1005,7 +1005,7 @@ func TestFetchBytes_Memory(t *testing.T) {
10051005
},
10061006
},
10071007
},
1008-
SizeLimit: 1,
1008+
MaxBytes: 1,
10091009
}
10101010
_, _, err = oras.FetchBytes(ctx, target, manifestTag, opts)
10111011
if !errors.Is(err, errdef.ErrSizeExceedsLimit) {
@@ -1192,8 +1192,8 @@ func TestFetchBytes_Repository(t *testing.T) {
11921192
t.Errorf("oras.FetchBytes() = %v, want %v", gotBytes, blob)
11931193
}
11941194

1195-
// test FetchBytes with SizeLimit = 1
1196-
_, _, err = oras.FetchBytes(ctx, repo, ref, oras.FetchBytesOptions{SizeLimit: 1})
1195+
// test FetchBytes with MaxBytes = 1
1196+
_, _, err = oras.FetchBytes(ctx, repo, ref, oras.FetchBytesOptions{MaxBytes: 1})
11971197
if !errors.Is(err, errdef.ErrSizeExceedsLimit) {
11981198
t.Fatalf("oras.FetchBytes() error = %v, wantErr %v", err, errdef.ErrSizeExceedsLimit)
11991199
}
@@ -1227,7 +1227,7 @@ func TestFetchBytes_Repository(t *testing.T) {
12271227
t.Errorf("oras.FetchBytes() = %v, want %v", gotBytes, manifest)
12281228
}
12291229

1230-
// test FetchBytes with TargetPlatform and SizeLimit = 1
1230+
// test FetchBytes with TargetPlatform and MaxBytes = 1
12311231
// should return size exceed error
12321232
opts = oras.FetchBytesOptions{
12331233
FetchOptions: oras.FetchOptions{
@@ -1238,7 +1238,7 @@ func TestFetchBytes_Repository(t *testing.T) {
12381238
},
12391239
},
12401240
},
1241-
SizeLimit: 1,
1241+
MaxBytes: 1,
12421242
}
12431243
_, _, err = oras.FetchBytes(ctx, repo, ref, opts)
12441244
if !errors.Is(err, errdef.ErrSizeExceedsLimit) {

copy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (opts *CopyOptions) WithTargetPlatform(p *ocispec.Platform) {
7777
// CopyGraphOptions contains parameters for oras.CopyGraph.
7878
type CopyGraphOptions struct {
7979
// Concurrency limits the maximum number of concurrent copy tasks.
80-
// If Concurrency is not specified, or the specified value is less
80+
// If Concurrency is not specified, or the specified value is less than
8181
// or equal to 0, the concurrency limit will be considered as infinity.
8282
Concurrency int64
8383
// PreCopy handles the current descriptor before copying it.

extendedcopy.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ type ExtendedCopyGraphOptions struct {
5151
CopyGraphOptions
5252
// Depth limits the maximum depth of the directed acyclic graph (DAG) that
5353
// will be extended-copied.
54-
// If Depth is no specified, or the specified value is less or equal than 0,
55-
// the depth limit will be considered as infinity.
54+
// If Depth is no specified, or the specified value is less than or
55+
// equal to 0, the depth limit will be considered as infinity.
5656
Depth int
5757
// FindPredecessors finds the predecessors of the current node.
5858
// If FindPredecessors is nil, src.Predecessors will be adapted and used.

registry/remote/repository.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ type Repository struct {
9898
// MaxMetadataBytes specifies a limit on how many response bytes are allowed
9999
// in the server's response to the metadata APIs, such as catalog list, tag
100100
// list, and referrers list.
101-
// If zero, a default (currently 4MiB) is used.
101+
// If less than or equal to zero, a default (currently 4MiB) is used.
102102
MaxMetadataBytes int64
103103
}
104104

registry/remote/utils.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ func parseLink(resp *http.Response) (string, error) {
5858
}
5959

6060
// limitReader returns a Reader that reads from r but stops with EOF after n
61-
// bytes. If n is zero, defaultMaxMetadataBytes is used.
61+
// bytes. If n is less than or equal to zero, defaultMaxMetadataBytes is used.
6262
func limitReader(r io.Reader, n int64) io.Reader {
63-
if n == 0 {
63+
if n <= 0 {
6464
n = defaultMaxMetadataBytes
6565
}
6666
return io.LimitReader(r, n)

0 commit comments

Comments
 (0)