Example #1
0
func (h *HANDLE) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
	var chunkIdx, numChunks int64
	var offInChunk, totalRead int

	f := h.f
	chunkSz := h.chunkSz

	// TODO: Use the masks!!
	chunkIdx = req.Offset / int64(chunkSz)
	offInChunk = int(req.Offset - chunkIdx*int64(chunkSz))
	numChunks = int64((req.Size + chunkSz - 1) / chunkSz)

	if chunkIdx >= f.Entry.NumChunks || req.Size == 0 {
		resp.Data = []byte{}
		return nil
	}

	if chunkIdx+numChunks > f.Entry.NumChunks {
		numChunks = f.Entry.NumChunks - chunkIdx
	}

	resp.Data = make([]byte, int(numChunks)*chunkSz)
	remain := req.Size
	totalRead = 0
	for i := int64(0); i < numChunks; i++ {
		n, err := readChunk(h, chunkIdx+i, resp.Data[totalRead:], offInChunk, remain)
		if err != nil && err != io.EOF {
			log.WithFields(log.Fields{
				"File":     f.cacheName,
				"chunkIdx": chunkIdx,
				"chunkSz":  chunkSz,
				"Error":    err,
			}).Error("Read: readChunk failed")
			return err
		}

		offInChunk = 0

		remain -= n
		totalRead += n

		if err == io.EOF {
			break
		}

	}

	resp.Data = resp.Data[:totalRead]
	log.WithFields(log.Fields{
		"Chunk Size": chunkSz,
		"File":       f.cacheName,
		"Offset":     req.Offset,
		"Size":       req.Size,
		"Read":       totalRead,
	}).Debug("Read")

	return nil
}
Example #2
0
// HandleRead handles a read request assuming that data is the entire file content.
// It adjusts the amount returned in resp according to req.Offset and req.Size.
func HandleRead(req *fuse.ReadRequest, resp *fuse.ReadResponse, data []byte) {
	if req.Offset >= int64(len(data)) {
		data = nil
	} else {
		data = data[req.Offset:]
	}
	if len(data) > req.Size {
		data = data[:req.Size]
	}
	n := copy(resp.Data[:req.Size], data)
	resp.Data = resp.Data[:n]
}