예제 #1
0
/* open two files and test write */
static BOOL cmdTest4(const char *tail)
{
	int fd1 = -1, fd2 = -1;

	MSGLN("open /a ...");
	if ((fd1 = uffs_open("/a", UO_RDWR | UO_CREATE)) < 0) {
		MSGLN("Can't open /a");
		goto fail_exit;
	}

	MSGLN("open /b ...");
	if ((fd2 = uffs_open("/b", UO_RDWR | UO_CREATE)) < 0) {
		MSGLN("Can't open /b");
		uffs_close(fd1);
		goto fail_exit;
	}

	MSGLN("write (1) to /a ...");
	uffs_write(fd1, "Hello,", 6);
	MSGLN("write (1) to /b ...");
	uffs_write(fd2, "Hello,", 6);
	MSGLN("write (2) to /a ...");
	uffs_write(fd1, "World.", 6);
	MSGLN("write (2) to /b ...");
	uffs_write(fd2, "World.", 6);
	MSGLN("close /a ...");
	uffs_close(fd1);
	MSGLN("close /b ...");
	uffs_close(fd2);

	return TRUE;

fail_exit:
	return TRUE;
}
예제 #2
0
파일: test_cmds.c 프로젝트: doushinide/uffs
/* open two files and test write */
static int cmd_t4(int argc, char *argv[])
{
	int fd1 = -1, fd2 = -1;

	MSGLN("open /a ...");
	if ((fd1 = uffs_open("/a", UO_RDWR | UO_CREATE)) < 0) {
		MSGLN("Can't open /a");
		goto fail_exit;
	}

	MSGLN("open /b ...");
	if ((fd2 = uffs_open("/b", UO_RDWR | UO_CREATE)) < 0) {
		MSGLN("Can't open /b");
		uffs_close(fd1);
		goto fail_exit;
	}

	MSGLN("write (1) to /a ...");
	uffs_write(fd1, "Hello,", 6);
	MSGLN("write (1) to /b ...");
	uffs_write(fd2, "Hello,", 6);
	MSGLN("write (2) to /a ...");
	uffs_write(fd1, "World.", 6);
	MSGLN("write (2) to /b ...");
	uffs_write(fd2, "World.", 6);
	MSGLN("close /a ...");
	uffs_close(fd1);
	MSGLN("close /b ...");
	uffs_close(fd2);

	return 0;

fail_exit:
	return -1;
}
예제 #3
0
static URET DoTest2(void)
{
	int fd = -1;
	URET ret = U_FAIL;
	char buf[100], buf_1[100];

	fd = uffs_open("/abc/", UO_RDWR|UO_DIR);
	if (fd < 0) {
		MSGLN("Can't open dir abc, err: %d", uffs_get_error());
		MSGLN("Try to create a new one...");
		fd = uffs_open("/abc/", UO_RDWR|UO_CREATE|UO_DIR);
		if (fd < 0) {
			MSGLN("Can't create new dir /abc/");
			goto exit_test;
		}
		else {
			uffs_close(fd);
		}
	}
	else {
		uffs_close(fd);
	}
	
	sprintf(buf, "123456789ABCDEF");
	fd = uffs_open("/abc/test.txt", UO_RDONLY);
	if( fd < 0 ){
		fd = uffs_open("/abc/test.txt", UO_RDWR|UO_CREATE);
		if (fd < 0) {
			MSGLN("Can't open /abc/test.txt");
			goto exit_test;
		}

		ret = uffs_write(fd, buf, strlen(buf));
		MSGLN("write %d bytes to file, content: %s", ret, buf);

		ret = uffs_seek(fd, 3, USEEK_SET);
		MSGLN("new file position: %d", ret);

		memset(buf_1, 0, sizeof(buf_1));
	}
	else{
		ret = uffs_seek(fd, 3, USEEK_SET);
		ret = uffs_read(fd, buf_1, 5);
	}
	MSGLN("read %d bytes, content: %s", ret, buf_1);

	if (memcmp(buf + 3, buf_1, 5) != 0) {
		ret = U_FAIL;
	}
	else {
		ret = U_SUCC;
	}

	uffs_close(fd);

exit_test:

	return ret;
}
예제 #4
0
int rand_file_close( int p )
{
	if( p<0) return -1;
	uffs_close( g_rand_file_info[p].ri_f );
	uffs_close( g_rand_file_info[p].f );
	rand_file_info_clear_item( p );
	return 0;
}
예제 #5
0
/* test appending file */
static BOOL cmdTest5(const char *tail)
{
	int fd = -1;
	URET ret;
	char buf[100];
	const char *name;

	if (!tail) {
		return FALSE;
	}

	name = cli_getparam(tail, NULL);

	fd = uffs_open(name, UO_RDWR|UO_APPEND);
	if (fd < 0) {
		MSGLN("Can't open %s", name);
		goto fail;
	}

	sprintf(buf, "append test...");
	ret = uffs_write(fd, buf, strlen(buf));
	if (ret != strlen(buf)) {
		MSGLN("write file failed, %d/%d", ret, strlen(buf));
	}
	else {
		MSGLN("write %d bytes to file, content: %s", ret, buf);
	}

	uffs_close(fd);

fail:

	return TRUE;
}
예제 #6
0
파일: test_cmds.c 프로젝트: doushinide/uffs
static URET test_append_file(const char *file_name, int size)
{
	int ret = U_FAIL;
	int fd = -1;

	if ((fd = uffs_open(file_name, UO_RDWR|UO_APPEND|UO_CREATE)) < 0) {
		MSGLN("Can't open file %s for append.", file_name);
		goto test_exit;
	}

	uffs_seek(fd, 0, USEEK_END);

	if (do_write_test_file(fd, size) == U_FAIL) {
		MSGLN("Write file %s failed.", file_name);
		goto test_failed;
	}
	ret = U_SUCC;

test_failed:
	uffs_close(fd);

test_exit:

	return ret;
}
예제 #7
0
int rand_file_open( char* fn )
{
	int p;
	int f, ri_f;
	int record_size, record_count;
	char tfn[64];
	rand_file_file_name( tfn, fn );
	p = rand_file_info_find_empty();
	if( p < 0 )
		return -1;
	g_rand_file_info[p].flag = 1;
	f = uffs_open( fn, UO_RDWR );
	if( f < 0 )
		goto rand_file_open_failed;
	ri_f = uffs_open( tfn, UO_RDWR );
	if( ri_f < 0 ){
		uffs_close( f );
		goto rand_file_open_failed;
	}
	uffs_read( ri_f, &record_size, 4 );
	uffs_read( ri_f, &record_count, 4 );
	g_rand_file_info[p].ri_f = ri_f;
	g_rand_file_info[p].f = f;
	g_rand_file_info[p].record_size = record_size;
	g_rand_file_info[p].record_count = record_count;
	return p;
rand_file_open_failed:	
	g_rand_file_info[p].flag = 0;
	return -1;
}
예제 #8
0
파일: test_cmds.c 프로젝트: doushinide/uffs
static URET test_write_file(const char *file_name, int pos, int size)
{
	int ret = U_FAIL;
	int fd = -1;

	if ((fd = uffs_open(file_name, UO_RDWR|UO_CREATE)) < 0) {
		MSGLN("Can't open file %s for write.", file_name);
		goto test_exit;
	}

	if (uffs_seek(fd, pos, USEEK_SET) != pos) {
		MSGLN("Can't seek file %s at pos %d", file_name, pos);
		goto test_failed;
	}

	if (do_write_test_file(fd, size) == U_FAIL) {
		MSGLN("Write file %s failed.", file_name);
		goto test_failed;
	}
	ret = U_SUCC;

test_failed:
	uffs_close(fd);

test_exit:

	return ret;
}
예제 #9
0
파일: test_cmds.c 프로젝트: doushinide/uffs
/**
 * close file
 *	t_close <fd>
 */
