webdavfs.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package webdavfs
  2. import (
  3. "errors"
  4. "io"
  5. "io/fs"
  6. "log"
  7. "os"
  8. "path/filepath"
  9. "regexp"
  10. "strings"
  11. "time"
  12. "github.com/studio-b12/gowebdav"
  13. )
  14. /*
  15. WebDAV Client
  16. This script is design as a wrapper of the studio-b12/gowebdav module
  17. that allow access to webdav network drive in ArozOS and allow arozos
  18. cross-mounting each others
  19. */
  20. type WebDAVFileSystem struct {
  21. UUID string
  22. Hierarchy string
  23. root string
  24. user string
  25. tmp string
  26. c *gowebdav.Client
  27. }
  28. type myFileType struct {
  29. io.Reader
  30. io.Closer
  31. }
  32. func NewWebDAVMount(UUID string, Hierarchy string, root string, user string, password string, tmpBuff string) (*WebDAVFileSystem, error) {
  33. //Connect to webdav server
  34. c := gowebdav.NewClient(root, user, password)
  35. err := c.Connect()
  36. if err != nil {
  37. log.Println("[WebDAV FS] Unable to connect to remote: ", err.Error())
  38. return nil, err
  39. } else {
  40. log.Println("[WebDAV FS] Connected to remote: " + root)
  41. }
  42. //Create tmp buff folder if not exists
  43. os.MkdirAll(tmpBuff, 0775)
  44. return &WebDAVFileSystem{
  45. UUID: UUID,
  46. Hierarchy: Hierarchy,
  47. c: c,
  48. root: root,
  49. user: user,
  50. tmp: tmpBuff,
  51. }, nil
  52. }
  53. func (e WebDAVFileSystem) Chmod(filename string, mode os.FileMode) error {
  54. return errors.New("filesystem type not supported")
  55. }
  56. func (e WebDAVFileSystem) Chown(filename string, uid int, gid int) error {
  57. return errors.New("filesystem type not supported")
  58. }
  59. func (e WebDAVFileSystem) Chtimes(filename string, atime time.Time, mtime time.Time) error {
  60. return errors.New("filesystem type not supported")
  61. }
  62. func (e WebDAVFileSystem) Create(filename string) (*os.File, error) {
  63. return nil, errors.New("filesystem type not supported")
  64. }
  65. func (e WebDAVFileSystem) Mkdir(filename string, mode os.FileMode) error {
  66. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  67. return e.c.Mkdir(filename, mode)
  68. }
  69. func (e WebDAVFileSystem) MkdirAll(filename string, mode os.FileMode) error {
  70. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  71. return e.c.MkdirAll(filename, mode)
  72. }
  73. func (e WebDAVFileSystem) Name() string {
  74. return ""
  75. }
  76. func (e WebDAVFileSystem) Open(filename string) (*os.File, error) {
  77. return nil, errors.New("filesystem type not supported")
  78. }
  79. func (e WebDAVFileSystem) OpenFile(filename string, flag int, perm os.FileMode) (*os.File, error) {
  80. //Buffer the target file to memory
  81. //To be implement: Wait for Golang's fs.File.Write function to be released
  82. //f := bufffs.New(filename)
  83. //return f, nil
  84. return nil, errors.New("filesystem type not supported")
  85. }
  86. func (e WebDAVFileSystem) Remove(filename string) error {
  87. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  88. return e.c.Remove(filename)
  89. }
  90. func (e WebDAVFileSystem) RemoveAll(filename string) error {
  91. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  92. return e.c.RemoveAll(filename)
  93. }
  94. func (e WebDAVFileSystem) Rename(oldname, newname string) error {
  95. oldname = filterFilepath(filepath.ToSlash(filepath.Clean(oldname)))
  96. newname = filterFilepath(filepath.ToSlash(filepath.Clean(newname)))
  97. return e.c.Rename(oldname, newname, true)
  98. }
  99. func (e WebDAVFileSystem) Stat(filename string) (os.FileInfo, error) {
  100. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  101. return e.c.Stat(filename)
  102. }
  103. func (e WebDAVFileSystem) VirtualPathToRealPath(subpath string, username string) (string, error) {
  104. subpath = filterFilepath(filepath.ToSlash(filepath.Clean(subpath)))
  105. if strings.HasPrefix(subpath, e.UUID+":") {
  106. //This is full virtual path. Trim the uuid and correct the subpath
  107. subpath = strings.TrimPrefix(subpath, e.UUID+":")
  108. }
  109. if e.Hierarchy == "user" {
  110. return filepath.ToSlash(filepath.Clean(filepath.Join("users", username, subpath))), nil
  111. } else if e.Hierarchy == "public" {
  112. return filepath.ToSlash(filepath.Clean(subpath)), nil
  113. }
  114. return "", errors.New("unsupported filesystem hierarchy")
  115. }
  116. func (e WebDAVFileSystem) RealPathToVirtualPath(rpath string, username string) (string, error) {
  117. rpath = filterFilepath(filepath.ToSlash(filepath.Clean(rpath)))
  118. if e.Hierarchy == "user" && strings.HasPrefix(rpath, "/users/"+username) {
  119. rpath = strings.TrimPrefix(rpath, "/users/"+username)
  120. }
  121. return e.UUID + ":" + filepath.ToSlash(rpath), nil
  122. }
  123. func (e WebDAVFileSystem) FileExists(filename string) bool {
  124. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  125. _, err := e.c.Stat(filename)
  126. if os.IsNotExist(err) || err != nil {
  127. return false
  128. }
  129. return true
  130. }
  131. func (e WebDAVFileSystem) IsDir(filename string) bool {
  132. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  133. s, err := e.c.Stat(filename)
  134. if err != nil {
  135. return false
  136. }
  137. return s.IsDir()
  138. }
  139. //Notes: This is not actual Glob function. This just emulate Glob using ReadDir with max depth 1 layer
  140. func (e WebDAVFileSystem) Glob(wildcard string) ([]string, error) {
  141. wildcard = filepath.ToSlash(filepath.Clean(wildcard))
  142. if !strings.HasPrefix(wildcard, "/") {
  143. //Handle case for listing root, "*"
  144. wildcard = "/" + wildcard
  145. }
  146. //Get the longest path without *
  147. chunksWithoutStar := []string{}
  148. chunks := strings.Split(wildcard, "/")
  149. for _, chunk := range chunks {
  150. if !strings.Contains(chunk, "*") {
  151. chunksWithoutStar = append(chunksWithoutStar, chunk)
  152. } else {
  153. //Cut off
  154. break
  155. }
  156. }
  157. if strings.Count(wildcard, "*") <= 1 && strings.Contains(chunks[len(chunks)-1], "*") {
  158. //Fast Glob
  159. fileInfos, err := e.c.ReadDir(filterFilepath(filepath.ToSlash(filepath.Clean(filepath.Dir(wildcard)))))
  160. if err != nil {
  161. return []string{}, err
  162. }
  163. validFiles := []string{}
  164. matchingRule := wildCardToRegexp(wildcard)
  165. for _, thisFileInfo := range fileInfos {
  166. thisFileFullpath := filepath.ToSlash(filepath.Join(filepath.Dir(wildcard), thisFileInfo.Name()))
  167. match, _ := regexp.MatchString(matchingRule, thisFileFullpath)
  168. if match {
  169. validFiles = append(validFiles, thisFileFullpath)
  170. }
  171. }
  172. return validFiles, nil
  173. } else {
  174. //Slow Glob
  175. walkRoot := strings.Join(chunksWithoutStar, "/")
  176. if !strings.HasPrefix(walkRoot, "/") {
  177. walkRoot = "/" + walkRoot
  178. }
  179. allFiles := []string{}
  180. e.Walk(walkRoot, func(path string, info fs.FileInfo, err error) error {
  181. allFiles = append(allFiles, path)
  182. return nil
  183. })
  184. validFiles := []string{}
  185. matchingRule := wildCardToRegexp(wildcard) + "$"
  186. for _, thisFilepath := range allFiles {
  187. match, _ := regexp.MatchString(matchingRule, thisFilepath)
  188. if match {
  189. validFiles = append(validFiles, thisFilepath)
  190. }
  191. }
  192. return validFiles, nil
  193. }
  194. }
  195. func (e WebDAVFileSystem) GetFileSize(filename string) int64 {
  196. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  197. s, err := e.Stat(filename)
  198. if err != nil {
  199. log.Println(err)
  200. return 0
  201. }
  202. return s.Size()
  203. }
  204. func (e WebDAVFileSystem) GetModTime(filename string) (int64, error) {
  205. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  206. s, err := e.Stat(filename)
  207. if err != nil {
  208. return 0, err
  209. }
  210. return s.ModTime().Unix(), nil
  211. }
  212. func (e WebDAVFileSystem) WriteFile(filename string, content []byte, mode os.FileMode) error {
  213. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  214. return e.c.Write(filename, content, mode)
  215. }
  216. func (e WebDAVFileSystem) ReadFile(filename string) ([]byte, error) {
  217. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  218. bytes, err := e.c.Read(filename)
  219. if err != nil {
  220. return []byte(""), err
  221. }
  222. return bytes, nil
  223. }
  224. func (e WebDAVFileSystem) WriteStream(filename string, stream io.Reader, mode os.FileMode) error {
  225. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  226. return e.c.WriteStream(filename, stream, mode)
  227. }
  228. func (e WebDAVFileSystem) ReadStream(filename string) (io.ReadCloser, error) {
  229. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  230. return e.c.ReadStream(filename)
  231. }
  232. func (e WebDAVFileSystem) Walk(rootpath string, walkFn filepath.WalkFunc) error {
  233. rootpath = filepath.ToSlash(filepath.Clean(rootpath))
  234. rootStat, err := e.Stat(rootpath)
  235. err = walkFn(rootpath, rootStat, err)
  236. if err != nil {
  237. return err
  238. }
  239. return e.walk(rootpath, walkFn)
  240. }
  241. /*
  242. Helper Functions
  243. */
  244. func (e WebDAVFileSystem) walk(thisPath string, walkFun filepath.WalkFunc) error {
  245. files, err := e.c.ReadDir(thisPath)
  246. if err != nil {
  247. return err
  248. }
  249. for _, file := range files {
  250. thisFileFullPath := filepath.ToSlash(filepath.Join(thisPath, file.Name()))
  251. if file.IsDir() {
  252. err = walkFun(thisFileFullPath, file, nil)
  253. if err != nil {
  254. return err
  255. }
  256. err = e.walk(thisFileFullPath, walkFun)
  257. if err != nil {
  258. return err
  259. }
  260. } else {
  261. err = walkFun(thisFileFullPath, file, nil)
  262. if err != nil {
  263. return err
  264. }
  265. }
  266. }
  267. return nil
  268. }
  269. /*
  270. func (e WebDAVFileSystem) globscan(currentPath string, wildcardChunks []string, layer int) ([]string, error) {
  271. if layer >= len(wildcardChunks) {
  272. return []string{}, nil
  273. }
  274. }
  275. */
  276. func filterFilepath(rawpath string) string {
  277. rawpath = strings.TrimSpace(rawpath)
  278. if strings.HasPrefix(rawpath, "./") {
  279. return rawpath[1:]
  280. } else if rawpath == "." || rawpath == "" {
  281. return "/"
  282. }
  283. return rawpath
  284. }
  285. func wildCardToRegexp(pattern string) string {
  286. var result strings.Builder
  287. for i, literal := range strings.Split(pattern, "*") {
  288. // Replace * with .*
  289. if i > 0 {
  290. result.WriteString(".*")
  291. }
  292. // Quote any regular expression meta characters in the
  293. // literal text.
  294. result.WriteString(regexp.QuoteMeta(literal))
  295. }
  296. return result.String()
  297. }