Beispiel #1
0
static byte CdCmd(const unsigned char *cmd, const CLS1_ConstStdIOType *io) {
  /* precondition: cmd starts with "cd" */
	
	// TODO: support relative pathnames 
	
	char d_name[UFFS_PATHSIZE];
	byte res = ERR_OK;
	
	if (*(cmd + sizeof("cd") - 1) == ' ')
	{ /* space after "cd": read name */
		if (UTIL1_ReadEscapedName(cmd + sizeof("cd"),
				(unsigned char*)d_name, sizeof(d_name), NULL,
				NULL, NULL) == ERR_OK)
		{
			cwd = uffs_opendir(d_name);
			res = uffs_get_error();
		}
		else
		{
			CLS1_SendStr((unsigned char*) "reading directory name failed!\r\n",
					io->stdErr);
			res = ERR_FAILED;
		}
	}
	
	/* print current directory */
	CLS1_SendStr((unsigned char*)cwd->obj->name, io->stdOut);
	CLS1_SendStr((unsigned char*) "\r\n", io->stdOut);
	
	return res;
}
Beispiel #2
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;
}
Beispiel #5
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;
}
Beispiel #6
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;
}
Beispiel #7
0
/**
 * 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;

}
Beispiel #8
0
int dfs_uffs_open(struct dfs_fd* fd)   
{
	if (fd->flags & DFS_O_DIRECTORY)
	{	/* directory */
		uffs_DIR* dirp;
		int oflag = UO_DIR;

		if (fd->flags & DFS_O_CREAT) oflag |= UO_CREATE;
		if (fd->flags & DFS_O_RDONLY) oflag |= UO_RDONLY;
		if (fd->flags & DFS_O_WRONLY) oflag |= UO_WRONLY;
		
		if (oflag & UO_CREATE)
		{	/* create directory right now */
			uffs_Object* fp = uffs_GetObject();
			if(fp == NULL) 
			{
				uffs_set_error(-UEMFILE);
				return U_FAIL;
			}
	
			if(uffs_OpenObject(fp, fd->path, oflag) != U_SUCC)
			{
				return U_FAIL;
			}
			/* release object hander */
			uffs_PutObject(fp);	
		}

		/* use directory handler */
		dirp = uffs_opendir(fd->path);
		if(dirp == NULL) 
		{
			uffs_set_error(-UEMFILE);
			return U_FAIL;
		}
		fd->data = dirp;

		return U_SUCC;
	}
	else
	{/* file */
		uffs_Object *fp;
		
		int mode = UO_RDONLY;

		if (fd->flags & DFS_O_WRONLY) mode |= UO_WRONLY;
		if ((fd->flags & DFS_O_ACCMODE) & DFS_O_RDWR) mode |= UO_WRONLY;
		/* Opens the file, if it is existing. If not, a new file is created. */
		if (fd->flags & DFS_O_CREAT) mode |= UO_CREATE;
		/* Creates a new file. If the file is existing, it is truncated and overwritten. */
		if (fd->flags & DFS_O_TRUNC) mode |= UO_TRUNC;
		/* Creates a new file. The function fails if the file is already existing. */
		if (fd->flags & DFS_O_EXCL) mode |= UO_EXCL;

		/* get an object hander */
		fp = uffs_GetObject();
		if(fp == NULL) 
		{
			uffs_set_error(-UEMFILE);
			return U_FAIL;
		}

		if(uffs_OpenObject(fp, fd->path, mode) == U_SUCC)
		{
			struct uffs_stat stat_buf;

			uffs_stat(fd->path, &stat_buf);

			fd->pos  = fp->pos;
			fd->size = stat_buf.st_size;
			fd->data = fp;

			if(fd->flags & DFS_O_APPEND)
			{
				fd->pos = uffs_SeekObject(fp, 0, USEEK_END);
			}
			return U_SUCC;
		}
		else
		{
			/* open failed, return */
			uffs_set_error(-uffs_GetObjectErr(fp));
			/* release object hander */
			uffs_PutObject(fp);
			return U_FAIL;
		}
	} 
}