localfs.go 6.8 KB

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