Skip to content

Commit 303ea8e

Browse files
committed
pkg/plugins: fix compatibility with go1.16
commit c55a4ac 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>
1 parent 7bdf982 commit 303ea8e

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

pkg/plugins/discovery.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package plugins // import "github.com/docker/docker/pkg/plugins"
33
import (
44
"encoding/json"
55
"fmt"
6-
"io/fs"
76
"net/url"
87
"os"
98
"path/filepath"
@@ -41,7 +40,7 @@ func Scan() ([]string, error) {
4140
continue
4241
}
4342

44-
entry = fs.FileInfoToDirEntry(fi)
43+
entry = fileInfoToDirEntry(fi)
4544
}
4645

4746
if entry.Type()&os.ModeSocket != 0 {

pkg/plugins/utils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//go:build go1.17
2+
// +build go1.17
3+
4+
package plugins
5+
6+
import "io/fs"
7+
8+
var fileInfoToDirEntry = fs.FileInfoToDirEntry

pkg/plugins/utils_go1.16.go

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

0 commit comments

Comments
 (0)