pfsdeletefile.c 3.45 KB

/**************************************************************************
 *                                                                        *
 *               Copyright (C) 1995, Silicon Graphics, Inc.               *
 *                                                                        *
 *  These coded instructions, statements, and computer programs  contain  *
 *  unpublished  proprietary  information of Silicon Graphics, Inc., and  *
 *  are protected by Federal copyright law.  They  may  not be disclosed  *
 *  to  third  parties  or copied or duplicated in any form, in whole or  *
 *  in part, without the prior written consent of Silicon Graphics, Inc.  *
 *                                                                        *
 **************************************************************************/

#include "osint.h"
#include "controller.h"

s32
osPfsDeleteFile(OSPfs *pfs, u16 company_code, u32 game_code,
	     u8 *game_name, u8 *ext_name)
{

	s32 file_no;
	s32 ret;
	__OSInode inode ;
	__OSDir dir;
	__OSInodeUnit last_page;
	u8 startpage;
	u8 bank;

	if ((company_code == 0) || (game_code == 0))
		return(PFS_ERR_INVALID);

	if ((ret = osPfsFindFile(pfs, company_code, game_code, 
				game_name, ext_name, &file_no))!= 0)
		return(ret);

#ifdef	_PFS_1M_EXTENSION
	if ((ret = __osPfsSelectIdBank(pfs)) != 0) return(ret);
#else
	if (pfs->activebank != 0)
	  if ((ret = __osPfsSelectBank(pfs, 0)) != 0) return(ret);
#endif
	/* get dir structure */

	if ((ret = __osContRamRead(pfs->queue, pfs->channel,
		(u16)(pfs->dir_table + (int)file_no), (u8 *)&dir)) != 0)
		return(ret);

	/* release pages */

	/* loop through all the banks */

	startpage = dir.start_page.inode_t.page;
	for (bank = dir.start_page.inode_t.bank ; bank < pfs->banks; ) {

		if ((ret = __osPfsRWInode(pfs, &inode, PFS_READ, bank)) != 0)
			return(ret);

		if ((ret = __osPfsReleasePages(pfs, &inode, startpage,
					       bank, &last_page )) != 0)
				return(ret);
		if ((ret = __osPfsRWInode(pfs, &inode, PFS_WRITE, bank)) != 0)
			return(ret);
		if (last_page.ipage == PFS_EOF)
			break;

		bank = last_page.inode_t.bank;
		startpage = last_page.inode_t.page;
	}

	if (bank >= pfs->banks)
		return(PFS_ERR_INCONSISTENT);

	/* update dir */
	bzero(&dir, sizeof(__OSDir));

	ret = __osContRamWrite(pfs->queue, pfs->channel,
		(u16)(pfs->dir_table + (int)file_no), (u8 *)&dir, 0);
	return(ret);
}

s32
__osPfsReleasePages(OSPfs *pfs, __OSInode *inode, u8 start_page, u8 bank,
		    __OSInodeUnit *last_page)
{
	__OSInodeUnit next_page, old_page;
	s32 ret = 0;

	next_page.ipage = (u16)((bank<<8) + start_page);

	do {
	  old_page = next_page;
	  next_page = inode->inode_page[next_page.inode_t.page];
	  inode->inode_page[old_page.inode_t.page].ipage = PFS_PAGE_NOT_USED;
	} while (next_page.ipage >= pfs->inode_start_page &&
		 next_page.inode_t.bank == bank);

	*last_page = next_page;

	return(ret);
}

#if	0
s32
__osBlockSum(OSPfs *pfs, u8 page_no, u16 *sum, u8 bank)
{
	int i;
	s32 ret = 0;
	u8 data[BLOCKSIZE];

	if ((ret = __osPfsSelectBank(pfs, bank)) != 0)
		return(ret);
	for (i = 0; i < PFS_ONE_PAGE; i++) {
		ret = __osContRamRead(pfs->queue, pfs->channel,
                (u16)(page_no * PFS_ONE_PAGE + i), (u8 *)&data);
                if (ret != 0) {
		  break;
		}
		*sum = (u16)(*sum +  __osSumcalc((u8 *)&data, BLOCKSIZE));
        }
#ifdef	_PFS_1M_EXTENSION
	ret = __osPfsSelectIdBank(pfs);
#else
	if (pfs->activebank != 0)
	  ret = __osPfsSelectBank(pfs, 0);
#endif
        return(ret);
}
#endif