storage.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. "log"
  10. "os"
  11. "imuslab.com/arozos/mod/disk/hybridBackup"
  12. fs "imuslab.com/arozos/mod/filesystem"
  13. )
  14. type StoragePool struct {
  15. Owner string //Owner of the storage pool, also act as the resolver's username
  16. OtherPermission string //Permissions on other users but not the owner
  17. Storages []*fs.FileSystemHandler //Storage pool accessable by this owner
  18. HyperBackupManager *hybridBackup.Manager //HyperBackup Manager
  19. }
  20. /*
  21. Permission Levels (From TOP to BOTTOM -> HIGHEST to LOWEST)
  22. 1. readwrite
  23. 2. readonly
  24. 3. denied
  25. */
  26. //Create all the required folder structure if it didn't exists
  27. func init() {
  28. os.MkdirAll("./system/storage", 0755)
  29. }
  30. //Create a new StoragePool objects with given uuids
  31. func NewStoragePool(fsHandlers []*fs.FileSystemHandler, owner string) (*StoragePool, error) {
  32. //Create new HypberBackup Manager
  33. backupManager := hybridBackup.NewHyperBackupManager()
  34. //Move all fshandler into the storageHandler
  35. storageHandlers := []*fs.FileSystemHandler{}
  36. for _, fsHandler := range fsHandlers {
  37. //Move the handler pointer to the target
  38. storageHandlers = append(storageHandlers, fsHandler)
  39. if fsHandler.Hierarchy == "backup" {
  40. //Backup disk. Build the Hierarchy Config for this drive
  41. backupConfig := fsHandler.HierarchyConfig.(hybridBackup.BackupTask)
  42. //Resolve parent path for backup File System Handler
  43. parentExists := false
  44. for _, potentialParnet := range fsHandlers {
  45. if potentialParnet.UUID == backupConfig.ParentUID {
  46. //This is the parent
  47. backupConfig.ParentPath = potentialParnet.Path
  48. parentExists = true
  49. }
  50. }
  51. if parentExists {
  52. backupManager.AddTask(&backupConfig)
  53. } else {
  54. log.Println("*ERROR* Backup disk " + backupConfig.DiskUID + ":/ source disk not found: " + backupConfig.ParentUID + ":/ not exists!")
  55. }
  56. }
  57. }
  58. return &StoragePool{
  59. Owner: owner,
  60. OtherPermission: "readonly",
  61. Storages: storageHandlers,
  62. HyperBackupManager: backupManager,
  63. }, nil
  64. }
  65. //Use to compare two StoragePool permissions leve
  66. func (s *StoragePool) HasHigherOrEqualPermissionThan(a *StoragePool) bool {
  67. if s.OtherPermission == "readonly" && a.OtherPermission == "readwrite" {
  68. return false
  69. } else if s.OtherPermission == "denied" && a.OtherPermission != "denied" {
  70. return false
  71. }
  72. return true
  73. }
  74. //Close all fsHandler under this storage pool
  75. func (s *StoragePool) Close() {
  76. //For each storage pool, close it
  77. for _, fsh := range s.Storages {
  78. fsh.Close()
  79. }
  80. //Close the running backup tasks
  81. s.HyperBackupManager.Close()
  82. }
  83. //Helper function
  84. func inSlice(slice []string, val string) bool {
  85. for _, item := range slice {
  86. if item == val {
  87. return true
  88. }
  89. }
  90. return false
  91. }