Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/commands!: remove deprecated object APIs #10375

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 6 additions & 18 deletions assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (
"github.com/ipfs/kubo/core/coreapi"

"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/path"
cid "github.com/ipfs/go-cid"
options "github.com/ipfs/kubo/core/coreiface/options"
)

//go:embed init-doc
Expand Down Expand Up @@ -39,30 +37,20 @@ func addAssetList(nd *core.IpfsNode, l []string) (cid.Cid, error) {
return cid.Cid{}, err
}

dirb, err := api.Object().New(nd.Context(), options.Object.Type("unixfs-dir"))
if err != nil {
return cid.Cid{}, err
}

basePath := path.FromCid(dirb.Cid())
dirMap := map[string]files.Node{}

for _, p := range l {
d, err := Asset.ReadFile(p)
if err != nil {
return cid.Cid{}, fmt.Errorf("assets: could load Asset '%s': %s", p, err)
}

fp, err := api.Unixfs().Add(nd.Context(), files.NewBytesFile(d))
if err != nil {
return cid.Cid{}, err
}

fname := gopath.Base(p)
dirMap[gopath.Base(p)] = files.NewBytesFile(d)
}

basePath, err = api.Object().AddLink(nd.Context(), basePath, fname, fp)
if err != nil {
return cid.Cid{}, err
}
basePath, err := api.Unixfs().Add(nd.Context(), files.NewMapDirectory(dirMap))
if err != nil {
return cid.Cid{}, err
}

if err := api.Pin().Add(nd.Context(), basePath); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion bin/ipns-republish
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if [ $? -ne 0 ]; then
fi

# check the object is there
ipfs object stat "$1" >/dev/null
ipfs dag stat "$1" >/dev/null
if [ $? -ne 0 ]; then
echo "error: ipfs cannot find $1"
exit 1
Expand Down
172 changes: 0 additions & 172 deletions client/rpc/object.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package rpc

import (
"bytes"
"context"
"fmt"
"io"

"github.com/ipfs/boxo/ipld/merkledag"
ft "github.com/ipfs/boxo/ipld/unixfs"
"github.com/ipfs/boxo/path"
"github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
iface "github.com/ipfs/kubo/core/coreiface"
caopts "github.com/ipfs/kubo/core/coreiface/options"
)
Expand All @@ -21,138 +15,6 @@ type objectOut struct {
Hash string
}

func (api *ObjectAPI) New(ctx context.Context, opts ...caopts.ObjectNewOption) (ipld.Node, error) {
options, err := caopts.ObjectNewOptions(opts...)
if err != nil {
return nil, err
}

var n ipld.Node
switch options.Type {
case "empty":
n = new(merkledag.ProtoNode)
case "unixfs-dir":
n = ft.EmptyDirNode()
default:
return nil, fmt.Errorf("unknown object type: %s", options.Type)
}

return n, nil
}

func (api *ObjectAPI) Put(ctx context.Context, r io.Reader, opts ...caopts.ObjectPutOption) (path.ImmutablePath, error) {
options, err := caopts.ObjectPutOptions(opts...)
if err != nil {
return path.ImmutablePath{}, err
}

var out objectOut
err = api.core().Request("object/put").
Option("inputenc", options.InputEnc).
Option("datafieldenc", options.DataType).
Option("pin", options.Pin).
FileBody(r).
Exec(ctx, &out)
if err != nil {
return path.ImmutablePath{}, err
}

c, err := cid.Parse(out.Hash)
if err != nil {
return path.ImmutablePath{}, err
}

return path.FromCid(c), nil
}

func (api *ObjectAPI) Get(ctx context.Context, p path.Path) (ipld.Node, error) {
r, err := api.core().Block().Get(ctx, p)
if err != nil {
return nil, err
}
b, err := io.ReadAll(r)
if err != nil {
return nil, err
}

return merkledag.DecodeProtobuf(b)
}

func (api *ObjectAPI) Data(ctx context.Context, p path.Path) (io.Reader, error) {
resp, err := api.core().Request("object/data", p.String()).Send(ctx)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}

// TODO: make Data return ReadCloser to avoid copying
defer resp.Close()
b := new(bytes.Buffer)
if _, err := io.Copy(b, resp.Output); err != nil {
return nil, err
}

return b, nil
}

