object.go 7.93 KB
Newer Older
1 2 3 4 5
package coreapi

import (
	"bytes"
	"context"
6 7 8 9 10
	"encoding/base64"
	"encoding/json"
	"encoding/xml"
	"errors"
	"fmt"
11 12 13 14
	"io"
	"io/ioutil"

	coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
15
	caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
16
	"github.com/ipfs/go-ipfs/dagutils"
17 18
	"github.com/ipfs/go-ipfs/pin"

19
	cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
Hector Sanjuan's avatar
Hector Sanjuan committed
20
	ipld "gx/ipfs/QmR7TcHkR9nxkUorfi8XMTAMLUK7GiP64TWWBzY3aacc1o/go-ipld-format"
Steven Allen's avatar
Steven Allen committed
21 22
	dag "gx/ipfs/QmXyuFW7at4r9dxRAbrPU9JpHW5aqngAFyxvyhvYjzxRKS/go-merkledag"
	ft "gx/ipfs/Qmcba8ak38WXFWuaLak5pfJPUDFxcSWbkycBNQfmarpuTv/go-unixfs"
23 24
)

Łukasz Magiera's avatar
Łukasz Magiera committed
25 26
const inputLimit = 2 << 20

27
type ObjectAPI CoreAPI
28

Łukasz Magiera's avatar
Łukasz Magiera committed
29 30 31 32 33 34 35 36 37 38
type Link struct {
	Name, Hash string
	Size       uint64
}

type Node struct {
	Links []Link
	Data  string
}

