arozfs.go 4.7 KB

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