dagreader.go 5.43 KB
Newer Older
1 2 3 4 5 6
package io

import (
	"bytes"
	"errors"
	"io"
Jeromy's avatar
Jeromy committed
7
	"os"
8

9
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
Jeromy's avatar
Jeromy committed
10

11 12 13
	proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
	mdag "github.com/jbenet/go-ipfs/merkledag"
	ft "github.com/jbenet/go-ipfs/unixfs"
14
	ftpb "github.com/jbenet/go-ipfs/unixfs/pb"
15 16 17 18 19 20
)

var ErrIsDir = errors.New("this dag node is a directory")

// DagReader provides a way to easily read the data contained in a dag.
type DagReader struct {
Jeromy's avatar
Jeromy committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
	serv mdag.DAGService

	// the node being read
	node *mdag.Node

	// cached protobuf structure from node.Data
	pbdata *ftpb.Data

	// the current data buffer to be read from
	// will either be a bytes.Reader or a child DagReader
	buf ReadSeekCloser

	// NodeGetters for each of 'nodes' child links
	promises []mdag.NodeGetter

	// the index of the child link currently being read from
Jeromy's avatar
Jeromy committed
37
	linkPosition int
Jeromy's avatar
Jeromy committed
38 39 40

	// current offset for the read head within the 'file'
	offset int64
Jeromy's avatar
Jeromy committed
41 42 43 44

	// Our context
	ctx context.Context

Jeromy's avatar
Jeromy committed
45
	// context cancel for children
Jeromy's avatar
Jeromy committed
46 47 48 49 50 51 52
	cancel func()
}

type ReadSeekCloser interface {
	io.Reader
	io.Seeker
	io.Closer
53 54 55 56
}

// NewDagReader creates a new reader object that reads the data represented by the given
// node, using the passed in DAGService for data retreival
Jeromy's avatar
Jeromy committed
57
func NewDagReader(ctx context.Context, n *mdag.Node, serv mdag.DAGService) (ReadSeekCloser, error) {
58
	pb := new(ftpb.Data)
59 60 61 62 63 64
	err := proto.Unmarshal(n.Data, pb)
	if err != nil {
		return nil, err
	}

	switch pb.GetType() {
65
	case ftpb.Data_Directory:
66 67
		// Dont allow reading directories
		return nil, ErrIsDir
68
	case ftpb.Data_File:
Jeromy's avatar
Jeromy committed
69
		fctx, cancel := context.WithCancel(ctx)
Jeromy's avatar
Jeromy committed
70
		promises := serv.GetDAG(fctx, n)
Jeromy's avatar
Jeromy committed
71
		return &DagReader{
Jeromy's avatar
Jeromy committed
72 73 74 75 76 77 78
			node:     n,
			serv:     serv,
			buf:      NewRSNCFromBytes(pb.GetData()),
			promises: promises,
			ctx:      fctx,
			cancel:   cancel,
			pbdata:   pb,
Jeromy's avatar
Jeromy committed
79
		}, nil
80
	case ftpb.Data_Raw:
81
		// Raw block will just be a single level, return a byte buffer
Jeromy's avatar
Jeromy committed
82
		return NewRSNCFromBytes(pb.GetData()), nil
83 84 85 86 87
	default:
		return nil, ft.ErrUnrecognizedType
	}
}

Jeromy's avatar
Jeromy committed
88
// precalcNextBuf follows the next link in line and loads it from the DAGService,
89 90
// setting the next buffer to read from
func (dr *DagReader) precalcNextBuf() error {
Jeromy's avatar
Jeromy committed
91 92 93
	dr.buf.Close() // Just to make sure
	if dr.linkPosition >= len(dr.promises) {
		return io.EOF
Jeromy's avatar
Jeromy committed
94
	}
Jeromy's avatar
Jeromy committed
95 96 97
	nxt, err := dr.promises[dr.linkPosition].Get()
	if err != nil {
		return err
98
	}
Jeromy's avatar
Jeromy committed
99
	dr.linkPosition++
Jeromy's avatar
Jeromy committed
100

101
	pb := new(ftpb.Data)
Jeromy's avatar
Jeromy committed
102
	err = proto.Unmarshal(nxt.Data, pb)
103 104 105 106 107
	if err != nil {
		return err
	}

	switch pb.GetType() {
108
	case ftpb.Data_Directory:
Jeromy's avatar
Jeromy committed
109
		// A directory should not exist within a file
110
		return ft.ErrInvalidDirLocation
111
	case ftpb.Data_File:
Jeromy's avatar
Jeromy committed
112
		subr, err := NewDagReader(dr.ctx, nxt, dr.serv)
Jeromy's avatar
Jeromy committed
113 114 115 116 117
		if err != nil {
			return err
		}
		dr.buf = subr
		return nil
118
	case ftpb.Data_Raw:
Jeromy's avatar
Jeromy committed
119
		dr.buf = NewRSNCFromBytes(pb.GetData())
120 121 122 123 124 125
		return nil
	default:
		return ft.ErrUnrecognizedType
	}
}

