object.go 3.63 KB
Newer Older
Henry's avatar
Henry committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
package commands

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"os"

	"github.com/jbenet/go-ipfs/core"
	dag "github.com/jbenet/go-ipfs/merkledag"
)

// ObjectData takes a key string from args and writes out the raw bytes of that node (if there is one)
func ObjectData(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
	dagnode, err := n.Resolver.ResolvePath(args[0])
	if err != nil {
		return fmt.Errorf("objectData error: %v", err)
	}
22
	log.Debugf("objectData: found dagnode %q (# of bytes: %d - # links: %d)", args[0], len(dagnode.Data), len(dagnode.Links))
Henry's avatar
Henry committed
23 24 25 26 27 28 29 30 31 32 33

	_, err = io.Copy(out, bytes.NewReader(dagnode.Data))
	return err
}

// ObjectLinks takes a key string from args and lists the links it points to
func ObjectLinks(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
	dagnode, err := n.Resolver.ResolvePath(args[0])
	if err != nil {
		return fmt.Errorf("objectLinks error: %v", err)
	}
34
	log.Debugf("ObjectLinks: found dagnode %q (# of bytes: %d - # links: %d)", args[0], len(dagnode.Data), len(dagnode.Links))
Henry's avatar
Henry committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

	for _, link := range dagnode.Links {
		_, err = fmt.Fprintf(out, "%s %d %q\n", link.Hash.B58String(), link.Size, link.Name)
		if err != nil {
			break
		}
	}

	return err
}

// ErrUnknownObjectEnc is returned if a invalid encoding is supplied
var ErrUnknownObjectEnc = errors.New("unknown object encoding")

type objectEncoding string

const (
	objectEncodingJSON     objectEncoding = "json"
	objectEncodingProtobuf                = "protobuf"
)

func getObjectEnc(o interface{}) objectEncoding {
	v, ok := o.(string)
	if !ok {
		// chosen as default because it's human readable
		log.Warning("option is not a string - falling back to json")
		return objectEncodingJSON
	}

	return objectEncoding(v)
}

// ObjectGet takes a key string from args and a format option and serializes the dagnode to that format
func ObjectGet(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
	dagnode, err := n.Resolver.ResolvePath(args[0])
	if err != nil {
		return fmt.Errorf("ObjectGet error: %v", err)
	}
73
	log.Debugf("objectGet: found dagnode %q (# of bytes: %d - # links: %d)", args[0], len(dagnode.Data), len(dagnode.Links))
Henry's avatar
Henry committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

	// sadly all encodings dont implement a common interface
	var data []byte
	switch getObjectEnc(opts["encoding"]) {
	case objectEncodingJSON:
		data, err = json.MarshalIndent(dagnode, "", "  ")

	case objectEncodingProtobuf:
		data, err = dagnode.Marshal()

	default:
		return ErrUnknownObjectEnc
	}

	if err != nil {
		return fmt.Errorf("ObjectGet error: %v", err)
	}

	_, err = io.Copy(out, bytes.NewReader(data))
	return err
}

Henry's avatar
Henry committed
96 97 98 99 100
// ErrObjectTooLarge is returned when too much data was read from stdin. current limit 512k
var ErrObjectTooLarge = errors.New("input object was too large. limit is 512kbytes")

const inputLimit = 512 * 1024

Henry's avatar
Henry committed
101 102 103 104 105 106 107 108
// ObjectPut takes a format option, serilizes bytes from stdin and updates the dag with that data
func ObjectPut(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
	var (
		dagnode *dag.Node
		data    []byte
		err     error
	)

Henry's avatar
Henry committed
109
	data, err = ioutil.ReadAll(io.LimitReader(os.Stdin, inputLimit+10))
Henry's avatar
Henry committed
110 111 112 113
	if err != nil {
		return fmt.Errorf("ObjectPut error: %v", err)
	}

Henry's avatar
Henry committed
114 115 116 117
	if len(data) >= inputLimit {
		return ErrObjectTooLarge
	}

Henry's avatar
Henry committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	switch getObjectEnc(opts["encoding"]) {
	case objectEncodingJSON:
		dagnode = new(dag.Node)
		err = json.Unmarshal(data, dagnode)

	case objectEncodingProtobuf:
		dagnode, err = dag.Decoded(data)

	default:
		return ErrUnknownObjectEnc
	}

	if err != nil {
		return fmt.Errorf("ObjectPut error: %v", err)
	}

	return addNode(n, dagnode, "stdin", out)
}