Skip to content

Commit d4d21a8

Browse files
authored
Merge pull request #2 from thaJeztah/fix_linting
fix linting issues
2 parents a36cd06 + 9aea114 commit d4d21a8

23 files changed

+186
-186
lines changed

archive.go

Lines changed: 16 additions & 15 deletions
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
@@ -344,7 +344,7 @@ func DecompressStream(archive io.Reader) (io.ReadCloser, error) {
344344
},
345345
}, nil
346346
default:
347-
return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
347+
return nil, fmt.Errorf("unsupported compression format: %s", (&compression).Extension())
348348
}
349349
}
350350

@@ -364,9 +364,9 @@ func CompressStream(dest io.Writer, compression Compression) (io.WriteCloser, er
364364
case Bzip2, Xz:
365365
// archive/bzip2 does not support writing, and there is no xz support at all
366366
// However, this is not a problem as docker only currently generates gzipped tars
367-
return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
367+
return nil, fmt.Errorf("unsupported compression format: %s", (&compression).Extension())
368368
default:
369-
return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
369+
return nil, fmt.Errorf("unsupported compression format: %s", (&compression).Extension())
370370
}
371371
}
372372

@@ -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 {
@@ -768,7 +768,7 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, o
768768
case tar.TypeDir:
769769
// Create directory unless it exists as a directory already.
770770
// In that case we just want to merge the two
771-
if fi, err := os.Lstat(path); !(err == nil && fi.IsDir()) {
771+
if fi, err := os.Lstat(path); err != nil || !fi.IsDir() {
772772
if err := os.Mkdir(path, hdrInfo.Mode()); err != nil {
773773
return err
774774
}
@@ -1031,7 +1031,8 @@ func (t *Tarballer) Do() {
10311031
)
10321032

10331033
walkRoot := getWalkRoot(t.srcPath, include)
1034-
filepath.WalkDir(walkRoot, func(filePath string, f os.DirEntry, err error) error {
1034+
// TODO(thaJeztah): should this error be handled?
1035+
_ = filepath.WalkDir(walkRoot, func(filePath string, f os.DirEntry, err error) error {
10351036
if err != nil {
10361037
log.G(context.TODO()).Errorf("Tar: Can't stat file %s to tar: %s", t.srcPath, err)
10371038
return nil
@@ -1135,7 +1136,7 @@ func (t *Tarballer) Do() {
11351136
if err := ta.addTarFile(filePath, relFilePath); err != nil {
11361137
log.G(context.TODO()).Errorf("Can't add file %s to tar: %s", filePath, err)
11371138
// if pipe is broken, stop writing tar stream to it
1138-
if err == io.ErrClosedPipe {
1139+
if errors.Is(err, io.ErrClosedPipe) {
11391140
return err
11401141
}
11411142
}
@@ -1155,7 +1156,7 @@ func Unpack(decompressedArchive io.Reader, dest string, options *TarOptions) err
11551156
loop:
11561157
for {
11571158
hdr, err := tr.Next()
1158-
if err == io.EOF {
1159+
if errors.Is(err, io.EOF) {
11591160
// end of tar archive
11601161
break
11611162
}
@@ -1217,7 +1218,7 @@ loop:
12171218
continue
12181219
}
12191220

1220-
if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
1221+
if !fi.IsDir() || hdr.Typeflag != tar.TypeDir {
12211222
if err := os.RemoveAll(path); err != nil {
12221223
return err
12231224
}
@@ -1309,7 +1310,7 @@ func UntarUncompressed(tarArchive io.Reader, dest string, options *TarOptions) e
13091310
// Handler for teasing out the automatic decompression
13101311
func untarHandler(tarArchive io.Reader, dest string, options *TarOptions, decompress bool) error {
13111312
if tarArchive == nil {
1312-
return fmt.Errorf("Empty archive")
1313+
return errors.New("empty archive")
13131314
}
13141315
dest = filepath.Clean(dest)
13151316
if options == nil {
@@ -1393,7 +1394,7 @@ func (archiver *Archiver) CopyFileWithTar(src, dst string) (err error) {
13931394
}
13941395

13951396
if srcSt.IsDir() {
1396-
return fmt.Errorf("Can't copy a directory")
1397+
return errors.New("can't copy a directory")
13971398
}
13981399

13991400
// Clean up the trailing slash. This must be done in an operating
@@ -1492,9 +1493,9 @@ func cmdStream(cmd *exec.Cmd, input io.Reader) (io.ReadCloser, error) {
14921493
// Copy stdout to the returned pipe
14931494
go func() {
14941495
if err := cmd.Wait(); err != nil {
1495-
pipeW.CloseWithError(fmt.Errorf("%s: %s", err, errBuf.String()))
1496+
_ = pipeW.CloseWithError(fmt.Errorf("%w: %s", err, errBuf.String()))
14961497
} else {
1497-
pipeW.Close()
1498+
_ = pipeW.Close()
14981499
}
14991500
close(done)
15001501
}()

archive_linux_test.go

Lines changed: 2 additions & 1 deletion
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

Lines changed: 27 additions & 32 deletions
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"
@@ -479,22 +480,20 @@ func TestCopyWithTarSrcFile(t *testing.T) {
479480
dest := filepath.Join(folder, "dest")
480481
srcFolder := filepath.Join(folder, "src")
481482
src := filepath.Join(folder, filepath.Join("src", "src"))
482-
err := os.MkdirAll(srcFolder, 0o740)
483-
if err != nil {
483+
if err := os.MkdirAll(srcFolder, 0o740); err != nil {
484484
t.Fatal(err)
485485
}
486-
err = os.MkdirAll(dest, 0o740)
487-
if err != nil {
486+
if err := os.MkdirAll(dest, 0o740); err != nil {
488487
t.Fatal(err)
489488
}
490-
os.WriteFile(src, []byte("content"), 0o777)
491-
err = defaultCopyWithTar(src, dest)
492-
if err != nil {
489+
if err := os.WriteFile(src, []byte("content"), 0o777); err != nil {
490+
t.Fatal(err)
491+
}
492+
if err := defaultCopyWithTar(src, dest); err != nil {
493493
t.Fatalf("archiver.CopyWithTar shouldn't throw an error, %s.", err)
494494
}
495-
_, err = os.Stat(dest)
496495
// FIXME Check the content
497-
if err != nil {
496+
if _, err := os.Stat(dest); err != nil {
498497
t.Fatalf("Destination file should be the same as the source.")
499498
}
500499
}
@@ -504,22 +503,20 @@ func TestCopyWithTarSrcFolder(t *testing.T) {
504503
folder := t.TempDir()
505504
dest := filepath.Join(folder, "dest")
506505
src := filepath.Join(folder, filepath.Join("src", "folder"))
507-
err := os.MkdirAll(src, 0o740)
508-
if err != nil {
506+
if err := os.MkdirAll(src, 0o740); err != nil {
509507
t.Fatal(err)
510508
}
511-
err = os.MkdirAll(dest, 0o740)
512-
if err != nil {
509+
if err := os.MkdirAll(dest, 0o740); err != nil {
513510
t.Fatal(err)
514511
}
515-
os.WriteFile(filepath.Join(src, "file"), []byte("content"), 0o777)
516-
err = defaultCopyWithTar(src, dest)
517-
if err != nil {
512+
if err := os.WriteFile(filepath.Join(src, "file"), []byte("content"), 0o777); err != nil {
513+
t.Fatal(err)
514+
}
515+
if err := defaultCopyWithTar(src, dest); err != nil {
518516
t.Fatalf("archiver.CopyWithTar shouldn't throw an error, %s.", err)
519517
}
520-
_, err = os.Stat(dest)
521518
// FIXME Check the content (the file inside)
522-
if err != nil {
519+
if _, err := os.Stat(dest); err != nil {
523520
t.Fatalf("Destination folder should contain the source file but did not.")
524521
}
525522
}
@@ -580,21 +577,19 @@ func TestCopyFileWithTarSrcFile(t *testing.T) {
580577
dest := filepath.Join(folder, "dest")
581578
srcFolder := filepath.Join(folder, "src")
582579
src := filepath.Join(folder, filepath.Join("src", "src"))
583-
err := os.MkdirAll(srcFolder, 0o740)
584-
if err != nil {
580+
if err := os.MkdirAll(srcFolder, 0o740); err != nil {
585581
t.Fatal(err)
586582
}
587-
err = os.MkdirAll(dest, 0o740)
588-
if err != nil {
583+
if err := os.MkdirAll(dest, 0o740); err != nil {
589584
t.Fatal(err)
590585
}
591-
os.WriteFile(src, []byte("content"), 0o777)
592-
err = defaultCopyWithTar(src, dest+"/")
593-
if err != nil {
586+
if err := os.WriteFile(src, []byte("content"), 0o777); err != nil {
587+
t.Fatal(err)
588+
}
589+
if err := defaultCopyWithTar(src, dest+"/"); err != nil {
594590
t.Fatalf("archiver.CopyFileWithTar shouldn't throw an error, %s.", err)
595591
}
596-
_, err = os.Stat(dest)
597-
if err != nil {
592+
if _, err := os.Stat(dest); err != nil {
598593
t.Fatalf("Destination folder should contain the source file but did not.")
599594
}
600595
}
@@ -655,9 +650,9 @@ func tarUntar(t *testing.T, origin string, options *TarOptions) ([]Change, error
655650
wrap := io.MultiReader(bytes.NewReader(buf), archive)
656651

657652
detectedCompression := DetectCompression(buf)
658-
compression := options.Compression
659-
if detectedCompression.Extension() != compression.Extension() {
660-
return nil, fmt.Errorf("Wrong compression detected. Actual compression: %s, found %s", compression.Extension(), detectedCompression.Extension())
653+
expected := options.Compression
654+
if detectedCompression.Extension() != expected.Extension() {
655+
return nil, fmt.Errorf("wrong compression detected; expected: %s, got: %s", expected.Extension(), detectedCompression.Extension())
661656
}
662657

663658
tmp := t.TempDir()
@@ -766,7 +761,7 @@ func TestTarWithOptionsChownOptsAlwaysOverridesIdPair(t *testing.T) {
766761
defer reader.Close()
767762
for {
768763
hdr, err := tr.Next()
769-
if err == io.EOF {
764+
if errors.Is(err, io.EOF) {
770765
// end of tar archive
771766
break
772767
}
@@ -838,7 +833,7 @@ func TestUntarUstarGnuConflict(t *testing.T) {
838833
// Iterate through the files in the archive.
839834
for {
840835
hdr, err := tr.Next()
841-
if err == io.EOF {
836+
if errors.Is(err, io.EOF) {
842837
// end of tar archive
843838
break
844839
}

archive_unix_test.go

Lines changed: 2 additions & 1 deletion
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)

archive_windows.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ func chmodTarEntry(perm os.FileMode) os.FileMode {
4141
return perm | 0o111
4242
}
4343

44-
func setHeaderForSpecialDevice(hdr *tar.Header, name string, stat interface{}) (err error) {
45-
// do nothing. no notion of Rdev, Nlink in stat on Windows
46-
return
47-
}
48-
4944
func getInodeFromStat(stat interface{}) (uint64, error) {
5045
// do nothing. no notion of Inode in stat on Windows
5146
return 0, nil

archive_windows_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ func TestCopyFileWithInvalidDest(t *testing.T) {
2121
dest := "c:dest"
2222
srcFolder := filepath.Join(folder, "src")
2323
src := filepath.Join(folder, "src", "src")
24-
err = os.MkdirAll(srcFolder, 0o740)
25-
if err != nil {
24+
if err := os.MkdirAll(srcFolder, 0o740); err != nil {
25+
t.Fatal(err)
26+
}
27+
if err := os.WriteFile(src, []byte("content"), 0o777); err != nil {
2628
t.Fatal(err)
2729
}
28-
os.WriteFile(src, []byte("content"), 0o777)
2930
err = defaultCopyWithTar(src, dest)
3031
if err == nil {
3132
t.Fatalf("archiver.CopyWithTar should throw an error on invalid dest.")

changes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func sameFsTime(a, b time.Time) bool {
7575
// Changes walks the path rw and determines changes for the files in the path,
7676
// with respect to the parent layers
7777
func Changes(layers []string, rw string) ([]Change, error) {
78-
return changes(layers, rw, aufsDeletedFile, aufsMetadataSkip)
78+
return collectChanges(layers, rw, aufsDeletedFile, aufsMetadataSkip)
7979
}
8080

8181
func aufsMetadataSkip(path string) (skip bool, err error) {
@@ -103,7 +103,7 @@ type (
103103
deleteChange func(string, string, os.FileInfo) (string, error)
104104
)
105105

106-
func changes(layers []string, rw string, dc deleteChange, sc skipChange) ([]Change, error) {
106+
func collectChanges(layers []string, rw string, dc deleteChange, sc skipChange) ([]Change, error) {
107107
var (
108108
changes []Change
109109
changedDirs = make(map[string]struct{})

changes_linux.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,7 @@ func (w *walker) walk(path string, i1, i2 os.FileInfo) (err error) {
132132
ix1 := 0
133133
ix2 := 0
134134

135-
for {
136-
if ix1 >= len(names1) {
137-
break
138-
}
139-
if ix2 >= len(names2) {
140-
break
141-
}
142-
135+
for ix1 < len(names1) && ix2 < len(names2) {
143136
ni1 := names1[ix1]
144137
ni2 := names2[ix2]
145138

changes_posix_test.go

Lines changed: 2 additions & 1 deletion
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

0 commit comments

Comments
 (0)