Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Consider XDG_DATA_DIRS while searching for plugins #118

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ all: build # plugins

build:
mkdir -p build
sed 's|%INSTALLPREFIX%|${PREFIX}|g' core/plugins.in > core/plugins.go
sed "s|%INSTALLPREFIX%|${PREFIX}|g" core/plugins.in > core/plugins.go
go build -a -o build/${BINARY_NAME}

build-plugins: FORCE
Expand Down
60 changes: 50 additions & 10 deletions core/plugins.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/vanilla-os/vib/api"
)
import (
"errors"
"os"
"syscall"
)
Expand All @@ -20,23 +21,61 @@ var openedFinalizePlugins map[string]Plugin
func LoadPlugin(name string, plugintype api.PluginType, recipe *api.Recipe) (uintptr, error) {
fmt.Println("Loading new plugin")

localPluginPath := fmt.Sprintf("%s/%s.so", recipe.PluginPath, name)
projectPluginPath := fmt.Sprintf("%s/%s.so", recipe.PluginPath, name)

globalPluginPath := fmt.Sprintf("%INSTALLPREFIX%/share/vib/plugins/%s.so", name)
installPrefixPath := fmt.Sprintf("%INSTALLPREFIX%/share/vib/plugins/%s.so", name)

globalPluginPathsEnv, isGPPEDefined := os.LookupEnv("XDG_DATA_DIRS")
if !isGPPEDefined || globalPluginPathsEnv == "" {
Comment on lines +28 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
globalPluginPathsEnv, isGPPEDefined := os.LookupEnv("XDG_DATA_DIRS")
if !isGPPEDefined || globalPluginPathsEnv == "" {
globalPluginPathsEnv, isGPPEDefined := os.LookupEnv("XDG_DATA_DIRS")
if !isGPPEDefined || len(strings.TrimSpace(globalPluginPathsEnv)) == "" {

just checking if its equal to "" wouldn't work since the envvar can be just a single space or some other whitespace
also what does GPPEDefined mean? I think something like XDDDefined would make more sense

globalPluginPathsEnv = "/usr/local/share:/usr/share"
}

globalPluginPaths_split := strings.Split(globalPluginPathsEnv, ":")

for index := range globalPluginPaths_split {
// Resolve each directory to a *possible* plugin file path.
globalPluginPaths_split[index] = fmt.Sprintf("%s/vib/plugins/%s.so", globalPluginPaths_split[index], name)
}

// Specify all the paths where the plugin file might be stored.
// Give priority to the projects "plugins" directory, then
// follow INSTALLPREFIX and $XDG_DATA_DIRS, respectively.
var allPluginPaths = append([]string{projectPluginPath, installPrefixPath}, globalPluginPaths_split...)
var lastIndex = len(allPluginPaths) - 1

// Prefer local plugins before global ones
var loadedPlugin uintptr
_, err := os.Stat(localPluginPath)
if os.IsNotExist(err) {
loadedPlugin, err = purego.Dlopen(globalPluginPath, purego.RTLD_NOW|purego.RTLD_GLOBAL)

// LoadPlugin() is run once for every plugin, therefore
// the size of the array is limited to the same number
// of paths to search.
var _errors = make([]error, len(allPluginPaths))

for index, path := range allPluginPaths {
_, err := os.Stat(path)
if err != nil {
panic(err) // yayyy panics <3
_errors = append(_errors, err)
if index == lastIndex {
// If the last available path doesn't exist,
// panic with all the error messages.
panic(errors.Join(_errors...))
}

continue
}
} else {
loadedPlugin, err = purego.Dlopen(localPluginPath, purego.RTLD_NOW|purego.RTLD_GLOBAL)

loadedPlugin, err = purego.Dlopen(path, purego.RTLD_NOW|purego.RTLD_GLOBAL)
if err != nil {
panic(err)
_errors = append(_errors, err)
if index == lastIndex {
// If the last available plugin can't be loaded,
// panic with all the error messages.
panic(errors.Join(_errors...))
}

continue
}

break
}

infoLoc, err := purego.Dlsym(loadedPlugin, "PlugInfo")
Expand Down Expand Up @@ -67,6 +106,7 @@ func LoadPlugin(name string, plugintype api.PluginType, recipe *api.Recipe) (uin
return loadedPlugin, fmt.Errorf("ERROR: Plugin %s is not of type FinalizePlugin", name)
}
}

return loadedPlugin, nil
}

Expand Down