filesys.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package wdfs
  2. import (
  3. "fmt"
  4. "github.com/kpmy/mipfs/ipfs_api"
  5. "github.com/kpmy/ypk/dom"
  6. "github.com/kpmy/ypk/fn"
  7. . "github.com/kpmy/ypk/tc"
  8. "golang.org/x/net/webdav"
  9. "os"
  10. "strings"
  11. "time"
  12. )
  13. const EmptyDirHash = "QmdniF66q5wYDEyp2PYp6wXwgTUg3ssmb8NYSyfytwyf2j"
  14. const EmptyFileHash = "QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH"
  15. type filesystem struct {
  16. webdav.FileSystem
  17. rootLevel *chain
  18. get func() string
  19. set func(string)
  20. }
  21. func (f *filesystem) root() *chain {
  22. f.rootLevel.Hash = f.get()
  23. f.rootLevel.name = f.rootLevel.Hash
  24. f.rootLevel.upd = f.set
  25. return f.rootLevel
  26. }
  27. func (f *filesystem) Mkdir(name string, perm os.FileMode) (err error) {
  28. root := f.root()
  29. chain := newChain(root.mirror(), root.Hash+"/"+strings.Trim(name, "/"))
  30. if tail := chain.tail(); !tail.exists() {
  31. onlyOne := true
  32. for tail != nil {
  33. if !tail.exists() {
  34. if onlyOne {
  35. tail.Hash, _ = ipfs_api.Shell().NewObject("unixfs-dir")
  36. prop := newPropsModel()
  37. prop.Attr("modified", fmt.Sprint(time.Now().Unix()))
  38. propHash, _ := ipfs_api.Shell().Add(dom.EncodeWithHeader(prop))
  39. if tail.Hash, err = ipfs_api.Shell().PatchLink(tail.Hash, "*", propHash, false); err != nil {
  40. Halt(100, err)
  41. return
  42. }
  43. onlyOne = false
  44. } else {
  45. err = os.ErrNotExist
  46. return
  47. }
  48. }
  49. if tail.down != nil {
  50. tail.Hash, _ = ipfs_api.Shell().PatchLink(tail.Hash, tail.down.name, tail.down.Hash, false)
  51. }
  52. tail = tail.up
  53. }
  54. chain.link.update(chain.Hash)
  55. } else {
  56. err = os.ErrExist
  57. }
  58. return
  59. }
  60. func (f *filesystem) OpenFile(name string, flag int, perm os.FileMode) (ret webdav.File, err error) {
  61. //log.Println("open", name, flag, perm)
  62. root := f.root()
  63. path := newChain(root.mirror(), root.Hash+"/"+strings.Trim(name, "/"))
  64. switch tail := path.tail(); {
  65. case tail.exists() && tail.IsDir():
  66. _l := &loc{ch: tail}
  67. _l.readPropsModel()
  68. ret = _l
  69. case tail.exists() && !tail.IsDir():
  70. _f := &file{ch: tail}
  71. _f.readPropsModel()
  72. ret = _f
  73. case !tail.exists() && flag&os.O_CREATE != 0:
  74. var edir *chain
  75. for edir = tail.up; edir != nil && edir.exists() && edir.IsDir(); edir = edir.up {
  76. }
  77. if fn.IsNil(edir) {
  78. ret = &file{ch: tail}
  79. } else {
  80. err = os.ErrNotExist
  81. }
  82. default:
  83. //log.Println("open error", name, flag, perm)
  84. err = os.ErrNotExist
  85. }
  86. return
  87. }
  88. func (f *filesystem) RemoveAll(name string) (err error) {
  89. root := f.root()
  90. chain := newChain(root.mirror(), root.Hash+"/"+strings.Trim(name, "/"))
  91. if tail := chain.tail(); tail.exists() {
  92. tail = tail.up
  93. tail.Hash, _ = ipfs_api.Shell().Patch(tail.Hash, "rm-link", tail.down.name)
  94. if !tail.down.IsDir() {
  95. //удалим пропы
  96. if th, err := ipfs_api.Shell().Patch(tail.Hash, "rm-link", "*"+tail.down.name); err == nil {
  97. tail.Hash = th
  98. }
  99. }
  100. tail = tail.up
  101. for tail != nil {
  102. tail.Hash, _ = ipfs_api.Shell().PatchLink(tail.Hash, tail.down.name, tail.down.Hash, false)
  103. tail = tail.up
  104. }
  105. chain.link.update(chain.Hash)
  106. }
  107. return
  108. }
  109. func (f *filesystem) Rename(oldName, newName string) (err error) {
  110. //log.Println("rename", oldName, newName)
  111. root := f.root()
  112. on := newChain(root.mirror(), root.Hash+"/"+strings.Trim(oldName, "/"))
  113. var op *chain
  114. if !on.tail().IsDir() {
  115. propPath := ""
  116. for x := on; x != nil; x = x.down {
  117. if x.down == nil {
  118. propPath = propPath + "/" + "*" + x.name
  119. } else {
  120. propPath = propPath + "/" + x.name
  121. }
  122. }
  123. op = newChain(root.mirror(), propPath)
  124. }
  125. nn := newChain(root.mirror(), root.Hash+"/"+strings.Trim(newName, "/"))
  126. if ot := on.tail(); ot.exists() {
  127. if nt := nn.tail(); !nt.exists() {
  128. Assert(ot.depth() == nt.depth(), 40)
  129. tail := ot.up
  130. tail.Hash, _ = ipfs_api.Shell().Patch(tail.Hash, "rm-link", ot.name)
  131. if op != nil {
  132. tail.Hash, _ = ipfs_api.Shell().Patch(tail.Hash, "rm-link", op.tail().name)
  133. }
  134. tail.Hash, _ = ipfs_api.Shell().PatchLink(tail.Hash, nt.name, ot.Hash, false)
  135. if op != nil {
  136. tail.Hash, _ = ipfs_api.Shell().PatchLink(tail.Hash, "*"+nt.name, op.tail().Hash, false)
  137. }
  138. tail = tail.up
  139. for tail != nil {
  140. tail.Hash, _ = ipfs_api.Shell().PatchLink(tail.Hash, tail.down.name, tail.down.Hash, false)
  141. tail = tail.up
  142. }
  143. on.link.update(on.Hash)
  144. } else {
  145. err = os.ErrExist
  146. }
  147. } else {
  148. err = os.ErrNotExist
  149. }
  150. return
  151. }
  152. func (f *filesystem) Stat(name string) (fi os.FileInfo, err error) {
  153. //log.Println("stat", name)
  154. root := f.root()
  155. chain := newChain(root.mirror(), root.Hash+"/"+strings.Trim(name, "/"))
  156. tail := chain.tail()
  157. if !tail.exists() {
  158. err = os.ErrNotExist
  159. } else if tail.IsDir() {
  160. _l := &loc{ch: tail}
  161. _l.readPropsModel()
  162. fi = _l
  163. } else {
  164. _f := &file{ch: tail}
  165. _f.readPropsModel()
  166. fi = _f
  167. }
  168. return
  169. }
  170. func (f *filesystem) String() string {
  171. return f.rootLevel.Hash
  172. }
  173. func (f *filesystem) ETag(name string) (ret string, err error) {
  174. var fi os.FileInfo
  175. if fi, err = f.Stat(name); err == nil {
  176. ret = fmt.Sprintf(`"%x%x"`, fi.ModTime().UnixNano(), fi.Size())
  177. }
  178. return
  179. }
  180. func NewFS(get func() string, set func(string)) *filesystem {
  181. ch := &chain{}
  182. ch.Hash = get()
  183. ch.name = ch.Hash
  184. ch.Type = "Directory"
  185. return &filesystem{get: get, set: set, rootLevel: ch}
  186. }