/*static char *SCCSID = "@(#)lookfile.c 6.2 92/01/31";*/
#define _BTXT _BTXT
#define INCL_DOSERRORS
#include <os2def.h>
#include <bse.h>
#include "api.h"
#include "const.h"
#include "misc.h"
#include "chain.h"
#include "buf.h"
#include "dir.h"
#include "volume.h"
#include "storage.h"
#include "fnode.h"
#include "tables.h"
#include "pbmacros.h"
#include "assert.h"
#include "lw.h"
#include "frame.h"
#include "fsdata.h"
#include "log.h"                                                 // @76193
#include "pr_DIR.h"
#include "pr_MISC.h"
#include "pr_debug.h"

/*
 ***    LookFile - Lookup File in Directory
 *
 *      LookFile takes a full pathname and locates the file in a directory.
 *      It's basically a call to FindDIR followed by a call to LDE; some folks
 *      need special processing and do this themselves, but folks that just
 *      want to lookup the file call LookFile.
 *
 *      ENTRY   pcp (ebp) = path name
 *      RETURNS NO_ERROR if successful
 *                SBDIR held
 *                *ppdbRes (eax) = address of start of directory block, NOT HELD
 *                *ppdeRes (EBX) = address of entry
 *                *ppbufRes(ECX) = address of buffer header for dirblk
 *              error code if unsuccessful
 *
 * HISTORY:
 *  @76193  23-Nov-1993 by D.Azzarito - Change holds for LOG support
 */

USHORT
LookFile( pcp, ppdbRes, ppdeRes, ppbufRes, pfrm )
PCRACKEDPATH   pcp;
PDIRBLK far *  ppdbRes;
PDIRENT far *  ppdeRes;
PBUFNODE far * ppbufRes;
PFRAME         pfrm;
{
        PDIRBLK pdbTop;         /*  ptr to top DIRBLK in directory      */
        PBUFNODE pbufTop;       /*  ptr to buffer for pdbTop            */
        PSUBDIR psbd;           /*  ptr to SUBDIR struct for directory  */

        /*  If FindDir fails, return the error.  Otherwise, it puts
         *  the subdir in pfrm->psbd and holds it.  It also gives us
         *  the top DIRBLK we need for LDE.
         *
         */
        if ( !FindDir( pcp, &pdbTop, &pbufTop, &psbd, pfrm ) ) {

                return (ERROR_PATH_NOT_FOUND);
        }

        ASSERT_DB(pdbTop);
        ASSERT_BUF(pbufTop);
        ASSERT_SBD(psbd);

        /*  LDE is called with action OAC_OF (fail if file does not exist),
         *  and without write-through.  Note that LDE will increment
         *  *ppbufRes' hold count even if the file is not found.
         *
         */
        if ( LDE( pdbTop, OAC_OF, FALSE, pbufTop, FILE_NAME(pcp),
                  ppbufRes, ppdbRes, ppdeRes, FALSE, pfrm) != LDE_FOUND ) { //#90495

            DecrHoldsBUF( *ppbufRes );  // @76193
            UnHoldDir();
            return (ERROR_FILE_NOT_FOUND);
        } else {
            DecrHoldsBUF( *ppbufRes );  // @76193
            return (NO_ERROR);
        }
}
