@@ -227,28 +227,28 @@ func NewAPI(fileStore *storage.FileStore, dns Resolver, resourceHandler *mru.Han
227227}
228228
229229// Upload to be used only in TEST
230- func (a * API ) Upload (uploadDir , index string , toEncrypt bool ) (hash string , err error ) {
230+ func (a * API ) Upload (ctx context. Context , uploadDir , index string , toEncrypt bool ) (hash string , err error ) {
231231 fs := NewFileSystem (a )
232232 hash , err = fs .Upload (uploadDir , index , toEncrypt )
233233 return hash , err
234234}
235235
236236// Retrieve FileStore reader API
237- func (a * API ) Retrieve (addr storage.Address ) (reader storage.LazySectionReader , isEncrypted bool ) {
238- return a .fileStore .Retrieve (addr )
237+ func (a * API ) Retrieve (ctx context. Context , addr storage.Address ) (reader storage.LazySectionReader , isEncrypted bool ) {
238+ return a .fileStore .Retrieve (ctx , addr )
239239}
240240
241241// Store wraps the Store API call of the embedded FileStore
242- func (a * API ) Store (data io.Reader , size int64 , toEncrypt bool ) (addr storage.Address , wait func () , err error ) {
242+ 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 ) {
243243 log .Debug ("api.store" , "size" , size )
244- return a .fileStore .Store (data , size , toEncrypt )
244+ return a .fileStore .Store (ctx , data , size , toEncrypt )
245245}
246246
247247// ErrResolve is returned when an URI cannot be resolved from ENS.
248248type ErrResolve error
249249
250250// Resolve resolves a URI to an Address using the MultiResolver.
251- func (a * API ) Resolve (uri * URI ) (storage.Address , error ) {
251+ func (a * API ) Resolve (ctx context. Context , uri * URI ) (storage.Address , error ) {
252252 apiResolveCount .Inc (1 )
253253 log .Trace ("resolving" , "uri" , uri .Addr )
254254
@@ -286,34 +286,37 @@ func (a *API) Resolve(uri *URI) (storage.Address, error) {
286286}
287287
288288// Put provides singleton manifest creation on top of FileStore store
289- func (a * API ) Put (content , contentType string , toEncrypt bool ) (k storage.Address , wait func () , err error ) {
289+ func (a * API ) Put (ctx context. Context , content string , contentType string , toEncrypt bool ) (k storage.Address , wait func (context. Context ) error , err error ) {
290290 apiPutCount .Inc (1 )
291291 r := strings .NewReader (content )
292- key , waitContent , err := a .fileStore .Store (r , int64 (len (content )), toEncrypt )
292+ key , waitContent , err := a .fileStore .Store (ctx , r , int64 (len (content )), toEncrypt )
293293 if err != nil {
294294 apiPutFail .Inc (1 )
295295 return nil , nil , err
296296 }
297297 manifest := fmt .Sprintf (`{"entries":[{"hash":"%v","contentType":"%s"}]}` , key , contentType )
298298 r = strings .NewReader (manifest )
299- key , waitManifest , err := a .fileStore .Store (r , int64 (len (manifest )), toEncrypt )
299+ key , waitManifest , err := a .fileStore .Store (ctx , r , int64 (len (manifest )), toEncrypt )
300300 if err != nil {
301301 apiPutFail .Inc (1 )
302302 return nil , nil , err
303303 }
304- return key , func () {
305- waitContent ()
306- waitManifest ()
304+ return key , func (ctx context.Context ) error {
305+ err := waitContent (ctx )
306+ if err != nil {
307+ return err
308+ }
309+ return waitManifest (ctx )
307310 }, nil
308311}
309312
310313// Get uses iterative manifest retrieval and prefix matching
311314// to resolve basePath to content using FileStore retrieve
312315// it returns a section reader, mimeType, status, the key of the actual content and an error
313- func (a * API ) Get (manifestAddr storage.Address , path string ) (reader storage.LazySectionReader , mimeType string , status int , contentAddr storage.Address , err error ) {
316+ func (a * API ) Get (ctx context. Context , manifestAddr storage.Address , path string ) (reader storage.LazySectionReader , mimeType string , status int , contentAddr storage.Address , err error ) {
314317 log .Debug ("api.get" , "key" , manifestAddr , "path" , path )
315318 apiGetCount .Inc (1 )
316- trie , err := loadManifest (a .fileStore , manifestAddr , nil )
319+ trie , err := loadManifest (ctx , a .fileStore , manifestAddr , nil )
317320 if err != nil {
318321 apiGetNotFound .Inc (1 )
319322 status = http .StatusNotFound
@@ -375,7 +378,7 @@ func (a *API) Get(manifestAddr storage.Address, path string) (reader storage.Laz
375378 log .Trace ("resource is multihash" , "key" , manifestAddr )
376379
377380 // get the manifest the multihash digest points to
378- trie , err := loadManifest (a .fileStore , manifestAddr , nil )
381+ trie , err := loadManifest (ctx , a .fileStore , manifestAddr , nil )
379382 if err != nil {
380383 apiGetNotFound .Inc (1 )
381384 status = http .StatusNotFound
@@ -410,7 +413,7 @@ func (a *API) Get(manifestAddr storage.Address, path string) (reader storage.Laz
410413 }
411414 mimeType = entry .ContentType
412415 log .Debug ("content lookup key" , "key" , contentAddr , "mimetype" , mimeType )
413- reader , _ = a .fileStore .Retrieve (contentAddr )
416+ reader , _ = a .fileStore .Retrieve (ctx , contentAddr )
414417 } else {
415418 // no entry found
416419 status = http .StatusNotFound
@@ -422,10 +425,10 @@ func (a *API) Get(manifestAddr storage.Address, path string) (reader storage.Laz
422425}
423426
424427// Modify loads manifest and checks the content hash before recalculating and storing the manifest.
425- func (a * API ) Modify (addr storage.Address , path , contentHash , contentType string ) (storage.Address , error ) {
428+ func (a * API ) Modify (ctx context. Context , addr storage.Address , path , contentHash , contentType string ) (storage.Address , error ) {
426429 apiModifyCount .Inc (1 )
427430 quitC := make (chan bool )
428- trie , err := loadManifest (a .fileStore , addr , quitC )
431+ trie , err := loadManifest (ctx , a .fileStore , addr , quitC )
429432 if err != nil {
430433 apiModifyFail .Inc (1 )
431434 return nil , err
@@ -449,15 +452,15 @@ func (a *API) Modify(addr storage.Address, path, contentHash, contentType string
449452}
450453
451454// AddFile creates a new manifest entry, adds it to swarm, then adds a file to swarm.
452- func (a * API ) AddFile (mhash , path , fname string , content []byte , nameresolver bool ) (storage.Address , string , error ) {
455+ func (a * API ) AddFile (ctx context. Context , mhash , path , fname string , content []byte , nameresolver bool ) (storage.Address , string , error ) {
453456 apiAddFileCount .Inc (1 )
454457
455458 uri , err := Parse ("bzz:/" + mhash )
456459 if err != nil {
457460 apiAddFileFail .Inc (1 )
458461 return nil , "" , err
459462 }
460- mkey , err := a .Resolve (uri )
463+ mkey , err := a .Resolve (ctx , uri )
461464 if err != nil {
462465 apiAddFileFail .Inc (1 )
463466 return nil , "" , err
@@ -476,13 +479,13 @@ func (a *API) AddFile(mhash, path, fname string, content []byte, nameresolver bo
476479 ModTime : time .Now (),
477480 }
478481
479- mw , err := a .NewManifestWriter (mkey , nil )
482+ mw , err := a .NewManifestWriter (ctx , mkey , nil )
480483 if err != nil {
481484 apiAddFileFail .Inc (1 )
482485 return nil , "" , err
483486 }
484487
485- fkey , err := mw .AddEntry (bytes .NewReader (content ), entry )
488+ fkey , err := mw .AddEntry (ctx , bytes .NewReader (content ), entry )
486489 if err != nil {
487490 apiAddFileFail .Inc (1 )
488491 return nil , "" , err
@@ -496,19 +499,18 @@ func (a *API) AddFile(mhash, path, fname string, content []byte, nameresolver bo
496499 }
497500
498501 return fkey , newMkey .String (), nil
499-
500502}
501503
502504// RemoveFile removes a file entry in a manifest.
503- func (a * API ) RemoveFile (mhash , path , fname string , nameresolver bool ) (string , error ) {
505+ func (a * API ) RemoveFile (ctx context. Context , mhash string , path string , fname string , nameresolver bool ) (string , error ) {
504506 apiRmFileCount .Inc (1 )
505507
506508 uri , err := Parse ("bzz:/" + mhash )
507509 if err != nil {
508510 apiRmFileFail .Inc (1 )
509511 return "" , err
510512 }
511- mkey , err := a .Resolve (uri )
513+ mkey , err := a .Resolve (ctx , uri )
512514 if err != nil {
513515 apiRmFileFail .Inc (1 )
514516 return "" , err
@@ -519,7 +521,7 @@ func (a *API) RemoveFile(mhash, path, fname string, nameresolver bool) (string,
519521 path = path [1 :]
520522 }
521523
522- mw , err := a .NewManifestWriter (mkey , nil )
524+ mw , err := a .NewManifestWriter (ctx , mkey , nil )
523525 if err != nil {
524526 apiRmFileFail .Inc (1 )
525527 return "" , err
@@ -542,7 +544,7 @@ func (a *API) RemoveFile(mhash, path, fname string, nameresolver bool) (string,
542544}
543545
544546// AppendFile removes old manifest, appends file entry to new manifest and adds it to Swarm.
545- func (a * API ) AppendFile (mhash , path , fname string , existingSize int64 , content []byte , oldAddr storage.Address , offset int64 , addSize int64 , nameresolver bool ) (storage.Address , string , error ) {
547+ 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 ) {
546548 apiAppendFileCount .Inc (1 )
547549
548550 buffSize := offset + addSize
@@ -552,7 +554,7 @@ func (a *API) AppendFile(mhash, path, fname string, existingSize int64, content
552554
553555 buf := make ([]byte , buffSize )
554556
555- oldReader , _ := a .Retrieve (oldAddr )
557+ oldReader , _ := a .Retrieve (ctx , oldAddr )
556558 io .ReadAtLeast (oldReader , buf , int (offset ))
557559
558560 newReader := bytes .NewReader (content )
@@ -575,7 +577,7 @@ func (a *API) AppendFile(mhash, path, fname string, existingSize int64, content
575577 apiAppendFileFail .Inc (1 )
576578 return nil , "" , err
577579 }
578- mkey , err := a .Resolve (uri )
580+ mkey , err := a .Resolve (ctx , uri )
579581 if err != nil {
580582 apiAppendFileFail .Inc (1 )
581583 return nil , "" , err
@@ -586,7 +588,7 @@ func (a *API) AppendFile(mhash, path, fname string, existingSize int64, content
586588 path = path [1 :]
587589 }
588590
589- mw , err := a .NewManifestWriter (mkey , nil )
591+ mw , err := a .NewManifestWriter (ctx , mkey , nil )
590592 if err != nil {
591593 apiAppendFileFail .Inc (1 )
592594 return nil , "" , err
@@ -606,7 +608,7 @@ func (a *API) AppendFile(mhash, path, fname string, existingSize int64, content
606608 ModTime : time .Now (),
607609 }
608610
609- fkey , err := mw .AddEntry (io .Reader (combinedReader ), entry )
611+ fkey , err := mw .AddEntry (ctx , io .Reader (combinedReader ), entry )
610612 if err != nil {
611613 apiAppendFileFail .Inc (1 )
612614 return nil , "" , err
@@ -620,23 +622,22 @@ func (a *API) AppendFile(mhash, path, fname string, existingSize int64, content
620622 }
621623
622624 return fkey , newMkey .String (), nil
623-
624625}
625626
626627// BuildDirectoryTree used by swarmfs_unix
627- func (a * API ) BuildDirectoryTree (mhash string , nameresolver bool ) (addr storage.Address , manifestEntryMap map [string ]* manifestTrieEntry , err error ) {
628+ func (a * API ) BuildDirectoryTree (ctx context. Context , mhash string , nameresolver bool ) (addr storage.Address , manifestEntryMap map [string ]* manifestTrieEntry , err error ) {
628629
629630 uri , err := Parse ("bzz:/" + mhash )
630631 if err != nil {
631632 return nil , nil , err
632633 }
633- addr , err = a .Resolve (uri )
634+ addr , err = a .Resolve (ctx , uri )
634635 if err != nil {
635636 return nil , nil , err
636637 }
637638
638639 quitC := make (chan bool )
639- rootTrie , err := loadManifest (a .fileStore , addr , quitC )
640+ rootTrie , err := loadManifest (ctx , a .fileStore , addr , quitC )
640641 if err != nil {
641642 return nil , nil , fmt .Errorf ("can't load manifest %v: %v" , addr .String (), err )
642643 }
@@ -725,8 +726,8 @@ func (a *API) ResourceIsValidated() bool {
725726}
726727
727728// ResolveResourceManifest retrieves the Mutable Resource manifest for the given address, and returns the address of the metadata chunk.
728- func (a * API ) ResolveResourceManifest (addr storage.Address ) (storage.Address , error ) {
729- trie , err := loadManifest (a .fileStore , addr , nil )
729+ func (a * API ) ResolveResourceManifest (ctx context. Context , addr storage.Address ) (storage.Address , error ) {
730+ trie , err := loadManifest (ctx , a .fileStore , addr , nil )
730731 if err != nil {
731732 return nil , fmt .Errorf ("cannot load resource manifest: %v" , err )
732733 }
0 commit comments