39
func (api *ObjectAPI) New(ctx context.Context, opts ...caopts.ObjectNewOption) (ipld.Node, error) {
Łukasz Magiera's avatar
Łukasz Magiera committed
40 41 42 43
	options, err := caopts.ObjectNewOptions(opts...)
	if err != nil {
		return nil, err
	}
44

45
	var n ipld.Node
Łukasz Magiera's avatar
Łukasz Magiera committed
46 47 48 49 50 51 52
	switch options.Type {
	case "empty":
		n = new(dag.ProtoNode)
	case "unixfs-dir":
		n = ft.EmptyDirNode()
	}

53
	err = api.dag.Add(ctx, n)
54 55 56
	if err != nil {
		return nil, err
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
57
	return n, nil
58 59
}

60
func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.ObjectPutOption) (coreiface.ResolvedPath, error) {
61 62 63 64 65
	options, err := caopts.ObjectPutOptions(opts...)
	if err != nil {
		return nil, err
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	data, err := ioutil.ReadAll(io.LimitReader(src, inputLimit+10))
	if err != nil {
		return nil, err
	}

	var dagnode *dag.ProtoNode
	switch options.InputEnc {
	case "json":
		node := new(Node)
		err = json.Unmarshal(data, node)
		if err != nil {
			return nil, err
		}

		// check that we have data in the Node to add
		// otherwise we will add the empty object without raising an error
		if nodeEmpty(node) {
			return nil, errors.New("no data or links in this node")
		}

		dagnode, err = deserializeNode(node, options.DataType)
		if err != nil {
			return nil, err
		}

	case "protobuf":
		dagnode, err = dag.DecodeProtobuf(data)

	case "xml":
		node := new(Node)
		err = xml.Unmarshal(data, node)
		if err != nil {
			return nil, err
		}

		// check that we have data in the Node to add
		// otherwise we will add the empty object without raising an error
		if nodeEmpty(node) {
			return nil, errors.New("no data or links in this node")
		}

		dagnode, err = deserializeNode(node, options.DataType)
		if err != nil {
			return nil, err
		}

	default:
		return nil, errors.New("unknown object encoding")
	}

	if err != nil {
		return nil, err
	}

120 121 122 123
	if options.Pin {
		defer api.node.Blockstore.PinLock().Unlock()
	}

124
	err = api.dag.Add(ctx, dagnode)
Łukasz Magiera's avatar
Łukasz Magiera committed
125 126 127 128
	if err != nil {
		return nil, err
	}

129 130 131 132 133 134 135 136
	if options.Pin {
		api.node.Pinning.PinWithMode(dagnode.Cid(), pin.Recursive)
		err = api.node.Pinning.Flush()
		if err != nil {
			return nil, err
		}
	}

137
	return coreiface.IpfsPath(dagnode.Cid()), nil
138 139
}

140
func (api *ObjectAPI) Get(ctx context.Context, path coreiface.Path) (ipld.Node, error) {
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	return api.core().ResolveNode(ctx, path)
}

func (api *ObjectAPI) Data(ctx context.Context, path coreiface.Path) (io.Reader, error) {
	nd, err := api.core().ResolveNode(ctx, path)
	if err != nil {
		return nil, err
	}

	pbnd, ok := nd.(*dag.ProtoNode)
	if !ok {
		return nil, dag.ErrNotProtobuf
	}

	return bytes.NewReader(pbnd.Data()), nil
}

158
func (api *ObjectAPI) Links(ctx context.Context, path coreiface.Path) ([]*ipld.Link, error) {
159 160 161 162 163 164
	nd, err := api.core().ResolveNode(ctx, path)
	if err != nil {
		return nil, err
	}

	links := nd.Links()
165
	out := make([]*ipld.Link, len(links))
166
	for n, l := range links {
167
		out[n] = (*ipld.Link)(l)
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	}

	return out, nil
}

func (api *ObjectAPI) Stat(ctx context.Context, path coreiface.Path) (*coreiface.ObjectStat, error) {
	nd, err := api.core().ResolveNode(ctx, path)
	if err != nil {
		return nil, err
	}

	stat, err := nd.Stat()
	if err != nil {
		return nil, err
	}

	out := &coreiface.ObjectStat{
		Cid:            nd.Cid(),
		NumLinks:       stat.NumLinks,
		BlockSize:      stat.BlockSize,
		LinksSize:      stat.LinksSize,
		DataSize:       stat.DataSize,
		CumulativeSize: stat.CumulativeSize,
	}

	return out, nil
}

196
func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name string, child coreiface.Path, opts ...caopts.ObjectAddLinkOption) (coreiface.ResolvedPath, error) {
Łukasz Magiera's avatar
Łukasz Magiera committed
197 198 199 200 201 202
	options, err := caopts.ObjectAddLinkOptions(opts...)
	if err != nil {
		return nil, err
	}

	baseNd, err := api.core().ResolveNode(ctx, base)
203 204 205 206 207 208 209 210 211
	if err != nil {
		return nil, err
	}

	childNd, err := api.core().ResolveNode(ctx, child)
	if err != nil {
		return nil, err
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
212
	basePb, ok := baseNd.(*dag.ProtoNode)
213 214 215 216 217
	if !ok {
		return nil, dag.ErrNotProtobuf
	}

	var createfunc func() *dag.ProtoNode
Łukasz Magiera's avatar
Łukasz Magiera committed
218
	if options.Create {
219 220 221
		createfunc = ft.EmptyDirNode
	}

222
	e := dagutils.NewDagEditor(basePb, api.dag)
223 224 225 226 227 228

	err = e.InsertNodeAtPath(ctx, name, childNd, createfunc)
	if err != nil {
		return nil, err
	}

229
	nnode, err := e.Finalize(ctx, api.dag)
230 231 232 233
	if err != nil {
		return nil, err
	}

234
	return coreiface.IpfsPath(nnode.Cid()), nil
235 236
}

237
func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link string) (coreiface.ResolvedPath, error) {
Łukasz Magiera's avatar
Łukasz Magiera committed
238
	baseNd, err := api.core().ResolveNode(ctx, base)
239 240 241 242
	if err != nil {
		return nil, err
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
243
	basePb, ok := baseNd.(*dag.ProtoNode)
244 245 246 247
	if !ok {
		return nil, dag.ErrNotProtobuf
	}

248
	e := dagutils.NewDagEditor(basePb, api.dag)
249 250 251 252 253 254

	err = e.RmLink(ctx, link)
	if err != nil {
		return nil, err
	}

255
	nnode, err := e.Finalize(ctx, api.dag)
256 257 258 259
	if err != nil {
		return nil, err
	}

260
	return coreiface.IpfsPath(nnode.Cid()), nil
261 262
}

263
func (api *ObjectAPI) AppendData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.ResolvedPath, error) {
264 265 266
	return api.patchData(ctx, path, r, true)
}

