quota.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package dataprovider
  2. import (
  3. "sync"
  4. "time"
  5. "github.com/drakkan/sftpgo/v2/util"
  6. )
  7. var (
  8. // QuotaScans is the list of active quota scans
  9. QuotaScans ActiveScans
  10. )
  11. // ActiveQuotaScan defines an active quota scan for a user home dir
  12. type ActiveQuotaScan struct {
  13. // Username to which the quota scan refers
  14. Username string `json:"username"`
  15. // quota scan start time as unix timestamp in milliseconds
  16. StartTime int64 `json:"start_time"`
  17. }
  18. // ActiveVirtualFolderQuotaScan defines an active quota scan for a virtual folder
  19. type ActiveVirtualFolderQuotaScan struct {
  20. // folder name to which the quota scan refers
  21. Name string `json:"name"`
  22. // quota scan start time as unix timestamp in milliseconds
  23. StartTime int64 `json:"start_time"`
  24. }
  25. // ActiveScans holds the active quota scans
  26. type ActiveScans struct {
  27. sync.RWMutex
  28. UserScans []ActiveQuotaScan
  29. FolderScans []ActiveVirtualFolderQuotaScan
  30. }
  31. // GetUsersQuotaScans returns the active quota scans for users home directories
  32. func (s *ActiveScans) GetUsersQuotaScans() []ActiveQuotaScan {
  33. s.RLock()
  34. defer s.RUnlock()
  35. scans := make([]ActiveQuotaScan, len(s.UserScans))
  36. copy(scans, s.UserScans)
  37. return scans
  38. }
  39. // AddUserQuotaScan adds a user to the ones with active quota scans.
  40. // Returns false if the user has a quota scan already running
  41. func (s *ActiveScans) AddUserQuotaScan(username string) bool {
  42. s.Lock()
  43. defer s.Unlock()
  44. for _, scan := range s.UserScans {
  45. if scan.Username == username {
  46. return false
  47. }
  48. }
  49. s.UserScans = append(s.UserScans, ActiveQuotaScan{
  50. Username: username,
  51. StartTime: util.GetTimeAsMsSinceEpoch(time.Now()),
  52. })
  53. return true
  54. }
  55. // RemoveUserQuotaScan removes a user from the ones with active quota scans.
  56. // Returns false if the user has no active quota scans
  57. func (s *ActiveScans) RemoveUserQuotaScan(username string) bool {
  58. s.Lock()
  59. defer s.Unlock()
  60. for idx, scan := range s.UserScans {
  61. if scan.Username == username {
  62. lastIdx := len(s.UserScans) - 1
  63. s.UserScans[idx] = s.UserScans[lastIdx]
  64. s.UserScans = s.UserScans[:lastIdx]
  65. return true
  66. }
  67. }
  68. return false
  69. }
  70. // GetVFoldersQuotaScans returns the active quota scans for virtual folders
  71. func (s *ActiveScans) GetVFoldersQuotaScans() []ActiveVirtualFolderQuotaScan {
  72. s.RLock()
  73. defer s.RUnlock()
  74. scans := make([]ActiveVirtualFolderQuotaScan, len(s.FolderScans))
  75. copy(scans, s.FolderScans)
  76. return scans
  77. }
  78. // AddVFolderQuotaScan adds a virtual folder to the ones with active quota scans.
  79. // Returns false if the folder has a quota scan already running
  80. func (s *ActiveScans) AddVFolderQuotaScan(folderName string) bool {
  81. s.Lock()
  82. defer s.Unlock()
  83. for _, scan := range s.FolderScans {
  84. if scan.Name == folderName {
  85. return false
  86. }
  87. }
  88. s.FolderScans = append(s.FolderScans, ActiveVirtualFolderQuotaScan{
  89. Name: folderName,
  90. StartTime: util.GetTimeAsMsSinceEpoch(time.Now()),
  91. })
  92. return true
  93. }
  94. // RemoveVFolderQuotaScan removes a folder from the ones with active quota scans.
  95. // Returns false if the folder has no active quota scans
  96. func (s *ActiveScans) RemoveVFolderQuotaScan(folderName string) bool {
  97. s.Lock()
  98. defer s.Unlock()
  99. for idx, scan := range s.FolderScans {
  100. if scan.Name == folderName {
  101. lastIdx := len(s.FolderScans) - 1
  102. s.FolderScans[idx] = s.FolderScans[lastIdx]
  103. s.FolderScans = s.FolderScans[:lastIdx]
  104. return true
  105. }
  106. }
  107. return false
  108. }