localfs.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package localfs
  2. import (
  3. "io"
  4. "io/fs"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "time"
  9. "imuslab.com/arozos/mod/filesystem/arozfs"
  10. "imuslab.com/arozos/mod/utils"
  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) (arozfs.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) (arozfs.File, error) {
  52. return os.Open(filename)
  53. }
  54. func (l LocalFileSystemAbstraction) OpenFile(filename string, flag int, perm os.FileMode) (arozfs.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. func (l LocalFileSystemAbstraction) Close() error {
  70. return nil
  71. }
  72. /*
  73. Abstraction Utilities
  74. */
  75. func (l LocalFileSystemAbstraction) VirtualPathToRealPath(subpath string, username string) (string, error) {
  76. subpath = filepath.ToSlash(subpath)
  77. if strings.HasPrefix(subpath, l.UUID+":") {
  78. //This is full virtual path. Trim the uuid and correct the subpath
  79. subpath = subpath[len(l.UUID+":"):]
  80. }
  81. if l.Hierarchy == "user" {
  82. return filepath.ToSlash(filepath.Join(l.Rootpath, "users", username, subpath)), nil
  83. } else if l.Hierarchy == "public" {
  84. return filepath.ToSlash(filepath.Join(l.Rootpath, subpath)), nil
  85. }
  86. return "", arozfs.ErrVpathResolveFailed
  87. }
  88. func (l LocalFileSystemAbstraction) RealPathToVirtualPath(fullpath string, username string) (string, error) {
  89. /*
  90. thisStorageRootAbs, err := filepath.Abs(l.Rootpath)
  91. if err != nil {
  92. //Fail to abs this path. Maybe this is a emulated file system?
  93. thisStorageRootAbs = l.Rootpath
  94. }
  95. thisStorageRootAbs = filepath.ToSlash(filepath.Clean(thisStorageRootAbs))
  96. subPath := ""
  97. if len(fullpath) > len(l.Rootpath) && filepath.ToSlash(fullpath[:len(l.Rootpath)]) == filepath.ToSlash(l.Rootpath) {
  98. //This realpath is in contained inside this storage root
  99. subtractionPath := l.Rootpath
  100. if l.Hierarchy == "user" {
  101. //Check if this file is belongs to this user
  102. startOffset := len(filepath.Clean(l.Rootpath) + "/users/")
  103. if len(fullpath) < startOffset+len(username) {
  104. //This file is not owned by this user
  105. return "", errors.New("File not owned by this user")
  106. } else {
  107. userNameMatch := fullpath[startOffset : startOffset+len(username)]
  108. if userNameMatch != username {
  109. //This file is not owned by this user
  110. return "", errors.New("File not owned by this user")
  111. }
  112. }
  113. //Generate subtraction path
  114. subtractionPath = filepath.ToSlash(filepath.Clean(filepath.Join(l.Rootpath, "users", username)))
  115. }
  116. if len(subtractionPath) < len(fullpath) {
  117. subPath = fullpath[len(subtractionPath):]
  118. }
  119. } else if len(fullpath) > len(thisStorageRootAbs) && filepath.ToSlash(fullpath[:len(thisStorageRootAbs)]) == filepath.ToSlash(thisStorageRootAbs) {
  120. //The realpath contains the absolute path of this storage root
  121. subtractionPath := thisStorageRootAbs
  122. if l.Hierarchy == "user" {
  123. subtractionPath = thisStorageRootAbs + "/users/" + username + "/"
  124. }
  125. if len(subtractionPath) < len(fullpath) {
  126. subPath = fullpath[len(subtractionPath):]
  127. }
  128. } else if filepath.ToSlash(fullpath) == filepath.ToSlash(l.Rootpath) {
  129. //Storage Root's root
  130. subPath = ""
  131. }
  132. if len(subPath) > 1 && subPath[:1] == "/" {
  133. subPath = subPath[1:]
  134. }
  135. */
  136. fullpath = filepath.ToSlash(fullpath)
  137. if strings.HasPrefix(fullpath, l.UUID+":") && !utils.FileExists(fullpath) {
  138. return "", arozfs.ErrRpathResolveFailed
  139. }
  140. prefix := filepath.ToSlash(filepath.Join(l.Rootpath, "users", username))
  141. if l.Hierarchy == "public" {
  142. prefix = filepath.ToSlash(l.Rootpath)
  143. }
  144. fullpath = filepath.ToSlash(filepath.Clean(fullpath))
  145. subPath := strings.TrimPrefix(fullpath, prefix)
  146. if !strings.HasPrefix(subPath, "/") {
  147. subPath = "/" + subPath
  148. }
  149. return l.UUID + ":" + filepath.ToSlash(subPath), nil
  150. }
  151. func (l LocalFileSystemAbstraction) FileExists(realpath string) bool {
  152. return utils.FileExists(realpath)
  153. }
  154. func (l LocalFileSystemAbstraction) IsDir(realpath string) bool {
  155. if !l.FileExists(realpath) {
  156. return false
  157. }
  158. fi, err := l.Stat(realpath)
  159. if err != nil {
  160. return false
  161. }
  162. switch mode := fi.Mode(); {
  163. case mode.IsDir():
  164. return true
  165. case mode.IsRegular():
  166. return false
  167. }
  168. return false
  169. }
  170. func (l LocalFileSystemAbstraction) Glob(realpathWildcard string) ([]string, error) {
  171. return filepath.Glob(realpathWildcard)
  172. }
  173. func (l LocalFileSystemAbstraction) GetFileSize(realpath string) int64 {
  174. fi, err := os.Stat(realpath)
  175. if err != nil {
  176. return 0
  177. }
  178. // get the size
  179. return fi.Size()
  180. }
  181. func (l LocalFileSystemAbstraction) GetModTime(realpath string) (int64, error) {
  182. f, err := os.Open(realpath)
  183. if err != nil {
  184. return -1, err
  185. }
  186. statinfo, err := f.Stat()
  187. if err != nil {
  188. return -1, err
  189. }
  190. f.Close()
  191. return statinfo.ModTime().Unix(), nil
  192. }
  193. func (l LocalFileSystemAbstraction) WriteFile(filename string, content []byte, mode os.FileMode) error {
  194. return os.WriteFile(filename, content, mode)
  195. }
  196. func (l LocalFileSystemAbstraction) ReadFile(filename string) ([]byte, error) {
  197. return os.ReadFile(filename)
  198. }
  199. func (l LocalFileSystemAbstraction) ReadDir(filename string) ([]fs.DirEntry, error) {
  200. return os.ReadDir(filename)
  201. }
  202. func (l LocalFileSystemAbstraction) WriteStream(filename string, stream io.Reader, mode os.FileMode) error {
  203. f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, mode)
  204. if err != nil {
  205. return err
  206. }
  207. _, err = io.Copy(f, stream)
  208. f.Close()
  209. return err
  210. }
  211. func (l LocalFileSystemAbstraction) ReadStream(filename string) (io.ReadCloser, error) {
  212. f, err := os.OpenFile(filename, os.O_RDONLY, 0644)
  213. if err != nil {
  214. return nil, err
  215. }
  216. return f, nil
  217. }
  218. func (l LocalFileSystemAbstraction) Walk(root string, walkFn filepath.WalkFunc) error {
  219. return filepath.Walk(root, walkFn)
  220. }
  221. func (l LocalFileSystemAbstraction) Heartbeat() error {
  222. return nil
  223. }