diff options
| author | [email protected] <[email protected]> | 2012-12-26 01:12:13 +0000 |
|---|---|---|
| committer | [email protected] <[email protected]> | 2012-12-26 01:12:13 +0000 |
| commit | 69521e2c8fa17515a625f96fae138df2c1ca8ba3 (patch) | |
| tree | 7228654ca2f917870d2f7a0c44cf0e1e33d7495f /components/dfs/src/dfs_file.c | |
| parent | ef5c47e6840a2d2cd986f4ecfdde87c9148375b2 (diff) | |
fixed the coding style in components/dfs
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2530 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'components/dfs/src/dfs_file.c')
| -rw-r--r-- | components/dfs/src/dfs_file.c | 736 |
1 files changed, 374 insertions, 362 deletions
diff --git a/components/dfs/src/dfs_file.c b/components/dfs/src/dfs_file.c index b18bdfaadd..0e757a2735 100644 --- a/components/dfs/src/dfs_file.c +++ b/components/dfs/src/dfs_file.c @@ -19,6 +19,7 @@ /** * @addtogroup FileApi */ + /*@{*/ /** @@ -32,84 +33,85 @@ */ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) { - struct dfs_filesystem *fs; - char *fullpath; - int result; + struct dfs_filesystem *fs; + char *fullpath; + int result; + + /* parameter check */ + if (fd == RT_NULL) + return -DFS_STATUS_EINVAL; + + /* make sure we have an absolute path */ + fullpath = dfs_normalize_path(RT_NULL, path); + if (fullpath == RT_NULL) + { + return -1; + } - /* parameter check */ - if (fd == RT_NULL) - return -DFS_STATUS_EINVAL; + dfs_log(DFS_DEBUG_INFO, ("open file:%s", fullpath)); - /* make sure we have an absolute path */ - fullpath = dfs_normalize_path(RT_NULL, path); - if (fullpath == RT_NULL) - { - return -1; - } + /* find filesystem */ + fs = dfs_filesystem_lookup(fullpath); + if (fs == RT_NULL) + { + rt_free(fullpath); /* release path */ - dfs_log(DFS_DEBUG_INFO, ("open file:%s", fullpath)); + return -DFS_STATUS_ENOENT; + } - /* find filesystem */ - fs = dfs_filesystem_lookup(fullpath); - if (fs == RT_NULL) - { - rt_free(fullpath); /* release path */ - return -DFS_STATUS_ENOENT; - } + dfs_log(DFS_DEBUG_INFO, ("open in filesystem:%s", fs->ops->name)); + fd->fs = fs; - dfs_log(DFS_DEBUG_INFO, ("open in filesystem:%s", fs->ops->name)); - fd->fs = fs; + /* initialize the fd item */ + fd->type = FT_REGULAR; + fd->flags = flags; + fd->size = 0; + fd->pos = 0; - /* initialize the fd item */ - fd->type = FT_REGULAR; - fd->flags = flags; - fd->size = 0; - fd->pos = 0; + if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH)) + { + if (dfs_subdir(fs->path, fullpath) == RT_NULL) + fd->path = rt_strdup("/"); + else + fd->path = rt_strdup(dfs_subdir(fs->path, fullpath)); + rt_free(fullpath); + dfs_log(DFS_DEBUG_INFO, ("Actual file path: %s\n", fd->path)); + } + else + { + fd->path = fullpath; + } - if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH)) - { - if (dfs_subdir(fs->path, fullpath) == RT_NULL) - fd->path = rt_strdup("/"); - else - fd->path = rt_strdup(dfs_subdir(fs->path, fullpath)); - rt_free(fullpath); - dfs_log(DFS_DEBUG_INFO, ("Actual file path: %s\n", fd->path)); - } - else - { - fd->path = fullpath; - } - - /* specific file system open routine */ - if (fs->ops->open == RT_NULL) - { - /* clear fd */ - rt_free(fd->path); - rt_memset(fd, 0, sizeof(*fd)); + /* specific file system open routine */ + if (fs->ops->open == RT_NULL) + { + /* clear fd */ + rt_free(fd->path); + rt_memset(fd, 0, sizeof(*fd)); - return -DFS_STATUS_ENOSYS; - } + return -DFS_STATUS_ENOSYS; + } - if ((result = fs->ops->open(fd)) < 0) - { - /* clear fd */ - rt_free(fd->path); - rt_memset(fd, 0, sizeof(*fd)); + if ((result = fs->ops->open(fd)) < 0) + { + /* clear fd */ + rt_free(fd->path); + rt_memset(fd, 0, sizeof(*fd)); - dfs_log(DFS_DEBUG_INFO, ("open failed")); + dfs_log(DFS_DEBUG_INFO, ("open failed")); - return result; - } + return result; + } - fd->flags |= DFS_F_OPEN; - if (flags & DFS_O_DIRECTORY) - { - fd->type = FT_DIRECTORY; - fd->flags |= DFS_F_DIRECTORY; - } + fd->flags |= DFS_F_OPEN; + if (flags & DFS_O_DIRECTORY) + { + fd->type = FT_DIRECTORY; + fd->flags |= DFS_F_DIRECTORY; + } - dfs_log(DFS_DEBUG_INFO, ("open successful")); - return 0; + dfs_log(DFS_DEBUG_INFO, ("open successful")); + return 0; } /** @@ -121,19 +123,19 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) */ int dfs_file_close(struct dfs_fd *fd) { - int result = 0; + int result = 0; - if (fd != RT_NULL && fd->fs->ops->close != RT_NULL) - result = fd->fs->ops->close(fd); + if (fd != RT_NULL && fd->fs->ops->close != RT_NULL) + result = fd->fs->ops->close(fd); - /* close fd error, return */ - if (result < 0) - return result; + /* close fd error, return */ + if (result < 0) + return result; - rt_free(fd->path); - rt_memset(fd, 0, sizeof(struct dfs_fd)); + rt_free(fd->path); + rt_memset(fd, 0, sizeof(struct dfs_fd)); - return result; + return result; } /** @@ -147,20 +149,21 @@ int dfs_file_close(struct dfs_fd *fd) */ int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args) { - struct dfs_filesystem *fs; + struct dfs_filesystem *fs; - if (fd == RT_NULL || fd->type != FT_REGULAR) - return -DFS_STATUS_EINVAL; + if (fd == RT_NULL || fd->type != FT_REGULAR) + return -DFS_STATUS_EINVAL; - fs = fd->fs; - if (fs->ops->ioctl != RT_NULL) - return fs->ops->ioctl(fd, cmd, args); + fs = fd->fs; + if (fs->ops->ioctl != RT_NULL) + return fs->ops->ioctl(fd, cmd, args); - return -DFS_STATUS_ENOSYS; + return -DFS_STATUS_ENOSYS; } /** - * this function will read specified length data from a file descriptor to a buffer. + * this function will read specified length data from a file descriptor to a + * buffer. * * @param fd the file descriptor. * @param buf the buffer to save the read data. @@ -170,20 +173,20 @@ int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args) */ int dfs_file_read(struct dfs_fd *fd, void *buf, rt_size_t len) { - struct dfs_filesystem *fs; - int result = 0; + struct dfs_filesystem *fs; + int result = 0; - if (fd == RT_NULL) - return -DFS_STATUS_EINVAL; + if (fd == RT_NULL) + return -DFS_STATUS_EINVAL; - fs = (struct dfs_filesystem *)fd->fs; - if (fs->ops->read == RT_NULL) - return -DFS_STATUS_ENOSYS; + fs = (struct dfs_filesystem *)fd->fs; + if (fs->ops->read == RT_NULL) + return -DFS_STATUS_ENOSYS; - if ((result = fs->ops->read(fd, buf, len)) < 0) - fd->flags |= DFS_F_EOF; + if ((result = fs->ops->read(fd, buf, len)) < 0) + fd->flags |= DFS_F_EOF; - return result; + return result; } /** @@ -197,17 +200,17 @@ int dfs_file_read(struct dfs_fd *fd, void *buf, rt_size_t len) */ int dfs_file_getdents(struct dfs_fd *fd, struct dirent *dirp, rt_size_t nbytes) { - struct dfs_filesystem *fs; + struct dfs_filesystem *fs; - /* parameter check */ - if (fd == RT_NULL || fd->type != FT_DIRECTORY) - return -DFS_STATUS_EINVAL; + /* parameter check */ + if (fd == RT_NULL || fd->type != FT_DIRECTORY) + return -DFS_STATUS_EINVAL; - fs = (struct dfs_filesystem *)fd->fs; - if (fs->ops->getdents != RT_NULL) - return fs->ops->getdents(fd, dirp, nbytes); + fs = (struct dfs_filesystem *)fd->fs; + if (fs->ops->getdents != RT_NULL) + return fs->ops->getdents(fd, dirp, nbytes); - return -DFS_STATUS_ENOSYS; + return -DFS_STATUS_ENOSYS; } /** @@ -219,50 +222,50 @@ int dfs_file_getdents(struct dfs_fd *fd, struct dirent *dirp, rt_size_t nbytes) */ int dfs_file_unlink(const char *path) { - int result; - char *fullpath; - struct dfs_filesystem *fs; + int result; + char *fullpath; + struct dfs_filesystem *fs; - result = DFS_STATUS_OK; + result = DFS_STATUS_OK; - /* Make sure we have an absolute path */ - fullpath = dfs_normalize_path(RT_NULL, path); - if (fullpath == RT_NULL) - { - return -DFS_STATUS_EINVAL; - } + /* Make sure we have an absolute path */ + fullpath = dfs_normalize_path(RT_NULL, path); + if (fullpath == RT_NULL) + { + return -DFS_STATUS_EINVAL; + } - /* get filesystem */ - if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL) - { - result = -DFS_STATUS_ENOENT; - goto __exit; - } + /* get filesystem */ + if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL) + { + result = -DFS_STATUS_ENOENT; + goto __exit; + } - /* Check whether file is already open */ - if (fd_is_open(fullpath) == 0) - { - result = -DFS_STATUS_EBUSY; - goto __exit; - } + /* Check whether file is already open */ + if (fd_is_open(fullpath) == 0) + { + result = -DFS_STATUS_EBUSY; + goto __exit; + } - if (fs->ops->unlink != RT_NULL) - { - if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH)) - { - if (dfs_subdir(fs->path, fullpath) == RT_NULL) - result = fs->ops->unlink(fs, "/"); - else - result = fs->ops->unlink(fs, dfs_subdir(fs->path, fullpath)); - } - else - result = fs->ops->unlink(fs, fullpath); - } - else result = -DFS_STATUS_ENOSYS; + if (fs->ops->unlink != RT_NULL) + { + if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH)) + { + if (dfs_subdir(fs->path, fullpath) == RT_NULL) + result = fs->ops->unlink(fs, "/"); + else + result = fs->ops->unlink(fs, dfs_subdir(fs->path, fullpath)); + } + else + result = fs->ops->unlink(fs, fullpath); + } + else result = -DFS_STATUS_ENOSYS; __exit: - rt_free(fullpath); - return result; + rt_free(fullpath); + return result; } /** @@ -276,16 +279,16 @@ __exit: */ int dfs_file_write(struct dfs_fd *fd, const void *buf, rt_size_t len) { - struct dfs_filesystem *fs; + struct dfs_filesystem *fs; - if (fd == RT_NULL) - return -DFS_STATUS_EINVAL; + if (fd == RT_NULL) + return -DFS_STATUS_EINVAL; - fs = fd->fs; - if (fs->ops->write == RT_NULL) - return -DFS_STATUS_ENOSYS; + fs = fd->fs; + if (fs->ops->write == RT_NULL) + return -DFS_STATUS_ENOSYS; - return fs->ops->write(fd, buf, len); + return fs->ops->write(fd, buf, len); } /** @@ -297,16 +300,16 @@ int dfs_file_write(struct dfs_fd *fd, const void *buf, rt_size_t len) */ int dfs_file_flush(struct dfs_fd *fd) { - struct dfs_filesystem *fs; + struct dfs_filesystem *fs; - if (fd == RT_NULL) - return -DFS_STATUS_EINVAL; + if (fd == RT_NULL) + return -DFS_STATUS_EINVAL; - fs = fd->fs; - if (fs->ops->flush == RT_NULL) - return -DFS_STATUS_ENOSYS; + fs = fd->fs; + if (fs->ops->flush == RT_NULL) + return -DFS_STATUS_ENOSYS; - return fs->ops->flush(fd); + return fs->ops->flush(fd); } /** @@ -319,21 +322,21 @@ int dfs_file_flush(struct dfs_fd *fd) */ int dfs_file_lseek(struct dfs_fd *fd, rt_off_t offset) { - int result; - struct dfs_filesystem *fs = fd->fs; + int result; + struct dfs_filesystem *fs = fd->fs; - if (fd == RT_NULL) - return -DFS_STATUS_EINVAL; - if (fs->ops->lseek == RT_NULL) - return -DFS_STATUS_ENOSYS; + if (fd == RT_NULL) + return -DFS_STATUS_EINVAL; + if (fs->ops->lseek == RT_NULL) + return -DFS_STATUS_ENOSYS; - result = fs->ops->lseek(fd, offset); + result = fs->ops->lseek(fd, offset); - /* update current position */ - if (result >= 0) - fd->pos = result; + /* update current position */ + if (result >= 0) + fd->pos = result; - return result; + return result; } /** @@ -346,62 +349,65 @@ int dfs_file_lseek(struct dfs_fd *fd, rt_off_t offset) */ int dfs_file_stat(const char *path, struct stat *buf) { - int result; - char *fullpath; - struct dfs_filesystem *fs; + int result; + char *fullpath; + struct dfs_filesystem *fs; + + fullpath = dfs_normalize_path(RT_NULL, path); + if (fullpath == RT_NULL) + { + return -1; + } + + if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL) + { + dfs_log(DFS_DEBUG_ERROR, + ("can't find mounted filesystem on this path:%s", fullpath)); + rt_free(fullpath); - fullpath = dfs_normalize_path(RT_NULL, path); - if (fullpath == RT_NULL) - { - return -1; - } + return -DFS_STATUS_ENOENT; + } - if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL) - { - dfs_log(DFS_DEBUG_ERROR, ("can't find mounted filesystem on this path:%s", fullpath)); - rt_free(fullpath); - return -DFS_STATUS_ENOENT; - } + if ((fullpath[0] == '/' && fullpath[1] == '\0') || + (dfs_subdir(fs->path, fullpath) == RT_NULL)) + { + /* it's the root directory */ + buf->st_dev = 0; - if ((fullpath[0] == '/' && fullpath[1] == '\0') || - (dfs_subdir(fs->path, fullpath) == RT_NULL)) - { - /* it's the root directory */ - buf->st_dev = 0; + buf->st_mode = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH | + DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH; + buf->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH; - buf->st_mode = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH | - DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH; - buf->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH; + buf->st_size = 0; + buf->st_mtime = 0; + buf->st_blksize = 512; - buf->st_size = 0; - buf->st_mtime = 0; - buf->st_blksize = 512; + /* release full path */ + rt_free(fullpath); - /* release full path */ - rt_free(fullpath); + return DFS_STATUS_OK; + } + else + { + if (fs->ops->stat == RT_NULL) + { + rt_free(fullpath); + dfs_log(DFS_DEBUG_ERROR, + ("the filesystem didn't implement this function")); - return DFS_STATUS_OK; - } - else - { - if (fs->ops->stat == RT_NULL) - { - rt_free(fullpath); - dfs_log(DFS_DEBUG_ERROR, ("the filesystem didn't implement this function")); - return -DFS_STATUS_ENOSYS; - } + return -DFS_STATUS_ENOSYS; + } - /* get the real file path and get file stat */ - if (fs->ops->flags & DFS_FS_FLAG_FULLPATH) - result = fs->ops->stat(fs, fullpath, buf); - else - result = fs->ops->stat(fs, dfs_subdir(fs->path, fullpath), buf); - - } + /* get the real file path and get file stat */ + if (fs->ops->flags & DFS_FS_FLAG_FULLPATH) + result = fs->ops->stat(fs, fullpath, buf); + else + result = fs->ops->stat(fs, dfs_subdir(fs->path, fullpath), buf); + } - rt_free(fullpath); + rt_free(fullpath); - return result; + return result; } /** @@ -414,58 +420,59 @@ int dfs_file_stat(const char *path, struct stat *buf) */ int dfs_file_rename(const char *oldpath, const char *newpath) { - int result; - struct dfs_filesystem *oldfs, *newfs; - char *oldfullpath, *newfullpath; + int result; + struct dfs_filesystem *oldfs, *newfs; + char *oldfullpath, *newfullpath; - result = DFS_STATUS_OK; - newfullpath = RT_NULL; - oldfullpath = RT_NULL; + result = DFS_STATUS_OK; + newfullpath = RT_NULL; + oldfullpath = RT_NULL; - oldfullpath = dfs_normalize_path(RT_NULL, oldpath); - if (oldfullpath == RT_NULL) - { - result = -DFS_STATUS_ENOENT; - goto __exit; - } + oldfullpath = dfs_normalize_path(RT_NULL, oldpath); + if (oldfullpath == RT_NULL) + { + result = -DFS_STATUS_ENOENT; + goto __exit; + } - newfullpath = dfs_normalize_path(RT_NULL, newpath); - if (newfullpath == RT_NULL) - { - result = -DFS_STATUS_ENOENT; - goto __exit; - } + newfullpath = dfs_normalize_path(RT_NULL, newpath); + if (newfullpath == RT_NULL) + { + result = -DFS_STATUS_ENOENT; + goto __exit; + } - oldfs = dfs_filesystem_lookup(oldfullpath); - newfs = dfs_filesystem_lookup(newfullpath); + oldfs = dfs_filesystem_lookup(oldfullpath); + newfs = dfs_filesystem_lookup(newfullpath); - if (oldfs == newfs) - { - if (oldfs->ops->rename == RT_NULL) - { - result = -DFS_STATUS_ENOSYS; - } - else - { - if (oldfs->ops->flags & DFS_FS_FLAG_FULLPATH) - result = oldfs->ops->rename(oldfs, oldfullpath, newfullpath); - else - /* use sub directory to rename in file system */ - result = oldfs->ops->rename(oldfs, dfs_subdir(oldfs->path, oldfullpath), - dfs_subdir(newfs->path, newfullpath)); - } - } - else - { - result = -DFS_STATUS_EXDEV; - } + if (oldfs == newfs) + { + if (oldfs->ops->rename == RT_NULL) + { + result = -DFS_STATUS_ENOSYS; + } + else + { + if (oldfs->ops->flags & DFS_FS_FLAG_FULLPATH) + result = oldfs->ops->rename(oldfs, oldfullpath, newfullpath); + else + /* use sub directory to rename in file system */ + result = oldfs->ops->rename(oldfs, + dfs_subdir(oldfs->path, oldfullpath), + dfs_subdir(newfs->path, newfullpath)); + } + } + else + { + result = -DFS_STATUS_EXDEV; + } __exit: - rt_free(oldfullpath); - rt_free(newfullpath); + rt_free(oldfullpath); + rt_free(newfullpath); - /* not at same file system, return EXDEV */ - return result; + /* not at same file system, return EXDEV */ + return result; } #ifdef RT_USING_FINSH @@ -475,147 +482,152 @@ static struct dfs_fd fd; static struct dirent dirent; void ls(const char *pathname) { - struct stat stat; - int length; - char *fullpath, *path; + struct stat stat; + int length; + char *fullpath, *path; - fullpath = RT_NULL; - if (pathname == RT_NULL) - { + fullpath = RT_NULL; + if (pathname == RT_NULL) + { #ifdef DFS_USING_WORKDIR - /* open current working directory */ - path = rt_strdup(working_directory); + /* open current working directory */ + path = rt_strdup(working_directory); #else - path = rt_strdup("/"); + path = rt_strdup("/"); #endif - if (path == RT_NULL) return ; /* out of memory */ - } - else - { - path = (char *)pathname; - } + 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)); + /* 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; + /* 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); + 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); + dfs_file_close(&fd); + } + else + { + rt_kprintf("No such directory\n"); + } + if (pathname == RT_NULL) + rt_free(path); } FINSH_FUNCTION_EXPORT(ls, list directory contents) void rm(const char *filename) { - if (dfs_file_unlink(filename) < 0) - { - rt_kprintf("Delete %s failed\n", filename); - } + if (dfs_file_unlink(filename) < 0) + { + rt_kprintf("Delete %s failed\n", filename); + } } FINSH_FUNCTION_EXPORT(rm, remove files or directories) void cat(const char* filename) { - rt_uint32_t length; - char buffer[81]; + 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; - } + if (dfs_file_open(&fd, filename, DFS_O_RDONLY) < 0) + { + rt_kprintf("Open %s failed\n", filename); - 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); + return; + } - dfs_file_close(&fd); + 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) -#define BUF_SZ 4096 +#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; + 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); - block_ptr = rt_malloc(BUF_SZ); - if (block_ptr == RT_NULL) - { - rt_kprintf("out of memory\n"); - return; - } + return; + } + if (dfs_file_open(&fd, dst, DFS_O_WRONLY | DFS_O_CREAT) < 0) + { + rt_free(block_ptr); + dfs_file_close(&src_fd); - 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); - rt_kprintf("Write %s failed\n", dst); - return; - } + 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); + 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); + dfs_file_close(&src_fd); + dfs_file_close(&fd); + rt_free(block_ptr); } FINSH_FUNCTION_EXPORT(copy, copy source file to destination file) |
