bokoworker.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package bokoworker
  2. import (
  3. "encoding/json"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/google/uuid"
  8. "imuslab.com/bokofs/bokofsd/mod/bokofs/bokofile"
  9. )
  10. /*
  11. Boko Worker
  12. A boko worker is an instance of WebDAV file server that serves a specific
  13. disk partition or subpath in which the user can interact with the disk
  14. via WebDAV interface
  15. */
  16. type Worker struct {
  17. /* Worker Properties */
  18. RootPath string //The root path (also act as ID) request by this worker, e.g. /disk1
  19. DiskUUID string // Disk UUID, when provided, will be used instead of disk path
  20. DiskPath string // Disk device path (e.g. /dev/sda1), Disk UUID will have higher priority
  21. Subpath string // Subpath to serve, default is root
  22. ReadOnly bool //Label this worker as read only
  23. AutoMount bool //Automatically mount the disk if not mounted
  24. /* Private Properties */
  25. Filesystem *bokofile.RouterDir
  26. }
  27. // GetDefaultWorker Generate and return a default worker serving ./ (CWD)
  28. func GetDefaultWorker(rootdir string) (*Worker, error) {
  29. if !strings.HasPrefix(rootdir, "/") {
  30. rootdir = "/" + rootdir
  31. }
  32. mountPath, _ := filepath.Abs("./")
  33. fs, err := bokofile.CreateRouterFromDir(mountPath, rootdir, false)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return &Worker{
  38. RootPath: rootdir,
  39. DiskUUID: "",
  40. DiskPath: "",
  41. Subpath: mountPath,
  42. ReadOnly: false,
  43. AutoMount: false,
  44. Filesystem: fs,
  45. }, nil
  46. }
  47. func GetWorkerFromConfig(configFilePath string) (*Worker, error) {
  48. randomRootPath := uuid.New().String()
  49. thisWorker, _ := GetDefaultWorker("/" + randomRootPath)
  50. configFile, err := os.ReadFile(configFilePath)
  51. if err != nil {
  52. return nil, err
  53. }
  54. //parse the config file into thisWorker
  55. err = json.Unmarshal(configFile, &thisWorker)
  56. if err != nil {
  57. return nil, err
  58. }
  59. //TODO: Start the worker
  60. return thisWorker, nil
  61. }