// IfsLeafClose.bcpl - Leaf Close - SWAPPABLE
// Copyright Xerox Corporation 1979, 1980

//Last modified by Butterfield, April 25, 1979  12:57 PM
//Last modified by Wobber, March 31, 1981  5:34 PM

get ecBrokenLeafQueue from "IfsLeafErrors.decl";
get "IfsLeaf.decl";
get "IfsSequin.decl";

external
[
//outgoing procedures
CloseLeaf; CloseFH;

//incoming procedures
AnswerSetOp; CheckHandle; DestroyFD;
FreeLeafVMem; Free; FreePointer; IFSError; LeafPurgeVFile; Unqueue;

//incoming statics
sysZone;
]

//----------------------------------------------------------------------------
let CloseLeaf(sequin, answerPBI, op) = valof
//----------------------------------------------------------------------------
[
let fh = CheckHandle(sequin, answerPBI, op);
test op>>Op.code eq opCloseTransaction
   ifso LeafPurgeVFile(fh>>FH.lvmd);
   ifnot CloseFH(sequin, fh);
AnswerSetOp(answerPBI, op, 2*lenLeafAnswer);
resultis leafOpComplete;
]

//----------------------------------------------------------------------------
and CloseFH(sequin, fh) be
//----------------------------------------------------------------------------
[
unless Unqueue(lv sequin>>Sequin.fhQ, fh) do IFSError(ecBrokenLeafQueue);
let fd = fh>>FH.fd;
if fd ne 0 then
   [
   let lvmd = fh>>FH.lvmd;
   if lvmd ne 0 then FreeLeafVMem(fd, lvmd);
   DestroyFD(fd);
   ]
FreePointer(lv fh>>FH.ui.userName, lv fh>>FH.ui.connName);
Free(sysZone, fh);
]