Skip to content
Snippets Groups Projects
Commit 4d63c8f0 authored by Patrick Steinhardt's avatar Patrick Steinhardt
Browse files

global: Replace trivial cases of deprecated `ioutil.ReadDir()`

With Go 1.16, the ioutil package was deprecated. In addition to being
moved into the os package, `ioutil.ReadDir()` was also changed to not
stat(3P) all dir entries anymore. As a result, the caller now has to
do so manually. This is a performance improvement in some cases where
the caller didn't require any of the file information, but really only
wanted to read the directory's entries.

Adapt trivial usecases of `ioutil.ReadDir()` which do not require this
information with usage of `os.ReadDir()`. This leaves a few callsites of
the old `ioutil.ReadDir()` function for future conversion.
parent d54f7f25
No related branches found
No related tags found
Loading
Showing
with 33 additions and 45 deletions
......@@ -65,7 +65,7 @@ func (c *DiskCache) cleanWalk(path string) error {
c.walkerRemovalTotal.Inc()
}
files, err := ioutil.ReadDir(path)
files, err := os.ReadDir(path)
if err != nil {
if os.IsNotExist(err) {
return nil
......
......@@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
......@@ -321,7 +320,7 @@ func removeRefEmptyDirs(ctx context.Context, repository *localrepo.Repo) error {
// we never want to delete the actual "refs" directory, so we start the
// recursive functions for each subdirectory
entries, err := ioutil.ReadDir(repoRefsPath)
entries, err := os.ReadDir(repoRefsPath)
if err != nil {
return err
}
......@@ -356,7 +355,7 @@ func removeEmptyDirs(ctx context.Context, target string) error {
return err
}
entries, err := ioutil.ReadDir(target)
entries, err := os.ReadDir(target)
switch {
case os.IsNotExist(err):
return nil // race condition: someone else deleted it first
......@@ -382,7 +381,7 @@ func removeEmptyDirs(ctx context.Context, target string) error {
}
// recheck entries now that we have potentially removed some dirs
entries, err = ioutil.ReadDir(target)
entries, err = os.ReadDir(target)
if err != nil && !os.IsNotExist(err) {
return err
}
......
......@@ -2,7 +2,7 @@ package objectpool
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
......@@ -138,7 +138,7 @@ func TestFetchFromOriginBitmapHashCache(t *testing.T) {
require.NoError(t, pool.FetchFromOrigin(ctx, testRepo), "seed pool")
packDir := filepath.Join(pool.FullPath(), "objects/pack")
packEntries, err := ioutil.ReadDir(packDir)
packEntries, err := os.ReadDir(packDir)
require.NoError(t, err)
var bitmap string
......
......@@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
......@@ -134,7 +133,7 @@ func (o *ObjectPool) removeMemberBitmaps(repo repository.GitRepo) error {
func getBitmaps(repoPath string) ([]string, error) {
packDir := filepath.Join(repoPath, "objects/pack")
entries, err := ioutil.ReadDir(packDir)
entries, err := os.ReadDir(packDir)
if err != nil {
return nil, err
}
......
......@@ -2,7 +2,6 @@ package objectpool
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"strings"
......@@ -116,7 +115,7 @@ func TestLinkRemoveBitmap(t *testing.T) {
}
func listBitmaps(t *testing.T, repoPath string) []string {
entries, err := ioutil.ReadDir(filepath.Join(repoPath, "objects/pack"))
entries, err := os.ReadDir(filepath.Join(repoPath, "objects/pack"))
require.NoError(t, err)
var bitmaps []string
......
package packfile
import (
"io/ioutil"
"os"
"path/filepath"
)
// List returns the packfiles in objDir.
func List(objDir string) ([]string, error) {
packDir := filepath.Join(objDir, "pack")
entries, err := ioutil.ReadDir(packDir)
entries, err := os.ReadDir(packDir)
if err != nil {
return nil, err
}
......
......@@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
......@@ -87,7 +86,7 @@ func (d *Dir) Migrate() error {
}
func migrate(sourcePath, targetPath string) error {
entries, err := ioutil.ReadDir(sourcePath)
entries, err := os.ReadDir(sourcePath)
if err != nil {
return fmt.Errorf("reading directory: %w", err)
}
......@@ -157,7 +156,7 @@ func finalizeObjectFile(sourcePath, targetPath string) error {
// sortEntries sorts packfiles and their associated metafiles such that we copy them over in the
// correct order.
func sortEntries(entries []os.FileInfo) {
func sortEntries(entries []os.DirEntry) {
sort.SliceStable(entries, func(i, j int) bool {
return packCopyPriority(entries[i].Name()) < packCopyPriority(entries[j].Name())
})
......
......@@ -372,12 +372,12 @@ func TestFinalizeObjectFile(t *testing.T) {
})
}
type mockFileInfo struct {
os.FileInfo
type mockDirEntry struct {
os.DirEntry
name string
}
func (e mockFileInfo) Name() string {
func (e mockDirEntry) Name() string {
return e.name
}
......@@ -486,14 +486,14 @@ func TestSortEntries(t *testing.T) {
},
} {
t.Run(tc.desc, func(t *testing.T) {
var actualEntries []os.FileInfo
var actualEntries []os.DirEntry
for _, entry := range tc.entries {
actualEntries = append(actualEntries, mockFileInfo{name: entry})
actualEntries = append(actualEntries, mockDirEntry{name: entry})
}
var expectedEntries []os.FileInfo
var expectedEntries []os.DirEntry
for _, entry := range tc.expected {
expectedEntries = append(expectedEntries, mockFileInfo{name: entry})
expectedEntries = append(expectedEntries, mockDirEntry{name: entry})
}
sortEntries(actualEntries)
......
......@@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
......@@ -83,7 +82,7 @@ func (m *GitLabHookManager) newCustomHooksExecutor(repo *gitalypb.Repository, ho
// valid if `isValidHook()` would return `true`. Matching hooks are sorted by
// filename.
func findHooks(dir string) ([]string, error) {
fis, err := ioutil.ReadDir(dir)
fis, err := os.ReadDir(dir)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
......
......@@ -2,7 +2,6 @@ package maintenance
import (
"errors"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
......@@ -12,7 +11,7 @@ var errIterOver = errors.New("random walker at end")
type stackFrame struct {
name string
entries []os.FileInfo
entries []os.DirEntry
}
// randomWalker is a filesystem walker which traverses a directory hierarchy in depth-first order,
......@@ -36,19 +35,19 @@ func newRandomWalker(root string, r *rand.Rand) *randomWalker {
// next returns the next file. Traversal happens in depth-first order, where each directory's
// entities are traversed in random order. If there are no more files to iterate, `errIterOver` is
// returned.
func (r *randomWalker) next() (os.FileInfo, string, error) {
func (r *randomWalker) next() (os.DirEntry, string, error) {
if r.pendingDir != "" {
// Reset pendingDir before returning the error such that the caller can continue if
// he doesn't care e.g. for the directory not existing.
pendingDir := r.pendingDir
r.pendingDir = ""
entries, err := ioutil.ReadDir(pendingDir)
entries, err := os.ReadDir(pendingDir)
if err != nil {
return nil, pendingDir, err
}
shuffleFileInfos(r.rand, entries)
shuffleDirEntries(r.rand, entries)
r.stack = append(r.stack, stackFrame{
name: pendingDir,
entries: entries,
......@@ -83,7 +82,7 @@ func (r *randomWalker) next() (os.FileInfo, string, error) {
}
}
func shuffleFileInfos(randSrc *rand.Rand, s []os.FileInfo) {
func shuffleDirEntries(randSrc *rand.Rand, s []os.DirEntry) {
randSrc.Shuffle(len(s), func(i, j int) { s[i], s[j] = s[j], s[i] })
}
......
......@@ -2,7 +2,6 @@ package operations
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
......@@ -141,7 +140,7 @@ func authorFromUser(user *gitalypb.User, seconds int64) *gitalypb.CommitAuthor {
func ensureSplitIndexExists(t *testing.T, cfg config.Cfg, repoDir string) bool {
gittest.Exec(t, cfg, "-C", repoDir, "update-index", "--add")
fis, err := ioutil.ReadDir(repoDir)
fis, err := os.ReadDir(repoDir)
require.NoError(t, err)
for _, fi := range fis {
if strings.HasPrefix(fi.Name(), "sharedindex") {
......
......@@ -3,7 +3,6 @@ package ref
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
......@@ -38,7 +37,7 @@ func TestPackRefsSuccessfulRequest(t *testing.T) {
_, err := client.PackRefs(ctx, &gitalypb.PackRefsRequest{Repository: repoProto})
require.NoError(t, err)
files, err := ioutil.ReadDir(filepath.Join(repoPath, "refs/heads"))
files, err := os.ReadDir(filepath.Join(repoPath, "refs/heads"))
require.NoError(t, err)
assert.Len(t, files, 0, "git pack-refs --all should have packed all refs in refs/heads")
assert.Equal(t, packedRefs+newBranches, linesInPackfile(t, repoPath), fmt.Sprintf("should have added %d new lines to the packfile", newBranches))
......
......@@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sync"
......@@ -33,7 +32,7 @@ func TestFileWriter_successful(t *testing.T) {
writtenContents := testhelper.MustReadFile(t, filePath)
require.Equal(t, fileContents, string(writtenContents))
filesInTempDir, err := ioutil.ReadDir(dir)
filesInTempDir, err := os.ReadDir(dir)
require.NoError(t, err)
require.Len(t, filesInTempDir, 1)
require.Equal(t, filepath.Base(filePath), filesInTempDir[0].Name())
......@@ -83,7 +82,7 @@ func TestFileWriter_race(t *testing.T) {
wg.Wait()
require.FileExists(t, filePath)
filesInTempDir, err := ioutil.ReadDir(dir)
filesInTempDir, err := os.ReadDir(dir)
require.NoError(t, err)
require.Len(t, filesInTempDir, 1, "make sure no other files were written")
}
......@@ -128,7 +127,7 @@ func TestFileWriter_commitBeforeClose(t *testing.T) {
}
func dirEmpty(t testing.TB, dirPath string) bool {
infos, err := ioutil.ReadDir(dirPath)
infos, err := os.ReadDir(dirPath)
require.NoError(t, err)
return len(infos) == 0
}
......@@ -2,7 +2,6 @@ package tempdir
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
......@@ -124,7 +123,7 @@ func assertEntries(t *testing.T, locator storage.Locator, storage config.Storage
root, err := locator.TempDir(storage.Name)
require.NoError(t, err)
foundEntries, err := ioutil.ReadDir(root)
foundEntries, err := os.ReadDir(root)
require.NoError(t, err)
require.Len(t, foundEntries, len(entries))
......
......@@ -3,7 +3,6 @@ package x509
import (
"crypto/x509"
"errors"
"io/ioutil"
"os"
"path/filepath"
)
......@@ -24,7 +23,7 @@ func SystemCertPool() (*x509.CertPool, error) {
}
if d := os.Getenv(SSLCertDir); len(d) > 0 {
entries, err := ioutil.ReadDir(d)
entries, err := os.ReadDir(d)
if err != nil {
return nil, err
}
......
......@@ -55,7 +55,6 @@ import (
"fmt"
"go/format"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
......@@ -156,7 +155,7 @@ func generateProtolistGo(req *pluginpb.CodeGeneratorRequest) error {
return fmt.Errorf("failed to get absolute path for %s: %v", gitalyProtoDir, err)
}
files, err := ioutil.ReadDir(gitalyProtoDir)
files, err := os.ReadDir(gitalyProtoDir)
if err != nil {
return fmt.Errorf("failed to read %s: %v", gitalyProtoDir, err)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment