storage.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "strings"
  11. "imuslab.com/arozos/mod/filesystem"
  12. fs "imuslab.com/arozos/mod/filesystem"
  13. "imuslab.com/arozos/mod/filesystem/fsdef"
  14. )
  15. type StoragePool struct {
  16. Owner string //Owner of the storage pool, also act as the resolver's username
  17. OtherPermission string //Permissions on other users but not the owner
  18. Storages []*fs.FileSystemHandler //Storage pool accessable by this owner
  19. //HyperBackupManager *hybridBackup.Manager //HyperBackup Manager
  20. }
  21. /*
  22. Permission Levels (From TOP to BOTTOM -> HIGHEST to LOWEST)
  23. 1. readwrite
  24. 2. readonly
  25. 3. denied
  26. */
  27. //Create all the required folder structure if it didn't exists
  28. func init() {
  29. os.MkdirAll("./system/storage", 0755)
  30. }
  31. //Create a new StoragePool objects with given uuids
  32. func NewStoragePool(fsHandlers []*fs.FileSystemHandler, owner string) (*StoragePool, error) {
  33. //Move all fshandler into the storageHandler
  34. storageHandlers := []*fs.FileSystemHandler{}
  35. for _, fsHandler := range fsHandlers {
  36. //Move the handler pointer to the target
  37. storageHandlers = append(storageHandlers, fsHandler)
  38. }
  39. return &StoragePool{
  40. Owner: owner,
  41. OtherPermission: fsdef.FsReadOnly,
  42. Storages: storageHandlers,
  43. }, nil
  44. }
  45. //Check if this storage pool contain this particular disk ID
  46. func (s *StoragePool) ContainDiskID(diskID string) bool {
  47. for _, fsh := range s.Storages {
  48. if fsh.UUID == diskID {
  49. return true
  50. }
  51. }
  52. return false
  53. }
  54. //Use to compare two StoragePool permissions leve
  55. func (s *StoragePool) HasHigherOrEqualPermissionThan(a *StoragePool) bool {
  56. if s.OtherPermission == fsdef.FsReadOnly && a.OtherPermission == fsdef.FsReadWrite {
  57. return false
  58. } else if s.OtherPermission == fsdef.FsDenied && a.OtherPermission != fsdef.FsDenied {
  59. return false
  60. }
  61. return true
  62. }
  63. //Get fsh from virtual path
  64. func (s *StoragePool) GetFSHandlerFromVirtualPath(vpath string) (*fs.FileSystemHandler, string, error) {
  65. fshid, subpath, err := filesystem.GetIDFromVirtualPath(vpath)
  66. if err != nil {
  67. return nil, subpath, err
  68. }
  69. fsh, err := s.GetFsHandlerByUUID(fshid)
  70. if err != nil {
  71. return nil, subpath, err
  72. }
  73. return fsh, subpath, nil
  74. }
  75. func (s *StoragePool) GetFsHandlerByUUID(uuid string) (*fs.FileSystemHandler, error) {
  76. //Filter out the :/ fropm uuid if exists
  77. if strings.Contains(uuid, ":") {
  78. uuid = strings.Split(uuid, ":")[0]
  79. }
  80. for _, fsh := range s.Storages {
  81. if fsh.UUID == uuid {
  82. return fsh, nil
  83. }
  84. }
  85. return nil, fsdef.ErrFSHNotFOund
  86. }
  87. //Close all fsHandler under this storage pool
  88. func (s *StoragePool) Close() {
  89. //For each storage pool, close it
  90. for _, fsh := range s.Storages {
  91. fsh.Close()
  92. }
  93. }
  94. //Helper function
  95. func inSlice(slice []string, val string) bool {
  96. for _, item := range slice {
  97. if item == val {
  98. return true
  99. }
  100. }
  101. return false
  102. }