files.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package wdfs
  2. import (
  3. "encoding/xml"
  4. "github.com/ipfs/go-ipfs-api"
  5. "github.com/kpmy/mipfs/ipfs_api"
  6. "github.com/kpmy/ypk/fn"
  7. "github.com/mattetti/filebuffer"
  8. "io/ioutil"
  9. "os"
  10. "sync"
  11. "time"
  12. "fmt"
  13. "github.com/kpmy/ypk/dom"
  14. . "github.com/kpmy/ypk/tc"
  15. "golang.org/x/net/webdav"
  16. "io"
  17. "log"
  18. "strconv"
  19. "strings"
  20. )
  21. type block struct {
  22. pos int64
  23. data *filebuffer.Buffer
  24. }
  25. type file struct {
  26. ch *chain
  27. pos int64
  28. links []*shell.LsLink
  29. buf *filebuffer.Buffer
  30. wr chan *block
  31. wg *sync.WaitGroup
  32. props dom.Element
  33. }
  34. func (f *file) Name() string {
  35. return f.ch.name
  36. }
  37. func (f *file) Size() int64 {
  38. return int64(f.ch.UnixLsObject.Size)
  39. }
  40. func (f *file) Mode() os.FileMode {
  41. return 0
  42. }
  43. func (f *file) ModTime() (ret time.Time) {
  44. ret = time.Now()
  45. if !fn.IsNil(f.props) {
  46. if ts := f.props.Attr("modified"); ts != "" {
  47. if sec, err := strconv.ParseInt(ts, 10, 64); err == nil {
  48. ret = time.Unix(sec, 0)
  49. }
  50. }
  51. }
  52. return
  53. }
  54. func (f *file) IsDir() bool {
  55. return false
  56. }
  57. func (f *file) Sys() interface{} {
  58. Halt(100)
  59. return nil
  60. }
  61. func (f *file) Close() error {
  62. if f.wr != nil {
  63. close(f.wr)
  64. f.wg.Wait()
  65. } else if !f.ch.exists() {
  66. f.update(nil)
  67. }
  68. return nil
  69. }
  70. func (f *file) Read(p []byte) (n int, err error) {
  71. if f.links == nil {
  72. f.links, _ = ipfs_api.Shell().List(f.ch.Hash)
  73. }
  74. if len(f.links) == 0 {
  75. if fn.IsNil(f.buf) {
  76. f.buf = filebuffer.New(nil)
  77. rd, _ := ipfs_api.Shell().Cat(f.ch.Hash)
  78. io.Copy(f.buf, rd)
  79. }
  80. f.buf.Seek(f.pos, io.SeekStart)
  81. n, err = f.buf.Read(p)
  82. f.pos = f.pos + int64(n)
  83. return n, err
  84. } else {
  85. var end int64 = 0
  86. for _, l := range f.links {
  87. begin := end
  88. end = begin + int64(l.Size)
  89. if begin <= f.pos && f.pos < end {
  90. if f.buf == nil {
  91. rd, _ := ipfs_api.Shell().Cat(l.Hash)
  92. f.buf = filebuffer.New(nil)
  93. io.Copy(f.buf, rd)
  94. l.Size = uint64(f.buf.Buff.Len())
  95. }
  96. f.buf.Seek(f.pos-begin, io.SeekStart)
  97. n, err = f.buf.Read(p)
  98. f.pos = f.pos + int64(n)
  99. if f.buf.Index == int64(l.Size) {
  100. f.buf = nil
  101. }
  102. return
  103. }
  104. }
  105. panic(100)
  106. }
  107. }
  108. func (f *file) Seek(offset int64, whence int) (seek int64, err error) {
  109. switch whence {
  110. case io.SeekStart:
  111. f.pos = offset
  112. case io.SeekCurrent:
  113. f.pos = f.pos + offset
  114. case io.SeekEnd:
  115. f.pos = f.Size() + offset
  116. default:
  117. Halt(100)
  118. }
  119. Assert(f.pos >= 0, 60)
  120. seek = f.pos
  121. return
  122. }
  123. func (f *file) Readdir(count int) (ret []os.FileInfo, err error) {
  124. return nil, webdav.ErrForbidden
  125. }
  126. func (f *file) Stat() (os.FileInfo, error) {
  127. return f, nil
  128. }
  129. const emptyFileHash = "QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH"
  130. func (f *file) update(data io.ReadCloser) {
  131. if !fn.IsNil(data) {
  132. f.ch.Hash, _ = ipfs_api.Shell().Add(data)
  133. } else {
  134. f.ch.Hash = emptyFileHash
  135. }
  136. for tail := f.ch.up; tail != nil; tail = tail.up {
  137. tail.Hash, _ = ipfs_api.Shell().PatchLink(tail.Hash, tail.down.name, tail.down.Hash, false)
  138. if tail.down.Hash == f.ch.Hash {
  139. //создадим пропы
  140. f.props = newPropsModel()
  141. f.props.Attr("modified", fmt.Sprint(time.Now().Unix()))
  142. propHash, _ := ipfs_api.Shell().Add(dom.EncodeWithHeader(f.props))
  143. tail.Hash, _ = ipfs_api.Shell().PatchLink(tail.Hash, "*"+f.ch.name, propHash, false)
  144. }
  145. }
  146. head := f.ch.head()
  147. head.link.update(head.Hash)
  148. }
  149. func (f *file) Write(p []byte) (n int, err error) {
  150. if f.wr == nil {
  151. f.wr = make(chan *block, 16)
  152. f.wg = new(sync.WaitGroup)
  153. f.wg.Add(1)
  154. go func(f *file) {
  155. tmp, _ := ioutil.TempFile(os.TempDir(), "mipfs")
  156. for w := range f.wr {
  157. tmp.Seek(w.pos, io.SeekStart)
  158. w.data.Seek(0, io.SeekStart)
  159. io.Copy(tmp, w.data)
  160. }
  161. tmp.Seek(0, io.SeekStart)
  162. f.update(tmp)
  163. f.wg.Done()
  164. }(f)
  165. }
  166. b := &block{pos: f.pos}
  167. b.data = filebuffer.New(nil)
  168. n, err = b.data.Write(p)
  169. f.wr <- b
  170. f.pos = f.pos + int64(n)
  171. return n, nil
  172. }
  173. func (f *file) readPropsModel() {
  174. if !strings.HasPrefix(f.ch.name, "*") {
  175. ls, _ := ipfs_api.Shell().FileList(f.ch.up.Hash)
  176. pm := propLinksMap(ls)
  177. if p, ok := pm[f.ch.name]; ok {
  178. rd, _ := ipfs_api.Shell().Cat(p.Hash)
  179. if el, err := dom.Decode(rd); err == nil {
  180. f.props = el.Model()
  181. } else {
  182. Halt(99, f.ch.name, f.ch.Hash)
  183. }
  184. } else {
  185. f.props = newPropsModel()
  186. }
  187. }
  188. }
  189. func (f *file) readPropsObject() (props map[xml.Name]dom.Element, err error) {
  190. props = make(map[xml.Name]dom.Element)
  191. f.readPropsModel()
  192. props = readProps(f.props)
  193. return
  194. }
  195. func (f *file) writePropsObject(props map[xml.Name]dom.Element) {
  196. if !strings.HasPrefix(f.ch.name, "*") {
  197. el := writeProps(props)
  198. propHash, _ := ipfs_api.Shell().Add(dom.EncodeWithHeader(el))
  199. for tail := f.ch.up; tail != nil; tail = tail.up {
  200. if tail.down.Hash == f.ch.Hash {
  201. tail.Hash, _ = ipfs_api.Shell().PatchLink(tail.Hash, "*"+f.ch.name, propHash, false)
  202. } else {
  203. tail.Hash, _ = ipfs_api.Shell().PatchLink(tail.Hash, tail.down.name, tail.down.Hash, false)
  204. }
  205. }
  206. head := f.ch.head()
  207. head.link.update(head.Hash)
  208. }
  209. }
  210. func (f *file) DeadProps() (ret map[xml.Name]webdav.Property, err error) {
  211. log.Println("file prop get")
  212. pm, _ := f.readPropsObject()
  213. ret = props2webdav(pm)
  214. log.Println("read file props", ret)
  215. return
  216. }
  217. func (f *file) Patch(patch []webdav.Proppatch) (ret []webdav.Propstat, err error) {
  218. log.Println("file prop patch", patch)
  219. pe, _ := f.readPropsObject()
  220. ret = propsPatch(pe, patch)
  221. log.Println("write file props", pe)
  222. f.writePropsObject(pe)
  223. return
  224. }