/********************************************************************
 ** 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
 **
 ** CLOSE (ACTION_END)
 **
 ** Actions are sent to the network device
 **
 ** BUGS: This filesystem does not directly support NewShell (& run) since
 ** they insist on opening a file called "*" for output, which, in some cases
 ** would be very difficult to track.
 **
 ** TABS to 4
 ********************************************************************/

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

#define CLOSE_PKTMSG_SIZE sizeof(struct ClosePktMsg)

/* variation of the "DoofusPktMsg" */
struct ClosePktMsg {
	struct	DoofusPktMsg dpm;

	struct	NFSCloseFile		closereq;	/* request packet struct */
	struct	NFSStdReturn		closeret;	/* what the server sends back */
	struct	FileArg1			*fa1;
};


/********************************************************************/
static long handle_end_net(struct ClosePktMsg *cpm)
/********************************************************************/
{
struct FileArg1	*fa1 = cpm->fa1;
	REMOVE_FROM_LIST(fa1, first_fa1);
	FreeRemember(&fa1->mem, TRUE);
	FreeMem(fa1, FA1_SIZE);
	return(AR_FLAG_RETURNDOSMSG | AR_FLAG_FREEDOOF);
}


/********************************************************************/
long action_end(struct DosPacket *dosmsg, struct MsgPort *dm_retport)
/********************************************************************/
{ /* FileHandle.Arg1 : bool */
struct FileArg1 *fa1 = (void *)dosmsg->dp_Arg1;
struct ClosePktMsg *cpm;

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

/** SEE IF WE HAVE TO SEND OFF A NFS REQUEST */
	if(fa1->Type == LK_REMOTE) {
	/** SEND OFF TO THE REMOTE MACHINE */
		if(!(cpm = AllocMem(CLOSE_PKTMSG_SIZE, MEMF_PUBLIC))) {
			dosmsg->dp_Res1 = 0; dosmsg->dp_Res2 = ERROR_NO_FREE_STORE;
			return(AR_FLAG_RETURNDOSMSG);
		}
		cpm->closereq.request 	= REQUEST_CLOSEFILE;
		cpm->closereq.remote_ifh = fa1->remote.remote_ifh;
		cpm->closereq.remote_majic = fa1->remote.majic;
		cpm->fa1 = fa1;
		INIT_DOOFPKTMSG(&cpm->dpm,
				handle_end_net,								/* our routine to call */
				netrepport,									/* net reply port */
				dosmsg, dm_retport,							/* dos msg & reply port */
				CLOSE_PKTMSG_SIZE,							/* saved stuff */
				fa1->remote.machine, fa1->remote.service,	/* FileArg1 */
				&cpm->closereq, sizeof(struct NFSCloseFile),/* net-service-request */
				NULL, 0,									/* send-body */
				NULL, 0,									/* result-body */
				&cpm->closeret, sizeof(struct NFSStdReturn));
															/* return stuff */

	{ char tt[100]; spf(tt, 100, "port=%lx", NET_SERV_PORT(fa1)); MyPrint(win1, tt); }

		PutMsg(NET_SERV_PORT(fa1), cpm);
		return(0);
	}

/** FREE ANY OTHER INTERNAL */
	REMOVE_FROM_LIST(fa1, first_fa1);
	FreeRemember(&fa1->mem, TRUE);
	FreeMem(fa1, sizeof(struct FileArg1));
	return(AR_FLAG_RETURNDOSMSG);
}
