update to use os.ReadDir where appropriate

This commit is contained in:
Emman 2022-03-31 10:37:09 +08:00
parent f15ccb066b
commit ef31153dba

View File

@ -4,7 +4,7 @@
package smx509 package smx509
import ( import (
"io/ioutil" "io/fs"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -17,6 +17,8 @@ const (
// certDirEnv is the environment variable which identifies which directory // certDirEnv is the environment variable which identifies which directory
// to check for SSL certificate files. If set this overrides the system default. // to check for SSL certificate files. If set this overrides the system default.
// It is a colon separated list of directories.
// See https://www.openssl.org/docs/man1.0.2/man1/c_rehash.html.
certDirEnv = "SSL_CERT_DIR" certDirEnv = "SSL_CERT_DIR"
) )
@ -34,7 +36,7 @@ func loadSystemRoots() (*CertPool, error) {
var firstErr error var firstErr error
for _, file := range files { for _, file := range files {
data, err := ioutil.ReadFile(file) data, err := os.ReadFile(file)
if err == nil { if err == nil {
roots.AppendCertsFromPEM(data) roots.AppendCertsFromPEM(data)
break break
@ -62,7 +64,7 @@ func loadSystemRoots() (*CertPool, error) {
continue continue
} }
for _, fi := range fis { for _, fi := range fis {
data, err := ioutil.ReadFile(directory + "/" + fi.Name()) data, err := os.ReadFile(directory + "/" + fi.Name())
if err == nil { if err == nil {
roots.AppendCertsFromPEM(data) roots.AppendCertsFromPEM(data)
} }
@ -76,17 +78,17 @@ func loadSystemRoots() (*CertPool, error) {
return nil, firstErr return nil, firstErr
} }
// readUniqueDirectoryEntries is like ioutil.ReadDir but omits // readUniqueDirectoryEntries is like os.ReadDir but omits
// symlinks that point within the directory. // symlinks that point within the directory.
func readUniqueDirectoryEntries(dir string) ([]os.FileInfo, error) { func readUniqueDirectoryEntries(dir string) ([]fs.DirEntry, error) {
fis, err := ioutil.ReadDir(dir) files, err := os.ReadDir(dir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
uniq := fis[:0] uniq := files[:0]
for _, fi := range fis { for _, f := range files {
if !isSameDirSymlink(fi, dir) { if !isSameDirSymlink(f, dir) {
uniq = append(uniq, fi) uniq = append(uniq, f)
} }
} }
return uniq, nil return uniq, nil
@ -94,10 +96,10 @@ func readUniqueDirectoryEntries(dir string) ([]os.FileInfo, error) {
// isSameDirSymlink reports whether fi in dir is a symlink with a // isSameDirSymlink reports whether fi in dir is a symlink with a
// target not containing a slash. // target not containing a slash.
func isSameDirSymlink(fi os.FileInfo, dir string) bool { func isSameDirSymlink(f fs.DirEntry, dir string) bool {
if fi.Mode()&os.ModeSymlink == 0 { if f.Type()&fs.ModeSymlink == 0 {
return false return false
} }
target, err := os.Readlink(filepath.Join(dir, fi.Name())) target, err := os.Readlink(filepath.Join(dir, f.Name()))
return err == nil && !strings.Contains(target, "/") return err == nil && !strings.Contains(target, "/")
} }