fillcard.c 5.89 KB
int maxcid;

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

	/*
	 * start with a random length file full of zeros
	 */
	len = rand() % 64;
	if (len == 0) len++;
	rlen = len * BLK_SIZE;
	strcpy(fname, "zerofile");
	mkzerofile(fname, rlen);

	/*
	 * set RTC to test fix (only meaningful in direct mode talking to BB)
	 */
	curtime = time(NULL);
	if ((rv = BBCSetTime(h, curtime)) < 0) {
		printf("%s: BBCSetTime returns %d\n", pname, rv);
		return;
	}
	cid = 500;
	if ((rv = storecont(h, fname, cid)) < 0) {
		printf("%s: storecont %s fails: %d\n", pname, fname, rv);
		return;
	}

	len = 0;
	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) return;
		if (freeblks == 0) {
		        printf("%s: card is full ... check files\n", pname);
			break;
		}
		len += BLK_SIZE*32;
		if (len > cardsize*1024/8)
			len = BLK_SIZE*32;
		//rlen = (cid & 1) ? len : len*4;
		rlen = len*32;
		sprintf(fname, "testfile.%d", cid);
		printf("%s: create content file %s, len %d\n", pname, fname, rlen);
		mkfile(fname, rlen);
		if ((rv = storecont(h, fname, cid)) < 0) {
			printf("%s: storecont %s fails: %d\n", pname, fname, rv);
			if (rv == BBC_NOSPACE || rv == BBC_BADHANDLE || rv == BBC_ERROR)
				break;
			return;
		}

		// Check file is written correctly 
		sprintf(fname, "testfile.%d", cid);
		sprintf(bbname, "%08x.app", cid);
		printf("comparing %s and %s\n", fname, bbname);
		rv = cmpfile(h, fname, bbname);
		if (rv != 0) {
			errs++;
			printf("%s: miscompare files %s, %s\n", pname, fname, bbname);
		}

	}
	maxcid = cid;

	/*
	 * Now delete all the ones with odd CIDs to create holes
	 */
	printf("%s: delete half the content files\n", pname);
	for (cid = 1; cid < maxcid; cid += 2) {
		if ((rv = BBCRemoveContent(h, cid)) < 0) {
			printf("%s: remove content id %x fails: %d\n", pname, cid, rv);
			return;
		}
	}

	printf("%s: check remaining content files\n", pname);
	for (cid = 0; cid < maxcid; cid += 2) {
		sprintf(fname, "testfile.%d", cid);
		sprintf(bbname, "%08x.app", cid);
		printf("comparing %s and %s\n", fname, bbname);
		rv = cmpfile(h, fname, bbname);
		if (rv != 0) {
			errs++;
			printf("%s: miscompare files %s, %s\n", pname, fname, bbname);
		}
	}
	sprintf(bbname, "%08x.app", 500);
	strcpy(fname, "zerofile");
	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);
		return;
	}

	/*
	 * Now try to fill up the remaining space with one file
	 */
	rv = BBCStats(h, &cardsize, &reservedblks, &freeblks, &badblks);
	printf("%s: BBCStats returns %d, cardsize %d, freeblks %d\n", pname, rv, cardsize, freeblks);
	if (rv < 0) return;
	/*
	 * First try one that is one block too big to fit
	 */
	len = BB_RND_BLKS(freeblks*1024+1);
	sprintf(fname, "testfile.%d", maxcid);
	printf("%s: create content file %s, len %d\n", pname, fname, len);
	mkfile(fname, len);
	if ((rv = storecont(h, fname, maxcid)) < 0) {
		printf("%s: storecont %s fails (EXPECTED): %d\n", pname, fname, rv);
	} else {
		printf("%s: storecont %s succeeds (should have failed): %d\n", pname, fname, rv);
		return;
	}
	/* Now try the exact size */
	len = BB_RND_BLKS(freeblks*1024);
	printf("%s: create content file %s, len %d\n", pname, fname, len);
	mkfile(fname, len);
	if ((rv = storecont(h, fname, maxcid)) < 0) {
		printf("%s: storecont %s fails: %d\n", pname, fname, rv);
		return;
	}
	rv = BBCStats(h, &cardsize, &reservedblks, &freeblks, &badblks);
	printf("%s: BBCStats returns %d, cardsize %d, freeblks %d\n", pname, rv, cardsize, freeblks);
	if (rv < 0) return;
	printf("%s: card is full, check all remaining content files\n", pname);
	for (cid = 0; cid < maxcid; cid += 2) {
		sprintf(fname, "testfile.%d", cid);
		sprintf(bbname, "%08x.app", cid);
		printf("comparing %s and %s\n", fname, bbname);
		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);
		return;
	}
	sprintf(bbname, "%08x.app", 500);
	strcpy(fname, "zerofile");
	printf("%s: check zero file %s\n", pname, fname);
	rv = cmpfile(h, fname, bbname);
	if (rv != 0) {
		errs++;
		printf("%s: miscompare files %s, %s\n", pname, fname, bbname);
	}
	sprintf(fname, "testfile.%d", maxcid);
	sprintf(bbname, "%08x.app", maxcid);
	printf("%s: check last big file %s\n", pname, fname);
	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);
		return;
	}
	return;
}

static void
do_check_files(BBCHandle h)
{
	int cid;
	int rv;
	char fname[32];
	char bbname[32];
	int errs = 0;

	printf("%s: check all content files\n", pname);
	for (cid = 0; cid < maxcid; cid += 2) {
		sprintf(fname, "testfile.%d", cid);
		sprintf(bbname, "%08x.app", cid);
		printf("comparing %s and %s\n", fname, bbname);
		rv = cmpfile(h, fname, bbname);
		if (rv != 0) {
			errs++;
			printf("%s: miscompare files %s, %s\n", pname, fname, bbname);
		}
	}
	sprintf(bbname, "%08x.app", 500);
	strcpy(fname, "zerofile");
	printf("%s: check zero file %s\n", pname, fname);
	rv = cmpfile(h, fname, bbname);
	if (rv != 0) {
		errs++;
		printf("%s: miscompare files %s, %s\n", pname, fname, bbname);
	}
	sprintf(fname, "testfile.%d", maxcid);
	sprintf(bbname, "%08x.app", maxcid);
	printf("%s: check last big file %s\n", pname, fname);
	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);
		return;
	}
}