diff --git a/components/dfs/src/dfs.c b/components/dfs/src/dfs.c index 7ac596a2a03ec2cb50b78db8cba50ddbb68082c1..3db6e5169481a54510cf3cfd58ccd5beb5a3f9ff 100644 --- a/components/dfs/src/dfs.c +++ b/components/dfs/src/dfs.c @@ -142,10 +142,10 @@ struct dfs_fd *fd_get(int fd) struct dfs_fd *d; #ifdef DFS_USING_STDIO - if (fd < 3 || fd > DFS_FD_MAX + 3) + if (fd < 3 || fd >= DFS_FD_MAX + 3) return RT_NULL; #else - if (fd < 0 || fd > DFS_FD_MAX) + if (fd < 0 || fd >= DFS_FD_MAX) return RT_NULL; #endif @@ -166,6 +166,8 @@ struct dfs_fd *fd_get(int fd) */ void fd_put(struct dfs_fd *fd) { + RT_ASSERT(fd != RT_NULL); + dfs_lock(); fd->ref_count --; diff --git a/components/dfs/src/dfs_file.c b/components/dfs/src/dfs_file.c index 3c9897fc774bcbe531336d0747a09c1d3e0a02af..a9e4241ac220ddcd646f465f6dac81b09c50e4a8 100644 --- a/components/dfs/src/dfs_file.c +++ b/components/dfs/src/dfs_file.c @@ -60,7 +60,7 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) dfs_log(DFS_DEBUG_INFO, ("open in filesystem:%s", fs->ops->name)); fd->fs = fs; - /* initilize the fd item */ + /* initialize the fd item */ fd->type = FT_REGULAR; fd->flags = flags; fd->size = 0; diff --git a/components/dfs/src/dfs_posix.c b/components/dfs/src/dfs_posix.c index f9608e4ac592bd626acace6cb97d8dede186775e..ea6ba198108ee36196dea1c11f975ecd6076485c 100644 --- a/components/dfs/src/dfs_posix.c +++ b/components/dfs/src/dfs_posix.c @@ -38,7 +38,10 @@ int open(const char *file, int flags, int mode) /* allocate a fd */ fd = fd_new(); if (fd < 0) + { + rt_set_errno(-DFS_STATUS_ENOMEM); return -1; + } d = fd_get(fd); result = dfs_file_open(d, file, flags); @@ -74,7 +77,7 @@ int close(int fd) d = fd_get(fd); if (d == RT_NULL) { - rt_set_errno(-RT_ERROR); + rt_set_errno(-DFS_STATUS_EBADF); return -1; } @@ -110,7 +113,7 @@ int read(int fd, void *buf, size_t len) d = fd_get(fd); if (d == RT_NULL) { - rt_set_errno(-RT_ERROR); + rt_set_errno(-DFS_STATUS_EBADF); return -1; } @@ -147,7 +150,7 @@ int write(int fd, const void *buf, size_t len) d = fd_get(fd); if (d == RT_NULL) { - rt_set_errno(-RT_ERROR); + rt_set_errno(-DFS_STATUS_EBADF); return -1; } @@ -183,7 +186,7 @@ off_t lseek(int fd, off_t offset, int whence) d = fd_get(fd); if (d == RT_NULL) { - rt_set_errno(-RT_ERROR); + rt_set_errno(-DFS_STATUS_EBADF); return -1; } @@ -304,7 +307,7 @@ int fstat(int fildes, struct stat *buf) d = fd_get(fildes); if (d == RT_NULL) { - rt_set_errno(-RT_ERROR); + rt_set_errno(-DFS_STATUS_EBADF); return -1; } @@ -368,7 +371,7 @@ int mkdir(const char *path, mode_t mode) fd = fd_new(); if (fd == -1) { - rt_kprintf("no fd\n"); + rt_set_errno(-DFS_STATUS_ENOMEM); return -1; } @@ -432,7 +435,7 @@ DIR *opendir(const char *name) fd = fd_new(); if (fd == -1) { - rt_kprintf("no fd\n"); + rt_set_errno(-DFS_STATUS_ENOMEM); return RT_NULL; } d = fd_get(fd); @@ -481,7 +484,7 @@ struct dirent *readdir(DIR *d) fd = fd_get(d->fd); if (fd == RT_NULL) { - rt_set_errno(-RT_ERROR); + rt_set_errno(-DFS_STATUS_EBADF); return RT_NULL; } @@ -521,7 +524,7 @@ long telldir(DIR *d) fd = fd_get(d->fd); if (fd == RT_NULL) { - rt_set_errno(-RT_ERROR); + rt_set_errno(-DFS_STATUS_EBADF); return 0; } @@ -545,7 +548,7 @@ void seekdir(DIR *d, off_t offset) fd = fd_get(d->fd); if (fd == RT_NULL) { - rt_set_errno(-RT_ERROR); + rt_set_errno(-DFS_STATUS_EBADF); return ; } @@ -567,7 +570,7 @@ void rewinddir(DIR *d) fd = fd_get(d->fd); if (fd == RT_NULL) { - rt_set_errno(-RT_ERROR); + rt_set_errno(-DFS_STATUS_EBADF); return ; } @@ -593,7 +596,7 @@ int closedir(DIR *d) fd = fd_get(d->fd); if (fd == RT_NULL) { - rt_set_errno(-RT_ERROR); + rt_set_errno(-DFS_STATUS_EBADF); return -1; } @@ -633,11 +636,17 @@ int chdir(const char *path) } if (rt_strlen(path) > DFS_PATH_MAX) + { + rt_set_errno(-DFS_STATUS_ENOTDIR); return -1; + } fullpath = dfs_normalize_path(NULL, path); if (fullpath == RT_NULL) + { + rt_set_errno(-DFS_STATUS_ENOTDIR); return -1; /* build path failed */ + } dfs_lock(); d = opendir(fullpath);