Esempio n. 1
0
func (r *fileReader) Read(data []byte) (int, error) {
	if r.reader == nil {
		if r.index == len(r.blockRefs) {
			return 0, io.EOF
		}
		blockRef := r.blockRefs[r.index]
		for r.offset != 0 && r.offset > int64(drive.ByteRangeSize(blockRef.Range)) {
			r.index++
			r.offset -= int64(drive.ByteRangeSize(blockRef.Range))
		}
		var err error
		r.reader, err = pfsutil.GetBlock(r.driveClient,
			r.blockRefs[r.index].Block.Hash, uint64(r.offset), uint64(r.size))
		if err != nil {
			return 0, err
		}
		r.offset = 0
		r.index++
	}
	size, err := r.reader.Read(data)
	if err != nil && err != io.EOF {
		return size, err
	}
	if err == io.EOF {
		r.reader = nil
	}
	r.size -= int64(size)
	if r.size == 0 {
		return size, io.EOF
	}
	return size, nil
}
Esempio n. 2
0
func (r *fileReader) Read(data []byte) (int, error) {
	log.Printf("newFileReader.Read r: %+v\n", r)
	defer log.Printf("newFileReader.Read return")
	if r.reader == nil {
		log.Printf("reader == nil")
		if r.index == len(r.blockRefs) {
			return 0, io.EOF
		}
		blockRef := r.blockRefs[r.index]
		for r.offset != 0 && r.offset > int64(drive.ByteRangeSize(blockRef.Range)) {
			log.Printf("r.index++")
			r.index++
			r.offset -= int64(drive.ByteRangeSize(blockRef.Range))
		}
		var err error
		r.reader, err = r.getBlock(r.blockRefs[r.index].Block.Hash, uint64(r.offset))
		if err != nil {
			return 0, err
		}
		r.offset = 0
		r.index++
	}
	if int(r.size) < len(data) {
		data = data[:r.size]
	}
	log.Printf("reading")
	size, err := r.reader.Read(data)
	log.Printf("done reading")
	if err != nil && err != io.EOF {
		return size, err
	}
	if err == io.EOF {
		r.reader = nil
	}
	r.size -= int64(size)
	if r.size == 0 {
		return size, io.EOF
	}
	return size, nil
}