localFilesystemAbstraction.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package filesystem
  2. import (
  3. "errors"
  4. "io"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "time"
  10. "imuslab.com/arozos/mod/common"
  11. )
  12. /*
  13. filesystemAbstraction.go
  14. This file contains all the abstraction funtion of a local file system.
  15. */
  16. type LocalFileSystemAbstraction struct {
  17. UUID string
  18. Rootpath string
  19. Hierarchy string
  20. ReadOnly bool
  21. }
  22. func NewLocalFileSystemAbstraction(uuid, root, hierarchy string, readonly bool) LocalFileSystemAbstraction {
  23. return LocalFileSystemAbstraction{
  24. UUID: uuid,
  25. Rootpath: root,
  26. Hierarchy: hierarchy,
  27. ReadOnly: readonly,
  28. }
  29. }
  30. func (l LocalFileSystemAbstraction) Chmod(filename string, mode os.FileMode) error {
  31. return os.Chmod(filename, mode)
  32. }
  33. func (l LocalFileSystemAbstraction) Chown(filename string, uid int, gid int) error {
  34. return os.Chown(filename, uid, gid)
  35. }
  36. func (l LocalFileSystemAbstraction) Chtimes(filename string, atime time.Time, mtime time.Time) error {
  37. return os.Chtimes(filename, atime, mtime)
  38. }
  39. func (l LocalFileSystemAbstraction) Create(filename string) (*os.File, error) {
  40. return os.Create(filename)
  41. }
  42. func (l LocalFileSystemAbstraction) Mkdir(filename string, mode os.FileMode) error {
  43. return os.Mkdir(filename, mode)
  44. }
  45. func (l LocalFileSystemAbstraction) MkdirAll(filename string, mode os.FileMode) error {
  46. return os.MkdirAll(filename, mode)
  47. }
  48. func (l LocalFileSystemAbstraction) Name() string {
  49. return ""
  50. }
  51. func (l LocalFileSystemAbstraction) Open(filename string) (*os.File, error) {
  52. return os.Open(filename)
  53. }
  54. func (l LocalFileSystemAbstraction) OpenFile(filename string, flag int, perm os.FileMode) (*os.File, error) {
  55. return os.OpenFile(filename, flag, perm)
  56. }
  57. func (l LocalFileSystemAbstraction) Remove(filename string) error {
  58. return os.Remove(filename)
  59. }
  60. func (l LocalFileSystemAbstraction) RemoveAll(path string) error {
  61. return os.RemoveAll(path)
  62. }
  63. func (l LocalFileSystemAbstraction) Rename(oldname, newname string) error {
  64. return os.Rename(oldname, newname)
  65. }
  66. func (l LocalFileSystemAbstraction) Stat(filename string) (os.FileInfo, error) {
  67. return os.Stat(filename)
  68. }
  69. /*
  70. Abstraction Utilities
  71. */
  72. func (l LocalFileSystemAbstraction) VirtualPathToRealPath(subpath string, username string) (string, error) {
  73. if strings.HasPrefix(subpath, l.UUID+":/") {
  74. //This is full virtual path. Trim the uuid and correct the subpath
  75. subpath = subpath[len(l.UUID+":/"):]
  76. }
  77. if l.Hierarchy == "user" {
  78. return filepath.ToSlash(filepath.Join(l.Rootpath, "users", username, subpath)), nil
  79. } else if l.Hierarchy == "public" {
  80. return filepath.ToSlash(filepath.Join(l.Rootpath, subpath)), nil
  81. }
  82. return "", errors.New("unsupported filesystem hierarchy")
  83. }
  84. func (l LocalFileSystemAbstraction) RealPathToVirtualPath(fullpath string, username string) (string, error) {
  85. thisStorageRootAbs, err := filepath.Abs(l.Rootpath)
  86. if err != nil {
  87. //Fail to abs this path. Maybe this is a emulated file system?
  88. thisStorageRootAbs = l.Rootpath
  89. }
  90. thisStorageRootAbs = filepath.ToSlash(filepath.Clean(thisStorageRootAbs))
  91. subPath := ""
  92. if len(fullpath) > len(l.Rootpath) && filepath.ToSlash(fullpath[:len(l.Rootpath)]) == filepath.ToSlash(l.Rootpath) {
  93. //This realpath is in contained inside this storage root
  94. subtractionPath := l.Rootpath
  95. if l.Hierarchy == "user" {
  96. //Check if this file is belongs to this user
  97. startOffset := len(filepath.Clean(l.Rootpath) + "/users/")
  98. if len(fullpath) < startOffset+len(username) {
  99. //This file is not owned by this user
  100. return "", errors.New("File not owned by this user")
  101. } else {
  102. userNameMatch := fullpath[startOffset : startOffset+len(username)]
  103. if userNameMatch != username {
  104. //This file is not owned by this user
  105. return "", errors.New("File not owned by this user")
  106. }
  107. }
  108. //Generate subtraction path
  109. subtractionPath = filepath.ToSlash(filepath.Clean(filepath.Join(l.Rootpath, "users", username)))
  110. }
  111. if len(subtractionPath) < len(fullpath) {
  112. subPath = fullpath[len(subtractionPath):]
  113. }
  114. } else if len(fullpath) > len(thisStorageRootAbs) && filepath.ToSlash(fullpath[:len(thisStorageRootAbs)]) == filepath.ToSlash(thisStorageRootAbs) {
  115. //The realpath contains the absolute path of this storage root
  116. subtractionPath := thisStorageRootAbs
  117. if l.Hierarchy == "user" {
  118. subtractionPath = thisStorageRootAbs + "/users/" + username + "/"
  119. }
  120. if len(subtractionPath) < len(fullpath) {
  121. subPath = fullpath[len(subtractionPath):]
  122. }
  123. } else if filepath.ToSlash(fullpath) == filepath.ToSlash(l.Rootpath) {
  124. //Storage Root's root
  125. subPath = ""
  126. }
  127. if len(subPath) > 1 && subPath[:1] == "/" {
  128. subPath = subPath[1:]
  129. }
  130. return l.UUID + ":" + filepath.ToSlash(subPath), nil
  131. }
  132. func (l LocalFileSystemAbstraction) FileExists(realpath string) bool {
  133. return common.FileExists(realpath)
  134. }
  135. func (l LocalFileSystemAbstraction) IsDir(realpath string) bool {
  136. if !l.FileExists(realpath) {
  137. return false
  138. }
  139. fi, err := l.Stat(realpath)
  140. if err != nil {
  141. log.Fatal(err)
  142. return false
  143. }
  144. switch mode := fi.Mode(); {
  145. case mode.IsDir():
  146. return true
  147. case mode.IsRegular():
  148. return false
  149. }
  150. return false
  151. }
  152. func (l LocalFileSystemAbstraction) Glob(realpathWildcard string) ([]string, error) {
  153. return filepath.Glob(realpathWildcard)
  154. }
  155. func (l LocalFileSystemAbstraction) GetFileSize(realpath string) int64 {
  156. fi, err := os.Stat(realpath)
  157. if err != nil {
  158. return 0
  159. }
  160. // get the size
  161. return fi.Size()
  162. }
  163. func (l LocalFileSystemAbstraction) GetModTime(realpath string) (int64, error) {
  164. f, err := os.Open(realpath)
  165. if err != nil {
  166. return -1, err
  167. }
  168. statinfo, err := f.Stat()
  169. if err != nil {
  170. return -1, err
  171. }
  172. f.Close()
  173. return statinfo.ModTime().Unix(), nil
  174. }
  175. func (l LocalFileSystemAbstraction) WriteFile(filename string, content []byte, mode os.FileMode) error {
  176. return os.WriteFile(filename, content, mode)
  177. }
  178. func (l LocalFileSystemAbstraction) ReadFile(filename string) ([]byte, error) {
  179. return os.ReadFile(filename)
  180. }
  181. func (l LocalFileSystemAbstraction) WriteStream(filename string, stream io.Reader, mode os.FileMode) error {
  182. f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, mode)
  183. if err != nil {
  184. return err
  185. }
  186. _, err = io.Copy(f, stream)
  187. defer f.Close()
  188. return err
  189. }
  190. func (l LocalFileSystemAbstraction) ReadStream(filename string) (io.ReadCloser, error) {
  191. f, err := os.OpenFile(filename, os.O_RDONLY, 0644)
  192. if err != nil {
  193. return nil, err
  194. }
  195. defer f.Close()
  196. return f, nil
  197. }
  198. func (l LocalFileSystemAbstraction) Walk(root string, walkFn filepath.WalkFunc) error {
  199. return filepath.Walk(root, walkFn)
  200. }