localfs.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. rpath, err := arozfs.GenericVirtualPathToRealPathTranslator(l.UUID, l.Hierarchy, subpath, username)
  77. if err != nil {
  78. return "", err
  79. }
  80. //Append the local root path to the translated path to allow read write from os package
  81. rpath = filepath.Join(l.Rootpath, rpath)
  82. //fmt.Println("VIRTUAL TO REAL", l.Rootpath, subpath, rpath)
  83. return rpath, nil
  84. }
  85. func (l LocalFileSystemAbstraction) RealPathToVirtualPath(fullpath string, username string) (string, error) {
  86. //Trim the absolute / relative path before passing into generic translator
  87. fullpath = arozfs.ToSlash(fullpath)
  88. if strings.HasPrefix(fullpath, arozfs.ToSlash(filepath.Clean(l.Rootpath))) {
  89. fullpath = strings.TrimPrefix(fullpath, arozfs.ToSlash(filepath.Clean(l.Rootpath)))
  90. }
  91. vpath, err := arozfs.GenericRealPathToVirtualPathTranslator(l.UUID, l.Hierarchy, arozfs.ToSlash(fullpath), username)
  92. //fmt.Println("REAL TO VIRTUAL", arozfs.ToSlash(filepath.Clean(l.Rootpath)), fullpath, vpath)
  93. return vpath, err
  94. }
  95. func (l LocalFileSystemAbstraction) FileExists(realpath string) bool {
  96. return utils.FileExists(realpath)
  97. }
  98. func (l LocalFileSystemAbstraction) IsDir(realpath string) bool {
  99. if !l.FileExists(realpath) {
  100. return false
  101. }
  102. fi, err := l.Stat(realpath)
  103. if err != nil {
  104. return false
  105. }
  106. switch mode := fi.Mode(); {
  107. case mode.IsDir():
  108. return true
  109. case mode.IsRegular():
  110. return false
  111. }
  112. return false
  113. }
  114. func (l LocalFileSystemAbstraction) Glob(realpathWildcard string) ([]string, error) {
  115. return filepath.Glob(realpathWildcard)
  116. }
  117. func (l LocalFileSystemAbstraction) GetFileSize(realpath string) int64 {
  118. fi, err := os.Stat(realpath)
  119. if err != nil {
  120. return 0
  121. }
  122. // get the size
  123. return fi.Size()
  124. }
  125. func (l LocalFileSystemAbstraction) GetModTime(realpath string) (int64, error) {
  126. f, err := os.Open(realpath)
  127. if err != nil {
  128. return -1, err
  129. }
  130. statinfo, err := f.Stat()
  131. if err != nil {
  132. return -1, err
  133. }
  134. f.Close()
  135. return statinfo.ModTime().Unix(), nil
  136. }
  137. func (l LocalFileSystemAbstraction) WriteFile(filename string, content []byte, mode os.FileMode) error {
  138. return os.WriteFile(filename, content, mode)
  139. }
  140. func (l LocalFileSystemAbstraction) ReadFile(filename string) ([]byte, error) {
  141. return os.ReadFile(filename)
  142. }
  143. func (l LocalFileSystemAbstraction) ReadDir(filename string) ([]fs.DirEntry, error) {
  144. return os.ReadDir(filename)
  145. }
  146. func (l LocalFileSystemAbstraction) WriteStream(filename string, stream io.Reader, mode os.FileMode) error {
  147. f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, mode)
  148. if err != nil {
  149. return err
  150. }
  151. _, err = io.Copy(f, stream)
  152. f.Close()
  153. return err
  154. }
  155. func (l LocalFileSystemAbstraction) ReadStream(filename string) (io.ReadCloser, error) {
  156. f, err := os.OpenFile(filename, os.O_RDONLY, 0644)
  157. if err != nil {
  158. return nil, err
  159. }
  160. return f, nil
  161. }
  162. func (l LocalFileSystemAbstraction) Walk(root string, walkFn filepath.WalkFunc) error {
  163. return filepath.Walk(root, walkFn)
  164. }
  165. func (l LocalFileSystemAbstraction) Heartbeat() error {
  166. return nil
  167. }