/********************************************************************
 ** 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
 **
 ** NET-WRITE
 **
 ** TABS to 4
 ********************************************************************/

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

#define WRITE_PKTMSG_SIZE (sizeof(struct WritePktMsg))

/** another variation of the "doofus" for writing */
struct WritePktMsg {
	struct	DoofusPktMsg	dpm;

	struct	NFSWrite			writereq;	/* request packet struct */
	struct	NFSStdReturn		writeret;	/* what the server sends back */
	struct	FileArg1			*fa1;
};

/********************************************************************/
static long handle_write_net(struct WritePktMsg *wpm)
/********************************************************************/
{
struct DosPacket *dosmsg = wpm->dpm.dp_original;
	dosmsg->dp_Res1 = wpm->writeret.remote_res1;
	dosmsg->dp_Res2 = wpm->writeret.remote_res2;
	if(wpm->dpm.net.pm.smsg.result != PAR_OK)
		{ dosmsg->dp_Res1 = 0; dosmsg->dp_Res2 = ABORT_BUSY; }
	return(AR_FLAG_RETURNDOSMSG | AR_FLAG_FREEDOOF);
}



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

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

/** SEND OFF TO THE REMOTE MACHINE */
	if(!(wpm = AllocMem(WRITE_PKTMSG_SIZE, MEMF_PUBLIC))) {
		dosmsg->dp_Res1 = 0; dosmsg->dp_Res2 = ERROR_NO_FREE_STORE;
		return(AR_FLAG_RETURNDOSMSG);
	}
	wpm->writereq.request 	= REQUEST_WRITE;
	wpm->writereq.remote_ifh = fa1->remote.remote_ifh;
	wpm->writereq.remote_majic = fa1->remote.majic;
	wpm->fa1 = fa1;
	INIT_DOOFPKTMSG(&wpm->dpm,
				handle_write_net,							/* our routine to call */
				netrepport,
				dosmsg, dm_retport,							/* original stuff */
				WRITE_PKTMSG_SIZE,
				fa1->remote.machine, fa1->remote.service,
				&wpm->writereq, sizeof(struct NFSWrite),	/* net-service-request */
				(void *)dosmsg->dp_Arg2, dosmsg->dp_Arg3,	/* send-body */
				NULL, 0,									/* result-body */
				&wpm->writeret, sizeof(struct NFSStdReturn));
															/* return stuff */
	PutMsg(NET_SERV_PORT(fa1), wpm);
	return(0);
}
