lindabell 发表于 2012-3-31 11:53:42

rt-thread 的DFS能同时打开多个文件吗

我看源码中有
dfs_lock()
dfs_unlock()
这两个函数,但是在cat、copy的代码中没有使用,我同时打开几个文件会不会出问题啊void cat(const char* filename)
{
        rt_uint32_t length;
        char buffer;

        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);
}
FINSH_FUNCTION_EXPORT(cat, print file e.g:cat("/fileName.txt"))

#define BUF_SZ        4096
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);
}
FINSH_FUNCTION_EXPORT(copy, copy source file to destination file e.g:copy("/xiaoshuo.txt","/MY/xiaoshuo.txt"))

aozima 发表于 2012-3-31 12:05:42

本帖最后由 aozima 于 2012-3-31 12:06 编辑

这不是给用户使用的API。
页: [1]
查看完整版本: rt-thread 的DFS能同时打开多个文件吗