Skip to content

Commit efc9eff

Browse files
craig[bot]nicktrav
andcommitted
Merge #155196
155196: server,security: remove some deprecated ioutil usages r=cthumuluru-crdb a=nicktrav The `ioutil` package is deprecated. Update callsites to a more suitable alternative. Fixes #92861. Release note: None. Epic: None. Co-authored-by: Nick Travers <travers@cockroachlabs.com>
2 parents b739945 + 0faffbe commit efc9eff

File tree

5 files changed

+53
-13
lines changed

5 files changed

+53
-13
lines changed

build/bazelutil/nogo_config.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -772,11 +772,6 @@
772772
"pkg/.*_generated\\.go$": "generated code",
773773
"pkg/roachprod/vm/aws/embedded\\.go$": "generated code",
774774
"pkg/security/securitytest/embedded\\.go$": "generated code",
775-
"pkg/security/securityassets/security_assets.go$": "excluded until all uses of io/ioutil are replaced",
776-
"pkg/security/securitytest/securitytest.go$": "excluded until all uses of io/ioutil are replaced",
777-
"pkg/server/dumpstore/dumpstore.go$": "excluded until all uses of io/ioutil are replaced",
778-
"pkg/server/dumpstore/dumpstore_test.go$": "excluded until all uses of io/ioutil are replaced",
779-
"pkg/server/profiler/profilestore_test.go$": "excluded until all uses of io/ioutil are replaced",
780775
"pkg/util/bulk/tracing_aggregator.go$":"temporary exclusion until #100438 is resolved",
781776
"pkg/util/log/file_api.go$": "excluded until all uses of io/ioutil are replaced",
782777
"pkg/build/bazel/bazel\\.go$": "Runfile function deprecated"

pkg/security/securityassets/security_assets.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
package securityassets
77

88
import (
9-
"io/ioutil"
9+
"io/fs"
1010
"os"
1111

1212
"github.com/cockroachdb/errors/oserror"
@@ -21,7 +21,7 @@ type Loader struct {
2121

2222
// defaultLoader uses real filesystem calls.
2323
var defaultLoader = Loader{
24-
ReadDir: ioutil.ReadDir,
24+
ReadDir: readDir,
2525
ReadFile: os.ReadFile,
2626
Stat: os.Stat,
2727
}
@@ -55,3 +55,23 @@ func (al Loader) FileExists(filename string) (bool, error) {
5555
}
5656
return true, nil
5757
}
58+
59+
// readDir reads the directory named by dirname and returns a list of
60+
// fs.FileInfo for the directory's contents, sorted by filename. If an error
61+
// occurs reading the directory, ReadDir returns no directory entries along with
62+
// the error.
63+
func readDir(dirname string) ([]os.FileInfo, error) {
64+
entries, err := os.ReadDir(dirname)
65+
if err != nil {
66+
return nil, err
67+
}
68+
infos := make([]fs.FileInfo, 0, len(entries))
69+
for _, entry := range entries {
70+
info, err := entry.Info()
71+
if err != nil {
72+
return nil, err
73+
}
74+
infos = append(infos, info)
75+
}
76+
return infos, nil
77+
}

pkg/server/dumpstore/dumpstore.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package dumpstore
77

88
import (
99
"context"
10-
"io/ioutil"
10+
"io/fs"
1111
"os"
1212
"path/filepath"
1313
"time"
@@ -85,11 +85,20 @@ func (s *DumpStore) GetFullPath(fileName string) string {
8585
func (s *DumpStore) GC(ctx context.Context, now time.Time, dumper Dumper) {
8686
// NB: ioutil.ReadDir sorts the file names in ascending order.
8787
// This brings the oldest files first.
88-
files, err := ioutil.ReadDir(s.dir)
88+
entries, err := os.ReadDir(s.dir)
8989
if err != nil {
9090
log.Dev.Warningf(ctx, "%v", err)
9191
return
9292
}
93+
files := make([]fs.FileInfo, 0, len(entries))
94+
for _, entry := range entries {
95+
info, err := entry.Info()
96+
if err != nil {
97+
log.Dev.Warningf(ctx, "%v", err)
98+
return
99+
}
100+
files = append(files, info)
101+
}
93102

94103
maxS := s.maxCombinedFileSizeSetting.Get(&s.st.SV)
95104

pkg/server/dumpstore/dumpstore_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package dumpstore
88
import (
99
"context"
1010
"fmt"
11-
"io/ioutil"
11+
"io/fs"
1212
"os"
1313
"path/filepath"
1414
"strings"
@@ -187,9 +187,17 @@ func populate(t *testing.T, dirName string, fileNames []string, sizes []int64) [
187187
}
188188

189189
// Retrieve the file list for the remainder of the test.
190-
files, err := ioutil.ReadDir(dirName)
190+
entries, err := os.ReadDir(dirName)
191191
if err != nil {
192192
t.Fatal(err)
193193
}
194+
files := make([]fs.FileInfo, 0, len(entries))
195+
for _, entry := range entries {
196+
info, err := entry.Info()
197+
if err != nil {
198+
t.Fatal(err)
199+
}
200+
files = append(files, info)
201+
}
194202
return files
195203
}

pkg/server/profiler/profilestore_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package profiler
88
import (
99
"context"
1010
"fmt"
11-
"io/ioutil"
11+
"io/fs"
1212
"os"
1313
"path/filepath"
1414
"testing"
@@ -221,9 +221,17 @@ func populate(t *testing.T, dirName string, fileNames []string) []os.FileInfo {
221221
}
222222

223223
// Retrieve the file list for the remainder of the test.
224-
files, err := ioutil.ReadDir(dirName)
224+
entries, err := os.ReadDir(dirName)
225225
if err != nil {
226226
t.Fatal(err)
227227
}
228+
files := make([]fs.FileInfo, 0, len(entries))
229+
for _, entry := range entries {
230+
info, err := entry.Info()
231+
if err != nil {
232+
t.Fatal(err)
233+
}
234+
files = append(files, info)
235+
}
228236
return files
229237
}

0 commit comments

Comments
 (0)