quota.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package user
  2. import (
  3. //"path/filepath"
  4. //"log"
  5. fs "imuslab.com/arozos/mod/filesystem"
  6. //quota "imuslab.com/arozos/mod/quota"
  7. )
  8. /*
  9. Quota Handler
  10. author: tobychui
  11. This module handle the user storage quota and its related functions
  12. */
  13. //Return the user quota information, returning used / total
  14. func (u *User) HaveSpaceFor(realpath string) bool {
  15. if u.StorageQuota.HaveSpace(fs.GetFileSize(realpath)) {
  16. return true
  17. } else {
  18. return false
  19. }
  20. }
  21. func (u *User) SetOwnerOfFile(realpath string) error {
  22. //Get handler from the path
  23. fsHandler, err := u.GetFileSystemHandlerFromRealPath(realpath)
  24. if err != nil {
  25. return err
  26. }
  27. //Check if it is user structured. If yes, add the filesize to user's quota
  28. if fsHandler.Hierarchy == "user" {
  29. //log.Println("Setting user ownership on: " + realpath)
  30. u.StorageQuota.AllocateSpace(fs.GetFileSize(realpath))
  31. }
  32. //Add to the fshandler database of this file owner
  33. err = fsHandler.CreateFileRecord(realpath, u.Username)
  34. return err
  35. }
  36. func (u *User) RemoveOwnershipFromFile(realpath string) error {
  37. //Get handler from the path
  38. fsHandler, err := u.GetFileSystemHandlerFromRealPath(realpath)
  39. if err != nil {
  40. return err
  41. }
  42. //Check if it is user structured. If yes, add the filesize to user's quota
  43. if fsHandler.Hierarchy == "user" {
  44. //log.Println("Removing user ownership on: " + realpath)
  45. u.StorageQuota.ReclaimSpace(fs.GetFileSize(realpath))
  46. }
  47. err = fsHandler.DeleteFileRecord(realpath)
  48. return err
  49. }
  50. func (u *User) IsOwnerOfFile(realpath string) bool {
  51. owner := u.GetFileOwner(realpath)
  52. if owner == u.Username {
  53. //This file is owned by this user
  54. return true
  55. } else {
  56. return false
  57. }
  58. }
  59. func (u *User) GetFileOwner(realpath string) string {
  60. fsHandler, err := u.GetFileSystemHandlerFromRealPath(realpath)
  61. if err != nil {
  62. return ""
  63. }
  64. owner, err := fsHandler.GetFileRecord(realpath)
  65. if err != nil {
  66. //Error occured. Either this file is not tracked or this file has no owner
  67. return ""
  68. }
  69. return owner
  70. }