示例#1
0
static void copyfile(const char *src, const char *dst)
{
    struct dfs_fd src_fd;
    rt_uint8_t *block_ptr;
    rt_int32_t read_bytes;

    block_ptr = rt_malloc(BUF_SZ);
    if (block_ptr == RT_NULL)
    {
        rt_kprintf("out of memory\n");

        return;
    }

    if (dfs_file_open(&src_fd, src, DFS_O_RDONLY) < 0)
    {
        rt_free(block_ptr);
        rt_kprintf("Read %s failed\n", src);

        return;
    }
    if (dfs_file_open(&fd, dst, DFS_O_WRONLY | DFS_O_CREAT) < 0)
    {
        rt_free(block_ptr);
        dfs_file_close(&src_fd);

        rt_kprintf("Write %s failed\n", dst);

        return;
    }

    do
    {
        read_bytes = dfs_file_read(&src_fd, block_ptr, BUF_SZ);
        if (read_bytes > 0)
        {
            int length;

            length = dfs_file_write(&fd, block_ptr, read_bytes);
            if (length != read_bytes)
            {
                /* write failed. */
                rt_kprintf("Write file data failed, errno=%d\n", length);
                break;
            }
        }
    } while (read_bytes > 0);

    dfs_file_close(&src_fd);
    dfs_file_close(&fd);
    rt_free(block_ptr);
}
示例#2
0
文件: dfs_posix.c 项目: wuliaodew/RTT
/**
 * this function is a POSIX compliant version, which will open a file and return
 * a file descriptor.
 *
 * @param file the path name of file.
 * @param flags the file open flags.
 * @param mode
 *
 * @return the non-negative integer on successful open, others for failed.
 */
