filesys.go 4.7 KB

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