diff options
| author | dzzxzz <[email protected]> | 2011-12-23 02:11:55 +0000 |
|---|---|---|
| committer | dzzxzz <[email protected]> | 2011-12-23 02:11:55 +0000 |
| commit | 44a2d720a02775987dca11dc6aab74e55c355a59 (patch) | |
| tree | 821dc316fa822ed9a8c56a1513a4b1fd66c8e65b /components/dfs/src | |
| parent | 701df33436f2c88dc2c8b6912f377e978c6ab6d7 (diff) | |
unify the components/dfs coding style according to the /documentation/coding_style_cn.txt
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1866 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'components/dfs/src')
| -rw-r--r-- | components/dfs/src/dfs.c | 75 | ||||
| -rw-r--r-- | components/dfs/src/dfs_file.c | 135 | ||||
| -rw-r--r-- | components/dfs/src/dfs_fs.c | 437 | ||||
| -rw-r--r-- | components/dfs/src/dfs_posix.c | 71 |
4 files changed, 383 insertions, 335 deletions
diff --git a/components/dfs/src/dfs.c b/components/dfs/src/dfs.c index bf042a1b58..33b8f497a9 100644 --- a/components/dfs/src/dfs.c +++ b/components/dfs/src/dfs.c @@ -1,7 +1,7 @@ /* * File : dfs.c * This file is part of Device File System in RT-Thread RTOS - * COPYRIGHT (C) 2004-2010, RT-Thread Development Team + * COPYRIGHT (C) 2004-2011, RT-Thread Development Team * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at @@ -10,7 +10,6 @@ * Change Logs: * Date Author Notes * 2005-02-22 Bernard The first version. - * 2010-07-16 */ #include <dfs.h> @@ -20,7 +19,7 @@ #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]; +const struct dfs_filesystem_operation *filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX]; struct dfs_filesystem filesystem_table[DFS_FILESYSTEMS_MAX]; /* device filesystem lock */ @@ -44,7 +43,7 @@ struct dfs_fd fd_table[DFS_FD_MAX]; /** * this function will initialize device file system. */ -void dfs_init() +void dfs_init(void) { /* clear filesystem operations table */ rt_memset(filesystem_operation_table, 0, sizeof(filesystem_operation_table)); @@ -68,7 +67,7 @@ void dfs_init() * * @note please don't invoke it on ISR. */ -void dfs_lock() +void dfs_lock(void) { rt_err_t result; @@ -84,7 +83,7 @@ void dfs_lock() * * @note please don't invoke it on ISR. */ -void dfs_unlock() +void dfs_unlock(void) { rt_mutex_release(&fslock); } @@ -97,7 +96,7 @@ void dfs_unlock() */ int fd_new(void) { - struct dfs_fd* d; + struct dfs_fd *d; int idx; /* lock filesystem */ @@ -138,14 +137,16 @@ __result: * @return NULL on on this file descriptor or the file descriptor structure * pointer. */ -struct dfs_fd* fd_get(int fd) +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(); @@ -163,13 +164,13 @@ struct dfs_fd* fd_get(int fd) * * This function will put the file descriptor. */ -void fd_put(struct dfs_fd* fd) +void fd_put(struct dfs_fd *fd) { dfs_lock(); fd->ref_count --; /* clear this fd entry */ - if ( fd->ref_count == 0 ) + if (fd->ref_count == 0) { rt_memset(fd, 0, sizeof(struct dfs_fd)); } @@ -185,12 +186,12 @@ void fd_put(struct dfs_fd* fd) * * @return 0 on file has been open successfully, -1 on open failed. */ -int fd_is_open(const char* pathname) +int fd_is_open(const char *pathname) { char *fullpath; unsigned int index; - struct dfs_filesystem* fs; - struct dfs_fd* fd; + struct dfs_filesystem *fs; + struct dfs_fd *fd; fullpath = dfs_normalize_path(RT_NULL, pathname); if (fullpath != RT_NULL) @@ -207,16 +208,17 @@ int fd_is_open(const char* pathname) /* get file path name under mounted file system */ if (fs->path[0] == '/' && fs->path[1] == '\0') mountpath = fullpath; - else mountpath = fullpath + strlen(fs->path); + 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; + if (fd->fs == RT_NULL) + continue; - if (fd->fs == fs && - strcmp(fd->path, mountpath) == 0) + if (fd->fs == fs && strcmp(fd->path, mountpath) == 0) { /* found file in file descriptor table */ rt_free(fullpath); @@ -240,9 +242,9 @@ int fd_is_open(const char* pathname) * * @return the subdir pointer in filename */ -const char* dfs_subdir(const char* directory, const char* filename) +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; @@ -263,7 +265,7 @@ const char* dfs_subdir(const char* directory, const char* filename) * * @return the built full file path (absoluted path) */ -char* dfs_normalize_path(const char* directory, const char* filename) +char *dfs_normalize_path(const char *directory, const char *filename) { char *fullpath; char *dst0, *dst, *src; @@ -311,7 +313,8 @@ char* dfs_normalize_path(const char* directory, const char* filename) /* './' case */ src += 2; - while ((*src == '/') && (*src != '\0')) src ++; + while ((*src == '/') && (*src != '\0')) + src ++; continue; } else if (src[1] == '.') @@ -327,37 +330,47 @@ char* dfs_normalize_path(const char* directory, const char* filename) /* '../' case */ src += 3; - while ((*src == '/') && (*src != '\0')) src ++; + while ((*src == '/') && (*src != '\0')) + src ++; goto up_one; } } } /* copy up the next '/' and erase all '/' */ - while ((c = *src++) != '\0' && c != '/') *dst ++ = c; + while ((c = *src++) != '\0' && c != '/') + *dst ++ = c; if (c == '/') { *dst ++ = '/'; - while (c == '/') c = *src++; + while (c == '/') + c = *src++; src --; } - else if (!c) break; + else if (!c) + break; continue; up_one: dst --; - if (dst < dst0) { rt_free(fullpath); return NULL;} - while (dst0 < dst && dst[-1] != '/') dst --; + if (dst < dst0) + { + rt_free(fullpath); + return NULL; + } + while (dst0 < dst && dst[-1] != '/') + dst --; } *dst = '\0'; /* remove '/' in the end of path if exist */ dst --; - if ((dst != fullpath) && (*dst == '/')) *dst = '\0'; + if ((dst != fullpath) && (*dst == '/')) + *dst = '\0'; return fullpath; } diff --git a/components/dfs/src/dfs_file.c b/components/dfs/src/dfs_file.c index f102c15d32..2746a11b66 100644 --- a/components/dfs/src/dfs_file.c +++ b/components/dfs/src/dfs_file.c @@ -1,7 +1,7 @@ /* * File : dfs_file.c * This file is part of Device File System in RT-Thread RTOS - * COPYRIGHT (C) 2004-2010, RT-Thread Development Team + * COPYRIGHT (C) 2004-2011, RT-Thread Development Team * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at @@ -11,6 +11,7 @@ * Date Author Notes * 2005-02-22 Bernard The first version. */ + #include <dfs.h> #include <dfs_file.h> @@ -28,14 +29,15 @@ * * @return 0 on successful, -1 on failed. */ -int dfs_file_open(struct dfs_fd* fd, const char *path, int flags) +int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) { - struct dfs_filesystem* fs; + struct dfs_filesystem *fs; char *fullpath; int result; /* parameter check */ - if ( fd == RT_NULL ) return -DFS_STATUS_EINVAL; + if (fd == RT_NULL) + return -DFS_STATUS_EINVAL; /* make sure we have an absolute path */ fullpath = dfs_normalize_path(RT_NULL, path); @@ -48,7 +50,7 @@ int dfs_file_open(struct dfs_fd* fd, const char *path, int flags) /* find filesystem */ fs = dfs_filesystem_lookup(fullpath); - if ( fs == RT_NULL ) + if (fs == RT_NULL) { rt_free(fullpath); /* release path */ return -DFS_STATUS_ENOENT; @@ -92,7 +94,7 @@ int dfs_file_open(struct dfs_fd* fd, const char *path, int flags) } fd->flags |= DFS_F_OPEN; - if ( flags & DFS_O_DIRECTORY ) + if (flags & DFS_O_DIRECTORY) { fd->type = FT_DIRECTORY; fd->flags |= DFS_F_DIRECTORY; @@ -109,14 +111,16 @@ int dfs_file_open(struct dfs_fd* fd, const char *path, int flags) * * @return 0 on successful, -1 on failed. */ -int dfs_file_close(struct dfs_fd* fd) +int dfs_file_close(struct dfs_fd *fd) { 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; + if (result < 0) + return result; rt_free(fd->path); rt_memset(fd, 0, sizeof(struct dfs_fd)); @@ -133,14 +137,16 @@ int dfs_file_close(struct dfs_fd* fd) * * @return 0 on successful, -1 on failed. */ -int dfs_file_ioctl(struct dfs_fd* fd, int cmd, void *args) +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); + if (fs->ops->ioctl != RT_NULL) + return fs->ops->ioctl(fd, cmd, args); return -DFS_STATUS_ENOSYS; } @@ -154,17 +160,20 @@ int dfs_file_ioctl(struct dfs_fd* fd, int cmd, void *args) * * @return the actual read data bytes or 0 on end of file or failed. */ -int dfs_file_read(struct dfs_fd* fd, void *buf, rt_size_t len) +int dfs_file_read(struct dfs_fd *fd, void *buf, rt_size_t len) { - struct dfs_filesystem* fs; + 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; } @@ -178,15 +187,17 @@ int dfs_file_read(struct dfs_fd* fd, void *buf, rt_size_t len) * * @return the read dirent, others on failed. */ -int dfs_file_getdents(struct dfs_fd* fd, struct dirent* dirp, rt_size_t nbytes) +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; + 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; } @@ -202,19 +213,19 @@ int dfs_file_unlink(const char *path) { int result; char *fullpath; - struct dfs_filesystem* fs; + struct dfs_filesystem *fs; result = DFS_STATUS_OK; /* Make sure we have an absolute path */ fullpath = dfs_normalize_path(RT_NULL, path); - if ( fullpath == RT_NULL) + if (fullpath == RT_NULL) { return -DFS_STATUS_EINVAL; } /* get filesystem */ - if ( (fs = dfs_filesystem_lookup(fullpath)) == RT_NULL) + if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL) { result = -DFS_STATUS_ENOENT; goto __exit; @@ -250,14 +261,16 @@ __exit: * * @return the actual written data length. */ -int dfs_file_write(struct dfs_fd* fd, const void *buf, rt_size_t len) +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; + if (fs->ops->write == RT_NULL) + return -DFS_STATUS_ENOSYS; return fs->ops->write(fd, buf, len); } @@ -269,14 +282,16 @@ int dfs_file_write(struct dfs_fd* fd, const void *buf, rt_size_t len) * * @return 0 on successful, -1 on failed. */ -int dfs_file_flush(struct dfs_fd* fd) +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; + if (fs->ops->flush == RT_NULL) + return -DFS_STATUS_ENOSYS; return fs->ops->flush(fd); } @@ -289,13 +304,15 @@ int dfs_file_flush(struct dfs_fd* fd) * * @return the current position after seek. */ -int dfs_file_lseek(struct dfs_fd* fd, rt_off_t offset) +int dfs_file_lseek(struct dfs_fd *fd, rt_off_t offset) { int result; - struct dfs_filesystem* fs = fd->fs; + 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); @@ -317,11 +334,11 @@ 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; + char *fullpath; + struct dfs_filesystem *fs; fullpath = dfs_normalize_path(RT_NULL, path); - if ( fullpath == RT_NULL ) + if (fullpath == RT_NULL) { return -1; } @@ -339,7 +356,7 @@ int dfs_file_stat(const char *path, struct stat *buf) /* it's the root directory */ buf->st_dev = 0; - buf->st_mode = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH | + 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; @@ -378,7 +395,7 @@ int dfs_file_stat(const char *path, struct stat *buf) * * @return 0 on successful, -1 on failed. */ -int dfs_file_rename(const char* oldpath, const char* newpath) +int dfs_file_rename(const char *oldpath, const char *newpath) { int result; struct dfs_filesystem *oldfs, *newfs; @@ -388,34 +405,34 @@ int dfs_file_rename(const char* oldpath, const char* newpath) newfullpath = RT_NULL; oldfullpath = dfs_normalize_path(RT_NULL, oldpath); - if ( oldfullpath == RT_NULL ) + if (oldfullpath == RT_NULL) { result = -DFS_STATUS_ENOENT; goto __exit; } newfullpath = dfs_normalize_path(RT_NULL, newpath); - if ( newfullpath == RT_NULL ) + if (newfullpath == RT_NULL) { result = -DFS_STATUS_ENOENT; goto __exit; } - if ( (oldfs = dfs_filesystem_lookup(oldfullpath)) == RT_NULL ) + if ((oldfs = dfs_filesystem_lookup(oldfullpath)) == RT_NULL) { result = -DFS_STATUS_ENOENT; goto __exit; } - if ( (newfs = dfs_filesystem_lookup(newfullpath)) == RT_NULL ) + if ((newfs = dfs_filesystem_lookup(newfullpath)) == RT_NULL) { result = -DFS_STATUS_ENOENT; goto __exit; } - if ( oldfs == newfs ) + if (oldfs == newfs) { - if ( oldfs->ops->rename == RT_NULL ) + if (oldfs->ops->rename == RT_NULL) { result = -DFS_STATUS_ENOSYS; goto __exit; @@ -440,7 +457,7 @@ __exit: static struct dfs_fd fd; static struct dirent dirent; -void ls(const char* pathname) +void ls(const char *pathname) { struct stat stat; int length; @@ -459,24 +476,25 @@ void ls(const char* pathname) } else { - path = (char*)pathname; + path = (char *)pathname; } /* list directory */ - if ( dfs_file_open(&fd, path, DFS_O_DIRECTORY) == 0 ) + 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 ) + 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 (fullpath == RT_NULL) + break; if (dfs_file_stat(fullpath, &stat) == 0) { @@ -502,11 +520,12 @@ void ls(const char* pathname) { rt_kprintf("No such directory\n"); } - if (pathname == RT_NULL) rt_free(path); + if (pathname == RT_NULL) + rt_free(path); } FINSH_FUNCTION_EXPORT(ls, list directory contents) -void rm(const char* filename) +void rm(const char *filename) { if (dfs_file_unlink(filename) < 0) { @@ -541,7 +560,7 @@ void cat(const char* filename) FINSH_FUNCTION_EXPORT(cat, print file) #define BUF_SZ 4096 -void copy(const char* src, const char* dst) +void copy(const char *src, const char *dst) { struct dfs_fd src_fd; rt_uint8_t *block_ptr; diff --git a/components/dfs/src/dfs_fs.c b/components/dfs/src/dfs_fs.c index da1e456c7b..fc27fe354a 100644 --- a/components/dfs/src/dfs_fs.c +++ b/components/dfs/src/dfs_fs.c @@ -1,7 +1,7 @@ /* * File : dfs_fs.c * This file is part of Device File System in RT-Thread RTOS - * COPYRIGHT (C) 2004-2010, RT-Thread Development Team + * COPYRIGHT (C) 2004-2011, RT-Thread Development Team * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at @@ -13,6 +13,7 @@ * 2010-06-30 Bernard Optimize for RT-Thread RTOS * 2011-03-12 Bernard fix the filesystem lookup issue. */ + #include <dfs_fs.h> #include <dfs_file.h> @@ -28,43 +29,43 @@ * * @return 0 on sucessful, -1 on failed. */ -int dfs_register(const struct dfs_filesystem_operation* ops) +int dfs_register(const struct dfs_filesystem_operation *ops) { - int index, result; + int index, result; result = 0; /* 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) + /* 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; + return result; } /** @@ -75,42 +76,44 @@ err: * @return the found file system or NULL if no file system mounted on * specified path */ -struct dfs_filesystem* dfs_filesystem_lookup(const char *path) +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 */ + /* 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; + /* 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 (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(); - return fs; + return fs; } /** @@ -122,69 +125,66 @@ 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 */ - 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; + return result; + } - /* get partition type */ - type = *(dpt+4); + /* get partition type */ + type = *(dpt+4); - if (type != 0) - { - /* set partition type */ - part->type = type; + if (type != 0) + { + /* set partition type */ + part->type = type; - /* 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; + /* 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; - 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; - } + 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; + } - return result; + return result; } /** @@ -198,17 +198,17 @@ 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 */ + /* open specific device */ if (device_name != RT_NULL) { dev_id = rt_device_find(device_name); @@ -225,112 +225,117 @@ int dfs_mount(const char* device_name, const char* path, dev_id = RT_NULL; } - /* find out specific filesystem */ + /* 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; - } + 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(); - return -1; - } - ops = filesystem_operation_table[index]; - dfs_unlock(); + /* 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(); - /* make full path for special file */ + /* 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; - } + 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); - return -1; - } - dfs_file_close(&fd); - } + 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); + } - /* check whether the file system mounted or not */ + /* 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; - } - } + 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 en 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 en 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; + } - /* register file system */ - fs = &(filesystem_table[index]); + /* register file system */ + fs = &(filesystem_table[index]); fs->path = fullpath; - fs->ops = ops; - fs->dev_id = dev_id; - /* release filesystem_table lock */ + fs->ops = ops; + fs->dev_id = dev_id; + /* release filesystem_table lock */ 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); + if (dev_id != RT_NULL) + rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDWR); - 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)); + 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(); 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); + 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); - /* mount failed */ + /* mount failed */ dfs_lock(); - /* clear filesystem table entry */ - rt_memset(fs, 0, sizeof(struct dfs_filesystem)); + /* clear filesystem table entry */ + rt_memset(fs, 0, sizeof(struct dfs_filesystem)); dfs_unlock(); rt_free(fullpath); return -1; - } + } - return 0; + 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; } /** @@ -342,42 +347,42 @@ 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); - return -1; - } + fullpath = dfs_normalize_path(RT_NULL, specialfile); + if (fullpath == RT_NULL) + { + rt_set_errno(-DFS_STATUS_ENOTDIR); + 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 */ + /* close device, but do not check the status of device */ if (fs->dev_id != RT_NULL) - rt_device_close(fs->dev_id); + rt_device_close(fs->dev_id); - /* 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); - return 0; + return 0; err1: dfs_unlock(); rt_free(fullpath); - return -1; + return -1; } /** @@ -388,18 +393,18 @@ err1: * * @return 0 on successful, otherwise failed. */ -int dfs_mkfs(const char* fs_name, const char* device_name) +int dfs_mkfs(const char *fs_name, const char *device_name) { - int index; + int index; /* lock filesystem */ 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 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(); @@ -408,8 +413,8 @@ int dfs_mkfs(const char* fs_name, const char* device_name) return ops->mkfs(device_name); break; - } - } + } + } dfs_unlock(); return -1; @@ -423,9 +428,9 @@ int dfs_mkfs(const char* fs_name, const char* device_name) * * @return 0 on successful, others on failed. */ -int dfs_statfs(const char* path, struct statfs* buffer) +int dfs_statfs(const char *path, struct statfs *buffer) { - struct dfs_filesystem* fs; + struct dfs_filesystem *fs; fs = dfs_filesystem_lookup(path); if (fs != NULL) @@ -439,13 +444,13 @@ int dfs_statfs(const char* path, struct statfs* buffer) #ifdef RT_USING_FINSH #include <finsh.h> -void mkfs(const char* fs_name, const char* device_name) +void mkfs(const char *fs_name, const char *device_name) { dfs_mkfs(fs_name, device_name); } FINSH_FUNCTION_EXPORT(mkfs, make a file system); -void df(const char* path) +void df(const char *path) { struct statfs buffer; diff --git a/components/dfs/src/dfs_posix.c b/components/dfs/src/dfs_posix.c index 1c81e6c1fc..c21917d29a 100644 --- a/components/dfs/src/dfs_posix.c +++ b/components/dfs/src/dfs_posix.c @@ -1,7 +1,7 @@ /*
* File : dfs_posix.c
* This file is part of Device File System in RT-Thread RTOS
- * COPYRIGHT (C) 2004-2010, RT-Thread Development Team
+ * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
@@ -33,11 +33,12 @@ int open(const char *file, int flags, int mode)
{
int fd, result;
- struct dfs_fd* d;
+ struct dfs_fd *d;
/* allocate a fd */
fd = fd_new();
- if (fd < 0) return -1;
+ if (fd < 0)
+ return -1;
d = fd_get(fd);
result = dfs_file_open(d, file, flags);
@@ -68,7 +69,7 @@ int open(const char *file, int flags, int mode) int close(int fd)
{
int result;
- struct dfs_fd* d;
+ struct dfs_fd *d;
d = fd_get(fd);
if (d == RT_NULL)
@@ -103,7 +104,7 @@ int close(int fd) int read(int fd, void *buf, size_t len)
{
int result;
- struct dfs_fd* d;
+ struct dfs_fd *d;
/* get the fd */
d = fd_get(fd);
@@ -140,7 +141,7 @@ int read(int fd, void *buf, size_t len) int write(int fd, const void *buf, size_t len)
{
int result;
- struct dfs_fd* d;
+ struct dfs_fd *d;
/* get the fd */
d = fd_get(fd);
@@ -177,7 +178,7 @@ int write(int fd, const void *buf, size_t len) off_t lseek(int fd, off_t offset, int whence)
{
int result;
- struct dfs_fd* d;
+ struct dfs_fd *d;
d = fd_get(fd);
if (d == RT_NULL)
@@ -200,7 +201,7 @@ off_t lseek(int fd, off_t offset, int whence) break;
}
- if( offset < 0 )
+ if(offset < 0)
{
rt_set_errno(DFS_STATUS_EINVAL);
return -1;
@@ -229,7 +230,7 @@ off_t lseek(int fd, off_t offset, int whence) *
* note: the old and new file name must be belong to a same file system.
*/
-int rename(const char* old, const char* new)
+int rename(const char *old, const char *new)
{
int result;
@@ -292,7 +293,7 @@ int stat(const char *file, struct stat *buf) */
int fstat(int fildes, struct stat *buf)
{
- struct dfs_fd* d;
+ struct dfs_fd *d;
/* get the fd */
d = fd_get(fildes);
@@ -303,7 +304,7 @@ int fstat(int fildes, struct stat *buf) }
/* it's the root directory */
- buf->st_dev = 0;
+ 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;
@@ -356,11 +357,15 @@ int statfs(const char *path, struct statfs *buf) int mkdir (const char *path, mode_t mode)
{
int fd;
- struct dfs_fd* d;
+ struct dfs_fd *d;
int result;
fd = fd_new();
- if (fd == -1) { rt_kprintf("no fd\n"); return -1; }
+ if (fd == -1)
+ {
+ rt_kprintf("no fd\n");
+ return -1;
+ }
d = fd_get(fd);
@@ -410,17 +415,21 @@ int rmdir(const char *pathname) *
* @return the DIR pointer of directory, NULL on open failed.
*/
-DIR* opendir(const char* name)
+DIR *opendir(const char *name)
{
- struct dfs_fd* d;
+ struct dfs_fd *d;
int fd, result;
- DIR* t;
+ DIR *t;
t = RT_NULL;
/* allocate a fd */
fd = fd_new();
- if (fd == -1) { rt_kprintf("no fd\n"); return RT_NULL; }
+ if (fd == -1)
+ {
+ rt_kprintf("no fd\n");
+ return RT_NULL;
+ }
d = fd_get(fd);
result = dfs_file_open(d, name, DFS_O_RDONLY | DFS_O_DIRECTORY);
@@ -459,10 +468,10 @@ DIR* opendir(const char* name) *
* @return the next directory entry, NULL on the end of directory or failed.
*/
-struct dirent* readdir(DIR *d)
+struct dirent *readdir(DIR *d)
{
int result;
- struct dfs_fd* fd;
+ struct dfs_fd *fd;
fd = fd_get(d->fd);
if (fd == RT_NULL)
@@ -488,7 +497,7 @@ struct dirent* readdir(DIR *d) }
fd_put(fd);
- return (struct dirent*)(d->buf+d->cur);
+ return (struct dirent *)(d->buf+d->cur);
}
/**
@@ -501,7 +510,7 @@ struct dirent* readdir(DIR *d) */
long telldir(DIR *d)
{
- struct dfs_fd* fd;
+ struct dfs_fd *fd;
long result;
fd = fd_get(d->fd);
@@ -526,7 +535,7 @@ long telldir(DIR *d) */
void seekdir(DIR *d, off_t offset)
{
- struct dfs_fd* fd;
+ struct dfs_fd *fd;
fd = fd_get(d->fd);
if (fd == RT_NULL)
@@ -536,7 +545,8 @@ void seekdir(DIR *d, off_t offset) }
/* seek to the offset position of directory */
- if (dfs_file_lseek(fd, offset) >= 0) d->num = d->cur = 0;
+ if (dfs_file_lseek(fd, offset) >= 0)
+ d->num = d->cur = 0;
fd_put(fd);
}
@@ -547,7 +557,7 @@ void seekdir(DIR *d, off_t offset) */
void rewinddir(DIR *d)
{
- struct dfs_fd* fd;
+ struct dfs_fd *fd;
fd = fd_get(d->fd);
if (fd == RT_NULL)
@@ -557,7 +567,8 @@ void rewinddir(DIR *d) }
/* seek to the beginning of directory */
- if (dfs_file_lseek(fd, 0) >= 0) d->num = d->cur = 0;
+ if (dfs_file_lseek(fd, 0) >= 0)
+ d->num = d->cur = 0;
fd_put(fd);
}
@@ -569,10 +580,10 @@ void rewinddir(DIR *d) *
* @return 0 on successful, -1 on failed.
*/
-int closedir(DIR* d)
+int closedir(DIR *d)
{
int result;
- struct dfs_fd* fd;
+ struct dfs_fd *fd;
fd = fd_get(d->fd);
if (fd == RT_NULL)
@@ -605,10 +616,10 @@ int closedir(DIR* d) */
int chdir(const char *path)
{
- char* fullpath;
- DIR* d;
+ char *fullpath;
+ DIR *d;
- if(path == RT_NULL)
+ if (path == RT_NULL)
{
dfs_lock();
rt_kprintf("%s\n", working_directory);
|
