github.com/docker/engine@v22.0.0-20211208180946-d456264580cf+incompatible/pkg/plugins/utils_go1.16.go (about)

     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  }