webdavfs.go 9.4 KB

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