⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tfstest.c

📁 极其轻量级的文件系统FAT16格式
💻 C
📖 第 1 页 / 共 2 页
字号:
	if (err != TFS_OKAY)
		tfsdie(err);
	err = mon_tfswrite(tfd,&c,1);
	if (err != TFS_OKAY)
		tfsdie(err);
	mon_tfsclose(tfd,0);

	/* Add a new file that "should" be identical to the modified file;
	 * then compare them and delete the newfile...
	 */
	err = mon_tfsadd(TMPFILE,"newdata1","2",newdata,strlen(newdata));
	if (err != TFS_OKAY)
		tfsdie(err);
	if (cmp(fname,TMPFILE) != 0)
		die();
	err = mon_tfsunlink(TMPFILE);
	if (err != TFS_OKAY)
		tfsdie(err);
	
	return(0);
}

/*
 *	ctrltest():
 *	Test various aspects of the tfsctrl() function.
 */
void
ctrltest(char *fname, char *data)
{
	int		tfd, ret;
	char	flags[16];

	/* Copy fname to TMPFILE... */
	cp (TMPFILE,fname);

	/* Verify TFS_UNOPEN...
	 *	Open a file, modify it, then prior to calling tfsclose(), call
	 *	tfsctrl(TFS_UNOPEN) on that file descriptor and make sure the
	 *	file is in its original state and the file descriptor has been
	 *	closed...
	 */
	tfd = mon_tfsopen(fname,TFS_APPEND,buffer1);
	if (tfd < 0)
		tfsdie(tfd);
	ret = mon_tfswrite(tfd,"abcdefg",7);
	if (ret != TFS_OKAY)
		tfsdie(ret);
	ret = mon_tfsctrl(TFS_UNOPEN,tfd,0);
	if (ret != TFS_OKAY)
		tfsdie(ret);
	ret = mon_tfsclose(tfd,0);
	if (ret != TFSERR_BADFD)
		tfsdie(ret);
	if (cmp(TMPFILE,fname))
		die();

	/* Verify TFS_TELL...
	 *	Open a file, seek to a known point, then make sure that
	 *	tfsctrl(TFS_TELL) returns the expected offset. 
	 */
	tfd = mon_tfsopen(fname,TFS_RDONLY,0);
	if (tfd < 0)
		tfsdie(tfd);
	ret = mon_tfsseek(tfd,5,TFS_BEGIN);
	if (ret != TFS_OKAY)
		tfsdie(ret);
	ret = mon_tfsctrl(TFS_TELL,tfd,0);
	if (ret != 5)
		tfsdie(ret);
	ret = mon_tfsread(tfd,buffer1,3);
	if (ret != 3)
		tfsdie(ret);
	ret = mon_tfsctrl(TFS_TELL,tfd,0);
	if (ret != 5+3)
		tfsdie(ret);

	ret = mon_tfsclose(tfd,0);
	if (ret != TFS_OKAY)
		tfsdie(ret);
	

	/* Test all "flag-ascii-to-binary" conversions... */
	if ((ret = mon_tfsctrl(TFS_FATOB,(long)"e",0)) !=  TFS_EXEC)
		tfsdie(ret);
	if ((ret = mon_tfsctrl(TFS_FATOB,(long)"b",0)) !=  TFS_BRUN)
		tfsdie(ret);
	if ((ret = mon_tfsctrl(TFS_FATOB,(long)"B",0)) !=  TFS_QRYBRUN)
		tfsdie(ret);
	if ((ret = mon_tfsctrl(TFS_FATOB,(long)"C",0)) !=  TFS_COFF)
		tfsdie(ret);
	if ((ret = mon_tfsctrl(TFS_FATOB,(long)"E",0)) !=  TFS_ELF)
		tfsdie(ret);
	if ((ret = mon_tfsctrl(TFS_FATOB,(long)"A",0)) !=  TFS_AOUT)
		tfsdie(ret);
	if ((ret = mon_tfsctrl(TFS_FATOB,(long)"c",0)) !=  TFS_CPRS)
		tfsdie(ret);
	if ((ret = mon_tfsctrl(TFS_FATOB,(long)"i",0)) !=  TFS_IPMOD)
		tfsdie(ret);
	if ((ret = mon_tfsctrl(TFS_FATOB,(long)"u",0)) !=  TFS_UNREAD)
		tfsdie(ret);
	if ((ret = mon_tfsctrl(TFS_FATOB,(long)"1",0)) !=  TFS_ULVL1)
		tfsdie(ret);
	if ((ret = mon_tfsctrl(TFS_FATOB,(long)"2",0)) !=  TFS_ULVL2)
		tfsdie(ret);
	if ((ret = mon_tfsctrl(TFS_FATOB,(long)"3",0)) !=  TFS_ULVL3)
		tfsdie(ret);

	/* Test all "flag-binary-to-ascii" conversions... */
	ret = mon_tfsctrl(TFS_FBTOA,TFS_EXEC,(long)flags);
	if ((ret == TFSERR_BADARG) || (strcmp(flags,"e")))
		tfsdie(ret);
	ret = mon_tfsctrl(TFS_FBTOA,TFS_BRUN,(long)flags);
	if ((ret == TFSERR_BADARG) || (strcmp(flags,"b")))
		tfsdie(ret);
	ret = mon_tfsctrl(TFS_FBTOA,TFS_QRYBRUN,(long)flags);
	if ((ret == TFSERR_BADARG) || (strcmp(flags,"B")))
		tfsdie(ret);
	ret = mon_tfsctrl(TFS_FBTOA,TFS_COFF,(long)flags);
	if ((ret == TFSERR_BADARG) || (strcmp(flags,"C")))
		tfsdie(ret);
	ret = mon_tfsctrl(TFS_FBTOA,TFS_ELF,(long)flags);
	if ((ret == TFSERR_BADARG) || (strcmp(flags,"E")))
		tfsdie(ret);
	ret = mon_tfsctrl(TFS_FBTOA,TFS_AOUT,(long)flags);
	if ((ret == TFSERR_BADARG) || (strcmp(flags,"A")))
		tfsdie(ret);
	ret = mon_tfsctrl(TFS_FBTOA,TFS_CPRS,(long)flags);
	if ((ret == TFSERR_BADARG) || (strcmp(flags,"c")))
		tfsdie(ret);
	ret = mon_tfsctrl(TFS_FBTOA,TFS_IPMOD,(long)flags);
	if ((ret == TFSERR_BADARG) || (strcmp(flags,"i")))
		tfsdie(ret);
	ret = mon_tfsctrl(TFS_FBTOA,TFS_UNREAD,(long)flags);
	if ((ret == TFSERR_BADARG) || (strcmp(flags,"u")))
		tfsdie(ret);
	ret = mon_tfsctrl(TFS_FBTOA,TFS_ULVL1,(long)flags);
	if ((ret == TFSERR_BADARG) || (strcmp(flags,"1")))
		tfsdie(ret);
	ret = mon_tfsctrl(TFS_FBTOA,TFS_ULVL2,(long)flags);
	if ((ret == TFSERR_BADARG) || (strcmp(flags,"2")))
		tfsdie(ret);
	ret = mon_tfsctrl(TFS_FBTOA,TFS_ULVL3,(long)flags);
	if ((ret == TFSERR_BADARG) || (strcmp(flags,"3")))
		tfsdie(ret);


	ret = mon_tfsunlink(TMPFILE);
	if (ret != TFS_OKAY)
		tfsdie(ret);
}