static int cmd_tclose(int argc, char *argv[])
{
	int fd;

	CHK_ARGC(2, 2);

	if (sscanf(argv[1], "%d", &fd) == 1) {
		return uffs_close(fd);
	}
	else
		return -1;
}
/** cat <file> [<offset>] [<size>] */
static int cmd_cat(int argc, char *argv[])
{
	int fd;
	const char *name = NULL;
	char buf[100];
	int start = 0, size = 0, printed = 0, n, len;
	int ret = -1;

	CHK_ARGC(2, 4);

	name = argv[1];

	if ((fd = uffs_open(name, UO_RDONLY)) < 0) {
		MSGLN("Can't open %s", name);
		goto fail;
	}

	if (argc > 2) {
		start = strtol(argv[2], NULL, 10);
		if (argc > 3) size = strtol(argv[3], NULL, 10);
	}

	if (start >= 0)
		uffs_seek(fd, start, USEEK_SET);
	else
		uffs_seek(fd, -start, USEEK_END);

	while (uffs_eof(fd) == 0) {
		len = uffs_read(fd, buf, sizeof(buf) - 1);
		if (len == 0) 
			break;
		if (len > 0) {
			if (size == 0 || printed < size) {
				n = (size == 0 ? len : (size - printed > len ? len : size - printed));
				buf[n] = 0;
				MSG("%s", buf);
				printed += n;
			}
			else {
				break;
			}
		}
	}
	MSG(TENDSTR);
	uffs_close(fd);

	ret = 0;
fail:

	return ret;
}
예제 #11
0
int rand_file_create( char* fn, int record_count, int record_size )
{
	char tfn[64];
	int i,f;
	unsigned char flag;
	char tbuf[ _d_max_rand_file_record_size ];
	// check file if exist.
	if( record_size > 128 )
		return -1;
	rand_file_file_name( tfn, fn );
	f = uffs_open( tfn, UO_RDONLY );
	if( f >= 0 )
		return -1;
	uffs_close( f );
	// create and write the base info
	f = uffs_open( tfn, UO_RDWR | UO_CREATE );
	if( f < 0 )
		return -1;
	uffs_write( f, &record_size, 4 );
	uffs_write( f, &record_count, 4 );
	memset( tbuf, 0xff, sizeof( tbuf ) );
	flag = 0xff;
	for( i=0; i<record_count; i++ ){
		uffs_write( f, &flag, 1 );
	}
	uffs_close(f);
	uffs_remove( fn );
	f = uffs_open( fn, UO_RDWR | UO_CREATE );
	if( f < 0 ){
		uffs_remove( tfn );
		return -1;
	}
	for( i=0; i<record_count; i++ ){
		uffs_write( f, tbuf, record_size );
	}
	uffs_close( f );
	return 0;
}
예제 #12
0
int property_set( char *name, char *val )
{
	char buf[96];
	int line_no = 0;
	int kc;
	char *ki[15];
	int tf, f;
	char *t_property_file_name = "/tmp_perperty.tr";
	int new_key = 1;

	f = uffs_open( g_property_file_name, UO_RDONLY );
	if( f == -1 ){
		f = uffs_open( g_property_file_name, UO_RDWR|UO_CREATE );
		if( f == -1 )
			return -1;
	}
	tf = uffs_open( t_property_file_name, UO_WRONLY|UO_CREATE );
	if( tf == -1 ){
		uffs_close( f );
		return -1;
	}
	while(1){
		if( uffs_read_line( f, buf, sizeof( buf ) ) < 0 )
			break;
		kc = analysis_string_to_strings_by_decollator( buf, "=", ki, 15 );
		if( kc !=2 )
			continue;
		if( s_strcmp( ki[0], name ) == 0 ){
			//int len;
			//strncpy( buf, &ki[1], sizeof(buf) );
			if( new_key == 0 )
				continue;
			sprintf( buf, "%s=%s\n", name, val );
			new_key = 0;
		}
		else	strcat( buf, "\n" );
		uffs_write( tf, buf, strlen( buf ) );
	}
	if( new_key == 1 ){
		uffs_close( tf );
		uffs_close( f );
		f = uffs_open( g_property_file_name, UO_RDWR|UO_CREATE );
		if( f == -1 )
			return -1;
		uffs_seek( f, 0, _SEEK_END );
		sprintf( buf, "%s=%s\n", name, val );
		uffs_write( f, buf, strlen( buf ) );
		uffs_close( f );
	}
	else{
		uffs_close( tf );
		uffs_close( f );
		uffs_remove( g_property_file_name );
		uffs_rename( t_property_file_name, g_property_file_name );
	}
	return 0;
}
예제 #13
0
파일: example.c 프로젝트: doushinide/uffs
int main(int argc, char *argv[])
{
	int version;
	int fd;
	char buf[128];

	api_client_init("192.168.0.103");

	version = uffs_version();
	printf("Version: %08X\n", version);

	fd = uffs_open("/test.txt", UO_RDWR|UO_CREATE);
	if (fd < 0) {
		printf("Can't create /test.txt\n");
		return -1;
	}
	
	sprintf(buf, "Hello, this is test\n");
	if (uffs_write(fd, buf, strlen(buf)) < 0) {
		printf("call uffs_write failed\n");
	}
	else {
		if (uffs_seek(fd, 7, USEEK_SET) != 7) {
			printf("call uffs_seek failed\n");
		}
		else {
			if (uffs_read(fd, buf, 4) != 4) {
				printf("call uffs_read failed\n");
			}
			else {
				if (memcmp(buf, "this", 4) != 0) {
					printf("uffs_read content not matched\n");
				}
				else {
					printf("everything is ok.\n");
				}
			}
		}
	}

	if (uffs_close(fd) < 0) {
		printf("uffs_close failed.\n");
	}

	return 0;
}
예제 #14
0
파일: test_cmds.c 프로젝트: doushinide/uffs
/* t_format : test format partition */
static int cmd_TestFormat(int argc, char *argv[])
{
	URET ret;
	const char *mount = "/";
	uffs_Device *dev;
	UBOOL force = U_FALSE;
	const char *test_file = "/a.txt";
	int fd;
	int rc = -1;

	if (argc > 1) {
		mount = argv[1];
		if (argc > 2 && strcmp(argv[2], "-f") == 0)
			force = U_TRUE;
	}

	fd = uffs_open(test_file, UO_RDWR | UO_CREATE);
	if (fd < 0) {
		MSGLN("can't create test file %s", test_file);
		goto ext;
	}

	MSGLN("Formating %s ... ", mount);

	dev = uffs_GetDeviceFromMountPoint(mount);
	if (dev == NULL) {
		MSGLN("Can't get device from mount point.");
		goto ext;
	}
	else {
		ret = uffs_FormatDevice(dev, force);
		if (ret != U_SUCC) {
			MSGLN("Format fail.");
		}
		else {
			MSGLN("Format succ.");
			rc = 0;
		}
		uffs_PutDevice(dev);
	}

	uffs_close(fd);  // this should fail on signature check !
ext:
	return rc;
}
예제 #15
0
파일: test_cmds.c 프로젝트: doushinide/uffs
static URET test_verify_file(const char *file_name, UBOOL noecc)
{
	int fd;
	int ret = U_FAIL;
	unsigned char buf[100];
	int i, pos, len;
	u8 x;

	if ((fd = uffs_open(file_name, (noecc ? UO_RDONLY|UO_NOECC : UO_RDONLY))) < 0) {
		MSGLN("Can't open file %s for read.", file_name);
		goto test_exit;
	}

	pos = 0;
	while (!uffs_eof(fd)) {
		len = uffs_read(fd, buf, sizeof(buf));
		if (len <= 0)
			goto test_failed;
		for (i = 0; i < len; i++) {
			x = (SEQ_INIT + pos + i) % SEQ_MOD_LEN;
			if (buf[i] != x) {
				MSGLN("Verify file %s failed at: %d, expect 0x%02x but got 0x%02x", file_name, pos + i, x, buf[i]);
				goto test_failed;
			}
		}
		pos += len;
	}

	if (pos != uffs_seek(fd, 0, USEEK_END)) {
		MSGLN("Verify file %s failed. invalid file length.", file_name);
		goto test_failed;
	}

	MSGLN("Verify file %s succ.", file_name);
	ret = U_SUCC;

test_failed:
	uffs_close(fd);

test_exit:

	return ret;
}
예제 #16
0
static BOOL cmdTestFormat(const char *tail)
{
	URET ret;
	const char *mount = "/";
	uffs_Device *dev;
	const char *next;
	UBOOL force = U_FALSE;
	const char *test_file = "/a.txt";
	int fd;

	if (tail) {
		mount = cli_getparam(tail, &next);
		if (next && strcmp(next, "-f") == 0)
			force = U_TRUE;
	}

	fd = uffs_open(test_file, UO_RDWR | UO_CREATE);
	if (fd < 0) {
		MSGLN("can't create test file %s", test_file);
		return U_TRUE;
	}

	MSGLN("Formating %s ... ", mount);

	dev = uffs_GetDeviceFromMountPoint(mount);
	if (dev == NULL) {
		MSGLN("Can't get device from mount point.");
	}
	else {
		ret = uffs_FormatDevice(dev, force);
		if (ret != U_SUCC) {
			MSGLN("Format fail.");
		}
		else {
			MSGLN("Format succ.");
		}
		uffs_PutDevice(dev);
	}

	uffs_close(fd);  // this should fail on signature check !

	return TRUE;
}
예제 #17
0
static URET test_verify_file(const char *file_name)
{
	int fd;
	int ret = U_FAIL;
	unsigned char buf[100];
	int i, pos, len;

	if ((fd = uffs_open(file_name, UO_RDONLY)) < 0) {
		MSGLN("Can't open file %s for read.", file_name);
		goto test_exit;
	}

	pos = 0;
	while (!uffs_eof(fd)) {
		len = uffs_read(fd, buf, sizeof(buf));
		if (len <= 0)
			goto test_failed;
		for (i = 0; i < len; i++) {
			if (buf[i] != (pos++ & 0xFF)) {
				pos--;
				MSGLN("Verify file %s failed at: %d, expect %x but got %x", file_name, pos, pos & 0xFF, buf[i]);
				goto test_failed;
			}
		}
	}

	if (pos != uffs_seek(fd, 0, USEEK_END)) {
		MSGLN("Verify file %s failed. invalid file length.", file_name);
		goto test_failed;
	}

	MSGLN("Verify file %s succ.", file_name);
	ret = U_SUCC;

test_failed:
	uffs_close(fd);

test_exit:

	return ret;
}
/** mkf <file> */
static int cmd_mkf(int argc, char *argv[])
{
	int fd;
	const char *name;
	int oflags = UO_RDWR | UO_CREATE;

	CHK_ARGC(2, 2);

	name = argv[1];
	fd = uffs_open(name, oflags);
	if (fd < 0) {
		MSGLN("Create %s fail, err: %d", name, uffs_get_error());
		return -1;
	}
	else {
		MSGLN("Create %s succ.", name);
		uffs_close(fd);
	}
	
	return 0;
}
예제 #19
0
파일: dfs_uffs.c 프로젝트: bobo8909/RTT
static int dfs_uffs_close(struct dfs_fd* file)
{
	int oflag;
	int fd;

	oflag = file->flags;
	if (oflag & DFS_O_DIRECTORY)
	{
		/* operations about dir */
		if (uffs_closedir((uffs_DIR *)(file->data)) < 0)
			return uffs_result_to_dfs(uffs_get_error());

		return 0;
	}
	/* regular file operations */
	fd = (int)(file->data);

	if (uffs_close(fd) == 0)
		return 0;

	return uffs_result_to_dfs(uffs_get_error());
}
예제 #20
0
int read_file( char* fn, char *buf, int max_buf_size)
{
#if _d_sd
	FILE *f;
	if (finit() != 0) 
		return;
	f = fopen ( fn , "r" );
	if( f == NULL )
		return 0;
	fread (buf,1,max_buf_size,f);
	fclose (f);
	return 1;
#elif _d_uffs
	int f;
	f = uffs_open ( fn , UO_RDONLY );
	if( f == -1 )
		return 0;
	uffs_read (f,buf,max_buf_size);
	uffs_close (f);
	return 1;
#endif
}
예제 #21
0
파일: test_cmds.c 프로젝트: doushinide/uffs
/* test create file, write file and read back */
static int cmd_t1(int argc, char *argv[])
{
	int fd;
	URET ret;
	char buf[100];
	const char *name;

	if (argc < 2) {
		return CLI_INVALID_ARG;
	}

	name = argv[1];

	fd = uffs_open(name, UO_RDWR|UO_CREATE|UO_TRUNC);
	if (fd < 0) {
		MSGLN("Can't open %s", name);
		goto fail;
	}

	sprintf(buf, "123456789ABCDEF");
	ret = uffs_write(fd, buf, strlen(buf));
	MSGLN("write %d bytes to file, content: %s", ret, buf);

	ret = uffs_seek(fd, 3, USEEK_SET);
	MSGLN("new file position: %d", ret);

	memset(buf, 0, sizeof(buf));
	ret = uffs_read(fd, buf, 5);
	MSGLN("read %d bytes, content: %s", ret, buf);

	uffs_close(fd);

	return 0;
fail:

	return -1;
}
예제 #22
0
파일: test_cmds.c 프로젝트: doushinide/uffs
/* test appending file */
static int cmd_t5(int argc, char *argv[])
{
	int fd = -1;
	URET ret;
	char buf[100];
	const char *name;

	if (argc < 2) {
		return CLI_INVALID_ARG;
	}

	name = argv[1];

	fd = uffs_open(name, UO_RDWR|UO_APPEND);
	if (fd < 0) {
		MSGLN("Can't open %s", name);
		goto fail;
	}

	sprintf(buf, "append test...");
	ret = uffs_write(fd, buf, strlen(buf));
	if (ret != strlen(buf)) {
		MSGLN("write file failed, %d/%d", ret, strlen(buf));
		ret = -1;
	}
	else {
		MSGLN("write %d bytes to file, content: %s", ret, buf);
		ret = 0;
	}

	uffs_close(fd);

	return ret;
fail:
	return -1;
}
예제 #23
0
/* test create file, write file and read back */
BOOL cmdTest1(const char *tail)
{
	int fd;
	URET ret;
	char buf[100];
	const char *name;

	if (!tail) {
		return FALSE;
	}

	name = cli_getparam(tail, NULL);

	fd = uffs_open(name, UO_RDWR|UO_CREATE|UO_TRUNC);
	if (fd < 0) {
		MSGLN("Can't open %s", name);
		goto fail;
	}

	sprintf(buf, "123456789ABCDEF");
	ret = uffs_write(fd, buf, strlen(buf));
	MSGLN("write %d bytes to file, content: %s", ret, buf);

	ret = uffs_seek(fd, 3, USEEK_SET);
	MSGLN("new file position: %d", ret);

	memset(buf, 0, sizeof(buf));
	ret = uffs_read(fd, buf, 5);
	MSGLN("read %d bytes, content: %s", ret, buf);

	uffs_close(fd);

fail:

	return TRUE;
}
/** cp <src> <des> */
static int cmd_cp(int argc, char *argv[])
{
	const char *src;
	const char *des;
	char buf[100];
	int fd1 = -1, fd2 = -1;
	int len;
	BOOL src_local = FALSE, des_local = FALSE;
	FILE *fp1 = NULL, *fp2 = NULL;
	int ret = -1;

	CHK_ARGC(3, 3);

	src = argv[1];
	des = argv[2];

	if (memcmp(src, "::", 2) == 0) {
		src += 2;
		src_local = TRUE;
	}
	if (memcmp(des, "::", 2) == 0) {
		des += 2;
		des_local = TRUE;
	}
	
	if (src_local) {
		if ((fp1 = fopen(src, "rb")) == NULL) {
			MSGLN("Can't open %s for copy.", src);
			goto fail_ext;
		}
	}
	else {
		if ((fd1 = uffs_open(src, UO_RDONLY)) < 0) {
			MSGLN("Can't open %s for copy.", src);
			goto fail_ext;
		}
	}

	if (des_local) {
		if ((fp2 = fopen(des, "wb")) == NULL) {
			MSGLN("Can't open %s for copy.", des);
			goto fail_ext;
		}
	}
	else {
		if ((fd2 = uffs_open(des, UO_RDWR|UO_CREATE|UO_TRUNC)) < 0) {
			MSGLN("Can't open %s for copy.", des);
			goto fail_ext;
		}
	}

	ret = 0;
	while (	(src_local ? (feof(fp1) == 0) : (uffs_eof(fd1) == 0)) ) {
		ret = -1;
		if (src_local) {
			len = fread(buf, 1, sizeof(buf), fp1);
		}
		else {
			len = uffs_read(fd1, buf, sizeof(buf));
		}
		if (len == 0) {
			ret = -1;
			break;
		}
		if (len < 0) {
			MSGLN("read file %s fail ?", src);
			break;
		}
		if (des_local) {
			if ((int)fwrite(buf, 1, len, fp2) != len) {
				MSGLN("write file %s fail ? ", des);
				break;
			}
		}
		else {
			if (uffs_write(fd2, buf, len) != len) {
				MSGLN("write file %s fail ? ", des);
				break;
			}
		}
		ret = 0;
	}

fail_ext:
	if (fd1 > 0)
		uffs_close(fd1);
	if (fd2 > 0)
		uffs_close(fd2);
	if (fp1) 
		fclose(fp1);
	if (fp2)
		fclose(fp2);

	return ret;
}
예제 #25
0
파일: test_cmds.c 프로젝트: doushinide/uffs
/**
 * usage: t_pfs <start> <n>
 *
 * for example: t_pfs /x/ 100
 *
 * This test case performs:
 *   1) create <n> files under <start>, write full file name as file content
 *   2) list files under <start>, check files are all listed once
 *   3) check file content aganist file name
 *   4) delete files on success
 */
