Browse code

pkg/plugins: fix compatibility with go1.16

commit c55a4ac7795c7606b548b38e24673733481e2167 changed the ioutil utilities
to use the new os variants, per recommendation from the go 1.16 release notes:
https://golang.org/doc/go1.16#ioutil

> we encourage new code to use the new definitions in the io and os packages.
> Here is a list of the new locations of the names exported by io/ioutil:

However, the devil is in the detail, and io.ReadDir() is not a direct
replacement for ioutil.ReadDir();

> ReadDir => os.ReadDir (note: returns a slice of os.DirEntry rather than a slice of fs.FileInfo)

go1.16 added a io.FileInfoToDirEntry() utility to concert a DirEntry to
a FileInfo, but it's not available in go1.16

This patch copies the FileInfoToDirEntry code, and uses it for go1.16.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2021/08/28 06:52:39
Showing 3 changed files
... ...
@@ -3,7 +3,6 @@ package plugins // import "github.com/docker/docker/pkg/plugins"
3 3
 import (
4 4
 	"encoding/json"
5 5
 	"fmt"
6
-	"io/fs"
7 6
 	"net/url"
8 7
 	"os"
9 8
 	"path/filepath"
... ...
@@ -41,7 +40,7 @@ func Scan() ([]string, error) {
41 41
 				continue
42 42
 			}
43 43
 
44
-			entry = fs.FileInfoToDirEntry(fi)
44
+			entry = fileInfoToDirEntry(fi)
45 45
 		}
46 46
 
47 47
 		if entry.Type()&os.ModeSocket != 0 {
48 48
new file mode 100644
... ...
@@ -0,0 +1,8 @@
0
+//go:build go1.17
1
+// +build go1.17
2
+
3
+package plugins
4
+
5
+import "io/fs"
6
+
7
+var fileInfoToDirEntry = fs.FileInfoToDirEntry
0 8
new file mode 100644
... ...
@@ -0,0 +1,47 @@
0
+//go:build !go1.17
1
+// +build !go1.17
2
+
3
+// This code is taken from https://github.com/golang/go/blob/go1.17/src/io/fs/readdir.go#L49-L77
4
+// and provides the io/fs.FileInfoToDirEntry() utility for go1.16. Go 1.16 and up
5
+// provide a new implementation of ioutil.ReadDir() (in os.ReadDir()) that returns
6
+// an os.DirEntry instead of fs.FileInfo. go1.17 added the io/fs.FileInfoToDirEntry()
7
+// utility to allow existing uses of ReadDir() to get the old type. This utility
8
+// is not available in go1.16, so we copied it to assist the migration to os.ReadDir().
9
+
10
+// Copyright 2020 The Go Authors. All rights reserved.
11
+// Use of this source code is governed by a BSD-style
12
+// license that can be found in the LICENSE file.
13
+
14
+package plugins
15
+
16
+import "os"
17
+
18
+// dirInfo is a DirEntry based on a FileInfo.
19
+type dirInfo struct {
20
+	fileInfo os.FileInfo
21
+}
22
+
23
+func (di dirInfo) IsDir() bool {
24
+	return di.fileInfo.IsDir()
25
+}
26
+
27
+func (di dirInfo) Type() os.FileMode {
28
+	return di.fileInfo.Mode().Type()
29
+}
30
+
31
+func (di dirInfo) Info() (os.FileInfo, error) {
32
+	return di.fileInfo, nil
33
+}
34
+
35
+func (di dirInfo) Name() string {
36
+	return di.fileInfo.Name()
37
+}
38
+
39
+// fileInfoToDirEntry returns a DirEntry that returns information from info.
40
+// If info is nil, fileInfoToDirEntry returns nil.
41
+func fileInfoToDirEntry(info os.FileInfo) os.DirEntry {
42
+	if info == nil {
43
+		return nil
44
+	}
45
+	return dirInfo{fileInfo: info}
46
+}