plugin.go 788 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //go:build (darwin || linux) && !gccgo
  2. package plugin
  3. import (
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "plugin"
  8. "strings"
  9. )
  10. func init() {
  11. dir := os.Getenv("RCLONE_PLUGIN_PATH")
  12. if dir == "" {
  13. return
  14. }
  15. // Get file names of plugin dir
  16. listing, err := os.ReadDir(dir)
  17. if err != nil {
  18. fmt.Fprintln(os.Stderr, "Failed to open plugin directory:", err)
  19. }
  20. // Enumerate file names, load valid plugins
  21. for _, file := range listing {
  22. // Match name
  23. fileName := file.Name()
  24. if !strings.HasPrefix(fileName, "librcloneplugin_") {
  25. continue
  26. }
  27. if !strings.HasSuffix(fileName, ".so") {
  28. continue
  29. }
  30. // Try to load plugin
  31. _, err := plugin.Open(filepath.Join(dir, fileName))
  32. if err != nil {
  33. fmt.Fprintf(os.Stderr, "Failed to load plugin %s: %s\n",
  34. fileName, err)
  35. }
  36. }
  37. }