int open(const char *file, int flags, int mode)
{
	int fd, result;
	struct dfs_fd *d;

	/* allocate a fd */
	fd = fd_new();
	if (fd < 0)
	{
		rt_set_errno(-DFS_STATUS_ENOMEM);
		return -1;
	}
	d  = fd_get(fd);

	result = dfs_file_open(d, file, flags);
	if (result < 0)
	{
		/* release the ref-count of fd */
		fd_put(d);
		fd_put(d);
		
		rt_set_errno(result);

		return -1;
	}

	/* release the ref-count of fd */
	fd_put(d);
	return fd;
}
示例#3
0
static void copydir(const char * src, const char * dst)
{
    struct dirent dirent;
    struct stat stat;
    int length;

    if (dfs_file_open(&fd, src, DFS_O_DIRECTORY) < 0)
    {
        rt_kprintf("open %s failed\n", src);
        return ;
    }

    do
    {
        rt_memset(&dirent, 0, sizeof(struct dirent));
        length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent));
        if (length > 0)
        {
            char * src_entry_full = RT_NULL;
            char * dst_entry_full = RT_NULL;

            if (strcmp(dirent.d_name, "..") == 0 || strcmp(dirent.d_name, ".") == 0)
                continue;

            /* build full path for each file */
            if ((src_entry_full = dfs_normalize_path(src, dirent.d_name)) == RT_NULL)
            {
                rt_kprintf("out of memory!\n");
                break;
            }
            if ((dst_entry_full = dfs_normalize_path(dst, dirent.d_name)) == RT_NULL)
            {
                rt_kprintf("out of memory!\n");
                rt_free(src_entry_full);
                break;
            }

            rt_memset(&stat, 0, sizeof(struct stat));
            if (dfs_file_stat(src_entry_full, &stat) != 0)
            {
                rt_kprintf("open file: %s failed\n", dirent.d_name);
                continue;
            }

            if (DFS_S_ISDIR(stat.st_mode))
            {
                mkdir(dst_entry_full, 0);
                copydir(src_entry_full, dst_entry_full);
            }
            else
            {
                copyfile(src_entry_full, dst_entry_full);
            }
            rt_free(src_entry_full);
            rt_free(dst_entry_full);
        }
    }while(length > 0);

    dfs_file_close(&fd);
}
void copy(const char* src, const char* dst)
{
	struct dfs_fd src_fd;
	rt_uint8_t *block_ptr;
	rt_uint32_t read_bytes;

	block_ptr = rt_malloc(BUF_SZ);
	if (block_ptr == RT_NULL)
	{
		rt_kprintf("out of memory\n");
		return;
	}
	
	if (dfs_file_open(&src_fd, src, DFS_O_RDONLY) < 0)
	{
		rt_free(block_ptr);
		rt_kprintf("Read %s failed\n", src);
		return;
	}
	if (dfs_file_open(&fd, dst, DFS_O_WRONLY | DFS_O_CREAT) < 0)
	{
		rt_free(block_ptr);
		dfs_file_close(&src_fd);

		rt_kprintf("Write %s failed\n", dst);
		return;
	}

	do
	{
		read_bytes = dfs_file_read(&src_fd, block_ptr, BUF_SZ);
		if (read_bytes > 0)
		{
			dfs_file_write(&fd, block_ptr, read_bytes);
		}
	} while (read_bytes > 0);

	dfs_file_close(&src_fd);
	dfs_file_close(&fd);
	rt_free(block_ptr);
}
static rt_err_t copyfile(const char *src,  const char *dst)
{
    struct dfs_fd src_fd, dst_fd;
    rt_uint8_t *block_ptr;
    rt_int32_t read_bytes;

    block_ptr = rt_malloc(BUF_SZ);
    if (block_ptr == RT_NULL)
    {
        return -RT_ENOMEM;
    }

    if (dfs_file_open(&src_fd, src, DFS_O_RDONLY) < 0)
    {
        rt_free(block_ptr);
        return -RT_ERROR;
    }
    if (dfs_file_open(&dst_fd, dst, DFS_O_WRONLY | DFS_O_CREAT) < 0)
    {
        rt_free(block_ptr);
        dfs_file_close(&src_fd);
        return -RT_ERROR;
    }

    do {
        read_bytes = dfs_file_read(&src_fd, block_ptr, BUF_SZ);
        if (read_bytes > 0)
        {
            dfs_file_write(&dst_fd, block_ptr, read_bytes);
        }
    } while (read_bytes > 0);

    dfs_file_close(&src_fd);
    dfs_file_close(&dst_fd);
    rt_free(block_ptr);

    return RT_EOK;
}
void cat(const char* filename)
{
	rt_uint32_t length;
	char buffer[81];
	
	if (dfs_file_open(&fd, filename, DFS_O_RDONLY) < 0)
	{
		rt_kprintf("Open %s failed\n", filename);
		return;
	}
	
	do
	{
		rt_memset(buffer, 0, sizeof(buffer));
		length = dfs_file_read(&fd, buffer, sizeof(buffer)-1 );
		if (length > 0)
		{
			rt_kprintf("%s", buffer);
		}
	}while (length > 0);
	
	dfs_file_close(&fd);
}
void ls(const char* pathname)
{
	struct stat stat;
	int length;
	char *fullpath, *path;

	fullpath = RT_NULL;
	if (pathname == RT_NULL)
	{
#ifdef DFS_USING_WORKDIR
		/* open current working directory */
		path = rt_strdup(working_directory);
#else
		path = rt_strdup("/");
#endif
		if (path == RT_NULL) return ; /* out of memory */
	}
	else
	{
		path = (char*)pathname;
	}

	/* list directory */
	if ( dfs_file_open(&fd, path, DFS_O_DIRECTORY) == 0 )
	{
		rt_kprintf("Directory %s:\n", path);
		do
		{
			rt_memset(&dirent, 0, sizeof(struct dirent));
			length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent));
			if ( length > 0 ) 
			{
				rt_memset(&stat, 0, sizeof(struct stat));

				/* build full path for each file */
				fullpath = dfs_normalize_path(path, dirent.d_name);
				if (fullpath == RT_NULL) break;

				if (dfs_file_stat(fullpath, &stat) == 0)
				{
					rt_kprintf("%-20s", dirent.d_name);
					if ( DFS_S_ISDIR(stat.st_mode))
					{
						rt_kprintf("%-25s\n", "<DIR>");
					}
					else
					{
						rt_kprintf("%-25lu\n", stat.st_size);
					}
				}
				else
					rt_kprintf("BAD file: %s\n", dirent.d_name);
				rt_free(fullpath);
			}
		}while(length > 0);

		dfs_file_close(&fd);
	}
	else
	{
		rt_kprintf("No such directory\n");
	}
	if (pathname == RT_NULL) rt_free(path);
}