static int cmd_TestPopulateFiles(int argc, char *argv[])
{
	const char *start = "/";
	int count = 80;
	int i, fd, num;
	char name[128];
	char buf[128];
	uffs_DIR *dirp;
	struct uffs_dirent *ent;
	unsigned long bitmap[50] = {0};	// one bit per file, maximu 32*50 = 1600 files
	UBOOL succ = U_TRUE;

#define SBIT(n) bitmap[(n)/(sizeof(bitmap[0]) * 8)] |= (1 << ((n) % (sizeof(bitmap[0]) * 8)))
#define GBIT(n) (bitmap[(n)/(sizeof(bitmap[0]) * 8)] & (1 << ((n) % (sizeof(bitmap[0]) * 8))))

	if (argc > 1) {
		start = argv[1];
		if (argc > 2) {
			count = strtol(argv[2], NULL, 10);
		}
	}

	if (count > sizeof(bitmap) * 8)
		count = sizeof(bitmap) * 8;

	for (i = 0, fd = -1; i < count; i++) {
		sprintf(name, "%sFile%03d", start, i);
		fd = uffs_open(name, UO_RDWR|UO_CREATE|UO_TRUNC);
		if (fd < 0) {
			MSGLN("Create file %s failed", name);
			break;
		}
		if (uffs_write(fd, name, strlen(name)) != strlen(name)) { // write full path name to file
			MSGLN("Write to file %s failed", name);
			uffs_close(fd);
			break;
		}
		uffs_close(fd);
	}

	if (i < count) {
		// not success, need to clean up
		for (; i >= 0; i--) {
			sprintf(name, "%sFile%03d", start, i);
			if (uffs_remove(name) < 0)
				MSGLN("Delete file %s failed", name);
		}
		succ = U_FALSE;
		goto ext;
	}

	MSGLN("%d files created.", count);

	// list files
	dirp = uffs_opendir(start);
	if (dirp == NULL) {
		MSGLN("Can't open dir %s !", start);
		succ = U_FALSE;
		goto ext;
	}
	ent = uffs_readdir(dirp);
	while (ent && succ) {

		if (!(ent->d_type & FILE_ATTR_DIR) &&					// not a dir
			ent->d_namelen == strlen("File000") &&				// check file name length
			memcmp(ent->d_name, "File", strlen("File")) == 0) {	// file name start with "File"
			
			MSGLN("List entry %s", ent->d_name);

			num = strtol(ent->d_name + 4, NULL, 10);
			if (GBIT(num)) {
				// file already listed ?
				MSGLN("File %d listed twice !", ent->d_name);
				succ = U_FALSE;
				break;
			}
			SBIT(num);

			// check file content
			sprintf(name, "%s%s", start, ent->d_name);
			fd = uffs_open(name, UO_RDONLY);
			if (fd < 0) {
				MSGLN("Open file %d for read failed !", name);
			}
			else {
				memset(buf, 0, sizeof(buf));
				num = uffs_read(fd, buf, sizeof(buf));
				if (num != strlen(name)) {
					MSGLN("%s Read data length expect %d but got %d !", name, strlen(name), num);
					succ = U_FALSE;
				}
				else {
					if (memcmp(name, buf, num) != 0) {
						MSGLN("File %s have wrong content '%s' !", name, buf);
						succ = U_FALSE;
					}
				}
				uffs_close(fd);
			}
		}
		ent = uffs_readdir(dirp);
	}
	uffs_closedir(dirp);

	// check absent files
	for (i = 0; i < count; i++) {
		if (GBIT(i) == 0) {
			sprintf(name, "%sFile%03d", start, i);
			MSGLN("File %s not listed !", name);
			succ = U_FALSE;
		}
	}
	
	// delete files if pass the test
	for (i = 0; succ && i < count; i++) {
		sprintf(name, "%sFile%03d", start, i);
		if (uffs_remove(name) < 0) {
			MSGLN("Delete file %s failed", name);
			succ = U_FALSE;
		}
	}

ext:
	MSGLN("Populate files test %s !", succ ? "SUCC" : "FAILED");
	return succ ? 0 : -1;

}
예제 #26
0
/*!
 * \brief Copy the source file to a destination file
 * \param[in] srcFileName Source file name
 * \param[in] dstFileName Destination file name
 * \param[in] io IO handler for output
 * \return Error code, ERR_OK for success.
 */
