storage.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. "os"
  10. fs "imuslab.com/arozos/mod/filesystem"
  11. )
  12. type StoragePool struct {
  13. Owner string //Owner of the storage pool, also act as the resolver's username
  14. OtherPermission string //Permissions on other users but not the owner
  15. Storages []*fs.FileSystemHandler //Storage pool accessable by this owner
  16. }
  17. /*
  18. Permission Levels (From TOP to BOTTOM -> HIGHEST to LOWEST)
  19. 1. readwrite
  20. 2. readonly
  21. 3. denied
  22. */
  23. //Create all the required folder structure if it didn't exists
  24. func init() {
  25. os.MkdirAll("./system/storage", 0755)
  26. }
  27. //Create a new StoragePool objects with given uuids
  28. func NewStoragePool(fsHandlers []*fs.FileSystemHandler, owner string) (*StoragePool, error) {
  29. //Move all fshandler into the storageHandler
  30. storageHandlers := []*fs.FileSystemHandler{}
  31. for _, fsHandler := range fsHandlers {
  32. //Move the handler pointer to the target
  33. storageHandlers = append(storageHandlers, fsHandler)
  34. }
  35. return &StoragePool{
  36. Owner: owner,
  37. OtherPermission: "readonly",
  38. Storages: storageHandlers,
  39. }, nil
  40. }
  41. //Use to compare two StoragePool permissions leve
  42. func (s *StoragePool) HasHigherOrEqualPermissionThan(a *StoragePool) bool {
  43. if s.OtherPermission == "readonly" && a.OtherPermission == "readwrite" {
  44. return false
  45. } else if s.OtherPermission == "denied" && a.OtherPermission != "denied" {
  46. return false
  47. }
  48. return true
  49. }
  50. //Close all fsHandler under this storage pool
  51. func (s *StoragePool) Close() {
  52. for _, fsh := range s.Storages {
  53. fsh.Close()
  54. }
  55. }
  56. //Helper function
  57. func inSlice(slice []string, val string) bool {
  58. for _, item := range slice {
  59. if item == val {
  60. return true
  61. }
  62. }
  63. return false
  64. }