directoryHandler.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package user
  2. import (
  3. "errors"
  4. "path/filepath"
  5. "strings"
  6. "os"
  7. fs "imuslab.com/arozos/mod/filesystem"
  8. )
  9. func (u *User) GetHomeDirectory() (string, error) {
  10. //Return the realpath of the user home directory
  11. for _, dir := range u.HomeDirectories.Storages {
  12. if dir.UUID == "user" {
  13. //This is the target user root
  14. root := filepath.ToSlash(filepath.Clean(dir.Path) + "/users/" + u.Username + "/")
  15. os.MkdirAll(root, 0755)
  16. return root, nil
  17. }
  18. }
  19. return "", errors.New("User root not found. Is this a permission group instead of a real user?")
  20. }
  21. func (u *User) GetAllFileSystemHandler() []*fs.FileSystemHandler {
  22. results := []*fs.FileSystemHandler{}
  23. uuids := []string{}
  24. //Get all FileSystem Handler from this user's Home Directory (aka base directory)
  25. for _, store := range u.HomeDirectories.Storages {
  26. if store.Closed == false {
  27. //Only return opened file system handlers
  28. results = append(results, store)
  29. uuids = append(uuids, store.UUID)
  30. }
  31. }
  32. //Get all the FileSystem handler that is accessable by this user
  33. for _, pg := range u.PermissionGroup {
  34. //For each permission group that this user is in
  35. for _, store := range pg.StoragePool.Storages {
  36. //Get each of the storage of this permission group is assigned to
  37. if !inSlice(uuids, store.UUID) {
  38. if store.Closed == false {
  39. //Only return opened file system handlers
  40. results = append(results, store)
  41. uuids = append(uuids, store.UUID)
  42. }
  43. }
  44. }
  45. }
  46. return results
  47. }
  48. func (u *User) VirtualPathToRealPath(vpath string) (string, error) {
  49. //Get all usable filesystem handler from the user's home directory and permission groups
  50. userFsHandlers := u.GetAllFileSystemHandler()
  51. //Clear the path
  52. virtualPath := filepath.ToSlash(filepath.Clean(vpath))
  53. //Check for path escape
  54. if len(virtualPath) > 2 && virtualPath[:2] == ".." {
  55. return "", errors.New("Request path out of storage root")
  56. }
  57. //Check for valid virtual device id
  58. vid, subpath, err := getIDFromVirtualPath(vpath)
  59. if err != nil {
  60. return "", err
  61. }
  62. //Look for the handler with the same virtualPath ID
  63. for _, storage := range userFsHandlers {
  64. if storage.UUID == vid {
  65. //This storage is the one we are looking at
  66. //Check if this has been closed
  67. if storage.Closed == true {
  68. return "", errors.New("Request Filesystem Handler has been closed by another process")
  69. }
  70. if storage.Hierarchy == "user" {
  71. return filepath.Clean(storage.Path) + "/users/" + u.Username + subpath, nil
  72. } else {
  73. return filepath.Clean(storage.Path) + subpath, nil
  74. }
  75. }
  76. }
  77. return "", errors.New("Translation failed: Vitrual storage ID not found")
  78. }
  79. func (u *User) RealPathToVirtualPath(rpath string) (string, error) {
  80. //Get all usable filesystem handler
  81. userFsHandlers := u.GetAllFileSystemHandler()
  82. //Clear the path
  83. realPath := filepath.ToSlash(filepath.Clean(rpath))
  84. //Check for path escape
  85. if len(realPath) > 2 && realPath[:2] == ".." {
  86. return "", errors.New("Request path out of storage root")
  87. }
  88. //Look for a real path of a virtual device that the realpath is containing
  89. for _, storage := range userFsHandlers {
  90. thisStorageRoot := filepath.Clean(filepath.ToSlash(storage.Path))
  91. thisStorageRootAbs, err := filepath.Abs(thisStorageRoot)
  92. if err != nil {
  93. //Fail to abs this path. Maybe this is a emulated file system?
  94. thisStorageRootAbs = thisStorageRoot
  95. }
  96. thisStorageRootAbs = filepath.ToSlash(filepath.Clean(thisStorageRootAbs))
  97. pathContained := false
  98. subPath := ""
  99. if len(realPath) > len(thisStorageRoot) && realPath[:len(thisStorageRoot)] == thisStorageRoot {
  100. //This realpath is in contained inside this storage root
  101. pathContained = true
  102. subtractionPath := thisStorageRoot
  103. if storage.Hierarchy == "user" {
  104. subtractionPath = thisStorageRoot + "/users/" + u.Username + "/"
  105. }
  106. if len(subtractionPath) < len(realPath) {
  107. subPath = realPath[len(subtractionPath):]
  108. }
  109. } else if len(realPath) > len(thisStorageRootAbs) && realPath[:len(thisStorageRootAbs)] == thisStorageRootAbs {
  110. //The realpath contains the absolute path of this storage root
  111. pathContained = true
  112. subtractionPath := thisStorageRootAbs
  113. if storage.Hierarchy == "user" {
  114. subtractionPath = thisStorageRootAbs + "/users/" + u.Username + "/"
  115. }
  116. if len(subtractionPath) < len(realPath) {
  117. subPath = realPath[len(subtractionPath):]
  118. }
  119. } else if realPath == thisStorageRoot {
  120. //Storage Root's root
  121. pathContained = true
  122. subPath = ""
  123. }
  124. if len(subPath) > 1 && subPath[:1] == "/" {
  125. subPath = subPath[1:]
  126. }
  127. if pathContained == true {
  128. //This storage is one of the root of the given realpath. Translate it into this
  129. if storage.Closed == true {
  130. return "", errors.New("Request Filesystem Handler has been closed by another process")
  131. }
  132. return storage.UUID + ":/" + subPath, nil
  133. }
  134. }
  135. return "", errors.New("Unable to resolve realpath in virtual devices root path")
  136. }
  137. //Get a file system handler from a virtual path, this file system handler might not be the highest prioity one
  138. func (u *User) GetFileSystemHandlerFromVirtualPath(vpath string) (*fs.FileSystemHandler, error) {
  139. fsHandlers := u.GetAllFileSystemHandler()
  140. handler, err := getHandlerFromVirtualPath(fsHandlers, vpath)
  141. return handler, err
  142. }
  143. func (u *User) GetFileSystemHandlerFromRealPath(rpath string) (*fs.FileSystemHandler, error) {
  144. vpath, err := u.RealPathToVirtualPath(rpath)
  145. if err != nil {
  146. return &fs.FileSystemHandler{}, err
  147. }
  148. return u.GetFileSystemHandlerFromVirtualPath(vpath)
  149. }
  150. /*
  151. PRIVATE FUNCTIONS HANDLERS
  152. */
  153. //Get a fs handler from a virtual path, quick function for getIDFromHandler + GetHandlerFromID
  154. func getHandlerFromVirtualPath(storages []*fs.FileSystemHandler, vpath string) (*fs.FileSystemHandler, error) {
  155. vid, _, err := getIDFromVirtualPath(vpath)
  156. if err != nil {
  157. return &fs.FileSystemHandler{}, err
  158. }
  159. return getHandlerFromID(storages, vid)
  160. }
  161. //Get a fs handler from the given virtial device id
  162. func getHandlerFromID(storages []*fs.FileSystemHandler, vid string) (*fs.FileSystemHandler, error) {
  163. for _, storage := range storages {
  164. if storage.UUID == vid {
  165. //This storage is the one we are looking at
  166. return storage, nil
  167. }
  168. }
  169. return &fs.FileSystemHandler{}, errors.New("Handler Not Found")
  170. }
  171. //Get the ID part of a virtual path, return ID, subpath and error
  172. func getIDFromVirtualPath(vpath string) (string, string, error) {
  173. if strings.Contains(vpath, ":") == false {
  174. return "", "", errors.New("Path missing Virtual Device ID. Given: " + vpath)
  175. }
  176. //Clean up the virutal path
  177. vpath = filepath.ToSlash(filepath.Clean(vpath))
  178. tmp := strings.Split(vpath, ":")
  179. vdID := tmp[0]
  180. pathSlice := tmp[1:]
  181. path := strings.Join(pathSlice, ":")
  182. return vdID, path, nil
  183. }