diff options
author | Daniel J Walsh <dwalsh@redhat.com> | 2022-03-26 06:39:11 -0400 |
---|---|---|
committer | Daniel J Walsh <dwalsh@redhat.com> | 2022-03-27 07:18:25 -0400 |
commit | d106b294b428fbb10f59d4cafe72c3dcaa4e73bb (patch) | |
tree | 3c81aa0b8452565c704629258643eb6c51b82b3e | |
parent | 56b2937f87bd67b46aa93109aefc08ce0edb5cf1 (diff) | |
download | podman-d106b294b428fbb10f59d4cafe72c3dcaa4e73bb.tar.gz podman-d106b294b428fbb10f59d4cafe72c3dcaa4e73bb.tar.bz2 podman-d106b294b428fbb10f59d4cafe72c3dcaa4e73bb.zip |
Switch all calls to filepath.Walk to filepath.WalkDir
WalkDir should be faster the Walk, since we often do
not need to stat files.
[NO NEW TESTS NEEDED] Existing tests should find errors.
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
-rw-r--r-- | libpod/container_internal.go | 11 | ||||
-rw-r--r-- | libpod/volume.go | 12 | ||||
-rw-r--r-- | pkg/bindings/images/build.go | 23 | ||||
-rw-r--r-- | pkg/domain/infra/abi/system.go | 22 | ||||
-rw-r--r-- | pkg/machine/ignition.go | 5 | ||||
-rw-r--r-- | pkg/machine/qemu/machine.go | 6 | ||||
-rw-r--r-- | pkg/machine/wsl/machine.go | 7 | ||||
-rw-r--r-- | pkg/specgen/generate/config_linux.go | 5 | ||||
-rw-r--r-- | pkg/util/utils.go | 18 | ||||
-rw-r--r-- | pkg/util/utils_linux.go | 9 | ||||
-rw-r--r-- | test/e2e/pod_rm_test.go | 7 |
11 files changed, 68 insertions, 57 deletions
diff --git a/libpod/container_internal.go b/libpod/container_internal.go index b7362e7fb..0db59f2fe 100644 --- a/libpod/container_internal.go +++ b/libpod/container_internal.go @@ -99,15 +99,8 @@ func (c *Container) rootFsSize() (int64, error) { // rwSize gets the size of the mutable top layer of the container. func (c *Container) rwSize() (int64, error) { if c.config.Rootfs != "" { - var size int64 - err := filepath.Walk(c.config.Rootfs, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - size += info.Size() - return nil - }) - return size, err + size, err := util.SizeOfPath(c.config.Rootfs) + return int64(size), err } container, err := c.runtime.store.Container(c.ID()) diff --git a/libpod/volume.go b/libpod/volume.go index f79ceaa87..bffafdc15 100644 --- a/libpod/volume.go +++ b/libpod/volume.go @@ -1,13 +1,12 @@ package libpod import ( - "os" - "path/filepath" "time" "github.com/containers/podman/v4/libpod/define" "github.com/containers/podman/v4/libpod/lock" "github.com/containers/podman/v4/libpod/plugin" + "github.com/containers/podman/v4/pkg/util" ) // Volume is a libpod named volume. @@ -93,14 +92,7 @@ func (v *Volume) Name() string { // Returns the size on disk of volume func (v *Volume) Size() (uint64, error) { - var size uint64 - err := filepath.Walk(v.config.MountPoint, func(path string, info os.FileInfo, err error) error { - if err == nil && !info.IsDir() { - size += (uint64)(info.Size()) - } - return err - }) - return size, err + return util.SizeOfPath(v.config.MountPoint) } // Driver retrieves the volume's driver. diff --git a/pkg/bindings/images/build.go b/pkg/bindings/images/build.go index e1b427742..f6739b7ca 100644 --- a/pkg/bindings/images/build.go +++ b/pkg/bindings/images/build.go @@ -7,6 +7,7 @@ import ( "encoding/json" "fmt" "io" + "io/fs" "io/ioutil" "net/http" "net/url" @@ -557,14 +558,14 @@ func nTar(excludes []string, sources ...string) (io.ReadCloser, error) { merr = multierror.Append(merr, err) return } - err = filepath.Walk(s, func(path string, info os.FileInfo, err error) error { + err = filepath.WalkDir(s, func(path string, d fs.DirEntry, err error) error { if err != nil { return err } // check if what we are given is an empty dir, if so then continue w/ it. Else return. // if we are given a file or a symlink, we do not want to exclude it. - if info.IsDir() && s == path { + if d.IsDir() && s == path { var p *os.File p, err = os.Open(path) if err != nil { @@ -588,7 +589,11 @@ func nTar(excludes []string, sources ...string) (io.ReadCloser, error) { return nil } - if info.Mode().IsRegular() { // add file item + if d.Type().IsRegular() { // add file item + info, err := d.Info() + if err != nil { + return err + } di, isHardLink := checkHardLink(info) if err != nil { return err @@ -624,7 +629,11 @@ func nTar(excludes []string, sources ...string) (io.ReadCloser, error) { seen[di] = name } return err - } else if info.Mode().IsDir() { // add folders + } else if d.IsDir() { // add folders + info, err := d.Info() + if err != nil { + return err + } hdr, lerr := tar.FileInfoHeader(info, name) if lerr != nil { return lerr @@ -634,11 +643,15 @@ func nTar(excludes []string, sources ...string) (io.ReadCloser, error) { if lerr := tw.WriteHeader(hdr); lerr != nil { return lerr } - } else if info.Mode()&os.ModeSymlink != 0 { // add symlinks as it, not content + } else if d.Type()&os.ModeSymlink != 0 { // add symlinks as it, not content link, err := os.Readlink(path) if err != nil { return err } + info, err := d.Info() + if err != nil { + return err + } hdr, lerr := tar.FileInfoHeader(info, link) if lerr != nil { return lerr diff --git a/pkg/domain/infra/abi/system.go b/pkg/domain/infra/abi/system.go index d12d14c1f..4361821d5 100644 --- a/pkg/domain/infra/abi/system.go +++ b/pkg/domain/infra/abi/system.go @@ -6,7 +6,6 @@ import ( "net/url" "os" "os/exec" - "path/filepath" "github.com/containers/common/pkg/cgroups" "github.com/containers/common/pkg/config" @@ -269,7 +268,7 @@ func (ic *ContainerEngine) SystemDf(ctx context.Context, options entities.System } dfVolumes := make([]*entities.SystemDfVolumeReport, 0, len(vols)) - var reclaimableSize int64 + var reclaimableSize uint64 for _, v := range vols { var consInUse int mountPoint, err := v.MountPoint() @@ -282,7 +281,7 @@ func (ic *ContainerEngine) SystemDf(ctx context.Context, options entities.System // TODO: fix this. continue } - volSize, err := sizeOfPath(mountPoint) + volSize, err := util.SizeOfPath(mountPoint) if err != nil { return nil, err } @@ -301,8 +300,8 @@ func (ic *ContainerEngine) SystemDf(ctx context.Context, options entities.System report := entities.SystemDfVolumeReport{ VolumeName: v.Name(), Links: consInUse, - Size: volSize, - ReclaimableSize: reclaimableSize, + Size: int64(volSize), + ReclaimableSize: int64(reclaimableSize), } dfVolumes = append(dfVolumes, &report) } @@ -313,19 +312,6 @@ func (ic *ContainerEngine) SystemDf(ctx context.Context, options entities.System }, nil } -// sizeOfPath determines the file usage of a given path. it was called volumeSize in v1 -// and now is made to be generic and take a path instead of a libpod volume -func sizeOfPath(path string) (int64, error) { - var size int64 - err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { - if err == nil && !info.IsDir() { - size += info.Size() - } - return err - }) - return size, err -} - func (se *SystemEngine) Reset(ctx context.Context) error { return se.Libpod.Reset(ctx) } diff --git a/pkg/machine/ignition.go b/pkg/machine/ignition.go index b2dabb689..fe47437e3 100644 --- a/pkg/machine/ignition.go +++ b/pkg/machine/ignition.go @@ -6,6 +6,7 @@ package machine import ( "encoding/json" "fmt" + "io/fs" "io/ioutil" "net/url" "os" @@ -507,8 +508,8 @@ func getCerts(certsDir string, isDir bool) []File { ) if isDir { - err := filepath.Walk(certsDir, func(path string, info os.FileInfo, err error) error { - if err == nil && !info.IsDir() { + err := filepath.WalkDir(certsDir, func(path string, d fs.DirEntry, err error) error { + if err == nil && !d.IsDir() { certPath, err := filepath.Rel(certsDir, path) if err != nil { logrus.Warnf("%s", err) diff --git a/pkg/machine/qemu/machine.go b/pkg/machine/qemu/machine.go index ffc90b2a0..f04625781 100644 --- a/pkg/machine/qemu/machine.go +++ b/pkg/machine/qemu/machine.go @@ -895,10 +895,10 @@ func GetVMInfos() ([]*machine.ListResponse, error) { var listed []*machine.ListResponse - if err = filepath.Walk(vmConfigDir, func(path string, info os.FileInfo, err error) error { + if err = filepath.WalkDir(vmConfigDir, func(path string, d fs.DirEntry, err error) error { vm := new(MachineVM) - if strings.HasSuffix(info.Name(), ".json") { - fullPath := filepath.Join(vmConfigDir, info.Name()) + if strings.HasSuffix(d.Name(), ".json") { + fullPath := filepath.Join(vmConfigDir, d.Name()) b, err := ioutil.ReadFile(fullPath) if err != nil { return err diff --git a/pkg/machine/wsl/machine.go b/pkg/machine/wsl/machine.go index 5b0c757f0..5128fa313 100644 --- a/pkg/machine/wsl/machine.go +++ b/pkg/machine/wsl/machine.go @@ -8,6 +8,7 @@ import ( "encoding/json" "fmt" "io" + "io/fs" "io/ioutil" "net/url" "os" @@ -1175,10 +1176,10 @@ func GetVMInfos() ([]*machine.ListResponse, error) { var listed []*machine.ListResponse - if err = filepath.Walk(vmConfigDir, func(path string, info os.FileInfo, err error) error { + if err = filepath.WalkDir(vmConfigDir, func(path string, d fs.DirEntry, err error) error { vm := new(MachineVM) - if strings.HasSuffix(info.Name(), ".json") { - fullPath := filepath.Join(vmConfigDir, info.Name()) + if strings.HasSuffix(d.Name(), ".json") { + fullPath := filepath.Join(vmConfigDir, d.Name()) b, err := ioutil.ReadFile(fullPath) if err != nil { return err diff --git a/pkg/specgen/generate/config_linux.go b/pkg/specgen/generate/config_linux.go index 35d7f0252..8f83fc09b 100644 --- a/pkg/specgen/generate/config_linux.go +++ b/pkg/specgen/generate/config_linux.go @@ -2,6 +2,7 @@ package generate import ( "fmt" + "io/fs" "io/ioutil" "os" "path" @@ -101,8 +102,8 @@ func DevicesFromPath(g *generate.Generator, devicePath string) error { } // mount the internal devices recursively - if err := filepath.Walk(resolvedDevicePath, func(dpath string, f os.FileInfo, e error) error { - if f.Mode()&os.ModeDevice == os.ModeDevice { + if err := filepath.WalkDir(resolvedDevicePath, func(dpath string, d fs.DirEntry, e error) error { + if d.Type()&os.ModeDevice == os.ModeDevice { found = true device := fmt.Sprintf("%s:%s", dpath, filepath.Join(dest, strings.TrimPrefix(dpath, src))) if devmode != "" { diff --git a/pkg/util/utils.go b/pkg/util/utils.go index 334a44a88..b89978601 100644 --- a/pkg/util/utils.go +++ b/pkg/util/utils.go @@ -3,6 +3,7 @@ package util import ( "encoding/json" "fmt" + "io/fs" "math" "os" "os/user" @@ -731,3 +732,20 @@ func LookupUser(name string) (*user.User, error) { } return user.Lookup(name) } + +// SizeOfPath determines the file usage of a given path. it was called volumeSize in v1 +// and now is made to be generic and take a path instead of a libpod volume +func SizeOfPath(path string) (uint64, error) { + var size uint64 + err := filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error { + if err == nil && !d.IsDir() { + info, err := d.Info() + if err != nil { + return err + } + size += uint64(info.Size()) + } + return err + }) + return size, err +} diff --git a/pkg/util/utils_linux.go b/pkg/util/utils_linux.go index 1cffab19d..0b21bf3c5 100644 --- a/pkg/util/utils_linux.go +++ b/pkg/util/utils_linux.go @@ -2,6 +2,7 @@ package util import ( "fmt" + "io/fs" "os" "path/filepath" "syscall" @@ -23,17 +24,21 @@ func GetContainerPidInformationDescriptors() ([]string, error) { // Symlinks to nodes are ignored. func FindDeviceNodes() (map[string]string, error) { nodes := make(map[string]string) - err := filepath.Walk("/dev", func(path string, info os.FileInfo, err error) error { + err := filepath.WalkDir("/dev", func(path string, d fs.DirEntry, err error) error { if err != nil { logrus.Warnf("Error descending into path %s: %v", path, err) return filepath.SkipDir } // If we aren't a device node, do nothing. - if info.Mode()&(os.ModeDevice|os.ModeCharDevice) == 0 { + if d.Type()&(os.ModeDevice|os.ModeCharDevice) == 0 { return nil } + info, err := d.Info() + if err != nil { + return err + } // We are a device node. Get major/minor. sysstat, ok := info.Sys().(*syscall.Stat_t) if !ok { diff --git a/test/e2e/pod_rm_test.go b/test/e2e/pod_rm_test.go index 7a0d97d28..dbb2d6d13 100644 --- a/test/e2e/pod_rm_test.go +++ b/test/e2e/pod_rm_test.go @@ -2,6 +2,7 @@ package integration import ( "fmt" + "io/fs" "io/ioutil" "os" "path/filepath" @@ -46,14 +47,14 @@ var _ = Describe("Podman pod rm", func() { Expect(result).Should(Exit(0)) // Also check that we don't leak cgroups - err := filepath.Walk("/sys/fs/cgroup", func(path string, info os.FileInfo, err error) error { + err := filepath.WalkDir("/sys/fs/cgroup", func(path string, d fs.DirEntry, err error) error { if err != nil { return err } - if !info.IsDir() { + if !d.IsDir() { Expect(err).To(BeNil()) } - if strings.Contains(info.Name(), podid) { + if strings.Contains(d.Name(), podid) { return fmt.Errorf("leaking cgroup path %s", path) } return nil |