directoryHandler.go 6.8 KB

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