Skip to content

fix linting issues #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 15, 2025
31 changes: 16 additions & 15 deletions archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (r *bufferedReader) Read(p []byte) (int, error) {
return 0, io.EOF
}
n, err := r.buf.Read(p)
if err == io.EOF {
if errors.Is(err, io.EOF) {
r.buf.Reset(nil)
bufioReader32KPool.Put(r.buf)
r.buf = nil
Expand All @@ -279,7 +279,7 @@ func (r *bufferedReader) Peek(n int) ([]byte, error) {
func DecompressStream(archive io.Reader) (io.ReadCloser, error) {
buf := newBufferedReader(archive)
bs, err := buf.Peek(10)
if err != nil && err != io.EOF {
if err != nil && !errors.Is(err, io.EOF) {
// Note: we'll ignore any io.EOF error because there are some odd
// cases where the layer.tar file will be empty (zero bytes) and
// that results in an io.EOF from the Peek() call. So, in those
Expand Down Expand Up @@ -344,7 +344,7 @@ func DecompressStream(archive io.Reader) (io.ReadCloser, error) {
},
}, nil
default:
return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
return nil, fmt.Errorf("unsupported compression format: %s", (&compression).Extension())
}
}

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

Expand Down Expand Up @@ -416,7 +416,7 @@ func ReplaceFileTarWrapper(inputTarStream io.ReadCloser, mods map[string]TarModi
var originalHeader *tar.Header
for {
originalHeader, err = tarReader.Next()
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
if err != nil {
Expand Down Expand Up @@ -768,7 +768,7 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, o
case tar.TypeDir:
// Create directory unless it exists as a directory already.
// In that case we just want to merge the two
if fi, err := os.Lstat(path); !(err == nil && fi.IsDir()) {
if fi, err := os.Lstat(path); err != nil || !fi.IsDir() {
if err := os.Mkdir(path, hdrInfo.Mode()); err != nil {
return err
}
Expand Down Expand Up @@ -1031,7 +1031,8 @@ func (t *Tarballer) Do() {
)

walkRoot := getWalkRoot(t.srcPath, include)
filepath.WalkDir(walkRoot, func(filePath string, f os.DirEntry, err error) error {
// TODO(thaJeztah): should this error be handled?
_ = filepath.WalkDir(walkRoot, func(filePath string, f os.DirEntry, err error) error {
if err != nil {
log.G(context.TODO()).Errorf("Tar: Can't stat file %s to tar: %s", t.srcPath, err)
return nil
Expand Down Expand Up @@ -1135,7 +1136,7 @@ func (t *Tarballer) Do() {
if err := ta.addTarFile(filePath, relFilePath); err != nil {
log.G(context.TODO()).Errorf("Can't add file %s to tar: %s", filePath, err)
// if pipe is broken, stop writing tar stream to it
if err == io.ErrClosedPipe {
if errors.Is(err, io.ErrClosedPipe) {
return err
}
}
Expand All @@ -1155,7 +1156,7 @@ func Unpack(decompressedArchive io.Reader, dest string, options *TarOptions) err
loop:
for {
hdr, err := tr.Next()
if err == io.EOF {
if errors.Is(err, io.EOF) {
// end of tar archive
break
}
Expand Down Expand Up @@ -1217,7 +1218,7 @@ loop:
continue
}

if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
if !fi.IsDir() || hdr.Typeflag != tar.TypeDir {
if err := os.RemoveAll(path); err != nil {
return err
}
Expand Down Expand Up @@ -1309,7 +1310,7 @@ func UntarUncompressed(tarArchive io.Reader, dest string, options *TarOptions) e
// Handler for teasing out the automatic decompression
func untarHandler(tarArchive io.Reader, dest string, options *TarOptions, decompress bool) error {
if tarArchive == nil {
return fmt.Errorf("Empty archive")
return errors.New("empty archive")
}
dest = filepath.Clean(dest)
if options == nil {
Expand Down Expand Up @@ -1393,7 +1394,7 @@ func (archiver *Archiver) CopyFileWithTar(src, dst string) (err error) {
}

if srcSt.IsDir() {
return fmt.Errorf("Can't copy a directory")
return errors.New("can't copy a directory")
}

// Clean up the trailing slash. This must be done in an operating
Expand Down Expand Up @@ -1492,9 +1493,9 @@ func cmdStream(cmd *exec.Cmd, input io.Reader) (io.ReadCloser, error) {
// Copy stdout to the returned pipe
go func() {
if err := cmd.Wait(); err != nil {
pipeW.CloseWithError(fmt.Errorf("%s: %s", err, errBuf.String()))
_ = pipeW.CloseWithError(fmt.Errorf("%w: %s", err, errBuf.String()))
} else {
pipeW.Close()
_ = pipeW.Close()
}
close(done)
}()
Expand Down
3 changes: 2 additions & 1 deletion archive_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package archive
import (
"archive/tar"
"bytes"
"errors"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -119,7 +120,7 @@ func TestOverlayTarUntar(t *testing.T) {
rdr := tar.NewReader(bytes.NewReader(archive))
for {
h, err := rdr.Next()
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
assert.NilError(t, err)
Expand Down
59 changes: 27 additions & 32 deletions archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/tar"
"bytes"
"compress/gzip"
"errors"
"fmt"
"io"
"io/fs"
Expand Down Expand Up @@ -479,22 +480,20 @@ func TestCopyWithTarSrcFile(t *testing.T) {
dest := filepath.Join(folder, "dest")
srcFolder := filepath.Join(folder, "src")
src := filepath.Join(folder, filepath.Join("src", "src"))
err := os.MkdirAll(srcFolder, 0o740)
if err != nil {
if err := os.MkdirAll(srcFolder, 0o740); err != nil {
t.Fatal(err)
}
err = os.MkdirAll(dest, 0o740)
if err != nil {
if err := os.MkdirAll(dest, 0o740); err != nil {
t.Fatal(err)
}
os.WriteFile(src, []byte("content"), 0o777)
err = defaultCopyWithTar(src, dest)
if err != nil {
if err := os.WriteFile(src, []byte("content"), 0o777); err != nil {
t.Fatal(err)
}
if err := defaultCopyWithTar(src, dest); err != nil {
t.Fatalf("archiver.CopyWithTar shouldn't throw an error, %s.", err)
}
_, err = os.Stat(dest)
// FIXME Check the content
if err != nil {
if _, err := os.Stat(dest); err != nil {
t.Fatalf("Destination file should be the same as the source.")
}
}
Expand All @@ -504,22 +503,20 @@ func TestCopyWithTarSrcFolder(t *testing.T) {
folder := t.TempDir()
dest := filepath.Join(folder, "dest")
src := filepath.Join(folder, filepath.Join("src", "folder"))
err := os.MkdirAll(src, 0o740)
if err != nil {
if err := os.MkdirAll(src, 0o740); err != nil {
t.Fatal(err)
}
err = os.MkdirAll(dest, 0o740)
if err != nil {
if err := os.MkdirAll(dest, 0o740); err != nil {
t.Fatal(err)
}
os.WriteFile(filepath.Join(src, "file"), []byte("content"), 0o777)
err = defaultCopyWithTar(src, dest)
if err != nil {
if err := os.WriteFile(filepath.Join(src, "file"), []byte("content"), 0o777); err != nil {
t.Fatal(err)
}
if err := defaultCopyWithTar(src, dest); err != nil {
t.Fatalf("archiver.CopyWithTar shouldn't throw an error, %s.", err)
}
_, err = os.Stat(dest)
// FIXME Check the content (the file inside)
if err != nil {
if _, err := os.Stat(dest); err != nil {
t.Fatalf("Destination folder should contain the source file but did not.")
}
}
Expand Down Expand Up @@ -580,21 +577,19 @@ func TestCopyFileWithTarSrcFile(t *testing.T) {
dest := filepath.Join(folder, "dest")
srcFolder := filepath.Join(folder, "src")
src := filepath.Join(folder, filepath.Join("src", "src"))
err := os.MkdirAll(srcFolder, 0o740)
if err != nil {
if err := os.MkdirAll(srcFolder, 0o740); err != nil {
t.Fatal(err)
}
err = os.MkdirAll(dest, 0o740)
if err != nil {
if err := os.MkdirAll(dest, 0o740); err != nil {
t.Fatal(err)
}
os.WriteFile(src, []byte("content"), 0o777)
err = defaultCopyWithTar(src, dest+"/")
if err != nil {
if err := os.WriteFile(src, []byte("content"), 0o777); err != nil {
t.Fatal(err)
}
if err := defaultCopyWithTar(src, dest+"/"); err != nil {
t.Fatalf("archiver.CopyFileWithTar shouldn't throw an error, %s.", err)
}
_, err = os.Stat(dest)
if err != nil {
if _, err := os.Stat(dest); err != nil {
t.Fatalf("Destination folder should contain the source file but did not.")
}
}
Expand Down Expand Up @@ -655,9 +650,9 @@ func tarUntar(t *testing.T, origin string, options *TarOptions) ([]Change, error
wrap := io.MultiReader(bytes.NewReader(buf), archive)

detectedCompression := DetectCompression(buf)
compression := options.Compression
if detectedCompression.Extension() != compression.Extension() {
return nil, fmt.Errorf("Wrong compression detected. Actual compression: %s, found %s", compression.Extension(), detectedCompression.Extension())
expected := options.Compression
if detectedCompression.Extension() != expected.Extension() {
return nil, fmt.Errorf("wrong compression detected; expected: %s, got: %s", expected.Extension(), detectedCompression.Extension())
}

tmp := t.TempDir()
Expand Down Expand Up @@ -766,7 +761,7 @@ func TestTarWithOptionsChownOptsAlwaysOverridesIdPair(t *testing.T) {
defer reader.Close()
for {
hdr, err := tr.Next()
if err == io.EOF {
if errors.Is(err, io.EOF) {
// end of tar archive
break
}
Expand Down Expand Up @@ -838,7 +833,7 @@ func TestUntarUstarGnuConflict(t *testing.T) {
// Iterate through the files in the archive.
for {
hdr, err := tr.Next()
if err == io.EOF {
if errors.Is(err, io.EOF) {
// end of tar archive
break
}
Expand Down
3 changes: 2 additions & 1 deletion archive_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package archive
import (
"archive/tar"
"bytes"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -259,7 +260,7 @@ func TestTarUntarWithXattr(t *testing.T) {
rdr := tar.NewReader(tarball)
for {
h, err := rdr.Next()
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
assert.NilError(t, err)
Expand Down
5 changes: 0 additions & 5 deletions archive_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ func chmodTarEntry(perm os.FileMode) os.FileMode {
return perm | 0o111
}

func setHeaderForSpecialDevice(hdr *tar.Header, name string, stat interface{}) (err error) {
// do nothing. no notion of Rdev, Nlink in stat on Windows
return
}

func getInodeFromStat(stat interface{}) (uint64, error) {
// do nothing. no notion of Inode in stat on Windows
return 0, nil
Expand Down
7 changes: 4 additions & 3 deletions archive_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ func TestCopyFileWithInvalidDest(t *testing.T) {
dest := "c:dest"
srcFolder := filepath.Join(folder, "src")
src := filepath.Join(folder, "src", "src")
err = os.MkdirAll(srcFolder, 0o740)
if err != nil {
if err := os.MkdirAll(srcFolder, 0o740); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(src, []byte("content"), 0o777); err != nil {
t.Fatal(err)
}
os.WriteFile(src, []byte("content"), 0o777)
err = defaultCopyWithTar(src, dest)
if err == nil {
t.Fatalf("archiver.CopyWithTar should throw an error on invalid dest.")
Expand Down
4 changes: 2 additions & 2 deletions changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func sameFsTime(a, b time.Time) bool {
// Changes walks the path rw and determines changes for the files in the path,
// with respect to the parent layers
func Changes(layers []string, rw string) ([]Change, error) {
return changes(layers, rw, aufsDeletedFile, aufsMetadataSkip)
return collectChanges(layers, rw, aufsDeletedFile, aufsMetadataSkip)
}

func aufsMetadataSkip(path string) (skip bool, err error) {
Expand Down Expand Up @@ -103,7 +103,7 @@ type (
deleteChange func(string, string, os.FileInfo) (string, error)
)

func changes(layers []string, rw string, dc deleteChange, sc skipChange) ([]Change, error) {
func collectChanges(layers []string, rw string, dc deleteChange, sc skipChange) ([]Change, error) {
var (
changes []Change
changedDirs = make(map[string]struct{})
Expand Down
9 changes: 1 addition & 8 deletions changes_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,7 @@ func (w *walker) walk(path string, i1, i2 os.FileInfo) (err error) {
ix1 := 0
ix2 := 0

for {
if ix1 >= len(names1) {
break
}
if ix2 >= len(names2) {
break
}

for ix1 < len(names1) && ix2 < len(names2) {
ni1 := names1[ix1]
ni2 := names2[ix2]

Expand Down
3 changes: 2 additions & 1 deletion changes_posix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package archive

import (
"archive/tar"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -112,7 +113,7 @@ func walkHeaders(r io.Reader) ([]tar.Header, error) {
for {
hdr, err := t.Next()
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
return headers, err
Expand Down
Loading