storage.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "errors"
  10. "os"
  11. "strings"
  12. "imuslab.com/arozos/mod/filesystem"
  13. fs "imuslab.com/arozos/mod/filesystem"
  14. "imuslab.com/arozos/mod/filesystem/arozfs"
  15. )
  16. // StoragePool is where the drives of the system or of one permission group are
  17. // configured. Who may use a drive is decided per drive, see mod/storage/access.
  18. type StoragePool struct {
  19. Owner string //"system" or the name of the permission group this pool belongs to
  20. Storages []*fs.FileSystemHandler //The drives configured in this pool
  21. //HyperBackupManager *hybridBackup.Manager //HyperBackup Manager
  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. Storages: storageHandlers,
  38. }, nil
  39. }
  40. // Check if this storage pool contain this particular disk ID
  41. func (s *StoragePool) ContainDiskID(diskID string) bool {
  42. for _, fsh := range s.Storages {
  43. if fsh.UUID == diskID {
  44. return true
  45. }
  46. }
  47. return false
  48. }
  49. // Get fsh from virtual path
  50. func (s *StoragePool) GetFSHandlerFromVirtualPath(vpath string) (*fs.FileSystemHandler, string, error) {
  51. fshid, subpath, err := filesystem.GetIDFromVirtualPath(vpath)
  52. if err != nil {
  53. return nil, subpath, err
  54. }
  55. fsh, err := s.GetFsHandlerByUUID(fshid)
  56. if err != nil {
  57. return nil, subpath, err
  58. }
  59. return fsh, subpath, nil
  60. }
  61. func (s *StoragePool) GetFsHandlerByUUID(uuid string) (*fs.FileSystemHandler, error) {
  62. //Filter out the :/ fropm uuid if exists
  63. if strings.Contains(uuid, ":") {
  64. uuid = strings.Split(uuid, ":")[0]
  65. }
  66. for _, fsh := range s.Storages {
  67. if fsh.UUID == uuid {
  68. return fsh, nil
  69. }
  70. }
  71. return nil, arozfs.ErrFSHNotFOund
  72. }
  73. // Attach a file system handler to this pool
  74. func (s *StoragePool) AttachFsHandler(fsh *filesystem.FileSystemHandler) error {
  75. if s.ContainDiskID(fsh.UUID) {
  76. return errors.New("file system handler with same uuid already exists in this pool")
  77. }
  78. s.Storages = append(s.Storages, fsh)
  79. return nil
  80. }
  81. // Detech a file system handler from this pool array
  82. func (s *StoragePool) DetachFsHandler(uuid string) {
  83. newFshList := []*fs.FileSystemHandler{}
  84. for _, fsh := range s.Storages {
  85. if fsh.UUID != uuid {
  86. newFshList = append(newFshList, fsh)
  87. }
  88. }
  89. s.Storages = newFshList
  90. }
  91. // Close all fsHandler under this storage pool
  92. func (s *StoragePool) Close() {
  93. //For each storage pool, close it
  94. for _, fsh := range s.Storages {
  95. if !fsh.Closed {
  96. fsh.Close()
  97. }
  98. }
  99. }