/*static char *SCCSID = "@(#)dirmap.c	6.1 90/11/17";*/
/*static char *SCCSID = "@(#)dirmap.c	13.2 90/01/25";*/
#include <os2.h>

#include "const.h"
#include "misc.h"
#include "chain.h"
#include "buf.h"
#include "dir.h"
#include "storage.h"
#include "fnode.h"
#include "volume.h"
#include "tables.h"
#include "superb.h"
#include "pb_ifs.h"
#include "lw.h"
#include "frame.h"
#include "fsdata.h"
#include "assert.h"

#include "pr_CACHE.h"


/*
 ***	DirMap - Produce a Mapping of Directories
 */

void far
DirMap (void)
{
#ifdef DEBUG1
    PDIRBLK far	    *ppdbFound;
    PBUFNODE far    *ppbufFound;
    PLSN	    plsnFound;
    USHORT	    dirlevel;


    if ((flFastFile & FF_DIRMAP) != 0) {

	/* get the sector data for the root */
	LSD (&(psbdRoot -> scpTopDirBlkHint), BP_TOSS,
	     (PSECTOR far *) ppdbFound, ppbufFound, plsnFound);

	/* Set up Directory Table */
	DirTab [0].lsn = *plsnFound;
	DirTab [0].pbuf = *ppbufFound;
	DirTab [0].level = 0;

	DirTab [1].lsn = -1;
	DirTab [1].level = -1;

	dirlevel = 0;

	/* loop through directory */
	while (DoMap (dirlevel))
	    dirlevel++;

	/* mark first empty entry */
	DirTab [dirlevel].lsn = -1;
    }
#endif
}


/*
ifdef	DEBUG

;**	DirMap - Produce a Mapping of Directorys


	Public	DirMap
DirMap	PROC	NEAR
	test	FastFile,FF_DIRMAP
	jz	short dmap4
	pushad
	lea	eax, DirRoot.SD_SEC
	call	lsd			; locate root
	mov	ecx,DirRoot.SD_SEC.SNUM
	mov	DirTab,ecx
	mov	DirTab+4,eax
	mov	DirTab+8,0
	mov	DirTab+16,-1
	mov	DirTab+16+8,-1

;	OK, do the levels recursively

	sub	eax,eax			; (eax) = level we want
	mov	edi,offset dgroup:DirTab+16
dmap1:	push	eax
	call	domap
	pop	eax
	jc	dmap2
	inc	eax
	jmp	dmap1

dmap2:	mov	dword ptr [edi],-1
	popad
dmap4:	ret


;*	DoMap - do next level
;
;	ENTRY	(edi) = address to store next result in
;		(ax) = level we want to traverse
;	EXIT	'C' set if can't go any lower
;		(edi) updated
;	uses	all


domap:	mov	esi,offset dgroup:DirTab-16
dm1:	add	esi,16
	cmp	word ptr 8[esi],ax
	jne	dm1

;	got our first guys here

dm2:	push	eax
	push	esi

	mov	eax,[esi]		; (eax) = sector of this guy
	mov	dl,BP_KEEP
	call	RdBuf			; read him hin
	push	ecx
	inc	B_HCNT[ecx]
	lea	esi,DB_START[eax]	; (esi) = first record
	test	DIR_FLAG[esi],DF_BTP
	stc
	jz	dm99
	movzx	edx,word ptr 8[esp]	; (edx) = level and sib
	inc	edx
dm5:	movzx	ebx,DIR_NAML[esi]
	mov	eax,DIR_BTP-1[ebx+esi]
	stosd				; store sector #
	push	edx
	call	RdBuf
	pop	edx
	stosd				; set address
	mov	dword ptr [edi],edx
	add	edi,4
	mov	eax,DB_PAR[eax]
	stosd
	add	edx,10000h
	test	DIR_FLAG[esi],DF_END
	jnz	short dm10
	add	si,DIR_ELEN[esi]
	jmp	dm5

;	done with this block

dm10:	pop	ecx
	dec	B_HCNT[ecx]		; unhold this dir guy
	pop	esi
	pop	eax
	add	esi,16
	cmp	word ptr 8[esi],ax
	je	dm2
	clc
	ret

dm99:	pop	ecx
	dec	B_HCNT[ecx]
	pop	esi
	pop	eax
	ret

DirMap	ENDP

endif
*/
