/********************************************************************
 ** NETWORK FILESYSTEM - DOS HANDLER
 **
 ** (c) Spak, Darrell Tam, c9107253@ee.newcastle.edu.au (1994)
 ** phone (Australia) 049-829-710
 **                    49-829-710
 ** (c) SST 1994
 **
 ** SEEK
 **
 ** TABS to 4
 ********************************************************************/

#include "/snd/everything.h"
#include "deadheads.h"
#include "root_protos.h"
#include "globs_protos.h"
#include "xtra_protos.h"

#define SEEK_PKTMSG_SIZE (sizeof(struct SeekPktMsg))

/** another variation of the "doofus" for seeking */
struct SeekPktMsg {
	struct DoofusPktMsg	dpm;

	struct	NFSSeek				seekreq;	/* request packet struct */
	struct	NFSStdReturn		seekret;	/* what the server sends back */
};


#define SEEK_PKTMSG_SIZE (sizeof(struct SeekPktMsg))

/********************************************************************/
static long handle_seek_net(struct SeekPktMsg *spm)
/********************************************************************/
{
struct DosPacket *dosmsg = spm->dpm.dp_original;
	dosmsg->dp_Res1 = spm->seekret.remote_res1;
	dosmsg->dp_Res2 = spm->seekret.remote_res2;
	if(spm->dpm.net.pm.smsg.result != PAR_OK)
		{ dosmsg->dp_Res1 = 0; dosmsg->dp_Res2 = ERROR_NO_DISK; }

	return(AR_FLAG_RETURNDOSMSG | AR_FLAG_FREEDOOF);
}


/********************************************************************/
   long action_seek(struct DosPacket *dosmsg,
								struct MsgPort *dm_retport)
/********************************************************************/
{ /* arg1, buffer, length : actual length */
struct FileArg1 *fa1 = (void *)dosmsg->dp_Arg1;
struct SeekPktMsg *spm;

/** CHECK THIS FILE ARG1 */
	if(!fa1) {
		dosmsg->dp_Res1 = 0; dosmsg->dp_Res2 = ERROR_INVALID_LOCK;
		return(AR_FLAG_RETURNDOSMSG);
	}

/** SEND OFF TO THE REMOTE MACHINE */
	if(!(spm = AllocMem(SEEK_PKTMSG_SIZE, MEMF_PUBLIC))) {
		dosmsg->dp_Res1 = 0; dosmsg->dp_Res2 = ERROR_NO_FREE_STORE;
		return(AR_FLAG_RETURNDOSMSG);
	}
	spm->seekreq.request 	= REQUEST_SEEK;
	spm->seekreq.remote_ifh = fa1->remote.remote_ifh;
	spm->seekreq.remote_majic = fa1->remote.majic;
	spm->seekreq.position 	= dosmsg->dp_Arg2;
	spm->seekreq.mode 		= dosmsg->dp_Arg3;

	INIT_DOOFPKTMSG(&spm->dpm,
				handle_seek_net,							/* our routine to call */
				netrepport,
				dosmsg, dm_retport,							/* original stuff */
				SEEK_PKTMSG_SIZE,
				fa1->remote.machine, fa1->remote.service,
				&spm->seekreq, sizeof(struct NFSSeek),		/* net-service-request */
				NULL, 0,									/* send-body */
				NULL, 0,									/* result-body */
				&spm->seekret, sizeof(struct NFSStdReturn));
															/* return stuff */
	PutMsg(NET_SERV_PORT(fa1), spm);
	return(0);
}


