api.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. "context"
  19. "fmt"
  20. "io"
  21. "math/big"
  22. "net/http"
  23. "path"
  24. "strings"
  25. "bytes"
  26. "mime"
  27. "path/filepath"
  28. "time"
  29. "github.com/ethereum/go-ethereum/common"
  30. "github.com/ethereum/go-ethereum/contracts/ens"
  31. "github.com/ethereum/go-ethereum/core/types"
  32. "github.com/ethereum/go-ethereum/metrics"
  33. "github.com/ethereum/go-ethereum/swarm/log"
  34. "github.com/ethereum/go-ethereum/swarm/multihash"
  35. "github.com/ethereum/go-ethereum/swarm/storage"
  36. "github.com/ethereum/go-ethereum/swarm/storage/mru"
  37. )
  38. var (
  39. apiResolveCount = metrics.NewRegisteredCounter("api.resolve.count", nil)
  40. apiResolveFail = metrics.NewRegisteredCounter("api.resolve.fail", nil)
  41. apiPutCount = metrics.NewRegisteredCounter("api.put.count", nil)
  42. apiPutFail = metrics.NewRegisteredCounter("api.put.fail", nil)
  43. apiGetCount = metrics.NewRegisteredCounter("api.get.count", nil)
  44. apiGetNotFound = metrics.NewRegisteredCounter("api.get.notfound", nil)
  45. apiGetHTTP300 = metrics.NewRegisteredCounter("api.get.http.300", nil)
  46. apiModifyCount = metrics.NewRegisteredCounter("api.modify.count", nil)
  47. apiModifyFail = metrics.NewRegisteredCounter("api.modify.fail", nil)
  48. apiAddFileCount = metrics.NewRegisteredCounter("api.addfile.count", nil)
  49. apiAddFileFail = metrics.NewRegisteredCounter("api.addfile.fail", nil)
  50. apiRmFileCount = metrics.NewRegisteredCounter("api.removefile.count", nil)
  51. apiRmFileFail = metrics.NewRegisteredCounter("api.removefile.fail", nil)
  52. apiAppendFileCount = metrics.NewRegisteredCounter("api.appendfile.count", nil)
  53. apiAppendFileFail = metrics.NewRegisteredCounter("api.appendfile.fail", nil)
  54. apiGetInvalid = metrics.NewRegisteredCounter("api.get.invalid", nil)
  55. )
  56. // Resolver interface resolve a domain name to a hash using ENS
  57. type Resolver interface {
  58. Resolve(string) (common.Hash, error)
  59. }
  60. // ResolveValidator is used to validate the contained Resolver
  61. type ResolveValidator interface {
  62. Resolver
  63. Owner(node [32]byte) (common.Address, error)
  64. HeaderByNumber(context.Context, *big.Int) (*types.Header, error)
  65. }
  66. // NoResolverError is returned by MultiResolver.Resolve if no resolver
  67. // can be found for the address.
  68. type NoResolverError struct {
  69. TLD string
  70. }
  71. // NewNoResolverError creates a NoResolverError for the given top level domain
  72. func NewNoResolverError(tld string) *NoResolverError {
  73. return &NoResolverError{TLD: tld}
  74. }
  75. // Error NoResolverError implements error
  76. func (e *NoResolverError) Error() string {
  77. if e.TLD == "" {
  78. return "no ENS resolver"
  79. }
  80. return fmt.Sprintf("no ENS endpoint configured to resolve .%s TLD names", e.TLD)
  81. }
  82. // MultiResolver is used to resolve URL addresses based on their TLDs.
  83. // Each TLD can have multiple resolvers, and the resoluton from the
  84. // first one in the sequence will be returned.
  85. type MultiResolver struct {
  86. resolvers map[string][]ResolveValidator
  87. nameHash func(string) common.Hash
  88. }
  89. // MultiResolverOption sets options for MultiResolver and is used as
  90. // arguments for its constructor.
  91. type MultiResolverOption func(*MultiResolver)
  92. // MultiResolverOptionWithResolver adds a Resolver to a list of resolvers
  93. // for a specific TLD. If TLD is an empty string, the resolver will be added
  94. // to the list of default resolver, the ones that will be used for resolution
  95. // of addresses which do not have their TLD resolver specified.
  96. func MultiResolverOptionWithResolver(r ResolveValidator, tld string) MultiResolverOption {
  97. return func(m *MultiResolver) {
  98. m.resolvers[tld] = append(m.resolvers[tld], r)
  99. }
  100. }
  101. // MultiResolverOptionWithNameHash is unused at the time of this writing
  102. func MultiResolverOptionWithNameHash(nameHash func(string) common.Hash) MultiResolverOption {
  103. return func(m *MultiResolver) {
  104. m.nameHash = nameHash
  105. }
  106. }
  107. // NewMultiResolver creates a new instance of MultiResolver.
  108. func NewMultiResolver(opts ...MultiResolverOption) (m *MultiResolver) {
  109. m = &MultiResolver{
  110. resolvers: make(map[string][]ResolveValidator),
  111. nameHash: ens.EnsNode,
  112. }
  113. for _, o := range opts {
  114. o(m)
  115. }
  116. return m
  117. }
  118. // Resolve resolves address by choosing a Resolver by TLD.
  119. // If there are more default Resolvers, or for a specific TLD,
  120. // the Hash from the the first one which does not return error
  121. // will be returned.
  122. func (m *MultiResolver) Resolve(addr string) (h common.Hash, err error) {
  123. rs, err := m.getResolveValidator(addr)
  124. if err != nil {
  125. return h, err
  126. }
  127. for _, r := range rs {
  128. h, err = r.Resolve(addr)
  129. if err == nil {
  130. return
  131. }
  132. }
  133. return
  134. }
  135. // ValidateOwner checks the ENS to validate that the owner of the given domain is the given eth address
  136. func (m *MultiResolver) ValidateOwner(name string, address common.Address) (bool, error) {
  137. rs, err := m.getResolveValidator(name)
  138. if err != nil {
  139. return false, err
  140. }
  141. var addr common.Address
  142. for _, r := range rs {
  143. addr, err = r.Owner(m.nameHash(name))
  144. // we hide the error if it is not for the last resolver we check
  145. if err == nil {
  146. return addr == address, nil
  147. }
  148. }
  149. return false, err
  150. }
  151. // HeaderByNumber uses the validator of the given domainname and retrieves the header for the given block number
  152. func (m *MultiResolver) HeaderByNumber(ctx context.Context, name string, blockNr *big.Int) (*types.Header, error) {
  153. rs, err := m.getResolveValidator(name)
  154. if err != nil {
  155. return nil, err
  156. }
  157. for _, r := range rs {
  158. var header *types.Header
  159. header, err = r.HeaderByNumber(ctx, blockNr)
  160. // we hide the error if it is not for the last resolver we check
  161. if err == nil {
  162. return header, nil
  163. }
  164. }
  165. return nil, err
  166. }
  167. // getResolveValidator uses the hostname to retrieve the resolver associated with the top level domain
  168. func (m *MultiResolver) getResolveValidator(name string) ([]ResolveValidator, error) {
  169. rs := m.resolvers[""]
  170. tld := path.Ext(name)
  171. if tld != "" {
  172. tld = tld[1:]
  173. rstld, ok := m.resolvers[tld]
  174. if ok {
  175. return rstld, nil
  176. }
  177. }
  178. if len(rs) == 0 {
  179. return rs, NewNoResolverError(tld)
  180. }
  181. return rs, nil
  182. }
  183. // SetNameHash sets the hasher function that hashes the domain into a name hash that ENS uses
  184. func (m *MultiResolver) SetNameHash(nameHash func(string) common.Hash) {
  185. m.nameHash = nameHash
  186. }
  187. /*
  188. API implements webserver/file system related content storage and retrieval
  189. on top of the FileStore
  190. it is the public interface of the FileStore which is included in the ethereum stack
  191. */
  192. type API struct {
  193. resource *mru.Handler
  194. fileStore *storage.FileStore
  195. dns Resolver
  196. }
  197. // NewAPI the api constructor initialises a new API instance.
  198. func NewAPI(fileStore *storage.FileStore, dns Resolver, resourceHandler *mru.Handler) (self *API) {
  199. self = &API{
  200. fileStore: fileStore,
  201. dns: dns,
  202. resource: resourceHandler,
  203. }
  204. return
  205. }
  206. // Upload to be used only in TEST
  207. func (a *API) Upload(ctx context.Context, uploadDir, index string, toEncrypt bool) (hash string, err error) {
  208. fs := NewFileSystem(a)
  209. hash, err = fs.Upload(uploadDir, index, toEncrypt)
  210. return hash, err
  211. }
  212. // Retrieve FileStore reader API
  213. func (a *API) Retrieve(ctx context.Context, addr storage.Address) (reader storage.LazySectionReader, isEncrypted bool) {
  214. return a.fileStore.Retrieve(ctx, addr)
  215. }
  216. // Store wraps the Store API call of the embedded FileStore
  217. func (a *API) Store(ctx context.Context, data io.Reader, size int64, toEncrypt bool) (addr storage.Address, wait func(ctx context.Context) error, err error) {
  218. log.Debug("api.store", "size", size)
  219. return a.fileStore.Store(ctx, data, size, toEncrypt)
  220. }
  221. // ErrResolve is returned when an URI cannot be resolved from ENS.
  222. type ErrResolve error
  223. // Resolve resolves a URI to an Address using the MultiResolver.
  224. func (a *API) Resolve(ctx context.Context, uri *URI) (storage.Address, error) {
  225. apiResolveCount.Inc(1)
  226. log.Trace("resolving", "uri", uri.Addr)
  227. // if the URI is immutable, check if the address looks like a hash
  228. if uri.Immutable() {
  229. key := uri.Address()
  230. if key == nil {
  231. return nil, fmt.Errorf("immutable address not a content hash: %q", uri.Addr)
  232. }
  233. return key, nil
  234. }
  235. // if DNS is not configured, check if the address is a hash
  236. if a.dns == nil {
  237. key := uri.Address()
  238. if key == nil {
  239. apiResolveFail.Inc(1)
  240. return nil, fmt.Errorf("no DNS to resolve name: %q", uri.Addr)
  241. }
  242. return key, nil
  243. }
  244. // try and resolve the address
  245. resolved, err := a.dns.Resolve(uri.Addr)
  246. if err == nil {
  247. return resolved[:], nil
  248. }
  249. key := uri.Address()
  250. if key == nil {
  251. apiResolveFail.Inc(1)
  252. return nil, err
  253. }
  254. return key, nil
  255. }
  256. // Put provides singleton manifest creation on top of FileStore store
  257. func (a *API) Put(ctx context.Context, content string, contentType string, toEncrypt bool) (k storage.Address, wait func(context.Context) error, err error) {
  258. apiPutCount.Inc(1)
  259. r := strings.NewReader(content)
  260. key, waitContent, err := a.fileStore.Store(ctx, r, int64(len(content)), toEncrypt)
  261. if err != nil {
  262. apiPutFail.Inc(1)
  263. return nil, nil, err
  264. }
  265. manifest := fmt.Sprintf(`{"entries":[{"hash":"%v","contentType":"%s"}]}`, key, contentType)
  266. r = strings.NewReader(manifest)
  267. key, waitManifest, err := a.fileStore.Store(ctx, r, int64(len(manifest)), toEncrypt)
  268. if err != nil {
  269. apiPutFail.Inc(1)
  270. return nil, nil, err
  271. }
  272. return key, func(ctx context.Context) error {
  273. err := waitContent(ctx)
  274. if err != nil {
  275. return err
  276. }
  277. return waitManifest(ctx)
  278. }, nil
  279. }
  280. // Get uses iterative manifest retrieval and prefix matching
  281. // to resolve basePath to content using FileStore retrieve
  282. // it returns a section reader, mimeType, status, the key of the actual content and an error
  283. func (a *API) Get(ctx context.Context, manifestAddr storage.Address, path string) (reader storage.LazySectionReader, mimeType string, status int, contentAddr storage.Address, err error) {
  284. log.Debug("api.get", "key", manifestAddr, "path", path)
  285. apiGetCount.Inc(1)
  286. trie, err := loadManifest(ctx, a.fileStore, manifestAddr, nil)
  287. if err != nil {
  288. apiGetNotFound.Inc(1)
  289. status = http.StatusNotFound
  290. log.Warn(fmt.Sprintf("loadManifestTrie error: %v", err))
  291. return
  292. }
  293. log.Debug("trie getting entry", "key", manifestAddr, "path", path)
  294. entry, _ := trie.getEntry(path)
  295. if entry != nil {
  296. log.Debug("trie got entry", "key", manifestAddr, "path", path, "entry.Hash", entry.Hash)
  297. // we need to do some extra work if this is a mutable resource manifest
  298. if entry.ContentType == ResourceContentType {
  299. // get the resource root chunk key
  300. log.Trace("resource type", "key", manifestAddr, "hash", entry.Hash)
  301. ctx, cancel := context.WithCancel(context.Background())
  302. defer cancel()
  303. rsrc, err := a.resource.Load(storage.Address(common.FromHex(entry.Hash)))
  304. if err != nil {
  305. apiGetNotFound.Inc(1)
  306. status = http.StatusNotFound
  307. log.Debug(fmt.Sprintf("get resource content error: %v", err))
  308. return reader, mimeType, status, nil, err
  309. }
  310. // use this key to retrieve the latest update
  311. rsrc, err = a.resource.LookupLatest(ctx, rsrc.NameHash(), true, &mru.LookupParams{})
  312. if err != nil {
  313. apiGetNotFound.Inc(1)
  314. status = http.StatusNotFound
  315. log.Debug(fmt.Sprintf("get resource content error: %v", err))
  316. return reader, mimeType, status, nil, err
  317. }
  318. // if it's multihash, we will transparently serve the content this multihash points to
  319. // \TODO this resolve is rather expensive all in all, review to see if it can be achieved cheaper
  320. if rsrc.Multihash {
  321. // get the data of the update
  322. _, rsrcData, err := a.resource.GetContent(rsrc.NameHash().Hex())
  323. if err != nil {
  324. apiGetNotFound.Inc(1)
  325. status = http.StatusNotFound
  326. log.Warn(fmt.Sprintf("get resource content error: %v", err))
  327. return reader, mimeType, status, nil, err
  328. }
  329. // validate that data as multihash
  330. decodedMultihash, err := multihash.FromMultihash(rsrcData)
  331. if err != nil {
  332. apiGetInvalid.Inc(1)
  333. status = http.StatusUnprocessableEntity
  334. log.Warn("invalid resource multihash", "err", err)
  335. return reader, mimeType, status, nil, err
  336. }
  337. manifestAddr = storage.Address(decodedMultihash)
  338. log.Trace("resource is multihash", "key", manifestAddr)
  339. // get the manifest the multihash digest points to
  340. trie, err := loadManifest(ctx, a.fileStore, manifestAddr, nil)
  341. if err != nil {
  342. apiGetNotFound.Inc(1)
  343. status = http.StatusNotFound
  344. log.Warn(fmt.Sprintf("loadManifestTrie (resource multihash) error: %v", err))
  345. return reader, mimeType, status, nil, err
  346. }
  347. // finally, get the manifest entry
  348. // it will always be the entry on path ""
  349. entry, _ = trie.getEntry(path)
  350. if entry == nil {
  351. status = http.StatusNotFound
  352. apiGetNotFound.Inc(1)
  353. err = fmt.Errorf("manifest (resource multihash) entry for '%s' not found", path)
  354. log.Trace("manifest (resource multihash) entry not found", "key", manifestAddr, "path", path)
  355. return reader, mimeType, status, nil, err
  356. }
  357. } else {
  358. // data is returned verbatim since it's not a multihash
  359. return rsrc, "application/octet-stream", http.StatusOK, nil, nil
  360. }
  361. }
  362. // regardless of resource update manifests or normal manifests we will converge at this point
  363. // get the key the manifest entry points to and serve it if it's unambiguous
  364. contentAddr = common.Hex2Bytes(entry.Hash)
  365. status = entry.Status
  366. if status == http.StatusMultipleChoices {
  367. apiGetHTTP300.Inc(1)
  368. return nil, entry.ContentType, status, contentAddr, err
  369. }
  370. mimeType = entry.ContentType
  371. log.Debug("content lookup key", "key", contentAddr, "mimetype", mimeType)
  372. reader, _ = a.fileStore.Retrieve(ctx, contentAddr)
  373. } else {
  374. // no entry found
  375. status = http.StatusNotFound
  376. apiGetNotFound.Inc(1)
  377. err = fmt.Errorf("manifest entry for '%s' not found", path)
  378. log.Trace("manifest entry not found", "key", contentAddr, "path", path)
  379. }
  380. return
  381. }
  382. // Modify loads manifest and checks the content hash before recalculating and storing the manifest.
  383. func (a *API) Modify(ctx context.Context, addr storage.Address, path, contentHash, contentType string) (storage.Address, error) {
  384. apiModifyCount.Inc(1)
  385. quitC := make(chan bool)
  386. trie, err := loadManifest(ctx, a.fileStore, addr, quitC)
  387. if err != nil {
  388. apiModifyFail.Inc(1)
  389. return nil, err
  390. }
  391. if contentHash != "" {
  392. entry := newManifestTrieEntry(&ManifestEntry{
  393. Path: path,
  394. ContentType: contentType,
  395. }, nil)
  396. entry.Hash = contentHash
  397. trie.addEntry(entry, quitC)
  398. } else {
  399. trie.deleteEntry(path, quitC)
  400. }
  401. if err := trie.recalcAndStore(); err != nil {
  402. apiModifyFail.Inc(1)
  403. return nil, err
  404. }
  405. return trie.ref, nil
  406. }
  407. // AddFile creates a new manifest entry, adds it to swarm, then adds a file to swarm.
  408. func (a *API) AddFile(ctx context.Context, mhash, path, fname string, content []byte, nameresolver bool) (storage.Address, string, error) {
  409. apiAddFileCount.Inc(1)
  410. uri, err := Parse("bzz:/" + mhash)
  411. if err != nil {
  412. apiAddFileFail.Inc(1)
  413. return nil, "", err
  414. }
  415. mkey, err := a.Resolve(ctx, uri)
  416. if err != nil {
  417. apiAddFileFail.Inc(1)
  418. return nil, "", err
  419. }
  420. // trim the root dir we added
  421. if path[:1] == "/" {
  422. path = path[1:]
  423. }
  424. entry := &ManifestEntry{
  425. Path: filepath.Join(path, fname),
  426. ContentType: mime.TypeByExtension(filepath.Ext(fname)),
  427. Mode: 0700,
  428. Size: int64(len(content)),
  429. ModTime: time.Now(),
  430. }
  431. mw, err := a.NewManifestWriter(ctx, mkey, nil)
  432. if err != nil {
  433. apiAddFileFail.Inc(1)
  434. return nil, "", err
  435. }
  436. fkey, err := mw.AddEntry(ctx, bytes.NewReader(content), entry)
  437. if err != nil {
  438. apiAddFileFail.Inc(1)
  439. return nil, "", err
  440. }
  441. newMkey, err := mw.Store()
  442. if err != nil {
  443. apiAddFileFail.Inc(1)
  444. return nil, "", err
  445. }
  446. return fkey, newMkey.String(), nil
  447. }
  448. // RemoveFile removes a file entry in a manifest.
  449. func (a *API) RemoveFile(ctx context.Context, mhash string, path string, fname string, nameresolver bool) (string, error) {
  450. apiRmFileCount.Inc(1)
  451. uri, err := Parse("bzz:/" + mhash)
  452. if err != nil {
  453. apiRmFileFail.Inc(1)
  454. return "", err
  455. }
  456. mkey, err := a.Resolve(ctx, uri)
  457. if err != nil {
  458. apiRmFileFail.Inc(1)
  459. return "", err
  460. }
  461. // trim the root dir we added
  462. if path[:1] == "/" {
  463. path = path[1:]
  464. }
  465. mw, err := a.NewManifestWriter(ctx, mkey, nil)
  466. if err != nil {
  467. apiRmFileFail.Inc(1)
  468. return "", err
  469. }
  470. err = mw.RemoveEntry(filepath.Join(path, fname))
  471. if err != nil {
  472. apiRmFileFail.Inc(1)
  473. return "", err
  474. }
  475. newMkey, err := mw.Store()
  476. if err != nil {
  477. apiRmFileFail.Inc(1)
  478. return "", err
  479. }
  480. return newMkey.String(), nil
  481. }
  482. // AppendFile removes old manifest, appends file entry to new manifest and adds it to Swarm.
  483. func (a *API) AppendFile(ctx context.Context, mhash, path, fname string, existingSize int64, content []byte, oldAddr storage.Address, offset int64, addSize int64, nameresolver bool) (storage.Address, string, error) {
  484. apiAppendFileCount.Inc(1)
  485. buffSize := offset + addSize
  486. if buffSize < existingSize {
  487. buffSize = existingSize
  488. }
  489. buf := make([]byte, buffSize)
  490. oldReader, _ := a.Retrieve(ctx, oldAddr)
  491. io.ReadAtLeast(oldReader, buf, int(offset))
  492. newReader := bytes.NewReader(content)
  493. io.ReadAtLeast(newReader, buf[offset:], int(addSize))
  494. if buffSize < existingSize {
  495. io.ReadAtLeast(oldReader, buf[addSize:], int(buffSize))
  496. }
  497. combinedReader := bytes.NewReader(buf)
  498. totalSize := int64(len(buf))
  499. // TODO(jmozah): to append using pyramid chunker when it is ready
  500. //oldReader := a.Retrieve(oldKey)
  501. //newReader := bytes.NewReader(content)
  502. //combinedReader := io.MultiReader(oldReader, newReader)
  503. uri, err := Parse("bzz:/" + mhash)
  504. if err != nil {
  505. apiAppendFileFail.Inc(1)
  506. return nil, "", err
  507. }
  508. mkey, err := a.Resolve(ctx, uri)
  509. if err != nil {
  510. apiAppendFileFail.Inc(1)
  511. return nil, "", err
  512. }
  513. // trim the root dir we added
  514. if path[:1] == "/" {
  515. path = path[1:]
  516. }
  517. mw, err := a.NewManifestWriter(ctx, mkey, nil)
  518. if err != nil {
  519. apiAppendFileFail.Inc(1)
  520. return nil, "", err
  521. }
  522. err = mw.RemoveEntry(filepath.Join(path, fname))
  523. if err != nil {
  524. apiAppendFileFail.Inc(1)
  525. return nil, "", err
  526. }
  527. entry := &ManifestEntry{
  528. Path: filepath.Join(path, fname),
  529. ContentType: mime.TypeByExtension(filepath.Ext(fname)),
  530. Mode: 0700,
  531. Size: totalSize,
  532. ModTime: time.Now(),
  533. }
  534. fkey, err := mw.AddEntry(ctx, io.Reader(combinedReader), entry)
  535. if err != nil {
  536. apiAppendFileFail.Inc(1)
  537. return nil, "", err
  538. }
  539. newMkey, err := mw.Store()
  540. if err != nil {
  541. apiAppendFileFail.Inc(1)
  542. return nil, "", err
  543. }
  544. return fkey, newMkey.String(), nil
  545. }
  546. // BuildDirectoryTree used by swarmfs_unix
  547. func (a *API) BuildDirectoryTree(ctx context.Context, mhash string, nameresolver bool) (addr storage.Address, manifestEntryMap map[string]*manifestTrieEntry, err error) {
  548. uri, err := Parse("bzz:/" + mhash)
  549. if err != nil {
  550. return nil, nil, err
  551. }
  552. addr, err = a.Resolve(ctx, uri)
  553. if err != nil {
  554. return nil, nil, err
  555. }
  556. quitC := make(chan bool)
  557. rootTrie, err := loadManifest(ctx, a.fileStore, addr, quitC)
  558. if err != nil {
  559. return nil, nil, fmt.Errorf("can't load manifest %v: %v", addr.String(), err)
  560. }
  561. manifestEntryMap = map[string]*manifestTrieEntry{}
  562. err = rootTrie.listWithPrefix(uri.Path, quitC, func(entry *manifestTrieEntry, suffix string) {
  563. manifestEntryMap[suffix] = entry
  564. })
  565. if err != nil {
  566. return nil, nil, fmt.Errorf("list with prefix failed %v: %v", addr.String(), err)
  567. }
  568. return addr, manifestEntryMap, nil
  569. }
  570. // ResourceLookup Looks up mutable resource updates at specific periods and versions
  571. func (a *API) ResourceLookup(ctx context.Context, addr storage.Address, period uint32, version uint32, maxLookup *mru.LookupParams) (string, []byte, error) {
  572. var err error
  573. rsrc, err := a.resource.Load(addr)
  574. if err != nil {
  575. return "", nil, err
  576. }
  577. if version != 0 {
  578. if period == 0 {
  579. return "", nil, mru.NewError(mru.ErrInvalidValue, "Period can't be 0")
  580. }
  581. _, err = a.resource.LookupVersion(ctx, rsrc.NameHash(), period, version, true, maxLookup)
  582. } else if period != 0 {
  583. _, err = a.resource.LookupHistorical(ctx, rsrc.NameHash(), period, true, maxLookup)
  584. } else {
  585. _, err = a.resource.LookupLatest(ctx, rsrc.NameHash(), true, maxLookup)
  586. }
  587. if err != nil {
  588. return "", nil, err
  589. }
  590. var data []byte
  591. _, data, err = a.resource.GetContent(rsrc.NameHash().Hex())
  592. if err != nil {
  593. return "", nil, err
  594. }
  595. return rsrc.Name(), data, nil
  596. }
  597. // ResourceCreate creates Resource and returns its key
  598. func (a *API) ResourceCreate(ctx context.Context, name string, frequency uint64) (storage.Address, error) {
  599. key, _, err := a.resource.New(ctx, name, frequency)
  600. if err != nil {
  601. return nil, err
  602. }
  603. return key, nil
  604. }
  605. // ResourceUpdateMultihash updates a Mutable Resource and marks the update's content to be of multihash type, which will be recognized upon retrieval.
  606. // It will fail if the data is not a valid multihash.
  607. func (a *API) ResourceUpdateMultihash(ctx context.Context, name string, data []byte) (storage.Address, uint32, uint32, error) {
  608. return a.resourceUpdate(ctx, name, data, true)
  609. }
  610. // ResourceUpdate updates a Mutable Resource with arbitrary data.
  611. // Upon retrieval the update will be retrieved verbatim as bytes.
  612. func (a *API) ResourceUpdate(ctx context.Context, name string, data []byte) (storage.Address, uint32, uint32, error) {
  613. return a.resourceUpdate(ctx, name, data, false)
  614. }
  615. func (a *API) resourceUpdate(ctx context.Context, name string, data []byte, multihash bool) (storage.Address, uint32, uint32, error) {
  616. var addr storage.Address
  617. var err error
  618. if multihash {
  619. addr, err = a.resource.UpdateMultihash(ctx, name, data)
  620. } else {
  621. addr, err = a.resource.Update(ctx, name, data)
  622. }
  623. period, _ := a.resource.GetLastPeriod(name)
  624. version, _ := a.resource.GetVersion(name)
  625. return addr, period, version, err
  626. }
  627. // ResourceHashSize returned the size of the digest produced by the Mutable Resource hashing function
  628. func (a *API) ResourceHashSize() int {
  629. return a.resource.HashSize
  630. }
  631. // ResourceIsValidated checks if the Mutable Resource has an active content validator.
  632. func (a *API) ResourceIsValidated() bool {
  633. return a.resource.IsValidated()
  634. }
  635. // ResolveResourceManifest retrieves the Mutable Resource manifest for the given address, and returns the address of the metadata chunk.
  636. func (a *API) ResolveResourceManifest(ctx context.Context, addr storage.Address) (storage.Address, error) {
  637. trie, err := loadManifest(ctx, a.fileStore, addr, nil)
  638. if err != nil {
  639. return nil, fmt.Errorf("cannot load resource manifest: %v", err)
  640. }
  641. entry, _ := trie.getEntry("")
  642. if entry.ContentType != ResourceContentType {
  643. return nil, fmt.Errorf("not a resource manifest: %s", addr)
  644. }
  645. return storage.Address(common.FromHex(entry.Hash)), nil
  646. }