mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 18:37:45 +08:00
Merge pull request #5938 from ipfs/misc/cleanup-coreunix
Drop some coreunix code
This commit is contained in:
commit
062f47ef91
@ -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)
|
||||
}
|
||||
|
||||
@ -13,15 +13,17 @@ import (
|
||||
|
||||
version "github.com/ipfs/go-ipfs"
|
||||
core "github.com/ipfs/go-ipfs/core"
|
||||
coreunix "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"
|
||||
namesys "github.com/ipfs/go-ipfs/namesys"
|
||||
nsopts "github.com/ipfs/go-ipfs/namesys/opts"
|
||||
repo "github.com/ipfs/go-ipfs/repo"
|
||||
|
||||
ci "gx/ipfs/QmNiJiXwWE3kRhZrC5ej3kSjWHm337pYfhjLGSCDNKJP2s/go-libp2p-crypto"
|
||||
path "gx/ipfs/QmWqh9oob7ZHQRwU5CdTqpnC8ip8BEkFNrwXRxeNo5Y7vA/go-path"
|
||||
files "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"
|
||||
id "gx/ipfs/QmYxivS34F2M2n44WQQnRHGAKS8aoRUxwGpi9wk4Cdn4Jf/go-libp2p/p2p/protocol/identify"
|
||||
dag "gx/ipfs/Qmb2UEG2TAeVrEJSjqsZF7Y2he7wRDkrdt6c3bECxwZf4k/go-merkledag"
|
||||
config "gx/ipfs/QmcRKBUqc2p3L1ZraoJjbXfs9E6xzvEuyK9iypb5RGwfsr/go-ipfs-config"
|
||||
datastore "gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore"
|
||||
syncds "gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore/sync"
|
||||
@ -117,7 +119,7 @@ func doWithoutRedirect(req *http.Request) (*http.Response, error) {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func newTestServerAndNode(t *testing.T, ns mockNamesys) (*httptest.Server, *core.IpfsNode) {
|
||||
func newTestServerAndNode(t *testing.T, ns mockNamesys) (*httptest.Server, iface.CoreAPI, context.Context) {
|
||||
n, err := newNodeWithMockNamesys(ns)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@ -144,23 +146,28 @@ func newTestServerAndNode(t *testing.T, ns mockNamesys) (*httptest.Server, *core
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
return ts, n
|
||||
api, err := coreapi.NewCoreAPI(n)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
return ts, api, n.Context()
|
||||
}
|
||||
|
||||
func TestGatewayGet(t *testing.T) {
|
||||
ns := mockNamesys{}
|
||||
ts, n := newTestServerAndNode(t, ns)
|
||||
ts, api, ctx := newTestServerAndNode(t, ns)
|
||||
defer ts.Close()
|
||||
|
||||
k, err := coreunix.Add(n, strings.NewReader("fnord"))
|
||||
k, err := api.Unixfs().Add(ctx, files.NewBytesFile([]byte("fnord")))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ns["/ipns/example.com"] = path.FromString("/ipfs/" + k)
|
||||
ns["/ipns/working.example.com"] = path.FromString("/ipfs/" + k)
|
||||
ns["/ipns/example.com"] = path.FromString(k.String())
|
||||
ns["/ipns/working.example.com"] = path.FromString(k.String())
|
||||
ns["/ipns/double.example.com"] = path.FromString("/ipns/working.example.com")
|
||||
ns["/ipns/triple.example.com"] = path.FromString("/ipns/double.example.com")
|
||||
ns["/ipns/broken.example.com"] = path.FromString("/ipns/" + k)
|
||||
ns["/ipns/broken.example.com"] = path.FromString("/ipns/" + k.Cid().String())
|
||||
// We picked .man because:
|
||||
// 1. It's a valid TLD.
|
||||
// 2. Go treats it as the file extension for "man" files (even though
|
||||
@ -168,18 +175,18 @@ func TestGatewayGet(t *testing.T) {
|
||||
//
|
||||
// Unfortunately, this may not work on all platforms as file type
|
||||
// detection is platform dependent.
|
||||
ns["/ipns/example.man"] = path.FromString("/ipfs/" + k)
|
||||
ns["/ipns/example.man"] = path.FromString(k.String())
|
||||
|
||||
t.Log(ts.URL)
|
||||
for _, test := range []struct {
|
||||
for i, test := range []struct {
|
||||
host string
|
||||
path string
|
||||
status int
|
||||
text string
|
||||
}{
|
||||
{"localhost:5001", "/", http.StatusNotFound, "404 page not found\n"},
|
||||
{"localhost:5001", "/" + k, http.StatusNotFound, "404 page not found\n"},
|
||||
{"localhost:5001", "/ipfs/" + k, http.StatusOK, "fnord"},
|
||||
{"localhost:5001", "/" + k.Cid().String(), http.StatusNotFound, "404 page not found\n"},
|
||||
{"localhost:5001", k.String(), http.StatusOK, "fnord"},
|
||||
{"localhost:5001", "/ipns/nxdomain.example.com", http.StatusNotFound, "ipfs resolve -r /ipns/nxdomain.example.com: " + namesys.ErrResolveFailed.Error() + "\n"},
|
||||
{"localhost:5001", "/ipns/%0D%0A%0D%0Ahello", http.StatusNotFound, "ipfs resolve -r /ipns/%0D%0A%0D%0Ahello: " + namesys.ErrResolveFailed.Error() + "\n"},
|
||||
{"localhost:5001", "/ipns/example.com", http.StatusOK, "fnord"},
|
||||
@ -188,9 +195,9 @@ func TestGatewayGet(t *testing.T) {
|
||||
{"working.example.com", "/", http.StatusOK, "fnord"},
|
||||
{"double.example.com", "/", http.StatusOK, "fnord"},
|
||||
{"triple.example.com", "/", http.StatusOK, "fnord"},
|
||||
{"working.example.com", "/ipfs/" + k, http.StatusNotFound, "ipfs resolve -r /ipns/working.example.com/ipfs/" + k + ": no link named \"ipfs\" under " + k + "\n"},
|
||||
{"working.example.com", k.String(), http.StatusNotFound, "ipfs resolve -r /ipns/working.example.com" + k.String() + ": no link named \"ipfs\" under " + k.Cid().String() + "\n"},
|
||||
{"broken.example.com", "/", http.StatusNotFound, "ipfs resolve -r /ipns/broken.example.com/: " + namesys.ErrResolveFailed.Error() + "\n"},
|
||||
{"broken.example.com", "/ipfs/" + k, http.StatusNotFound, "ipfs resolve -r /ipns/broken.example.com/ipfs/" + k + ": " + namesys.ErrResolveFailed.Error() + "\n"},
|
||||
{"broken.example.com", k.String(), http.StatusNotFound, "ipfs resolve -r /ipns/broken.example.com" + k.String() + ": " + namesys.ErrResolveFailed.Error() + "\n"},
|
||||
// This test case ensures we don't treat the TLD as a file extension.
|
||||
{"example.man", "/", http.StatusOK, "fnord"},
|
||||
} {
|
||||
@ -213,7 +220,7 @@ func TestGatewayGet(t *testing.T) {
|
||||
t.Errorf("expected content type to be text/plain, got %s", contentType)
|
||||
}
|
||||
if resp.StatusCode != test.status {
|
||||
t.Errorf("got %d, expected %d from %s", resp.StatusCode, test.status, urlstr)
|
||||
t.Errorf("(%d) got %d, expected %d from %s", i, resp.StatusCode, test.status, urlstr)
|
||||
continue
|
||||
}
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
@ -232,39 +239,26 @@ func TestIPNSHostnameRedirect(t *testing.T) {
|
||||
defer cancel()
|
||||
|
||||
ns := mockNamesys{}
|
||||
ts, n := newTestServerAndNode(t, ns)
|
||||
ts, api, ctx := newTestServerAndNode(t, ns)
|
||||
t.Logf("test server url: %s", ts.URL)
|
||||
defer ts.Close()
|
||||
|
||||
// create /ipns/example.net/foo/index.html
|
||||
_, dagn1, err := coreunix.AddWrapped(n, strings.NewReader("_"), "_")
|
||||
|
||||
f1 := files.NewMapDirectory(map[string]files.Node{
|
||||
"_": files.NewBytesFile([]byte("_")),
|
||||
"foo": files.NewMapDirectory(map[string]files.Node{
|
||||
"index.html": files.NewBytesFile([]byte("_")),
|
||||
}),
|
||||
})
|
||||
|
||||
k, err := api.Unixfs().Add(ctx, f1, options.Unixfs.Wrap(true))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, dagn2, err := coreunix.AddWrapped(n, strings.NewReader("_"), "index.html")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
dagn1.(*dag.ProtoNode).AddNodeLink("foo", dagn2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = n.DAG.Add(ctx, dagn2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = n.DAG.Add(ctx, dagn1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
k := dagn1.Cid()
|
||||
t.Logf("k: %s\n", k)
|
||||
ns["/ipns/example.net"] = path.FromString("/ipfs/" + k.String())
|
||||
ns["/ipns/example.net"] = path.FromString(k.String())
|
||||
|
||||
// make request to directory containing index.html
|
||||
req, err := http.NewRequest("GET", ts.URL+"/foo", nil)
|
||||
@ -336,45 +330,38 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
|
||||
defer cancel()
|
||||
|
||||
ns := mockNamesys{}
|
||||
ts, n := newTestServerAndNode(t, ns)
|
||||
ts, api, ctx := newTestServerAndNode(t, ns)
|
||||
t.Logf("test server url: %s", ts.URL)
|
||||
defer ts.Close()
|
||||
|
||||
f1 := files.NewMapDirectory(map[string]files.Node{
|
||||
"file.txt": files.NewBytesFile([]byte("1")),
|
||||
"foo? #<'": files.NewMapDirectory(map[string]files.Node{
|
||||
"file.txt": files.NewBytesFile([]byte("2")),
|
||||
"bar": files.NewMapDirectory(map[string]files.Node{
|
||||
"file.txt": files.NewBytesFile([]byte("3")),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
// create /ipns/example.net/foo/
|
||||
_, dagn1, err := coreunix.AddWrapped(n, strings.NewReader("1"), "file.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, dagn2, err := coreunix.AddWrapped(n, strings.NewReader("2"), "file.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, dagn3, err := coreunix.AddWrapped(n, strings.NewReader("3"), "file.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dagn2.(*dag.ProtoNode).AddNodeLink("bar", dagn3)
|
||||
dagn1.(*dag.ProtoNode).AddNodeLink("foo? #<'", dagn2)
|
||||
k, err := api.Unixfs().Add(ctx, f1, options.Unixfs.Wrap(true))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = n.DAG.Add(ctx, dagn3)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = n.DAG.Add(ctx, dagn2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = n.DAG.Add(ctx, dagn1)
|
||||
k2, err := api.ResolvePath(ctx, iface.Join(k, "foo? #<'"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
k3, err := api.ResolvePath(ctx, iface.Join(k, "foo? #<'/bar"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
k := dagn1.Cid()
|
||||
t.Logf("k: %s\n", k)
|
||||
ns["/ipns/example.net"] = path.FromString("/ipfs/" + k.String())
|
||||
ns["/ipns/example.net"] = path.FromString(k.String())
|
||||
|
||||
// make request to directory listing
|
||||
req, err := http.NewRequest("GET", ts.URL+"/foo%3F%20%23%3C%27/", nil)
|
||||
@ -405,7 +392,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
|
||||
if !strings.Contains(s, "<a href=\"/foo%3F%20%23%3C%27/file.txt\">") {
|
||||
t.Fatalf("expected file in directory listing")
|
||||
}
|
||||
if !strings.Contains(s, dagn2.Cid().String()) {
|
||||
if !strings.Contains(s, k2.Cid().String()) {
|
||||
t.Fatalf("expected hash in directory listing")
|
||||
}
|
||||
|
||||
@ -438,7 +425,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
|
||||
if !strings.Contains(s, "<a href=\"/file.txt\">") {
|
||||
t.Fatalf("expected file in directory listing")
|
||||
}
|
||||
if !strings.Contains(s, dagn1.Cid().String()) {
|
||||
if !strings.Contains(s, k.Cid().String()) {
|
||||
t.Fatalf("expected hash in directory listing")
|
||||
}
|
||||
|
||||
@ -471,7 +458,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
|
||||
if !strings.Contains(s, "<a href=\"/foo%3F%20%23%3C%27/bar/file.txt\">") {
|
||||
t.Fatalf("expected file in directory listing")
|
||||
}
|
||||
if !strings.Contains(s, dagn3.Cid().String()) {
|
||||
if !strings.Contains(s, k3.Cid().String()) {
|
||||
t.Fatalf("expected hash in directory listing")
|
||||
}
|
||||
|
||||
@ -505,7 +492,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
|
||||
if !strings.Contains(s, "<a href=\"/good-prefix/file.txt\">") {
|
||||
t.Fatalf("expected file in directory listing")
|
||||
}
|
||||
if !strings.Contains(s, dagn1.Cid().String()) {
|
||||
if !strings.Contains(s, k.Cid().String()) {
|
||||
t.Fatalf("expected hash in directory listing")
|
||||
}
|
||||
|
||||
@ -547,13 +534,13 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
|
||||
if !strings.Contains(s, "<a href=\"/file.txt\">") {
|
||||
t.Fatalf("expected file in directory listing")
|
||||
}
|
||||
if !strings.Contains(s, dagn1.Cid().String()) {
|
||||
if !strings.Contains(s, k.Cid().String()) {
|
||||
t.Fatalf("expected hash in directory listing")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCacheControlImmutable(t *testing.T) {
|
||||
ts, _ := newTestServerAndNode(t, nil)
|
||||
ts, _, _ := newTestServerAndNode(t, nil)
|
||||
t.Logf("test server url: %s", ts.URL)
|
||||
defer ts.Close()
|
||||
|
||||
@ -579,7 +566,7 @@ func TestCacheControlImmutable(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGoGetSupport(t *testing.T) {
|
||||
ts, _ := newTestServerAndNode(t, nil)
|
||||
ts, _, _ := newTestServerAndNode(t, nil)
|
||||
t.Logf("test server url: %s", ts.URL)
|
||||
defer ts.Close()
|
||||
|
||||
@ -603,7 +590,7 @@ func TestVersion(t *testing.T) {
|
||||
version.CurrentCommit = "theshortcommithash"
|
||||
|
||||
ns := mockNamesys{}
|
||||
ts, _ := newTestServerAndNode(t, ns)
|
||||
ts, _, _ := newTestServerAndNode(t, ns)
|
||||
t.Logf("test server url: %s", ts.URL)
|
||||
defer ts.Close()
|
||||
|
||||
|
||||
@ -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))
|
||||
added, err := adderApi.Unixfs().Add(ctx, files.NewBytesFile(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ap, err := iface.ParsePath(added)
|
||||
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))
|
||||
added, err := adderApi.Unixfs().Add(ctx, files.NewBytesFile(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ap, err := iface.ParsePath(added)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
readerCatted, err := catterApi.Unixfs().Get(ctx, ap)
|
||||
readerCatted, err := catterApi.Unixfs().Get(ctx, added)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user