storage.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package storage
  2. /*
  3. ArOZ Online Storage Handler Module
  4. author: tobychui
  5. This is a system for allowing generic interfacing to the filesystems
  6. To add more supports for different type of file system, add more storage handlers.
  7. */
  8. import (
  9. fs "imuslab.com/arozos/mod/filesystem"
  10. )
  11. type StoragePool struct {
  12. Owner string //Owner of the storage pool, also act as the resolver's username
  13. OtherPermission string //Permissions on other users but not the owner
  14. Storages []*fs.FileSystemHandler //Storage pool accessable by this owner
  15. }
  16. /*
  17. Permission Levels (From TOP to BOTTOM -> HIGHEST to LOWEST)
  18. 1. readwrite
  19. 2. readonly
  20. 3. denied
  21. */
  22. //Create a new StoragePool objects with given uuids
  23. func NewStoragePool(fsHandlers []*fs.FileSystemHandler, owner string) (*StoragePool, error) {
  24. //Move all fshandler into the storageHandler
  25. storageHandlers := []*fs.FileSystemHandler{}
  26. for _, fsHandler := range fsHandlers {
  27. //Move the handler pointer to the target
  28. storageHandlers = append(storageHandlers, fsHandler)
  29. }
  30. return &StoragePool{
  31. Owner: owner,
  32. OtherPermission: "readonly",
  33. Storages: storageHandlers,
  34. }, nil
  35. }
  36. //Use to compare two StoragePool permissions leve
  37. func (s *StoragePool) HasHigherOrEqualPermissionThan(a *StoragePool) bool {
  38. if s.OtherPermission == "readonly" && a.OtherPermission == "readwrite" {
  39. return false
  40. } else if s.OtherPermission == "denied" && a.OtherPermission != "denied" {
  41. return false
  42. }
  43. return true
  44. }
  45. //Close all fsHandler under this storage pool
  46. func (s *StoragePool) Close() {
  47. for _, fsh := range s.Storages {
  48. fsh.Close()
  49. }
  50. }
  51. //Helper function
  52. func inSlice(slice []string, val string) bool {
  53. for _, item := range slice {
  54. if item == val {
  55. return true
  56. }
  57. }
  58. return false
  59. }