webdavfs.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. err := e.c.Rename(oldname, newname, true)
  98. if err != nil {
  99. //Unable to rename due to reverse proxy issue. Use Copy and Delete
  100. f, err := e.c.ReadStream(oldname)
  101. if err != nil {
  102. return err
  103. }
  104. err = e.c.WriteStream(newname, f, 0775)
  105. if err != nil {
  106. return err
  107. }
  108. f.Close()
  109. e.c.RemoveAll(oldname)
  110. }
  111. return nil
  112. }
  113. func (e WebDAVFileSystem) Stat(filename string) (os.FileInfo, error) {
  114. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  115. return e.c.Stat(filename)
  116. }
  117. func (e WebDAVFileSystem) VirtualPathToRealPath(subpath string, username string) (string, error) {
  118. subpath = filterFilepath(filepath.ToSlash(filepath.Clean(subpath)))
  119. if strings.HasPrefix(subpath, e.UUID+":") {
  120. //This is full virtual path. Trim the uuid and correct the subpath
  121. subpath = strings.TrimPrefix(subpath, e.UUID+":")
  122. }
  123. if e.Hierarchy == "user" {
  124. return filepath.ToSlash(filepath.Clean(filepath.Join("users", username, subpath))), nil
  125. } else if e.Hierarchy == "public" {
  126. return filepath.ToSlash(filepath.Clean(subpath)), nil
  127. }
  128. return "", errors.New("unsupported filesystem hierarchy")
  129. }
  130. func (e WebDAVFileSystem) RealPathToVirtualPath(rpath string, username string) (string, error) {
  131. rpath = filterFilepath(filepath.ToSlash(filepath.Clean(rpath)))
  132. if e.Hierarchy == "user" && strings.HasPrefix(rpath, "/users/"+username) {
  133. rpath = strings.TrimPrefix(rpath, "/users/"+username)
  134. }
  135. return e.UUID + ":" + filepath.ToSlash(rpath), nil
  136. }
  137. func (e WebDAVFileSystem) FileExists(filename string) bool {
  138. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  139. _, err := e.c.Stat(filename)
  140. if os.IsNotExist(err) || err != nil {
  141. return false
  142. }
  143. return true
  144. }
  145. func (e WebDAVFileSystem) IsDir(filename string) bool {
  146. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  147. s, err := e.c.Stat(filename)
  148. if err != nil {
  149. return false
  150. }
  151. return s.IsDir()
  152. }
  153. //Notes: This is not actual Glob function. This just emulate Glob using ReadDir with max depth 1 layer
  154. func (e WebDAVFileSystem) Glob(wildcard string) ([]string, error) {
  155. wildcard = filepath.ToSlash(filepath.Clean(wildcard))
  156. if !strings.HasPrefix(wildcard, "/") {
  157. //Handle case for listing root, "*"
  158. wildcard = "/" + wildcard
  159. }
  160. //Get the longest path without *
  161. chunksWithoutStar := []string{}
  162. chunks := strings.Split(wildcard, "/")
  163. for _, chunk := range chunks {
  164. if !strings.Contains(chunk, "*") {
  165. chunksWithoutStar = append(chunksWithoutStar, chunk)
  166. } else {
  167. //Cut off
  168. break
  169. }
  170. }
  171. if strings.Count(wildcard, "*") <= 1 && strings.Contains(chunks[len(chunks)-1], "*") {
  172. //Fast Glob
  173. fileInfos, err := e.c.ReadDir(filterFilepath(filepath.ToSlash(filepath.Clean(filepath.Dir(wildcard)))))
  174. if err != nil {
  175. return []string{}, err
  176. }
  177. validFiles := []string{}
  178. matchingRule := wildCardToRegexp(wildcard)
  179. for _, thisFileInfo := range fileInfos {
  180. thisFileFullpath := filepath.ToSlash(filepath.Join(filepath.Dir(wildcard), thisFileInfo.Name()))
  181. match, _ := regexp.MatchString(matchingRule, thisFileFullpath)
  182. if match {
  183. validFiles = append(validFiles, thisFileFullpath)
  184. }
  185. }
  186. return validFiles, nil
  187. } else {
  188. //Slow Glob
  189. walkRoot := strings.Join(chunksWithoutStar, "/")
  190. if !strings.HasPrefix(walkRoot, "/") {
  191. walkRoot = "/" + walkRoot
  192. }
  193. allFiles := []string{}
  194. e.Walk(walkRoot, func(path string, info fs.FileInfo, err error) error {
  195. allFiles = append(allFiles, path)
  196. return nil
  197. })
  198. validFiles := []string{}
  199. matchingRule := wildCardToRegexp(wildcard) + "$"
  200. for _, thisFilepath := range allFiles {
  201. match, _ := regexp.MatchString(matchingRule, thisFilepath)
  202. if match {
  203. validFiles = append(validFiles, thisFilepath)
  204. }
  205. }
  206. return validFiles, nil
  207. }
  208. }
  209. func (e WebDAVFileSystem) GetFileSize(filename string) int64 {
  210. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  211. s, err := e.Stat(filename)
  212. if err != nil {
  213. log.Println(err)
  214. return 0
  215. }
  216. return s.Size()
  217. }
  218. func (e WebDAVFileSystem) GetModTime(filename string) (int64, error) {
  219. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  220. s, err := e.Stat(filename)
  221. if err != nil {
  222. return 0, err
  223. }
  224. return s.ModTime().Unix(), nil
  225. }
  226. func (e WebDAVFileSystem) WriteFile(filename string, content []byte, mode os.FileMode) error {
  227. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  228. return e.c.Write(filename, content, mode)
  229. }
  230. func (e WebDAVFileSystem) ReadFile(filename string) ([]byte, error) {
  231. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  232. bytes, err := e.c.Read(filename)
  233. if err != nil {
  234. return []byte(""), err
  235. }
  236. return bytes, nil
  237. }
  238. func (e WebDAVFileSystem) WriteStream(filename string, stream io.Reader, mode os.FileMode) error {
  239. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  240. return e.c.WriteStream(filename, stream, mode)
  241. }
  242. func (e WebDAVFileSystem) ReadStream(filename string) (io.ReadCloser, error) {
  243. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  244. return e.c.ReadStream(filename)
  245. }
  246. func (e WebDAVFileSystem) Walk(rootpath string, walkFn filepath.WalkFunc) error {
  247. rootpath = filepath.ToSlash(filepath.Clean(rootpath))
  248. rootStat, err := e.Stat(rootpath)
  249. err = walkFn(rootpath, rootStat, err)
  250. if err != nil {
  251. return err
  252. }
  253. return e.walk(rootpath, walkFn)
  254. }
  255. /*
  256. Helper Functions
  257. */
  258. func (e WebDAVFileSystem) walk(thisPath string, walkFun filepath.WalkFunc) error {
  259. files, err := e.c.ReadDir(thisPath)
  260. if err != nil {
  261. return err
  262. }
  263. for _, file := range files {
  264. thisFileFullPath := filepath.ToSlash(filepath.Join(thisPath, file.Name()))
  265. if file.IsDir() {
  266. err = walkFun(thisFileFullPath, file, nil)
  267. if err != nil {
  268. return err
  269. }
  270. err = e.walk(thisFileFullPath, walkFun)
  271. if err != nil {
  272. return err
  273. }
  274. } else {
  275. err = walkFun(thisFileFullPath, file, nil)
  276. if err != nil {
  277. return err
  278. }
  279. }
  280. }
  281. return nil
  282. }
  283. /*
  284. func (e WebDAVFileSystem) globscan(currentPath string, wildcardChunks []string, layer int) ([]string, error) {
  285. if layer >= len(wildcardChunks) {
  286. return []string{}, nil
  287. }
  288. }
  289. */
  290. func filterFilepath(rawpath string) string {
  291. rawpath = strings.TrimSpace(rawpath)
  292. if strings.HasPrefix(rawpath, "./") {
  293. return rawpath[1:]
  294. } else if rawpath == "." || rawpath == "" {
  295. return "/"
  296. }
  297. return rawpath
  298. }
  299. func wildCardToRegexp(pattern string) string {
  300. var result strings.Builder
  301. for i, literal := range strings.Split(pattern, "*") {
  302. // Replace * with .*
  303. if i > 0 {
  304. result.WriteString(".*")
  305. }
  306. // Quote any regular expression meta characters in the
  307. // literal text.
  308. result.WriteString(regexp.QuoteMeta(literal))
  309. }
  310. return result.String()
  311. }