Skip to content

Commit 71f570d

Browse files
committed
remove usage of deprecated functions
Signed-off-by: Tim Ramlot <[email protected]>
1 parent 3c1ecb0 commit 71f570d

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

third_party/util/atomic_writer.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package util
1616
import (
1717
"bytes"
1818
"fmt"
19-
"io/ioutil"
2019
"os"
2120
"path"
2221
"path/filepath"
@@ -147,7 +146,7 @@ func (w *AtomicWriter) Write(payload map[string]FileProjection) error {
147146
}
148147
oldTsPath := filepath.Join(w.targetDir, oldTsDir)
149148

150-
var pathsToRemove sets.String
149+
var pathsToRemove sets.Set[string]
151150
// if there was no old version, there's nothing to remove
152151
if len(oldTsDir) != 0 {
153152
// (3)
@@ -307,7 +306,7 @@ func shouldWriteFile(path string, content []byte) (bool, error) {
307306
return true, nil
308307
}
309308

310-
contentOnFs, err := ioutil.ReadFile(path)
309+
contentOnFs, err := os.ReadFile(path)
311310
if err != nil {
312311
return false, err
313312
}
@@ -318,8 +317,8 @@ func shouldWriteFile(path string, content []byte) (bool, error) {
318317
// pathsToRemove walks the current version of the data directory and
319318
// determines which paths should be removed (if any) after the payload is
320319
// written to the target directory.
321-
func (w *AtomicWriter) pathsToRemove(payload map[string]FileProjection, oldTsDir string) (sets.String, error) {
322-
paths := sets.NewString()
320+
func (w *AtomicWriter) pathsToRemove(payload map[string]FileProjection, oldTsDir string) (sets.Set[string], error) {
321+
paths := sets.New[string]()
323322
visitor := func(path string, info os.FileInfo, err error) error {
324323
relativePath := strings.TrimPrefix(path, oldTsDir)
325324
relativePath = strings.TrimPrefix(relativePath, string(os.PathSeparator))
@@ -337,9 +336,9 @@ func (w *AtomicWriter) pathsToRemove(payload map[string]FileProjection, oldTsDir
337336
} else if err != nil {
338337
return nil, err
339338
}
340-
klog.V(5).Infof("%s: current paths: %+v", w.targetDir, paths.List())
339+
klog.V(5).Infof("%s: current paths: %+v", w.targetDir, paths.UnsortedList())
341340

342-
newPaths := sets.NewString()
341+
newPaths := sets.New[string]()
343342
for file := range payload {
344343
// add all subpaths for the payload to the set of new paths
345344
// to avoid attempting to remove non-empty dirs
@@ -349,7 +348,7 @@ func (w *AtomicWriter) pathsToRemove(payload map[string]FileProjection, oldTsDir
349348
subPath = strings.TrimSuffix(subPath, string(os.PathSeparator))
350349
}
351350
}
352-
klog.V(5).Infof("%s: new paths: %+v", w.targetDir, newPaths.List())
351+
klog.V(5).Infof("%s: new paths: %+v", w.targetDir, newPaths.UnsortedList())
353352

354353
result := paths.Difference(newPaths)
355354
klog.V(5).Infof("%s: paths to remove: %+v", w.targetDir, result)
@@ -359,7 +358,7 @@ func (w *AtomicWriter) pathsToRemove(payload map[string]FileProjection, oldTsDir
359358

360359
// newTimestampDir creates a new timestamp directory
361360
func (w *AtomicWriter) newTimestampDir() (string, error) {
362-
tsDir, err := ioutil.TempDir(w.targetDir, time.Now().UTC().Format("..2006_01_02_15_04_05."))
361+
tsDir, err := os.MkdirTemp(w.targetDir, time.Now().UTC().Format("..2006_01_02_15_04_05."))
363362
if err != nil {
364363
klog.Errorf("%s: unable to create new temp directory: %v", w.logContext, err)
365364
return "", err
@@ -391,11 +390,11 @@ func (w *AtomicWriter) writePayloadToDir(payload map[string]FileProjection, dir
391390
return err
392391
}
393392

394-
if err := ioutil.WriteFile(fullPath, content, mode); err != nil {
393+
if err := os.WriteFile(fullPath, content, mode); err != nil {
395394
klog.Errorf("%s: unable to write file %s with mode %v: %v", w.logContext, fullPath, mode, err)
396395
return err
397396
}
398-
// Chmod is needed because ioutil.WriteFile() ends up calling
397+
// Chmod is needed because os.WriteFile() ends up calling
399398
// open(2) to create the file, so the final mode used is "mode &
400399
// ~umask". But we want to make sure the specified mode is used
401400
// in the file no matter what the umask is.
@@ -450,7 +449,7 @@ func (w *AtomicWriter) createUserVisibleFiles(payload map[string]FileProjection)
450449

451450
// removeUserVisiblePaths removes the set of paths from the user-visible
452451
// portion of the writer's target directory.
453-
func (w *AtomicWriter) removeUserVisiblePaths(paths sets.String) error {
452+
func (w *AtomicWriter) removeUserVisiblePaths(paths sets.Set[string]) error {
454453
ps := string(os.PathSeparator)
455454
var lasterr error
456455
for p := range paths {

third_party/util/atomic_writer_test.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package util
1818

1919
import (
2020
"encoding/base64"
21-
"io/ioutil"
2221
"os"
2322
"path/filepath"
2423
"reflect"
@@ -130,7 +129,7 @@ func TestPathsToRemove(t *testing.T) {
130129
name string
131130
payload1 map[string]FileProjection
132131
payload2 map[string]FileProjection
133-
expected sets.String
132+
expected sets.Set[string]
134133
}{
135134
{
136135
name: "simple",
@@ -141,7 +140,7 @@ func TestPathsToRemove(t *testing.T) {
141140
payload2: map[string]FileProjection{
142141
"foo.txt": {Mode: 0644, Data: []byte("foo")},
143142
},
144-
expected: sets.NewString("bar.txt"),
143+
expected: sets.New("bar.txt"),
145144
},
146145
{
147146
name: "simple 2",
@@ -152,7 +151,7 @@ func TestPathsToRemove(t *testing.T) {
152151
payload2: map[string]FileProjection{
153152
"foo.txt": {Mode: 0644, Data: []byte("foo")},
154153
},
155-
expected: sets.NewString("zip/bar.txt", "zip"),
154+
expected: sets.New("zip/bar.txt", "zip"),
156155
},
157156
{
158157
name: "subdirs 1",
@@ -163,7 +162,7 @@ func TestPathsToRemove(t *testing.T) {
163162
payload2: map[string]FileProjection{
164163
"foo.txt": {Mode: 0644, Data: []byte("foo")},
165164
},
166-
expected: sets.NewString("zip/zap/bar.txt", "zip", "zip/zap"),
165+
expected: sets.New("zip/zap/bar.txt", "zip", "zip/zap"),
167166
},
168167
{
169168
name: "subdirs 2",
@@ -174,7 +173,7 @@ func TestPathsToRemove(t *testing.T) {
174173
payload2: map[string]FileProjection{
175174
"foo.txt": {Mode: 0644, Data: []byte("foo")},
176175
},
177-
expected: sets.NewString("zip/1/2/3/4/bar.txt", "zip", "zip/1", "zip/1/2", "zip/1/2/3", "zip/1/2/3/4"),
176+
expected: sets.New("zip/1/2/3/4/bar.txt", "zip", "zip/1", "zip/1/2", "zip/1/2/3", "zip/1/2/3/4"),
178177
},
179178
{
180179
name: "subdirs 3",
@@ -186,7 +185,7 @@ func TestPathsToRemove(t *testing.T) {
186185
payload2: map[string]FileProjection{
187186
"foo.txt": {Mode: 0644, Data: []byte("foo")},
188187
},
189-
expected: sets.NewString("zip/1/2/3/4/bar.txt", "zip", "zip/1", "zip/1/2", "zip/1/2/3", "zip/1/2/3/4", "zap", "zap/a", "zap/a/b", "zap/a/b/c", "zap/a/b/c/bar.txt"),
188+
expected: sets.New("zip/1/2/3/4/bar.txt", "zip", "zip/1", "zip/1/2", "zip/1/2/3", "zip/1/2/3/4", "zap", "zap/a", "zap/a/b", "zap/a/b/c", "zap/a/b/c/bar.txt"),
190189
},
191190
{
192191
name: "subdirs 4",
@@ -200,7 +199,7 @@ func TestPathsToRemove(t *testing.T) {
200199
"foo.txt": {Mode: 0644, Data: []byte("foo")},
201200
"zap/1/2/magic.txt": {Mode: 0644, Data: []byte("indigo")},
202201
},
203-
expected: sets.NewString("zap/1/2/3/4/bar.txt", "zap/1/2/3", "zap/1/2/3/4", "zap/1/2/3/4/bar.txt", "zap/1/2/c", "zap/1/2/c/bar.txt"),
202+
expected: sets.New("zap/1/2/3/4/bar.txt", "zap/1/2/3", "zap/1/2/3/4", "zap/1/2/3/4/bar.txt", "zap/1/2/c", "zap/1/2/c/bar.txt"),
204203
},
205204
{
206205
name: "subdirs 5",
@@ -213,7 +212,7 @@ func TestPathsToRemove(t *testing.T) {
213212
"foo.txt": {Mode: 0644, Data: []byte("foo")},
214213
"zap/1/2/magic.txt": {Mode: 0644, Data: []byte("indigo")},
215214
},
216-
expected: sets.NewString("zap/1/2/3/4/bar.txt", "zap/1/2/3", "zap/1/2/3/4", "zap/1/2/3/4/bar.txt", "zap/1/2/c", "zap/1/2/c/bar.txt"),
215+
expected: sets.New("zap/1/2/3/4/bar.txt", "zap/1/2/3", "zap/1/2/3/4", "zap/1/2/3/4/bar.txt", "zap/1/2/c", "zap/1/2/c/bar.txt"),
217216
},
218217
}
219218

@@ -762,7 +761,7 @@ func checkVolumeContents(targetDir, tcName string, payload map[string]FileProjec
762761
return nil
763762
}
764763

765-
content, err := ioutil.ReadFile(path)
764+
content, err := os.ReadFile(path)
766765
if err != nil {
767766
return err
768767
}
@@ -777,15 +776,21 @@ func checkVolumeContents(targetDir, tcName string, payload map[string]FileProjec
777776
return nil
778777
}
779778

780-
d, err := ioutil.ReadDir(targetDir)
779+
d, err := os.ReadDir(targetDir)
781780
if err != nil {
782781
t.Errorf("Unable to read dir %v: %v", targetDir, err)
783782
return
784783
}
785-
for _, info := range d {
786-
if strings.HasPrefix(info.Name(), "..") {
784+
for _, entry := range d {
785+
if strings.HasPrefix(entry.Name(), "..") {
787786
continue
788787
}
788+
789+
info, err := entry.Info()
790+
if err != nil {
791+
continue
792+
}
793+
789794
if info.Mode()&os.ModeSymlink != 0 {
790795
p := filepath.Join(targetDir, info.Name())
791796
actual, err := os.Readlink(p)
@@ -815,7 +820,7 @@ func TestValidatePayload(t *testing.T) {
815820
cases := []struct {
816821
name string
817822
payload map[string]FileProjection
818-
expected sets.String
823+
expected sets.Set[string]
819824
valid bool
820825
}{
821826
{
@@ -825,7 +830,7 @@ func TestValidatePayload(t *testing.T) {
825830
"bar": {},
826831
},
827832
valid: true,
828-
expected: sets.NewString("foo", "bar"),
833+
expected: sets.New("foo", "bar"),
829834
},
830835
{
831836
name: "payload with path length > 4096 is invalid",
@@ -868,11 +873,11 @@ func TestValidatePayload(t *testing.T) {
868873
"foo////bar": {},
869874
},
870875
valid: true,
871-
expected: sets.NewString("foo/bar"),
876+
expected: sets.New("foo/bar"),
872877
},
873878
}
874-
getPayloadPaths := func(payload map[string]FileProjection) sets.String {
875-
paths := sets.NewString()
879+
getPayloadPaths := func(payload map[string]FileProjection) sets.Set[string] {
880+
paths := sets.New[string]()
876881
for path := range payload {
877882
paths.Insert(path)
878883
}

0 commit comments

Comments
 (0)