webdavfs.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. package webdavfs
  2. import (
  3. "errors"
  4. "io"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "regexp"
  9. "strings"
  10. "time"
  11. "github.com/studio-b12/gowebdav"
  12. "imuslab.com/arozos/mod/filesystem/arozfs"
  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) (arozfs.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) (arozfs.File, error) {
  69. return nil, errors.New("filesystem type not supported")
  70. }
  71. func (e WebDAVFileSystem) OpenFile(filename string, flag int, perm os.FileMode) (arozfs.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. chunks := strings.Split(strings.TrimPrefix(wildcard, "/"), "/")
  153. results, err := e.globpath("/", chunks, 0)
  154. return results, err
  155. /*
  156. //Get the longest path without *
  157. chunksWithoutStar := []string{}
  158. chunks := strings.Split(wildcard, "/")
  159. for _, chunk := range chunks {
  160. if !strings.Contains(chunk, "*") {
  161. chunksWithoutStar = append(chunksWithoutStar, chunk)
  162. } else {
  163. //Cut off
  164. break
  165. }
  166. }
  167. if strings.Count(wildcard, "*") <= 1 && strings.Contains(chunks[len(chunks)-1], "*") {
  168. //Fast Glob
  169. fileInfos, err := e.c.ReadDir(filterFilepath(filepath.ToSlash(filepath.Clean(filepath.Dir(wildcard)))))
  170. if err != nil {
  171. return []string{}, err
  172. }
  173. validFiles := []string{}
  174. matchingRule := wildCardToRegexp(wildcard)
  175. for _, thisFileInfo := range fileInfos {
  176. thisFileFullpath := filepath.ToSlash(filepath.Join(filepath.Dir(wildcard), thisFileInfo.Name()))
  177. match, _ := regexp.MatchString(matchingRule, thisFileFullpath)
  178. if match {
  179. validFiles = append(validFiles, thisFileFullpath)
  180. }
  181. }
  182. return validFiles, nil
  183. } else {
  184. //Slow Glob
  185. walkRoot := strings.Join(chunksWithoutStar, "/")
  186. if !strings.HasPrefix(walkRoot, "/") {
  187. walkRoot = "/" + walkRoot
  188. }
  189. allFiles := []string{}
  190. e.Walk(walkRoot, func(path string, info fs.FileInfo, err error) error {
  191. allFiles = append(allFiles, path)
  192. return nil
  193. })
  194. validFiles := []string{}
  195. matchingRule := wildCardToRegexp(wildcard) + "$"
  196. for _, thisFilepath := range allFiles {
  197. match, _ := regexp.MatchString(matchingRule, thisFilepath)
  198. if match {
  199. validFiles = append(validFiles, thisFilepath)
  200. }
  201. }
  202. return validFiles, nil
  203. }
  204. */
  205. }
  206. func (e WebDAVFileSystem) GetFileSize(filename string) int64 {
  207. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  208. s, err := e.Stat(filename)
  209. if err != nil {
  210. log.Println(err)
  211. return 0
  212. }
  213. return s.Size()
  214. }
  215. func (e WebDAVFileSystem) GetModTime(filename string) (int64, error) {
  216. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  217. s, err := e.Stat(filename)
  218. if err != nil {
  219. return 0, err
  220. }
  221. return s.ModTime().Unix(), nil
  222. }
  223. func (e WebDAVFileSystem) WriteFile(filename string, content []byte, mode os.FileMode) error {
  224. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  225. return e.c.Write(filename, content, mode)
  226. }
  227. func (e WebDAVFileSystem) ReadFile(filename string) ([]byte, error) {
  228. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  229. bytes, err := e.c.Read(filename)
  230. if err != nil {
  231. return []byte(""), err
  232. }
  233. return bytes, nil
  234. }
  235. func (e WebDAVFileSystem) WriteStream(filename string, stream io.Reader, mode os.FileMode) error {
  236. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  237. return e.c.WriteStream(filename, stream, mode)
  238. }
  239. func (e WebDAVFileSystem) ReadStream(filename string) (io.ReadCloser, error) {
  240. filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
  241. return e.c.ReadStream(filename)
  242. }
  243. func (e WebDAVFileSystem) Walk(rootpath string, walkFn filepath.WalkFunc) error {
  244. rootpath = filepath.ToSlash(filepath.Clean(rootpath))
  245. rootStat, err := e.Stat(rootpath)
  246. err = walkFn(rootpath, rootStat, err)
  247. if err != nil {
  248. return err
  249. }
  250. return e.walk(rootpath, walkFn)
  251. }
  252. func (e WebDAVFileSystem) Close() error {
  253. return nil
  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. func (e WebDAVFileSystem) globpath(currentPath string, pathSegments []string, depth int) ([]string, error) {
  284. const pathSeparatorsLimit = 1000
  285. if depth == pathSeparatorsLimit {
  286. return nil, errors.New("bad pattern")
  287. }
  288. // Check pattern is well-formed.
  289. if _, err := regexp.MatchString(wildCardToRegexp(strings.Join(pathSegments, "/")), ""); err != nil {
  290. return nil, err
  291. }
  292. if len(pathSegments) == 0 {
  293. //Reaching the bottom
  294. return []string{}, nil
  295. }
  296. thisSegment := pathSegments[0]
  297. if strings.Contains(thisSegment, "*") {
  298. //Search for matching
  299. matchPattern := currentPath + thisSegment
  300. files, err := e.c.ReadDir(currentPath)
  301. if err != nil {
  302. return []string{}, nil
  303. }
  304. //Check which file in the currentPath matches the wildcard
  305. matchedSubpaths := []string{}
  306. for _, file := range files {
  307. thisPath := currentPath + file.Name()
  308. match, _ := regexp.MatchString(wildCardToRegexp(matchPattern), thisPath)
  309. if match {
  310. if file.IsDir() {
  311. matchedSubpaths = append(matchedSubpaths, thisPath+"/")
  312. } else {
  313. matchedSubpaths = append(matchedSubpaths, thisPath)
  314. }
  315. }
  316. }
  317. if len(pathSegments[1:]) == 0 {
  318. return matchedSubpaths, nil
  319. }
  320. //For each of the subpaths, do a globpath
  321. matchingFilenames := []string{}
  322. for _, subpath := range matchedSubpaths {
  323. thisMatchedNames, _ := e.globpath(subpath, pathSegments[1:], depth+1)
  324. matchingFilenames = append(matchingFilenames, thisMatchedNames...)
  325. }
  326. return matchingFilenames, nil
  327. } else {
  328. //Check folder exists
  329. if e.FileExists(currentPath+thisSegment) && e.IsDir(currentPath+thisSegment) {
  330. return e.globpath(currentPath+thisSegment+"/", pathSegments[1:], depth+1)
  331. } else {
  332. //Not matching
  333. return []string{}, nil
  334. }
  335. }
  336. }
  337. func filterFilepath(rawpath string) string {
  338. rawpath = strings.TrimSpace(rawpath)
  339. if strings.HasPrefix(rawpath, "./") {
  340. return rawpath[1:]
  341. } else if rawpath == "." || rawpath == "" {
  342. return "/"
  343. }
  344. return rawpath
  345. }
  346. func wildCardToRegexp(pattern string) string {
  347. var result strings.Builder
  348. for i, literal := range strings.Split(pattern, "*") {
  349. // Replace * with .*
  350. if i > 0 {
  351. result.WriteString(".*")
  352. }
  353. // Quote any regular expression meta characters in the
  354. // literal text.
  355. result.WriteString(regexp.QuoteMeta(literal))
  356. }
  357. return result.String()
  358. }