cardfull.c 2.01 KB
#include "testlib.c"

char *pname;
char *dname = "/dev/usb/bbrdb0";
char *uname = "user.sys";
char *tname = "tix.sys";

static void
do_fill_card(BBCHandle h)
{
	int rv;
	int cid;
	int errs = 0;
	char fname[32];
	char bbname[32];
	int len;
	int cardsize, freeblks, reservedblks, badblks;

	for (cid = 0; ; cid++) {
		rv = BBCStats(h, &cardsize, &reservedblks, &freeblks, &badblks);
		printf("%s: BBCStats returns %d, cardsize %d, freeblks %d\n", pname, rv, cardsize, freeblks);
		if (rv < 0) break;
		if (freeblks == 0) {
		        printf("%s: card is full ... check files\n", pname);
			break;
		}
		len = freeblks >> 1;
		if (len == 0) len++;
		len *= 1024;	/* stats are in terms of KB */
		len = BB_RND_BLKS(len);
		len = MIN(len, 32*1024*1024);	/* not enough memory on depot to compare 64MB files */
		sprintf(fname, "testfile.%d", cid);
		printf("%s: create content file %s, len %d\n", pname, fname, len);
		mkfile(fname, len);
		if ((rv = storecont(h, fname, cid)) < 0) {
		        printf("%s: store content fails: %d\n", pname, rv);
			break;
		}
	}

	printf("%s: check all content files\n", pname);
	while (--cid >= 0) {
		sprintf(fname, "testfile.%d", cid);
		sprintf(bbname, "%08x.app", cid);
		rv = cmpfile(h, fname, bbname);
		if (rv != 0) {
			errs++;
			printf("%s: miscompare files %s, %s\n", pname, fname, bbname);
		}
	}
	if (errs > 0) {
		printf("%s: total %d miscompares, exiting\n", pname, errs);
		exit(1);
	}
	return;
}

int
main(int argc, char **argv)
{
	BBCHandle h;
	int rv;

	pname = argv[0];
	if (argc > 1)
		dname = argv[argc - 1];
	setbuf(stdout, NULL);
	setbuf(stderr, NULL);

	while (1) {
		h = BBCInit(dname, BBC_SYNC);
		printf("%s: BBCInit(%s) returns %d\n", pname, dname, h);
		if (h < 0) continue;
		printf("%s: format card ...\n", pname);
		rv = BBCSetLed(h, BBC_LED_ORANGE);
		rv = BBCFormatCard(h);
		printf("%s: BBCFormatCard returns %d\n", pname, rv);
		if (h < 0) continue;
		do_fill_card(h);
		rv = BBCSetLed(h, BBC_LED_OFF);
		rv = BBCClose(h);
		printf("%s: BBCClose returns %d\n", pname, rv);
	}

	exit(0);
}