Esempio n. 1
0
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;
}
Esempio n. 2
0
static int dfs_uffs_write(struct dfs_fd* file,
                   const void* buf,
                   size_t len)
{
    int fd;
    int char_write;

    fd = (int)(file->data);

    char_write = uffs_write(fd, buf, len);
    if (char_write < 0)
        return uffs_result_to_dfs(uffs_get_error());

    /* update position */
    file->pos = uffs_seek(fd, 0, USEEK_CUR);
    return char_write;
}
Esempio n. 3
0
int rand_file_add( int p, char *buf, int buf_size )
{
	char tbuf[ _d_max_rand_file_record_size ];
	int size;
	int pos;
	struct _s_rand_file_info *rfi = &g_rand_file_info[ p ];
	if( p<0) return -1;
	memset( tbuf, 0xff, sizeof( tbuf ) );
	size = min( _d_max_rand_file_record_size, buf_size );
	memcpy( tbuf, buf, size );
	pos = rand_file_find_empty_record_pos( p );
	if( pos < 0 )
		return -1;
	uffs_seek( rfi->f, pos * rfi->record_size, USEEK_SET );
	uffs_write( rfi->f, tbuf, size );
	rand_file_set_record_flag( p, pos, 0x7f);
	return pos;
}
Esempio n. 4
0
static URET do_write_test_file(int fd, int size)
{
	long pos;
	unsigned char buf[100];
	int len;

	while (size > 0) {
		pos = uffs_seek(fd, 0, USEEK_CUR);
		len = (size > sizeof(buf) ? sizeof(buf) : size);
		memcp_seq(buf, len, pos);
		if (uffs_write(fd, buf, len) != len) {
			MSGLN("Write file failed, size %d at %d", len, pos);
			return U_FAIL;
		}
		size -= len;
	}

	return U_SUCC;
}
Esempio n. 5
0
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;
}
Esempio n. 6
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;
}
Esempio n. 7
0
/**
 * seek file pointer
 *	t_seek <fd> <offset> [<origin>]
 * if success, $1 = file position after seek
 */
static int cmd_tseek(int argc, char *argv[])
{
	int origin = USEEK_SET;
	int offset;
	int fd;
	int ret;

	CHK_ARGC(3, 4);

	if (sscanf(argv[1], "%d", &fd) != 1 ||
		sscanf(argv[2], "%d", &offset) != 1)
	{
		return CLI_INVALID_ARG;
	}

	if (argc > 3) {
		switch(argv[3][0]) {
		case 's':
			origin = USEEK_SET;
			break;
		case 'c':
			origin = USEEK_CUR;
			break;
		case 'e':
			origin = USEEK_END;
			break;
		default:
			return CLI_INVALID_ARG;
		}
	}

	ret = uffs_seek(fd, offset, origin);
	if (ret >= 0) {
		cli_env_set('1', ret);
		return 0;
	}
	else {
		return -1;
	}
}
Esempio n. 8
0
static URET do_write_test_file(int fd, int size)
{
	long pos;
	unsigned char buf[100];
	unsigned char data;
	int i, len;

	while (size > 0) {
		pos = uffs_seek(fd, 0, USEEK_CUR);
		len = (size > sizeof(buf) ? sizeof(buf) : size);
		data = pos & 0xFF;
		for (i = 0; i < len; i++, data++) {
			buf[i] = data;
		}
		if (uffs_write(fd, buf, len) != len) {
			MSGLN("Write file failed, size %d at %d", len, pos);
			return U_FAIL;
		}
		size -= len;
	}

	return U_SUCC;
}
Esempio n. 9
0
/* 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;
}
Esempio n. 10
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;
}
Esempio n. 11
0
static int dfs_uffs_open(struct dfs_fd* file)
{
	int fd;
	int oflag, mode;
	char * file_path;

	oflag = file->flags;
	if (oflag & DFS_O_DIRECTORY)   /* operations about dir */
	{
		uffs_DIR * dir;

		if (oflag & DFS_O_CREAT)   /* create a dir*/
		{
			if (uffs_mkdir(file->path) < 0)
				return uffs_result_to_dfs(uffs_get_error());
		}
		/* open dir */
		file_path = rt_malloc(FILE_PATH_MAX);
		if(file_path == RT_NULL)
			return -DFS_STATUS_ENOMEM;			

		if (file->path[0] == '/' && !(file->path[1] == 0))
			rt_snprintf(file_path, FILE_PATH_MAX, "%s/", file->path);
		else
		{
			file_path[0] = '/';
			file_path[1] = 0;
		}

		dir = uffs_opendir(file_path);

		if (dir == RT_NULL)
		{
			rt_free(file_path);			
			return uffs_result_to_dfs(uffs_get_error());
		}
		/* save this pointer,will used by  dfs_uffs_getdents*/
		file->data = dir;
		rt_free(file_path);
		return DFS_STATUS_OK;
	}
	/* regular file operations */
	/* int uffs_open(const char *name, int oflag, ...); what is this?
	 * uffs_open can open dir!!  **/
	mode = 0;
	if (oflag & DFS_O_RDONLY) mode |= UO_RDONLY;
	if (oflag & DFS_O_WRONLY) mode |= UO_WRONLY;
	if (oflag & DFS_O_RDWR)   mode |= UO_RDWR;
	/* Opens the file, if it is existing. If not, a new file is created. */
	if (oflag & DFS_O_CREAT) mode |= UO_CREATE;
	/* Creates a new file. If the file is existing, it is truncated and overwritten. */
	if (oflag & DFS_O_TRUNC) mode |= UO_TRUNC;
	/* Creates a new file. The function fails if the file is already existing. */
	if (oflag & DFS_O_EXCL) mode |= UO_EXCL;

	fd = uffs_open(file->path, mode);
	if (fd < 0)
	{
		return uffs_result_to_dfs(uffs_get_error());
	}

	/* save this pointer, it will be used when calling read()£¬write(),
	 * flush(), seek(), and will be free when calling close()*/

	file->data = (void *)fd;
	file->pos  = uffs_seek(fd, 0, USEEK_CUR);
	file->size = uffs_seek(fd, 0, USEEK_END);
	uffs_seek(fd, file->pos, USEEK_SET);

	if (oflag & DFS_O_APPEND)
	{
		file->pos = uffs_seek(fd, 0, USEEK_END);
	}
	return 0;
}
Esempio n. 12
0
/*
 * 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;
}