arozfs.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 == "nfs" {
  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", "nfs"}
  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(subpath))
  85. subpath = ToSlash(filepath.Clean(strings.TrimSpace(subpath)))
  86. if strings.HasPrefix(subpath, "./") {
  87. subpath = subpath[1:]
  88. }
  89. if subpath == "." || subpath == "" {
  90. subpath = "/"
  91. }
  92. if strings.HasPrefix(subpath, uuid+":") {
  93. //This is full virtual path. Trim the uuid and correct the subpath
  94. subpath = strings.TrimPrefix(subpath, uuid+":")
  95. }
  96. if hierarchy == "user" {
  97. return filepath.ToSlash(filepath.Clean(filepath.Join("users", username, subpath))), nil
  98. } else if hierarchy == "public" {
  99. return filepath.ToSlash(filepath.Clean(subpath)), nil
  100. }
  101. return "", errors.New("unsupported filesystem hierarchy")
  102. }
  103. //Generic real path to virtual path translator
  104. func GenericRealPathToVirtualPathTranslator(uuid string, hierarchy string, rpath string, username string) (string, error) {
  105. rpath = ToSlash(filepath.Clean(strings.TrimSpace(rpath)))
  106. if strings.HasPrefix(rpath, "./") {
  107. rpath = rpath[1:]
  108. }
  109. if rpath == "." || rpath == "" {
  110. rpath = "/"
  111. }
  112. if hierarchy == "user" && strings.HasPrefix(rpath, "/users/"+username) {
  113. rpath = strings.TrimPrefix(rpath, "/users/"+username)
  114. }
  115. rpath = filepath.ToSlash(rpath)
  116. if !strings.HasPrefix(rpath, "/") {
  117. rpath = "/" + rpath
  118. }
  119. return uuid + ":" + rpath, nil
  120. }
  121. //Filter illegal characters in filename
  122. func FilterIllegalCharInFilename(filename string, replacement string) string {
  123. re, _ := regexp.Compile(`[\\\[\]$?#<>+%!"'|{}:@]`)
  124. return re.ReplaceAllString(filename, replacement)
  125. }
  126. /*
  127. OS Independent filepath functions
  128. */
  129. func ToSlash(filename string) string {
  130. return strings.ReplaceAll(filename, "\\", "/")
  131. }
  132. func Base(filename string) string {
  133. filename = ToSlash(filename)
  134. if filename == "" {
  135. return "."
  136. }
  137. if filename == "/" {
  138. return filename
  139. }
  140. for len(filename) > 0 && filename[len(filename)-1] == '/' {
  141. filename = filename[0 : len(filename)-1]
  142. }
  143. c := strings.Split(filename, "/")
  144. if len(c) == 1 {
  145. return c[0]
  146. } else {
  147. return c[len(c)-1]
  148. }
  149. }