BurstFS_Client/src/cruise-sysio.c
int burstfs_match_received_ack(read_req_t *read_req, int count,\
		read_req_t *match_req) {

	read_req_t match_start = *match_req;
	read_req_t match_end = *match_req;
	read_req_t tmp_req_start, tmp_req_end;
	match_end.offset += match_end.length - 1;

	int start_pos = burstfs_locate_req(read_req, count, &match_start);
	int end_pos = burstfs_locate_req(read_req, count, &match_end);

	if (start_pos == -1)
		return -1;

	/*s: start of match_req, e: end of match_req*/
	if (start_pos == 0) {
		if (compare_read_req(&match_start, &read_req[0]) < 0) {
			/*
			 *						************	***********			*************
			 * 					s
			 * */
			return -1;
		}
	}

	tmp_req_start = read_req[start_pos];
	tmp_req_end = read_req[start_pos];
	tmp_req_end.offset += tmp_req_end.length - 1;

	/*				tmp_s		tmp_e
	 *				*****************			*************
	 * 					   s  e
	 * */
	if (compare_read_req(&match_start, &tmp_req_start) >= 0 && \
			compare_read_req(&match_end, &tmp_req_end) <= 0) {


		int copy_offset = match_start.offset - tmp_req_start.offset;
		memcpy(tmp_req_start.buf + copy_offset, match_req->buf,\
				match_req->length);
		return 0;
	}

	/*	tmp_s		tmp_e	req_s	req_endreq_s	req_endreq_s			req_e
	 *	*************************************************************************
	 * 			s														e
	 * */


	read_req_t tmp_start_req_start, tmp_start_req_end, \
		tmp_end_req_start, tmp_end_req_end;

	tmp_start_req_start = read_req[start_pos];
	tmp_start_req_end = read_req[start_pos];
	tmp_start_req_end.offset += tmp_start_req_end.length - 1;

	tmp_end_req_start = read_req[end_pos];
	tmp_end_req_end = read_req[end_pos];
	tmp_end_req_end.offset += tmp_end_req_end.length - 1;


	if (compare_read_req(&match_start, &tmp_start_req_start) >= 0 && \
			compare_read_req(&match_end, &tmp_end_req_end) <= 0) {

		/*read requests are noncontiguous, so we returned more
		 * data, error*/
		int i;
		for (i = start_pos + 1; i <= end_pos; i++) {
			if (read_req[i - 1].offset + read_req[i - 1].length != read_req[i].offset)
				return -1;
		}

		/*read requests are contiguous, so we fill all req buffers in the
		 * middle*/
		long copy_offset = match_start.offset - tmp_start_req_start.offset;
		long copy_length = tmp_start_req_end.offset - match_start.offset + 1;
		memcpy(tmp_start_req_start.buf + copy_offset, match_req->buf,\
				copy_length);

		long cursor = copy_length;
		for (i = start_pos + 1; i < end_pos; i++) {
			memcpy(read_req[i].buf, match_req->buf + cursor,\
					read_req[i].length);
			cursor += read_req[i].length;
		}

		copy_offset = 0;
		copy_length = match_end.offset - tmp_end_req_start.offset + 1;
		memcpy(tmp_end_req_start.buf + copy_offset, match_req->buf,\
				copy_length);

		return 0;
	}

	return -1;

}
/*
 * match the received read_requests with the
 * client's read requests
 * @param read_req: a list of read requests
 * @param count: number of read requests
 * @param match_req: received read request to match
 * @return error code
 *
 * */