byte UFFS_FAT_CopyFile(const byte *srcFileName, const byte *dstFileName, const CLS1_StdIOType *io)
{
	bool sourceUffs = pdTRUE;	///< source is uffs (otherwise FAT)
	bool destUffs = pdTRUE;		///< destination is uffs (otherwise FAT)
	int fd1=-1, fd2=-1;			// uffs file pointers
	FAT1_FIL fsrc, fdst;  	// FAT file objects
	FAT1_FRESULT fres;		// FAT result
	uint8_t buffer[32];   /* copy buffer */
	UINT br, bw, bt=0;          /* file read/write counters */
	byte res =  ERR_OK;
	
	if( srcFileName[1] == ':')
		sourceUffs = pdFALSE;
	if( dstFileName[1] == ':')
	{
		destUffs = pdFALSE;
		if (FAT1_isWriteProtected() || FAT1_FS_READONLY) 
		{
			MSGLN("destination FAT disk is write protected!");
			return ERR_FAILED;
		}
	}

	/* open source file */
	
	if(sourceUffs)
	{
		fd1 = uffs_open((char*)srcFileName, UO_RDONLY);
		if (fd1  < 0) {
			MSGLN("open source file failed");
			return ERR_FAILED;
		}		
	}
	else
	{
		fres = FAT1_open(&fsrc, (char*)srcFileName, FA_OPEN_EXISTING | FA_READ);
		if (fres != FR_OK) 
			{
				MSGLN("open source file failed. Result=%d", fres);
				return ERR_FAILED;
			}
	}
	
	/* create destination file */
	
	if(destUffs)
	{
		fd2 = uffs_open((char*)dstFileName, UO_CREATE|UO_WRONLY);
		if (fd2  < 0) {
			MSGLN("open destination file failed");
			return ERR_FAILED;
		}		
	}
	else
	{
		fres = FAT1_open(&fdst, (char*)dstFileName, FA_CREATE_ALWAYS | FA_WRITE);
		if (fres != FR_OK) {
			MSGLN("open destination file failed. Result=%d", fres);
			return ERR_FAILED;
		}
	}
	
	/* now copy source to destination */
	
	for (;;) 
	{
		if(sourceUffs)
		{
			br = uffs_read(fd1, buffer, sizeof(buffer));
		}
		else
		{
			fres = FAT1_read(&fsrc, buffer, sizeof(buffer), &br);
			if (fres != FR_OK) 
			{
				MSGLN("reading source file failed. Result=%d", fres);
				res = ERR_FAILED;
				break;
			}
		}
		bt += br;
		if (br == 0) 
		{ /* EOF */
			break; /* get out of loop */
		}
		if(destUffs)
		{
			bw = uffs_write(fd2, buffer, br);
		}
		else
		{
			fres = FAT1_write(&fdst, buffer, br, &bw);
			if (fres != ERR_OK) {
				MSGLN("writing destination file failed. Result=%d", fres);
				res = ERR_FAILED;
				break;
			}			
		}
		if (bw < br) {
			MSGLN("failed writing destination file, or disk full");
			res = ERR_FAILED;
			break;
		}
	} /* for */
	
	/* close all files */
	
	if(sourceUffs)
		uffs_close(fd1);
	else
	{
		fres = FAT1_close(&fsrc);
		if (fres != FR_OK) 
		{
			MSGLN("closing source file failed. Result=%d", fres);
			res = ERR_FAILED;
		}
	}
	if(destUffs)
		uffs_close(fd2);
	else
	{
		fres = FAT1_close(&fdst);
		if (fres != FR_OK) {
			MSGLN("closing destination file failed. Result=%d", fres);
			res = ERR_FAILED;
		}
	}
	MSGLN("%u bytes copied.", bt);
	return res;
}
예제 #27
0
static byte UFFS_Benchmark(const unsigned char *cmd, const CLS1_ConstStdIOType *io)
{
	uint16_t i, j;
	uint8_t read_buf[10];
	TIMEREC time, startTime;
	int32_t start_mseconds, mseconds;

	int fd, fd2;

	/* write benchmark */
	MSGLN("Benchmark: open file, write 10k times 10 bytes (100'000 bytes), close file:");
	MSGLN("Deleting existing benchmark files...");
	uffs_remove("/bench.txt");
	uffs_remove("/copy.txt");

	MSGLN("Creating benchmark file...");
	(void)TmDt1_GetTime(&startTime);
	fd = uffs_open("/bench.txt", UO_CREATE|UO_WRONLY);
	if (fd  < 0) {
		MSGLN("*** Failed opening benchmark file!");
		return ERR_FAILED;
	}
	for(i=0;i<10000;i++) {
		if (uffs_write(fd, "benchmark ", sizeof("benchmark ")-1) <= 0) {
			MSGLN("*** Failed writing file!");
			uffs_close(fd);
			return ERR_FAILED;
		}
	}
	uffs_close(fd);
	(void)TmDt1_GetTime(&time);
	start_mseconds = startTime.Hour*60*60*1000 + startTime.Min*60*1000 + startTime.Sec*1000 + startTime.Sec100*10;
	mseconds = time.Hour*60*60*1000 + time.Min*60*1000 + time.Sec*1000 + time.Sec100*10 - start_mseconds;
	MSGLN("%ld ms needed for creating.",mseconds);

	/* read benchmark */
	MSGLN("Reading benchmark file...");
	(void)TmDt1_GetTime(&startTime);
	fd = uffs_open("/bench.txt", UO_RDONLY);
	if (fd < 0) {
		MSGLN("*** Failed opening benchmark file!");
		return ERR_FAILED;
	}
	for(i=0;i<10000;i++) {
		if (uffs_read(fd, read_buf, sizeof(read_buf)) <= 0) {
			MSGLN("*** Failed reading file!");
			uffs_close(fd);
			return ERR_FAILED;
		}
	}
	uffs_close(fd);
	(void)TmDt1_GetTime(&time);
	start_mseconds = startTime.Hour*60*60*1000 + startTime.Min*60*1000 + startTime.Sec*1000 + startTime.Sec100*10;
	mseconds = time.Hour*60*60*1000 + time.Min*60*1000 + time.Sec*1000 + time.Sec100*10 - start_mseconds;
	MSGLN("%ld ms needed for reading.",mseconds);

	/* copy benchmark */
	MSGLN("Copy file (100'000 bytes)...");
	(void)TmDt1_GetTime(&startTime);
	
	fd2 = uffs_open("/copy.txt", UO_CREATE|UO_WRONLY);
	if (fd2  < 0) {
		MSGLN("*** Failed opening copy file!");
		return ERR_FAILED;
	}
	fd = uffs_open("/bench.txt", UO_RDONLY);
	if (fd < 0) {
		MSGLN("*** Failed opening benchmark file!");
		return ERR_FAILED;
	}
	i = 0;
	do
	{
		j = uffs_read(fd, read_buf, sizeof(read_buf));
		i += j;
		if (uffs_write(fd2, read_buf, j) < j) {
			MSGLN("*** Failed writing file!");
			uffs_close(fd);
			uffs_close(fd2);
			return ERR_FAILED;
		}
	} while( j > 0);
	uffs_close(fd);
	uffs_close(fd2);
	
	(void)TmDt1_GetTime(&time);
	start_mseconds = startTime.Hour*60*60*1000 + startTime.Min*60*1000 + startTime.Sec*1000 + startTime.Sec100*10;
	mseconds = time.Hour*60*60*1000 + time.Min*60*1000 + time.Sec*1000 + time.Sec100*10 - start_mseconds;
	MSGLN("%ld ms needed for copy.",mseconds);
	
	return ERR_OK;
}
예제 #28
0
//uffs拷贝函数,参数之间加空格
//需要从elm拷贝到uffs时(跨文件系统),参数名称前加::
//例如uffs_copy("::/01.hdc  /dir1/01.hdc")
//上例从SD卡拷贝一个文件01.hdc到flash中,
//也可用dfs的函数,那样就不用考虑是否跨文件系统了.
int uffs_copy(const char *tail)
{
	const char *src;
	const char *des;
	char buf[100];
	int fd1=-1, fd2=-1;
	int len;
	int src_local = FALSE, des_local = FALSE;
	int fd3=-1, fd4=-1;

	if(!tail) 
		return FALSE;

	src = cli_getparam(tail, &des);

	if(!des)
		return FALSE;

	if(memcmp(src, "::", 2) == 0) 
	{
		src += 2;
		src_local = TRUE;
	}
	if(memcmp(des, "::", 2) == 0) 
	{
		des += 2;
		des_local = TRUE;
	}
	
	if(src_local) 
	{	
		//if((fp1 = fopen(src, "rb")) == NULL) 
		if((fd3 = open(src,O_RDONLY,0)) < 0)
		{
			uffs_Perror(UFFS_ERR_NORMAL, "Can't open %s for copy.", src);
			goto fail_ext;
		}
	}
	else 
	{
		if((fd1 = uffs_open(src, UO_RDONLY)) < 0) 
		{
			uffs_Perror(UFFS_ERR_NORMAL, "Can't open %s for copy.", src);
			goto fail_ext;
		}
	}

	if(des_local) 
	{
		if((fd4 = open(des,O_WRONLY | O_CREAT,0)) < 0) 
		{
			uffs_Perror(UFFS_ERR_NORMAL, "Can't open %s for copy.", des);
			goto fail_ext;
		}
	}
	else 
	{	
		if((fd2 = uffs_open(des, UO_RDWR|UO_CREATE|UO_TRUNC)) < 0) 
		{
			uffs_Perror(UFFS_ERR_NORMAL, "Can't open %s for copy.", des);
			goto fail_ext;
		}
	}
	
	uffs_Perror(UFFS_ERR_NORMAL, "copy %s to %s... ",src,des);

	while((src_local ? (1) : (uffs_eof(fd1) == 0))) 
	{
		if(src_local) 
		{
			len = read(fd3, buf, sizeof(buf));
		}
		else 
		{
			len = uffs_read(fd1, buf, sizeof(buf));
		}
		if(len == 0) 
			break;
		if(len < 0) 
		{
			uffs_Perror(UFFS_ERR_NORMAL, "read file %s fail!", src);
			break;
		}
		if(des_local) 
		{
			if(write(fd4, buf, len) != len) 
			{
				uffs_Perror(UFFS_ERR_NORMAL, "write file %s fail!", des);
				break;
			}
		}
		else 
		{
			if(uffs_write(fd2, buf, len) != len) 
			{
				uffs_Perror(UFFS_ERR_NORMAL, "write file %s fail ?", des);
				break;
			}
		}
	}
	uffs_Perror(UFFS_ERR_NORMAL, "succ.");
fail_ext:
	if(fd1 > 0)
		uffs_close(fd1);
	if(fd2 > 0)
		uffs_close(fd2);
	if(fd3 > 0) 
		close(fd3);
	if(fd4 > 0)
		close(fd4);

	return TRUE;
}
예제 #29
0
파일: test_cmds.c 프로젝트: doushinide/uffs
/*
 * This verify the bug fixed by commit dede97b1.
 * The bug caused a clone buf failure and UFFS complain something like "no enough free pages for clone!".
 */
