dagreader.go 2.54 KB
Newer Older
1 2 3 4 5 6 7
package merkledag

import (
	"bytes"
	"errors"
	"io"

8
	proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
9
	ft "github.com/jbenet/go-ipfs/importer/format"
10
	u "github.com/jbenet/go-ipfs/util"
11 12
)

Siraj Ravel's avatar
Siraj Ravel committed
13
var ErrIsDir = errors.New("this dag node is a directory")
14 15 16

// DagReader provides a way to easily read the data contained in a dag.
type DagReader struct {
17
	serv     *DAGService
18 19 20 21 22
	node     *Node
	position int
	buf      *bytes.Buffer
}

23
func NewDagReader(n *Node, serv *DAGService) (io.Reader, error) {
24
	pb := new(ft.PBData)
25 26 27 28
	err := proto.Unmarshal(n.Data, pb)
	if err != nil {
		return nil, err
	}
29

30
	switch pb.GetType() {
31
	case ft.PBData_Directory:
32
		return nil, ErrIsDir
33
	case ft.PBData_File:
34
		return &DagReader{
Jeromy's avatar
Jeromy committed
35 36 37
			node: n,
			serv: serv,
			buf:  bytes.NewBuffer(pb.GetData()),
38
		}, nil
39
	case ft.PBData_Raw:
40 41 42 43 44 45 46 47 48 49 50 51 52
		return bytes.NewBuffer(pb.GetData()), nil
	default:
		panic("Unrecognized node type!")
	}
}

func (dr *DagReader) precalcNextBuf() error {
	if dr.position >= len(dr.node.Links) {
		return io.EOF
	}
	nxtLink := dr.node.Links[dr.position]
	nxt := nxtLink.Node
	if nxt == nil {
53 54 55 56 57
		nxtNode, err := dr.serv.Get(u.Key(nxtLink.Hash))
		if err != nil {
			return err
		}
		nxt = nxtNode
58
	}
59
	pb := new(ft.PBData)
60 61 62 63 64 65 66
	err := proto.Unmarshal(nxt.Data, pb)
	if err != nil {
		return err
	}
	dr.position++

	switch pb.GetType() {
67
	case ft.PBData_Directory:
68
		panic("Why is there a directory under a file?")
69
	case ft.PBData_File:
Jeromy's avatar
Jeromy committed
70 71
		//TODO: this *should* work, needs testing first
		//return NewDagReader(nxt, dr.serv)
72
		panic("Not yet handling different layers of indirection!")
73
	case ft.PBData_Raw:
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
		dr.buf = bytes.NewBuffer(pb.GetData())
		return nil
	default:
		panic("Unrecognized node type!")
	}
}

func (dr *DagReader) Read(b []byte) (int, error) {
	if dr.buf == nil {
		err := dr.precalcNextBuf()
		if err != nil {
			return 0, err
		}
	}
	total := 0
	for {
		n, err := dr.buf.Read(b[total:])
		total += n
		if err != nil {
			if err != io.EOF {
				return total, err
			}
		}
		if total == len(b) {
			return total, nil
		}
		err = dr.precalcNextBuf()
		if err != nil {
			return total, err
		}
	}
}
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

/*
func (dr *DagReader) Seek(offset int64, whence int) (int64, error) {
	switch whence {
	case os.SEEK_SET:
		for i := 0; i < len(dr.node.Links); i++ {
			nsize := dr.node.Links[i].Size - 8
			if offset > nsize {
				offset -= nsize
			} else {
				break
			}
		}
		dr.position = i
		err := dr.precalcNextBuf()
		if err != nil {
			return 0, err
		}
	case os.SEEK_CUR:
	case os.SEEK_END:
	default:
		return 0, errors.New("invalid whence")
	}
	return 0, nil
}
*/