Unverified 提交 062f47ef 作者: Steven Allen 提交者: GitHub

Merge pull request #5938 from ipfs/misc/cleanup-coreunix

Drop some coreunix code
......@@ -4,15 +4,17 @@
package assets
import (
"bytes"
"fmt"
"os"
"path/filepath"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreunix"
"github.com/ipfs/go-ipfs/core/coreapi"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
uio "gx/ipfs/QmSMJ4rZbCJaih3y82Ebq7BZqK6vU2FHsKcWKQiE1DPTpS/go-unixfs/io"
files "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"
// this import keeps gx from thinking the dep isn't used
_ "gx/ipfs/QmT1jwrqzSMjSjLG5oBd9w4P9vXPKQksWuf5ghsE3Q88ZV/dir-index-html"
......@@ -45,7 +47,17 @@ func SeedInitDirIndex(nd *core.IpfsNode) (cid.Cid, error) {
}
func addAssetList(nd *core.IpfsNode, l []string) (cid.Cid, error) {
dirb := uio.NewDirectory(nd.DAG)
api, err := coreapi.NewCoreAPI(nd)
if err != nil {
return cid.Cid{}, err
}
dirb, err := api.Object().New(nd.Context(), options.Object.Type("unixfs-dir"))
if err != nil {
return cid.Cid{}, err
}
basePath := iface.IpfsPath(dirb.Cid())
for _, p := range l {
d, err := Asset(p)
......@@ -53,40 +65,22 @@ func addAssetList(nd *core.IpfsNode, l []string) (cid.Cid, error) {
return cid.Cid{}, fmt.Errorf("assets: could load Asset '%s': %s", p, err)
}
s, err := coreunix.Add(nd, bytes.NewBuffer(d))
fp, err := api.Unixfs().Add(nd.Context(), files.NewBytesFile(d))
if err != nil {
return cid.Cid{}, fmt.Errorf("assets: could not Add '%s': %s", p, err)
return cid.Cid{}, err
}
fname := filepath.Base(p)
c, err := cid.Decode(s)
basePath, err = api.Object().AddLink(nd.Context(), basePath, fname, fp)
if err != nil {
return cid.Cid{}, err
}
node, err := nd.DAG.Get(nd.Context(), c)
if err != nil {
return cid.Cid{}, err
}
if err := dirb.AddChild(nd.Context(), fname, node); err != nil {
return cid.Cid{}, fmt.Errorf("assets: could not add '%s' as a child: %s", fname, err)
}
}
dir, err := dirb.GetNode()
if err != nil {
if err := api.Pin().Add(nd.Context(), basePath); err != nil {
return cid.Cid{}, err
}
if err := nd.Pinning.Pin(nd.Context(), dir, true); err != nil {
return cid.Cid{}, fmt.Errorf("assets: Pinning on init-docu failed: %s", err)
}
if err := nd.Pinning.Flush(); err != nil {
return cid.Cid{}, fmt.Errorf("assets: Pinning flush failed: %s", err)
}
return dir.Cid(), nil
return basePath.Cid(), nil
}
......@@ -11,11 +11,12 @@ import (
commands "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
corehttp "github.com/ipfs/go-ipfs/core/corehttp"
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
process "gx/ipfs/QmSF8fPo3jgVBAy8fpdjjYqgG87dkJgUprRBHRd2tmfgpP/goprocess"
files "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"
config "gx/ipfs/QmcRKBUqc2p3L1ZraoJjbXfs9E6xzvEuyK9iypb5RGwfsr/go-ipfs-config"
homedir "gx/ipfs/QmdcULN1WCzgoQmcCaUAmEhwcxHYsDrbZ2LvRJKCL8dMrK/go-homedir"
fsnotify "gx/ipfs/QmfNjggF4Pt6erqg3NDafD3MdvDHk1qqCVr8pL5hnPucS8/fsnotify"
......@@ -83,6 +84,11 @@ func run(ipfsPath, watchPath string) error {
}
defer node.Close()
api, err := coreapi.NewCoreAPI(node)
if err != nil {
return err
}
if *http {
addr := "/ip4/127.0.0.1/tcp/5001"
var opts = []corehttp.ServeOption{
......@@ -130,9 +136,23 @@ func run(ipfsPath, watchPath string) error {
file, err := os.Open(e.Name)
if err != nil {
log.Println(err)
return
}
defer file.Close()
k, err := coreunix.Add(node, file)
st, err := file.Stat()
if err != nil {
log.Println(err)
return
}
f, err := files.NewReaderPathFile(e.Name, file, st)
if err != nil {
log.Println(err)
return
}
k, err := api.Unixfs().Add(node.Context(), f)
if err != nil {
log.Println(err)
}
......
......@@ -7,10 +7,8 @@ import (
"io"
"os"
gopath "path"
"path/filepath"
"strconv"
"github.com/ipfs/go-ipfs/core"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/pin"
......@@ -276,90 +274,6 @@ func (adder *Adder) outputDirs(path string, fsn mfs.FSNode) error {
}
}
// Add builds a merkledag node from a reader, adds it to the blockstore,
// and returns the key representing that node.
// If you want to pin it, use NewAdder() and Adder.PinRoot().
func Add(n *core.IpfsNode, r io.Reader) (string, error) {
return AddWithContext(n.Context(), n, r)
}
// AddWithContext does the same as Add, but with a custom context.
func AddWithContext(ctx context.Context, n *core.IpfsNode, r io.Reader) (string, error) {
defer n.Blockstore.PinLock().Unlock()
fileAdder, err := NewAdder(ctx, n.Pinning, n.Blockstore, n.DAG)
if err != nil {
return "", err
}
node, err := fileAdder.add(r)
if err != nil {
return "", err
}
return node.Cid().String(), nil
}
// AddR recursively adds files in |path|.
func AddR(n *core.IpfsNode, root string) (key string, err error) {
defer n.Blockstore.PinLock().Unlock()
stat, err := os.Lstat(root)
if err != nil {
return "", err
}
f, err := files.NewSerialFile(root, false, stat)
if err != nil {
return "", err
}
defer f.Close()
fileAdder, err := NewAdder(n.Context(), n.Pinning, n.Blockstore, n.DAG)
if err != nil {
return "", err
}
err = fileAdder.addFileNode(filepath.Base(root), f)
if err != nil {
return "", err
}
nd, err := fileAdder.Finalize()
if err != nil {
return "", err
}
return nd.String(), nil
}
// AddWrapped adds data from a reader, and wraps it with a directory object
// to preserve the filename.
// Returns the path of the added file ("<dir hash>/filename"), the DAG node of
// the directory, and and error if any.
func AddWrapped(n *core.IpfsNode, r io.Reader, filename string) (string, ipld.Node, error) {
fileAdder, err := NewAdder(n.Context(), n.Pinning, n.Blockstore, n.DAG)
if err != nil {
return "", nil, err
}
fileAdder.Wrap = true
defer n.Blockstore.PinLock().Unlock()
err = fileAdder.addFileNode(filename, files.NewReaderFile(r))
if err != nil {
return "", nil, err
}
dagnode, err := fileAdder.Finalize()
if err != nil {
return "", nil, err
}
c := dagnode.Cid()
return gopath.Join(c.String(), filename), dagnode, nil
}
func (adder *Adder) addNode(node ipld.Node, path string) error {
// patch it into the root
if path == "" {
......
......@@ -30,26 +30,6 @@ import (
const testPeerID = "QmTFauExutTsy4XP6JbMFcw2Wa9645HJt2bTqL6qYDCKfe"
func TestAddRecursive(t *testing.T) {
r := &repo.Mock{
C: config.Config{
Identity: config.Identity{
PeerID: testPeerID, // required by offline node
},
},
D: syncds.MutexWrap(datastore.NewMapDatastore()),
}
node, err := core.NewNode(context.Background(), &core.BuildCfg{Repo: r})
if err != nil {
t.Fatal(err)
}
if k, err := AddR(node, "test/data"); err != nil {
t.Fatal(err)
} else if k != "QmWCCga8AbTyfAQ7pTnGT6JgmRMAB3Qp8ZmTEFi5q5o8jC" {
t.Fatal("keys do not match: ", k)
}
}
func TestAddGCLive(t *testing.T) {
r := &repo.Mock{
C: config.Config{
......
......@@ -13,14 +13,13 @@ import (
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreapi"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
mock "github.com/ipfs/go-ipfs/core/mock"
"github.com/ipfs/go-ipfs/thirdparty/unit"
testutil "gx/ipfs/QmNvHv84aH2qZafDuSdKJCQ1cvPZ1kmQmyD4YtzjUHuk9v/go-testutil"
pstore "gx/ipfs/QmPiemjiKBC9VA7vZF82m4x1oygtg2c2YVqag8PX7dN1BD/go-libp2p-peerstore"
random "gx/ipfs/QmSJ9n2s9NUoA9D849W5jj5SJ94nMcZpj1jCgQJieiNqSt/go-random"
files "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"
mocknet "gx/ipfs/QmYxivS34F2M2n44WQQnRHGAKS8aoRUxwGpi9wk4Cdn4Jf/go-libp2p/p2p/net/mock"
logging "gx/ipfs/QmcuXC5cxs79ro2cUuHs4HQ2bkDLJUYokwL8aivcX6HW3C/go-log"
)
......@@ -120,6 +119,11 @@ func DirectAddCat(data []byte, conf testutil.LatencyConfig) error {
}
defer catter.Close()
adderApi, err := coreapi.NewCoreAPI(adder)
if err != nil {
return err
}
catterApi, err := coreapi.NewCoreAPI(catter)
if err != nil {
return err
......@@ -140,17 +144,12 @@ func DirectAddCat(data []byte, conf testutil.LatencyConfig) error {
return err
}
added, err := coreunix.Add(adder, bytes.NewReader(data))
if err != nil {
return err
}
ap, err := iface.ParsePath(added)
added, err := adderApi.Unixfs().Add(ctx, files.NewBytesFile(data))
if err != nil {
return err
}
readerCatted, err := catterApi.Unixfs().Get(ctx, ap)
readerCatted, err := catterApi.Unixfs().Get(ctx, added)
if err != nil {
return err
}
......
......@@ -10,13 +10,12 @@ import (
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreapi"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
mock "github.com/ipfs/go-ipfs/core/mock"
"github.com/ipfs/go-ipfs/thirdparty/unit"
testutil "gx/ipfs/QmNvHv84aH2qZafDuSdKJCQ1cvPZ1kmQmyD4YtzjUHuk9v/go-testutil"
pstore "gx/ipfs/QmPiemjiKBC9VA7vZF82m4x1oygtg2c2YVqag8PX7dN1BD/go-libp2p-peerstore"
files "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"
mocknet "gx/ipfs/QmYxivS34F2M2n44WQQnRHGAKS8aoRUxwGpi9wk4Cdn4Jf/go-libp2p/p2p/net/mock"
)
......@@ -66,6 +65,11 @@ func benchCat(b *testing.B, data []byte, conf testutil.LatencyConfig) error {
}
defer catter.Close()
adderApi, err := coreapi.NewCoreAPI(adder)
if err != nil {
return err
}
catterApi, err := coreapi.NewCoreAPI(catter)
if err != nil {
return err
......@@ -86,18 +90,13 @@ func benchCat(b *testing.B, data []byte, conf testutil.LatencyConfig) error {
return err
}
added, err := coreunix.Add(adder, bytes.NewReader(data))
if err != nil {
return err
}
ap, err := iface.ParsePath(added)
added, err := adderApi.Unixfs().Add(ctx, files.NewBytesFile(data))
if err != nil {
return err
}
b.StartTimer()
readerCatted, err := catterApi.Unixfs().Get(ctx, ap)
readerCatted, err := catterApi.Unixfs().Get(ctx, added)
if err != nil {
return err
}
......
......@@ -11,13 +11,12 @@ import (
core "github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreapi"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
mock "github.com/ipfs/go-ipfs/core/mock"
"github.com/ipfs/go-ipfs/thirdparty/unit"
testutil "gx/ipfs/QmNvHv84aH2qZafDuSdKJCQ1cvPZ1kmQmyD4YtzjUHuk9v/go-testutil"
pstore "gx/ipfs/QmPiemjiKBC9VA7vZF82m4x1oygtg2c2YVqag8PX7dN1BD/go-libp2p-peerstore"
files "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"
mocknet "gx/ipfs/QmYxivS34F2M2n44WQQnRHGAKS8aoRUxwGpi9wk4Cdn4Jf/go-libp2p/p2p/net/mock"
)
......@@ -103,6 +102,11 @@ func RunThreeLeggedCat(data []byte, conf testutil.LatencyConfig) error {
}
defer catter.Close()
adderApi, err := coreapi.NewCoreAPI(adder)
if err != nil {
return err
}
catterApi, err := coreapi.NewCoreAPI(catter)
if err != nil {
return err
......@@ -119,17 +123,12 @@ func RunThreeLeggedCat(data []byte, conf testutil.LatencyConfig) error {
return err
}
added, err := coreunix.Add(adder, bytes.NewReader(data))
if err != nil {
return err
}
ap, err := iface.ParsePath(added)
added, err := adderApi.Unixfs().Add(ctx, files.NewBytesFile(data))
if err != nil {
return err
}
readerCatted, err := catterApi.Unixfs().Get(ctx, ap)
readerCatted, err := catterApi.Unixfs().Get(ctx, added)
if err != nil {
return err
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论