static int cmd_t_dede97b1(int argc, char *argv[])
{
	// assume:
	//	total page buf: 40
	//	page size: 512
	//  pages per block: 32
	//  spare size: 16
	
#define LEN_A (508 * 30) // 30 pages
#define LEN_B (508 * 10) // 10 pages

	int fd = -1;
	URET ret = -1;
	const int START_A = 508 * 31;			// the second block
	const int START_B = START_A + 508 * 32; // the third block
	const char *name;
	char buf_a[LEN_A];
	char buf_b[LEN_B];

	uffs_Device *dev;
	const char *mount = "/";

	if (argc < 2) {
		return CLI_INVALID_ARG;
	}

	name = argv[1];

	fd = uffs_open(name, UO_RDWR);
	if (fd < 0) {
		MSGLN("Can't open %s", name);
		goto ext;
	}

	///
	/// READ A
	///

	ret = uffs_seek(fd, START_A, USEEK_SET);
	if (ret != START_A) {
		MSGLN("lseek return %d\n", ret);
		goto ext;
	}

	sprintf(buf_a, "start test, read %d bytes...", LEN_A);
	ret = uffs_read(fd, buf_a, LEN_A);
	if (ret != LEN_A) {
		MSGLN("read file failed, ret = %d", ret);
		ret = -1;
		goto ext;
	}
	else {
		MSGLN("read %d bytes succ.", ret);
	}

	MSGLN("now print buf status:");
	dev = uffs_GetDeviceFromMountPoint(mount);
	if (dev == NULL) {
		MSGLN("Can't get device from mount point %s", mount);
		ret = -1;
		goto ext;
	}
	uffs_BufInspect(dev);
	uffs_PutDevice(dev);

	///
	/// READ B
	///

	ret = uffs_seek(fd, START_B, USEEK_SET);
	if (ret != START_B) {
		MSGLN("lseek return %d\n", ret);
		goto ext;
	}

	sprintf(buf_b, "start test, read %d bytes...", LEN_B);
	ret = uffs_read(fd, buf_b, LEN_B);
	if (ret != LEN_B) {
		MSGLN("read file failed, ret = %d", ret);
		ret = -1;
		goto ext;
	}
	else {
		MSGLN("read %d bytes succ.", ret);
	}

	MSGLN("now print buf status:");
	dev = uffs_GetDeviceFromMountPoint(mount);
	if (dev == NULL) {
		MSGLN("Can't get device from mount point %s", mount);
		ret = -1;
		goto ext;
	}
	uffs_BufInspect(dev);
	uffs_PutDevice(dev);


	///
	/// WRITE A
	///

	ret = uffs_seek(fd, START_A, USEEK_SET);
	if (ret != START_A) {
		MSGLN("lseek return %d\n", ret);
		goto ext;
	}

	MSGLN("now try write %d bytes...", LEN_A);
	ret = uffs_write(fd, buf_a, LEN_A);
	if (ret != LEN_A) {
		MSGLN("write %d bytes failed, return %d\n", LEN_A, ret);
		ret = -1;
		goto ext;
	}

	MSGLN("now print buf status again:");
	dev = uffs_GetDeviceFromMountPoint(mount);
	if (dev == NULL) {
		MSGLN("Can't get device from mount point %s", mount);
		ret = -1;
		goto ext;
	}
	uffs_BufInspect(dev);
	uffs_PutDevice(dev);

	///
	/// WRITE B
	///

	ret = uffs_seek(fd, START_B, USEEK_SET);
	if (ret != START_B) {
		MSGLN("lseek return %d\n", ret);
		goto ext;
	}

	MSGLN("now try write %d bytes...", LEN_B);
	ret = uffs_write(fd, buf_b, LEN_B);
	if (ret != LEN_B) {
		MSGLN("write %d bytes failed, return %d\n", LEN_B, ret);
		ret = -1;
		goto ext;
	}

	MSGLN("now print buf status again:");
	dev = uffs_GetDeviceFromMountPoint(mount);
	if (dev == NULL) {
		MSGLN("Can't get device from mount point %s", mount);
		ret = -1;
		goto ext;
	}
	uffs_BufInspect(dev);
	uffs_PutDevice(dev);

	ret = 0;
	MSGLN("test completed.");

ext:
	if (fd >= 0)
		uffs_close(fd);

	return ret;
}