directoryHandler.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package user
  2. import (
  3. "errors"
  4. "os"
  5. "path/filepath"
  6. "strings"
  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. //Check if this is a backup drive
  71. if storage.Hierarchy == "backup" {
  72. return "", errors.New("Request Filesystem Handler do not allow direct access")
  73. }
  74. //Handle general cases
  75. if storage.Hierarchy == "user" {
  76. return filepath.ToSlash(filepath.Clean(storage.Path) + "/users/" + u.Username + subpath), nil
  77. } else if storage.Hierarchy == "public" {
  78. return filepath.ToSlash(filepath.Clean(storage.Path) + subpath), nil
  79. } else {
  80. return "", errors.New("Unknown Filesystem Handler Hierarchy")
  81. }
  82. }
  83. }
  84. return "", errors.New("Translation failed: Vitrual storage ID not found")
  85. }
  86. func (u *User) RealPathToVirtualPath(rpath string) (string, error) {
  87. //Get all usable filesystem handler
  88. userFsHandlers := u.GetAllFileSystemHandler()
  89. //Clear the path
  90. realPath := filepath.ToSlash(filepath.Clean(rpath))
  91. //Check for path escape
  92. if len(realPath) > 2 && realPath[:2] == ".." {
  93. //Fix: 20 May 2021: Allow using ../folder as virtual root directory
  94. //Check if there are vroots that actually use relative path as root directory.
  95. allowSpecialCasePassThrough := false
  96. for _, fsh := range userFsHandlers {
  97. thisVrootPath := fsh.Path
  98. if len(realPath) > len(thisVrootPath) && filepath.ToSlash(realPath[:len(thisVrootPath)]) == filepath.ToSlash(thisVrootPath) {
  99. allowSpecialCasePassThrough = true
  100. }
  101. }
  102. if !allowSpecialCasePassThrough {
  103. return "", errors.New("Request path out of storage root")
  104. }
  105. }
  106. //Look for a real path of a virtual device that the realpath is containing
  107. for _, storage := range userFsHandlers {
  108. thisStorageRoot := filepath.Clean(filepath.ToSlash(storage.Path))
  109. thisStorageRootAbs, err := filepath.Abs(thisStorageRoot)
  110. if err != nil {
  111. //Fail to abs this path. Maybe this is a emulated file system?
  112. thisStorageRootAbs = thisStorageRoot
  113. }
  114. thisStorageRootAbs = filepath.ToSlash(filepath.Clean(thisStorageRootAbs))
  115. pathContained := false
  116. subPath := ""
  117. if len(realPath) > len(thisStorageRoot) && filepath.ToSlash(realPath[:len(thisStorageRoot)]) == filepath.ToSlash(thisStorageRoot) {
  118. //This realpath is in contained inside this storage root
  119. pathContained = true
  120. subtractionPath := thisStorageRoot
  121. if storage.Hierarchy == "user" {
  122. subtractionPath = thisStorageRoot + "/users/" + u.Username + "/"
  123. }
  124. if len(subtractionPath) < len(realPath) {
  125. subPath = realPath[len(subtractionPath):]
  126. }
  127. } else if len(realPath) > len(thisStorageRootAbs) && filepath.ToSlash(realPath[:len(thisStorageRootAbs)]) == filepath.ToSlash(thisStorageRootAbs) {
  128. //The realpath contains the absolute path of this storage root
  129. pathContained = true
  130. subtractionPath := thisStorageRootAbs
  131. if storage.Hierarchy == "user" {
  132. subtractionPath = thisStorageRootAbs + "/users/" + u.Username + "/"
  133. }
  134. if len(subtractionPath) < len(realPath) {
  135. subPath = realPath[len(subtractionPath):]
  136. }
  137. } else if filepath.ToSlash(realPath) == filepath.ToSlash(thisStorageRoot) {
  138. //Storage Root's root
  139. pathContained = true
  140. subPath = ""
  141. }
  142. if len(subPath) > 1 && subPath[:1] == "/" {
  143. subPath = subPath[1:]
  144. }
  145. if pathContained == true {
  146. //This storage is one of the root of the given realpath. Translate it into this
  147. if storage.Closed == true {
  148. return "", errors.New("Request Filesystem Handler has been closed by another process")
  149. }
  150. return storage.UUID + ":/" + subPath, nil
  151. }
  152. }
  153. return "", errors.New("Unable to resolve realpath in virtual devices root path")
  154. }
  155. //Get a file system handler from a virtual path, this file system handler might not be the highest prioity one
  156. func (u *User) GetFileSystemHandlerFromVirtualPath(vpath string) (*fs.FileSystemHandler, error) {
  157. fsHandlers := u.GetAllFileSystemHandler()
  158. handler, err := getHandlerFromVirtualPath(fsHandlers, vpath)
  159. return handler, err
  160. }
  161. func (u *User) GetFileSystemHandlerFromRealPath(rpath string) (*fs.FileSystemHandler, error) {
  162. vpath, err := u.RealPathToVirtualPath(rpath)
  163. if err != nil {
  164. return &fs.FileSystemHandler{}, err
  165. }
  166. return u.GetFileSystemHandlerFromVirtualPath(vpath)
  167. }
  168. /*
  169. PRIVATE FUNCTIONS HANDLERS
  170. */
  171. //Get a fs handler from a virtual path, quick function for getIDFromHandler + GetHandlerFromID
  172. func getHandlerFromVirtualPath(storages []*fs.FileSystemHandler, vpath string) (*fs.FileSystemHandler, error) {
  173. vid, _, err := getIDFromVirtualPath(vpath)
  174. if err != nil {
  175. return &fs.FileSystemHandler{}, err
  176. }
  177. return getHandlerFromID(storages, vid)
  178. }
  179. //Get a fs handler from the given virtial device id
  180. func getHandlerFromID(storages []*fs.FileSystemHandler, vid string) (*fs.FileSystemHandler, error) {
  181. for _, storage := range storages {
  182. if storage.UUID == vid {
  183. //This storage is the one we are looking at
  184. return storage, nil
  185. }
  186. }
  187. return &fs.FileSystemHandler{}, errors.New("Handler Not Found")
  188. }
  189. //Get the ID part of a virtual path, return ID, subpath and error
  190. func getIDFromVirtualPath(vpath string) (string, string, error) {
  191. if strings.Contains(vpath, ":") == false {
  192. return "", "", errors.New("Path missing Virtual Device ID. Given: " + vpath)
  193. }
  194. //Clean up the virutal path
  195. vpath = filepath.ToSlash(filepath.Clean(vpath))
  196. tmp := strings.Split(vpath, ":")
  197. vdID := tmp[0]
  198. pathSlice := tmp[1:]
  199. path := strings.Join(pathSlice, ":")
  200. return vdID, path, nil
  201. }