match the received read_requests with the
client's read requests
@param read_req: a list of read requests
@param count: number of read requests
@param match_req: received read request to match
@return error code
match the received read_requests with the client ' s read requests @param read_req : a list of read requests @param count : number of read requests @param match_req : received read request to match @return error code
int burstfs_match_received_ack(read_req_t *read_req, int count,\
		read_req_t *match_req) {
	read_req_t match_start = *match_req;
	read_req_t match_end = *match_req;
	read_req_t tmp_req_start, tmp_req_end;
	match_end.offset += match_end.length - 1;
	int start_pos = burstfs_locate_req(read_req, count, &match_start);
	int end_pos = burstfs_locate_req(read_req, count, &match_end);
	if (start_pos == -1)
		return -1;
	if (start_pos == 0) {
		if (compare_read_req(&match_start, &read_req[0]) < 0) {
			return -1;
		}
	}
	tmp_req_start = read_req[start_pos];
	tmp_req_end = read_req[start_pos];
	tmp_req_end.offset += tmp_req_end.length - 1;
	if (compare_read_req(&match_start, &tmp_req_start) >= 0 && \
			compare_read_req(&match_end, &tmp_req_end) <= 0) {
		int copy_offset = match_start.offset - tmp_req_start.offset;
		memcpy(tmp_req_start.buf + copy_offset, match_req->buf,\
				match_req->length);
		return 0;
	}
	read_req_t tmp_start_req_start, tmp_start_req_end, \
		tmp_end_req_start, tmp_end_req_end;
	tmp_start_req_start = read_req[start_pos];
	tmp_start_req_end = read_req[start_pos];
	tmp_start_req_end.offset += tmp_start_req_end.length - 1;
	tmp_end_req_start = read_req[end_pos];
	tmp_end_req_end = read_req[end_pos];
	tmp_end_req_end.offset += tmp_end_req_end.length - 1;
	if (compare_read_req(&match_start, &tmp_start_req_start) >= 0 && \
			compare_read_req(&match_end, &tmp_end_req_end) <= 0) {
		int i;
		for (i = start_pos + 1; i <= end_pos; i++) {
			if (read_req[i - 1].offset + read_req[i - 1].length != read_req[i].offset)
				return -1;
		}
		long copy_offset = match_start.offset - tmp_start_req_start.offset;
		long copy_length = tmp_start_req_end.offset - match_start.offset + 1;
		memcpy(tmp_start_req_start.buf + copy_offset, match_req->buf,\
				copy_length);
		long cursor = copy_length;
		for (i = start_pos + 1; i < end_pos; i++) {
			memcpy(read_req[i].buf, match_req->buf + cursor,\
					read_req[i].length);
			cursor += read_req[i].length;
		}
		copy_offset = 0;
		copy_length = match_end.offset - tmp_end_req_start.offset + 1;
		memcpy(tmp_end_req_start.buf + copy_offset, match_req->buf,\
				copy_length);
		return 0;
	}
	return -1;
}
int burstfs_match_received_ack ( read_req_t * read_req , int count , read_req_t * match_req ) { read_req_t match_start = * match_req ; read_req_t match_end = * match_req ; read_req_t tmp_req_start , tmp_req_end ; match_end . offset += match_end . length - 1 ; int start_pos = burstfs_locate_req ( read_req , count , & match_start ) ; int end_pos = burstfs_locate_req ( read_req , count , & match_end ) ; if ( start_pos == -1 ) return -1 ; if ( start_pos == 0 ) { if ( compare_read_req ( & match_start , & read_req [ 0 ] ) < 0 ) { return -1 ; } } tmp_req_start = read_req [ start_pos ] ; tmp_req_end = read_req [ start_pos ] ; tmp_req_end . offset += tmp_req_end . length - 1 ; if ( compare_read_req ( & match_start , & tmp_req_start ) >= 0 && compare_read_req ( & match_end , & tmp_req_end ) <= 0 ) { int copy_offset = match_start . offset - tmp_req_start . offset ; memcpy ( tmp_req_start . buf + copy_offset , match_req -> buf , match_req -> length ) ; return 0 ; } read_req_t tmp_start_req_start , tmp_start_req_end , tmp_end_req_start , tmp_end_req_end ; tmp_start_req_start = read_req [ start_pos ] ; tmp_start_req_end = read_req [ start_pos ] ; tmp_start_req_end . offset += tmp_start_req_end . length - 1 ; tmp_end_req_start = read_req [ end_pos ] ; tmp_end_req_end = read_req [ end_pos ] ; tmp_end_req_end . offset += tmp_end_req_end . length - 1 ; if ( compare_read_req ( & match_start , & tmp_start_req_start ) >= 0 && compare_read_req ( & match_end , & tmp_end_req_end ) <= 0 ) { int i ; for ( i = start_pos + 1 ; i <= end_pos ; i ++ ) { if ( read_req [ i - 1 ] . offset + read_req [ i - 1 ] . length != read_req [ i ] . offset ) return -1 ; } long copy_offset = match_start . offset - tmp_start_req_start . offset ; long copy_length = tmp_start_req_end . offset - match_start . offset + 1 ; memcpy ( tmp_start_req_start . buf + copy_offset , match_req -> buf , copy_length ) ; long cursor = copy_length ; for ( i = start_pos + 1 ; i < end_pos ; i ++ ) { memcpy ( read_req [ i ] . buf , match_req -> buf + cursor , read_req [ i ] . length ) ; cursor += read_req [ i ] . length ; } copy_offset = 0 ; copy_length = match_end . offset - tmp_end_req_start . offset + 1 ; memcpy ( tmp_end_req_start . buf + copy_offset , match_req -> buf , copy_length ) ; return 0 ; } return -1 ; }
match the received read_requests with the
 client's read requests
 @param read_req: a list of read requests
 @param count: number of read requests
 @param match_req: received read request to match
 @return error code
match the received read_requests with the client ' s read requests @param read_req : a list of read requests @param count : number of read requests @param match_req : received read request to match @return error code
/*s: start of match_req, e: end of match_req*/ /*
			 *						************	***********			*************
			 * 					s
			 * */ /*				tmp_s		tmp_e
	 *				*****************			*************
	 * 					   s  e
	 * */ /*	tmp_s		tmp_e	req_s	req_endreq_s	req_endreq_s			req_e
	 *	*************************************************************************
	 * 			s														e
	 * */ /*read requests are noncontiguous, so we returned more
		 * data, error*/ /*read requests are contiguous, so we fill all req buffers in the
		 * middle*/