Skip to content

Commit d3a439d

Browse files
vpasdfcopybara-github
authored andcommitted
Create units package to make size values more readable and less error prone
PiperOrigin-RevId: 624166373
1 parent ca4b155 commit d3a439d

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

extractor/internal/units/units.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package units provides constants for common units.
16+
package units
17+
18+
const (
19+
// KiB is a Kibibyte
20+
KiB = int64(1024)
21+
// MiB is a Mebibyte
22+
MiB = 1024 * KiB
23+
// GiB is a Gibibyte
24+
GiB = 1024 * MiB
25+
// TiB is a Tebibyte
26+
TiB = 1024 * GiB
27+
// PiB is a Pebibyte
28+
PiB = 1024 * TiB
29+
// EiB is a Exbibyte
30+
EiB = 1024 * PiB
31+
)

extractor/language/java/archive/extractor.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
"go.uber.org/multierr"
3232
"github.com/google/osv-scalibr/extractor"
33+
"github.com/google/osv-scalibr/extractor/internal/units"
3334
"github.com/google/osv-scalibr/log"
3435
"github.com/google/osv-scalibr/purl"
3536
)
@@ -43,7 +44,7 @@ const (
4344
defaultMaxZipDepth = 16
4445
// defaultMaxZipBytes in the maximum number of bytes recursively read from an archive file.
4546
// If this limit is reached, the default extractor is halted and results so far are returned.
46-
defaultMaxZipBytes = 4 << 30 // 4GiB
47+
defaultMaxZipBytes = 4 * units.GiB
4748
// defaultMinZipBytes is slightly larger than an empty zip file which is 22 bytes.
4849
// https://en.wikipedia.org/wiki/ZIP_(file_format)#:~:text=Viewed%20as%20an%20ASCII%20string,file%20are%20usually%20%22PK%22.
4950
defaultMinZipBytes = 30
@@ -60,7 +61,7 @@ type Config struct {
6061
MaxZipDepth int
6162
// MaxOpenedBytes is the maximum number of bytes recursively read from an archive file.
6263
// If this limit is reached, extraction is halted and results so far are returned.
63-
MaxOpenedBytes int
64+
MaxOpenedBytes int64
6465
// MinZipBytes is use to ignore empty zip files during extraction.
6566
// Zip files smaller than minZipBytes are ignored.
6667
MinZipBytes int
@@ -73,7 +74,7 @@ type Config struct {
7374
// Extractor extracts Java packages from archive files.
7475
type Extractor struct {
7576
maxZipDepth int
76-
maxOpenedBytes int
77+
maxOpenedBytes int64
7778
minZipBytes int
7879
extractFromFilename bool
7980
hashJars bool
@@ -128,12 +129,12 @@ func (e Extractor) Extract(ctx context.Context, input *extractor.ScanInput) ([]*
128129
//
129130
// It returns early with an error if max depth or max opened bytes is reached.
130131
// Extracted packages are returned even if an error has occurred.
131-
func (e Extractor) extractWithMax(ctx context.Context, input *extractor.ScanInput, depth, openedBytes int) ([]*extractor.Inventory, error) {
132+
func (e Extractor) extractWithMax(ctx context.Context, input *extractor.ScanInput, depth int, openedBytes int64) ([]*extractor.Inventory, error) {
132133
// Return early if any max/min thresholds are hit.
133134
if depth > e.maxZipDepth {
134135
return nil, fmt.Errorf("%s reached max zip depth %d at %q", e.Name(), depth, input.Path)
135136
}
136-
if oBytes := openedBytes + int(input.Info.Size()); oBytes > e.maxOpenedBytes {
137+
if oBytes := openedBytes + input.Info.Size(); oBytes > e.maxOpenedBytes {
137138
return nil, fmt.Errorf("%s reached max opened bytes of %d at %q", e.Name(), oBytes, input.Path)
138139
}
139140
if int(input.Info.Size()) < e.minZipBytes {
@@ -151,7 +152,7 @@ func (e Extractor) extractWithMax(ctx context.Context, input *extractor.ScanInpu
151152
if err != nil {
152153
return nil, fmt.Errorf("%s failed to read file at %q: %w", e.Name(), input.Path, err)
153154
}
154-
openedBytes += len(b)
155+
openedBytes += int64(len(b))
155156
// Check size again in case input.Info.Size() was not accurate. Return early if hit max.
156157
if openedBytes > e.maxOpenedBytes {
157158
return nil, fmt.Errorf("%s reached max opened bytes of %d at %q", e.Name(), openedBytes, input.Path)

extractor/language/javascript/packagejson/extractor.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"strings"
2626

2727
"github.com/google/osv-scalibr/extractor"
28+
"github.com/google/osv-scalibr/extractor/internal/units"
2829
"github.com/google/osv-scalibr/log"
2930
"github.com/google/osv-scalibr/purl"
3031
)
@@ -35,7 +36,7 @@ const (
3536

3637
// defaultMaxJSONSize is the maximum file size an extractor will unmarshal.
3738
// If Extract gets a bigger file, it will return an error.
38-
defaultMaxJSONSize = int64(100) << 20 // 100MiB
39+
defaultMaxJSONSize = 100 * units.MiB
3940
)
4041

4142
type packageJSON struct {

extractor/language/python/wheelegg/extractor.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"strings"
3030

3131
"github.com/google/osv-scalibr/extractor"
32+
"github.com/google/osv-scalibr/extractor/internal/units"
3233
"github.com/google/osv-scalibr/purl"
3334
)
3435

@@ -38,7 +39,7 @@ const (
3839

3940
// defaultMaxFileSize is the maximum file size an extractor will unmarshal.
4041
// If Extract gets a bigger file, it will return an error.
41-
defaultMaxFileSize = int64(100) << 20 // 100MiB
42+
defaultMaxFileSize = 100 * units.MiB
4243
)
4344

4445
// Extractor extracts python packages from wheel/egg files.

extractor/os/dpkg/extractor.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"strings"
2828

2929
"github.com/google/osv-scalibr/extractor"
30+
"github.com/google/osv-scalibr/extractor/internal/units"
3031
"github.com/google/osv-scalibr/extractor/os/osrelease"
3132
"github.com/google/osv-scalibr/log"
3233
"github.com/google/osv-scalibr/purl"
@@ -38,7 +39,7 @@ const (
3839

3940
// defaultMaxFileSize is the maximum file size an extractor will unmarshal.
4041
// If Extract gets a bigger file, it will return an error.
41-
defaultMaxFileSize = int64(100) << 20 // 100MiB
42+
defaultMaxFileSize = 100 * units.MiB
4243
)
4344

4445
// Config is the configuration for the Extractor.

0 commit comments

Comments
 (0)