Skip to content

Commit 2cb920d

Browse files
samifruit514simar7
authored andcommitted
Using bufio reader for Stdin, otherwise the first 3 bytes are consumed and file gets "corrupted" (stdin is not seekable?)
1 parent 9bf16ae commit 2cb920d

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

analyzer/analyzer.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package analyzer
22

33
import (
4+
"bufio"
45
"compress/gzip"
56
"context"
67
"io"
@@ -136,12 +137,14 @@ func (ac Config) Analyze(ctx context.Context, imageName string, opts ...types.Do
136137

137138
func (ac Config) AnalyzeFile(ctx context.Context, f *os.File) (fileMap extractor.FileMap, err error) {
138139
var r io.Reader
139-
r = f
140-
if utils.IsGzip(f) {
141-
r, err = gzip.NewReader(f)
140+
br := bufio.NewReader(f)
141+
if utils.IsGzip(br) {
142+
r, err = gzip.NewReader(br)
142143
if err != nil {
143144
return nil, xerrors.Errorf("failed to open gzip: %w", err)
144145
}
146+
} else {
147+
r = br
145148
}
146149
fileMap, err = ac.Extractor.ExtractFromFile(ctx, r, RequiredFilenames())
147150
if err != nil {

utils/utils.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package utils
22

33
import (
4+
"bufio"
45
"fmt"
5-
"io"
66
"os"
77
"os/exec"
88
"path/filepath"
@@ -37,11 +37,10 @@ func IsCommandAvailable(name string) bool {
3737
return true
3838
}
3939

40-
func IsGzip(f *os.File) bool {
40+
func IsGzip(f *bufio.Reader) bool {
4141
buf := make([]byte, 3)
42-
n, _ := f.Read(buf)
43-
defer f.Seek(0, io.SeekStart)
44-
if n < 3 {
42+
buf, err := f.Peek(3)
43+
if err != nil {
4544
return false
4645
}
4746
return buf[0] == 0x1F && buf[1] == 0x8B && buf[2] == 0x8

utils/utils_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package utils
22

33
import (
4+
"bufio"
45
"os"
56
"testing"
67
)
@@ -23,7 +24,7 @@ func TestIsGzip(t *testing.T) {
2324
t.Fatalf("unknown error: %s", err)
2425
}
2526

26-
got := IsGzip(f)
27+
got := IsGzip(bufio.NewReader(f))
2728
if got != tt.want {
2829
t.Errorf("got %t, want %t", got, tt.want)
2930
}

0 commit comments

Comments
 (0)