plugin/discovery: ignore non-files when discovering

If we encounter something that isn't a file -- for example, a dangling
symlink whose referent has been deleted -- we'll ignore it so that we
can either later produce a "no such plugin" error or auto-install a plugin
that will actually work.
This commit is contained in:
Martin Atkins 2017-09-01 16:00:15 -07:00
parent 3f401f0cd4
commit 12d6bc8c30

View File

@ -3,6 +3,7 @@ package discovery
import ( import (
"io/ioutil" "io/ioutil"
"log" "log"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
) )
@ -70,6 +71,12 @@ func findPluginPaths(kind string, dirs []string) []string {
continue continue
} }
// Check that the file we found is usable
if !pathIsFile(absPath) {
log.Printf("[ERROR] ignoring non-file %s", absPath)
continue
}
log.Printf("[DEBUG] found %s %q", kind, fullName) log.Printf("[DEBUG] found %s %q", kind, fullName)
ret = append(ret, filepath.Clean(absPath)) ret = append(ret, filepath.Clean(absPath))
continue continue
@ -82,6 +89,12 @@ func findPluginPaths(kind string, dirs []string) []string {
continue continue
} }
// Check that the file we found is usable
if !pathIsFile(absPath) {
log.Printf("[ERROR] ignoring non-file %s", absPath)
continue
}
log.Printf("[WARNING] found legacy %s %q", kind, fullName) log.Printf("[WARNING] found legacy %s %q", kind, fullName)
ret = append(ret, filepath.Clean(absPath)) ret = append(ret, filepath.Clean(absPath))
@ -91,6 +104,17 @@ func findPluginPaths(kind string, dirs []string) []string {
return ret return ret
} }
// Returns true if and only if the given path refers to a file or a symlink
// to a file.
func pathIsFile(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
return !info.IsDir()
}
// ResolvePluginPaths takes a list of paths to plugin executables (as returned // ResolvePluginPaths takes a list of paths to plugin executables (as returned
// by e.g. FindPluginPaths) and produces a PluginMetaSet describing the // by e.g. FindPluginPaths) and produces a PluginMetaSet describing the
// referenced plugins. // referenced plugins.