diff --git a/.golangci.yml b/.golangci.yml index 9363333a6..ad4dec0c1 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -5,6 +5,9 @@ formatters: - gofumpt linters: + enable: + - nolintlint + - unconvert exclusions: presets: - comments diff --git a/cmd/containers-storage/layers.go b/cmd/containers-storage/layers.go index 11e4cbfc5..d29509c4e 100644 --- a/cmd/containers-storage/layers.go +++ b/cmd/containers-storage/layers.go @@ -67,8 +67,8 @@ func layers(flags *mflag.FlagSet, action string, m storage.Store, args []string) for _, layer := range layers { if listLayersTree { node := treeNode{ - left: string(layer.Parent), - right: string(layer.ID), + left: layer.Parent, + right: layer.ID, notes: []string{}, } if node.left == "" { diff --git a/drivers/chown_unix.go b/drivers/chown_unix.go index b0c25cd99..d5a58a507 100644 --- a/drivers/chown_unix.go +++ b/drivers/chown_unix.go @@ -36,8 +36,8 @@ func (c *platformChowner) LChown(path string, info os.FileInfo, toHost, toContai } i := inode{ - Dev: uint64(st.Dev), - Ino: uint64(st.Ino), + Dev: uint64(st.Dev), //nolint:unconvert + Ino: st.Ino, } c.mutex.Lock() diff --git a/drivers/copy/copy_linux.go b/drivers/copy/copy_linux.go index 5187bc8ca..c94cb5e12 100644 --- a/drivers/copy/copy_linux.go +++ b/drivers/copy/copy_linux.go @@ -160,7 +160,10 @@ func DirCopy(srcDir, dstDir string, copyMode Mode, copyXattrs bool) error { switch mode := f.Mode(); { case mode.IsRegular(): - id := fileID{dev: uint64(stat.Dev), ino: stat.Ino} + id := fileID{ + dev: uint64(stat.Dev), //nolint:unconvert + ino: stat.Ino, + } if copyMode == Hardlink { isHardlink = true if err2 := os.Link(srcPath, dstPath); err2 != nil { @@ -242,12 +245,11 @@ func DirCopy(srcDir, dstDir string, copyMode Mode, copyXattrs bool) error { } // system.Chtimes doesn't support a NOFOLLOW flag atm - // nolint: unconvert if f.IsDir() { dirsToSetMtimes.PushFront(&dirMtimeInfo{dstPath: &dstPath, stat: stat}) } else if !isSymlink { - aTime := time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) - mTime := time.Unix(int64(stat.Mtim.Sec), int64(stat.Mtim.Nsec)) + aTime := time.Unix(stat.Atim.Unix()) + mTime := time.Unix(stat.Mtim.Unix()) if err := system.Chtimes(dstPath, aTime, mTime); err != nil { return err } diff --git a/drivers/overlay/check.go b/drivers/overlay/check.go index 527701746..e398b0aa8 100644 --- a/drivers/overlay/check.go +++ b/drivers/overlay/check.go @@ -259,7 +259,7 @@ func supportsIdmappedLowerLayers(home string) (bool, error) { } defer cleanupFunc() - if err := idmap.CreateIDMappedMount(lowerDir, lowerMappedDir, int(pid)); err != nil { + if err := idmap.CreateIDMappedMount(lowerDir, lowerMappedDir, pid); err != nil { return false, fmt.Errorf("create mapped mount: %w", err) } defer func() { diff --git a/drivers/overlay/overlay.go b/drivers/overlay/overlay.go index e15530b80..5345ff9d6 100644 --- a/drivers/overlay/overlay.go +++ b/drivers/overlay/overlay.go @@ -492,7 +492,7 @@ func parseOptions(options []string) (*overlayOptions, error) { if err != nil { return nil, err } - o.quota.Inodes = uint64(inodes) + o.quota.Inodes = inodes case "imagestore", "additionalimagestore": logrus.Debugf("overlay: imagestore=%s", val) // Additional read only image stores to use for lower paths @@ -1163,7 +1163,7 @@ func (d *Driver) parseStorageOpt(storageOpt map[string]string, driver *Driver) e if err != nil { return err } - driver.options.quota.Inodes = uint64(inodes) + driver.options.quota.Inodes = inodes default: return fmt.Errorf("unknown option %s", key) } @@ -1551,7 +1551,7 @@ func (d *Driver) get(id string, disableShifting bool, options graphdriver.MountO permsKnown := false st, err := os.Stat(filepath.Join(dir, nameWithSuffix("diff", diffN))) if err == nil { - perms = os.FileMode(st.Mode()) + perms = st.Mode() permsKnown = true } for err == nil { @@ -1566,7 +1566,7 @@ func (d *Driver) get(id string, disableShifting bool, options graphdriver.MountO if err != nil { return "", err } - idmappedMountProcessPid = int(pid) + idmappedMountProcessPid = pid defer cleanupFunc() } @@ -1638,7 +1638,7 @@ func (d *Driver) get(id string, disableShifting bool, options graphdriver.MountO lower = path.Join(p, d.name, l) if st2, err2 := os.Stat(lower); err2 == nil { if !permsKnown { - perms = os.FileMode(st2.Mode()) + perms = st2.Mode() permsKnown = true } break @@ -1659,7 +1659,7 @@ func (d *Driver) get(id string, disableShifting bool, options graphdriver.MountO } } else { if !permsKnown { - perms = os.FileMode(st.Mode()) + perms = st.Mode() permsKnown = true } lower = newpath @@ -2505,7 +2505,7 @@ func (d *Driver) UpdateLayerIDMap(id string, toContainer, toHost *idtools.IDMapp perms = *d.options.forceMask } else { if err == nil { - perms = os.FileMode(st.Mode()) + perms = st.Mode() } } for err == nil { diff --git a/internal/dedup/dedup_linux.go b/internal/dedup/dedup_linux.go index 1f6179fbc..e13548ff8 100644 --- a/internal/dedup/dedup_linux.go +++ b/internal/dedup/dedup_linux.go @@ -48,7 +48,7 @@ func (d *dedupFiles) isFirstVisitOf(fi fs.FileInfo) (bool, error) { if !ok { return false, fmt.Errorf("unable to get raw syscall.Stat_t data") } - return d.recordInode(uint64(st.Dev), st.Ino) + return d.recordInode(uint64(st.Dev), st.Ino) //nolint:unconvert } // dedup deduplicates the file at src path to dst path @@ -94,7 +94,7 @@ func (d *dedupFiles) dedup(src, dst string, fiDst fs.FileInfo) (uint64, error) { } err = unix.IoctlFileDedupeRange(int(srcFile.Fd()), &value) if err == nil { - return uint64(value.Info[0].Bytes_deduped), nil + return value.Info[0].Bytes_deduped, nil } if errors.Is(err, unix.ENOTSUP) { diff --git a/internal/opts/opts.go b/internal/opts/opts.go index 87226e775..dba0a448b 100644 --- a/internal/opts/opts.go +++ b/internal/opts/opts.go @@ -28,7 +28,7 @@ func NewListOptsRef(values *[]string, validator ValidatorFctType) *ListOpts { } func (opts *ListOpts) String() string { - return fmt.Sprintf("%v", []string((*opts.values))) + return fmt.Sprintf("%v", *opts.values) } // Set validates if needed the input value and adds it to the @@ -150,7 +150,7 @@ func (opts *MapOpts) GetAll() map[string]string { } func (opts *MapOpts) String() string { - return fmt.Sprintf("%v", map[string]string((opts.values))) + return fmt.Sprintf("%v", opts.values) } // Type returns a string name for this Option type diff --git a/pkg/archive/archive.go b/pkg/archive/archive.go index d63fe24cc..3dfadf009 100644 --- a/pkg/archive/archive.go +++ b/pkg/archive/archive.go @@ -1406,7 +1406,7 @@ func remapIDs(readIDMappings, writeIDMappings *idtools.IDMappings, chownOpts *id } else if runtime.GOOS == darwin { uid, gid = hdr.Uid, hdr.Gid if xstat, ok := hdr.PAXRecords[PaxSchilyXattr+idtools.ContainersOverrideXattr]; ok { - attrs := strings.Split(string(xstat), ":") + attrs := strings.Split(xstat, ":") if len(attrs) >= 3 { val, err := strconv.ParseUint(attrs[0], 10, 32) if err != nil { diff --git a/pkg/archive/archive_linux.go b/pkg/archive/archive_linux.go index 011668627..b3245f7fd 100644 --- a/pkg/archive/archive_linux.go +++ b/pkg/archive/archive_linux.go @@ -173,7 +173,7 @@ func (o overlayWhiteoutConverter) ConvertRead(hdr *tar.Header, path string) (boo func isWhiteOut(stat os.FileInfo) bool { s := stat.Sys().(*syscall.Stat_t) - return major(uint64(s.Rdev)) == 0 && minor(uint64(s.Rdev)) == 0 + return major(uint64(s.Rdev)) == 0 && minor(uint64(s.Rdev)) == 0 //nolint:unconvert } func GetFileOwner(path string) (uint32, uint32, uint32, error) { diff --git a/pkg/archive/archive_unix_test.go b/pkg/archive/archive_unix_test.go index 2a8a04544..e3cd4fde8 100644 --- a/pkg/archive/archive_unix_test.go +++ b/pkg/archive/archive_unix_test.go @@ -161,8 +161,7 @@ func getNlink(path string) (uint64, error) { if !ok { return 0, fmt.Errorf("expected type *syscall.Stat_t, got %t", stat.Sys()) } - // We need this conversion on ARM64 - return uint64(statT.Nlink), nil + return uint64(statT.Nlink), nil //nolint:unconvert // Need the conversion for e.g. linux/arm64. } func getInode(path string) (uint64, error) { diff --git a/pkg/chunked/bloom_filter_linux.go b/pkg/chunked/bloom_filter_linux.go index 09e75680a..c2dbe19a4 100644 --- a/pkg/chunked/bloom_filter_linux.go +++ b/pkg/chunked/bloom_filter_linux.go @@ -65,7 +65,7 @@ func (bf *bloomFilter) writeTo(writer io.Writer) error { if err := binary.Write(writer, binary.LittleEndian, uint64(len(bf.bitArray))); err != nil { return err } - if err := binary.Write(writer, binary.LittleEndian, uint32(bf.k)); err != nil { + if err := binary.Write(writer, binary.LittleEndian, bf.k); err != nil { return err } if err := binary.Write(writer, binary.LittleEndian, bf.bitArray); err != nil { diff --git a/pkg/chunked/cache_linux_test.go b/pkg/chunked/cache_linux_test.go index 20414acac..3580ca93b 100644 --- a/pkg/chunked/cache_linux_test.go +++ b/pkg/chunked/cache_linux_test.go @@ -238,7 +238,7 @@ func FuzzReadCache(f *testing.F) { dest = nil f.Fuzz(func(t *testing.T, orig []byte) { - cacheRead, err := readCacheFileFromMemory([]byte(orig)) + cacheRead, err := readCacheFileFromMemory(orig) if err != nil || cacheRead == nil { return } diff --git a/pkg/chunked/compression_linux.go b/pkg/chunked/compression_linux.go index aee387658..62dd22dfb 100644 --- a/pkg/chunked/compression_linux.go +++ b/pkg/chunked/compression_linux.go @@ -87,7 +87,7 @@ func readEstargzChunkedManifest(blobStream ImageSourceSeekable, blobSize int64, return nil, 0, fmt.Errorf("parse ToC offset: %w", err) } - size := int64(blobSize - footerSize - tocOffset) + size := blobSize - footerSize - tocOffset // set a reasonable limit if size > maxTocSize { // Not errFallbackCanConvert: we would still use too much memory. diff --git a/pkg/chunked/dump/dump.go b/pkg/chunked/dump/dump.go index 3a5f61917..0bda7a0d8 100644 --- a/pkg/chunked/dump/dump.go +++ b/pkg/chunked/dump/dump.go @@ -43,7 +43,7 @@ func escaped(val []byte, escape int) string { } var result string - for _, c := range []byte(val) { + for _, c := range val { hexEscape := false var special string diff --git a/pkg/chunked/filesystem_linux_test.go b/pkg/chunked/filesystem_linux_test.go index 979543704..cdec54032 100644 --- a/pkg/chunked/filesystem_linux_test.go +++ b/pkg/chunked/filesystem_linux_test.go @@ -177,8 +177,7 @@ func TestSafeLink(t *testing.T) { err = syscall.Fstat(int(newFile.Fd()), &st) assert.NoError(t, err) - // We need this conversion on ARM64 - assert.Equal(t, uint64(st.Nlink), uint64(2)) + assert.Equal(t, 2, int(st.Nlink)) err = newFile.Close() assert.NoError(t, err) diff --git a/pkg/chunked/internal/minimal/compression.go b/pkg/chunked/internal/minimal/compression.go index 377ece2e8..f85c5973c 100644 --- a/pkg/chunked/internal/minimal/compression.go +++ b/pkg/chunked/internal/minimal/compression.go @@ -234,7 +234,7 @@ func WriteZstdChunkedManifest(dest io.Writer, outMetadata map[string]string, off Offset: manifestOffset, LengthCompressed: uint64(len(compressedManifest)), LengthUncompressed: uint64(len(manifest)), - OffsetTarSplit: uint64(tarSplitOffset), + OffsetTarSplit: tarSplitOffset, LengthCompressedTarSplit: uint64(len(tarSplitData.Data)), LengthUncompressedTarSplit: uint64(tarSplitData.UncompressedSize), } diff --git a/pkg/chunked/storage_linux.go b/pkg/chunked/storage_linux.go index 1f3e229f9..75148bf2b 100644 --- a/pkg/chunked/storage_linux.go +++ b/pkg/chunked/storage_linux.go @@ -1011,7 +1011,7 @@ func mergeMissingChunks(missingParts []missingPart, target int) []missingPart { !missingParts[prevIndex].Hole && !missingParts[i].Hole && len(missingParts[prevIndex].Chunks) == 1 && len(missingParts[i].Chunks) == 1 && missingParts[prevIndex].Chunks[0].File.Name == missingParts[i].Chunks[0].File.Name { - missingParts[prevIndex].SourceChunk.Length += uint64(gap) + missingParts[i].SourceChunk.Length + missingParts[prevIndex].SourceChunk.Length += gap + missingParts[i].SourceChunk.Length missingParts[prevIndex].Chunks[0].CompressedSize += missingParts[i].Chunks[0].CompressedSize missingParts[prevIndex].Chunks[0].UncompressedSize += missingParts[i].Chunks[0].UncompressedSize } else { @@ -1069,7 +1069,7 @@ func mergeMissingChunks(missingParts []missingPart, target int) []missingPart { } else { gap := getGap(missingParts, i) prev := &newMissingParts[len(newMissingParts)-1] - prev.SourceChunk.Length += uint64(gap) + missingParts[i].SourceChunk.Length + prev.SourceChunk.Length += gap + missingParts[i].SourceChunk.Length prev.Hole = false prev.OriginFile = nil if gap > 0 { @@ -1761,7 +1761,7 @@ func (c *chunkedDiffer) ApplyDiff(dest string, options *archive.TarOptions, diff // the file is missing, attempt to find individual chunks. for _, chunk := range r.chunks { - compressedSize := int64(chunk.EndOffset - chunk.Offset) + compressedSize := chunk.EndOffset - chunk.Offset size := remainingSize if chunk.ChunkSize > 0 { size = chunk.ChunkSize diff --git a/pkg/directory/directory_unix.go b/pkg/directory/directory_unix.go index 08060f2a2..9855abd13 100644 --- a/pkg/directory/directory_unix.go +++ b/pkg/directory/directory_unix.go @@ -42,13 +42,11 @@ func Usage(dir string) (usage *DiskUsage, err error) { // Check inode to only count the sizes of files with multiple hard links once. inode := fileInfo.Sys().(*syscall.Stat_t).Ino - // inode is not a uint64 on all platforms. Cast it to avoid issues. - if _, exists := data[uint64(inode)]; exists { + if _, exists := data[inode]; exists { return nil } - // inode is not a uint64 on all platforms. Cast it to avoid issues. - data[uint64(inode)] = struct{}{} + data[inode] = struct{}{} // Ignore directory sizes if entry.IsDir() { return nil diff --git a/pkg/ioutils/fswriters_test.go b/pkg/ioutils/fswriters_test.go index 9c997c9e6..da85607a0 100644 --- a/pkg/ioutils/fswriters_test.go +++ b/pkg/ioutils/fswriters_test.go @@ -38,7 +38,7 @@ func TestAtomicWriteToFile(t *testing.T) { if err != nil { t.Fatalf("Error statting file: %v", err) } - if expected := os.FileMode(testMode); st.Mode() != expected { + if expected := testMode; st.Mode() != expected { t.Fatalf("Mode mismatched, expected %o, got %o", expected, st.Mode()) } } @@ -131,7 +131,7 @@ func TestAtomicWriteSetCommit(t *testing.T) { if err != nil { t.Fatalf("Error statting file: %v", err) } - if expected := os.FileMode(testMode); st.Mode() != expected { + if expected := testMode; st.Mode() != expected { t.Fatalf("Mode mismatched, expected %o, got %o", expected, st.Mode()) } } diff --git a/pkg/loopback/attach_loopback.go b/pkg/loopback/attach_loopback.go index e329aa9a1..d5f5d6381 100644 --- a/pkg/loopback/attach_loopback.go +++ b/pkg/loopback/attach_loopback.go @@ -113,7 +113,7 @@ func openNextAvailableLoopback(sparseName string, sparseFile *os.File) (*os.File logrus.Errorf("Getting loopback backing file: %s", err) return nil, ErrGetLoopbackBackingFile } - if dev != uint64(st.Dev) || ino != st.Ino { + if dev != uint64(st.Dev) || ino != st.Ino { //nolint:unconvert logrus.Errorf("Loopback device and filesystem disagree on device/inode for %q: %#x(%d):%#x(%d) vs %#x(%d):%#x(%d)", sparseName, dev, dev, ino, ino, st.Dev, st.Dev, st.Ino, st.Ino) } return loopFile, nil diff --git a/pkg/loopback/loopback.go b/pkg/loopback/loopback.go index b3527e3a0..f773e11d6 100644 --- a/pkg/loopback/loopback.go +++ b/pkg/loopback/loopback.go @@ -36,7 +36,7 @@ func FindLoopDeviceFor(file *os.File) *os.File { return nil } targetInode := stat.Sys().(*syscall.Stat_t).Ino - targetDevice := stat.Sys().(*syscall.Stat_t).Dev + targetDevice := uint64(stat.Sys().(*syscall.Stat_t).Dev) //nolint:unconvert for i := 0; true; i++ { path := fmt.Sprintf("/dev/loop%d", i) @@ -53,7 +53,7 @@ func FindLoopDeviceFor(file *os.File) *os.File { } dev, inode, err := getLoopbackBackingFile(file) - if err == nil && dev == uint64(targetDevice) && inode == targetInode { + if err == nil && dev == targetDevice && inode == targetInode { return file } file.Close() diff --git a/pkg/reexec/reexec.go b/pkg/reexec/reexec.go index 0c032e6c4..a1938cd4f 100644 --- a/pkg/reexec/reexec.go +++ b/pkg/reexec/reexec.go @@ -49,7 +49,7 @@ func panicIfNotInitialized() { } } -func naiveSelf() string { //nolint: unused +func naiveSelf() string { name := os.Args[0] if filepath.Base(name) == name { if lp, err := exec.LookPath(name); err == nil { diff --git a/pkg/system/chtimes_linux_test.go b/pkg/system/chtimes_linux_test.go index df3ff26f4..bb6a1af83 100644 --- a/pkg/system/chtimes_linux_test.go +++ b/pkg/system/chtimes_linux_test.go @@ -9,6 +9,18 @@ import ( "time" ) +func atime(t *testing.T, file string) time.Time { + t.Helper() + + fi, err := os.Stat(file) + if err != nil { + t.Fatal(err) + } + + stat := fi.Sys().(*syscall.Stat_t) + return time.Unix(stat.Atim.Unix()) +} + // TestChtimesLinux tests Chtimes access time on a tempfile on Linux func TestChtimesLinux(t *testing.T) { file := prepareTempFile(t) @@ -23,13 +35,7 @@ func TestChtimesLinux(t *testing.T) { t.Fatal(err) } - f, err := os.Stat(file) - if err != nil { - t.Fatal(err) - } - - stat := f.Sys().(*syscall.Stat_t) - aTime := time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) + aTime := atime(t, file) if aTime != unixEpochTime { t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) } @@ -39,13 +45,7 @@ func TestChtimesLinux(t *testing.T) { t.Fatal(err) } - f, err = os.Stat(file) - if err != nil { - t.Fatal(err) - } - - stat = f.Sys().(*syscall.Stat_t) - aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) + aTime = atime(t, file) if aTime != unixEpochTime { t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) } @@ -55,13 +55,7 @@ func TestChtimesLinux(t *testing.T) { t.Fatal(err) } - f, err = os.Stat(file) - if err != nil { - t.Fatal(err) - } - - stat = f.Sys().(*syscall.Stat_t) - aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) + aTime = atime(t, file) if aTime != unixEpochTime { t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) } @@ -71,13 +65,7 @@ func TestChtimesLinux(t *testing.T) { t.Fatal(err) } - f, err = os.Stat(file) - if err != nil { - t.Fatal(err) - } - - stat = f.Sys().(*syscall.Stat_t) - aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) + aTime = atime(t, file) if aTime != afterUnixEpochTime { t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, aTime) } @@ -87,13 +75,7 @@ func TestChtimesLinux(t *testing.T) { t.Fatal(err) } - f, err = os.Stat(file) - if err != nil { - t.Fatal(err) - } - - stat = f.Sys().(*syscall.Stat_t) - aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) + aTime = atime(t, file) if aTime.Truncate(time.Second) != unixMaxTime.Truncate(time.Second) { t.Fatalf("Expected: %s, got: %s", unixMaxTime.Truncate(time.Second), aTime.Truncate(time.Second)) } diff --git a/pkg/system/stat_linux.go b/pkg/system/stat_linux.go index e3d13463f..0dee88d1b 100644 --- a/pkg/system/stat_linux.go +++ b/pkg/system/stat_linux.go @@ -9,9 +9,9 @@ func fromStatT(s *syscall.Stat_t) (*StatT, error) { mode: s.Mode, uid: s.Uid, gid: s.Gid, - rdev: uint64(s.Rdev), + rdev: uint64(s.Rdev), //nolint:unconvert mtim: s.Mtim, - dev: uint64(s.Dev), + dev: uint64(s.Dev), //nolint:unconvert }, nil } diff --git a/pkg/unshare/unshare_linux.go b/pkg/unshare/unshare_linux.go index 3f63d3191..9e0e562d2 100644 --- a/pkg/unshare/unshare_linux.go +++ b/pkg/unshare/unshare_linux.go @@ -468,7 +468,7 @@ type Runnable interface { Run() error } -func bailOnError(err error, format string, a ...any) { // nolint: revive,goprintffuncname +func bailOnError(err error, format string, a ...any) { //nolint:revive,goprintffuncname if err != nil { if format != "" { logrus.Errorf("%s: %v", fmt.Sprintf(format, a...), err)