directoryHandler.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. //Check if this file is belongs to this user
  123. startOffset := len(filepath.Clean(thisStorageRoot) + "/users/")
  124. if len(realPath) < startOffset+len(u.Username) {
  125. //This file is not owned by this user
  126. return "", errors.New("File not owned by this user")
  127. } else {
  128. userNameMatch := realPath[startOffset : startOffset+len(u.Username)]
  129. if userNameMatch != u.Username {
  130. //This file is not owned by this user
  131. return "", errors.New("File not owned by this user")
  132. }
  133. }
  134. //Generate subtraction path
  135. subtractionPath = thisStorageRoot + "/users/" + u.Username + "/"
  136. }
  137. if len(subtractionPath) < len(realPath) {
  138. subPath = realPath[len(subtractionPath):]
  139. }
  140. } else if len(realPath) > len(thisStorageRootAbs) && filepath.ToSlash(realPath[:len(thisStorageRootAbs)]) == filepath.ToSlash(thisStorageRootAbs) {
  141. //The realpath contains the absolute path of this storage root
  142. pathContained = true
  143. subtractionPath := thisStorageRootAbs
  144. if storage.Hierarchy == "user" {
  145. subtractionPath = thisStorageRootAbs + "/users/" + u.Username + "/"
  146. }
  147. if len(subtractionPath) < len(realPath) {
  148. subPath = realPath[len(subtractionPath):]
  149. }
  150. } else if filepath.ToSlash(realPath) == filepath.ToSlash(thisStorageRoot) {
  151. //Storage Root's root
  152. pathContained = true
  153. subPath = ""
  154. }
  155. if len(subPath) > 1 && subPath[:1] == "/" {
  156. subPath = subPath[1:]
  157. }
  158. if pathContained == true {
  159. //This storage is one of the root of the given realpath. Translate it into this
  160. if storage.Closed == true {
  161. return "", errors.New("Request Filesystem Handler has been closed by another process")
  162. }
  163. return storage.UUID + ":/" + subPath, nil
  164. }
  165. }
  166. return "", errors.New("Unable to resolve realpath in virtual devices root path")
  167. }
  168. //Get a file system handler from a virtual path, this file system handler might not be the highest prioity one
  169. func (u *User) GetFileSystemHandlerFromVirtualPath(vpath string) (*fs.FileSystemHandler, error) {
  170. fsHandlers := u.GetAllFileSystemHandler()
  171. handler, err := getHandlerFromVirtualPath(fsHandlers, vpath)
  172. return handler, err
  173. }
  174. func (u *User) GetFileSystemHandlerFromRealPath(rpath string) (*fs.FileSystemHandler, error) {
  175. vpath, err := u.RealPathToVirtualPath(rpath)
  176. if err != nil {
  177. return &fs.FileSystemHandler{}, err
  178. }
  179. return u.GetFileSystemHandlerFromVirtualPath(vpath)
  180. }
  181. /*
  182. PRIVATE FUNCTIONS HANDLERS
  183. */
  184. //Get a fs handler from a virtual path, quick function for getIDFromHandler + GetHandlerFromID
  185. func getHandlerFromVirtualPath(storages []*fs.FileSystemHandler, vpath string) (*fs.FileSystemHandler, error) {
  186. vid, _, err := getIDFromVirtualPath(vpath)
  187. if err != nil {
  188. return &fs.FileSystemHandler{}, err
  189. }
  190. return getHandlerFromID(storages, vid)
  191. }
  192. //Get a fs handler from the given virtial device id
  193. func getHandlerFromID(storages []*fs.FileSystemHandler, vid string) (*fs.FileSystemHandler, error) {
  194. for _, storage := range storages {
  195. if storage.UUID == vid {
  196. //This storage is the one we are looking at
  197. return storage, nil
  198. }
  199. }
  200. return &fs.FileSystemHandler{}, errors.New("Handler Not Found")
  201. }
  202. //Get the ID part of a virtual path, return ID, subpath and error
  203. func getIDFromVirtualPath(vpath string) (string, string, error) {
  204. if strings.Contains(vpath, ":") == false {
  205. return "", "", errors.New("Path missing Virtual Device ID. Given: " + vpath)
  206. }
  207. //Clean up the virutal path
  208. vpath = filepath.ToSlash(filepath.Clean(vpath))
  209. tmp := strings.Split(vpath, ":")
  210. vdID := tmp[0]
  211. pathSlice := tmp[1:]
  212. path := strings.Join(pathSlice, ":")
  213. return vdID, path, nil
  214. }