-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_linux.go
66 lines (55 loc) · 1.35 KB
/
load_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package loader
import (
"errors"
"fmt"
"os"
"path/filepath"
"plugin"
iplugin "github.com/ipfs/go-ipfs/plugin"
)
func init() {
loadPluginsFunc = linuxLoadFunc
}
func linuxLoadFunc(pluginDir string) ([]iplugin.Plugin, error) {
var plugins []iplugin.Plugin
err := filepath.Walk(pluginDir, func(fi string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if fi != pluginDir {
log.Warningf("found directory inside plugins directory: %s", fi)
}
return nil
}
if info.Mode().Perm()&0111 == 0 {
// file is not executable let's not load it
// this is to prevent loading plugins from for example non-executable
// mounts, some /tmp mounts are marked as such for security
log.Errorf("non-executable file in plugins directory: %s", fi)
return nil
}
if newPlugins, err := loadPlugin(fi); err == nil {
plugins = append(plugins, newPlugins...)
} else {
return fmt.Errorf("loading plugin %s: %s", fi, err)
}
return nil
})
return plugins, err
}
func loadPlugin(fi string) ([]iplugin.Plugin, error) {
pl, err := plugin.Open(fi)
if err != nil {
return nil, err
}
pls, err := pl.Lookup("Plugins")
if err != nil {
return nil, err
}
typePls, ok := pls.(*[]iplugin.Plugin)
if !ok {
return nil, errors.New("filed 'Plugins' didn't contain correct type")
}
return *typePls, nil
}