Jeromy's avatar
Jeromy committed
126
// Read reads data from the DAG structured file
127 128 129 130 131 132 133
func (dr *DagReader) Read(b []byte) (int, error) {
	// If no cached buffer, load one
	total := 0
	for {
		// Attempt to fill bytes from cached buffer
		n, err := dr.buf.Read(b[total:])
		total += n
Jeromy's avatar
Jeromy committed
134
		dr.offset += int64(n)
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
		if err != nil {
			// EOF is expected
			if err != io.EOF {
				return total, err
			}
		}

		// If weve read enough bytes, return
		if total == len(b) {
			return total, nil
		}

		// Otherwise, load up the next block
		err = dr.precalcNextBuf()
		if err != nil {
			return total, err
		}
	}
}

Jeromy's avatar
Jeromy committed
155
func (dr *DagReader) Close() error {
Jeromy's avatar
Jeromy committed
156
	dr.cancel()
Jeromy's avatar
Jeromy committed
157 158 159
	return nil
}

Jeromy's avatar
Jeromy committed
160 161
// Seek implements io.Seeker, and will seek to a given offset in the file
// interface matches standard unix seek
162 163 164
func (dr *DagReader) Seek(offset int64, whence int) (int64, error) {
	switch whence {
	case os.SEEK_SET:
Jeromy's avatar
Jeromy committed
165 166 167 168
		if offset < 0 {
			return -1, errors.New("Invalid offset")
		}

Jeromy's avatar
Jeromy committed
169
		// Grab cached protobuf object (solely to make code look cleaner)
Jeromy's avatar
Jeromy committed
170
		pb := dr.pbdata
Jeromy's avatar
Jeromy committed
171 172

		// left represents the number of bytes remaining to seek to (from beginning)
Jeromy's avatar
Jeromy committed
173
		left := offset
174
		if int64(len(pb.Data)) >= offset {
Jeromy's avatar
Jeromy committed
175
			// Close current buf to close potential child dagreader
Jeromy's avatar
Jeromy committed
176
			dr.buf.Close()
Jeromy's avatar
Jeromy committed
177
			dr.buf = NewRSNCFromBytes(pb.GetData()[offset:])
Jeromy's avatar
Jeromy committed
178 179

			// start reading links from the beginning
Jeromy's avatar
Jeromy committed
180 181 182 183
			dr.linkPosition = 0
			dr.offset = offset
			return offset, nil
		} else {
Jeromy's avatar
Jeromy committed
184
			// skip past root block data
Jeromy's avatar
Jeromy committed
185 186 187
			left -= int64(len(pb.Data))
		}

Jeromy's avatar
Jeromy committed
188
		// iterate through links and find where we need to be
Jeromy's avatar
Jeromy committed
189
		for i := 0; i < len(pb.Blocksizes); i++ {
Jeromy's avatar
Jeromy committed
190
			if pb.Blocksizes[i] > uint64(left) {
Jeromy's avatar
Jeromy committed
191
				dr.linkPosition = i
192
				break
Jeromy's avatar
Jeromy committed
193 194
			} else {
				left -= int64(pb.Blocksizes[i])
195 196
			}
		}
Jeromy's avatar
Jeromy committed
197

Jeromy's avatar
Jeromy committed
198
		// start sub-block request
Jeromy's avatar
Jeromy committed
199
		err := dr.precalcNextBuf()
200 201 202
		if err != nil {
			return 0, err
		}
Jeromy's avatar
Jeromy committed
203

Jeromy's avatar
Jeromy committed
204
		// set proper offset within child readseeker
Jeromy's avatar
Jeromy committed
205
		n, err := dr.buf.Seek(left, os.SEEK_SET)
Jeromy's avatar
Jeromy committed
206 207 208
		if err != nil {
			return -1, err
		}
Jeromy's avatar
Jeromy committed
209 210

		// sanity
Jeromy's avatar
Jeromy committed
211 212 213 214 215 216
		left -= n
		if left != 0 {
			return -1, errors.New("failed to seek properly")
		}
		dr.offset = offset
		return offset, nil
217
	case os.SEEK_CUR:
Jeromy's avatar
Jeromy committed
218
		// TODO: be smarter here
Jeromy's avatar
Jeromy committed
219 220
		noffset := dr.offset + offset
		return dr.Seek(noffset, os.SEEK_SET)
221
	case os.SEEK_END:
Jeromy's avatar
Jeromy committed
222
		noffset := int64(dr.pbdata.GetFilesize()) - offset
Jeromy's avatar
Jeromy committed
223
		return dr.Seek(noffset, os.SEEK_SET)
224 225 226 227 228
	default:
		return 0, errors.New("invalid whence")
	}
	return 0, nil
}
Jeromy's avatar
Jeromy committed
229

Jeromy's avatar
Jeromy committed
230
// readSeekNopCloser wraps a bytes.Reader to implement ReadSeekCloser
Jeromy's avatar
Jeromy committed
231 232 233 234 235 236 237 238 239
type readSeekNopCloser struct {
	*bytes.Reader
}

func NewRSNCFromBytes(b []byte) ReadSeekCloser {
	return &readSeekNopCloser{bytes.NewReader(b)}
}

func (r *readSeekNopCloser) Close() error { return nil }