manifest.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package api
  17. import (
  18. "bytes"
  19. "encoding/json"
  20. "errors"
  21. "fmt"
  22. "io"
  23. "net/http"
  24. "strings"
  25. "sync"
  26. "time"
  27. "github.com/ethereum/go-ethereum/common"
  28. "github.com/ethereum/go-ethereum/log"
  29. "github.com/ethereum/go-ethereum/swarm/storage"
  30. )
  31. const (
  32. ManifestType = "application/bzz-manifest+json"
  33. )
  34. // Manifest represents a swarm manifest
  35. type Manifest struct {
  36. Entries []ManifestEntry `json:"entries,omitempty"`
  37. }
  38. // ManifestEntry represents an entry in a swarm manifest
  39. type ManifestEntry struct {
  40. Hash string `json:"hash,omitempty"`
  41. Path string `json:"path,omitempty"`
  42. ContentType string `json:"contentType,omitempty"`
  43. Mode int64 `json:"mode,omitempty"`
  44. Size int64 `json:"size,omitempty"`
  45. ModTime time.Time `json:"mod_time,omitempty"`
  46. Status int `json:"status,omitempty"`
  47. }
  48. // ManifestList represents the result of listing files in a manifest
  49. type ManifestList struct {
  50. CommonPrefixes []string `json:"common_prefixes,omitempty"`
  51. Entries []*ManifestEntry `json:"entries,omitempty"`
  52. }
  53. // NewManifest creates and stores a new, empty manifest
  54. func (a *Api) NewManifest() (storage.Key, error) {
  55. var manifest Manifest
  56. data, err := json.Marshal(&manifest)
  57. if err != nil {
  58. return nil, err
  59. }
  60. return a.Store(bytes.NewReader(data), int64(len(data)), &sync.WaitGroup{})
  61. }
  62. // ManifestWriter is used to add and remove entries from an underlying manifest
  63. type ManifestWriter struct {
  64. api *Api
  65. trie *manifestTrie
  66. quitC chan bool
  67. }
  68. func (a *Api) NewManifestWriter(key storage.Key, quitC chan bool) (*ManifestWriter, error) {
  69. trie, err := loadManifest(a.dpa, key, quitC)
  70. if err != nil {
  71. return nil, fmt.Errorf("error loading manifest %s: %s", key, err)
  72. }
  73. return &ManifestWriter{a, trie, quitC}, nil
  74. }
  75. // AddEntry stores the given data and adds the resulting key to the manifest
  76. func (m *ManifestWriter) AddEntry(data io.Reader, e *ManifestEntry) (storage.Key, error) {
  77. key, err := m.api.Store(data, e.Size, nil)
  78. if err != nil {
  79. return nil, err
  80. }
  81. entry := newManifestTrieEntry(e, nil)
  82. entry.Hash = key.String()
  83. m.trie.addEntry(entry, m.quitC)
  84. return key, nil
  85. }
  86. // RemoveEntry removes the given path from the manifest
  87. func (m *ManifestWriter) RemoveEntry(path string) error {
  88. m.trie.deleteEntry(path, m.quitC)
  89. return nil
  90. }
  91. // Store stores the manifest, returning the resulting storage key
  92. func (m *ManifestWriter) Store() (storage.Key, error) {
  93. return m.trie.hash, m.trie.recalcAndStore()
  94. }
  95. // ManifestWalker is used to recursively walk the entries in the manifest and
  96. // all of its submanifests
  97. type ManifestWalker struct {
  98. api *Api
  99. trie *manifestTrie
  100. quitC chan bool
  101. }
  102. func (a *Api) NewManifestWalker(key storage.Key, quitC chan bool) (*ManifestWalker, error) {
  103. trie, err := loadManifest(a.dpa, key, quitC)
  104. if err != nil {
  105. return nil, fmt.Errorf("error loading manifest %s: %s", key, err)
  106. }
  107. return &ManifestWalker{a, trie, quitC}, nil
  108. }
  109. // SkipManifest is used as a return value from WalkFn to indicate that the
  110. // manifest should be skipped
  111. var SkipManifest = errors.New("skip this manifest")
  112. // WalkFn is the type of function called for each entry visited by a recursive
  113. // manifest walk
  114. type WalkFn func(entry *ManifestEntry) error
  115. // Walk recursively walks the manifest calling walkFn for each entry in the
  116. // manifest, including submanifests
  117. func (m *ManifestWalker) Walk(walkFn WalkFn) error {
  118. return m.walk(m.trie, "", walkFn)
  119. }
  120. func (m *ManifestWalker) walk(trie *manifestTrie, prefix string, walkFn WalkFn) error {
  121. for _, entry := range trie.entries {
  122. if entry == nil {
  123. continue
  124. }
  125. entry.Path = prefix + entry.Path
  126. err := walkFn(&entry.ManifestEntry)
  127. if err != nil {
  128. if entry.ContentType == ManifestType && err == SkipManifest {
  129. continue
  130. }
  131. return err
  132. }
  133. if entry.ContentType != ManifestType {
  134. continue
  135. }
  136. if err := trie.loadSubTrie(entry, nil); err != nil {
  137. return err
  138. }
  139. if err := m.walk(entry.subtrie, entry.Path, walkFn); err != nil {
  140. return err
  141. }
  142. }
  143. return nil
  144. }
  145. type manifestTrie struct {
  146. dpa *storage.DPA
  147. entries [257]*manifestTrieEntry // indexed by first character of basePath, entries[256] is the empty basePath entry
  148. hash storage.Key // if hash != nil, it is stored
  149. }
  150. func newManifestTrieEntry(entry *ManifestEntry, subtrie *manifestTrie) *manifestTrieEntry {
  151. return &manifestTrieEntry{
  152. ManifestEntry: *entry,
  153. subtrie: subtrie,
  154. }
  155. }
  156. type manifestTrieEntry struct {
  157. ManifestEntry
  158. subtrie *manifestTrie
  159. }
  160. func loadManifest(dpa *storage.DPA, hash storage.Key, quitC chan bool) (trie *manifestTrie, err error) { // non-recursive, subtrees are downloaded on-demand
  161. log.Trace(fmt.Sprintf("manifest lookup key: '%v'.", hash.Log()))
  162. // retrieve manifest via DPA
  163. manifestReader := dpa.Retrieve(hash)
  164. return readManifest(manifestReader, hash, dpa, quitC)
  165. }
  166. func readManifest(manifestReader storage.LazySectionReader, hash storage.Key, dpa *storage.DPA, quitC chan bool) (trie *manifestTrie, err error) { // non-recursive, subtrees are downloaded on-demand
  167. // TODO check size for oversized manifests
  168. size, err := manifestReader.Size(quitC)
  169. if err != nil { // size == 0
  170. // can't determine size means we don't have the root chunk
  171. err = fmt.Errorf("Manifest not Found")
  172. return
  173. }
  174. manifestData := make([]byte, size)
  175. read, err := manifestReader.Read(manifestData)
  176. if int64(read) < size {
  177. log.Trace(fmt.Sprintf("Manifest %v not found.", hash.Log()))
  178. if err == nil {
  179. err = fmt.Errorf("Manifest retrieval cut short: read %v, expect %v", read, size)
  180. }
  181. return
  182. }
  183. log.Trace(fmt.Sprintf("Manifest %v retrieved", hash.Log()))
  184. var man struct {
  185. Entries []*manifestTrieEntry `json:"entries"`
  186. }
  187. err = json.Unmarshal(manifestData, &man)
  188. if err != nil {
  189. err = fmt.Errorf("Manifest %v is malformed: %v", hash.Log(), err)
  190. log.Trace(fmt.Sprintf("%v", err))
  191. return
  192. }
  193. log.Trace(fmt.Sprintf("Manifest %v has %d entries.", hash.Log(), len(man.Entries)))
  194. trie = &manifestTrie{
  195. dpa: dpa,
  196. }
  197. for _, entry := range man.Entries {
  198. trie.addEntry(entry, quitC)
  199. }
  200. return
  201. }
  202. func (self *manifestTrie) addEntry(entry *manifestTrieEntry, quitC chan bool) {
  203. self.hash = nil // trie modified, hash needs to be re-calculated on demand
  204. if len(entry.Path) == 0 {
  205. self.entries[256] = entry
  206. return
  207. }
  208. b := entry.Path[0]
  209. oldentry := self.entries[b]
  210. if (oldentry == nil) || (oldentry.Path == entry.Path && oldentry.ContentType != ManifestType) {
  211. self.entries[b] = entry
  212. return
  213. }
  214. cpl := 0
  215. for (len(entry.Path) > cpl) && (len(oldentry.Path) > cpl) && (entry.Path[cpl] == oldentry.Path[cpl]) {
  216. cpl++
  217. }
  218. if (oldentry.ContentType == ManifestType) && (cpl == len(oldentry.Path)) {
  219. if self.loadSubTrie(oldentry, quitC) != nil {
  220. return
  221. }
  222. entry.Path = entry.Path[cpl:]
  223. oldentry.subtrie.addEntry(entry, quitC)
  224. oldentry.Hash = ""
  225. return
  226. }
  227. commonPrefix := entry.Path[:cpl]
  228. subtrie := &manifestTrie{
  229. dpa: self.dpa,
  230. }
  231. entry.Path = entry.Path[cpl:]
  232. oldentry.Path = oldentry.Path[cpl:]
  233. subtrie.addEntry(entry, quitC)
  234. subtrie.addEntry(oldentry, quitC)
  235. self.entries[b] = newManifestTrieEntry(&ManifestEntry{
  236. Path: commonPrefix,
  237. ContentType: ManifestType,
  238. }, subtrie)
  239. }
  240. func (self *manifestTrie) getCountLast() (cnt int, entry *manifestTrieEntry) {
  241. for _, e := range self.entries {
  242. if e != nil {
  243. cnt++
  244. entry = e
  245. }
  246. }
  247. return
  248. }
  249. func (self *manifestTrie) deleteEntry(path string, quitC chan bool) {
  250. self.hash = nil // trie modified, hash needs to be re-calculated on demand
  251. if len(path) == 0 {
  252. self.entries[256] = nil
  253. return
  254. }
  255. b := path[0]
  256. entry := self.entries[b]
  257. if entry == nil {
  258. return
  259. }
  260. if entry.Path == path {
  261. self.entries[b] = nil
  262. return
  263. }
  264. epl := len(entry.Path)
  265. if (entry.ContentType == ManifestType) && (len(path) >= epl) && (path[:epl] == entry.Path) {
  266. if self.loadSubTrie(entry, quitC) != nil {
  267. return
  268. }
  269. entry.subtrie.deleteEntry(path[epl:], quitC)
  270. entry.Hash = ""
  271. // remove subtree if it has less than 2 elements
  272. cnt, lastentry := entry.subtrie.getCountLast()
  273. if cnt < 2 {
  274. if lastentry != nil {
  275. lastentry.Path = entry.Path + lastentry.Path
  276. }
  277. self.entries[b] = lastentry
  278. }
  279. }
  280. }
  281. func (self *manifestTrie) recalcAndStore() error {
  282. if self.hash != nil {
  283. return nil
  284. }
  285. var buffer bytes.Buffer
  286. buffer.WriteString(`{"entries":[`)
  287. list := &Manifest{}
  288. for _, entry := range self.entries {
  289. if entry != nil {
  290. if entry.Hash == "" { // TODO: paralellize
  291. err := entry.subtrie.recalcAndStore()
  292. if err != nil {
  293. return err
  294. }
  295. entry.Hash = entry.subtrie.hash.String()
  296. }
  297. list.Entries = append(list.Entries, entry.ManifestEntry)
  298. }
  299. }
  300. manifest, err := json.Marshal(list)
  301. if err != nil {
  302. return err
  303. }
  304. sr := bytes.NewReader(manifest)
  305. wg := &sync.WaitGroup{}
  306. key, err2 := self.dpa.Store(sr, int64(len(manifest)), wg, nil)
  307. wg.Wait()
  308. self.hash = key
  309. return err2
  310. }
  311. func (self *manifestTrie) loadSubTrie(entry *manifestTrieEntry, quitC chan bool) (err error) {
  312. if entry.subtrie == nil {
  313. hash := common.Hex2Bytes(entry.Hash)
  314. entry.subtrie, err = loadManifest(self.dpa, hash, quitC)
  315. entry.Hash = "" // might not match, should be recalculated
  316. }
  317. return
  318. }
  319. func (self *manifestTrie) listWithPrefixInt(prefix, rp string, quitC chan bool, cb func(entry *manifestTrieEntry, suffix string)) error {
  320. plen := len(prefix)
  321. var start, stop int
  322. if plen == 0 {
  323. start = 0
  324. stop = 256
  325. } else {
  326. start = int(prefix[0])
  327. stop = start
  328. }
  329. for i := start; i <= stop; i++ {
  330. select {
  331. case <-quitC:
  332. return fmt.Errorf("aborted")
  333. default:
  334. }
  335. entry := self.entries[i]
  336. if entry != nil {
  337. epl := len(entry.Path)
  338. if entry.ContentType == ManifestType {
  339. l := plen
  340. if epl < l {
  341. l = epl
  342. }
  343. if prefix[:l] == entry.Path[:l] {
  344. err := self.loadSubTrie(entry, quitC)
  345. if err != nil {
  346. return err
  347. }
  348. err = entry.subtrie.listWithPrefixInt(prefix[l:], rp+entry.Path[l:], quitC, cb)
  349. if err != nil {
  350. return err
  351. }
  352. }
  353. } else {
  354. if (epl >= plen) && (prefix == entry.Path[:plen]) {
  355. cb(entry, rp+entry.Path[plen:])
  356. }
  357. }
  358. }
  359. }
  360. return nil
  361. }
  362. func (self *manifestTrie) listWithPrefix(prefix string, quitC chan bool, cb func(entry *manifestTrieEntry, suffix string)) (err error) {
  363. return self.listWithPrefixInt(prefix, "", quitC, cb)
  364. }
  365. func (self *manifestTrie) findPrefixOf(path string, quitC chan bool) (entry *manifestTrieEntry, pos int) {
  366. log.Trace(fmt.Sprintf("findPrefixOf(%s)", path))
  367. if len(path) == 0 {
  368. return self.entries[256], 0
  369. }
  370. //see if first char is in manifest entries
  371. b := path[0]
  372. entry = self.entries[b]
  373. if entry == nil {
  374. return self.entries[256], 0
  375. }
  376. epl := len(entry.Path)
  377. log.Trace(fmt.Sprintf("path = %v entry.Path = %v epl = %v", path, entry.Path, epl))
  378. if len(path) <= epl {
  379. if entry.Path[:len(path)] == path {
  380. if entry.ContentType == ManifestType {
  381. err := self.loadSubTrie(entry, quitC)
  382. if err == nil && entry.subtrie != nil {
  383. subentries := entry.subtrie.entries
  384. for i := 0; i < len(subentries); i++ {
  385. sub := subentries[i]
  386. if sub != nil && sub.Path == "" {
  387. return sub, len(path)
  388. }
  389. }
  390. }
  391. entry.Status = http.StatusMultipleChoices
  392. }
  393. pos = len(path)
  394. return
  395. }
  396. return nil, 0
  397. }
  398. if path[:epl] == entry.Path {
  399. log.Trace(fmt.Sprintf("entry.ContentType = %v", entry.ContentType))
  400. //the subentry is a manifest, load subtrie
  401. if entry.ContentType == ManifestType && (strings.Contains(entry.Path, path) || strings.Contains(path, entry.Path)) {
  402. err := self.loadSubTrie(entry, quitC)
  403. if err != nil {
  404. return nil, 0
  405. }
  406. sub, pos := entry.subtrie.findPrefixOf(path[epl:], quitC)
  407. if sub != nil {
  408. entry = sub
  409. pos += epl
  410. return sub, pos
  411. } else if path == entry.Path {
  412. entry.Status = http.StatusMultipleChoices
  413. }
  414. } else {
  415. //entry is not a manifest, return it
  416. if path != entry.Path {
  417. return nil, 0
  418. }
  419. pos = epl
  420. }
  421. }
  422. return
  423. }
  424. // file system manifest always contains regularized paths
  425. // no leading or trailing slashes, only single slashes inside
  426. func RegularSlashes(path string) (res string) {
  427. for i := 0; i < len(path); i++ {
  428. if (path[i] != '/') || ((i > 0) && (path[i-1] != '/')) {
  429. res = res + path[i:i+1]
  430. }
  431. }
  432. if (len(res) > 0) && (res[len(res)-1] == '/') {
  433. res = res[:len(res)-1]
  434. }
  435. return
  436. }
  437. func (self *manifestTrie) getEntry(spath string) (entry *manifestTrieEntry, fullpath string) {
  438. path := RegularSlashes(spath)
  439. var pos int
  440. quitC := make(chan bool)
  441. entry, pos = self.findPrefixOf(path, quitC)
  442. return entry, path[:pos]
  443. }