Browse Source

add dfs_fd check code in dfs and fix a warning.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2547 bbd45198-f89e-11dd-88c7-29a3b14d5316
goprife@gmail.com 12 years ago
parent
commit
58fc3334b0
2 changed files with 12 additions and 2 deletions
  1. 3 1
      components/dfs/include/dfs_def.h
  2. 9 1
      components/dfs/src/dfs.c

+ 3 - 1
components/dfs/include/dfs_def.h

@@ -282,10 +282,12 @@ struct dirent
 #endif
 #endif
 
 
 /* file descriptor */
 /* file descriptor */
+#define DFS_FD_MAGIC	 0xfdfd
 struct dfs_fd
 struct dfs_fd
 {
 {
+    rt_uint16_t magic;           /* file descriptor magic number */
+    rt_uint16_t type;            /* Type (regular or socket) */
     char *path;                  /* Name (below mount point) */
     char *path;                  /* Name (below mount point) */
-    int type;                    /* Type (regular or socket) */
     int ref_count;               /* Descriptor reference count */
     int ref_count;               /* Descriptor reference count */
 
 
     struct dfs_filesystem *fs;   /* Resident file system */
     struct dfs_filesystem *fs;   /* Resident file system */

+ 9 - 1
components/dfs/src/dfs.c

@@ -47,7 +47,7 @@ struct dfs_fd fd_table[DFS_FD_MAX];
 void dfs_init(void)
 void dfs_init(void)
 {
 {
     /* clear filesystem operations table */
     /* clear filesystem operations table */
-    rt_memset(filesystem_operation_table, 0, sizeof(filesystem_operation_table));
+    rt_memset((void *)filesystem_operation_table, 0, sizeof(filesystem_operation_table));
     /* clear filesystem table */
     /* clear filesystem table */
     rt_memset(filesystem_table, 0, sizeof(filesystem_table));
     rt_memset(filesystem_table, 0, sizeof(filesystem_table));
     /* clean fd table */
     /* clean fd table */
@@ -123,6 +123,7 @@ int fd_new(void)
 
 
     d = &(fd_table[idx]);
     d = &(fd_table[idx]);
     d->ref_count = 1;
     d->ref_count = 1;
+    d->magic = DFS_FD_MAGIC;
 
 
 __result:
 __result:
     dfs_unlock();
     dfs_unlock();
@@ -153,6 +154,13 @@ struct dfs_fd *fd_get(int fd)
     dfs_lock();
     dfs_lock();
     d = &fd_table[fd];
     d = &fd_table[fd];
 
 
+    /* check dfs_fd valid or not */
+    if (d->magic != DFS_FD_MAGIC)
+    {
+        dfs_unlock();
+        return RT_NULL;
+    }
+
     /* increase the reference count */
     /* increase the reference count */
     d->ref_count ++;
     d->ref_count ++;
     dfs_unlock();
     dfs_unlock();