예제 #1
0
/*
 * 函数功能: 列出文件清单
 * 输入参数: name:分区名称
 * 返回参数: 成功:TRUE,失败:rt_false
 */
int uffs_ls(const char *name)
{
	uffs_DIR *dirp;
	struct uffs_dirent *ent;
	struct uffs_stat stat_buf;
	int count = 0;
	char buf[MAX_FILENAME_LENGTH+2];
	char *sub;

	if(name == NULL) 
	{
		return FALSE;
	}

	dirp = uffs_opendir(name); //会获得一个uffs_DIR实例

	if(dirp == NULL) 
	{
		rt_kprintf("Can't open '%s' for list\n", name);
	}
	else 
	{
		rt_kprintf("%-16s%-8s%-8s%-8s\n","name","type","size","serial");
		rt_kprintf("-----------------------------------------\n");
		ent = uffs_readdir(dirp);
		while(ent) 
		{
			rt_kprintf("%-16s", ent->d_name);
			strcpy(buf, name);
			sub = buf;
			if(name[strlen(name)-1] != '/')
				sub = strcat(buf, "/");
			sub = strcat(sub, ent->d_name);

			if(ent->d_type & FILE_ATTR_DIR) 
			{
				sub = strcat(sub, "/");
				rt_kprintf("%-8s", "<DIR>");
				rt_kprintf("%-8d", CountFileUnder(sub));
			}
			else
			{
				uffs_stat(sub, &stat_buf);
				rt_kprintf("%-8s", "");
				rt_kprintf("%-8d", stat_buf.st_size);
			}
			rt_kprintf("%-8d\n", ent->d_ino);
			count++;
			ent = uffs_readdir(dirp);
		}
		
		if(dirp != NULL)
			uffs_closedir(dirp);

		rt_kprintf("Total: %d objects.\n", count);
	}

	return TRUE;
}
static int CountObjectUnder(const char *dir)
{
	int count = 0;
	uffs_DIR *dirp;

	dirp = uffs_opendir(dir);
	if (dirp) {
		while (uffs_readdir(dirp) != NULL)
			count++;
		uffs_closedir(dirp);
	}
	return count;
}
/** ls [<dir>] */
static int cmd_ls(int argc, char *argv[])
{
	uffs_DIR *dirp;
	struct uffs_dirent *ent;
	struct uffs_stat stat_buf;
	int count = 0;
	char buf[MAX_PATH_LENGTH+2];
	const char *name = "/";
	char *sub;
	int ret = 0;

	CHK_ARGC(1, 2);

	if (argc > 1)
		name = argv[1];

	dirp = uffs_opendir(name);
	if (dirp == NULL) {
		MSGLN("Can't open '%s' for list", name);
		ret = -1;
	}
	else {
		MSG("------name-----------size---------serial-----" TENDSTR);
		ent = uffs_readdir(dirp);
		while (ent) {
			MSG("%9s", ent->d_name);
			strcpy(buf, name);
			sub = buf;
			if (name[strlen(name)-1] != '/')
				sub = strcat(buf, "/");
			sub = strcat(sub, ent->d_name);
			if (ent->d_type & FILE_ATTR_DIR) {
				sub = strcat(sub, "/");
				MSG("/  \t<%8d>", CountObjectUnder(sub));
			}
			else {
				uffs_stat(sub, &stat_buf);
				MSG("   \t %8d ", stat_buf.st_size);
			}
			MSG("\t%6d" TENDSTR, ent->d_ino);
			count++;
			ent = uffs_readdir(dirp);
		}
		
		uffs_closedir(dirp);

		MSG("Total: %d objects." TENDSTR, count);
	}

	return ret;
}
예제 #4
0
/// dir is the DOS equivalent of ls
static byte DirCmd(const unsigned char *cmd, const CLS1_ConstStdIOType *io) {
	/* precondition: cmd starts with "dir" */
	uffs_DIR *curdir = cwd;
	char d_name[UFFS_PATHSIZE];
	byte res = ERR_OK;
	
	if (*(cmd + sizeof("dir") - 1) == ' ')
	{ /* space after "dir": read name */
		if (UTIL1_ReadEscapedName(cmd + sizeof("dir"),
				(unsigned char*)d_name, sizeof(d_name), NULL,
				NULL, NULL) == ERR_OK)
		{
			curdir = uffs_opendir(d_name);
			res = uffs_get_error();
		}
		else
		{
			CLS1_SendStr((unsigned char*) "reading directory name failed!\r\n",
					io->stdErr);
			res = ERR_FAILED;
		}
	}
	// else use current directory
	struct uffs_dirent *entry;
	
	if (curdir == NULL)
	{
		CLS1_SendStr((unsigned char*) "can't open dir\r\n", io->stdErr);
		res = ERR_FAILED;
	}
	uffs_rewinddir(curdir);

	while ((entry = uffs_readdir(curdir)))
	{
		MSGLN( "%s", entry->d_name);
	}
	// unless it was cwd close it
	if (*(cmd + sizeof("dir") - 1) == ' ')
		uffs_closedir(curdir);
	return res;
}
예제 #5
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());
}
예제 #6
0
int dfs_uffs_close(struct dfs_fd* fd)   
{
	int ret=U_SUCC;

	if (fd->type == FT_DIRECTORY)
	{
		uffs_DIR* dirp;
		dirp = (uffs_DIR*)(fd->data);
		RT_ASSERT(dirp != RT_NULL);

		ret = uffs_closedir(dirp);
	}
	else if (fd->type == FT_REGULAR)
	{
		uffs_Object* fp = (uffs_Object*)(fd->data);
		RT_ASSERT(fd != RT_NULL);

		ret = uffs_CloseObject(fp);
		/* release object hander */
		uffs_PutObject(fp);
	}

	return ret;   
}      
예제 #7
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;

}