Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <psteinhardt@gitlab.com>2021-09-20 15:12:48 +0300
committerPatrick Steinhardt <psteinhardt@gitlab.com>2021-09-20 17:23:54 +0300
commit4d63c8f0bfb5874ba2a9a68be0b3d73d9669cd09 (patch)
treed68ad0aa219b0655140ddaa40bb135fe643c5714
parentd54f7f25a1c8d3eba3ac66f62c56d599ca36986b (diff)
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.
-rw-r--r--internal/cache/walker.go2
-rw-r--r--internal/git/housekeeping/housekeeping.go7
-rw-r--r--internal/git/objectpool/fetch_test.go4
-rw-r--r--internal/git/objectpool/link.go3
-rw-r--r--internal/git/objectpool/link_test.go3
-rw-r--r--internal/git/packfile/packfile.go4
-rw-r--r--internal/git/quarantine/quarantine.go5
-rw-r--r--internal/git/quarantine/quarantine_test.go14
-rw-r--r--internal/gitaly/hook/custom.go3
-rw-r--r--internal/gitaly/maintenance/randomwalker.go11
-rw-r--r--internal/gitaly/service/operations/squash_test.go3
-rw-r--r--internal/gitaly/service/ref/pack_refs_test.go3
-rw-r--r--internal/safe/file_writer_test.go7
-rw-r--r--internal/tempdir/clean_test.go3
-rw-r--r--internal/x509/pool_darwin.go3
-rw-r--r--proto/go/internal/cmd/protoc-gen-gitaly/main.go3
16 files changed, 33 insertions, 45 deletions
diff --git a/internal/cache/walker.go b/internal/cache/walker.go
index cfb8cee7a..a71a2819c 100644
--- a/internal/cache/walker.go
+++ b/internal/cache/walker.go
@@ -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
diff --git a/internal/git/housekeeping/housekeeping.go b/internal/git/housekeeping/housekeeping.go
index b9d3fbe42..2a15d5c30 100644
--- a/internal/git/housekeeping/housekeeping.go
+++ b/internal/git/housekeeping/housekeeping.go
@@ -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
}
diff --git a/internal/git/objectpool/fetch_test.go b/internal/git/objectpool/fetch_test.go
index d0dbb2fb7..fd7b11502 100644
--- a/internal/git/objectpool/fetch_test.go
+++ b/internal/git/objectpool/fetch_test.go
@@ -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
diff --git a/internal/git/objectpool/link.go b/internal/git/objectpool/link.go
index fda3a8806..710fbd11c 100644
--- a/internal/git/objectpool/link.go
+++ b/internal/git/objectpool/link.go
@@ -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
}
diff --git a/internal/git/objectpool/link_test.go b/internal/git/objectpool/link_test.go
index 46ec094d7..c49cdc3cc 100644
--- a/internal/git/objectpool/link_test.go
+++ b/internal/git/objectpool/link_test.go
@@ -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
diff --git a/internal/git/packfile/packfile.go b/internal/git/packfile/packfile.go
index 63716a18c..67015aa20 100644
--- a/internal/git/packfile/packfile.go
+++ b/internal/git/packfile/packfile.go
@@ -1,14 +1,14 @@
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
}
diff --git a/internal/git/quarantine/quarantine.go b/internal/git/quarantine/quarantine.go
index 38cef7e9d..7638a606e 100644
--- a/internal/git/quarantine/quarantine.go
+++ b/internal/git/quarantine/quarantine.go
@@ -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())
})
diff --git a/internal/git/quarantine/quarantine_test.go b/internal/git/quarantine/quarantine_test.go
index 5896d73f6..cf6403390 100644
--- a/internal/git/quarantine/quarantine_test.go
+++ b/internal/git/quarantine/quarantine_test.go
@@ -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)
diff --git a/internal/gitaly/hook/custom.go b/internal/gitaly/hook/custom.go
index 63d2fa3d1..cbbf10c1b 100644
--- a/internal/gitaly/hook/custom.go
+++ b/internal/gitaly/hook/custom.go
@@ -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
diff --git a/internal/gitaly/maintenance/randomwalker.go b/internal/gitaly/maintenance/randomwalker.go
index 6424b5432..eed27dbda 100644
--- a/internal/gitaly/maintenance/randomwalker.go
+++ b/internal/gitaly/maintenance/randomwalker.go
@@ -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] })
}
diff --git a/internal/gitaly/service/operations/squash_test.go b/internal/gitaly/service/operations/squash_test.go
index 02e833c5a..bba554088 100644
--- a/internal/gitaly/service/operations/squash_test.go
+++ b/internal/gitaly/service/operations/squash_test.go
@@ -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") {
diff --git a/internal/gitaly/service/ref/pack_refs_test.go b/internal/gitaly/service/ref/pack_refs_test.go
index 3863b34ce..c9f068df6 100644
--- a/internal/gitaly/service/ref/pack_refs_test.go
+++ b/internal/gitaly/service/ref/pack_refs_test.go
@@ -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))
diff --git a/internal/safe/file_writer_test.go b/internal/safe/file_writer_test.go
index 1f2e6cd60..c31420a7f 100644
--- a/internal/safe/file_writer_test.go
+++ b/internal/safe/file_writer_test.go
@@ -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
}
diff --git a/internal/tempdir/clean_test.go b/internal/tempdir/clean_test.go
index 46bf58a6d..3d3e16c13 100644
--- a/internal/tempdir/clean_test.go
+++ b/internal/tempdir/clean_test.go
@@ -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))
diff --git a/internal/x509/pool_darwin.go b/internal/x509/pool_darwin.go
index bb69533f3..851b16d72 100644
--- a/internal/x509/pool_darwin.go
+++ b/internal/x509/pool_darwin.go
@@ -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
}
diff --git a/proto/go/internal/cmd/protoc-gen-gitaly/main.go b/proto/go/internal/cmd/protoc-gen-gitaly/main.go
index a364b68ee..efe8a312e 100644
--- a/proto/go/internal/cmd/protoc-gen-gitaly/main.go
+++ b/proto/go/internal/cmd/protoc-gen-gitaly/main.go
@@ -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)
}