267
func (api *ObjectAPI) SetData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.ResolvedPath, error) {
268 269 270
	return api.patchData(ctx, path, r, false)
}

271
func (api *ObjectAPI) patchData(ctx context.Context, path coreiface.Path, r io.Reader, appendData bool) (coreiface.ResolvedPath, error) {
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
	nd, err := api.core().ResolveNode(ctx, path)
	if err != nil {
		return nil, err
	}

	pbnd, ok := nd.(*dag.ProtoNode)
	if !ok {
		return nil, dag.ErrNotProtobuf
	}

	data, err := ioutil.ReadAll(r)
	if err != nil {
		return nil, err
	}

	if appendData {
		data = append(pbnd.Data(), data...)
	}
	pbnd.SetData(data)

292
	err = api.dag.Add(ctx, pbnd)
293 294 295 296
	if err != nil {
		return nil, err
	}

297
	return coreiface.IpfsPath(pbnd.Cid()), nil
298 299
}

300 301 302 303 304 305 306 307 308 309 310
func (api *ObjectAPI) Diff(ctx context.Context, before coreiface.Path, after coreiface.Path) ([]coreiface.ObjectChange, error) {
	beforeNd, err := api.core().ResolveNode(ctx, before)
	if err != nil {
		return nil, err
	}

	afterNd, err := api.core().ResolveNode(ctx, after)
	if err != nil {
		return nil, err
	}

311
	changes, err := dagutils.Diff(ctx, api.dag, beforeNd, afterNd)
312 313 314 315 316 317 318
	if err != nil {
		return nil, err
	}

	out := make([]coreiface.ObjectChange, len(changes))
	for i, change := range changes {
		out[i] = coreiface.ObjectChange{
319 320 321 322
			Type: change.Type,
			Path: change.Path,
		}

323
		if change.Before.Defined() {
324 325 326
			out[i].Before = coreiface.IpfsPath(change.Before)
		}

327
		if change.After.Defined() {
328
			out[i].After = coreiface.IpfsPath(change.After)
329 330 331 332 333 334
		}
	}

	return out, nil
}

335
func (api *ObjectAPI) core() coreiface.CoreAPI {
336
	return (*CoreAPI)(api)
337
}
Łukasz Magiera's avatar
Łukasz Magiera committed
338 339 340 341 342 343 344

func deserializeNode(nd *Node, dataFieldEncoding string) (*dag.ProtoNode, error) {
	dagnode := new(dag.ProtoNode)
	switch dataFieldEncoding {
	case "text":
		dagnode.SetData([]byte(nd.Data))
	case "base64":
Steven Allen's avatar
Steven Allen committed
345 346 347 348
		data, err := base64.StdEncoding.DecodeString(nd.Data)
		if err != nil {
			return nil, err
		}
Łukasz Magiera's avatar
Łukasz Magiera committed
349 350
		dagnode.SetData(data)
	default:
Łukasz Magiera's avatar
Łukasz Magiera committed
351
		return nil, fmt.Errorf("unkown data field encoding")
Łukasz Magiera's avatar
Łukasz Magiera committed
352 353
	}

Steven Allen's avatar
Steven Allen committed
354
	links := make([]*ipld.Link, len(nd.Links))
Łukasz Magiera's avatar
Łukasz Magiera committed
355 356 357 358 359
	for i, link := range nd.Links {
		c, err := cid.Decode(link.Hash)
		if err != nil {
			return nil, err
		}
Steven Allen's avatar
Steven Allen committed
360
		links[i] = &ipld.Link{
Łukasz Magiera's avatar
Łukasz Magiera committed
361 362 363 364 365
			Name: link.Name,
			Size: link.Size,
			Cid:  c,
		}
	}
Steven Allen's avatar
Steven Allen committed
366
	dagnode.SetLinks(links)
Łukasz Magiera's avatar
Łukasz Magiera committed
367 368 369 370 371 372 373

	return dagnode, nil
}

func nodeEmpty(node *Node) bool {
	return node.Data == "" && len(node.Links) == 0
}