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 | |
| 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')
| -rw-r--r-- | components/dfs/src/dfs.c | 383 | ||||
| -rw-r--r-- | components/dfs/src/dfs_file.c | 736 | ||||
| -rw-r--r-- | components/dfs/src/dfs_fs.c | 608 | ||||
| -rw-r--r-- | components/dfs/src/dfs_posix.c | 732 |
4 files changed, 1255 insertions, 1204 deletions
diff --git a/components/dfs/src/dfs.c b/components/dfs/src/dfs.c index 05a7615f2e..86ef809fbf 100644 --- a/components/dfs/src/dfs.c +++ b/components/dfs/src/dfs.c @@ -16,7 +16,7 @@ #include <dfs_fs.h> #include <dfs_file.h> -#define NO_WORKING_DIR "system does not support working dir\n" +#define NO_WORKING_DIR "system does not support working dir\n" /* Global variables */ const struct dfs_filesystem_operation *filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX]; @@ -46,20 +46,20 @@ struct dfs_fd fd_table[DFS_FD_MAX]; */ void dfs_init(void) { - /* clear filesystem operations table */ - rt_memset(filesystem_operation_table, 0, sizeof(filesystem_operation_table)); - /* clear filesystem table */ - rt_memset(filesystem_table, 0, sizeof(filesystem_table)); - /* clean fd table */ - rt_memset(fd_table, 0, sizeof(fd_table)); + /* clear filesystem operations table */ + rt_memset(filesystem_operation_table, 0, sizeof(filesystem_operation_table)); + /* clear filesystem table */ + rt_memset(filesystem_table, 0, sizeof(filesystem_table)); + /* clean fd table */ + rt_memset(fd_table, 0, sizeof(fd_table)); - /* create device filesystem lock */ - rt_mutex_init(&fslock, "fslock", RT_IPC_FLAG_FIFO); + /* create device filesystem lock */ + rt_mutex_init(&fslock, "fslock", RT_IPC_FLAG_FIFO); #ifdef DFS_USING_WORKDIR - /* set current working directory */ - rt_memset(working_directory, 0, sizeof(working_directory)); - working_directory[0] = '/'; + /* set current working directory */ + rt_memset(working_directory, 0, sizeof(working_directory)); + working_directory[0] = '/'; #endif } @@ -70,13 +70,13 @@ void dfs_init(void) */ void dfs_lock(void) { - rt_err_t result; + rt_err_t result; - result = rt_mutex_take(&fslock, RT_WAITING_FOREVER); - if (result != RT_EOK) - { - RT_ASSERT(0); - } + result = rt_mutex_take(&fslock, RT_WAITING_FOREVER); + if (result != RT_EOK) + { + RT_ASSERT(0); + } } /** @@ -86,7 +86,7 @@ void dfs_lock(void) */ void dfs_unlock(void) { - rt_mutex_release(&fslock); + rt_mutex_release(&fslock); } /** @@ -97,36 +97,36 @@ void dfs_unlock(void) */ int fd_new(void) { - struct dfs_fd *d; - int idx; + struct dfs_fd *d; + int idx; - /* lock filesystem */ - dfs_lock(); + /* lock filesystem */ + dfs_lock(); - /* find an empty fd entry */ + /* find an empty fd entry */ #ifdef DFS_USING_STDIO - for (idx = 3; idx < DFS_FD_MAX + 3 && fd_table[idx].ref_count > 0; idx++); + for (idx = 3; idx < DFS_FD_MAX + 3 && fd_table[idx].ref_count > 0; idx++); #else - for (idx = 0; idx < DFS_FD_MAX && fd_table[idx].ref_count > 0; idx++); + for (idx = 0; idx < DFS_FD_MAX && fd_table[idx].ref_count > 0; idx++); #endif - /* can't find an empty fd entry */ + /* can't find an empty fd entry */ #ifdef DFS_USING_STDIO - if (idx == DFS_FD_MAX + 3) + if (idx == DFS_FD_MAX + 3) #else - if (idx == DFS_FD_MAX) + if (idx == DFS_FD_MAX) #endif - { - idx = -1; - goto __result; - } + { + idx = -1; + goto __result; + } - d = &(fd_table[idx]); - d->ref_count = 1; + d = &(fd_table[idx]); + d->ref_count = 1; __result: - dfs_unlock(); - return idx; + dfs_unlock(); + return idx; } /** @@ -140,24 +140,24 @@ __result: */ struct dfs_fd *fd_get(int fd) { - struct dfs_fd *d; + struct dfs_fd *d; #ifdef DFS_USING_STDIO - if (fd < 3 || fd >= DFS_FD_MAX + 3) - return RT_NULL; + if (fd < 3 || fd >= DFS_FD_MAX + 3) + return RT_NULL; #else - if (fd < 0 || fd >= DFS_FD_MAX) - return RT_NULL; + if (fd < 0 || fd >= DFS_FD_MAX) + return RT_NULL; #endif - dfs_lock(); - d = &fd_table[fd]; + dfs_lock(); + d = &fd_table[fd]; - /* increase the reference count */ - d->ref_count ++; - dfs_unlock(); + /* increase the reference count */ + d->ref_count ++; + dfs_unlock(); - return d; + return d; } /** @@ -167,17 +167,17 @@ struct dfs_fd *fd_get(int fd) */ void fd_put(struct dfs_fd *fd) { - RT_ASSERT(fd != RT_NULL); + RT_ASSERT(fd != RT_NULL); - dfs_lock(); - fd->ref_count --; + dfs_lock(); + fd->ref_count --; - /* clear this fd entry */ - if (fd->ref_count == 0) - { - rt_memset(fd, 0, sizeof(struct dfs_fd)); - } - dfs_unlock(); + /* clear this fd entry */ + if (fd->ref_count == 0) + { + rt_memset(fd, 0, sizeof(struct dfs_fd)); + } + dfs_unlock(); }; /** @@ -191,52 +191,52 @@ void fd_put(struct dfs_fd *fd) */ int fd_is_open(const char *pathname) { - char *fullpath; - unsigned int index; - struct dfs_filesystem *fs; - struct dfs_fd *fd; + char *fullpath; + unsigned int index; + struct dfs_filesystem *fs; + struct dfs_fd *fd; - fullpath = dfs_normalize_path(RT_NULL, pathname); - if (fullpath != RT_NULL) - { - char *mountpath; - fs = dfs_filesystem_lookup(fullpath); - if (fs == RT_NULL) - { - /* can't find mounted file system */ - rt_free(fullpath); + fullpath = dfs_normalize_path(RT_NULL, pathname); + if (fullpath != RT_NULL) + { + char *mountpath; + fs = dfs_filesystem_lookup(fullpath); + if (fs == RT_NULL) + { + /* can't find mounted file system */ + rt_free(fullpath); - return -1; - } + return -1; + } - /* get file path name under mounted file system */ - if (fs->path[0] == '/' && fs->path[1] == '\0') - mountpath = fullpath; - else - mountpath = fullpath + strlen(fs->path); + /* get file path name under mounted file system */ + if (fs->path[0] == '/' && fs->path[1] == '\0') + mountpath = fullpath; + else + mountpath = fullpath + strlen(fs->path); - dfs_lock(); - for (index = 0; index < DFS_FD_MAX; index++) - { - fd = &(fd_table[index]); - if (fd->fs == RT_NULL) - continue; + dfs_lock(); + for (index = 0; index < DFS_FD_MAX; index++) + { + fd = &(fd_table[index]); + if (fd->fs == RT_NULL) + continue; - if (fd->fs == fs && strcmp(fd->path, mountpath) == 0) - { - /* found file in file descriptor table */ - rt_free(fullpath); - dfs_unlock(); + if (fd->fs == fs && strcmp(fd->path, mountpath) == 0) + { + /* found file in file descriptor table */ + rt_free(fullpath); + dfs_unlock(); - return 0; - } - } - dfs_unlock(); + return 0; + } + } + dfs_unlock(); - rt_free(fullpath); - } + rt_free(fullpath); + } - return -1; + return -1; } /** @@ -249,22 +249,23 @@ int fd_is_open(const char *pathname) */ const char *dfs_subdir(const char *directory, const char *filename) { - const char *dir; + const char *dir; - if (strlen(directory) == strlen(filename)) /* it's a same path */ - return RT_NULL; + if (strlen(directory) == strlen(filename)) /* it's a same path */ + return RT_NULL; - dir = filename + strlen(directory); - if ((*dir != '/') && (dir != filename)) - { - dir --; - } + dir = filename + strlen(directory); + if ((*dir != '/') && (dir != filename)) + { + dir --; + } - return dir; + return dir; } /** - * this function will normalize a path according to specified parent directory and file name. + * this function will normalize a path according to specified parent directory + * and file name. * * @param directory the parent path * @param filename the file name @@ -273,113 +274,113 @@ const char *dfs_subdir(const char *directory, const char *filename) */ char *dfs_normalize_path(const char *directory, const char *filename) { - char *fullpath; - char *dst0, *dst, *src; + char *fullpath; + char *dst0, *dst, *src; - /* check parameters */ - RT_ASSERT(filename != RT_NULL); + /* check parameters */ + RT_ASSERT(filename != RT_NULL); #ifdef DFS_USING_WORKDIR - if (directory == RT_NULL) /* shall use working directory */ - directory = &working_directory[0]; + if (directory == RT_NULL) /* shall use working directory */ + directory = &working_directory[0]; #else - if ((directory == RT_NULL) && (filename[0] != '/')) - { - rt_kprintf(NO_WORKING_DIR); + if ((directory == RT_NULL) && (filename[0] != '/')) + { + rt_kprintf(NO_WORKING_DIR); - return RT_NULL; - } + return RT_NULL; + } #endif - if (filename[0] != '/') /* it's a absolute path, use it directly */ - { - fullpath = rt_malloc(strlen(directory) + strlen(filename) + 2); + if (filename[0] != '/') /* it's a absolute path, use it directly */ + { + fullpath = rt_malloc(strlen(directory) + strlen(filename) + 2); - /* join path and file name */ - rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2, - "%s/%s", directory, filename); - } - else - { - fullpath = rt_strdup(filename); /* copy string */ - } + /* join path and file name */ + rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2, + "%s/%s", directory, filename); + } + else + { + fullpath = rt_strdup(filename); /* copy string */ + } - src = fullpath; - dst = fullpath; - - dst0 = dst; - while (1) - { - char c = *src; + src = fullpath; + dst = fullpath; + + dst0 = dst; + while (1) + { + char c = *src; - if (c == '.') - { - if (!src[1]) src ++; /* '.' and ends */ - else if (src[1] == '/') - { - /* './' case */ - src += 2; + if (c == '.') + { + if (!src[1]) src ++; /* '.' and ends */ + else if (src[1] == '/') + { + /* './' case */ + src += 2; - while ((*src == '/') && (*src != '\0')) - src ++; - continue; - } - else if (src[1] == '.') - { - if (!src[2]) - { - /* '..' and ends case */ - src += 2; - goto up_one; - } - else if (src[2] == '/') - { - /* '../' case */ - src += 3; + while ((*src == '/') && (*src != '\0')) + src ++; + continue; + } + else if (src[1] == '.') + { + if (!src[2]) + { + /* '..' and ends case */ + src += 2; + goto up_one; + } + else if (src[2] == '/') + { + /* '../' case */ + src += 3; - while ((*src == '/') && (*src != '\0')) - src ++; - goto up_one; - } - } - } + while ((*src == '/') && (*src != '\0')) + src ++; + goto up_one; + } + } + } - /* copy up the next '/' and erase all '/' */ - while ((c = *src++) != '\0' && c != '/') - *dst ++ = c; + /* copy up the next '/' and erase all '/' */ + while ((c = *src++) != '\0' && c != '/') + *dst ++ = c; - if (c == '/') - { - *dst ++ = '/'; - while (c == '/') - c = *src++; + if (c == '/') + { + *dst ++ = '/'; + while (c == '/') + c = *src++; - src --; - } - else if (!c) - break; + src --; + } + else if (!c) + break; - continue; + continue; up_one: - dst --; - if (dst < dst0) - { - rt_free(fullpath); - return RT_NULL; - } - while (dst0 < dst && dst[-1] != '/') - dst --; - } + dst --; + if (dst < dst0) + { + rt_free(fullpath); + return RT_NULL; + } + while (dst0 < dst && dst[-1] != '/') + dst --; + } - *dst = '\0'; + *dst = '\0'; - /* remove '/' in the end of path if exist */ - dst --; - if ((dst != fullpath) && (*dst == '/')) - *dst = '\0'; + /* remove '/' in the end of path if exist */ + dst --; + if ((dst != fullpath) && (*dst == '/')) + *dst = '\0'; - return fullpath; + return fullpath; } /*@}*/ 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) diff --git a/components/dfs/src/dfs_fs.c b/components/dfs/src/dfs_fs.c index f747617e5f..78c40013a8 100644 --- a/components/dfs/src/dfs_fs.c +++ b/components/dfs/src/dfs_fs.c @@ -31,41 +31,43 @@ */ int dfs_register(const struct dfs_filesystem_operation *ops) { - int index, result; + int index, result; - result = 0; + result = 0; - /* lock filesystem */ - dfs_lock(); + /* lock filesystem */ + dfs_lock(); - /* check if this filesystem was already registered */ - for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++) - { - if (filesystem_operation_table[index] != RT_NULL && - strcmp(filesystem_operation_table[index]->name, ops->name) == 0) - { - result = -1; - goto err; - } - } + /* check if this filesystem was already registered */ + for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++) + { + if (filesystem_operation_table[index] != RT_NULL && + strcmp(filesystem_operation_table[index]->name, ops->name) == 0) + { + result = -1; + goto err; + } + } - /* find out an empty filesystem type entry */ - for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX && filesystem_operation_table[index] != RT_NULL; - index++) ; + /* find out an empty filesystem type entry */ + for (index = 0; + index < DFS_FILESYSTEM_TYPES_MAX && filesystem_operation_table[index] != RT_NULL; + index ++) + ; - /* filesystem type table full */ - if (index == DFS_FILESYSTEM_TYPES_MAX) - { - result = -1; - goto err; - } + /* filesystem type table full */ + if (index == DFS_FILESYSTEM_TYPES_MAX) + { + result = -1; + goto err; + } - /* save the filesystem's operations */ - filesystem_operation_table[index] = ops; + /* save the filesystem's operations */ + filesystem_operation_table[index] = ops; err: - dfs_unlock(); - return result; + dfs_unlock(); + return result; } /** @@ -78,42 +80,42 @@ err: */ struct dfs_filesystem *dfs_filesystem_lookup(const char *path) { - struct dfs_filesystem *fs; - rt_uint32_t index, fspath, prefixlen; + struct dfs_filesystem *fs; + rt_uint32_t index, fspath, prefixlen; - fs = RT_NULL; - prefixlen = 0; + fs = RT_NULL; + prefixlen = 0; - /* lock filesystem */ - dfs_lock(); + /* lock filesystem */ + dfs_lock(); - /* lookup it in the filesystem table */ - for (index = 0; index < DFS_FILESYSTEMS_MAX; index++) - { - if (filesystem_table[index].path == RT_NULL) - continue; - else - { - fspath = strlen(filesystem_table[index].path); - if (fspath < prefixlen) - continue; - } + /* lookup it in the filesystem table */ + for (index = 0; index < DFS_FILESYSTEMS_MAX; index++) + { + if (filesystem_table[index].path == RT_NULL) + continue; + else + { + fspath = strlen(filesystem_table[index].path); + if (fspath < prefixlen) + continue; + } - if ((filesystem_table[index].ops != RT_NULL) && - (strncmp(filesystem_table[index].path, path, fspath) == 0)) - { - /* check next path separator */ - if (fspath > 1 && (strlen(path) > fspath) && (path[fspath] != '/')) - continue; + if ((filesystem_table[index].ops != RT_NULL) && + (strncmp(filesystem_table[index].path, path, fspath) == 0)) + { + /* check next path separator */ + if (fspath > 1 && (strlen(path) > fspath) && (path[fspath] != '/')) + continue; - fs = &filesystem_table[index]; - prefixlen = fspath; - } - } + fs = &filesystem_table[index]; + prefixlen = fspath; + } + } - dfs_unlock(); + dfs_unlock(); - return fs; + return fs; } /** @@ -125,66 +127,73 @@ struct dfs_filesystem *dfs_filesystem_lookup(const char *path) * * @return RT_EOK on successful or -RT_ERROR on failed. */ -rt_err_t dfs_filesystem_get_partition(struct dfs_partition *part, rt_uint8_t *buf, rt_uint32_t pindex) +rt_err_t dfs_filesystem_get_partition(struct dfs_partition *part, + rt_uint8_t *buf, + rt_uint32_t pindex) { -#define DPT_ADDRESS 0x1be /* device partition offset in Boot Sector */ -#define DPT_ITEM_SIZE 16 /* partition item size */ +#define DPT_ADDRESS 0x1be /* device partition offset in Boot Sector */ +#define DPT_ITEM_SIZE 16 /* partition item size */ - rt_uint8_t *dpt; - rt_uint8_t type; - rt_err_t result; + rt_uint8_t *dpt; + rt_uint8_t type; + rt_err_t result; - RT_ASSERT(part != RT_NULL); - RT_ASSERT(buf != RT_NULL); + RT_ASSERT(part != RT_NULL); + RT_ASSERT(buf != RT_NULL); - result = RT_EOK; + result = RT_EOK; - dpt = buf + DPT_ADDRESS + pindex * DPT_ITEM_SIZE; + dpt = buf + DPT_ADDRESS + pindex * DPT_ITEM_SIZE; - if ((*dpt != 0x80) && (*dpt != 0x00)) - { - /* which is not a partition table */ - result = -RT_ERROR; - return result; - } + if ((*dpt != 0x80) && (*dpt != 0x00)) + { + /* which is not a partition table */ + result = -RT_ERROR; - /* get partition type */ - type = *(dpt+4); + return result; + } - if (type != 0) - { - /* set partition type */ - part->type = type; + /* get partition type */ + type = *(dpt+4); - /* get partition offset and size */ - part->offset = *(dpt+8) | *(dpt+9)<<8 | *(dpt+10)<<16 | *(dpt+11)<<24; - part->size = *(dpt+12) | *(dpt+13)<<8 | *(dpt+14)<<16 | *(dpt+15)<<24; + if (type != 0) + { + /* set partition type */ + part->type = type; - rt_kprintf("found part[%d], begin: %d, size: ", pindex, part->offset*512); - if ((part->size>>11) > 0) /* MB */ - { - unsigned int part_size; - part_size = part->size >> 11;/* MB */ - if ((part_size>>10) > 0) /* GB */ - { - rt_kprintf("%d.%d%s",part_size>>10,part_size&0x3FF,"GB\r\n");/* GB */ - } - else - { - rt_kprintf("%d.%d%s",part_size,(part->size>>1)&0x3FF,"MB\r\n");/* MB */ - } - } - else - { - rt_kprintf("%d%s",part->size>>1,"KB\r\n");/* KB */ - } - } - else - { - result = -RT_ERROR; - } + /* get partition offset and size */ + part->offset = *(dpt+8) | *(dpt+9)<<8 | *(dpt+10)<<16 | *(dpt+11)<<24; + part->size = *(dpt+12) | *(dpt+13)<<8 | *(dpt+14)<<16 | *(dpt+15)<<24; - return result; + rt_kprintf("found part[%d], begin: %d, size: ", + pindex, part->offset*512); + if ((part->size>>11) > 0) /* MB */ + { + unsigned int part_size; + part_size = part->size >> 11;/* MB */ + if ((part_size>>10) > 0) /* GB */ + { + /* GB */ + rt_kprintf("%d.%d%s",part_size>>10,part_size&0x3FF,"GB\r\n"); + } + else + { + /* MB */ + rt_kprintf("%d.%d%s",part_size,(part->size>>1)&0x3FF,"MB\r\n"); + } + } + else + { + /* KB */ + rt_kprintf("%d%s",part->size>>1,"KB\r\n"); + } + } + else + { + result = -RT_ERROR; + } + + return result; } /** @@ -198,144 +207,157 @@ rt_err_t dfs_filesystem_get_partition(struct dfs_partition *part, rt_uint8_t *bu * * @return 0 on successful or -1 on failed. */ -int dfs_mount(const char *device_name, const char *path, - const char *filesystemtype, unsigned long rwflag, const - void *data) +int dfs_mount(const char *device_name, + const char *path, + const char *filesystemtype, + unsigned long rwflag, + const void *data) { - const struct dfs_filesystem_operation *ops; - struct dfs_filesystem *fs; - char *fullpath=RT_NULL; - rt_device_t dev_id; - int index; + const struct dfs_filesystem_operation *ops; + struct dfs_filesystem *fs; + char *fullpath=RT_NULL; + rt_device_t dev_id; + int index; + + /* open specific device */ + if (device_name != RT_NULL) + { + dev_id = rt_device_find(device_name); + if (dev_id == RT_NULL) + { + /* no this device */ + rt_set_errno(-DFS_STATUS_ENODEV); + + return -1; + } + } + else + { + /* which is a non-device filesystem mount */ + dev_id = RT_NULL; + } + + /* find out specific filesystem */ + dfs_lock(); + for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++) + { + if (strcmp(filesystem_operation_table[index]->name, filesystemtype) == 0) + break; + } + + /* can't find filesystem */ + if (index == DFS_FILESYSTEM_TYPES_MAX) + { + rt_set_errno(-DFS_STATUS_ENODEV); + dfs_unlock(); - /* open specific device */ - if (device_name != RT_NULL) - { - dev_id = rt_device_find(device_name); - if (dev_id == RT_NULL) - { - /* no this device */ - rt_set_errno(-DFS_STATUS_ENODEV); - return -1; - } - } - else - { - /* which is a non-device filesystem mount */ - dev_id = RT_NULL; - } + return -1; + } + ops = filesystem_operation_table[index]; + dfs_unlock(); - /* find out specific filesystem */ - dfs_lock(); - for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++) - { - if (strcmp(filesystem_operation_table[index]->name, filesystemtype) == 0) - break; - } + /* make full path for special file */ + fullpath = dfs_normalize_path(RT_NULL, path); + if (fullpath == RT_NULL) /* not an abstract path */ + { + rt_set_errno(-DFS_STATUS_ENOTDIR); - /* can't find filesystem */ - if (index == DFS_FILESYSTEM_TYPES_MAX) - { - rt_set_errno(-DFS_STATUS_ENODEV); - dfs_unlock(); - return -1; - } - ops = filesystem_operation_table[index]; - dfs_unlock(); + return -1; + } - /* make full path for special file */ - fullpath = dfs_normalize_path(RT_NULL, path); - if (fullpath == RT_NULL) /* not an abstract path */ - { - rt_set_errno(-DFS_STATUS_ENOTDIR); - return -1; - } + /* Check if the path exists or not, raw APIs call, fixme */ + if ((strcmp(fullpath, "/") != 0) && (strcmp(fullpath, "/dev") != 0)) + { + struct dfs_fd fd; - /* Check if the path exists or not, raw APIs call, fixme */ - if ((strcmp(fullpath, "/") != 0) && (strcmp(fullpath, "/dev") != 0)) - { - struct dfs_fd fd; + if (dfs_file_open(&fd, fullpath, DFS_O_RDONLY | DFS_O_DIRECTORY) < 0) + { + rt_free(fullpath); + rt_set_errno(-DFS_STATUS_ENOTDIR); - if (dfs_file_open(&fd, fullpath, DFS_O_RDONLY | DFS_O_DIRECTORY) < 0) - { - rt_free(fullpath); - rt_set_errno(-DFS_STATUS_ENOTDIR); - return -1; - } - dfs_file_close(&fd); - } + return -1; + } + dfs_file_close(&fd); + } - /* check whether the file system mounted or not */ - dfs_lock(); - for (index =0; index < DFS_FILESYSTEMS_MAX; index++) - { - if (filesystem_table[index].ops != RT_NULL && - strcmp(filesystem_table[index].path, path) == 0) - { - rt_set_errno(-DFS_STATUS_EINVAL); - goto err1; - } - } + /* check whether the file system mounted or not */ + dfs_lock(); + for (index = 0; index < DFS_FILESYSTEMS_MAX; index ++) + { + if (filesystem_table[index].ops != RT_NULL && + strcmp(filesystem_table[index].path, path) == 0) + { + rt_set_errno(-DFS_STATUS_EINVAL); + goto err1; + } + } - /* find out an empty filesystem table entry */ - for (index = 0; index < DFS_FILESYSTEMS_MAX && filesystem_table[index].ops != RT_NULL; - index++) ; - if (index == DFS_FILESYSTEMS_MAX) /* can't find en empty filesystem table entry */ - { - rt_set_errno(-DFS_STATUS_ENOSPC); - goto err1; - } + /* find out an empty filesystem table entry */ + for (index = 0; + index < DFS_FILESYSTEMS_MAX && filesystem_table[index].ops != RT_NULL; + index ++) + ; - /* register file system */ - fs = &(filesystem_table[index]); - fs->path = fullpath; - fs->ops = ops; - fs->dev_id = dev_id; - /* release filesystem_table lock */ - dfs_unlock(); + /* can't find en empty filesystem table entry */ + if (index == DFS_FILESYSTEMS_MAX) + { + rt_set_errno(-DFS_STATUS_ENOSPC); + goto err1; + } - /* open device, but do not check the status of device */ - if (dev_id != RT_NULL) - rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDWR); + /* register file system */ + fs = &(filesystem_table[index]); + fs->path = fullpath; + fs->ops = ops; + fs->dev_id = dev_id; + /* release filesystem_table lock */ + dfs_unlock(); - if (ops->mount == RT_NULL) /* there is no mount implementation */ - { - if (dev_id != RT_NULL) - rt_device_close(dev_id); - dfs_lock(); - /* clear filesystem table entry */ - rt_memset(fs, 0, sizeof(struct dfs_filesystem)); - dfs_unlock(); + /* open device, but do not check the status of device */ + if (dev_id != RT_NULL) + rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDWR); - rt_free(fullpath); - rt_set_errno(-DFS_STATUS_ENOSYS); - return -1; - } - /* call mount of this filesystem */ - else if (ops->mount(fs, rwflag, data) < 0) - { - /* close device */ - if (dev_id != RT_NULL) - rt_device_close(fs->dev_id); + /* there is no mount implementation */ + if (ops->mount == RT_NULL) + { + if (dev_id != RT_NULL) + rt_device_close(dev_id); + dfs_lock(); + /* clear filesystem table entry */ + rt_memset(fs, 0, sizeof(struct dfs_filesystem)); + dfs_unlock(); - /* mount failed */ - dfs_lock(); - /* clear filesystem table entry */ - rt_memset(fs, 0, sizeof(struct dfs_filesystem)); - dfs_unlock(); + rt_free(fullpath); + rt_set_errno(-DFS_STATUS_ENOSYS); - rt_free(fullpath); - return -1; - } + return -1; + } + /* call mount of this filesystem */ + else if (ops->mount(fs, rwflag, data) < 0) + { + /* close device */ + if (dev_id != RT_NULL) + rt_device_close(fs->dev_id); - return 0; + /* mount failed */ + dfs_lock(); + /* clear filesystem table entry */ + rt_memset(fs, 0, sizeof(struct dfs_filesystem)); + dfs_unlock(); + + rt_free(fullpath); + + return -1; + } + + return 0; err1: - dfs_unlock(); - if (fullpath != RT_NULL) - rt_free(fullpath); + dfs_unlock(); + if (fullpath != RT_NULL) + rt_free(fullpath); - return -1; + return -1; } /** @@ -347,45 +369,48 @@ err1: */ int dfs_unmount(const char *specialfile) { - char *fullpath; - struct dfs_filesystem *fs = RT_NULL; + char *fullpath; + struct dfs_filesystem *fs = RT_NULL; + + fullpath = dfs_normalize_path(RT_NULL, specialfile); + if (fullpath == RT_NULL) + { + rt_set_errno(-DFS_STATUS_ENOTDIR); - fullpath = dfs_normalize_path(RT_NULL, specialfile); - if (fullpath == RT_NULL) - { - rt_set_errno(-DFS_STATUS_ENOTDIR); - return -1; - } + return -1; + } - /* lock filesystem */ - dfs_lock(); + /* lock filesystem */ + dfs_lock(); - fs = dfs_filesystem_lookup(fullpath); - if (fs == RT_NULL || fs->ops->unmount == RT_NULL || fs->ops->unmount(fs) < 0) - { - goto err1; - } + fs = dfs_filesystem_lookup(fullpath); + if (fs == RT_NULL || + fs->ops->unmount == RT_NULL || + fs->ops->unmount(fs) < 0) + { + goto err1; + } - /* close device, but do not check the status of device */ - if (fs->dev_id != RT_NULL) - rt_device_close(fs->dev_id); + /* close device, but do not check the status of device */ + if (fs->dev_id != RT_NULL) + rt_device_close(fs->dev_id); - if (fs->path != RT_NULL) - rt_free(fs->path); + if (fs->path != RT_NULL) + rt_free(fs->path); - /* clear this filesystem table entry */ - rt_memset(fs, 0, sizeof(struct dfs_filesystem)); + /* clear this filesystem table entry */ + rt_memset(fs, 0, sizeof(struct dfs_filesystem)); - dfs_unlock(); - rt_free(fullpath); + dfs_unlock(); + rt_free(fullpath); - return 0; + return 0; err1: - dfs_unlock(); - rt_free(fullpath); + dfs_unlock(); + rt_free(fullpath); - return -1; + return -1; } /** @@ -398,30 +423,30 @@ err1: */ int dfs_mkfs(const char *fs_name, const char *device_name) { - int index; + int index; - /* lock file system */ - dfs_lock(); - /* find the file system operations */ - for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++) - { - if (filesystem_operation_table[index] != RT_NULL && - strcmp(filesystem_operation_table[index]->name, fs_name) == 0) - { - /* find file system operation */ - const struct dfs_filesystem_operation* ops = filesystem_operation_table[index]; - dfs_unlock(); + /* lock file system */ + dfs_lock(); + /* find the file system operations */ + for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index ++) + { + if (filesystem_operation_table[index] != RT_NULL && + strcmp(filesystem_operation_table[index]->name, fs_name) == 0) + { + /* find file system operation */ + const struct dfs_filesystem_operation *ops = filesystem_operation_table[index]; + dfs_unlock(); - if (ops->mkfs != RT_NULL) - return ops->mkfs(device_name); + if (ops->mkfs != RT_NULL) + return ops->mkfs(device_name); - break; - } - } - dfs_unlock(); + break; + } + } + dfs_unlock(); - rt_kprintf("Can not find the file system which named as %s.\n", fs_name); - return -1; + rt_kprintf("Can not find the file system which named as %s.\n", fs_name); + return -1; } /** @@ -434,40 +459,41 @@ int dfs_mkfs(const char *fs_name, const char *device_name) */ int dfs_statfs(const char *path, struct statfs *buffer) { - struct dfs_filesystem *fs; + struct dfs_filesystem *fs; - fs = dfs_filesystem_lookup(path); - if (fs != RT_NULL) - { - if (fs->ops->statfs != RT_NULL) - return fs->ops->statfs(fs, buffer); - } + fs = dfs_filesystem_lookup(path); + if (fs != RT_NULL) + { + if (fs->ops->statfs != RT_NULL) + return fs->ops->statfs(fs, buffer); + } - return -1; + return -1; } #ifdef RT_USING_FINSH #include <finsh.h> void mkfs(const char *fs_name, const char *device_name) { - dfs_mkfs(fs_name, device_name); + dfs_mkfs(fs_name, device_name); } FINSH_FUNCTION_EXPORT(mkfs, make a file system); void df(const char *path) { - int result; - struct statfs buffer; + int result; + struct statfs buffer; - if (path == RT_NULL) - result = dfs_statfs("/", &buffer); - else - result = dfs_statfs(path, &buffer); + if (path == RT_NULL) + result = dfs_statfs("/", &buffer); + else + result = dfs_statfs(path, &buffer); - if (result == 0) - { - rt_kprintf("disk free: %d block[%d bytes per block]\n", buffer.f_bfree, buffer.f_bsize); - } + if (result == 0) + { + rt_kprintf("disk free: %d block[%d bytes per block]\n", + buffer.f_bfree, buffer.f_bsize); + } } FINSH_FUNCTION_EXPORT(df, get disk free); #endif diff --git a/components/dfs/src/dfs_posix.c b/components/dfs/src/dfs_posix.c index fb69d484c0..2ccc1541d5 100644 --- a/components/dfs/src/dfs_posix.c +++ b/components/dfs/src/dfs_posix.c @@ -18,11 +18,12 @@ /**
* @addtogroup FsPosixApi
*/
+
/*@{*/
/**
- * this function is a POSIX compliant version, which will open a file and return
- * a file descriptor.
+ * 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.
@@ -32,34 +33,35 @@ */
int open(const char *file, int flags, int mode)
{
- int fd, result;
- struct dfs_fd *d;
+ int fd, result;
+ struct dfs_fd *d;
+
+ /* allocate a fd */
+ fd = fd_new();
+ if (fd < 0)
+ {
+ rt_set_errno(-DFS_STATUS_ENOMEM);
- /* allocate a fd */
- fd = fd_new();
- if (fd < 0)
- {
- rt_set_errno(-DFS_STATUS_ENOMEM);
- return -1;
- }
- d = fd_get(fd);
+ 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);
+ 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;
- }
+ return -1;
+ }
- /* release the ref-count of fd */
- fd_put(d);
+ /* release the ref-count of fd */
+ fd_put(d);
- return fd;
+ return fd;
}
RTM_EXPORT(open);
@@ -73,35 +75,36 @@ RTM_EXPORT(open); */
int close(int fd)
{
- int result;
- struct dfs_fd *d;
+ int result;
+ struct dfs_fd *d;
- d = fd_get(fd);
- if (d == RT_NULL)
- {
- rt_set_errno(-DFS_STATUS_EBADF);
- return -1;
- }
+ d = fd_get(fd);
+ if (d == RT_NULL)
+ {
+ rt_set_errno(-DFS_STATUS_EBADF);
- result = dfs_file_close(d);
- fd_put(d);
+ return -1;
+ }
- if (result < 0)
- {
- rt_set_errno(result);
+ result = dfs_file_close(d);
+ fd_put(d);
- return -1;
- }
+ if (result < 0)
+ {
+ rt_set_errno(result);
- fd_put(d);
+ return -1;
+ }
- return 0;
+ fd_put(d);
+
+ return 0;
}
RTM_EXPORT(close);
/**
- * this function is a POSIX compliant version, which will read specified data buffer
- * length for an open file descriptor.
+ * this function is a POSIX compliant version, which will read specified data
+ * buffer length for an open file descriptor.
*
* @param fd the file descriptor.
* @param buf the buffer to save the read data.
@@ -111,36 +114,37 @@ RTM_EXPORT(close); */
int read(int fd, void *buf, size_t len)
{
- int result;
- struct dfs_fd *d;
+ int result;
+ struct dfs_fd *d;
+
+ /* get the fd */
+ d = fd_get(fd);
+ if (d == RT_NULL)
+ {
+ rt_set_errno(-DFS_STATUS_EBADF);
- /* get the fd */
- d = fd_get(fd);
- if (d == RT_NULL)
- {
- rt_set_errno(-DFS_STATUS_EBADF);
- return -1;
- }
+ return -1;
+ }
- result = dfs_file_read(d, buf, len);
- if (result < 0)
- {
- fd_put(d);
- rt_set_errno(result);
+ result = dfs_file_read(d, buf, len);
+ if (result < 0)
+ {
+ fd_put(d);
+ rt_set_errno(result);
- return -1;
- }
+ return -1;
+ }
- /* release the ref-count of fd */
- fd_put(d);
+ /* release the ref-count of fd */
+ fd_put(d);
- return result;
+ return result;
}
RTM_EXPORT(read);
/**
- * this function is a POSIX compliant version, which will write specified data buffer
- * length for an open file descriptor.
+ * this function is a POSIX compliant version, which will write specified data
+ * buffer length for an open file descriptor.
*
* @param fd the file descriptor
* @param buf the data buffer to be written.
@@ -150,37 +154,37 @@ RTM_EXPORT(read); */
int write(int fd, const void *buf, size_t len)
{
- int result;
- struct dfs_fd *d;
+ int result;
+ struct dfs_fd *d;
- /* get the fd */
- d = fd_get(fd);
- if (d == RT_NULL)
- {
- rt_set_errno(-DFS_STATUS_EBADF);
+ /* get the fd */
+ d = fd_get(fd);
+ if (d == RT_NULL)
+ {
+ rt_set_errno(-DFS_STATUS_EBADF);
- return -1;
- }
+ return -1;
+ }
- result = dfs_file_write(d, buf, len);
- if (result < 0)
- {
- fd_put(d);
- rt_set_errno(result);
+ result = dfs_file_write(d, buf, len);
+ if (result < 0)
+ {
+ fd_put(d);
+ rt_set_errno(result);
- return -1;
- }
+ return -1;
+ }
- /* release the ref-count of fd */
- fd_put(d);
+ /* release the ref-count of fd */
+ fd_put(d);
- return result;
+ return result;
}
RTM_EXPORT(write);
/**
- * this function is a POSIX compliant version, which will seek the offset for an
- * open file descriptor.
+ * this function is a POSIX compliant version, which will seek the offset for
+ * an open file descriptor.
*
* @param fd the file descriptor.
* @param offset the offset to be seeked.
@@ -190,59 +194,61 @@ RTM_EXPORT(write); */
off_t lseek(int fd, off_t offset, int whence)
{
- int result;
- struct dfs_fd *d;
+ int result;
+ struct dfs_fd *d;
- d = fd_get(fd);
- if (d == RT_NULL)
- {
- rt_set_errno(-DFS_STATUS_EBADF);
- return -1;
- }
+ d = fd_get(fd);
+ if (d == RT_NULL)
+ {
+ rt_set_errno(-DFS_STATUS_EBADF);
- switch (whence)
- {
- case DFS_SEEK_SET:
- break;
+ return -1;
+ }
- case DFS_SEEK_CUR:
- offset += d->pos;
- break;
+ switch (whence)
+ {
+ case DFS_SEEK_SET:
+ break;
- case DFS_SEEK_END:
- offset += d->size;
- break;
+ case DFS_SEEK_CUR:
+ offset += d->pos;
+ break;
- default:
- rt_set_errno(-DFS_STATUS_EINVAL);
- return -1;
- }
+ case DFS_SEEK_END:
+ offset += d->size;
+ break;
- if (offset < 0)
- {
- rt_set_errno(-DFS_STATUS_EINVAL);
+ default:
+ rt_set_errno(-DFS_STATUS_EINVAL);
- return -1;
- }
- result = dfs_file_lseek(d, offset);
- if (result < 0)
- {
- fd_put(d);
- rt_set_errno(result);
+ return -1;
+ }
- return -1;
- }
+ if (offset < 0)
+ {
+ rt_set_errno(-DFS_STATUS_EINVAL);
- /* release the ref-count of fd */
- fd_put(d);
+ return -1;
+ }
+ result = dfs_file_lseek(d, offset);
+ if (result < 0)
+ {
+ fd_put(d);
+ rt_set_errno(result);
- return offset;
+ return -1;
+ }
+
+ /* release the ref-count of fd */
+ fd_put(d);
+
+ return offset;
}
RTM_EXPORT(lseek);
/**
- * this function is a POSIX compliant version, which will rename old file name to
- * new file name.
+ * this function is a POSIX compliant version, which will rename old file name
+ * to new file name.
*
* @param old the old file name.
* @param new the new file name.
@@ -253,17 +259,17 @@ RTM_EXPORT(lseek); */
int rename(const char *old, const char *new)
{
- int result;
+ int result;
- result = dfs_file_rename(old, new);
- if (result < 0)
- {
- rt_set_errno(result);
+ result = dfs_file_rename(old, new);
+ if (result < 0)
+ {
+ rt_set_errno(result);
- return -1;
- }
+ return -1;
+ }
- return 0;
+ return 0;
}
RTM_EXPORT(rename);
@@ -277,17 +283,17 @@ RTM_EXPORT(rename); */
int unlink(const char *pathname)
{
- int result;
+ int result;
- result = dfs_file_unlink(pathname);
- if (result < 0)
- {
- rt_set_errno(result);
+ result = dfs_file_unlink(pathname);
+ if (result < 0)
+ {
+ rt_set_errno(result);
- return -1;
- }
+ return -1;
+ }
- return 0;
+ return 0;
}
RTM_EXPORT(unlink);
@@ -301,17 +307,17 @@ RTM_EXPORT(unlink); */
int stat(const char *file, struct stat *buf)
{
- int result;
+ int result;
- result = dfs_file_stat(file, buf);
- if (result < 0)
- {
- rt_set_errno(result);
+ result = dfs_file_stat(file, buf);
+ if (result < 0)
+ {
+ rt_set_errno(result);
- return -1;
- }
+ return -1;
+ }
- return result;
+ return result;
}
RTM_EXPORT(stat);
@@ -323,35 +329,35 @@ RTM_EXPORT(stat); */
int fstat(int fildes, struct stat *buf)
{
- struct dfs_fd *d;
+ struct dfs_fd *d;
- /* get the fd */
- d = fd_get(fildes);
- if (d == RT_NULL)
- {
- rt_set_errno(-DFS_STATUS_EBADF);
+ /* get the fd */
+ d = fd_get(fildes);
+ if (d == RT_NULL)
+ {
+ rt_set_errno(-DFS_STATUS_EBADF);
- return -1;
- }
+ return -1;
+ }
- /* it's the root directory */
- buf->st_dev = 0;
+ /* it's the root directory */
+ buf->st_dev = 0;
- buf->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
- DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
- if (d->type == FT_DIRECTORY)
- {
- buf->st_mode &= ~DFS_S_IFREG;
- buf->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
- }
+ buf->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
+ DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
+ if (d->type == FT_DIRECTORY)
+ {
+ buf->st_mode &= ~DFS_S_IFREG;
+ buf->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
+ }
- buf->st_size = d->size;
- buf->st_mtime = 0;
- buf->st_blksize = 512;
+ buf->st_size = d->size;
+ buf->st_mtime = 0;
+ buf->st_blksize = 512;
- fd_put(d);
+ fd_put(d);
- return DFS_STATUS_OK;
+ return DFS_STATUS_OK;
}
RTM_EXPORT(fstat);
@@ -366,17 +372,17 @@ RTM_EXPORT(fstat); */
int statfs(const char *path, struct statfs *buf)
{
- int result;
+ int result;
- result = dfs_statfs(path, buf);
- if (result < 0)
- {
- rt_set_errno(result);
+ result = dfs_statfs(path, buf);
+ if (result < 0)
+ {
+ rt_set_errno(result);
- return -1;
- }
+ return -1;
+ }
- return result;
+ return result;
}
RTM_EXPORT(statfs);
@@ -390,34 +396,34 @@ RTM_EXPORT(statfs); */
int mkdir(const char *path, mode_t mode)
{
- int fd;
- struct dfs_fd *d;
- int result;
+ int fd;
+ struct dfs_fd *d;
+ int result;
- fd = fd_new();
- if (fd == -1)
- {
- rt_set_errno(-DFS_STATUS_ENOMEM);
+ fd = fd_new();
+ if (fd == -1)
+ {
+ rt_set_errno(-DFS_STATUS_ENOMEM);
- return -1;
- }
+ return -1;
+ }
- d = fd_get(fd);
+ d = fd_get(fd);
- result = dfs_file_open(d, path, DFS_O_DIRECTORY | DFS_O_CREAT);
+ result = dfs_file_open(d, path, DFS_O_DIRECTORY | DFS_O_CREAT);
- if (result < 0)
- {
- fd_put(d);
- rt_set_errno(result);
+ if (result < 0)
+ {
+ fd_put(d);
+ rt_set_errno(result);
- return -1;
- }
+ return -1;
+ }
- dfs_file_close(d);
- fd_put(d);
+ dfs_file_close(d);
+ fd_put(d);
- return 0;
+ return 0;
}
RTM_EXPORT(mkdir);
@@ -435,17 +441,17 @@ FINSH_FUNCTION_EXPORT(mkdir, create a directory); */
int rmdir(const char *pathname)
{
- int result;
+ int result;
- result = dfs_file_unlink(pathname);
- if (result < 0)
- {
- rt_set_errno(result);
+ result = dfs_file_unlink(pathname);
+ if (result < 0)
+ {
+ rt_set_errno(result);
- return -1;
- }
+ return -1;
+ }
- return 0;
+ return 0;
}
RTM_EXPORT(rmdir);
@@ -458,48 +464,48 @@ RTM_EXPORT(rmdir); */
DIR *opendir(const char *name)
{
- struct dfs_fd *d;
- int fd, result;
- DIR *t;
+ struct dfs_fd *d;
+ int fd, result;
+ DIR *t;
- t = RT_NULL;
+ t = RT_NULL;
- /* allocate a fd */
- fd = fd_new();
- if (fd == -1)
- {
- rt_set_errno(-DFS_STATUS_ENOMEM);
+ /* allocate a fd */
+ fd = fd_new();
+ if (fd == -1)
+ {
+ rt_set_errno(-DFS_STATUS_ENOMEM);
- return RT_NULL;
- }
- d = fd_get(fd);
+ return RT_NULL;
+ }
+ d = fd_get(fd);
- result = dfs_file_open(d, name, DFS_O_RDONLY | DFS_O_DIRECTORY);
- if (result >= 0)
- {
- /* open successfully */
- t = (DIR *) rt_malloc(sizeof(DIR));
- if (t == RT_NULL)
- {
- dfs_file_close(d);
- fd_put(d);
- }
- else
- {
- rt_memset(t, 0, sizeof(DIR));
- t->fd = fd;
- }
- fd_put(d);
+ result = dfs_file_open(d, name, DFS_O_RDONLY | DFS_O_DIRECTORY);
+ if (result >= 0)
+ {
+ /* open successfully */
+ t = (DIR *) rt_malloc(sizeof(DIR));
+ if (t == RT_NULL)
+ {
+ dfs_file_close(d);
+ fd_put(d);
+ }
+ else
+ {
+ rt_memset(t, 0, sizeof(DIR));
+ t->fd = fd;
+ }
+ fd_put(d);
- return t;
- }
+ return t;
+ }
- /* open failed */
- fd_put(d);
- fd_put(d);
- rt_set_errno(result);
+ /* open failed */
+ fd_put(d);
+ fd_put(d);
+ rt_set_errno(result);
- return RT_NULL;
+ return RT_NULL;
}
RTM_EXPORT(opendir);
@@ -514,36 +520,39 @@ RTM_EXPORT(opendir); */
struct dirent *readdir(DIR *d)
{
- int result;
- struct dfs_fd *fd;
+ int result;
+ struct dfs_fd *fd;
- fd = fd_get(d->fd);
- if (fd == RT_NULL)
- {
- rt_set_errno(-DFS_STATUS_EBADF);
+ fd = fd_get(d->fd);
+ if (fd == RT_NULL)
+ {
+ rt_set_errno(-DFS_STATUS_EBADF);
- return RT_NULL;
- }
+ return RT_NULL;
+ }
- if (!d->num || (d->cur += ((struct dirent*)(d->buf + d->cur))->d_reclen) >= d->num)
- {
- /* get a new entry */
- result = dfs_file_getdents(fd, (struct dirent*)d->buf, sizeof(d->buf) - 1);
- if (result <= 0)
- {
- fd_put(fd);
- rt_set_errno(result);
+ if (!d->num ||
+ (d->cur += ((struct dirent *)(d->buf + d->cur))->d_reclen) >= d->num)
+ {
+ /* get a new entry */
+ result = dfs_file_getdents(fd,
+ (struct dirent*)d->buf,
+ sizeof(d->buf) - 1);
+ if (result <= 0)
+ {
+ fd_put(fd);
+ rt_set_errno(result);
- return RT_NULL;
- }
+ return RT_NULL;
+ }
- d->num = result;
- d->cur = 0; /* current entry index */
- }
+ d->num = result;
+ d->cur = 0; /* current entry index */
+ }
- fd_put(fd);
+ fd_put(fd);
- return (struct dirent *)(d->buf+d->cur);
+ return (struct dirent *)(d->buf+d->cur);
}
RTM_EXPORT(readdir);
@@ -557,21 +566,21 @@ RTM_EXPORT(readdir); */
long telldir(DIR *d)
{
- struct dfs_fd *fd;
- long result;
+ struct dfs_fd *fd;
+ long result;
- fd = fd_get(d->fd);
- if (fd == RT_NULL)
- {
- rt_set_errno(-DFS_STATUS_EBADF);
+ fd = fd_get(d->fd);
+ if (fd == RT_NULL)
+ {
+ rt_set_errno(-DFS_STATUS_EBADF);
- return 0;
- }
+ return 0;
+ }
- result = fd->pos - d->num + d->cur;
- fd_put(fd);
+ result = fd->pos - d->num + d->cur;
+ fd_put(fd);
- return result;
+ return result;
}
RTM_EXPORT(telldir);
@@ -584,44 +593,45 @@ RTM_EXPORT(telldir); */
void seekdir(DIR *d, off_t offset)
{
- struct dfs_fd *fd;
+ struct dfs_fd *fd;
- fd = fd_get(d->fd);
- if (fd == RT_NULL)
- {
- rt_set_errno(-DFS_STATUS_EBADF);
+ fd = fd_get(d->fd);
+ if (fd == RT_NULL)
+ {
+ rt_set_errno(-DFS_STATUS_EBADF);
- return ;
- }
+ return ;
+ }
- /* seek to the offset position of directory */
- if (dfs_file_lseek(fd, offset) >= 0)
- d->num = d->cur = 0;
- fd_put(fd);
+ /* seek to the offset position of directory */
+ if (dfs_file_lseek(fd, offset) >= 0)
+ d->num = d->cur = 0;
+ fd_put(fd);
}
RTM_EXPORT(seekdir);
/**
- * this function is a POSIX compliant version, which will reset directory stream.
+ * this function is a POSIX compliant version, which will reset directory
+ * stream.
*
* @param d the directory stream.
*/
void rewinddir(DIR *d)
{
- struct dfs_fd *fd;
+ struct dfs_fd *fd;
- fd = fd_get(d->fd);
- if (fd == RT_NULL)
- {
- rt_set_errno(-DFS_STATUS_EBADF);
+ fd = fd_get(d->fd);
+ if (fd == RT_NULL)
+ {
+ rt_set_errno(-DFS_STATUS_EBADF);
- return ;
- }
+ return ;
+ }
- /* seek to the beginning of directory */
- if (dfs_file_lseek(fd, 0) >= 0)
- d->num = d->cur = 0;
- fd_put(fd);
+ /* seek to the beginning of directory */
+ if (dfs_file_lseek(fd, 0) >= 0)
+ d->num = d->cur = 0;
+ fd_put(fd);
}
RTM_EXPORT(rewinddir);
@@ -635,37 +645,38 @@ RTM_EXPORT(rewinddir); */
int closedir(DIR *d)
{
- int result;
- struct dfs_fd *fd;
+ int result;
+ struct dfs_fd *fd;
- fd = fd_get(d->fd);
- if (fd == RT_NULL)
- {
- rt_set_errno(-DFS_STATUS_EBADF);
+ fd = fd_get(d->fd);
+ if (fd == RT_NULL)
+ {
+ rt_set_errno(-DFS_STATUS_EBADF);
- return -1;
- }
+ return -1;
+ }
- result = dfs_file_close(fd);
- fd_put(fd);
+ result = dfs_file_close(fd);
+ fd_put(fd);
- fd_put(fd);
- rt_free(d);
+ fd_put(fd);
+ rt_free(d);
- if (result < 0)
- {
- rt_set_errno(result);
+ if (result < 0)
+ {
+ rt_set_errno(result);
- return -1;
- }
- else
- return 0;
+ return -1;
+ }
+ else
+ return 0;
}
RTM_EXPORT(closedir);
#ifdef DFS_USING_WORKDIR
/**
- * this function is a POSIX compliant version, which will change working directory.
+ * this function is a POSIX compliant version, which will change working
+ * directory.
*
* @param path the path name to be changed to.
*
@@ -673,54 +684,55 @@ RTM_EXPORT(closedir); */
int chdir(const char *path)
{
- char *fullpath;
- DIR *d;
+ char *fullpath;
+ DIR *d;
- if (path == RT_NULL)
- {
- dfs_lock();
- rt_kprintf("%s\n", working_directory);
- dfs_unlock();
+ if (path == RT_NULL)
+ {
+ dfs_lock();
+ rt_kprintf("%s\n", working_directory);
+ dfs_unlock();
- return 0;
- }
+ return 0;
+ }
- if (rt_strlen(path) > DFS_PATH_MAX)
- {
- rt_set_errno(-DFS_STATUS_ENOTDIR);
+ if (rt_strlen(path) > DFS_PATH_MAX)
+ {
+ rt_set_errno(-DFS_STATUS_ENOTDIR);
- return -1;
- }
+ return -1;
+ }
- fullpath = dfs_normalize_path(NULL, path);
- if (fullpath == RT_NULL)
- {
- rt_set_errno(-DFS_STATUS_ENOTDIR);
+ fullpath = dfs_normalize_path(NULL, path);
+ if (fullpath == RT_NULL)
+ {
+ rt_set_errno(-DFS_STATUS_ENOTDIR);
- return -1; /* build path failed */
- }
+ return -1; /* build path failed */
+ }
- dfs_lock();
- d = opendir(fullpath);
- if (d == RT_NULL)
- {
- rt_free(fullpath);
- /* this is a not exist directory */
- dfs_unlock();
+ dfs_lock();
+ d = opendir(fullpath);
+ if (d == RT_NULL)
+ {
+ rt_free(fullpath);
+ /* this is a not exist directory */
+ dfs_unlock();
- return -1;
- }
+ return -1;
+ }
- /* close directory stream */
- closedir(d);
+ /* close directory stream */
+ closedir(d);
- /* copy full path to working directory */
- strncpy(working_directory, fullpath, DFS_PATH_MAX);
- rt_free(fullpath); /* release normalize directory path name */
+ /* copy full path to working directory */
+ strncpy(working_directory, fullpath, DFS_PATH_MAX);
+ /* release normalize directory path name */
+ rt_free(fullpath);
- dfs_unlock();
+ dfs_unlock();
- return 0;
+ return 0;
}
RTM_EXPORT(chdir);
@@ -741,14 +753,14 @@ FINSH_FUNCTION_EXPORT_ALIAS(chdir, cd, change current working directory); char *getcwd(char *buf, size_t size)
{
#ifdef DFS_USING_WORKDIR
- rt_enter_critical();
- rt_strncpy(buf, working_directory, size);
- rt_exit_critical();
+ rt_enter_critical();
+ rt_strncpy(buf, working_directory, size);
+ rt_exit_critical();
#else
- rt_kprintf("WARNING: not support working directory\n");
+ rt_kprintf("WARNING: not support working directory\n");
#endif
- return buf;
+ return buf;
}
RTM_EXPORT(getcwd);
|
