/*static char *SCCSID = "@(#)stfilea.c	6.3 92/02/21";*/
#define _BTXT _BTXT

#define INCL_DOSERRORS
#include <os2def.h>
#include <bse.h>

#include "const.h"
#include "misc.h"
#include "chain.h"
#include "buf.h"
#include "dir.h"
#include "storage.h"
#include "ea.h"
#include "fnode.h"
#include "volume.h"
#include "tables.h"
#include "pbmacros.h"
#include "superb.h"
#include "lw.h"
#include "frame.h"
#include "fsdata.h"
#include "log.h"

#include "pr_cache.h"
#include "pr_debug.h"
#include "pr_dir.h"
#include "pr_ea.h"
#include "pr_fssub.h"
#include "pr_misc.h"
#include "pr_read.h"

/*
 ***	SetFileEA - Set EAs for a File Handle
 *
 *	Set Extended Attributes for an opened file
 *	(DosSetFileInfo level 2)
 *
 *	Entry	(eax) = file handle
 *		(edi) = ptr to FEA buffer
 *
 *	Exit	0 clear if OK
 *		  *pusRet = 1 if resulting list has FEAs marked as NEED
 *			    0 otherwise
 *
 *		error code if not OK
 *		  *pusRet = offset in FEA buffer of error
 *
 *	HISTORY
 *	    19-Sep-89 by JR (John Rogers, JohnRo@Microsoft)
 *		- Avoid warnings from Microsoft C 5.2 (on slash-slash comments).
 */

USHORT
SetFileEA (PFHT pfht, PFEALIST pfeal, PUSHORT pusRet, PFRAME pfrm )
{
    USHORT cbLen;
    USHORT us;
    PDIRENT pde;
    PBUFNODE pbuf;
    POFT poft;

    pfrm->flDMask |= (poft = pfht->poft)->flDMask;

    LockOFT (poft,pfrm);

    SetLockOFT(poft);

    pde = RDE (poft, &pbuf, pfrm);

    if ( pde == NULL ) {
	/*  Hard error encountered by RDE  */
	UnLockOFT( poft );
	return( GetHrdErr_() );
    }
    cbLen = (USHORT)pde->ulEALen;

    /* un hold dir held by RDE */
    UnHoldDir ();

    CHECKSWAP(&swapBSWAP0);
    if( (us = SetEA (pfeal, poft, pusRet, &cbLen, FALSE, pfrm )) ==
							     NO_ERROR ) {
	/* EA len has changed, reflect this in the dir ent */

	/*
	 * BUGBUG - maybe we should just hold the pbuf
	 * accross the SetEA call so we would not have to do
	 * this RDE.  At least this way we don't have the
	 * sub dir held during the possible block
	 */
	pde = RDE( poft, &pbuf, pfrm );

	if( pde == NULL ) {
	    /*	Hard error encountered by RDE  */
	    UnLockOFT( poft );
	    return( GetHrdErr_() );
	}

	/* set/reset the need feas bit for this file */
	pde->fFlags = (pde->fFlags & ~DF_NEEDEAS) | (BYTE)*pusRet;

	pde->ulEALen = (ULONG)cbLen;

	/*
	 * Don't want write thru because this is a handle based
	 * operation, and the DirEnt will get flushed in Close().
	 */
	SetDEDirt( pde, pbuf, pfrm->u.fWrtThru, pfrm );
	UnHoldDir();
    }

    UnLockOFT( poft );

    if( us != NO_ERROR )
	return( GetHardErr(us) );
    else
	return( NO_ERROR );
}
