/*
 * subroutine for reading records from Ultrix traces
 * $Header: /tmp_mnt/n/fs/grad1/pc/src/iotrace/rtlib/RCS/readrec.c,v 1.2 94/01/06 14:07:16 pc Exp Locker: pc $
 */

#include <stdio.h>
#include "iotrace.h"
#include "record.h"

FILE *tracefile, *fopen();

int
initRead(name, argc, argv)
char *name;
int argc;
char *argv[];
{
	/* since this is a hook to the Sprite trace generator, here I skip
	all passed -* arguments, i.e. starting time, etc */
	while (argc>0 && argv[0][0] == '-') {
		argc-=2;
		argv+=2;
	}

	/* this version does not deal with multiple input files yet */
	tracefile = fopen(argv[0], "r");
	if (tracefile == NULL) {
		fprintf(stderr, "Can't open trace file %s\n", argv[0]);
		exit(1);
	}

	return(0);

}

int time_counter = 0;

int
getNextRecord(recPtr)
struct f_rec *recPtr;
{
	struct tracerecord record;

	if (recPtr == NULL) {
		fprintf(stderr, "buf == NULL\n");
		exit(1);
	}

	while (fread(&record, sizeof(struct tracerecord), 1, tracefile) !=0) {

		/* set common fields for all types of records */
		recPtr->hostID = 0;
		recPtr->userid = record.pid;
		recPtr->fid.id.host = 0;
		recPtr->fid.id.domain = record.i_dev;
		recPtr->fid.id.inode = record.i_num;

		/* the only use for time field would be to identify a file
		action */
		recPtr->time[0] = time_counter++;
		recPtr->time[1] = record.time;

		/* clear other fields */
		recPtr->start = recPtr->size = recPtr->flags = 0;
	
		switch (record.op) {
	case MYTRACE_TIMESTAMP:
		/* for some reason this didn't appear in previous traces ---
		why??? */
		printf("begin of new records: %d.%d\n",
                        record.count, record.offset);
		continue;
		
	case MYTRACE_OPEN:
	case MYTRACE_CREATE:
		/* create and open are not distinguished here */
		recPtr->type = REPL_OPEN;

		/* I guess if you are writing and appending, then the starting
		offset may not be 0; but for read and write I have the offset
		anyway */
		recPtr->start = recPtr->size = 0;

		recPtr->flags = record.offset;	/* count is the mode */
		
		return(1);	/* got one record */

	case MYTRACE_FDOPEN:
	case MYTRACE_FDCREATE:
		continue;

	case MYTRACE_READ:
		recPtr->type = REPL_READ | FROM_READ;
		recPtr->start = record.offset;
		recPtr->size = record.count;
		return(1);	/* get one record */

	case MYTRACE_WRITE:
		recPtr->type = REPL_WRITE | FROM_READ;
		recPtr->start = record.offset;
                recPtr->size = record.count;
                return(1);      /* get one record */

	case MYTRACE_LSEEK:
		recPtr->type = REPL_LSEEK;
		recPtr->start = record.offset;	/* start is sbase */
                recPtr->size = record.count;
                return(1);      /* get one record */

	case MYTRACE_CLOSE:
		recPtr->type = REPL_CLOSE;
		return(1);

	case MYTRACE_FTRUNCATE:
		recPtr->type = REPL_TRUNC;
		recPtr->start = record.offset;	/* truncate from here to end */
		return(1);

	case MYTRACE_TRUNCATE:
		recPtr->type = REPL_TRUNC;
		recPtr->start = record.offset;	/* truncate from here to end */
		return(1);

	case MYTRACE_UNLINK:
		recPtr->type = REPL_DELDESC;
		return(1);

	case MYTRACE_STAT:
	case MYTRACE_FCNTL:
		continue;

	case MYTRACE_FORK:
		recPtr->type = REPL_FORK;
		return(1);
	case MYTRACE_EXIT:
		recPtr->type = REPL_EXIT;
		return(1);

	default:
	/*
		fprintf(stderr, "Unknown record type: %d\n", record.op);
	*/
		continue;
		}

	}

	return(0);	/* end of trace file */
}
