arozfs.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package arozfs
  2. /*
  3. arozfs.go
  4. This package handle error related to file systems.
  5. See comments below for usage.
  6. */
  7. import (
  8. "errors"
  9. "io"
  10. "io/fs"
  11. "path/filepath"
  12. "strings"
  13. )
  14. type File interface {
  15. Chdir() error
  16. Chmod(mode fs.FileMode) error
  17. Chown(uid, gid int) error
  18. Close() error
  19. Name() string
  20. Read(b []byte) (n int, err error)
  21. ReadAt(b []byte, off int64) (n int, err error)
  22. Readdirnames(n int) (names []string, err error)
  23. ReadFrom(r io.Reader) (n int64, err error)
  24. Readdir(n int) ([]fs.FileInfo, error)
  25. Seek(offset int64, whence int) (ret int64, err error)
  26. Stat() (fs.FileInfo, error)
  27. Sync() error
  28. Truncate(size int64) error
  29. Write(b []byte) (n int, err error)
  30. WriteAt(b []byte, off int64) (n int, err error)
  31. WriteString(s string) (n int, err error)
  32. }
  33. //A shortcut representing struct
  34. type ShortcutData struct {
  35. Type string //The type of shortcut
  36. Name string //The name of the shortcut
  37. Path string //The path of shortcut
  38. Icon string //The icon of shortcut
  39. }
  40. var (
  41. /*
  42. READ WRITE PERMISSIONS
  43. */
  44. FsReadOnly = "readonly"
  45. FsWriteOnly = "writeonly"
  46. FsReadWrite = "readwrite"
  47. FsDenied = "denied"
  48. /*
  49. ERROR TYPES
  50. */
  51. //Redirective Error
  52. ErrRedirectParent = errors.New("Redirect:parent")
  53. ErrRedirectCurrentRoot = errors.New("Redirect:root")
  54. ErrRedirectUserRoot = errors.New("Redirect:userroot")
  55. //Resolve errors
  56. ErrVpathResolveFailed = errors.New("FS_VPATH_RESOLVE_FAILED")
  57. ErrRpathResolveFailed = errors.New("FS_RPATH_RESOLVE_FAILED")
  58. ErrFSHNotFOund = errors.New("FS_FILESYSTEM_HANDLER_NOT_FOUND")
  59. //Operation errors
  60. ErrOperationNotSupported = errors.New("FS_OPR_NOT_SUPPORTED")
  61. ErrNullOperation = errors.New("FS_NULL_OPR")
  62. )
  63. //Generate a File Manager redirection error message
  64. func NewRedirectionError(targetVpath string) error {
  65. return errors.New("Redirect:" + targetVpath)
  66. }
  67. //Check if a file system is network drive
  68. func IsNetworkDrive(fstype string) bool {
  69. if fstype == "webdav" || fstype == "ftp" || fstype == "smb" {
  70. return true
  71. }
  72. return false
  73. }
  74. //Get a list of supported file system types for mounting via arozos
  75. func GetSupportedFileSystemTypes() []string {
  76. return []string{"ext4", "ext2", "ext3", "fat", "vfat", "ntfs", "webdav", "ftp", "smb"}
  77. }
  78. /*
  79. Standard file system abstraction translate function
  80. */
  81. //Generic virtual path to real path translator
  82. func GenericVirtualPathToRealPathTranslator(uuid string, hierarchy string, subpath string, username string) (string, error) {
  83. subpath = ToSlash(filepath.Clean(subpath))
  84. subpath = ToSlash(filepath.Clean(strings.TrimSpace(subpath)))
  85. if strings.HasPrefix(subpath, "./") {
  86. subpath = subpath[1:]
  87. }
  88. if subpath == "." || subpath == "" {
  89. subpath = "/"
  90. }
  91. if strings.HasPrefix(subpath, uuid+":") {
  92. //This is full virtual path. Trim the uuid and correct the subpath
  93. subpath = strings.TrimPrefix(subpath, uuid+":")
  94. }
  95. if hierarchy == "user" {
  96. return filepath.ToSlash(filepath.Clean(filepath.Join("users", username, subpath))), nil
  97. } else if hierarchy == "public" {
  98. return filepath.ToSlash(filepath.Clean(subpath)), nil
  99. }
  100. return "", errors.New("unsupported filesystem hierarchy")
  101. }
  102. //Generic real path to virtual path translator
  103. func GenericRealPathToVirtualPathTranslator(uuid string, hierarchy string, rpath string, username string) (string, error) {
  104. rpath = ToSlash(filepath.Clean(strings.TrimSpace(rpath)))
  105. if strings.HasPrefix(rpath, "./") {
  106. rpath = rpath[1:]
  107. }
  108. if rpath == "." || rpath == "" {
  109. rpath = "/"
  110. }
  111. if hierarchy == "user" && strings.HasPrefix(rpath, "/users/"+username) {
  112. rpath = strings.TrimPrefix(rpath, "/users/"+username)
  113. }
  114. rpath = filepath.ToSlash(rpath)
  115. if !strings.HasPrefix(rpath, "/") {
  116. rpath = "/" + rpath
  117. }
  118. return uuid + ":" + rpath, nil
  119. }
  120. /*
  121. OS Independent filepath functions
  122. */
  123. func ToSlash(filename string) string {
  124. return strings.ReplaceAll(filename, "\\", "/")
  125. }
  126. func Base(filename string) string {
  127. filename = ToSlash(filename)
  128. if filename == "" {
  129. return "."
  130. }
  131. if filename == "/" {
  132. return filename
  133. }
  134. for len(filename) > 0 && filename[len(filename)-1] == '/' {
  135. filename = filename[0 : len(filename)-1]
  136. }
  137. c := strings.Split(filename, "/")
  138. if len(c) == 1 {
  139. return c[0]
  140. } else {
  141. return c[len(c)-1]
  142. }
  143. }