Skip to content

Commit

Permalink
fix(coreapi/add): close the fake repo used when adding with hash-only
Browse files Browse the repository at this point in the history
fixes ipfs#6744
  • Loading branch information
Stebalien authored and Walter Beegle committed Jun 8, 2020
1 parent 7e0ed73 commit 21ff416
Showing 1 changed file with 10 additions and 61 deletions.
71 changes: 10 additions & 61 deletions core/coreapi/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package coreapi
import (
"context"
"fmt"
"sync"

"github.com/ipfs/go-ipfs/core"

Expand All @@ -12,7 +11,6 @@ import (
blockservice "github.com/ipfs/go-blockservice"
cid "github.com/ipfs/go-cid"
cidutil "github.com/ipfs/go-cidutil"
filestore "github.com/ipfs/go-filestore"
bstore "github.com/ipfs/go-ipfs-blockstore"
files "github.com/ipfs/go-ipfs-files"
ipld "github.com/ipfs/go-ipld-format"
Expand All @@ -30,28 +28,6 @@ import (

type UnixfsAPI CoreAPI

var nilNode *core.IpfsNode
var once sync.Once

func getOrCreateNilNode() (*core.IpfsNode, error) {
once.Do(func() {
if nilNode != nil {
return
}
node, err := core.NewNode(context.Background(), &core.BuildCfg{
//TODO: need this to be true or all files
// hashed will be stored in memory!
NilRepo: true,
})
if err != nil {
panic(err)
}
nilNode = node
})

return nilNode, nil
}

// Add builds a merkledag node from a reader, adds it to the blockstore,
// and returns the key representing that node.
func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options.UnixfsAddOption) (path.Resolved, error) {
Expand Down Expand Up @@ -85,41 +61,24 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options
pinning := api.pinning

if settings.OnlyHash {
node, err := getOrCreateNilNode()
nilnode, err := core.NewNode(ctx, &core.BuildCfg{
//TODO: need this to be true or all files
// hashed will be stored in memory!
NilRepo: true,
})
if err != nil {
return nil, err
}
addblockstore = node.Blockstore
exch = node.Exchange
pinning = node.Pinning
defer nilnode.Close()
addblockstore = nilnode.Blockstore
exch = nilnode.Exchange
pinning = nilnode.Pinning
}

bserv := blockservice.New(addblockstore, exch) // hash security 001
dserv := dag.NewDAGService(bserv)

// add a sync call to the DagService
// this ensures that data written to the DagService is persisted to the underlying datastore
// TODO: propagate the Sync function from the datastore through the blockstore, blockservice and dagservice
var syncDserv *syncDagService
if settings.OnlyHash {
syncDserv = &syncDagService{
DAGService: dserv,
syncFn: func() error { return nil },
}
} else {
syncDserv = &syncDagService{
DAGService: dserv,
syncFn: func() error {
ds := api.repo.Datastore()
if err := ds.Sync(bstore.BlockPrefix); err != nil {
return err
}
return ds.Sync(filestore.FilestorePrefix)
},
}
}

fileAdder, err := coreunix.NewAdder(ctx, pinning, addblockstore, syncDserv)
fileAdder, err := coreunix.NewAdder(ctx, pinning, addblockstore, dserv)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -295,13 +254,3 @@ func (api *UnixfsAPI) lsFromLinks(ctx context.Context, ndlinks []*ipld.Link, set
func (api *UnixfsAPI) core() *CoreAPI {
return (*CoreAPI)(api)
}

// syncDagService is used by the Adder to ensure blocks get persisted to the underlying datastore
type syncDagService struct {
ipld.DAGService
syncFn func() error
}

func (s *syncDagService) Sync() error {
return s.syncFn()
}

0 comments on commit 21ff416

Please sign in to comment.