Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
// We wait for the node to close first, as the node has children
// that it will wait for before closing, such as the API server.
node.Close()
_ = coreapi.CloseFakeRepo()

select {
case <-req.Context.Done():
Expand Down
51 changes: 42 additions & 9 deletions core/coreapi/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package coreapi
import (
"context"
"fmt"
"sync"

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

Expand All @@ -27,6 +28,43 @@ import (
)

type UnixfsAPI CoreAPI
var nilNode *core.IpfsNode
var lock sync.Mutex
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use a sync.Once.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


func getOrCreateNilNode() (*core.IpfsNode,error) {
lock.Lock()
defer lock.Unlock()

if nilNode != nil {
return nilNode,nil
}

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 {
return nil, err
}

nilNode = node
return nilNode,nil
}

func CloseFakeRepo() error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't bother with this. We don't actually need to close this node.

lock.Lock()
defer lock.Unlock()

if nilNode != nil {
if err := nilNode.Close(); err != nil {
return err
}
nilNode = nil
}
return nil
}

// Add builds a merkledag node from a reader, adds it to the blockstore,
// and returns the key representing that node.
Expand Down Expand Up @@ -61,18 +99,13 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options
pinning := api.pinning

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

bserv := blockservice.New(addblockstore, exch) // hash security 001
Expand Down