/*
 * trunctest():
 *	Test file truncation.  Verify that file truncates to expected size
 *	and that the data in the file is as expected.
 */
int
trunctest(char *fname, char *data)
{
	int	tfd, err, size;
	TFILE	*tfp;

	/* Copy incoming file name to a tempoary file... */
	cp(TMPFILE,fname);

	/* Open the temporary file and truncate it.
	 * First verify that a truncation size too big will fail, then
	 * do a real truncation.  Close the file then verify that the
	 * file has been trunated.
	 */
	tfd = mon_tfsopen(TMPFILE,TFS_APPEND,buffer1);
	if (tfd < 0)
		tfsdie(tfd);
	err = mon_tfstruncate(tfd,9999999);
	if (err != TFSERR_BADARG)
		tfsdie(err);
	err = mon_tfstruncate(tfd,TRUNCATE_SIZE);
	if (err != TFS_OKAY)
		tfsdie(err);
	err = mon_tfsclose(tfd,0);
	if (err < 0)
		tfsdie(err);

	/* Make sure that the file was truncated to the proper size. */
	tfp = mon_tfsstat(TMPFILE);
	if (!tfp)
		die();
	if (TFS_SIZE(tfp) != TRUNCATE_SIZE)
		die();
	
	/* Now reopen the file and verify that the data is correct... */
	tfd = mon_tfsopen(TMPFILE,TFS_RDONLY,0);
	if (tfd < 0)
		tfsdie(tfd);
	
	size = mon_tfsread(tfd,buffer1,TFS_SIZE(tfp));
	if (size != TFS_SIZE(tfp))
		tfsdie(size);

	if (memcmp(buffer1,data,TRUNCATE_SIZE))
		die();

	/* Close and remove the temporary file. */
	mon_tfsclose(tfd,0);
	err = mon_tfsunlink(TMPFILE);
	if (err != TFS_OKAY)
		tfsdie(err);
	return(0);
}

/* ls():
 *	Just list current set of files in TFS...
 */
ls()
{
	TFILE	*tfp;

	tfp = (TFILE *)0;
	while(tfp = mon_tfsnext(tfp))
		mon_printf("%s\n",TFS_NAME(tfp));
}


