fsrename.c 1.13 KB
#include <PR/bcp.h>
#include "os.h"
#include "os_bbfs.h"
#include "bbint.h"
s32
osBbFRename(const char* old, const char* new) {
    unsigned char fold[BB_INODE16_NAMELEN], fnew[BB_INODE16_NAMELEN];
    s32 rv, inew = -1, iold = -1;
    BbFat16* fat;
    int i;
    __osBbFsFormatName(fold, old);
    __osBbFsFormatName(fnew, new);
    if (!fold[0] || !fnew[0]) return BBFS_ERR_INVALID;
    if ((rv = __osBbFsGetAccess()) < 0) return rv;
    fat = __osBbFat;
    for(i = 0; i < BB_INODE16_ENTRIES; i++) {
	if (fat->inode[i].type) {
	    if (bcmp(fnew, fat->inode[i].name, BB_INODE16_NAMELEN) == 0)
		inew = i;
	    else if (bcmp(fold, fat->inode[i].name, BB_INODE16_NAMELEN) == 0)
	        iold = i;
	}
    }
    if (iold == -1) {
	rv = BBFS_ERR_ENTRY;
	goto error;
    } else if (inew != -1) {
	u16 b;
	/* free all of the blocks */
	for(b = fat->inode[inew].block; b != BB_FAT_LAST; ) {
	    u16 o = BB_FAT16_NEXT(fat,b);
	    BB_FAT16_NEXT(fat,b) = BB_FAT_AVAIL;
	    b = o;
	}
	bzero(fat->inode+inew, sizeof(BbInode));
    }
    bcopy(fnew, fat->inode[iold].name, BB_INODE16_NAMELEN);
    rv = __osBbFsSync(0);
error:
    __osBbFsRelAccess();
    return rv;
}