Merge pull request #15737 from Juneezee/refactor/os.ReadDir

refactor: use `os.ReadDir` for lightweight directory reading
This commit is contained in:
OpenShift Merge Robot
2022-09-12 06:50:48 +02:00
committed by GitHub
11 changed files with 13 additions and 18 deletions

View File

@ -1671,7 +1671,7 @@ func (c *Container) mountNamedVolume(v *ContainerNamedVolume, mountpoint string)
// a bizarre issue where something copier.Get will ENOENT on // a bizarre issue where something copier.Get will ENOENT on
// empty directories and sometimes it will not. // empty directories and sometimes it will not.
// RHBZ#1928643 // RHBZ#1928643
srcContents, err := ioutil.ReadDir(srcDir) srcContents, err := os.ReadDir(srcDir)
if err != nil { if err != nil {
return nil, fmt.Errorf("error reading contents of source directory for copy up into volume %s: %w", vol.Name(), err) return nil, fmt.Errorf("error reading contents of source directory for copy up into volume %s: %w", vol.Name(), err)
} }
@ -1681,7 +1681,7 @@ func (c *Container) mountNamedVolume(v *ContainerNamedVolume, mountpoint string)
// If the volume is not empty, we should not copy up. // If the volume is not empty, we should not copy up.
volMount := vol.mountPoint() volMount := vol.mountPoint()
contents, err := ioutil.ReadDir(volMount) contents, err := os.ReadDir(volMount)
if err != nil { if err != nil {
return nil, fmt.Errorf("error listing contents of volume %s mountpoint when copying up from container %s: %w", vol.Name(), c.ID(), err) return nil, fmt.Errorf("error listing contents of volume %s mountpoint when copying up from container %s: %w", vol.Name(), c.ID(), err)
} }

View File

@ -2,7 +2,6 @@ package file
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -129,7 +128,7 @@ func (locks *FileLocks) DeallocateAllLocks() error {
if !locks.valid { if !locks.valid {
return fmt.Errorf("locks have already been closed: %w", syscall.EINVAL) return fmt.Errorf("locks have already been closed: %w", syscall.EINVAL)
} }
files, err := ioutil.ReadDir(locks.lockPath) files, err := os.ReadDir(locks.lockPath)
if err != nil { if err != nil {
return fmt.Errorf("error reading directory %s: %w", locks.lockPath, err) return fmt.Errorf("error reading directory %s: %w", locks.lockPath, err)
} }

View File

@ -2,7 +2,6 @@ package compat
import ( import (
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"os" "os"
goRuntime "runtime" goRuntime "runtime"
@ -198,7 +197,7 @@ func getRuntimes(configInfo *config.Config) map[string]docker.Runtime {
func getFdCount() (count int) { func getFdCount() (count int) {
count = -1 count = -1
if entries, err := ioutil.ReadDir("/proc/self/fd"); err == nil { if entries, err := os.ReadDir("/proc/self/fd"); err == nil {
count = len(entries) count = len(entries)
} }
return return

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"strconv" "strconv"
"sync" "sync"
@ -858,7 +857,7 @@ func makeExecConfig(options entities.ExecOptions, rt *libpod.Runtime) (*libpod.E
func checkExecPreserveFDs(options entities.ExecOptions) error { func checkExecPreserveFDs(options entities.ExecOptions) error {
if options.PreserveFDs > 0 { if options.PreserveFDs > 0 {
entries, err := ioutil.ReadDir("/proc/self/fd") entries, err := os.ReadDir("/proc/self/fd")
if err != nil { if err != nil {
return err return err
} }

View File

@ -869,7 +869,7 @@ func execTransferPodman(execUser *user.User, command []string, needToTag bool) (
func getSigFilename(sigStoreDirPath string) (string, error) { func getSigFilename(sigStoreDirPath string) (string, error) {
sigFileSuffix := 1 sigFileSuffix := 1
sigFiles, err := ioutil.ReadDir(sigStoreDirPath) sigFiles, err := os.ReadDir(sigStoreDirPath)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -4,7 +4,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/fs" "io/fs"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -119,7 +118,7 @@ func AddPrivilegedDevices(g *generate.Generator) error {
// based on getDevices from runc (libcontainer/devices/devices.go) // based on getDevices from runc (libcontainer/devices/devices.go)
func getDevices(path string) ([]spec.LinuxDevice, error) { func getDevices(path string) ([]spec.LinuxDevice, error) {
files, err := ioutil.ReadDir(path) files, err := os.ReadDir(path)
if err != nil { if err != nil {
if rootless.IsRootless() && os.IsPermission(err) { if rootless.IsRootless() && os.IsPermission(err) {
return nil, nil return nil, nil
@ -146,7 +145,7 @@ func getDevices(path string) ([]spec.LinuxDevice, error) {
} }
case f.Name() == "console": case f.Name() == "console":
continue continue
case f.Mode()&os.ModeSymlink != 0: case f.Type()&os.ModeSymlink != 0:
continue continue
} }

View File

@ -99,7 +99,7 @@ var _ = Describe("Podman Benchmark Suite", func() {
} }
totalMemoryInKb := func() (total uint64) { totalMemoryInKb := func() (total uint64) {
files, err := ioutil.ReadDir(timedir) files, err := os.ReadDir(timedir)
if err != nil { if err != nil {
Fail(fmt.Sprintf("Error reading timing dir: %v", err)) Fail(fmt.Sprintf("Error reading timing dir: %v", err))
} }

View File

@ -58,7 +58,7 @@ func checkDataVolumeContainer(pTest *PodmanTestIntegration, image, cont, dest, d
Expect(volList.OutputToStringArray()[0]).To(Equal(mntName)) Expect(volList.OutputToStringArray()[0]).To(Equal(mntName))
// Check the mount source directory // Check the mount source directory
files, err := ioutil.ReadDir(mntSource) files, err := os.ReadDir(mntSource)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
if data == "" { if data == "" {

View File

@ -1,7 +1,6 @@
package integration package integration
import ( import (
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -69,7 +68,7 @@ var _ = Describe("Podman image sign", func() {
session := podmanTest.Podman([]string{"image", "sign", "--all", "--directory", sigDir, "--sign-by", "foo@bar.com", "docker://library/alpine"}) session := podmanTest.Podman([]string{"image", "sign", "--all", "--directory", sigDir, "--sign-by", "foo@bar.com", "docker://library/alpine"})
session.WaitWithDefaultTimeout() session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0)) Expect(session).Should(Exit(0))
fInfos, err := ioutil.ReadDir(filepath.Join(sigDir, "library")) fInfos, err := os.ReadDir(filepath.Join(sigDir, "library"))
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(len(fInfos)).To(BeNumerically(">", 1), "len(fInfos)") Expect(len(fInfos)).To(BeNumerically(">", 1), "len(fInfos)")
}) })

View File

@ -332,7 +332,7 @@ var _ = Describe("Podman manifest", func() {
blobsDir := filepath.Join(dest, "blobs", "sha256") blobsDir := filepath.Join(dest, "blobs", "sha256")
blobs, err := ioutil.ReadDir(blobsDir) blobs, err := os.ReadDir(blobsDir)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
for _, f := range blobs { for _, f := range blobs {

View File

@ -78,7 +78,7 @@ var _ = Describe("Podman push", func() {
blobsDir := filepath.Join(bbdir, "blobs/sha256") blobsDir := filepath.Join(bbdir, "blobs/sha256")
blobs, err := ioutil.ReadDir(blobsDir) blobs, err := os.ReadDir(blobsDir)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
for _, f := range blobs { for _, f := range blobs {