char *usage_text[] = {
	"Usage: tfstest [options] {file1} {file2}",
	" Options...",
	"  -l   run 'ls' at the end",
	"  -r   don't remove files when done",
	"  -v   verbosity level (additive)",
	" Note: neither file1 nor file2 can be the string 'newfile'",
	(char *)0,
};

void
usage(char *msg)
{
	char **use;

	if (msg)
		mon_printf("%s\n",msg);
	use = usage_text;

	while(*use) {
		mon_printf("%s\n",*use);
		use++;
	}
	mon_appexit(-1);
}

int
main(int argc,char *argv[])
{
	extern int optind;
	int	err, opt, removefiles, list;
	char *file1, *file2;

	verbose = 0;
	list = 0;
	removefiles = 1;
	while((opt=getopt(argc,argv,"lrv")) != -1) {
		switch(opt) {
		case 'l':
			list = 1;
			break;
		case 'r':
			removefiles = 0;
			break;
		case 'v':
			verbose++;
			break;
		default:
			usage(0);
		}
	}

	if (argc != optind+2)
		usage("Bad arg count");

	/* Test all aspects of TFS API calls: */
	file1 = argv[optind];
	file2 = argv[optind+1];
	if ((!strcmp(file1,TMPFILE)) || (!strcmp(file2,TMPFILE)))
		usage(TMPFILE);


	if (verbose)
		mon_printf("tfstest %s %s...\n",file1,file2);
	

	/* 
	 *	Start by removing files to be created later...
	 */
	if (mon_tfsstat(TMPFILE)) {
		if (verbose)
			mon_printf("Removing %s...\n",TMPFILE);
		err = mon_tfsunlink(TMPFILE);
		if (err != TFS_OKAY)
			tfsdie(err);
	}
	if (mon_tfsstat(file1)) {
		if (verbose)
			mon_printf("Removing %s...\n",file1);
		err = mon_tfsunlink(file1);
		if (err != TFS_OKAY)
			tfsdie(err);
	}
	if (mon_tfsstat(file2)) {
		if (verbose)
			mon_printf("Removing %s...\n",file2);
		err = mon_tfsunlink(file2);
		if (err != TFS_OKAY)
			tfsdie(err);
	}

	/*
	 *	Create a file...
	 */
	if (verbose)
		mon_printf("Creating %s...\n",file1);
	err = mon_tfsadd(file1,"data1","2",data1,strlen(data1));
	if (err != TFS_OKAY)
		tfsdie(err);

	/*
	 *	Basic getline test...
	 */
	if (verbose)
		mon_printf("Checking 'getline'...\n");
	getlinetest(file1,data1);

	/*
	 *	Now copy the file...
	 */
	if (verbose)
		mon_printf("Copying %s to %s...\n",file1,file2);
	cp(file2,file1);

	/*
	 *	Now compare the two...
	 *	(they should be identical)
	 */
	if (verbose)
		mon_printf("Comparing %s to %s...\n",file1,file2);
	if (cmp(file1,file2) != 0)
		die();

	/*
	 *	Seek test...
	 *  Verify that data at a specified offset is as expected based on the
	 *	file (file1) initially created from the data1 array...
	 */
	if (verbose)
		mon_printf("Running seek test on %s...\n",file1);
	seektest(file1,38,data1[38]);

	/* 
	 *	Truncateion test...
	 *	Truncate a file and verify.
	 */
	if (verbose)
		mon_printf("Running truncation test on %s...\n",file1);
	trunctest(file1,data1);

	/*
	 *	Tfsctrl() function test...
	 */
	if (verbose)
		mon_printf("Running tfsctrl test...\n");
	ctrltest(file1,data1);

	/* 
	 *	Write test...
	 *	Modify a file in a few different ways and verify the modification...
	 *	Note that after this point, file1 and data1 are not the same.
	 *	The content of file1 will be the same as the new_data1 array.
	 */
	if (verbose)
		mon_printf("Running write test on %s...\n",file1);
	writetest(file1,new_data1);
	
	/*
	 *	File in-use test...
	 *	Verify that if a file is in-use, it cannot be removed.
	 */
	if (verbose)
		mon_printf("Running in-use test on %s...\n",file1);
	inusetest(file1);

	/*
	 * If the -r option is not set, then remove the files...
	 */
	if (removefiles) {
		if (mon_tfsstat(file1)) {
			err = mon_tfsunlink(file1);
			if (err != TFS_OKAY)
				tfsdie(err);
		}
		if (mon_tfsstat(file2)) {
			err = mon_tfsunlink(file2);
			if (err != TFS_OKAY)
				tfsdie(err);
		}
		if (mon_tfsstat(TMPFILE)) {
			err = mon_tfsunlink(TMPFILE);
			if (err != TFS_OKAY)
				tfsdie(err);
		}
	}

	if (list)
		ls();

	/* All error cases checked above would have resulted in an exit
	 * of this application, so if we got here, the testing must
	 * have succeeded...
	 */
	mon_printf("TFS test on %s & %s PASSED\n",file1,file2);
	mon_appexit(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -