directoryHandler.go 7.2 KB

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