From 12d6bc8c3065163e76179a46a5716c3c8a014231 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 1 Sep 2017 16:00:15 -0700 Subject: [PATCH] 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. --- plugin/discovery/find.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/plugin/discovery/find.go b/plugin/discovery/find.go index 10f8fce94..5c643f96f 100644 --- a/plugin/discovery/find.go +++ b/plugin/discovery/find.go @@ -3,6 +3,7 @@ package discovery import ( "io/ioutil" "log" + "os" "path/filepath" "strings" ) @@ -70,6 +71,12 @@ func findPluginPaths(kind string, dirs []string) []string { 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) ret = append(ret, filepath.Clean(absPath)) continue @@ -82,6 +89,12 @@ func findPluginPaths(kind string, dirs []string) []string { 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) ret = append(ret, filepath.Clean(absPath)) @@ -91,6 +104,17 @@ func findPluginPaths(kind string, dirs []string) []string { 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 // by e.g. FindPluginPaths) and produces a PluginMetaSet describing the // referenced plugins.