func (api *ObjectAPI) Links(ctx context.Context, p path.Path) ([]*ipld.Link, error) {
var out struct {
Links []struct {
Name string
Hash string
Size uint64
}
}
if err := api.core().Request("object/links", p.String()).Exec(ctx, &out); err != nil {
return nil, err
}
res := make([]*ipld.Link, len(out.Links))
for i, l := range out.Links {
c, err := cid.Parse(l.Hash)
if err != nil {
return nil, err
}

res[i] = &ipld.Link{
Cid: c,
Name: l.Name,
Size: l.Size,
}
}

return res, nil
}

func (api *ObjectAPI) Stat(ctx context.Context, p path.Path) (*iface.ObjectStat, error) {
var out struct {
Hash string
NumLinks int
BlockSize int
LinksSize int
DataSize int
CumulativeSize int
}
if err := api.core().Request("object/stat", p.String()).Exec(ctx, &out); err != nil {
return nil, err
}

c, err := cid.Parse(out.Hash)
if err != nil {
return nil, err
}

return &iface.ObjectStat{
Cid: c,
NumLinks: out.NumLinks,
BlockSize: out.BlockSize,
LinksSize: out.LinksSize,
DataSize: out.DataSize,
CumulativeSize: out.CumulativeSize,
}, nil
}

func (api *ObjectAPI) AddLink(ctx context.Context, base path.Path, name string, child path.Path, opts ...caopts.ObjectAddLinkOption) (path.ImmutablePath, error) {
options, err := caopts.ObjectAddLinkOptions(opts...)
if err != nil {
Expand Down Expand Up @@ -191,40 +53,6 @@ func (api *ObjectAPI) RmLink(ctx context.Context, base path.Path, link string) (
return path.FromCid(c), nil
}

func (api *ObjectAPI) AppendData(ctx context.Context, p path.Path, r io.Reader) (path.ImmutablePath, error) {
var out objectOut
err := api.core().Request("object/patch/append-data", p.String()).
FileBody(r).
Exec(ctx, &out)
if err != nil {
return path.ImmutablePath{}, err
}

c, err := cid.Parse(out.Hash)
if err != nil {
return path.ImmutablePath{}, err
}

return path.FromCid(c), nil
}

func (api *ObjectAPI) SetData(ctx context.Context, p path.Path, r io.Reader) (path.ImmutablePath, error) {
var out objectOut
err := api.core().Request("object/patch/set-data", p.String()).
FileBody(r).
Exec(ctx, &out)
if err != nil {
return path.ImmutablePath{}, err
}

c, err := cid.Parse(out.Hash)
if err != nil {
return path.ImmutablePath{}, err
}

return path.FromCid(c), nil
}

type change struct {
Type iface.ChangeType
Path string
Expand Down
5 changes: 5 additions & 0 deletions core/commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func TestCommands(t *testing.T) {
"/dag/stat",
"/dht",
"/dht/query",
"/dht/findprovs",
"/dht/findpeer",
"/dht/get",
"/dht/provide",
"/dht/put",
"/routing",
"/routing/put",
"/routing/get",
Expand Down
18 changes: 17 additions & 1 deletion core/commands/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ import (
var ErrNotDHT = errors.New("routing service is not a DHT")

var DhtCmd = &cmds.Command{
Status: cmds.Deprecated,
Helptext: cmds.HelpText{
Tagline: "Issue commands directly through the DHT.",
ShortDescription: ``,
},

Subcommands: map[string]*cmds.Command{
"query": queryDhtCmd,
"query": queryDhtCmd,
"findprovs": RemovedDHTCmd,
"findpeer": RemovedDHTCmd,
"get": RemovedDHTCmd,
"put": RemovedDHTCmd,
"provide": RemovedDHTCmd,
},
}

Expand All @@ -32,6 +38,7 @@ type kademlia interface {
}

var queryDhtCmd = &cmds.Command{
Status: cmds.Deprecated,
Helptext: cmds.HelpText{
Tagline: "Find the closest Peer IDs to a given Peer ID by querying the DHT.",
ShortDescription: "Outputs a list of newline-delimited Peer IDs.",
Expand Down Expand Up @@ -114,3 +121,12 @@ var queryDhtCmd = &cmds.Command{
},
Type: routing.QueryEvent{},
}
var RemovedDHTCmd = &cmds.Command{
Status: cmds.Removed,
hacdias marked this conversation as resolved.
Show resolved Hide resolved
Helptext: cmds.HelpText{
Tagline: "Removed, use 'ipfs routing' instead.",
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
return errors.New("removed, use 'ipfs routing' instead")
},
}
Loading
Loading