Skip to content

Commit f77280f

Browse files
committed
fix errorlint errors
Error: archive.go:1138:8: comparing with == will fail on wrapped errors. Use errors.Is to check for a specific error (errorlint) if err == io.ErrClosedPipe { ^ Error: archive.go:1495:46: non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint) pipeW.CloseWithError(fmt.Errorf("%s: %s", err, errBuf.String())) ^ Error: changes_test.go:37:20: type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors (errorlint) if exiterr, ok := err.(*exec.ExitError); ok { ^ Error: copy_unix_test.go:358:5: comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error (errorlint) if err != ErrDirNotExists { ^ Error: copy_unix_test.go:367:5: comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error (errorlint) if err != ErrDirNotExists { ^ Error: diff.go:226:63: non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint) return true, fmt.Errorf("failed to decompress archive: %v", err) ^ Error: diff.go:235:70: non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint) return false, fmt.Errorf("failed to read next archive header: %v", err) ^ Error: utils_test.go:78:15: type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors (errorlint) if _, ok := err.(breakoutError); !ok { ^ Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent aa81301 commit f77280f

14 files changed

+46
-35
lines changed

archive.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func (r *bufferedReader) Read(p []byte) (int, error) {
260260
return 0, io.EOF
261261
}
262262
n, err := r.buf.Read(p)
263-
if err == io.EOF {
263+
if errors.Is(err, io.EOF) {
264264
r.buf.Reset(nil)
265265
bufioReader32KPool.Put(r.buf)
266266
r.buf = nil
@@ -279,7 +279,7 @@ func (r *bufferedReader) Peek(n int) ([]byte, error) {
279279
func DecompressStream(archive io.Reader) (io.ReadCloser, error) {
280280
buf := newBufferedReader(archive)
281281
bs, err := buf.Peek(10)
282-
if err != nil && err != io.EOF {
282+
if err != nil && !errors.Is(err, io.EOF) {
283283
// Note: we'll ignore any io.EOF error because there are some odd
284284
// cases where the layer.tar file will be empty (zero bytes) and
285285
// that results in an io.EOF from the Peek() call. So, in those
@@ -416,7 +416,7 @@ func ReplaceFileTarWrapper(inputTarStream io.ReadCloser, mods map[string]TarModi
416416
var originalHeader *tar.Header
417417
for {
418418
originalHeader, err = tarReader.Next()
419-
if err == io.EOF {
419+
if errors.Is(err, io.EOF) {
420420
break
421421
}
422422
if err != nil {
@@ -1135,7 +1135,7 @@ func (t *Tarballer) Do() {
11351135
if err := ta.addTarFile(filePath, relFilePath); err != nil {
11361136
log.G(context.TODO()).Errorf("Can't add file %s to tar: %s", filePath, err)
11371137
// if pipe is broken, stop writing tar stream to it
1138-
if err == io.ErrClosedPipe {
1138+
if errors.Is(err, io.ErrClosedPipe) {
11391139
return err
11401140
}
11411141
}
@@ -1155,7 +1155,7 @@ func Unpack(decompressedArchive io.Reader, dest string, options *TarOptions) err
11551155
loop:
11561156
for {
11571157
hdr, err := tr.Next()
1158-
if err == io.EOF {
1158+
if errors.Is(err, io.EOF) {
11591159
// end of tar archive
11601160
break
11611161
}
@@ -1492,9 +1492,9 @@ func cmdStream(cmd *exec.Cmd, input io.Reader) (io.ReadCloser, error) {
14921492
// Copy stdout to the returned pipe
14931493
go func() {
14941494
if err := cmd.Wait(); err != nil {
1495-
pipeW.CloseWithError(fmt.Errorf("%s: %s", err, errBuf.String()))
1495+
_ = pipeW.CloseWithError(fmt.Errorf("%w: %s", err, errBuf.String()))
14961496
} else {
1497-
pipeW.Close()
1497+
_ = pipeW.Close()
14981498
}
14991499
close(done)
15001500
}()

archive_linux_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package archive
33
import (
44
"archive/tar"
55
"bytes"
6+
"errors"
67
"io"
78
"os"
89
"path/filepath"
@@ -119,7 +120,7 @@ func TestOverlayTarUntar(t *testing.T) {
119120
rdr := tar.NewReader(bytes.NewReader(archive))
120121
for {
121122
h, err := rdr.Next()
122-
if err == io.EOF {
123+
if errors.Is(err, io.EOF) {
123124
break
124125
}
125126
assert.NilError(t, err)

archive_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"archive/tar"
55
"bytes"
66
"compress/gzip"
7+
"errors"
78
"fmt"
89
"io"
910
"io/fs"
@@ -766,7 +767,7 @@ func TestTarWithOptionsChownOptsAlwaysOverridesIdPair(t *testing.T) {
766767
defer reader.Close()
767768
for {
768769
hdr, err := tr.Next()
769-
if err == io.EOF {
770+
if errors.Is(err, io.EOF) {
770771
// end of tar archive
771772
break
772773
}
@@ -838,7 +839,7 @@ func TestUntarUstarGnuConflict(t *testing.T) {
838839
// Iterate through the files in the archive.
839840
for {
840841
hdr, err := tr.Next()
841-
if err == io.EOF {
842+
if errors.Is(err, io.EOF) {
842843
// end of tar archive
843844
break
844845
}

archive_unix_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package archive
55
import (
66
"archive/tar"
77
"bytes"
8+
"errors"
89
"fmt"
910
"io"
1011
"os"
@@ -259,7 +260,7 @@ func TestTarUntarWithXattr(t *testing.T) {
259260
rdr := tar.NewReader(tarball)
260261
for {
261262
h, err := rdr.Next()
262-
if err == io.EOF {
263+
if errors.Is(err, io.EOF) {
263264
break
264265
}
265266
assert.NilError(t, err)

changes_posix_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package archive
22

33
import (
44
"archive/tar"
5+
"errors"
56
"fmt"
67
"io"
78
"os"
@@ -112,7 +113,7 @@ func walkHeaders(r io.Reader) ([]tar.Header, error) {
112113
for {
113114
hdr, err := t.Next()
114115
if err != nil {
115-
if err == io.EOF {
116+
if errors.Is(err, io.EOF) {
116117
break
117118
}
118119
return headers, err

changes_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package archive
22

33
import (
4+
"errors"
45
"os"
56
"os/exec"
67
"path"
@@ -34,8 +35,9 @@ func copyDir(src, dst string) error {
3435
// Use robocopy instead. Note this isn't available in microsoft/nanoserver.
3536
// But it has gotchas. See https://weblogs.sqlteam.com/robv/archive/2010/02/17/61106.aspx
3637
err := exec.Command("robocopy", filepath.FromSlash(src), filepath.FromSlash(dst), "/SL", "/COPYALL", "/MIR").Run()
37-
if exiterr, ok := err.(*exec.ExitError); ok {
38-
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
38+
var exitError *exec.ExitError
39+
if errors.As(err, &exitError) {
40+
if status, ok := exitError.Sys().(syscall.WaitStatus); ok {
3941
if status.ExitStatus()&24 == 0 {
4042
return nil
4143
}

chrootarchive/archive_unix_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package chrootarchive
55
import (
66
gotar "archive/tar"
77
"bytes"
8+
"errors"
89
"io"
910
"os"
1011
"path"
@@ -152,7 +153,7 @@ func TestTarWithMaliciousSymlinks(t *testing.T) {
152153
func isDataInTar(t *testing.T, tr *gotar.Reader, compare []byte, maxBytes int64) bool {
153154
for {
154155
h, err := tr.Next()
155-
if err == io.EOF {
156+
if errors.Is(err, io.EOF) {
156157
break
157158
}
158159
assert.NilError(t, err)

chrootarchive/diff_windows.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func applyLayerHandler(dest string, layer io.Reader, options *archive.TarOptions
2626
dest = addLongPathPrefix(filepath.Clean(dest))
2727
s, err := archive.UnpackLayer(dest, layer, nil)
2828
if err != nil {
29-
return 0, fmt.Errorf("ApplyLayer %s failed UnpackLayer to %s: %s", layer, dest, err)
29+
return 0, fmt.Errorf("ApplyLayer %s failed UnpackLayer to %s: %w", layer, dest, err)
3030
}
3131

3232
return s, nil

copy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ func RebaseArchiveEntries(srcContent io.Reader, oldBase, newBase string) io.Read
335335

336336
for {
337337
hdr, err := srcTar.Next()
338-
if err == io.EOF {
338+
if errors.Is(err, io.EOF) {
339339
// Signals end of archive.
340340
rebasedTar.Close()
341341
w.Close()

copy_unix_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"crypto/sha256"
1010
"encoding/hex"
11+
"errors"
1112
"fmt"
1213
"io"
1314
"os"
@@ -355,7 +356,7 @@ func TestCopyCaseB(t *testing.T) {
355356
t.Fatal("expected ErrDirNotExists error, but got nil instead")
356357
}
357358

358-
if err != ErrDirNotExists {
359+
if !errors.Is(err, ErrDirNotExists) {
359360
t.Fatalf("expected ErrDirNotExists error, but got %T: %s", err, err)
360361
}
361362

@@ -364,7 +365,7 @@ func TestCopyCaseB(t *testing.T) {
364365
if err = testCopyHelperFSym(t, symlinkPath, dstDir); err == nil {
365366
t.Fatal("expected ErrDirNotExists error, but got nil instead")
366367
}
367-
if err != ErrDirNotExists {
368+
if !errors.Is(err, ErrDirNotExists) {
368369
t.Fatalf("expected ErrDirNotExists error, but got %T: %s", err, err)
369370
}
370371
}
@@ -647,7 +648,7 @@ func TestCopyCaseF(t *testing.T) {
647648
t.Fatal("expected ErrCannotCopyDir error, but got nil instead")
648649
}
649650

650-
if err != ErrCannotCopyDir {
651+
if !errors.Is(err, ErrCannotCopyDir) {
651652
t.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
652653
}
653654

@@ -656,7 +657,7 @@ func TestCopyCaseF(t *testing.T) {
656657
t.Fatal("expected ErrCannotCopyDir error, but got nil instead")
657658
}
658659

659-
if err != ErrCannotCopyDir {
660+
if !errors.Is(err, ErrCannotCopyDir) {
660661
t.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
661662
}
662663
}
@@ -871,7 +872,7 @@ func TestCopyCaseI(t *testing.T) {
871872
t.Fatal("expected ErrCannotCopyDir error, but got nil instead")
872873
}
873874

874-
if err != ErrCannotCopyDir {
875+
if !errors.Is(err, ErrCannotCopyDir) {
875876
t.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
876877
}
877878

@@ -880,7 +881,7 @@ func TestCopyCaseI(t *testing.T) {
880881
t.Fatal("expected ErrCannotCopyDir error, but got nil instead")
881882
}
882883

883-
if err != ErrCannotCopyDir {
884+
if !errors.Is(err, ErrCannotCopyDir) {
884885
t.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
885886
}
886887
}

diff.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package archive
33
import (
44
"archive/tar"
55
"context"
6+
"errors"
67
"fmt"
78
"io"
89
"os"
@@ -35,7 +36,7 @@ func UnpackLayer(dest string, layer io.Reader, options *TarOptions) (size int64,
3536
// Iterate through the files in the archive.
3637
for {
3738
hdr, err := tr.Next()
38-
if err == io.EOF {
39+
if errors.Is(err, io.EOF) {
3940
// end of tar archive
4041
break
4142
}
@@ -223,16 +224,16 @@ func ApplyUncompressedLayer(dest string, layer io.Reader, options *TarOptions) (
223224
func IsEmpty(rd io.Reader) (bool, error) {
224225
decompRd, err := DecompressStream(rd)
225226
if err != nil {
226-
return true, fmt.Errorf("failed to decompress archive: %v", err)
227+
return true, fmt.Errorf("failed to decompress archive: %w", err)
227228
}
228229
defer decompRd.Close()
229230

230231
tarReader := tar.NewReader(decompRd)
231232
if _, err := tarReader.Next(); err != nil {
232-
if err == io.EOF {
233+
if errors.Is(err, io.EOF) {
233234
return true, nil
234235
}
235-
return false, fmt.Errorf("failed to read next archive header: %v", err)
236+
return false, fmt.Errorf("failed to read next archive header: %w", err)
236237
}
237238

238239
return false, nil

example_changes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func main() {
7373
defer a.Close()
7474

7575
i, err := io.Copy(os.Stdout, a)
76-
if err != nil && err != io.EOF {
76+
if err != nil && !errors.Is(err, io.EOF) {
7777
log.Fatal(err)
7878
}
7979
fmt.Fprintf(os.Stderr, "wrote archive of %d bytes", i)

utils_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package archive
33
import (
44
"archive/tar"
55
"bytes"
6+
"errors"
67
"fmt"
78
"io"
89
"os"
@@ -75,21 +76,21 @@ func testBreakout(untarFn string, tmpdir string, headers []*tar.Header) error {
7576
return fmt.Errorf("could not find untar function %q in testUntarFns", untarFn)
7677
}
7778
if err := untar(dest, reader); err != nil {
78-
if _, ok := err.(breakoutError); !ok {
79+
if !errors.As(err, new(breakoutError)) {
7980
// If untar returns an error unrelated to an archive breakout,
8081
// then consider this an unexpected error and abort.
8182
return err
8283
}
8384
// Here, untar detected the breakout.
8485
// Let's move on verifying that indeed there was no breakout.
85-
fmt.Printf("breakoutError: %v\n", err)
86+
fmt.Println("breakoutError:", err)
8687
}
8788

8889
// Check victim folder
8990
f, err := os.Open(victim)
9091
if err != nil {
9192
// codepath taken if victim folder was removed
92-
return fmt.Errorf("archive breakout: error reading %q: %v", victim, err)
93+
return fmt.Errorf("archive breakout: error reading %q: %w", victim, err)
9394
}
9495
defer f.Close()
9596

@@ -102,7 +103,7 @@ func testBreakout(untarFn string, tmpdir string, headers []*tar.Header) error {
102103
names, err := f.Readdirnames(2)
103104
if err != nil {
104105
// codepath taken if victim is not a folder
105-
return fmt.Errorf("archive breakout: error reading directory content of %q: %v", victim, err)
106+
return fmt.Errorf("archive breakout: error reading directory content of %q: %w", victim, err)
106107
}
107108
for _, name := range names {
108109
if name != "hello" {
@@ -115,7 +116,7 @@ func testBreakout(untarFn string, tmpdir string, headers []*tar.Header) error {
115116
f, err = os.Open(hello)
116117
if err != nil {
117118
// codepath taken if read permissions were removed
118-
return fmt.Errorf("archive breakout: could not lstat %q: %v", hello, err)
119+
return fmt.Errorf("archive breakout: could not lstat %q: %w", hello, err)
119120
}
120121
defer f.Close()
121122
b, err := io.ReadAll(f)

wrap_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package archive
33
import (
44
"archive/tar"
55
"bytes"
6+
"errors"
67
"io"
78
"testing"
89

@@ -25,7 +26,7 @@ func TestGenerateEmptyFile(t *testing.T) {
2526
i := 0
2627
for {
2728
hdr, err := tr.Next()
28-
if err == io.EOF {
29+
if errors.Is(err, io.EOF) {
2930
break
3031
}
3132
assert.NilError(t, err)
@@ -66,7 +67,7 @@ func TestGenerateWithContent(t *testing.T) {
6667
i := 0
6768
for {
6869
hdr, err := tr.Next()
69-
if err == io.EOF {
70+
if errors.Is(err, io.EOF) {
7071
break
7172
}
7273
assert.NilError(t, err)

0 commit comments

Comments
 (0)