From a727db0ebd2b7a396b2c7f45e370c763c6cae39c Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Fri, 29 Jan 2021 18:00:25 +0800 Subject: [PATCH 01/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9fd=20get=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E6=9C=BA=E5=88=B6=E4=B8=BA=E5=8F=AA=E4=BD=9Cfd?= =?UTF-8?q?=E5=88=B0dfd=5Ffd=E7=9A=84=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/dfs/include/dfs.h | 8 +- components/dfs/src/dfs.c | 17 ++-- components/dfs/src/dfs_posix.c | 80 +++++-------------- components/dfs/src/poll.c | 4 +- components/lwp/lwp.c | 5 +- components/lwp/lwp_ipc.c | 11 +-- components/lwp/lwp_syscall.c | 3 +- components/net/sal_socket/dfs_net/dfs_net.c | 3 +- .../net/sal_socket/socket/net_sockets.c | 24 ++---- 9 files changed, 51 insertions(+), 104 deletions(-) diff --git a/components/dfs/include/dfs.h b/components/dfs/include/dfs.h index 8f63eec415..165bf325fd 100644 --- a/components/dfs/include/dfs.h +++ b/components/dfs/include/dfs.h @@ -102,11 +102,11 @@ void dfs_fd_unlock(void); /* FD APIs */ int fdt_fd_new(struct dfs_fdtable *fdt); -struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd); -void fdt_fd_put(struct dfs_fdtable* fdt, struct dfs_fd *fd); +struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd, int ref_inc_nr); +void fdt_fd_release(struct dfs_fdtable* fdt, struct dfs_fd *fd); int fd_new(void); -struct dfs_fd *fd_get(int fd); -void fd_put(struct dfs_fd *fd); +struct dfs_fd *dfs_fd_get(int fd); +void dfs_fd_release(struct dfs_fd *fd); int fd_is_open(const char *pathname); struct dfs_fdtable *dfs_fdtable_get(void); diff --git a/components/dfs/src/dfs.c b/components/dfs/src/dfs.c index 506d97d2d7..7e3e640360 100644 --- a/components/dfs/src/dfs.c +++ b/components/dfs/src/dfs.c @@ -239,7 +239,8 @@ int fd_new(void) * @return NULL on on this file descriptor or the file descriptor structure * pointer. */ -struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd) + +struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd, int ref_inc_nr) { struct dfs_fd *d; @@ -262,19 +263,19 @@ struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd) return NULL; } - /* increase the reference count */ - d->ref_count ++; + d->ref_count += ref_inc_nr; + dfs_fd_unlock(); return d; } -struct dfs_fd *fd_get(int fd) +struct dfs_fd *dfs_fd_get(int fd) { struct dfs_fdtable *fdt; fdt = dfs_fdtable_get(); - return fdt_fd_get(fdt, fd); + return fdt_fd_get(fdt, fd, 0); } /** @@ -282,7 +283,7 @@ struct dfs_fd *fd_get(int fd) * * This function will put the file descriptor. */ -void fdt_fd_put(struct dfs_fdtable* fdt, struct dfs_fd *fd) +void fdt_fd_release(struct dfs_fdtable* fdt, struct dfs_fd *fd) { RT_ASSERT(fd != NULL); @@ -308,12 +309,12 @@ void fdt_fd_put(struct dfs_fdtable* fdt, struct dfs_fd *fd) dfs_fd_unlock(); } -void fd_put(struct dfs_fd *fd) +void dfs_fd_release(struct dfs_fd *fd) { struct dfs_fdtable *fdt; fdt = dfs_fdtable_get(); - fdt_fd_put(fdt, fd); + fdt_fd_release(fdt, fd); } /** diff --git a/components/dfs/src/dfs_posix.c b/components/dfs/src/dfs_posix.c index fc603df437..b8b8034d20 100644 --- a/components/dfs/src/dfs_posix.c +++ b/components/dfs/src/dfs_posix.c @@ -41,23 +41,19 @@ int open(const char *file, int flags, ...) return -1; } - d = fd_get(fd); + d = dfs_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); + dfs_fd_release(d); rt_set_errno(result); return -1; } - /* release the ref-count of fd */ - fd_put(d); - return fd; } RTM_EXPORT(open); @@ -120,7 +116,7 @@ int read(int fd, void *buf, size_t len) struct dfs_fd *d; /* get the fd */ - d = fd_get(fd); + d = dfs_fd_get(fd); if (d == NULL) { rt_set_errno(-EBADF); @@ -131,15 +127,11 @@ int read(int fd, void *buf, size_t len) result = dfs_file_read(d, buf, len); if (result < 0) { - fd_put(d); rt_set_errno(result); return -1; } - /* release the ref-count of fd */ - fd_put(d); - return result; } RTM_EXPORT(read); @@ -164,7 +156,7 @@ int write(int fd, const void *buf, size_t len) struct dfs_fd *d; /* get the fd */ - d = fd_get(fd); + d = dfs_fd_get(fd); if (d == NULL) { rt_set_errno(-EBADF); @@ -175,15 +167,11 @@ int write(int fd, const void *buf, size_t len) result = dfs_file_write(d, buf, len); if (result < 0) { - fd_put(d); rt_set_errno(result); return -1; } - /* release the ref-count of fd */ - fd_put(d); - return result; } RTM_EXPORT(write); @@ -203,7 +191,7 @@ off_t lseek(int fd, off_t offset, int whence) int result; struct dfs_fd *d; - d = fd_get(fd); + d = dfs_fd_get(fd); if (d == NULL) { rt_set_errno(-EBADF); @@ -225,7 +213,6 @@ off_t lseek(int fd, off_t offset, int whence) break; default: - fd_put(d); rt_set_errno(-EINVAL); return -1; @@ -233,7 +220,6 @@ off_t lseek(int fd, off_t offset, int whence) if (offset < 0) { - fd_put(d); rt_set_errno(-EINVAL); return -1; @@ -241,15 +227,11 @@ off_t lseek(int fd, off_t offset, int whence) result = dfs_file_lseek(d, offset); if (result < 0) { - fd_put(d); rt_set_errno(result); return -1; } - /* release the ref-count of fd */ - fd_put(d); - return offset; } RTM_EXPORT(lseek); @@ -343,7 +325,7 @@ int fstat(int fildes, struct stat *buf) struct dfs_fd *d; /* get the fd */ - d = fd_get(fildes); + d = dfs_fd_get(fildes); if (d == NULL) { rt_set_errno(-EBADF); @@ -365,8 +347,6 @@ int fstat(int fildes, struct stat *buf) buf->st_size = d->size; buf->st_mtime = 0; - fd_put(d); - return RT_EOK; } RTM_EXPORT(fstat); @@ -388,7 +368,7 @@ int fsync(int fildes) struct dfs_fd *d; /* get the fd */ - d = fd_get(fildes); + d = dfs_fd_get(fildes); if (d == NULL) { rt_set_errno(-EBADF); @@ -397,7 +377,6 @@ int fsync(int fildes) ret = dfs_file_flush(d); - fd_put(d); return ret; } RTM_EXPORT(fsync); @@ -420,7 +399,7 @@ int fcntl(int fildes, int cmd, ...) struct dfs_fd *d; /* get the fd */ - d = fd_get(fildes); + d = dfs_fd_get(fildes); if (d) { void *arg; @@ -431,7 +410,6 @@ int fcntl(int fildes, int cmd, ...) va_end(ap); ret = dfs_file_ioctl(d, cmd, arg); - fd_put(d); } else ret = -EBADF; @@ -486,7 +464,7 @@ int ftruncate(int fd, off_t length) int result; struct dfs_fd *d; - d = fd_get(fd); + d = dfs_fd_get(fd); if (d == NULL) { rt_set_errno(-EBADF); @@ -496,7 +474,6 @@ int ftruncate(int fd, off_t length) if (length < 0) { - fd_put(d); rt_set_errno(-EINVAL); return -1; @@ -504,15 +481,11 @@ int ftruncate(int fd, off_t length) result = dfs_file_ftruncate(d, length); if (result < 0) { - fd_put(d); rt_set_errno(result); return -1; } - /* release the ref-count of fd */ - fd_put(d); - return 0; } RTM_EXPORT(ftruncate); @@ -564,22 +537,20 @@ int mkdir(const char *path, mode_t mode) return -1; } - d = fd_get(fd); + d = dfs_fd_get(fd); result = dfs_file_open(d, path, O_DIRECTORY | O_CREAT); if (result < 0) { - fd_put(d); - fd_put(d); + dfs_fd_release(d); rt_set_errno(result); return -1; } dfs_file_close(d); - fd_put(d); - fd_put(d); + dfs_fd_release(d); return 0; } @@ -636,7 +607,7 @@ DIR *opendir(const char *name) return NULL; } - d = fd_get(fd); + d = dfs_fd_get(fd); result = dfs_file_open(d, name, O_RDONLY | O_DIRECTORY); if (result >= 0) @@ -646,7 +617,7 @@ DIR *opendir(const char *name) if (t == NULL) { dfs_file_close(d); - fd_put(d); + dfs_fd_release(d); } else { @@ -654,14 +625,12 @@ DIR *opendir(const char *name) t->fd = fd; } - fd_put(d); return t; } /* open failed */ - fd_put(d); - fd_put(d); + dfs_fd_release(d); rt_set_errno(result); return NULL; @@ -682,7 +651,7 @@ struct dirent *readdir(DIR *d) int result; struct dfs_fd *fd; - fd = fd_get(d->fd); + fd = dfs_fd_get(d->fd); if (fd == NULL) { rt_set_errno(-EBADF); @@ -704,7 +673,6 @@ struct dirent *readdir(DIR *d) sizeof(d->buf) - 1); if (result <= 0) { - fd_put(fd); rt_set_errno(result); return NULL; @@ -714,8 +682,6 @@ struct dirent *readdir(DIR *d) d->cur = 0; /* current entry index */ } - fd_put(fd); - return (struct dirent *)(d->buf + d->cur); } RTM_EXPORT(readdir); @@ -733,7 +699,7 @@ long telldir(DIR *d) struct dfs_fd *fd; long result; - fd = fd_get(d->fd); + fd = dfs_fd_get(d->fd); if (fd == NULL) { rt_set_errno(-EBADF); @@ -742,7 +708,6 @@ long telldir(DIR *d) } result = fd->pos - d->num + d->cur; - fd_put(fd); return result; } @@ -759,7 +724,7 @@ void seekdir(DIR *d, off_t offset) { struct dfs_fd *fd; - fd = fd_get(d->fd); + fd = dfs_fd_get(d->fd); if (fd == NULL) { rt_set_errno(-EBADF); @@ -770,7 +735,6 @@ 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; - fd_put(fd); } RTM_EXPORT(seekdir); @@ -784,7 +748,7 @@ void rewinddir(DIR *d) { struct dfs_fd *fd; - fd = fd_get(d->fd); + fd = dfs_fd_get(d->fd); if (fd == NULL) { rt_set_errno(-EBADF); @@ -795,7 +759,6 @@ void rewinddir(DIR *d) /* seek to the beginning of directory */ if (dfs_file_lseek(fd, 0) >= 0) d->num = d->cur = 0; - fd_put(fd); } RTM_EXPORT(rewinddir); @@ -812,7 +775,7 @@ int closedir(DIR *d) int result; struct dfs_fd *fd; - fd = fd_get(d->fd); + fd = dfs_fd_get(d->fd); if (fd == NULL) { rt_set_errno(-EBADF); @@ -821,9 +784,8 @@ int closedir(DIR *d) } result = dfs_file_close(fd); - fd_put(fd); + dfs_fd_release(fd); - fd_put(fd); rt_free(d); if (result < 0) diff --git a/components/dfs/src/poll.c b/components/dfs/src/poll.c index 2d948a30a3..184efc0e26 100644 --- a/components/dfs/src/poll.c +++ b/components/dfs/src/poll.c @@ -128,7 +128,7 @@ static int do_pollfd(struct pollfd *pollfd, rt_pollreq_t *req) if (fd >= 0) { - struct dfs_fd *f = fd_get(fd); + struct dfs_fd *f = dfs_fd_get(fd); mask = POLLNVAL; if (f) @@ -143,14 +143,12 @@ static int do_pollfd(struct pollfd *pollfd, rt_pollreq_t *req) /* dealwith the device return error -1*/ if (mask < 0) { - fd_put(f); pollfd->revents = 0; return mask; } } /* Mask out unneeded events. */ mask &= pollfd->events | POLLERR | POLLHUP; - fd_put(f); } } pollfd->revents = mask; diff --git a/components/lwp/lwp.c b/components/lwp/lwp.c index fc199e7ec8..25ba78014e 100644 --- a/components/lwp/lwp.c +++ b/components/lwp/lwp.c @@ -752,10 +752,11 @@ static void lwp_copy_stdio_fdt(struct rt_lwp *lwp) int fd; struct dfs_fd *d; struct dfs_fdtable *lwp_fdt; + struct dfs_fdtable *fdt; fd = libc_stdio_get_console(); - d = fd_get(fd); - fd_put(d); + fdt = dfs_fdtable_get(); + d = fdt_fd_get(fdt, fd, 0); /* inc 0 ref count */ fd = fd - DFS_FD_OFFSET; if (d == NULL) diff --git a/components/lwp/lwp_ipc.c b/components/lwp/lwp_ipc.c index 40126229bc..1d4e2d971b 100644 --- a/components/lwp/lwp_ipc.c +++ b/components/lwp/lwp_ipc.c @@ -795,10 +795,10 @@ static struct dfs_fd *lwp_fd_get(int fdt_type, int fd) { fdt = dfs_fdtable_get(); } - return fdt_fd_get(fdt, fd); + return fdt_fd_get(fdt, fd, 0); } -static void lwp_fd_put(int fdt_type, struct dfs_fd *fd) +static void lwp_fd_release(int fdt_type, struct dfs_fd *fd) { struct dfs_fdtable *fdt; @@ -810,7 +810,7 @@ static void lwp_fd_put(int fdt_type, struct dfs_fd *fd) { fdt = dfs_fdtable_get(); } - fdt_fd_put(fdt, fd); + fdt_fd_release(fdt, fd); } static int _chfd_alloc(int fdt_type) @@ -837,8 +837,7 @@ static void _chfd_free(int fd, int fdt_type) { return; } - lwp_fd_put(fdt_type, d); - lwp_fd_put(fdt_type, d); + lwp_fd_release(fdt_type, d); } /* for fops */ @@ -924,7 +923,6 @@ int lwp_channel_open(int fdt_type, const char *name, int flags) /* set socket to the data of dfs_fd */ d->data = (void *)ch; - lwp_fd_put(fdt_type, d); } else { @@ -945,7 +943,6 @@ static rt_channel_t fd_2_channel(int fdt_type, int fd) rt_channel_t ch; ch = (rt_channel_t)d->data; - lwp_fd_put(fdt_type, d); if (ch) { return ch; diff --git a/components/lwp/lwp_syscall.c b/components/lwp/lwp_syscall.c index 12df9e413c..c87e8eb085 100644 --- a/components/lwp/lwp_syscall.c +++ b/components/lwp/lwp_syscall.c @@ -2339,9 +2339,8 @@ int sys_getdents(int fd, struct libc_dirent *dirp, size_t nbytes) rt_set_errno(ENOMEM); return -1; } - dfs_fd = fd_get(fd); + dfs_fd = dfs_fd_get(fd); ret = dfs_file_getdents(dfs_fd, rtt_dirp, nbytes); - fd_put(dfs_fd); if (ret) { size_t i = 0; diff --git a/components/net/sal_socket/dfs_net/dfs_net.c b/components/net/sal_socket/dfs_net/dfs_net.c index f1a3d777a1..5cae1d2e9d 100644 --- a/components/net/sal_socket/dfs_net/dfs_net.c +++ b/components/net/sal_socket/dfs_net/dfs_net.c @@ -23,13 +23,12 @@ int dfs_net_getsocket(int fd) int socket; struct dfs_fd *_dfs_fd; - _dfs_fd = fd_get(fd); + _dfs_fd = dfs_fd_get(fd); if (_dfs_fd == NULL) return -1; if (_dfs_fd->type != FT_SOCKET) socket = -1; else socket = (int)_dfs_fd->data; - fd_put(_dfs_fd); /* put this dfs fd */ return socket; } diff --git a/components/net/sal_socket/socket/net_sockets.c b/components/net/sal_socket/socket/net_sockets.c index a53b2f1f9c..1adde5413d 100644 --- a/components/net/sal_socket/socket/net_sockets.c +++ b/components/net/sal_socket/socket/net_sockets.c @@ -37,7 +37,7 @@ int accept(int s, struct sockaddr *addr, socklen_t *addrlen) return -1; } - d = fd_get(fd); + d = dfs_fd_get(fd); if(d) { /* this is a socket fd */ @@ -53,9 +53,6 @@ int accept(int s, struct sockaddr *addr, socklen_t *addrlen) /* set socket to the data of dfs_fd */ d->data = (void *) new_socket; - /* release the ref-count of fd */ - fd_put(d); - return fd; } @@ -89,13 +86,13 @@ int shutdown(int s, int how) return -1; } - d = fd_get(s); + d = dfs_fd_get(s); if (d == NULL) { rt_set_errno(-EBADF); return -1; } - + if (sal_shutdown(socket, how) == 0) { error = 0; @@ -105,8 +102,6 @@ int shutdown(int s, int how) rt_set_errno(-ENOTSOCK); error = -1; } - - fd_put(d); return error; } @@ -209,7 +204,7 @@ int socket(int domain, int type, int protocol) return -1; } - d = fd_get(fd); + d = dfs_fd_get(fd); /* create socket and then put it to the dfs_fd */ socket = sal_socket(domain, type, protocol); @@ -231,17 +226,13 @@ int socket(int domain, int type, int protocol) else { /* release fd */ - fd_put(d); - fd_put(d); + dfs_fd_release(d); rt_set_errno(-ENOMEM); return -1; } - /* release the ref-count of fd */ - fd_put(d); - return fd; } RTM_EXPORT(socket); @@ -259,7 +250,7 @@ int closesocket(int s) return -1; } - d = fd_get(s); + d = dfs_fd_get(s); if (d == RT_NULL) { rt_set_errno(-EBADF); @@ -277,8 +268,7 @@ int closesocket(int s) } /* socket has been closed, delete it from file system fd */ - fd_put(d); - fd_put(d); + dfs_fd_release(d); return error; } -- Gitee From 9c96c85e1a3fe3663c9a48c8ca737dacf920cbf1 Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Tue, 2 Feb 2021 13:44:06 +0800 Subject: [PATCH 02/12] =?UTF-8?q?dfs=5Ffd=E7=BB=93=E6=9E=84=E5=88=86?= =?UTF-8?q?=E6=88=90=E4=B8=A4=E5=B1=82=EF=BC=8C=E5=A2=9E=E5=8A=A0=E4=B8=80?= =?UTF-8?q?=E7=BA=A7dfs=5Ffnode=E7=BB=93=E6=9E=84=E4=BD=93,=20=E6=89=80?= =?UTF-8?q?=E6=9C=89=E6=96=87=E4=BB=B6=E7=B3=BB=E7=BB=9F=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E4=BD=BF=E7=94=A8fnode->fs=E6=9D=A5=E5=8F=96=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dfs/filesystems/cromfs/dfs_cromfs.c | 36 +++---- components/dfs/filesystems/devfs/devfs.c | 40 ++++---- components/dfs/filesystems/elmfat/dfs_elm.c | 38 ++++---- components/dfs/filesystems/jffs2/dfs_jffs2.c | 50 +++++----- .../filesystems/jffs2/include/port/sys/stat.h | 4 + components/dfs/filesystems/jffs2/porting.h | 6 +- components/dfs/filesystems/nfs/dfs_nfs.c | 54 +++++------ components/dfs/filesystems/ramfs/dfs_ramfs.c | 44 ++++----- components/dfs/filesystems/romfs/dfs_romfs.c | 28 +++--- components/dfs/filesystems/uffs/dfs_uffs.c | 44 ++++----- .../filesystems/uffs/src/inc/uffs/uffs_fd.h | 6 +- components/dfs/include/dfs.h | 4 +- components/dfs/include/dfs_file.h | 16 ++- components/dfs/src/dfs.c | 97 ++++++++++++------- components/dfs/src/dfs_file.c | 93 +++++++++--------- components/dfs/src/dfs_fs.c | 3 + components/dfs/src/dfs_posix.c | 53 +++++----- components/dfs/src/poll.c | 6 +- components/drivers/serial/serial.c | 22 ++--- components/drivers/src/pipe.c | 22 ++--- components/lwp/lwp_console.c | 14 +-- components/lwp/lwp_ipc.c | 18 ++-- components/lwp/lwp_syscall.c | 2 +- components/net/sal_socket/dfs_net/dfs_net.c | 18 ++-- components/net/sal_socket/impl/af_inet_lwip.c | 2 +- .../net/sal_socket/socket/net_sockets.c | 36 +++---- components/net/sal_socket/src/sal_socket.c | 2 +- 27 files changed, 398 insertions(+), 360 deletions(-) diff --git a/components/dfs/filesystems/cromfs/dfs_cromfs.c b/components/dfs/filesystems/cromfs/dfs_cromfs.c index 8584779826..5c942951b9 100644 --- a/components/dfs/filesystems/cromfs/dfs_cromfs.c +++ b/components/dfs/filesystems/cromfs/dfs_cromfs.c @@ -640,14 +640,14 @@ static int dfs_cromfs_read(struct dfs_fd *file, void *buf, size_t count) cromfs_info *ci; uint32_t length; - fs = (struct dfs_filesystem *)file->fs; + fs = (struct dfs_filesystem *)file->fnode->fs; ci = (cromfs_info *)fs->data; - fi = (file_info *)file->data; + fi = (file_info *)file->fnode->data; - if (count < file->size - file->pos) + if (count < file->fnode->size - file->pos) length = count; else - length = file->size - file->pos; + length = file->fnode->size - file->pos; if (length > 0) { @@ -690,7 +690,7 @@ static int dfs_cromfs_read(struct dfs_fd *file, void *buf, size_t count) static int dfs_cromfs_lseek(struct dfs_fd *file, off_t offset) { - if (offset <= file->size) + if (offset <= file->fnode->size) { file->pos = offset; return file->pos; @@ -784,8 +784,8 @@ static int dfs_cromfs_close(struct dfs_fd *file) cromfs_info *ci; rt_err_t result; - fi = (file_info *)file->data; - fs = (struct dfs_filesystem *)file->fs; + fi = (file_info *)file->fnode->data; + fs = (struct dfs_filesystem *)file->fnode->fs; ci = (cromfs_info *)fs->data; result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER); @@ -793,7 +793,7 @@ static int dfs_cromfs_close(struct dfs_fd *file) return -RT_ERROR; deref_file_info(ci, fi->partition_pos); rt_mutex_release(&ci->lock); - file->data = NULL; + file->fnode->data = NULL; return RT_EOK; } @@ -808,13 +808,13 @@ static int dfs_cromfs_open(struct dfs_fd *file) int is_dir; rt_err_t result; - if (file->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR)) + if (file->fnode->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR)) return -EINVAL; - fs = (struct dfs_filesystem *)file->fs; + fs = file->fnode->fs; ci = (cromfs_info *)fs->data; - file_pos = dfs_cromfs_lookup(ci, file->path, &is_dir, &size, &osize); + file_pos = dfs_cromfs_lookup(ci, file->fnode->path, &is_dir, &size, &osize); if (file_pos == CROMFS_POS_ERROR) { ret = -ENOENT; @@ -824,7 +824,7 @@ static int dfs_cromfs_open(struct dfs_fd *file) /* entry is a directory file type */ if (is_dir) { - if (!(file->flags & O_DIRECTORY)) + if (!(file->fnode->flags & O_DIRECTORY)) { ret = -ENOENT; goto end; @@ -833,7 +833,7 @@ static int dfs_cromfs_open(struct dfs_fd *file) else { /* entry is a file, but open it as a directory */ - if (file->flags & O_DIRECTORY) + if (file->fnode->flags & O_DIRECTORY) { ret = -ENOENT; goto end; @@ -859,11 +859,11 @@ static int dfs_cromfs_open(struct dfs_fd *file) goto end; } - file->data = fi; + file->fnode->data = fi; if (is_dir) - file->size = size; + file->fnode->size = size; else - file->size = osize; + file->fnode->size = osize; file->pos = 0; ret = RT_EOK; @@ -915,7 +915,7 @@ static int dfs_cromfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_ void *di_mem; rt_err_t result; - fi = (file_info *)file->data; + fi = (file_info *)file->fnode->data; ci = fi->ci; RT_ASSERT(fi->buff == NULL); @@ -951,7 +951,7 @@ static int dfs_cromfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_ return -EINVAL; } - for (index = 0; index < count && file->pos < file->size; index ++) + for (index = 0; index < count && file->pos < file->fnode->size; index ++) { uint32_t name_size; diff --git a/components/dfs/filesystems/devfs/devfs.c b/components/dfs/filesystems/devfs/devfs.c index d096f89847..f700d0ecc0 100644 --- a/components/dfs/filesystems/devfs/devfs.c +++ b/components/dfs/filesystems/devfs/devfs.c @@ -37,7 +37,7 @@ int dfs_device_fs_ioctl(struct dfs_fd *file, int cmd, void *args) RT_ASSERT(file != RT_NULL); /* get device handler */ - dev_id = (rt_device_t)file->data; + dev_id = (rt_device_t)file->fnode->data; RT_ASSERT(dev_id != RT_NULL); /* close device handler */ @@ -56,7 +56,7 @@ int dfs_device_fs_read(struct dfs_fd *file, void *buf, size_t count) RT_ASSERT(file != RT_NULL); /* get device handler */ - dev_id = (rt_device_t)file->data; + dev_id = (rt_device_t)file->fnode->data; RT_ASSERT(dev_id != RT_NULL); /* read device data */ @@ -74,7 +74,7 @@ int dfs_device_fs_write(struct dfs_fd *file, const void *buf, size_t count) RT_ASSERT(file != RT_NULL); /* get device handler */ - dev_id = (rt_device_t)file->data; + dev_id = (rt_device_t)file->fnode->data; RT_ASSERT(dev_id != RT_NULL); /* read device data */ @@ -91,11 +91,11 @@ int dfs_device_fs_close(struct dfs_fd *file) RT_ASSERT(file != RT_NULL); - if (file->type == FT_DIRECTORY) + if (file->fnode->type == FT_DIRECTORY) { struct device_dirent *root_dirent; - root_dirent = (struct device_dirent *)file->data; + root_dirent = (struct device_dirent *)file->fnode->data; RT_ASSERT(root_dirent != RT_NULL); /* release dirent */ @@ -104,14 +104,14 @@ int dfs_device_fs_close(struct dfs_fd *file) } /* get device handler */ - dev_id = (rt_device_t)file->data; + dev_id = (rt_device_t)file->fnode->data; RT_ASSERT(dev_id != RT_NULL); /* close device handler */ result = rt_device_close(dev_id); if (result == RT_EOK) { - file->data = RT_NULL; + file->fnode->data = RT_NULL; return RT_EOK; } @@ -126,8 +126,8 @@ int dfs_device_fs_open(struct dfs_fd *file) rt_base_t level; /* open root directory */ - if ((file->path[0] == '/') && (file->path[1] == '\0') && - (file->flags & O_DIRECTORY)) + if ((file->fnode->path[0] == '/') && (file->fnode->path[1] == '\0') && + (file->fnode->flags & O_DIRECTORY)) { struct rt_object *object; struct rt_list_node *node; @@ -165,12 +165,12 @@ int dfs_device_fs_open(struct dfs_fd *file) rt_hw_interrupt_enable(level); /* set data */ - file->data = root_dirent; + file->fnode->data = root_dirent; return RT_EOK; } - device = rt_device_find(&file->path[1]); + device = rt_device_find(&file->fnode->path[1]); if (device == RT_NULL) return -ENODEV; @@ -178,16 +178,16 @@ int dfs_device_fs_open(struct dfs_fd *file) if (device->fops) { /* use device fops */ - file->fops = device->fops; - file->data = (void *)device; + file->fnode->fops = device->fops; + file->fnode->data = (void *)device; /* use fops */ - if (file->fops->open) + if (file->fnode->fops->open) { - result = file->fops->open(file); + result = file->fnode->fops->open(file); if (result == RT_EOK || result == -RT_ENOSYS) { - file->type = FT_DEVICE; + file->fnode->type = FT_DEVICE; return 0; } } @@ -198,13 +198,13 @@ int dfs_device_fs_open(struct dfs_fd *file) result = rt_device_open(device, RT_DEVICE_OFLAG_RDWR); if (result == RT_EOK || result == -RT_ENOSYS) { - file->data = device; - file->type = FT_DEVICE; + file->fnode->data = device; + file->fnode->type = FT_DEVICE; return RT_EOK; } } - file->data = RT_NULL; + file->fnode->data = RT_NULL; /* open device failed. */ return -EIO; } @@ -264,7 +264,7 @@ int dfs_device_fs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t co struct dirent *d; struct device_dirent *root_dirent; - root_dirent = (struct device_dirent *)file->data; + root_dirent = (struct device_dirent *)file->fnode->data; RT_ASSERT(root_dirent != RT_NULL); /* make integer count */ diff --git a/components/dfs/filesystems/elmfat/dfs_elm.c b/components/dfs/filesystems/elmfat/dfs_elm.c index 182adfd3d6..b28da54f07 100644 --- a/components/dfs/filesystems/elmfat/dfs_elm.c +++ b/components/dfs/filesystems/elmfat/dfs_elm.c @@ -330,7 +330,7 @@ int dfs_elm_open(struct dfs_fd *file) #if (_VOLUMES > 1) int vol; - struct dfs_filesystem *fs = (struct dfs_filesystem *)file->data; + struct dfs_filesystem *fs = file->fnode->fs; extern int elm_get_vol(FATFS * fat); if (fs == NULL) @@ -344,16 +344,16 @@ int dfs_elm_open(struct dfs_fd *file) if (drivers_fn == RT_NULL) return -ENOMEM; - rt_snprintf(drivers_fn, 256, "%d:%s", vol, file->path); + rt_snprintf(drivers_fn, 256, "%d:%s", vol, file->fnode->path); #else - drivers_fn = file->path; + drivers_fn = file->fnode->path; #endif - if (file->flags & O_DIRECTORY) + if (file->fnode->flags & O_DIRECTORY) { DIR *dir; - if (file->flags & O_CREAT) + if (file->fnode->flags & O_CREAT) { result = f_mkdir(drivers_fn); if (result != FR_OK) @@ -392,18 +392,18 @@ int dfs_elm_open(struct dfs_fd *file) { mode = FA_READ; - if (file->flags & O_WRONLY) + if (file->fnode->flags & O_WRONLY) mode |= FA_WRITE; - if ((file->flags & O_ACCMODE) & O_RDWR) + if ((file->fnode->flags & O_ACCMODE) & O_RDWR) mode |= FA_WRITE; /* Opens the file, if it is existing. If not, a new file is created. */ - if (file->flags & O_CREAT) + if (file->fnode->flags & O_CREAT) mode |= FA_OPEN_ALWAYS; /* Creates a new file. If the file is existing, it is truncated and overwritten. */ - if (file->flags & O_TRUNC) + if (file->fnode->flags & O_TRUNC) mode |= FA_CREATE_ALWAYS; /* Creates a new file. The function fails if the file is already existing. */ - if (file->flags & O_EXCL) + if (file->fnode->flags & O_EXCL) mode |= FA_CREATE_NEW; /* allocate a fd */ @@ -423,10 +423,10 @@ int dfs_elm_open(struct dfs_fd *file) if (result == FR_OK) { file->pos = fd->fptr; - file->size = f_size(fd); + file->fnode->size = f_size(fd); file->data = fd; - if (file->flags & O_APPEND) + if (file->fnode->flags & O_APPEND) { /* seek to the end of file */ f_lseek(fd, f_size(fd)); @@ -449,7 +449,7 @@ int dfs_elm_close(struct dfs_fd *file) FRESULT result; result = FR_OK; - if (file->type == FT_DIRECTORY) + if (file->fnode->type == FT_DIRECTORY) { DIR *dir; @@ -459,7 +459,7 @@ int dfs_elm_close(struct dfs_fd *file) /* release memory */ rt_free(dir); } - else if (file->type == FT_REGULAR) + else if (file->fnode->type == FT_REGULAR) { FIL *fd; fd = (FIL *)(file->data); @@ -514,7 +514,7 @@ int dfs_elm_read(struct dfs_fd *file, void *buf, size_t len) FRESULT result; UINT byte_read; - if (file->type == FT_DIRECTORY) + if (file->fnode->type == FT_DIRECTORY) { return -EISDIR; } @@ -537,7 +537,7 @@ int dfs_elm_write(struct dfs_fd *file, const void *buf, size_t len) FRESULT result; UINT byte_write; - if (file->type == FT_DIRECTORY) + if (file->fnode->type == FT_DIRECTORY) { return -EISDIR; } @@ -548,7 +548,7 @@ int dfs_elm_write(struct dfs_fd *file, const void *buf, size_t len) result = f_write(fd, buf, len, &byte_write); /* update position and file size */ file->pos = fd->fptr; - file->size = f_size(fd); + file->fnode->size = f_size(fd); if (result == FR_OK) return byte_write; @@ -570,7 +570,7 @@ int dfs_elm_flush(struct dfs_fd *file) int dfs_elm_lseek(struct dfs_fd *file, off_t offset) { FRESULT result = FR_OK; - if (file->type == FT_REGULAR) + if (file->fnode->type == FT_REGULAR) { FIL *fd; @@ -586,7 +586,7 @@ int dfs_elm_lseek(struct dfs_fd *file, off_t offset) return fd->fptr; } } - else if (file->type == FT_DIRECTORY) + else if (file->fnode->type == FT_DIRECTORY) { /* which is a directory */ DIR *dir; diff --git a/components/dfs/filesystems/jffs2/dfs_jffs2.c b/components/dfs/filesystems/jffs2/dfs_jffs2.c index 82017dd41c..acf94e7ed7 100644 --- a/components/dfs/filesystems/jffs2/dfs_jffs2.c +++ b/components/dfs/filesystems/jffs2/dfs_jffs2.c @@ -215,8 +215,8 @@ static int dfs_jffs2_open(struct dfs_fd* file) struct dfs_filesystem *fs; struct cyg_mtab_entry * mte; - oflag = file->flags; - fs = (struct dfs_filesystem *)file->data; + oflag = file->fnode->flags; + fs = file->fnode->fs; RT_ASSERT(fs != RT_NULL); jffs2_file = rt_malloc(sizeof(cyg_file)); @@ -224,7 +224,7 @@ static int dfs_jffs2_open(struct dfs_fd* file) return -ENOMEM; /* just escape '/' provided by dfs code */ - name = file->path; + name = file->fnode->path; if ((name[0] == '/') && (name[1] == 0)) name = jffs2_root_path; else /* name[0] still will be '/' */ @@ -267,7 +267,7 @@ static int dfs_jffs2_open(struct dfs_fd* file) jffs2_file->f_offset = 2; #endif /* save this pointer, it will be used by dfs_jffs2_getdents*/ - file->data = jffs2_file; + file->fnode->data = jffs2_file; return 0; } /* regular file operations */ @@ -292,17 +292,17 @@ static int dfs_jffs2_open(struct dfs_fd* file) /* save this pointer, it will be used when calling read(), write(), flush(), lessk(), and will be rt_free when calling close()*/ - file->data = jffs2_file; + file->fnode->data = jffs2_file; file->pos = jffs2_file->f_offset; - file->size = 0; - jffs2_file_lseek(jffs2_file, (off_t *)(&(file->size)), SEEK_END); + file->fnode->size = 0; + jffs2_file_lseek(jffs2_file, (off_t *)(&(file->fnode->size)), SEEK_END); jffs2_file->f_offset = (off_t)file->pos; rt_mutex_release(&jffs2_lock); if (oflag & O_APPEND) { - file->pos = file->size; - jffs2_file->f_offset = file->size; + file->pos = file->fnode->size; + jffs2_file->f_offset = file->fnode->size; } return 0; @@ -313,10 +313,10 @@ static int dfs_jffs2_close(struct dfs_fd* file) int result; cyg_file * jffs2_file; - RT_ASSERT(file->data != NULL); - jffs2_file = (cyg_file *)(file->data); + RT_ASSERT(file->fnode->data != NULL); + jffs2_file = (cyg_file *)(file->fnode->data); - if (file->flags & O_DIRECTORY) /* operations about dir */ + if (file->fnode->flags & O_DIRECTORY) /* operations about dir */ { rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER); result = jffs2_dir_colse(jffs2_file); @@ -352,8 +352,8 @@ static int dfs_jffs2_read(struct dfs_fd* file, void* buf, size_t len) int char_read; int result; - RT_ASSERT(file->data != NULL); - jffs2_file = (cyg_file *)(file->data); + RT_ASSERT(file->fnode->data != NULL); + jffs2_file = (cyg_file *)(file->fnode->data); uio_s.uio_iov = &iovec; uio_s.uio_iov->iov_base = buf; uio_s.uio_iov->iov_len = len; @@ -384,8 +384,8 @@ static int dfs_jffs2_write(struct dfs_fd* file, int char_write; int result; - RT_ASSERT(file->data != NULL); - jffs2_file = (cyg_file *)(file->data); + RT_ASSERT(file->fnode->data != NULL); + jffs2_file = (cyg_file *)(file->fnode->data); uio_s.uio_iov = &iovec; uio_s.uio_iov->iov_base = (void *)buf; uio_s.uio_iov->iov_len = len; @@ -414,13 +414,13 @@ static int dfs_jffs2_flush(struct dfs_fd* file) /* fixme warning: the offset is rt_off_t, so maybe the size of a file is must <= 2G*/ static int dfs_jffs2_lseek(struct dfs_fd* file, - rt_off_t offset) + off_t offset) { cyg_file * jffs2_file; int result; - RT_ASSERT(file->data != NULL); - jffs2_file = (cyg_file *)(file->data); + RT_ASSERT(file->fnode->data != NULL); + jffs2_file = (cyg_file *)(file->fnode->data); /* set offset as current offset */ rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER); @@ -451,8 +451,8 @@ static int dfs_jffs2_getdents(struct dfs_fd* file, #endif int result; - RT_ASSERT(file->data != RT_NULL); - jffs2_file = (cyg_file*)(file->data); + RT_ASSERT(file->fnode->data != RT_NULL); + jffs2_file = (cyg_file*)(file->fnode->data); mte = jffs2_file->f_mte; //set jffs2_d @@ -494,15 +494,15 @@ static int dfs_jffs2_getdents(struct dfs_fd* file, return -ENOMEM; /* make a right entry */ - if ((file->path[0] == '/') ) + if ((file->fnode->path[0] == '/') ) { - if (file->path[1] == 0) + if (file->fnode->path[1] == 0) strcpy(fullname, jffs2_d.d_name); else - rt_sprintf(fullname, "%s/%s", file->path+1, jffs2_d.d_name); + rt_sprintf(fullname, "%s/%s", file->fnode->path+1, jffs2_d.d_name); } else - rt_sprintf(fullname, "%s/%s", file->path, jffs2_d.d_name); + rt_sprintf(fullname, "%s/%s", file->fnode->path, jffs2_d.d_name); rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER); result = jffs2_porting_stat(mte, mte->root, fullname, (void *)&s); rt_mutex_release(&jffs2_lock); diff --git a/components/dfs/filesystems/jffs2/include/port/sys/stat.h b/components/dfs/filesystems/jffs2/include/port/sys/stat.h index 43958a752f..10a0c98208 100644 --- a/components/dfs/filesystems/jffs2/include/port/sys/stat.h +++ b/components/dfs/filesystems/jffs2/include/port/sys/stat.h @@ -82,11 +82,15 @@ typedef unsigned int mode_t; #endif */ +/* typedef unsigned short nlink_t; typedef long off_t; +*/ +/* typedef unsigned short gid_t; typedef unsigned short uid_t; +*/ typedef int pid_t; // diff --git a/components/dfs/filesystems/jffs2/porting.h b/components/dfs/filesystems/jffs2/porting.h index 2671f46dde..3354cc1d7d 100644 --- a/components/dfs/filesystems/jffs2/porting.h +++ b/components/dfs/filesystems/jffs2/porting.h @@ -22,9 +22,9 @@ struct jffs2_stat { unsigned short st_uid; /* User ID of the file owner */ unsigned short st_gid; /* Group ID of the file's group */ long st_size; /* File size (regular files only) */ - long st_atime; /* Last access time */ - long st_mtime; /* Last data modification time */ - long st_ctime; /* Last file status change time */ + struct timespec st_atim; /* Last access time */ + struct timespec st_mtim; /* Last data modification time */ + struct timespec st_ctim; /* Last file status change time */ }; struct jffs2_dirent diff --git a/components/dfs/filesystems/nfs/dfs_nfs.c b/components/dfs/filesystems/nfs/dfs_nfs.c index e60f6f98c0..454a2303c5 100644 --- a/components/dfs/filesystems/nfs/dfs_nfs.c +++ b/components/dfs/filesystems/nfs/dfs_nfs.c @@ -559,12 +559,12 @@ int nfs_read(struct dfs_fd *file, void *buf, size_t count) nfs_file *fd; nfs_filesystem *nfs; - if (file->type == FT_DIRECTORY) + if (file->fnode->type == FT_DIRECTORY) return -EISDIR; - RT_ASSERT(file->data != NULL); - struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->data)); + RT_ASSERT(file->fnode->data != NULL); + struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->fnode->data)); nfs = (struct nfs_filesystem *)(dfs_nfs->data); fd = (nfs_file *)(nfs->data); RT_ASSERT(fd != NULL); @@ -629,11 +629,11 @@ int nfs_write(struct dfs_fd *file, const void *buf, size_t count) nfs_file *fd; nfs_filesystem *nfs; - if (file->type == FT_DIRECTORY) + if (file->fnode->type == FT_DIRECTORY) return -EISDIR; - RT_ASSERT(file->data != NULL); - struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->data)); + RT_ASSERT(file->fnode->data != NULL); + struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->fnode->data)); nfs = (struct nfs_filesystem *)(dfs_nfs->data); fd = (nfs_file *)(nfs->data); RT_ASSERT(fd != NULL); @@ -676,7 +676,7 @@ int nfs_write(struct dfs_fd *file, const void *buf, size_t count) file->pos = fd->offset; /* update file size */ if (fd->size < fd->offset) fd->size = fd->offset; - file->size = fd->size; + file->fnode->size = fd->size; } xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res); } @@ -692,11 +692,11 @@ int nfs_lseek(struct dfs_fd *file, off_t offset) nfs_file *fd; nfs_filesystem *nfs; - if (file->type == FT_DIRECTORY) + if (file->fnode->type == FT_DIRECTORY) return -EISDIR; - RT_ASSERT(file->data != NULL); - struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->data)); + RT_ASSERT(file->fnode->data != NULL); + struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->fnode->data)); nfs = (struct nfs_filesystem *)(dfs_nfs->data); fd = (nfs_file *)(nfs->data); RT_ASSERT(fd != NULL); @@ -714,11 +714,11 @@ int nfs_lseek(struct dfs_fd *file, off_t offset) int nfs_close(struct dfs_fd *file) { nfs_filesystem *nfs; - RT_ASSERT(file->data != NULL); - struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->data)); + RT_ASSERT(file->fnode->data != NULL); + struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->fnode->data)); nfs = (struct nfs_filesystem *)(dfs_nfs->data); - if (file->type == FT_DIRECTORY) + if (file->fnode->type == FT_DIRECTORY) { struct nfs_dir *dir; @@ -727,7 +727,7 @@ int nfs_close(struct dfs_fd *file) xdr_free((xdrproc_t)xdr_READDIR3res, (char *)&dir->res); rt_free(dir); } - else if (file->type == FT_REGULAR) + else if (file->fnode->type == FT_REGULAR) { struct nfs_file *fd; @@ -744,23 +744,23 @@ int nfs_close(struct dfs_fd *file) int nfs_open(struct dfs_fd *file) { nfs_filesystem *nfs; - RT_ASSERT(file->data != NULL); - struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->data)); + RT_ASSERT(file->fnode->data != NULL); + struct dfs_filesystem *dfs_nfs = file->fnode->fs; nfs = (struct nfs_filesystem *)(dfs_nfs->data); RT_ASSERT(nfs != NULL); - if (file->flags & O_DIRECTORY) + if (file->fnode->flags & O_DIRECTORY) { nfs_dir *dir; - if (file->flags & O_CREAT) + if (file->fnode->flags & O_CREAT) { - if (nfs_mkdir(nfs, file->path, 0755) < 0) + if (nfs_mkdir(nfs, file->fnode->path, 0755) < 0) return -EAGAIN; } /* open directory */ - dir = nfs_opendir(nfs, file->path); + dir = nfs_opendir(nfs, file->fnode->path); if (dir == NULL) return -ENOENT; nfs->data = dir; } @@ -770,9 +770,9 @@ int nfs_open(struct dfs_fd *file) nfs_fh3 *handle; /* create file */ - if (file->flags & O_CREAT) + if (file->fnode->flags & O_CREAT) { - if (nfs_create(nfs, file->path, 0664) < 0) + if (nfs_create(nfs, file->fnode->path, 0664) < 0) return -EAGAIN; } @@ -781,7 +781,7 @@ int nfs_open(struct dfs_fd *file) if (fp == NULL) return -ENOMEM; - handle = get_handle(nfs, file->path); + handle = get_handle(nfs, file->fnode->path); if (handle == NULL) { rt_free(fp); @@ -798,14 +798,14 @@ int nfs_open(struct dfs_fd *file) xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle); rt_free(handle); - if (file->flags & O_APPEND) + if (file->fnode->flags & O_APPEND) { fp->offset = fp->size; } /* set private file */ nfs->data = fp; - file->size = fp->size; + file->fnode->size = fp->size; } return 0; @@ -1086,8 +1086,8 @@ int nfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count) char *name; - RT_ASSERT(file->data != NULL); - struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->data)); + RT_ASSERT(file->fnode->data != NULL); + struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->fnode->data)); nfs = (struct nfs_filesystem *)(dfs_nfs->data); dir = (nfs_dir *)(nfs->data); RT_ASSERT(dir != NULL); diff --git a/components/dfs/filesystems/ramfs/dfs_ramfs.c b/components/dfs/filesystems/ramfs/dfs_ramfs.c index aa85081e1e..3e47720a1c 100644 --- a/components/dfs/filesystems/ramfs/dfs_ramfs.c +++ b/components/dfs/filesystems/ramfs/dfs_ramfs.c @@ -97,13 +97,13 @@ int dfs_ramfs_read(struct dfs_fd *file, void *buf, size_t count) rt_size_t length; struct ramfs_dirent *dirent; - dirent = (struct ramfs_dirent *)file->data; + dirent = (struct ramfs_dirent *)file->fnode->data; RT_ASSERT(dirent != NULL); - if (count < file->size - file->pos) + if (count < file->fnode->size - file->pos) length = count; else - length = file->size - file->pos; + length = file->fnode->size - file->pos; if (length > 0) memcpy(buf, &(dirent->data[file->pos]), length); @@ -119,13 +119,13 @@ int dfs_ramfs_write(struct dfs_fd *fd, const void *buf, size_t count) struct ramfs_dirent *dirent; struct dfs_ramfs *ramfs; - dirent = (struct ramfs_dirent *)fd->data; + dirent = (struct ramfs_dirent *)fd->fnode->data; RT_ASSERT(dirent != NULL); ramfs = dirent->fs; RT_ASSERT(ramfs != NULL); - if (count + fd->pos > fd->size) + if (count + fd->pos > fd->fnode->size) { rt_uint8_t *ptr; ptr = rt_memheap_realloc(&(ramfs->memheap), dirent->data, fd->pos + count); @@ -139,7 +139,7 @@ int dfs_ramfs_write(struct dfs_fd *fd, const void *buf, size_t count) /* update dirent and file size */ dirent->data = ptr; dirent->size = fd->pos + count; - fd->size = dirent->size; + fd->fnode->size = dirent->size; } if (count > 0) @@ -153,7 +153,7 @@ int dfs_ramfs_write(struct dfs_fd *fd, const void *buf, size_t count) int dfs_ramfs_lseek(struct dfs_fd *file, off_t offset) { - if (offset <= (off_t)file->size) + if (offset <= (off_t)file->fnode->size) { file->pos = offset; @@ -165,7 +165,7 @@ int dfs_ramfs_lseek(struct dfs_fd *file, off_t offset) int dfs_ramfs_close(struct dfs_fd *file) { - file->data = NULL; + file->fnode->data = NULL; return RT_EOK; } @@ -177,25 +177,25 @@ int dfs_ramfs_open(struct dfs_fd *file) struct ramfs_dirent *dirent; struct dfs_filesystem *fs; - fs = (struct dfs_filesystem *)file->data; + fs = file->fnode->fs; ramfs = (struct dfs_ramfs *)fs->data; RT_ASSERT(ramfs != NULL); - if (file->flags & O_DIRECTORY) + if (file->fnode->flags & O_DIRECTORY) { - if (file->flags & O_CREAT) + if (file->fnode->flags & O_CREAT) { return -ENOSPC; } /* open directory */ - dirent = dfs_ramfs_lookup(ramfs, file->path, &size); + dirent = dfs_ramfs_lookup(ramfs, file->fnode->path, &size); if (dirent == NULL) return -ENOENT; if (dirent == &(ramfs->root)) /* it's root directory */ { - if (!(file->flags & O_DIRECTORY)) + if (!(file->fnode->flags & O_DIRECTORY)) { return -ENOENT; } @@ -203,7 +203,7 @@ int dfs_ramfs_open(struct dfs_fd *file) } else { - dirent = dfs_ramfs_lookup(ramfs, file->path, &size); + dirent = dfs_ramfs_lookup(ramfs, file->fnode->path, &size); if (dirent == &(ramfs->root)) /* it's root directory */ { return -ENOENT; @@ -211,7 +211,7 @@ int dfs_ramfs_open(struct dfs_fd *file) if (dirent == NULL) { - if (file->flags & O_CREAT || file->flags & O_WRONLY) + if (file->fnode->flags & O_CREAT || file->fnode->flags & O_WRONLY) { char *name_ptr; @@ -225,7 +225,7 @@ int dfs_ramfs_open(struct dfs_fd *file) } /* remove '/' separator */ - name_ptr = file->path; + name_ptr = file->fnode->path; while (*name_ptr == '/' && *name_ptr) name_ptr ++; strncpy(dirent->name, name_ptr, RAMFS_NAME_MAX); @@ -245,7 +245,7 @@ int dfs_ramfs_open(struct dfs_fd *file) /* Creates a new file. * If the file is existing, it is truncated and overwritten. */ - if (file->flags & O_TRUNC) + if (file->fnode->flags & O_TRUNC) { dirent->size = 0; if (dirent->data != NULL) @@ -256,10 +256,10 @@ int dfs_ramfs_open(struct dfs_fd *file) } } - file->data = dirent; - file->size = dirent->size; - if (file->flags & O_APPEND) - file->pos = file->size; + file->fnode->data = dirent; + file->fnode->size = dirent->size; + if (file->fnode->flags & O_APPEND) + file->pos = file->fnode->size; else file->pos = 0; @@ -299,7 +299,7 @@ int dfs_ramfs_getdents(struct dfs_fd *file, struct ramfs_dirent *dirent; struct dfs_ramfs *ramfs; - dirent = (struct ramfs_dirent *)file->data; + dirent = (struct ramfs_dirent *)file->fnode->data; ramfs = dirent->fs; RT_ASSERT(ramfs != RT_NULL); diff --git a/components/dfs/filesystems/romfs/dfs_romfs.c b/components/dfs/filesystems/romfs/dfs_romfs.c index dcc34e5830..455987a363 100644 --- a/components/dfs/filesystems/romfs/dfs_romfs.c +++ b/components/dfs/filesystems/romfs/dfs_romfs.c @@ -133,7 +133,7 @@ int dfs_romfs_read(struct dfs_fd *file, void *buf, size_t count) rt_size_t length; struct romfs_dirent *dirent; - dirent = (struct romfs_dirent *)file->data; + dirent = (struct romfs_dirent *)file->fnode->data; RT_ASSERT(dirent != NULL); if (check_dirent(dirent) != 0) @@ -141,10 +141,10 @@ int dfs_romfs_read(struct dfs_fd *file, void *buf, size_t count) return -EIO; } - if (count < file->size - file->pos) + if (count < file->fnode->size - file->pos) length = count; else - length = file->size - file->pos; + length = file->fnode->size - file->pos; if (length > 0) memcpy(buf, &(dirent->data[file->pos]), length); @@ -157,7 +157,7 @@ int dfs_romfs_read(struct dfs_fd *file, void *buf, size_t count) int dfs_romfs_lseek(struct dfs_fd *file, off_t offset) { - if (offset <= file->size) + if (offset <= file->fnode->size) { file->pos = offset; return file->pos; @@ -168,7 +168,7 @@ int dfs_romfs_lseek(struct dfs_fd *file, off_t offset) int dfs_romfs_close(struct dfs_fd *file) { - file->data = NULL; + file->fnode->data = NULL; return RT_EOK; } @@ -179,34 +179,34 @@ int dfs_romfs_open(struct dfs_fd *file) struct romfs_dirent *root_dirent; struct dfs_filesystem *fs; - fs = (struct dfs_filesystem *)file->data; + fs = file->fnode->fs; root_dirent = (struct romfs_dirent *)fs->data; if (check_dirent(root_dirent) != 0) return -EIO; - if (file->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR)) + if (file->fnode->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR)) return -EINVAL; - dirent = dfs_romfs_lookup(root_dirent, file->path, &size); + dirent = dfs_romfs_lookup(root_dirent, file->fnode->path, &size); if (dirent == NULL) return -ENOENT; /* entry is a directory file type */ if (dirent->type == ROMFS_DIRENT_DIR) { - if (!(file->flags & O_DIRECTORY)) + if (!(file->fnode->flags & O_DIRECTORY)) return -ENOENT; } else { /* entry is a file, but open it as a directory */ - if (file->flags & O_DIRECTORY) + if (file->fnode->flags & O_DIRECTORY) return -ENOENT; } - file->data = dirent; - file->size = size; + file->fnode->data = dirent; + file->fnode->size = size; file->pos = 0; return RT_EOK; @@ -247,7 +247,7 @@ int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count) struct dirent *d; struct romfs_dirent *dirent, *sub_dirent; - dirent = (struct romfs_dirent *)file->data; + dirent = (struct romfs_dirent *)file->fnode->data; if (check_dirent(dirent) != 0) return -EIO; RT_ASSERT(dirent->type == ROMFS_DIRENT_DIR); @@ -261,7 +261,7 @@ int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count) return -EINVAL; index = 0; - for (index = 0; index < count && file->pos < file->size; index ++) + for (index = 0; index < count && file->pos < file->fnode->size; index ++) { d = dirp + index; diff --git a/components/dfs/filesystems/uffs/dfs_uffs.c b/components/dfs/filesystems/uffs/dfs_uffs.c index bc0e79a9a9..f3b588e142 100644 --- a/components/dfs/filesystems/uffs/dfs_uffs.c +++ b/components/dfs/filesystems/uffs/dfs_uffs.c @@ -281,14 +281,14 @@ static int dfs_uffs_open(struct dfs_fd *file) int oflag, mode; char *file_path; - oflag = file->flags; + oflag = file->fnode->flags; if (oflag & O_DIRECTORY) /* operations about dir */ { uffs_DIR *dir; if (oflag & O_CREAT) /* create a dir*/ { - if (uffs_mkdir(file->path) < 0) + if (uffs_mkdir(file->fnode->path) < 0) return uffs_result_to_dfs(uffs_get_error()); } /* open dir */ @@ -296,8 +296,8 @@ static int dfs_uffs_open(struct dfs_fd *file) if (file_path == RT_NULL) return -ENOMEM; - if (file->path[0] == '/' && !(file->path[1] == 0)) - rt_snprintf(file_path, FILE_PATH_MAX, "%s/", file->path); + if (file->fnode->path[0] == '/' && !(file->fnode->path[1] == 0)) + rt_snprintf(file_path, FILE_PATH_MAX, "%s/", file->fnode->path); else { file_path[0] = '/'; @@ -312,7 +312,7 @@ static int dfs_uffs_open(struct dfs_fd *file) return uffs_result_to_dfs(uffs_get_error()); } /* save this pointer,will used by dfs_uffs_getdents*/ - file->data = dir; + file->fnode->data = dir; rt_free(file_path); return RT_EOK; } @@ -330,7 +330,7 @@ static int dfs_uffs_open(struct dfs_fd *file) /* Creates a new file. The function fails if the file is already existing. */ if (oflag & O_EXCL) mode |= UO_EXCL; - fd = uffs_open(file->path, mode); + fd = uffs_open(file->fnode->path, mode); if (fd < 0) { return uffs_result_to_dfs(uffs_get_error()); @@ -339,9 +339,9 @@ static int dfs_uffs_open(struct dfs_fd *file) /* save this pointer, it will be used when calling read(), write(), * flush(), seek(), and will be free when calling close()*/ - file->data = (void *)fd; + file->fnode->data = (void *)fd; file->pos = uffs_seek(fd, 0, USEEK_CUR); - file->size = uffs_seek(fd, 0, USEEK_END); + file->fnode->size = uffs_seek(fd, 0, USEEK_END); uffs_seek(fd, file->pos, USEEK_SET); if (oflag & O_APPEND) @@ -356,17 +356,17 @@ static int dfs_uffs_close(struct dfs_fd *file) int oflag; int fd; - oflag = file->flags; + oflag = file->fnode->flags; if (oflag & O_DIRECTORY) { /* operations about dir */ - if (uffs_closedir((uffs_DIR *)(file->data)) < 0) + if (uffs_closedir((uffs_DIR *)(file->fnode->data)) < 0) return uffs_result_to_dfs(uffs_get_error()); return 0; } /* regular file operations */ - fd = (int)(file->data); + fd = (int)(file->fnode->data); if (uffs_close(fd) == 0) return 0; @@ -384,7 +384,7 @@ static int dfs_uffs_read(struct dfs_fd *file, void *buf, size_t len) int fd; int char_read; - fd = (int)(file->data); + fd = (int)(file->fnode->data); char_read = uffs_read(fd, buf, len); if (char_read < 0) return uffs_result_to_dfs(uffs_get_error()); @@ -401,7 +401,7 @@ static int dfs_uffs_write(struct dfs_fd *file, int fd; int char_write; - fd = (int)(file->data); + fd = (int)(file->fnode->data); char_write = uffs_write(fd, buf, len); if (char_write < 0) @@ -417,7 +417,7 @@ static int dfs_uffs_flush(struct dfs_fd *file) int fd; int result; - fd = (int)(file->data); + fd = (int)(file->fnode->data); result = uffs_flush(fd); if (result < 0) @@ -445,19 +445,19 @@ static int dfs_uffs_seek(struct dfs_fd *file, int result; /* set offset as current offset */ - if (file->type == FT_DIRECTORY) + if (file->fnode->type == FT_DIRECTORY) { - uffs_rewinddir((uffs_DIR *)(file->data)); - result = uffs_seekdir((uffs_DIR *)(file->data), offset / sizeof(struct dirent)); + uffs_rewinddir((uffs_DIR *)(file->fnode->data)); + result = uffs_seekdir((uffs_DIR *)(file->fnode->data), offset / sizeof(struct dirent)); if (result >= 0) { file->pos = offset; return offset; } } - else if (file->type == FT_REGULAR) + else if (file->fnode->type == FT_REGULAR) { - result = uffs_seek((int)(file->data), offset, USEEK_SET); + result = uffs_seek((int)(file->fnode->data), offset, USEEK_SET); if (result >= 0) return offset; } @@ -477,7 +477,7 @@ static int dfs_uffs_getdents( uffs_DIR *dir; struct uffs_dirent *uffs_d; - dir = (uffs_DIR *)(file->data); + dir = (uffs_DIR *)(file->fnode->data); RT_ASSERT(dir != RT_NULL); /* round count, count is always 1 */ @@ -504,8 +504,8 @@ static int dfs_uffs_getdents( return (uffs_result_to_dfs(uffs_get_error())); } - if (file->path[0] == '/' && !(file->path[1] == 0)) - rt_snprintf(file_path, FILE_PATH_MAX, "%s/%s", file->path, uffs_d->d_name); + if (file->fnode->path[0] == '/' && !(file->fnode->path[1] == 0)) + rt_snprintf(file_path, FILE_PATH_MAX, "%s/%s", file->fnode->path, uffs_d->d_name); else rt_strncpy(file_path, uffs_d->d_name, FILE_PATH_MAX); diff --git a/components/dfs/filesystems/uffs/src/inc/uffs/uffs_fd.h b/components/dfs/filesystems/uffs/src/inc/uffs/uffs_fd.h index c5d8159074..4d6d596bc5 100644 --- a/components/dfs/filesystems/uffs/src/inc/uffs/uffs_fd.h +++ b/components/dfs/filesystems/uffs/src/inc/uffs/uffs_fd.h @@ -97,9 +97,9 @@ struct uffs_stat { long st_size; /* total size, in bytes */ int st_blksize; /* blocksize for filesystem I/O */ int st_blocks; /* number of blocks allocated */ - unsigned int st_atime; /* time of last access */ - unsigned int st_mtime; /* time of last modification */ - unsigned int st_ctime; /* time of last status change */ + struct timespec st_atim; /* time of last access */ + struct timespec st_mtim; /* time of last modification */ + struct timespec st_ctim; /* time of last status change */ }; /* POSIX complaint file system APIs */ diff --git a/components/dfs/include/dfs.h b/components/dfs/include/dfs.h index 165bf325fd..14b3995f37 100644 --- a/components/dfs/include/dfs.h +++ b/components/dfs/include/dfs.h @@ -105,8 +105,8 @@ int fdt_fd_new(struct dfs_fdtable *fdt); struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd, int ref_inc_nr); void fdt_fd_release(struct dfs_fdtable* fdt, struct dfs_fd *fd); int fd_new(void); -struct dfs_fd *dfs_fd_get(int fd); -void dfs_fd_release(struct dfs_fd *fd); +struct dfs_fd *fd_get(int fd, int ref_inc_nr); +void fd_release(struct dfs_fd *fd); int fd_is_open(const char *pathname); struct dfs_fdtable *dfs_fdtable_get(void); diff --git a/components/dfs/include/dfs_file.h b/components/dfs/include/dfs_file.h index f90fd805e2..c77902654b 100644 --- a/components/dfs/include/dfs_file.h +++ b/components/dfs/include/dfs_file.h @@ -36,9 +36,9 @@ struct dfs_file_ops /* file descriptor */ #define DFS_FD_MAGIC 0xfdfd -struct dfs_fd + +struct dfs_fnode { - uint16_t magic; /* file descriptor magic number */ uint16_t type; /* Type (regular or socket) */ char *path; /* Name (below mount point) */ @@ -49,11 +49,19 @@ struct dfs_fd uint32_t flags; /* Descriptor flags */ size_t size; /* Size in bytes */ - off_t pos; /* Current file position */ - void *data; /* Specific file system data */ }; +struct dfs_fd +{ + int idx; /* the idx in fdt */ + uint16_t magic; /* file descriptor magic number */ + int ref_count; /* Descriptor reference count */ + off_t pos; /* Current file position */ + struct dfs_fnode *fnode; /* file node struct */ + void *data; /* Specific fd data */ +}; + int dfs_file_open(struct dfs_fd *fd, const char *path, int flags); int dfs_file_close(struct dfs_fd *fd); int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args); diff --git a/components/dfs/src/dfs.c b/components/dfs/src/dfs.c index 7e3e640360..15a480d4ee 100644 --- a/components/dfs/src/dfs.c +++ b/components/dfs/src/dfs.c @@ -142,7 +142,8 @@ void dfs_fd_unlock(void) static int fd_alloc(struct dfs_fdtable *fdt, int startfd) { - int idx; + int idx = 0; + struct dfs_fd *fd = NULL; /* find an empty fd entry */ for (idx = startfd; idx < (int)fdt->maxfd; idx++) @@ -156,15 +157,19 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd) /* allocate a larger FD container */ if (idx == fdt->maxfd && fdt->maxfd < DFS_FD_MAX) { - int cnt, index; - struct dfs_fd **fds; + int cnt = 0; + int index = 0; + struct dfs_fd **fds = NULL; /* increase the number of FD with 4 step length */ cnt = fdt->maxfd + 4; cnt = cnt > DFS_FD_MAX ? DFS_FD_MAX : cnt; fds = (struct dfs_fd **)rt_realloc(fdt->fds, cnt * sizeof(struct dfs_fd *)); - if (fds == NULL) goto __exit; /* return fdt->maxfd */ + if (fds == NULL) /* return fdt->maxfd */ + { + return fdt->maxfd; + } /* clean the new allocated fds */ for (index = fdt->maxfd; index < cnt; index ++) @@ -179,12 +184,27 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd) /* allocate 'struct dfs_fd' */ if (idx < (int)fdt->maxfd && fdt->fds[idx] == RT_NULL) { - fdt->fds[idx] = (struct dfs_fd *)rt_calloc(1, sizeof(struct dfs_fd)); - if (fdt->fds[idx] == RT_NULL) - idx = fdt->maxfd; + struct dfs_fnode *fnode = rt_calloc(1, sizeof(struct dfs_fnode)); + + if (!fnode) + { + return fdt->maxfd; + } + fnode->ref_count = 1; + + fd = (struct dfs_fd *)rt_calloc(1, sizeof(struct dfs_fd)); + if (!fd) + { + rt_free(fnode); + return fdt->maxfd; + } + fd->ref_count = 1; + fd->magic = DFS_FD_MAGIC; + fd->fnode = fnode; + fd->idx = idx; + fdt->fds[idx] = fd; } -__exit: return idx; } @@ -196,8 +216,7 @@ __exit: */ int fdt_fd_new(struct dfs_fdtable *fdt) { - struct dfs_fd *d; - int idx; + int idx = 0; /* lock filesystem */ dfs_fd_lock(); @@ -213,10 +232,6 @@ int fdt_fd_new(struct dfs_fdtable *fdt) goto __result; } - d = fdt->fds[idx]; - d->ref_count = 1; - d->magic = DFS_FD_MAGIC; - __result: dfs_fd_unlock(); return idx + DFS_FD_OFFSET; @@ -224,7 +239,7 @@ __result: int fd_new(void) { - struct dfs_fdtable *fdt; + struct dfs_fdtable *fdt = NULL; fdt = dfs_fdtable_get(); return fdt_fd_new(fdt); @@ -270,12 +285,12 @@ struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd, int ref_inc_nr) return d; } -struct dfs_fd *dfs_fd_get(int fd) +struct dfs_fd *fd_get(int fd, int ref_inc_nr) { struct dfs_fdtable *fdt; fdt = dfs_fdtable_get(); - return fdt_fd_get(fdt, fd, 0); + return fdt_fd_get(fdt, fd, ref_inc_nr); } /** @@ -285,31 +300,39 @@ struct dfs_fd *dfs_fd_get(int fd) */ void fdt_fd_release(struct dfs_fdtable* fdt, struct dfs_fd *fd) { + int idx = -1; + RT_ASSERT(fd != NULL); + RT_ASSERT(fdt != NULL); dfs_fd_lock(); - fd->ref_count --; + idx = fd->idx; + /* check fd */ + RT_ASSERT(fdt->fds[idx] == fd); + RT_ASSERT(fd->magic == DFS_FD_MAGIC); + + fd->ref_count--; /* clear this fd entry */ if (fd->ref_count == 0) { - int index; - - for (index = 0; index < (int)fdt->maxfd; index ++) + struct dfs_fnode *fnode = fd->fnode; + if (fnode) { - if (fdt->fds[index] == fd) + fnode->ref_count--; + if (fnode->ref_count == 0) { - rt_free(fd); - fdt->fds[index] = 0; - break; + rt_free(fnode); } } + rt_free(fd); + fdt->fds[idx] = 0; } dfs_fd_unlock(); } -void dfs_fd_release(struct dfs_fd *fd) +void fd_release(struct dfs_fd *fd) { struct dfs_fdtable *fdt; @@ -359,9 +382,9 @@ int fd_is_open(const char *pathname) for (index = 0; index < fdt->maxfd; index++) { fd = fdt->fds[index]; - if (fd == NULL || fd->fops == NULL || fd->path == NULL) continue; + if (fd == NULL || fd->fnode->fops == NULL || fd->fnode->path == NULL) continue; - if (fd->fs == fs && strcmp(fd->path, mountpath) == 0) + if (fd->fnode->fs == fs && strcmp(fd->fnode->path, mountpath) == 0) { /* found file in file descriptor table */ rt_free(fullpath); @@ -581,20 +604,20 @@ int list_fd(void) { struct dfs_fd *fd = fd_table->fds[index]; - if (fd && fd->fops) + if (fd && fd->fnode->fops) { rt_kprintf("%2d ", index + DFS_FD_OFFSET); - if (fd->type == FT_DIRECTORY) rt_kprintf("%-7.7s ", "dir"); - else if (fd->type == FT_REGULAR) rt_kprintf("%-7.7s ", "file"); - else if (fd->type == FT_SOCKET) rt_kprintf("%-7.7s ", "socket"); - else if (fd->type == FT_USER) rt_kprintf("%-7.7s ", "user"); - else if (fd->type == FT_DEVICE) rt_kprintf("%-7.7s ", "device"); + if (fd->fnode->type == FT_DIRECTORY) rt_kprintf("%-7.7s ", "dir"); + else if (fd->fnode->type == FT_REGULAR) rt_kprintf("%-7.7s ", "file"); + else if (fd->fnode->type == FT_SOCKET) rt_kprintf("%-7.7s ", "socket"); + else if (fd->fnode->type == FT_USER) rt_kprintf("%-7.7s ", "user"); + else if (fd->fnode->type == FT_DEVICE) rt_kprintf("%-7.7s ", "device"); else rt_kprintf("%-8.8s ", "unknown"); - rt_kprintf("%3d ", fd->ref_count); + rt_kprintf("%3d ", fd->fnode->ref_count); rt_kprintf("%04x ", fd->magic); - if (fd->path) + if (fd->fnode->path) { - rt_kprintf("%s\n", fd->path); + rt_kprintf("%s\n", fd->fnode->path); } else { diff --git a/components/dfs/src/dfs_file.c b/components/dfs/src/dfs_file.c index 2de0ae4883..90a61dd201 100644 --- a/components/dfs/src/dfs_file.c +++ b/components/dfs/src/dfs_file.c @@ -59,56 +59,56 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) } LOG_D("open in filesystem:%s", fs->ops->name); - fd->fs = fs; /* set file system */ - fd->fops = fs->ops->fops; /* set file ops */ + fd->fnode->fs = fs; /* set file system */ + fd->fnode->fops = fs->ops->fops; /* set file ops */ /* initialize the fd item */ - fd->type = FT_REGULAR; - fd->flags = flags; - fd->size = 0; + fd->fnode->type = FT_REGULAR; + fd->fnode->flags = flags; + fd->fnode->size = 0; fd->pos = 0; - fd->data = fs; + fd->fnode->data = fs; if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH)) { if (dfs_subdir(fs->path, fullpath) == NULL) - fd->path = rt_strdup("/"); + fd->fnode->path = rt_strdup("/"); else - fd->path = rt_strdup(dfs_subdir(fs->path, fullpath)); + fd->fnode->path = rt_strdup(dfs_subdir(fs->path, fullpath)); rt_free(fullpath); - LOG_D("Actual file path: %s", fd->path); + LOG_D("Actual file path: %s", fd->fnode->path); } else { - fd->path = fullpath; + fd->fnode->path = fullpath; } /* specific file system open routine */ - if (fd->fops->open == NULL) + if (fd->fnode->fops->open == NULL) { /* clear fd */ - rt_free(fd->path); - fd->path = NULL; + rt_free(fd->fnode->path); + fd->fnode->path = NULL; return -ENOSYS; } - if ((result = fd->fops->open(fd)) < 0) + if ((result = fd->fnode->fops->open(fd)) < 0) { /* clear fd */ - rt_free(fd->path); - fd->path = NULL; + rt_free(fd->fnode->path); + fd->fnode->path = NULL; LOG_D("%s open failed", fullpath); return result; } - fd->flags |= DFS_F_OPEN; + fd->fnode->flags |= DFS_F_OPEN; if (flags & O_DIRECTORY) { - fd->type = FT_DIRECTORY; - fd->flags |= DFS_F_DIRECTORY; + fd->fnode->type = FT_DIRECTORY; + fd->fnode->flags |= DFS_F_DIRECTORY; } LOG_D("open successful"); @@ -129,15 +129,15 @@ int dfs_file_close(struct dfs_fd *fd) if (fd == NULL) return -ENXIO; - if (fd->fops->close != NULL) - result = fd->fops->close(fd); + if (fd->fnode->fops->close != NULL) + result = fd->fnode->fops->close(fd); /* close fd error, return */ if (result < 0) return result; - rt_free(fd->path); - fd->path = NULL; + rt_free(fd->fnode->path); + fd->fnode->path = NULL; return result; } @@ -157,27 +157,27 @@ int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args) return -EINVAL; /* regular file system fd */ - if (fd->type == FT_REGULAR || fd->type == FT_DEVICE) + if (fd->fnode->type == FT_REGULAR || fd->fnode->type == FT_DEVICE) { switch (cmd) { case F_GETFL: - return fd->flags; /* return flags */ + return fd->fnode->flags; /* return flags */ case F_SETFL: { int flags = (int)(rt_base_t)args; int mask = O_NONBLOCK | O_APPEND; flags &= mask; - fd->flags &= ~mask; - fd->flags |= flags; + fd->fnode->flags &= ~mask; + fd->fnode->flags |= flags; } return 0; } } - if (fd->fops->ioctl != NULL) - return fd->fops->ioctl(fd, cmd, args); + if (fd->fnode->fops->ioctl != NULL) + return fd->fnode->fops->ioctl(fd, cmd, args); return -ENOSYS; } @@ -199,11 +199,11 @@ int dfs_file_read(struct dfs_fd *fd, void *buf, size_t len) if (fd == NULL) return -EINVAL; - if (fd->fops->read == NULL) + if (fd->fnode->fops->read == NULL) return -ENOSYS; - if ((result = fd->fops->read(fd, buf, len)) < 0) - fd->flags |= DFS_F_EOF; + if ((result = fd->fnode->fops->read(fd, buf, len)) < 0) + fd->fnode->flags |= DFS_F_EOF; return result; } @@ -220,11 +220,11 @@ int dfs_file_read(struct dfs_fd *fd, void *buf, size_t len) int dfs_file_getdents(struct dfs_fd *fd, struct dirent *dirp, size_t nbytes) { /* parameter check */ - if (fd == NULL || fd->type != FT_DIRECTORY) + if (fd == NULL || fd->fnode->type != FT_DIRECTORY) return -EINVAL; - if (fd->fops->getdents != NULL) - return fd->fops->getdents(fd, dirp, nbytes); + if (fd->fnode->fops->getdents != NULL) + return fd->fnode->fops->getdents(fd, dirp, nbytes); return -ENOSYS; } @@ -296,10 +296,10 @@ int dfs_file_write(struct dfs_fd *fd, const void *buf, size_t len) if (fd == NULL) return -EINVAL; - if (fd->fops->write == NULL) + if (fd->fnode->fops->write == NULL) return -ENOSYS; - return fd->fops->write(fd, buf, len); + return fd->fnode->fops->write(fd, buf, len); } /** @@ -314,10 +314,10 @@ int dfs_file_flush(struct dfs_fd *fd) if (fd == NULL) return -EINVAL; - if (fd->fops->flush == NULL) + if (fd->fnode->fops->flush == NULL) return -ENOSYS; - return fd->fops->flush(fd); + return fd->fnode->fops->flush(fd); } /** @@ -335,10 +335,10 @@ int dfs_file_lseek(struct dfs_fd *fd, off_t offset) if (fd == NULL) return -EINVAL; - if (fd->fops->lseek == NULL) + if (fd->fnode->fops->lseek == NULL) return -ENOSYS; - result = fd->fops->lseek(fd, offset); + result = fd->fnode->fops->lseek(fd, offset); /* update current position */ if (result >= 0) @@ -494,17 +494,17 @@ int dfs_file_ftruncate(struct dfs_fd *fd, off_t length) int result; /* fd is null or not a regular file system fd, or length is invalid */ - if (fd == NULL || fd->type != FT_REGULAR || length < 0) + if (fd == NULL || fd->fnode->type != FT_REGULAR || length < 0) return -EINVAL; - if (fd->fops->ioctl == NULL) + if (fd->fnode->fops->ioctl == NULL) return -ENOSYS; - result = fd->fops->ioctl(fd, RT_FIOFTRUNCATE, (void*)&length); + result = fd->fnode->fops->ioctl(fd, RT_FIOFTRUNCATE, (void*)&length); /* update current size */ if (result == 0) - fd->size = length; + fd->fnode->size = length; return result; } @@ -512,7 +512,8 @@ int dfs_file_ftruncate(struct dfs_fd *fd, off_t length) #ifdef RT_USING_FINSH #include -static struct dfs_fd fd; +static struct dfs_fnode fnode; +static struct dfs_fd fd = {0, 0, 0, 0, &fnode}; static struct dirent dirent; void ls(const char *pathname) { diff --git a/components/dfs/src/dfs_fs.c b/components/dfs/src/dfs_fs.c index da13c76e3c..d867de013a 100644 --- a/components/dfs/src/dfs_fs.c +++ b/components/dfs/src/dfs_fs.c @@ -272,6 +272,9 @@ int dfs_mount(const char *device_name, if ((strcmp(fullpath, "/") != 0) && (strcmp(fullpath, "/dev") != 0)) { struct dfs_fd fd; + struct dfs_fnode fnode; + + fd.fnode = &fnode; if (dfs_file_open(&fd, fullpath, O_RDONLY | O_DIRECTORY) < 0) { diff --git a/components/dfs/src/dfs_posix.c b/components/dfs/src/dfs_posix.c index b8b8034d20..92790d965a 100644 --- a/components/dfs/src/dfs_posix.c +++ b/components/dfs/src/dfs_posix.c @@ -41,13 +41,13 @@ int open(const char *file, int flags, ...) return -1; } - d = dfs_fd_get(fd); + d = fd_get(fd, 0); result = dfs_file_open(d, file, flags); if (result < 0) { /* release the ref-count of fd */ - dfs_fd_release(d); + fd_release(d); rt_set_errno(result); @@ -71,7 +71,7 @@ int close(int fd) int result; struct dfs_fd *d; - d = fd_get(fd); + d = fd_get(fd, 0); if (d == NULL) { rt_set_errno(-EBADF); @@ -80,7 +80,6 @@ int close(int fd) } result = dfs_file_close(d); - fd_put(d); if (result < 0) { @@ -89,7 +88,7 @@ int close(int fd) return -1; } - fd_put(d); + fd_release(d); return 0; } @@ -116,7 +115,7 @@ int read(int fd, void *buf, size_t len) struct dfs_fd *d; /* get the fd */ - d = dfs_fd_get(fd); + d = fd_get(fd, 0); if (d == NULL) { rt_set_errno(-EBADF); @@ -156,7 +155,7 @@ int write(int fd, const void *buf, size_t len) struct dfs_fd *d; /* get the fd */ - d = dfs_fd_get(fd); + d = fd_get(fd, 0); if (d == NULL) { rt_set_errno(-EBADF); @@ -191,7 +190,7 @@ off_t lseek(int fd, off_t offset, int whence) int result; struct dfs_fd *d; - d = dfs_fd_get(fd); + d = fd_get(fd, 0); if (d == NULL) { rt_set_errno(-EBADF); @@ -209,7 +208,7 @@ off_t lseek(int fd, off_t offset, int whence) break; case SEEK_END: - offset += d->size; + offset += d->fnode->size; break; default: @@ -325,7 +324,7 @@ int fstat(int fildes, struct stat *buf) struct dfs_fd *d; /* get the fd */ - d = dfs_fd_get(fildes); + d = fd_get(fildes, 0); if (d == NULL) { rt_set_errno(-EBADF); @@ -338,13 +337,13 @@ int fstat(int fildes, struct stat *buf) buf->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH; - if (d->type == FT_DIRECTORY) + if (d->fnode->type == FT_DIRECTORY) { buf->st_mode &= ~S_IFREG; buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH; } - buf->st_size = d->size; + buf->st_size = d->fnode->size; buf->st_mtime = 0; return RT_EOK; @@ -368,7 +367,7 @@ int fsync(int fildes) struct dfs_fd *d; /* get the fd */ - d = dfs_fd_get(fildes); + d = fd_get(fildes, 0); if (d == NULL) { rt_set_errno(-EBADF); @@ -399,7 +398,7 @@ int fcntl(int fildes, int cmd, ...) struct dfs_fd *d; /* get the fd */ - d = dfs_fd_get(fildes); + d = fd_get(fildes, 0); if (d) { void *arg; @@ -464,7 +463,7 @@ int ftruncate(int fd, off_t length) int result; struct dfs_fd *d; - d = dfs_fd_get(fd); + d = fd_get(fd, 0); if (d == NULL) { rt_set_errno(-EBADF); @@ -537,20 +536,20 @@ int mkdir(const char *path, mode_t mode) return -1; } - d = dfs_fd_get(fd); + d = fd_get(fd, 0); result = dfs_file_open(d, path, O_DIRECTORY | O_CREAT); if (result < 0) { - dfs_fd_release(d); + fd_release(d); rt_set_errno(result); return -1; } dfs_file_close(d); - dfs_fd_release(d); + fd_release(d); return 0; } @@ -607,7 +606,7 @@ DIR *opendir(const char *name) return NULL; } - d = dfs_fd_get(fd); + d = fd_get(fd, 0); result = dfs_file_open(d, name, O_RDONLY | O_DIRECTORY); if (result >= 0) @@ -617,7 +616,7 @@ DIR *opendir(const char *name) if (t == NULL) { dfs_file_close(d); - dfs_fd_release(d); + fd_release(d); } else { @@ -630,7 +629,7 @@ DIR *opendir(const char *name) } /* open failed */ - dfs_fd_release(d); + fd_release(d); rt_set_errno(result); return NULL; @@ -651,7 +650,7 @@ struct dirent *readdir(DIR *d) int result; struct dfs_fd *fd; - fd = dfs_fd_get(d->fd); + fd = fd_get(d->fd, 0); if (fd == NULL) { rt_set_errno(-EBADF); @@ -699,7 +698,7 @@ long telldir(DIR *d) struct dfs_fd *fd; long result; - fd = dfs_fd_get(d->fd); + fd = fd_get(d->fd, 0); if (fd == NULL) { rt_set_errno(-EBADF); @@ -724,7 +723,7 @@ void seekdir(DIR *d, off_t offset) { struct dfs_fd *fd; - fd = dfs_fd_get(d->fd); + fd = fd_get(d->fd, 0); if (fd == NULL) { rt_set_errno(-EBADF); @@ -748,7 +747,7 @@ void rewinddir(DIR *d) { struct dfs_fd *fd; - fd = dfs_fd_get(d->fd); + fd = fd_get(d->fd, 0); if (fd == NULL) { rt_set_errno(-EBADF); @@ -775,7 +774,7 @@ int closedir(DIR *d) int result; struct dfs_fd *fd; - fd = dfs_fd_get(d->fd); + fd = fd_get(d->fd, 0); if (fd == NULL) { rt_set_errno(-EBADF); @@ -784,7 +783,7 @@ int closedir(DIR *d) } result = dfs_file_close(fd); - dfs_fd_release(fd); + fd_release(fd); rt_free(d); diff --git a/components/dfs/src/poll.c b/components/dfs/src/poll.c index 184efc0e26..3a8f4f6570 100644 --- a/components/dfs/src/poll.c +++ b/components/dfs/src/poll.c @@ -128,17 +128,17 @@ static int do_pollfd(struct pollfd *pollfd, rt_pollreq_t *req) if (fd >= 0) { - struct dfs_fd *f = dfs_fd_get(fd); + struct dfs_fd *f = fd_get(fd, 0); mask = POLLNVAL; if (f) { mask = POLLMASK_DEFAULT; - if (f->fops->poll) + if (f->fnode->fops->poll) { req->_key = pollfd->events | POLLERR | POLLHUP; - mask = f->fops->poll(f, req); + mask = f->fnode->fops->poll(f, req); /* dealwith the device return error -1*/ if (mask < 0) diff --git a/components/drivers/serial/serial.c b/components/drivers/serial/serial.c index dc9c7ac2cf..86e89e3c51 100644 --- a/components/drivers/serial/serial.c +++ b/components/drivers/serial/serial.c @@ -65,10 +65,10 @@ static int serial_fops_open(struct dfs_fd *fd) rt_uint16_t flags = 0; rt_device_t device; - device = (rt_device_t)fd->data; + device = (rt_device_t)fd->fnode->data; RT_ASSERT(device != RT_NULL); - switch (fd->flags & O_ACCMODE) + switch (fd->fnode->flags & O_ACCMODE) { case O_RDONLY: LOG_D("fops open: O_RDONLY!"); @@ -83,11 +83,11 @@ static int serial_fops_open(struct dfs_fd *fd) flags = RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_RDWR; break; default: - LOG_E("fops open: unknown mode - %d!", fd->flags & O_ACCMODE); + LOG_E("fops open: unknown mode - %d!", fd->fnode->flags & O_ACCMODE); break; } - if ((fd->flags & O_ACCMODE) != O_WRONLY) + if ((fd->fnode->flags & O_ACCMODE) != O_WRONLY) rt_device_set_rx_indicate(device, serial_fops_rx_ind); ret = rt_device_open(device, flags); if (ret == RT_EOK) return 0; @@ -99,7 +99,7 @@ static int serial_fops_close(struct dfs_fd *fd) { rt_device_t device; - device = (rt_device_t)fd->data; + device = (rt_device_t)fd->fnode->data; rt_device_set_rx_indicate(device, RT_NULL); rt_device_close(device); @@ -111,7 +111,7 @@ static int serial_fops_ioctl(struct dfs_fd *fd, int cmd, void *args) { rt_device_t device; - device = (rt_device_t)fd->data; + device = (rt_device_t)fd->fnode->data; switch (cmd) { case FIONREAD: @@ -129,14 +129,14 @@ static int serial_fops_read(struct dfs_fd *fd, void *buf, size_t count) rt_device_t device; int wait_ret; - device = (rt_device_t)fd->data; + device = (rt_device_t)fd->fnode->data; do { size = rt_device_read(device, -1, buf, count); if (size <= 0) { - if (fd->flags & O_NONBLOCK) + if (fd->fnode->flags & O_NONBLOCK) { break; } @@ -160,7 +160,7 @@ static int serial_fops_write(struct dfs_fd *fd, const void *buf, size_t count) { rt_device_t device; - device = (rt_device_t)fd->data; + device = (rt_device_t)fd->fnode->data; return rt_device_write(device, -1, buf, count); } @@ -171,13 +171,13 @@ static int serial_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req) rt_device_t device; struct rt_serial_device *serial; - device = (rt_device_t)fd->data; + device = (rt_device_t)fd->fnode->data; RT_ASSERT(device != RT_NULL); serial = (struct rt_serial_device *)device; /* only support POLLIN */ - flags = fd->flags & O_ACCMODE; + flags = fd->fnode->flags & O_ACCMODE; if (flags == O_RDONLY || flags == O_RDWR) { rt_base_t level; diff --git a/components/drivers/src/pipe.c b/components/drivers/src/pipe.c index 2a7e143f6c..2a09459b6b 100644 --- a/components/drivers/src/pipe.c +++ b/components/drivers/src/pipe.c @@ -23,7 +23,7 @@ static int pipe_fops_open(struct dfs_fd *fd) rt_device_t device; rt_pipe_t *pipe; - pipe = (rt_pipe_t *)fd->data; + pipe = (rt_pipe_t *)fd->fnode->data; if (!pipe) return -1; device = &(pipe->parent); @@ -39,7 +39,7 @@ static int pipe_fops_open(struct dfs_fd *fd) } } - switch (fd->flags & O_ACCMODE) + switch (fd->fnode->flags & O_ACCMODE) { case O_RDONLY: pipe->readers ++; @@ -65,13 +65,13 @@ static int pipe_fops_close(struct dfs_fd *fd) rt_device_t device; rt_pipe_t *pipe; - pipe = (rt_pipe_t *)fd->data; + pipe = (rt_pipe_t *)fd->fnode->data; if (!pipe) return -1; device = &(pipe->parent); rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER); - switch (fd->flags & O_ACCMODE) + switch (fd->fnode->flags & O_ACCMODE) { case O_RDONLY: pipe->readers --; @@ -119,7 +119,7 @@ static int pipe_fops_ioctl(struct dfs_fd *fd, int cmd, void *args) rt_pipe_t *pipe; int ret = 0; - pipe = (rt_pipe_t *)fd->data; + pipe = (rt_pipe_t *)fd->fnode->data; switch (cmd) { @@ -142,7 +142,7 @@ static int pipe_fops_read(struct dfs_fd *fd, void *buf, size_t count) int len = 0; rt_pipe_t *pipe; - pipe = (rt_pipe_t *)fd->data; + pipe = (rt_pipe_t *)fd->fnode->data; /* no process has the pipe open for writing, return end-of-file */ if (pipe->writers == 0) @@ -165,7 +165,7 @@ static int pipe_fops_read(struct dfs_fd *fd, void *buf, size_t count) } else { - if (fd->flags & O_NONBLOCK) + if (fd->fnode->flags & O_NONBLOCK) { len = -EAGAIN; goto out; @@ -194,7 +194,7 @@ static int pipe_fops_write(struct dfs_fd *fd, const void *buf, size_t count) int ret = 0; uint8_t *pbuf; - pipe = (rt_pipe_t *)fd->data; + pipe = (rt_pipe_t *)fd->fnode->data; if (pipe->readers == 0) { @@ -228,7 +228,7 @@ static int pipe_fops_write(struct dfs_fd *fd, const void *buf, size_t count) } else { - if (fd->flags & O_NONBLOCK) + if (fd->fnode->flags & O_NONBLOCK) { if (ret == 0) { @@ -261,12 +261,12 @@ static int pipe_fops_poll(struct dfs_fd *fd, rt_pollreq_t *req) int mask = 0; rt_pipe_t *pipe; int mode = 0; - pipe = (rt_pipe_t *)fd->data; + pipe = (rt_pipe_t *)fd->fnode->data; rt_poll_add(&(pipe->reader_queue), req); rt_poll_add(&(pipe->writer_queue), req); - switch (fd->flags & O_ACCMODE) + switch (fd->fnode->flags & O_ACCMODE) { case O_RDONLY: mode = 1; diff --git a/components/lwp/lwp_console.c b/components/lwp/lwp_console.c index f964d6a055..881e05dea9 100644 --- a/components/lwp/lwp_console.c +++ b/components/lwp/lwp_console.c @@ -214,10 +214,10 @@ static int console_fops_open(struct dfs_fd *fd) int ret; struct rt_device * device; - device = (struct rt_device *)fd->data; + device = (struct rt_device *)fd->fnode->data; RT_ASSERT(device != RT_NULL); - ret = rt_device_open(device, fd->flags); + ret = rt_device_open(device, fd->fnode->flags); return ret; } @@ -226,7 +226,7 @@ static int console_fops_close(struct dfs_fd *fd) int ret; struct rt_device * device; - device = (struct rt_device *)fd->data; + device = (struct rt_device *)fd->fnode->data; RT_ASSERT(device != RT_NULL); ret = rt_device_close(device); @@ -242,7 +242,7 @@ static int console_fops_read(struct dfs_fd *fd, void *buf, size_t count) struct rt_wqueue *wq; int wait_ret; - console = (struct rt_console_device *)fd->data; + console = (struct rt_console_device *)fd->fnode->data; RT_ASSERT(console != RT_NULL); RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED); @@ -258,7 +258,7 @@ static int console_fops_read(struct dfs_fd *fd, void *buf, size_t count) { break; } - if (fd->flags & O_NONBLOCK) + if (fd->fnode->flags & O_NONBLOCK) { break; } @@ -281,7 +281,7 @@ static int console_fops_write(struct dfs_fd *fd, const void *buf, size_t count) int size; struct rt_device * device; - device = (struct rt_device *)fd->data; + device = (struct rt_device *)fd->fnode->data; RT_ASSERT(device != RT_NULL); size = rt_device_write(device, -1, buf, count); return size; @@ -296,7 +296,7 @@ static int console_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req) struct rt_wqueue *wq; struct rt_lwp *lwp; - device = (struct rt_device *)fd->data; + device = (struct rt_device *)fd->fnode->data; RT_ASSERT(device != RT_NULL); console = (struct rt_console_device *)device; diff --git a/components/lwp/lwp_ipc.c b/components/lwp/lwp_ipc.c index 1d4e2d971b..c2e4d86845 100644 --- a/components/lwp/lwp_ipc.c +++ b/components/lwp/lwp_ipc.c @@ -846,7 +846,7 @@ static int channel_fops_poll(struct dfs_fd *file, struct rt_pollreq *req) int mask = POLLOUT; rt_channel_t ch; - ch = (rt_channel_t)file->data; + ch = (rt_channel_t)file->fnode->data; rt_poll_add(&(ch->reader_queue), req); if (ch->stat != RT_IPC_STAT_IDLE) { @@ -865,7 +865,7 @@ static int channel_fops_close(struct dfs_fd *file) rt_base_t level; level = rt_hw_interrupt_disable(); - ch = (rt_channel_t)file->data; + ch = (rt_channel_t)file->fnode->data; ch->ref--; if (ch->ref == 0) { @@ -912,17 +912,17 @@ int lwp_channel_open(int fdt_type, const char *name, int flags) d = lwp_fd_get(fdt_type, fd); - d->type = FT_USER; - d->path = NULL; + d->fnode->type = FT_USER; + d->fnode->path = NULL; - d->fops = &channel_fops; + d->fnode->fops = &channel_fops; - d->flags = O_RDWR; /* set flags as read and write */ - d->size = 0; + d->fnode->flags = O_RDWR; /* set flags as read and write */ + d->fnode->size = 0; d->pos = 0; /* set socket to the data of dfs_fd */ - d->data = (void *)ch; + d->fnode->data = (void *)ch; } else { @@ -942,7 +942,7 @@ static rt_channel_t fd_2_channel(int fdt_type, int fd) { rt_channel_t ch; - ch = (rt_channel_t)d->data; + ch = (rt_channel_t)d->fnode->data; if (ch) { return ch; diff --git a/components/lwp/lwp_syscall.c b/components/lwp/lwp_syscall.c index c87e8eb085..3d63279812 100644 --- a/components/lwp/lwp_syscall.c +++ b/components/lwp/lwp_syscall.c @@ -2339,7 +2339,7 @@ int sys_getdents(int fd, struct libc_dirent *dirp, size_t nbytes) rt_set_errno(ENOMEM); return -1; } - dfs_fd = dfs_fd_get(fd); + dfs_fd = fd_get(fd, 0); ret = dfs_file_getdents(dfs_fd, rtt_dirp, nbytes); if (ret) { diff --git a/components/net/sal_socket/dfs_net/dfs_net.c b/components/net/sal_socket/dfs_net/dfs_net.c index 5cae1d2e9d..20abd078e1 100644 --- a/components/net/sal_socket/dfs_net/dfs_net.c +++ b/components/net/sal_socket/dfs_net/dfs_net.c @@ -21,41 +21,41 @@ int dfs_net_getsocket(int fd) { int socket; - struct dfs_fd *_dfs_fd; + struct dfs_fd *_dfs_fd; - _dfs_fd = dfs_fd_get(fd); + _dfs_fd = fd_get(fd, 0); if (_dfs_fd == NULL) return -1; - if (_dfs_fd->type != FT_SOCKET) socket = -1; - else socket = (int)_dfs_fd->data; + if (_dfs_fd->fnode->type != FT_SOCKET) socket = -1; + else socket = (int)_dfs_fd->fnode->data; return socket; } static int dfs_net_ioctl(struct dfs_fd* file, int cmd, void* args) { - int socket = (int) file->data; + int socket = (int) file->fnode->data; return sal_ioctlsocket(socket, cmd, args); } static int dfs_net_read(struct dfs_fd* file, void *buf, size_t count) { - int socket = (int) file->data; + int socket = (int) file->fnode->data; return sal_recvfrom(socket, buf, count, 0, NULL, NULL); } static int dfs_net_write(struct dfs_fd *file, const void *buf, size_t count) { - int socket = (int) file->data; + int socket = (int) file->fnode->data; return sal_sendto(socket, buf, count, 0, NULL, 0); } static int dfs_net_close(struct dfs_fd* file) { - int socket = (int) file->data; + int socket = (int) file->fnode->data; return sal_closesocket(socket); } @@ -67,7 +67,7 @@ static int dfs_net_poll(struct dfs_fd *file, struct rt_pollreq *req) return sal_poll(file, req); } -const struct dfs_file_ops _net_fops = +const struct dfs_file_ops _net_fops = { NULL, /* open */ dfs_net_close, diff --git a/components/net/sal_socket/impl/af_inet_lwip.c b/components/net/sal_socket/impl/af_inet_lwip.c index 56df735ab9..29fb84e027 100644 --- a/components/net/sal_socket/impl/af_inet_lwip.c +++ b/components/net/sal_socket/impl/af_inet_lwip.c @@ -243,7 +243,7 @@ static int inet_poll(struct dfs_fd *file, struct rt_pollreq *req) struct lwip_sock *sock; struct sal_socket *sal_sock; - sal_sock = sal_get_socket((int) file->data); + sal_sock = sal_get_socket((int) file->fnode->data); if(!sal_sock) { return -1; diff --git a/components/net/sal_socket/socket/net_sockets.c b/components/net/sal_socket/socket/net_sockets.c index 1adde5413d..e9017b3403 100644 --- a/components/net/sal_socket/socket/net_sockets.c +++ b/components/net/sal_socket/socket/net_sockets.c @@ -37,21 +37,21 @@ int accept(int s, struct sockaddr *addr, socklen_t *addrlen) return -1; } - d = dfs_fd_get(fd); + d = fd_get(fd, 0); if(d) { /* this is a socket fd */ - d->type = FT_SOCKET; - d->path = NULL; + d->fnode->type = FT_SOCKET; + d->fnode->path = NULL; - d->fops = dfs_net_get_fops(); + d->fnode->fops = dfs_net_get_fops(); - d->flags = O_RDWR; /* set flags as read and write */ - d->size = 0; + d->fnode->flags = O_RDWR; /* set flags as read and write */ + d->fnode->size = 0; d->pos = 0; /* set socket to the data of dfs_fd */ - d->data = (void *) new_socket; + d->fnode->data = (void *) new_socket; return fd; } @@ -86,7 +86,7 @@ int shutdown(int s, int how) return -1; } - d = dfs_fd_get(s); + d = fd_get(s, 0); if (d == NULL) { rt_set_errno(-EBADF); @@ -204,29 +204,29 @@ int socket(int domain, int type, int protocol) return -1; } - d = dfs_fd_get(fd); + d = fd_get(fd, 0); /* create socket and then put it to the dfs_fd */ socket = sal_socket(domain, type, protocol); if (socket >= 0) { /* this is a socket fd */ - d->type = FT_SOCKET; - d->path = NULL; + d->fnode->type = FT_SOCKET; + d->fnode->path = NULL; - d->fops = dfs_net_get_fops(); + d->fnode->fops = dfs_net_get_fops(); - d->flags = O_RDWR; /* set flags as read and write */ - d->size = 0; + d->fnode->flags = O_RDWR; /* set flags as read and write */ + d->fnode->size = 0; d->pos = 0; /* set socket to the data of dfs_fd */ - d->data = (void *) socket; + d->fnode->data = (void *) socket; } else { /* release fd */ - dfs_fd_release(d); + fd_release(d); rt_set_errno(-ENOMEM); @@ -250,7 +250,7 @@ int closesocket(int s) return -1; } - d = dfs_fd_get(s); + d = fd_get(s, 0); if (d == RT_NULL) { rt_set_errno(-EBADF); @@ -268,7 +268,7 @@ int closesocket(int s) } /* socket has been closed, delete it from file system fd */ - dfs_fd_release(d); + fd_release(d); return error; } diff --git a/components/net/sal_socket/src/sal_socket.c b/components/net/sal_socket/src/sal_socket.c index ec0519df02..c57271ca71 100644 --- a/components/net/sal_socket/src/sal_socket.c +++ b/components/net/sal_socket/src/sal_socket.c @@ -1020,7 +1020,7 @@ int sal_poll(struct dfs_fd *file, struct rt_pollreq *req) { struct sal_socket *sock; struct sal_proto_family *pf; - int socket = (int) file->data; + int socket = (int) file->fnode->data; /* get the socket object by socket descriptor */ SAL_SOCKET_OBJ_GET(sock, socket); -- Gitee From 19329f17598791e741475e14ebce5b2aba1971d1 Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Wed, 3 Feb 2021 10:03:04 +0800 Subject: [PATCH 03/12] =?UTF-8?q?dfs=E4=BF=AE=E6=94=B9:=20fnode=E7=9A=84?= =?UTF-8?q?=E7=94=B3=E8=AF=B7=E9=87=8A=E6=94=BE=E6=94=B9=E7=94=B1dfs?= =?UTF-8?q?=E7=9A=84open=E5=92=8Cclose=E5=B1=82=E8=B4=9F=E8=B4=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/dfs/src/dfs.c | 15 +-------------- components/dfs/src/dfs_file.c | 18 ++++++++++++++++-- components/dfs/src/dfs_fs.c | 3 --- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/components/dfs/src/dfs.c b/components/dfs/src/dfs.c index 15a480d4ee..4a13dc22ce 100644 --- a/components/dfs/src/dfs.c +++ b/components/dfs/src/dfs.c @@ -184,23 +184,14 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd) /* allocate 'struct dfs_fd' */ if (idx < (int)fdt->maxfd && fdt->fds[idx] == RT_NULL) { - struct dfs_fnode *fnode = rt_calloc(1, sizeof(struct dfs_fnode)); - - if (!fnode) - { - return fdt->maxfd; - } - fnode->ref_count = 1; - fd = (struct dfs_fd *)rt_calloc(1, sizeof(struct dfs_fd)); if (!fd) { - rt_free(fnode); return fdt->maxfd; } fd->ref_count = 1; fd->magic = DFS_FD_MAGIC; - fd->fnode = fnode; + fd->fnode = NULL; fd->idx = idx; fdt->fds[idx] = fd; } @@ -321,10 +312,6 @@ void fdt_fd_release(struct dfs_fdtable* fdt, struct dfs_fd *fd) if (fnode) { fnode->ref_count--; - if (fnode->ref_count == 0) - { - rt_free(fnode); - } } rt_free(fd); fdt->fds[idx] = 0; diff --git a/components/dfs/src/dfs_file.c b/components/dfs/src/dfs_file.c index 90a61dd201..b94cc35be1 100644 --- a/components/dfs/src/dfs_file.c +++ b/components/dfs/src/dfs_file.c @@ -35,6 +35,7 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) struct dfs_filesystem *fs; char *fullpath; int result; + struct dfs_fnode *fnode = NULL; /* parameter check */ if (fd == NULL) @@ -58,6 +59,14 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) return -ENOENT; } + fnode = rt_calloc(1, sizeof(struct dfs_fnode)); + if (!fnode) + { + return -ENOMEM; + } + fnode->ref_count = 1; + fd->fnode = fnode; + LOG_D("open in filesystem:%s", fs->ops->name); fd->fnode->fs = fs; /* set file system */ fd->fnode->fops = fs->ops->fops; /* set file ops */ @@ -89,6 +98,8 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) /* clear fd */ rt_free(fd->fnode->path); fd->fnode->path = NULL; + rt_free(fd->fnode); + fd->fnode = NULL; return -ENOSYS; } @@ -98,6 +109,8 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) /* clear fd */ rt_free(fd->fnode->path); fd->fnode->path = NULL; + rt_free(fd->fnode); + fd->fnode = NULL; LOG_D("%s open failed", fullpath); @@ -138,6 +151,8 @@ int dfs_file_close(struct dfs_fd *fd) rt_free(fd->fnode->path); fd->fnode->path = NULL; + rt_free(fd->fnode); + fd->fnode = NULL; return result; } @@ -512,8 +527,7 @@ int dfs_file_ftruncate(struct dfs_fd *fd, off_t length) #ifdef RT_USING_FINSH #include -static struct dfs_fnode fnode; -static struct dfs_fd fd = {0, 0, 0, 0, &fnode}; +static struct dfs_fd fd; static struct dirent dirent; void ls(const char *pathname) { diff --git a/components/dfs/src/dfs_fs.c b/components/dfs/src/dfs_fs.c index d867de013a..da13c76e3c 100644 --- a/components/dfs/src/dfs_fs.c +++ b/components/dfs/src/dfs_fs.c @@ -272,9 +272,6 @@ int dfs_mount(const char *device_name, if ((strcmp(fullpath, "/") != 0) && (strcmp(fullpath, "/dev") != 0)) { struct dfs_fd fd; - struct dfs_fnode fnode; - - fd.fnode = &fnode; if (dfs_file_open(&fd, fullpath, O_RDONLY | O_DIRECTORY) < 0) { -- Gitee From bd0f6ba823e60c877e64c3ad69b2eebda6f3e137 Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Sun, 7 Feb 2021 11:26:34 +0800 Subject: [PATCH 04/12] =?UTF-8?q?=E5=8A=A0=E5=85=A5fnode=E6=9F=A5=E6=89=BE?= =?UTF-8?q?hash=E8=A1=A8=20=E5=AF=B9fd->data=E4=B8=8D=E5=86=8D=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E5=88=9D=E5=A7=8B=E4=B8=BAfs=E7=9A=84=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=20=E5=88=A0=E9=99=A4=E5=85=A8=E5=B1=80fd=E5=92=8Cdire?= =?UTF-8?q?nt=E5=8F=98=E9=87=8F=20=E6=AD=A4=E7=89=88=E6=9C=AC=E4=B8=ADfops?= =?UTF-8?q?=E7=9A=84open=E5=92=8Cclose=E6=97=B6=E5=BA=94=E5=88=A4=E6=96=AD?= =?UTF-8?q?dfs=5Ffd::fnode::ref=5Fcount,=E5=85=B6=E7=AD=89=E4=BA=8E1?= =?UTF-8?q?=E6=97=B6=E6=89=8D=E9=A1=BB=E8=BF=9B=E8=A1=8Cfnode=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=93=8D=E4=BD=9C=20console=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E9=80=82=E9=85=8D=E4=BF=AE=E6=94=B9=E7=9A=84=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/dfs/include/dfs.h | 3 +- components/dfs/include/dfs_file.h | 4 + components/dfs/src/dfs.c | 67 +----- components/dfs/src/dfs_file.c | 345 +++++++++++++++++++++++------- components/dfs/src/dfs_fs.c | 1 + components/lwp/lwp_console.c | 98 +++++---- 6 files changed, 338 insertions(+), 180 deletions(-) diff --git a/components/dfs/include/dfs.h b/components/dfs/include/dfs.h index 14b3995f37..df35a873f9 100644 --- a/components/dfs/include/dfs.h +++ b/components/dfs/include/dfs.h @@ -107,7 +107,8 @@ void fdt_fd_release(struct dfs_fdtable* fdt, struct dfs_fd *fd); int fd_new(void); struct dfs_fd *fd_get(int fd, int ref_inc_nr); void fd_release(struct dfs_fd *fd); -int fd_is_open(const char *pathname); + +void fd_init(struct dfs_fd *fd); struct dfs_fdtable *dfs_fdtable_get(void); struct dfs_fdtable *dfs_fdtable_get_global(void); diff --git a/components/dfs/include/dfs_file.h b/components/dfs/include/dfs_file.h index c77902654b..42c2207ae5 100644 --- a/components/dfs/include/dfs_file.h +++ b/components/dfs/include/dfs_file.h @@ -42,7 +42,9 @@ struct dfs_fnode uint16_t type; /* Type (regular or socket) */ char *path; /* Name (below mount point) */ + char *fullpath; /* Full path is hash key */ int ref_count; /* Descriptor reference count */ + rt_list_t list; /* The node of fnode hash table */ struct dfs_filesystem *fs; const struct dfs_file_ops *fops; @@ -62,6 +64,8 @@ struct dfs_fd void *data; /* Specific fd data */ }; +void dfs_fnode_mgr_init(void); +int dfs_file_is_open(const char *pathname); int dfs_file_open(struct dfs_fd *fd, const char *path, int flags); int dfs_file_close(struct dfs_fd *fd); int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args); diff --git a/components/dfs/src/dfs.c b/components/dfs/src/dfs.c index 4a13dc22ce..73f48c0e55 100644 --- a/components/dfs/src/dfs.c +++ b/components/dfs/src/dfs.c @@ -56,6 +56,9 @@ int dfs_init(void) return 0; } + /* init fnode hash table */ + dfs_fnode_mgr_init(); + /* clear filesystem operations table */ memset((void *)filesystem_operation_table, 0, sizeof(filesystem_operation_table)); /* clear filesystem table */ @@ -327,65 +330,17 @@ void fd_release(struct dfs_fd *fd) fdt_fd_release(fdt, fd); } -/** - * @ingroup Fd - * - * This function will return whether this file has been opend. - * - * @param pathname the file path name. - * - * @return 0 on file has been open successfully, -1 on open failed. - */ -int fd_is_open(const char *pathname) +void fd_init(struct dfs_fd *fd) { - char *fullpath; - unsigned int index; - struct dfs_filesystem *fs; - struct dfs_fd *fd; - struct dfs_fdtable *fdt; - - fdt = dfs_fdtable_get(); - fullpath = dfs_normalize_path(NULL, pathname); - if (fullpath != NULL) + if (fd) { - char *mountpath; - fs = dfs_filesystem_lookup(fullpath); - if (fs == NULL) - { - /* can't find mounted file system */ - rt_free(fullpath); - - 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); - - dfs_fd_lock(); - - for (index = 0; index < fdt->maxfd; index++) - { - fd = fdt->fds[index]; - if (fd == NULL || fd->fnode->fops == NULL || fd->fnode->path == NULL) continue; - - if (fd->fnode->fs == fs && strcmp(fd->fnode->path, mountpath) == 0) - { - /* found file in file descriptor table */ - rt_free(fullpath); - dfs_fd_unlock(); - - return 0; - } - } - dfs_fd_unlock(); - - rt_free(fullpath); + fd->idx = -1; + fd->magic = DFS_FD_MAGIC; + fd->ref_count = 1; + fd->pos = 0; + fd->fnode = NULL; + fd->data = NULL; } - - return -1; } /** diff --git a/components/dfs/src/dfs_file.c b/components/dfs/src/dfs_file.c index b94cc35be1..db299f55ca 100644 --- a/components/dfs/src/dfs_file.c +++ b/components/dfs/src/dfs_file.c @@ -15,12 +15,101 @@ #include #include +#define DFS_FNODE_HASH_NR 128 + +struct dfs_fnode_mgr +{ + struct rt_mutex lock; + rt_list_t head[DFS_FNODE_HASH_NR]; +}; + +static struct dfs_fnode_mgr dfs_fm; + +void dfs_fnode_mgr_init(void) +{ + int i = 0; + + rt_mutex_init(&dfs_fm.lock, "dfs_mgr", RT_IPC_FLAG_PRIO); + for (i = 0; i < DFS_FNODE_HASH_NR; i++) + { + rt_list_init(&dfs_fm.head[i]); + } +} + +/* BKDR Hash Function */ +static unsigned int bkdr_hash(const char *str) +{ + unsigned int seed = 131; // 31 131 1313 13131 131313 etc.. + unsigned int hash = 0; + + while (*str) + { + hash = hash * seed + (*str++); + } + + return (hash % DFS_FNODE_HASH_NR); +} + +static struct dfs_fnode *dfs_fnode_find(const char *path, rt_list_t **hash_head) +{ + struct dfs_fnode *fnode = NULL; + int hash = bkdr_hash(path); + rt_list_t *hh; + + hh = dfs_fm.head[hash].next; + + if (hash_head) + { + *hash_head = &dfs_fm.head[hash]; + } + + while (hh != &dfs_fm.head[hash]) + { + fnode = rt_container_of(hh, struct dfs_fnode, list); + if (rt_strcmp(path, fnode->fullpath) == 0) + { + /* found */ + return fnode; + } + hh = hh->next; + } + return NULL; +} + /** * @addtogroup FileApi */ /*@{*/ +/** + * This function will return whether this file has been opend. + * + * @param pathname the file path name. + * + * @return 0 on file has been open successfully, -1 on open failed. + */ +int dfs_file_is_open(const char *pathname) +{ + char *fullpath = NULL; + struct dfs_fnode *fnode = NULL; + int ret = 0; + + fullpath = dfs_normalize_path(NULL, pathname); + + rt_mutex_take(&dfs_fm.lock, RT_WAITING_FOREVER); + fnode = dfs_fnode_find(fullpath, NULL); + if (fnode) + { + ret = 1; + } + rt_mutex_release(&dfs_fm.lock); + + rt_free(fullpath); + return ret; +} + + /** * this function will open a file which specified by path with specified flags. * @@ -36,6 +125,7 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) char *fullpath; int result; struct dfs_fnode *fnode = NULL; + rt_list_t *hash_head; /* parameter check */ if (fd == NULL) @@ -50,79 +140,110 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) LOG_D("open file:%s", fullpath); - /* find filesystem */ - fs = dfs_filesystem_lookup(fullpath); - if (fs == NULL) + rt_mutex_take(&dfs_fm.lock, RT_WAITING_FOREVER); + /* fnode find */ + fnode = dfs_fnode_find(fullpath, &hash_head); + if (fnode) { - rt_free(fullpath); /* release path */ - - return -ENOENT; + fnode->ref_count++; + fd->pos = 0; + fd->fnode = fnode; + rt_mutex_release(&dfs_fm.lock); } - - fnode = rt_calloc(1, sizeof(struct dfs_fnode)); - if (!fnode) + else { - return -ENOMEM; - } - fnode->ref_count = 1; - fd->fnode = fnode; + /* find filesystem */ + fs = dfs_filesystem_lookup(fullpath); + if (fs == NULL) + { + rt_mutex_release(&dfs_fm.lock); + rt_free(fullpath); /* release path */ + return -ENOENT; + } + + fnode = rt_calloc(1, sizeof(struct dfs_fnode)); + if (!fnode) + { + rt_mutex_release(&dfs_fm.lock); + rt_free(fullpath); /* release path */ + return -ENOMEM; + } + fnode->ref_count = 1; - LOG_D("open in filesystem:%s", fs->ops->name); - fd->fnode->fs = fs; /* set file system */ - fd->fnode->fops = fs->ops->fops; /* set file ops */ + LOG_D("open in filesystem:%s", fs->ops->name); + fnode->fs = fs; /* set file system */ + fnode->fops = fs->ops->fops; /* set file ops */ - /* initialize the fd item */ - fd->fnode->type = FT_REGULAR; - fd->fnode->flags = flags; - fd->fnode->size = 0; - fd->pos = 0; - fd->fnode->data = fs; + /* initialize the fd item */ + fnode->type = FT_REGULAR; + fnode->flags = flags; - if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH)) - { - if (dfs_subdir(fs->path, fullpath) == NULL) - fd->fnode->path = rt_strdup("/"); + if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH)) + { + if (dfs_subdir(fs->path, fullpath) == NULL) + fnode->path = rt_strdup("/"); + else + fnode->path = rt_strdup(dfs_subdir(fs->path, fullpath)); + LOG_D("Actual file path: %s", fnode->path); + } else - fd->fnode->path = rt_strdup(dfs_subdir(fs->path, fullpath)); - rt_free(fullpath); - LOG_D("Actual file path: %s", fd->fnode->path); - } - else - { - fd->fnode->path = fullpath; - } + { + fnode->path = fullpath; + } + fnode->fullpath = fullpath; - /* specific file system open routine */ - if (fd->fnode->fops->open == NULL) - { - /* clear fd */ - rt_free(fd->fnode->path); - fd->fnode->path = NULL; - rt_free(fd->fnode); - fd->fnode = NULL; + /* specific file system open routine */ + if (fnode->fops->open == NULL) + { + rt_mutex_release(&dfs_fm.lock); + /* clear fd */ + if (fnode->path != fnode->fullpath) + { + rt_free(fnode->fullpath); + } + rt_free(fnode->path); + rt_free(fnode); - return -ENOSYS; + return -ENOSYS; + } + + fd->pos = 0; + fd->fnode = fnode; + + /* insert fnode to hash */ + rt_list_insert_after(hash_head, &fnode->list); } - if ((result = fd->fnode->fops->open(fd)) < 0) + if ((result = fnode->fops->open(fd)) < 0) { - /* clear fd */ - rt_free(fd->fnode->path); - fd->fnode->path = NULL; - rt_free(fd->fnode); - fd->fnode = NULL; + fnode->ref_count--; + if (fnode->ref_count == 0) + { + /* remove from hash */ + rt_list_remove(&fnode->list); + /* clear fd */ + if (fnode->path != fnode->fullpath) + { + rt_free(fnode->fullpath); + } + rt_free(fnode->path); + fd->fnode = NULL; + rt_free(fnode); + rt_mutex_release(&dfs_fm.lock); + } LOG_D("%s open failed", fullpath); return result; } - fd->fnode->flags |= DFS_F_OPEN; + fnode->flags |= DFS_F_OPEN; if (flags & O_DIRECTORY) { - fd->fnode->type = FT_DIRECTORY; - fd->fnode->flags |= DFS_F_DIRECTORY; + fnode->type = FT_DIRECTORY; + fnode->flags |= DFS_F_DIRECTORY; } + rt_mutex_release(&dfs_fm.lock); LOG_D("open successful"); return 0; @@ -137,22 +258,52 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) */ int dfs_file_close(struct dfs_fd *fd) { + struct dfs_fnode *fnode = NULL; int result = 0; if (fd == NULL) + { return -ENXIO; + } - if (fd->fnode->fops->close != NULL) - result = fd->fnode->fops->close(fd); + if (fd->ref_count == 1) + { + rt_mutex_take(&dfs_fm.lock, RT_WAITING_FOREVER); + fnode = fd->fnode; - /* close fd error, return */ - if (result < 0) - return result; + if (fnode->ref_count <= 0) + { + rt_mutex_release(&dfs_fm.lock); + return -ENXIO; + } + + if (fnode->fops->close != NULL) + { + result = fnode->fops->close(fd); + } - rt_free(fd->fnode->path); - fd->fnode->path = NULL; - rt_free(fd->fnode); - fd->fnode = NULL; + /* close fd error, return */ + if (result < 0) + { + rt_mutex_release(&dfs_fm.lock); + return result; + } + + if (fnode->ref_count == 1) + { + /* remove from hash */ + rt_list_remove(&fnode->list); + fd->fnode = NULL; + + if (fnode->path != fnode->fullpath) + { + rt_free(fnode->fullpath); + } + rt_free(fnode->path); + rt_free(fnode); + rt_mutex_release(&dfs_fm.lock); + } + } return result; } @@ -169,7 +320,9 @@ int dfs_file_close(struct dfs_fd *fd) int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args) { if (fd == NULL) + { return -EINVAL; + } /* regular file system fd */ if (fd->fnode->type == FT_REGULAR || fd->fnode->type == FT_DEVICE) @@ -192,7 +345,9 @@ int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args) } if (fd->fnode->fops->ioctl != NULL) + { return fd->fnode->fops->ioctl(fd, cmd, args); + } return -ENOSYS; } @@ -212,13 +367,19 @@ int dfs_file_read(struct dfs_fd *fd, void *buf, size_t len) int result = 0; if (fd == NULL) + { return -EINVAL; + } if (fd->fnode->fops->read == NULL) + { return -ENOSYS; + } if ((result = fd->fnode->fops->read(fd, buf, len)) < 0) + { fd->fnode->flags |= DFS_F_EOF; + } return result; } @@ -235,11 +396,20 @@ int dfs_file_read(struct dfs_fd *fd, void *buf, size_t len) int dfs_file_getdents(struct dfs_fd *fd, struct dirent *dirp, size_t nbytes) { /* parameter check */ - if (fd == NULL || fd->fnode->type != FT_DIRECTORY) + if (fd == NULL) + { return -EINVAL; + } + + if (fd->fnode->type != FT_DIRECTORY) + { + return -EINVAL; + } if (fd->fnode->fops->getdents != NULL) + { return fd->fnode->fops->getdents(fd, dirp, nbytes); + } return -ENOSYS; } @@ -264,17 +434,17 @@ int dfs_file_unlink(const char *path) return -EINVAL; } - /* get filesystem */ - if ((fs = dfs_filesystem_lookup(fullpath)) == NULL) + /* Check whether file is already open */ + if (dfs_file_is_open(fullpath)) { - result = -ENOENT; + result = -EBUSY; goto __exit; } - /* Check whether file is already open */ - if (fd_is_open(fullpath) == 0) + /* get filesystem */ + if ((fs = dfs_filesystem_lookup(fullpath)) == NULL) { - result = -EBUSY; + result = -ENOENT; goto __exit; } @@ -309,10 +479,14 @@ __exit: int dfs_file_write(struct dfs_fd *fd, const void *buf, size_t len) { if (fd == NULL) + { return -EINVAL; + } if (fd->fnode->fops->write == NULL) + { return -ENOSYS; + } return fd->fnode->fops->write(fd, buf, len); } @@ -440,11 +614,10 @@ 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 = RT_EOK; + struct dfs_filesystem *oldfs = NULL, *newfs = NULL; + char *oldfullpath = NULL, *newfullpath = NULL; - result = RT_EOK; newfullpath = NULL; oldfullpath = NULL; @@ -455,6 +628,12 @@ int dfs_file_rename(const char *oldpath, const char *newpath) goto __exit; } + if (dfs_file_is_open((const char *)oldfullpath)) + { + result = -EBUSY; + goto __exit; + } + newfullpath = dfs_normalize_path(NULL, newpath); if (newfullpath == NULL) { @@ -488,8 +667,14 @@ int dfs_file_rename(const char *oldpath, const char *newpath) } __exit: - rt_free(oldfullpath); - rt_free(newfullpath); + if (oldfullpath) + { + rt_free(oldfullpath); + } + if (newfullpath) + { + rt_free(newfullpath); + } /* not at same file system, return EXDEV */ return result; @@ -527,10 +712,10 @@ int dfs_file_ftruncate(struct dfs_fd *fd, off_t length) #ifdef RT_USING_FINSH #include -static struct dfs_fd fd; -static struct dirent dirent; void ls(const char *pathname) { + struct dfs_fd fd; + struct dirent dirent; struct stat stat; int length; char *fullpath, *path; @@ -552,6 +737,7 @@ void ls(const char *pathname) path = (char *)pathname; } + fd_init(&fd); /* list directory */ if (dfs_file_open(&fd, path, O_DIRECTORY) == 0) { @@ -610,9 +796,11 @@ FINSH_FUNCTION_EXPORT(rm, remove files or directories); void cat(const char *filename) { - uint32_t length; + struct dfs_fd fd; + uint32_t length = 0; char buffer[81]; + fd_init(&fd); if (dfs_file_open(&fd, filename, O_RDONLY) < 0) { rt_kprintf("Open %s failed\n", filename); @@ -638,6 +826,7 @@ FINSH_FUNCTION_EXPORT(cat, print file); #define BUF_SZ 4096 static void copyfile(const char *src, const char *dst) { + struct dfs_fd fd; struct dfs_fd src_fd; rt_uint8_t *block_ptr; rt_int32_t read_bytes; @@ -650,6 +839,7 @@ static void copyfile(const char *src, const char *dst) return; } + fd_init(&src_fd); if (dfs_file_open(&src_fd, src, O_RDONLY) < 0) { rt_free(block_ptr); @@ -657,6 +847,7 @@ static void copyfile(const char *src, const char *dst) return; } + fd_init(&fd); if (dfs_file_open(&fd, dst, O_WRONLY | O_CREAT) < 0) { rt_free(block_ptr); diff --git a/components/dfs/src/dfs_fs.c b/components/dfs/src/dfs_fs.c index da13c76e3c..6013cc55b0 100644 --- a/components/dfs/src/dfs_fs.c +++ b/components/dfs/src/dfs_fs.c @@ -273,6 +273,7 @@ int dfs_mount(const char *device_name, { struct dfs_fd fd; + fd_init(&fd); if (dfs_file_open(&fd, fullpath, O_RDONLY | O_DIRECTORY) < 0) { rt_free(fullpath); diff --git a/components/lwp/lwp_console.c b/components/lwp/lwp_console.c index 881e05dea9..ec3ca787a4 100644 --- a/components/lwp/lwp_console.c +++ b/components/lwp/lwp_console.c @@ -45,8 +45,8 @@ rt_inline struct rt_wqueue *wait_queue_current_get(void) static void console_wakeup_check(struct rt_console_device *console) { - rt_size_t len; - struct rt_wqueue *wq; + rt_size_t len = 0; + struct rt_wqueue *wq = NULL; len = rt_ringbuffer_data_len(&console->input_rb); if (len) @@ -58,9 +58,9 @@ static void console_wakeup_check(struct rt_console_device *console) static void console_rx_notify(struct rt_device *dev) { - struct rt_console_device *console; - rt_size_t len; - rt_uint8_t ch; + struct rt_console_device *console = NULL; + rt_size_t len = 0; + rt_uint8_t ch = 0; console = (struct rt_console_device *)dev; RT_ASSERT(console != RT_NULL); @@ -95,7 +95,7 @@ static void console_rx_notify(struct rt_device *dev) void rt_console_set_foreground(struct rt_lwp *lwp) { - rt_base_t level; + rt_base_t level = 0; level = rt_hw_interrupt_disable(); if (_console.init_flag != CONSOLE_INIT_FLAG_INITED) @@ -111,8 +111,8 @@ exit: struct rt_lwp * rt_console_get_foreground(void) { - struct rt_lwp *lwp; - rt_base_t level; + struct rt_lwp *lwp = RT_NULL; + rt_base_t level = 0; level = rt_hw_interrupt_disable(); lwp = _console.foreground; @@ -135,9 +135,9 @@ static void iodev_close(struct rt_console_device *console) static rt_err_t iodev_open(struct rt_console_device *console) { - rt_err_t ret; + rt_err_t ret = RT_EOK; struct rt_device_notify rx_notify; - rt_uint16_t oflags; + rt_uint16_t oflags = 0; rt_device_control(console->iodev, RT_DEVICE_CTRL_CONSOLE_OFLAG, &oflags); @@ -155,8 +155,8 @@ static rt_err_t iodev_open(struct rt_console_device *console) struct rt_device *rt_console_get_iodev(void) { - rt_base_t level; - struct rt_device *iodev; + rt_base_t level = 0; + struct rt_device *iodev = RT_NULL; level = rt_hw_interrupt_disable(); iodev = _console.iodev; @@ -166,9 +166,9 @@ struct rt_device *rt_console_get_iodev(void) struct rt_device *rt_console_set_iodev(struct rt_device *iodev) { - rt_base_t level; - struct rt_device *io_before; - struct rt_console_device *console; + rt_base_t level = 0; + struct rt_device *io_before = RT_NULL; + struct rt_console_device *console = RT_NULL; RT_ASSERT(iodev != RT_NULL); @@ -211,36 +211,42 @@ exit: /* fops for console */ static int console_fops_open(struct dfs_fd *fd) { - int ret; - struct rt_device * device; + int ret = 0; + struct rt_device *device = RT_NULL; device = (struct rt_device *)fd->fnode->data; RT_ASSERT(device != RT_NULL); - ret = rt_device_open(device, fd->fnode->flags); + if (fd->fnode->ref_count == 1) + { + ret = rt_device_open(device, fd->fnode->flags); + } return ret; } static int console_fops_close(struct dfs_fd *fd) { - int ret; - struct rt_device * device; + int ret = 0; + struct rt_device *device = RT_NULL; device = (struct rt_device *)fd->fnode->data; RT_ASSERT(device != RT_NULL); - ret = rt_device_close(device); + if (fd->fnode->ref_count == 1) + { + ret = rt_device_close(device); + } return ret; } static int console_fops_read(struct dfs_fd *fd, void *buf, size_t count) { - rt_base_t level; + rt_base_t level = 0; int size = 0; - struct rt_console_device *console; - struct rt_lwp *lwp; - struct rt_wqueue *wq; - int wait_ret; + struct rt_console_device *console = RT_NULL; + struct rt_lwp *lwp = RT_NULL; + struct rt_wqueue *wq = RT_NULL; + int wait_ret = 0; console = (struct rt_console_device *)fd->fnode->data; RT_ASSERT(console != RT_NULL); @@ -278,8 +284,8 @@ static int console_fops_read(struct dfs_fd *fd, void *buf, size_t count) static int console_fops_write(struct dfs_fd *fd, const void *buf, size_t count) { - int size; - struct rt_device * device; + int size = 0; + struct rt_device *device = RT_NULL; device = (struct rt_device *)fd->fnode->data; RT_ASSERT(device != RT_NULL); @@ -289,12 +295,12 @@ static int console_fops_write(struct dfs_fd *fd, const void *buf, size_t count) static int console_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req) { - rt_base_t level; + rt_base_t level = 0; int mask = POLLOUT; - struct rt_device * device; - struct rt_console_device *console; - struct rt_wqueue *wq; - struct rt_lwp *lwp; + struct rt_device *device = RT_NULL; + struct rt_console_device *console = RT_NULL; + struct rt_wqueue *wq = RT_NULL; + struct rt_lwp *lwp = RT_NULL; device = (struct rt_device *)fd->fnode->data; RT_ASSERT(device != RT_NULL); @@ -343,9 +349,9 @@ const static struct dfs_file_ops _console_fops = */ static rt_err_t rt_console_init(struct rt_device *dev) { - rt_base_t level; - rt_err_t result; - struct rt_console_device *console; + rt_base_t level = 0; + rt_err_t result = RT_EOK; + struct rt_console_device *console = RT_NULL; RT_ASSERT(dev != RT_NULL); @@ -370,7 +376,7 @@ exit: static rt_err_t rt_console_open(struct rt_device *dev, rt_uint16_t oflag) { rt_err_t result = RT_EOK; - struct rt_console_device *console; + struct rt_console_device *console = RT_NULL; RT_ASSERT(dev != RT_NULL); console = (struct rt_console_device *)dev; @@ -382,7 +388,7 @@ static rt_err_t rt_console_open(struct rt_device *dev, rt_uint16_t oflag) static rt_err_t rt_console_close(struct rt_device *dev) { rt_err_t result = RT_EOK; - struct rt_console_device *console; + struct rt_console_device *console = RT_NULL; console = (struct rt_console_device *)dev; RT_ASSERT(console != RT_NULL); @@ -395,10 +401,10 @@ static rt_size_t rt_console_read(struct rt_device *dev, void *buffer, rt_size_t size) { - rt_base_t level; + rt_base_t level = 0; rt_size_t len = 0; - struct rt_lwp *lwp; - struct rt_console_device *console; + struct rt_lwp *lwp = RT_NULL; + struct rt_console_device *console = RT_NULL; console = (struct rt_console_device *)dev; RT_ASSERT(console != RT_NULL); @@ -431,9 +437,9 @@ static rt_size_t rt_console_write(struct rt_device *dev, const void *buffer, rt_size_t size) { - rt_base_t level; + rt_base_t level = 0; rt_size_t len = 0; - struct rt_console_device *console; + struct rt_console_device *console = RT_NULL; console = (struct rt_console_device *)dev; RT_ASSERT(console != RT_NULL); @@ -463,9 +469,9 @@ const static struct rt_device_ops console_ops = */ rt_err_t rt_console_register(const char *name, struct rt_device *iodev) { - rt_base_t level; - rt_err_t ret; - struct rt_device *device; + rt_base_t level = 0; + rt_err_t ret = RT_EOK; + struct rt_device *device = RT_NULL; struct rt_console_device *console = &_console; level = rt_hw_interrupt_disable(); -- Gitee From 63c39dd3367ba6382900441c58d1112251ed49a4 Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Sun, 7 Feb 2021 14:03:25 +0800 Subject: [PATCH 05/12] =?UTF-8?q?cromfs=E4=BF=AE=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E9=80=82=E9=85=8D=E4=BF=AE=E6=94=B9=E7=9A=84=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dfs/filesystems/cromfs/dfs_cromfs.c | 314 +++++++++++++----- 1 file changed, 227 insertions(+), 87 deletions(-) diff --git a/components/dfs/filesystems/cromfs/dfs_cromfs.c b/components/dfs/filesystems/cromfs/dfs_cromfs.c index 5c942951b9..9ca472995b 100644 --- a/components/dfs/filesystems/cromfs/dfs_cromfs.c +++ b/components/dfs/filesystems/cromfs/dfs_cromfs.c @@ -123,18 +123,18 @@ struct cromfs_avl_struct file_info *fi; }; -static void cromfs_avl_remove(struct cromfs_avl_struct * node_to_delete, struct cromfs_avl_struct ** ptree); -static void cromfs_avl_insert(struct cromfs_avl_struct * new_node, struct cromfs_avl_struct ** ptree); -static struct cromfs_avl_struct* cromfs_avl_find(avl_key_t key, struct cromfs_avl_struct* ptree); +static void cromfs_avl_remove(struct cromfs_avl_struct *node_to_delete, struct cromfs_avl_struct **ptree); +static void cromfs_avl_insert(struct cromfs_avl_struct *new_node, struct cromfs_avl_struct **ptree); +static struct cromfs_avl_struct* cromfs_avl_find(avl_key_t key, struct cromfs_avl_struct *ptree); -static void cromfs_avl_rebalance(struct cromfs_avl_struct *** nodeplaces_ptr, int count) +static void cromfs_avl_rebalance(struct cromfs_avl_struct ***nodeplaces_ptr, int count) { - for ( ; count > 0 ; count--) + for ( ;count > 0; count--) { - struct cromfs_avl_struct ** nodeplace = *--nodeplaces_ptr; - struct cromfs_avl_struct * node = *nodeplace; - struct cromfs_avl_struct * nodeleft = node->avl_left; - struct cromfs_avl_struct * noderight = node->avl_right; + struct cromfs_avl_struct **nodeplace = *--nodeplaces_ptr; + struct cromfs_avl_struct *node = *nodeplace; + struct cromfs_avl_struct *nodeleft = node->avl_left; + struct cromfs_avl_struct *noderight = node->avl_right; int heightleft = heightof(nodeleft); int heightright = heightof(noderight); if (heightright + 1 < heightleft) @@ -161,8 +161,8 @@ static void cromfs_avl_rebalance(struct cromfs_avl_struct *** nodeplaces_ptr, in } else if (heightleft + 1 < heightright) { - struct cromfs_avl_struct * noderightright = noderight->avl_right; - struct cromfs_avl_struct * noderightleft = noderight->avl_left; + struct cromfs_avl_struct *noderightright = noderight->avl_right; + struct cromfs_avl_struct *noderightleft = noderight->avl_left; int heightrightleft = heightof(noderightleft); if (heightof(noderightright) >= heightrightleft) { @@ -184,32 +184,42 @@ static void cromfs_avl_rebalance(struct cromfs_avl_struct *** nodeplaces_ptr, in else { int height = (heightleftavl_height) + { break; + } node->avl_height = height; } } } -static void cromfs_avl_remove(struct cromfs_avl_struct * node_to_delete, struct cromfs_avl_struct ** ptree) +static void cromfs_avl_remove(struct cromfs_avl_struct *node_to_delete, struct cromfs_avl_struct **ptree) { avl_key_t key = node_to_delete->avl_key; - struct cromfs_avl_struct ** nodeplace = ptree; - struct cromfs_avl_struct ** stack[avl_maxheight]; + struct cromfs_avl_struct **nodeplace = ptree; + struct cromfs_avl_struct **stack[avl_maxheight]; uint32_t stack_count = 0; - struct cromfs_avl_struct *** stack_ptr = &stack[0]; /* = &stack[stackcount] */ - struct cromfs_avl_struct ** nodeplace_to_delete; + struct cromfs_avl_struct ***stack_ptr = &stack[0]; /* = &stack[stackcount] */ + struct cromfs_avl_struct **nodeplace_to_delete; for (;;) { - struct cromfs_avl_struct * node = *nodeplace; + struct cromfs_avl_struct *node = *nodeplace; if (node == AVL_EMPTY) + { return; + } *stack_ptr++ = nodeplace; stack_count++; if (key == node->avl_key) + { break; + } if (key < node->avl_key) + { nodeplace = &node->avl_left; + } else + { nodeplace = &node->avl_right; + } } nodeplace_to_delete = nodeplace; if (node_to_delete->avl_left == AVL_EMPTY) @@ -226,7 +236,9 @@ static void cromfs_avl_remove(struct cromfs_avl_struct * node_to_delete, struct { node = *nodeplace; if (node->avl_right == AVL_EMPTY) + { break; + } *stack_ptr++ = nodeplace; stack_count++; nodeplace = &node->avl_right; } @@ -240,23 +252,29 @@ static void cromfs_avl_remove(struct cromfs_avl_struct * node_to_delete, struct cromfs_avl_rebalance(stack_ptr,stack_count); } -static void cromfs_avl_insert(struct cromfs_avl_struct * new_node, struct cromfs_avl_struct ** ptree) +static void cromfs_avl_insert(struct cromfs_avl_struct *new_node, struct cromfs_avl_struct **ptree) { avl_key_t key = new_node->avl_key; - struct cromfs_avl_struct ** nodeplace = ptree; - struct cromfs_avl_struct ** stack[avl_maxheight]; + struct cromfs_avl_struct **nodeplace = ptree; + struct cromfs_avl_struct **stack[avl_maxheight]; int stack_count = 0; - struct cromfs_avl_struct *** stack_ptr = &stack[0]; /* = &stack[stackcount] */ + struct cromfs_avl_struct ***stack_ptr = &stack[0]; /* = &stack[stackcount] */ for (;;) { struct cromfs_avl_struct * node = *nodeplace; if (node == AVL_EMPTY) + { break; + } *stack_ptr++ = nodeplace; stack_count++; if (key < node->avl_key) + { nodeplace = &node->avl_left; + } else + { nodeplace = &node->avl_right; + } } new_node->avl_left = AVL_EMPTY; new_node->avl_right = AVL_EMPTY; @@ -270,13 +288,21 @@ static struct cromfs_avl_struct* cromfs_avl_find(avl_key_t key, struct cromfs_av for (;;) { if (ptree == AVL_EMPTY) + { return (struct cromfs_avl_struct *)0; + } if (key == ptree->avl_key) + { break; + } if (key < ptree->avl_key) + { ptree = ptree->avl_left; + } else + { ptree = ptree->avl_right; + } } return ptree; } @@ -286,37 +312,47 @@ static struct cromfs_avl_struct* cromfs_avl_find(avl_key_t key, struct cromfs_av static uint32_t cromfs_read_bytes(cromfs_info *ci, uint32_t pos, void *buf, uint32_t size) { if (pos >= ci->partition_size || pos + size > ci->partition_size) + { return 0; + } return ci->read_bytes(ci, pos, buf, size); } static uint32_t cromfs_noblk_read_bytes(cromfs_info *ci, uint32_t pos, void *buf, uint32_t size) { - uint32_t ret; + uint32_t ret = 0; ret = rt_device_read(ci->device, pos, buf, size); if (ret != size) + { return 0; + } else + { return ret; + } } static uint32_t cromfs_blk_read_bytes(cromfs_info *ci, uint32_t pos, void *buf, uint32_t size) { uint32_t ret = 0; uint32_t size_bak = size; - uint32_t start_blk; - uint32_t end_blk; - uint32_t off_s; - uint32_t sector_nr; - uint8_t *block_buff; - uint32_t ret_len; + uint32_t start_blk = 0; + uint32_t end_blk = 0; + uint32_t off_s = 0; + uint32_t sector_nr = 0; + uint8_t *block_buff = NULL; + uint32_t ret_len = 0; if (!size || !buf) + { return 0; + } block_buff = (uint8_t *)malloc(2 * ci->bytes_per_sector); if (!block_buff) + { return 0; + } start_blk = pos / ci->bytes_per_sector; off_s = pos % ci->bytes_per_sector; end_blk = (pos + size - 1) / ci->bytes_per_sector; @@ -326,14 +362,18 @@ static uint32_t cromfs_blk_read_bytes(cromfs_info *ci, uint32_t pos, void *buf, { ret_len = rt_device_read(ci->device, start_blk, block_buff, sector_nr + 1); if (ret_len != sector_nr + 1) + { goto end; + } memcpy(buf, block_buff + off_s, size); } else { ret_len = rt_device_read(ci->device, start_blk, block_buff, 1); if (ret_len != 1) + { goto end; + } memcpy(buf, block_buff + off_s, ci->bytes_per_sector - off_s); off_s = (ci->bytes_per_sector - off_s); size -= off_s; @@ -343,14 +383,18 @@ static uint32_t cromfs_blk_read_bytes(cromfs_info *ci, uint32_t pos, void *buf, { ret_len = rt_device_read(ci->device, start_blk, (char*)buf + off_s, sector_nr); if (ret_len != sector_nr) + { goto end; + } start_blk += sector_nr; off_s += (sector_nr * ci->bytes_per_sector); size -= (sector_nr * ci->bytes_per_sector); } ret_len = rt_device_read(ci->device, start_blk, block_buff, 1); if (ret_len != 1) + { goto end; + } memcpy((char*)buf + off_s, block_buff, size); } ret = size_bak; @@ -363,9 +407,9 @@ end: static uint8_t *cromfs_dirent_cache_get(cromfs_info *ci, uint32_t pos, uint32_t size) { - rt_list_t *l; - cromfs_dirent_cache *dir; - uint32_t len; + rt_list_t *l = NULL; + cromfs_dirent_cache *dir = NULL; + uint32_t len = 0; /* find */ for (l = ci->cromfs_dirent_cache_head.next; l != &ci->cromfs_dirent_cache_head; l = l->next) @@ -391,19 +435,21 @@ static uint8_t *cromfs_dirent_cache_get(cromfs_info *ci, uint32_t pos, uint32_t } dir = (cromfs_dirent_cache *)malloc(sizeof *dir); if (!dir) - return RT_NULL; + { + return NULL; + } dir->buff = (uint8_t *)malloc(size); if (!dir->buff) { free(dir); - return RT_NULL; + return NULL; } len = cromfs_read_bytes(ci, pos, dir->buff, size); if (len != size) { free(dir->buff); free(dir); - return RT_NULL; + return NULL; } rt_list_insert_after(&ci->cromfs_dirent_cache_head, (rt_list_t *)dir); ci->cromfs_dirent_cache_nr++; @@ -414,8 +460,8 @@ static uint8_t *cromfs_dirent_cache_get(cromfs_info *ci, uint32_t pos, uint32_t static void cromfs_dirent_cache_destroy(cromfs_info *ci) { - rt_list_t *l; - cromfs_dirent_cache *dir; + rt_list_t *l = NULL; + cromfs_dirent_cache *dir = NULL; while ((l = ci->cromfs_dirent_cache_head.next) != &ci->cromfs_dirent_cache_head) { @@ -432,12 +478,14 @@ static void cromfs_dirent_cache_destroy(cromfs_info *ci) static int dfs_cromfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data) { struct rt_device_blk_geometry geometry; - uint32_t len; + uint32_t len = 0; cromfs_info *ci = NULL; ci = (cromfs_info *)malloc(sizeof *ci); if (!ci) + { return -ENOMEM; + } memset(ci, 0, sizeof *ci); ci->device = fs->dev_id; @@ -474,8 +522,8 @@ static int dfs_cromfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, con static int dfs_cromfs_unmount(struct dfs_filesystem *fs) { - rt_err_t result; - cromfs_info *ci; + rt_err_t result = RT_EOK; + cromfs_info *ci = NULL; ci = (cromfs_info *)fs->data; @@ -490,14 +538,16 @@ static int dfs_cromfs_unmount(struct dfs_filesystem *fs) while (ci->cromfs_avl_root) { struct cromfs_avl_struct *node; - file_info *fi; + file_info *fi = NULL; node = ci->cromfs_avl_root; fi = node->fi; cromfs_avl_remove(node, &ci->cromfs_avl_root); free(node); if (fi->buff) + { free(fi->buff); + } free(fi); } @@ -515,13 +565,15 @@ static int dfs_cromfs_ioctl(struct dfs_fd *file, int cmd, void *args) static uint32_t cromfs_lookup(cromfs_info *ci, const char *path, int* is_dir, uint32_t *size, uint32_t *osize) { - uint32_t cur_size, cur_pos, cur_osize; - const char *subpath, *subpath_end; - void *di_mem; - int isdir; + uint32_t cur_size = 0, cur_pos = 0, cur_osize = 0; + const char *subpath = NULL, *subpath_end = NULL; + void *di_mem = NULL; + int isdir = 0; if (path[0] == '\0') + { return CROMFS_POS_ERROR; + } cur_size = ci->part_info.root_dir_size; cur_osize = 0; @@ -531,33 +583,43 @@ static uint32_t cromfs_lookup(cromfs_info *ci, const char *path, int* is_dir, ui subpath_end = path; while (1) { - cromfs_dirent_item *di_iter; - int found; + cromfs_dirent_item *di_iter = NULL; + int found = 0; /* skip /// */ while (*subpath_end && *subpath_end == '/') + { subpath_end ++; + } subpath = subpath_end; while ((*subpath_end != '/') && *subpath_end) + { subpath_end ++; + } if (*subpath == '\0') + { break; + } /* if not dir or empty dir, error */ if (!isdir || !cur_size) + { return CROMFS_POS_ERROR; + } /* find subpath */ di_mem = cromfs_dirent_cache_get(ci, cur_pos, cur_size); if (!di_mem) + { return CROMFS_POS_ERROR; + } found = 0; di_iter = (cromfs_dirent_item *)di_mem; while (1) { uint32_t name_len = subpath_end - subpath; - uint32_t name_block; + uint32_t name_block = 0; if (di_iter->dirent.name_size == name_len) { @@ -568,19 +630,27 @@ static uint32_t cromfs_lookup(cromfs_info *ci, const char *path, int* is_dir, ui cur_osize = di_iter->dirent.file_origin_size; cur_pos = di_iter->dirent.parition_pos; if (di_iter->dirent.attr == CROMFS_DIRENT_ATTR_DIR) + { isdir = 1; + } else + { isdir = 0; + } break; } } name_block = (di_iter->dirent.name_size + CROMFS_ALIGN_SIZE_MASK) >> CROMFS_ALIGN_SIZE_BIT; di_iter += (1 + name_block); if ((uint32_t)di_iter - (uint32_t)di_mem >= cur_size) + { break; + } } if (!found) + { return CROMFS_POS_ERROR; + } } *size = cur_size; *osize = cur_osize; @@ -590,12 +660,14 @@ static uint32_t cromfs_lookup(cromfs_info *ci, const char *path, int* is_dir, ui static uint32_t dfs_cromfs_lookup(cromfs_info *ci, const char *path, int* is_dir, uint32_t *size, uint32_t *osize) { - rt_err_t result; - uint32_t ret; + rt_err_t result = RT_EOK; + uint32_t ret = 0; result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER); if (result != RT_EOK) + { return CROMFS_POS_ERROR; + } ret = cromfs_lookup(ci, path, is_dir, size, osize); rt_mutex_release(&ci->lock); return ret; @@ -604,13 +676,13 @@ static uint32_t dfs_cromfs_lookup(cromfs_info *ci, const char *path, int* is_dir static int fill_file_data(file_info *fi) { int ret = -1; - cromfs_info *ci; + cromfs_info *ci = NULL; void *compressed_file_buff = NULL; - uint32_t size, osize; + uint32_t size = 0, osize = 0; if (!fi->data_valid) { - RT_ASSERT(fi->buff != RT_NULL); + RT_ASSERT(fi->buff != NULL); ci = fi->ci; osize = fi->size; @@ -618,36 +690,48 @@ static int fill_file_data(file_info *fi) compressed_file_buff = (void *)malloc(size); if (!compressed_file_buff) + { goto end; + } if (cromfs_read_bytes(ci, fi->partition_pos, compressed_file_buff, size) != size) + { goto end; + } if (uncompress((uint8_t *)fi->buff, (uLongf *)&osize, (uint8_t *)compressed_file_buff, size) != Z_OK) + { goto end; + } fi->data_valid = 1; } ret = 0; end: if (compressed_file_buff) + { free(compressed_file_buff); + } return ret; } static int dfs_cromfs_read(struct dfs_fd *file, void *buf, size_t count) { - rt_err_t result; - struct dfs_filesystem *fs; - file_info *fi; - cromfs_info *ci; - uint32_t length; + rt_err_t result = RT_EOK; + struct dfs_filesystem *fs = NULL; + file_info *fi = NULL; + cromfs_info *ci = NULL; + uint32_t length = 0; fs = (struct dfs_filesystem *)file->fnode->fs; ci = (cromfs_info *)fs->data; fi = (file_info *)file->fnode->data; if (count < file->fnode->size - file->pos) + { length = count; + } else + { length = file->fnode->size - file->pos; + } if (length > 0) { @@ -655,31 +739,41 @@ static int dfs_cromfs_read(struct dfs_fd *file, void *buf, size_t count) if (fi->buff) { - int fill_ret; + int fill_ret = 0; result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER); if (result != RT_EOK) + { return 0; + } fill_ret = fill_file_data(fi); rt_mutex_release(&ci->lock); if (fill_ret < 0) + { return 0; + } memcpy(buf, fi->buff + file->pos, length); } else { - void *di_mem; + void *di_mem = NULL; result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER); if (result != RT_EOK) + { return 0; + } di_mem = cromfs_dirent_cache_get(ci, fi->partition_pos, fi->size); if (di_mem) + { memcpy(buf, (char*)di_mem + file->pos, length); + } rt_mutex_release(&ci->lock); if (!di_mem) + { return 0; + } } /* update file current position */ file->pos += length; @@ -695,7 +789,6 @@ static int dfs_cromfs_lseek(struct dfs_fd *file, off_t offset) file->pos = offset; return file->pos; } - return -EIO; } @@ -706,7 +799,9 @@ static file_info *get_file_info(cromfs_info *ci, uint32_t partition_pos, int inc if (node) { if (inc_ref) + { node->fi->ref++; + } return node->fi; } return NULL; @@ -716,11 +811,13 @@ static file_info *inset_file_info(cromfs_info *ci, uint32_t partition_pos, int i { file_info *fi = NULL; void *file_buff = NULL; - struct cromfs_avl_struct* node = NULL; + struct cromfs_avl_struct *node = NULL; fi = (file_info *)malloc(sizeof *fi); if (!fi) + { goto err; + } fi->partition_pos = partition_pos; fi->ci = ci; if (is_dir) @@ -736,7 +833,9 @@ static file_info *inset_file_info(cromfs_info *ci, uint32_t partition_pos, int i { file_buff = (void *)malloc(osize); if (!file_buff) + { goto err; + } } } fi->buff = file_buff; @@ -744,23 +843,29 @@ static file_info *inset_file_info(cromfs_info *ci, uint32_t partition_pos, int i node = (struct cromfs_avl_struct *)malloc(sizeof *node); if (!node) + { goto err; + } node->avl_key = partition_pos; node->fi = fi; cromfs_avl_insert(node, &ci->cromfs_avl_root); return fi; err: if (file_buff) + { free(file_buff); + } if (fi) + { free(fi); + } return NULL; } static void deref_file_info(cromfs_info *ci, uint32_t partition_pos) { struct cromfs_avl_struct* node = cromfs_avl_find(partition_pos, ci->cromfs_avl_root); - file_info *fi; + file_info *fi = NULL; if (node) { @@ -771,7 +876,9 @@ static void deref_file_info(cromfs_info *ci, uint32_t partition_pos) cromfs_avl_remove(node, &ci->cromfs_avl_root); free(node); if (fi->buff) + { free(fi->buff); + } free(fi); } } @@ -779,10 +886,16 @@ static void deref_file_info(cromfs_info *ci, uint32_t partition_pos) static int dfs_cromfs_close(struct dfs_fd *file) { - file_info *fi; - struct dfs_filesystem *fs; - cromfs_info *ci; - rt_err_t result; + file_info *fi = NULL; + struct dfs_filesystem *fs = NULL; + cromfs_info *ci = NULL; + rt_err_t result = 0; + + RT_ASSERT(file->fnode->ref_count > 0); + if (file->fnode->ref_count > 1) + { + return 0; + } fi = (file_info *)file->fnode->data; fs = (struct dfs_filesystem *)file->fnode->fs; @@ -790,7 +903,9 @@ static int dfs_cromfs_close(struct dfs_fd *file) result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER); if (result != RT_EOK) + { return -RT_ERROR; + } deref_file_info(ci, fi->partition_pos); rt_mutex_release(&ci->lock); file->fnode->data = NULL; @@ -799,17 +914,26 @@ static int dfs_cromfs_close(struct dfs_fd *file) static int dfs_cromfs_open(struct dfs_fd *file) { - int ret; - struct dfs_filesystem *fs; - file_info *fi; - cromfs_info *ci; - uint32_t file_pos; - uint32_t size, osize; - int is_dir; - rt_err_t result; + int ret = 0; + struct dfs_filesystem *fs = NULL; + file_info *fi = NULL; + cromfs_info *ci = NULL; + uint32_t file_pos = 0; + uint32_t size = 0, osize = 0; + int is_dir = 0; + rt_err_t result = RT_EOK; if (file->fnode->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR)) + { return -EINVAL; + } + + RT_ASSERT(file->fnode->ref_count > 0); + if (file->fnode->ref_count > 1) + { + file->pos = 0; + return 0; + } fs = file->fnode->fs; ci = (cromfs_info *)fs->data; @@ -861,9 +985,13 @@ static int dfs_cromfs_open(struct dfs_fd *file) file->fnode->data = fi; if (is_dir) + { file->fnode->size = size; + } else + { file->fnode->size = osize; + } file->pos = 0; ret = RT_EOK; @@ -873,16 +1001,18 @@ end: static int dfs_cromfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st) { - uint32_t size, osize; - int is_dir; - cromfs_info *ci; - uint32_t file_pos; + uint32_t size = 0, osize = 0; + int is_dir = 0; + cromfs_info *ci = NULL; + uint32_t file_pos = 0; ci = (cromfs_info *)fs->data; file_pos = dfs_cromfs_lookup(ci, path, &is_dir, &size, &osize); if (file_pos == CROMFS_POS_ERROR) + { return -ENOENT; + } st->st_dev = 0; st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH | @@ -906,14 +1036,14 @@ static int dfs_cromfs_stat(struct dfs_filesystem *fs, const char *path, struct s static int dfs_cromfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count) { - uint32_t index; - uint8_t *name; - struct dirent *d; - file_info *fi; - cromfs_info *ci; - cromfs_dirent_item *dirent, *sub_dirent; - void *di_mem; - rt_err_t result; + uint32_t index = 0; + uint8_t *name = NULL; + struct dirent *d = NULL; + file_info *fi = NULL; + cromfs_info *ci = NULL; + cromfs_dirent_item *dirent = NULL, *sub_dirent = NULL; + void *di_mem = NULL; + rt_err_t result = RT_EOK; fi = (file_info *)file->fnode->data; ci = fi->ci; @@ -921,11 +1051,15 @@ static int dfs_cromfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_ RT_ASSERT(fi->buff == NULL); if (!fi->size) + { return -EINVAL; + } dirent = (cromfs_dirent_item *)malloc(fi->size); if (!dirent) + { return -ENOMEM; + } result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER); if (result != RT_EOK) @@ -935,7 +1069,9 @@ static int dfs_cromfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_ } di_mem = cromfs_dirent_cache_get(ci, fi->partition_pos, fi->size); if (di_mem) + { memcpy(dirent, di_mem, fi->size); + } rt_mutex_release(&ci->lock); if (!di_mem) { @@ -953,7 +1089,7 @@ static int dfs_cromfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_ for (index = 0; index < count && file->pos < file->fnode->size; index ++) { - uint32_t name_size; + uint32_t name_size = 0; d = dirp + index; sub_dirent = &dirent[file->pos >> CROMFS_ALIGN_SIZE_BIT]; @@ -961,9 +1097,13 @@ static int dfs_cromfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_ /* fill dirent */ if (sub_dirent->dirent.attr == CROMFS_DIRENT_ATTR_DIR) + { d->d_type = DT_DIR; + } else + { d->d_type = DT_REG; + } d->d_namlen = sub_dirent->dirent.name_size; d->d_reclen = (rt_uint16_t)sizeof(struct dirent); -- Gitee From db97f89d3601b4112ca0eb5dc838f3a5590243ca Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Sun, 7 Feb 2021 15:20:36 +0800 Subject: [PATCH 06/12] add fd dup support --- components/dfs/include/dfs.h | 1 + components/dfs/src/dfs.c | 42 +++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/components/dfs/include/dfs.h b/components/dfs/include/dfs.h index df35a873f9..77d3b7b978 100644 --- a/components/dfs/include/dfs.h +++ b/components/dfs/include/dfs.h @@ -105,6 +105,7 @@ int fdt_fd_new(struct dfs_fdtable *fdt); struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd, int ref_inc_nr); void fdt_fd_release(struct dfs_fdtable* fdt, struct dfs_fd *fd); int fd_new(void); +int fd_dup(int fd); struct dfs_fd *fd_get(int fd, int ref_inc_nr); void fd_release(struct dfs_fd *fd); diff --git a/components/dfs/src/dfs.c b/components/dfs/src/dfs.c index 73f48c0e55..39826bc168 100644 --- a/components/dfs/src/dfs.c +++ b/components/dfs/src/dfs.c @@ -296,11 +296,16 @@ void fdt_fd_release(struct dfs_fdtable* fdt, struct dfs_fd *fd) { int idx = -1; - RT_ASSERT(fd != NULL); RT_ASSERT(fdt != NULL); dfs_fd_lock(); + if (fd == NULL) + { + dfs_fd_unlock(); + return; + } + idx = fd->idx; /* check fd */ RT_ASSERT(fdt->fds[idx] == fd); @@ -330,6 +335,41 @@ void fd_release(struct dfs_fd *fd) fdt_fd_release(fdt, fd); } +int fd_dup(int oldfd) +{ + int newfd = -1; + struct dfs_fdtable *fdt = NULL; + + dfs_fd_lock(); + /* check old fd */ + fdt = dfs_fdtable_get(); + oldfd -= DFS_FD_OFFSET; + if ((oldfd < 0) || (oldfd >= fdt->maxfd)) + { + goto exit; + } + if (!fdt->fds[oldfd]) + { + goto exit; + } + /* get a new fd */ + newfd = fd_new(); + if (newfd < 0) + { + goto exit; + } + + newfd -= DFS_FD_OFFSET; + /* replace newfd ref */ + rt_free(fdt->fds[newfd]); + fdt->fds[newfd] = fdt->fds[oldfd]; + /* inc ref_count */ + fdt->fds[newfd]->ref_count++; +exit: + dfs_fd_unlock(); + return (newfd == -1 ? newfd : newfd + DFS_FD_OFFSET); +} + void fd_init(struct dfs_fd *fd) { if (fd) -- Gitee From 01f480e2c0dd678dd30cd082f0ba03eb5769b25c Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Sun, 7 Feb 2021 16:51:36 +0800 Subject: [PATCH 07/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9stdio=E4=B8=89=E4=B8=AA?= =?UTF-8?q?fd=E4=B8=8D=E5=86=8D=E9=9C=80=E8=A6=81=E7=89=B9=E6=AE=8A?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/dfs/include/dfs.h | 5 +- components/dfs/include/dfs_file.h | 1 - components/dfs/src/dfs.c | 94 +++++++++++++++++++------- components/libc/compilers/musl/stdio.c | 13 +++- components/lwp/lwp.c | 28 ++++---- components/lwp/lwp_pid.c | 20 ++---- components/lwp/lwp_syscall.c | 4 -- 7 files changed, 101 insertions(+), 64 deletions(-) diff --git a/components/dfs/include/dfs.h b/components/dfs/include/dfs.h index 77d3b7b978..e79856e3f6 100644 --- a/components/dfs/include/dfs.h +++ b/components/dfs/include/dfs.h @@ -31,8 +31,8 @@ /* * skip stdin/stdout/stderr normally */ -#ifndef DFS_FD_OFFSET -#define DFS_FD_OFFSET 3 +#ifndef DFS_STDIO_OFFSET +#define DFS_STDIO_OFFSET 3 #endif #ifndef DFS_PATH_MAX @@ -106,6 +106,7 @@ struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd, int ref_inc_nr); void fdt_fd_release(struct dfs_fdtable* fdt, struct dfs_fd *fd); int fd_new(void); int fd_dup(int fd); +int fd_associate(struct dfs_fdtable *fdt, int fd, struct dfs_fd *file); struct dfs_fd *fd_get(int fd, int ref_inc_nr); void fd_release(struct dfs_fd *fd); diff --git a/components/dfs/include/dfs_file.h b/components/dfs/include/dfs_file.h index 42c2207ae5..da45ec2096 100644 --- a/components/dfs/include/dfs_file.h +++ b/components/dfs/include/dfs_file.h @@ -56,7 +56,6 @@ struct dfs_fnode struct dfs_fd { - int idx; /* the idx in fdt */ uint16_t magic; /* file descriptor magic number */ int ref_count; /* Descriptor reference count */ off_t pos; /* Current file position */ diff --git a/components/dfs/src/dfs.c b/components/dfs/src/dfs.c index 39826bc168..06e4849d2d 100644 --- a/components/dfs/src/dfs.c +++ b/components/dfs/src/dfs.c @@ -143,12 +143,11 @@ void dfs_fd_unlock(void) rt_mutex_release(&fdlock); } -static int fd_alloc(struct dfs_fdtable *fdt, int startfd) +static int fd_slot_alloc(struct dfs_fdtable *fdt, int startfd) { int idx = 0; - struct dfs_fd *fd = NULL; - /* find an empty fd entry */ + /* find an empty fd slot */ for (idx = startfd; idx < (int)fdt->maxfd; idx++) { if (fdt->fds[idx] == RT_NULL) @@ -158,7 +157,7 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd) } /* allocate a larger FD container */ - if (idx == fdt->maxfd && fdt->maxfd < DFS_FD_MAX) + if (idx >= fdt->maxfd && fdt->maxfd < DFS_FD_MAX) { int cnt = 0; int index = 0; @@ -183,6 +182,15 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd) fdt->fds = fds; fdt->maxfd = cnt; } + return idx; +} + +static int fd_alloc(struct dfs_fdtable *fdt, int startfd) +{ + int idx = 0; + struct dfs_fd *fd = NULL; + + idx = fd_slot_alloc(fdt, startfd); /* allocate 'struct dfs_fd' */ if (idx < (int)fdt->maxfd && fdt->fds[idx] == RT_NULL) @@ -195,7 +203,6 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd) fd->ref_count = 1; fd->magic = DFS_FD_MAGIC; fd->fnode = NULL; - fd->idx = idx; fdt->fds[idx] = fd; } @@ -216,19 +223,19 @@ int fdt_fd_new(struct dfs_fdtable *fdt) dfs_fd_lock(); /* find an empty fd entry */ - idx = fd_alloc(fdt, 0); + idx = fd_alloc(fdt, DFS_STDIO_OFFSET); /* can't find an empty fd entry */ if (idx == fdt->maxfd) { - idx = -(1 + DFS_FD_OFFSET); + idx = -1; LOG_E("DFS fd new is failed! Could not found an empty fd entry."); goto __result; } __result: dfs_fd_unlock(); - return idx + DFS_FD_OFFSET; + return idx; } int fd_new(void) @@ -253,12 +260,6 @@ struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd, int ref_inc_nr) { struct dfs_fd *d; -#if defined(RT_USING_DFS_DEVFS) && defined(RT_USING_POSIX) - if ((0 <= fd) && (fd <= 2)) - fd = libc_stdio_get_console(); -#endif - - fd = fd - DFS_FD_OFFSET; if (fd < 0 || fd >= (int)fdt->maxfd) return NULL; @@ -294,7 +295,8 @@ struct dfs_fd *fd_get(int fd, int ref_inc_nr) */ void fdt_fd_release(struct dfs_fdtable* fdt, struct dfs_fd *fd) { - int idx = -1; + int idx = 0; + struct dfs_fd *fd_iter = NULL; RT_ASSERT(fdt != NULL); @@ -306,9 +308,21 @@ void fdt_fd_release(struct dfs_fdtable* fdt, struct dfs_fd *fd) return; } - idx = fd->idx; + for (idx = 0; idx < fdt->maxfd; idx++) + { + fd_iter = fdt->fds[idx]; + if (fd_iter == fd) + { + break; + } + } + if (idx == fdt->maxfd) + { + dfs_fd_unlock(); + return; + } + /* check fd */ - RT_ASSERT(fdt->fds[idx] == fd); RT_ASSERT(fd->magic == DFS_FD_MAGIC); fd->ref_count--; @@ -343,7 +357,6 @@ int fd_dup(int oldfd) dfs_fd_lock(); /* check old fd */ fdt = dfs_fdtable_get(); - oldfd -= DFS_FD_OFFSET; if ((oldfd < 0) || (oldfd >= fdt->maxfd)) { goto exit; @@ -353,28 +366,57 @@ int fd_dup(int oldfd) goto exit; } /* get a new fd */ - newfd = fd_new(); - if (newfd < 0) + newfd = fd_slot_alloc(fdt, DFS_STDIO_OFFSET); + if (newfd >= fdt->maxfd) { goto exit; } - newfd -= DFS_FD_OFFSET; - /* replace newfd ref */ - rt_free(fdt->fds[newfd]); fdt->fds[newfd] = fdt->fds[oldfd]; /* inc ref_count */ fdt->fds[newfd]->ref_count++; exit: dfs_fd_unlock(); - return (newfd == -1 ? newfd : newfd + DFS_FD_OFFSET); + return newfd; +} + +int fd_associate(struct dfs_fdtable *fdt, int fd, struct dfs_fd *file) +{ + int retfd = -1; + + if (!file) + { + return retfd; + } + if (!fdt) + { + return retfd; + } + + dfs_fd_lock(); + /* check old fd */ + if ((fd < 0) || (fd >= fdt->maxfd)) + { + goto exit; + } + + if (fdt->fds[fd]) + { + goto exit; + } + /* inc ref_count */ + file->ref_count++; + fdt->fds[fd] = file; + retfd = fd; +exit: + dfs_fd_unlock(); + return retfd; } void fd_init(struct dfs_fd *fd) { if (fd) { - fd->idx = -1; fd->magic = DFS_FD_MAGIC; fd->ref_count = 1; fd->pos = 0; @@ -588,7 +630,7 @@ int list_fd(void) if (fd && fd->fnode->fops) { - rt_kprintf("%2d ", index + DFS_FD_OFFSET); + rt_kprintf("%2d ", index); if (fd->fnode->type == FT_DIRECTORY) rt_kprintf("%-7.7s ", "dir"); else if (fd->fnode->type == FT_REGULAR) rt_kprintf("%-7.7s ", "file"); else if (fd->fnode->type == FT_SOCKET) rt_kprintf("%-7.7s ", "socket"); diff --git a/components/libc/compilers/musl/stdio.c b/components/libc/compilers/musl/stdio.c index 4ccd3a4923..d1ebcba52d 100644 --- a/components/libc/compilers/musl/stdio.c +++ b/components/libc/compilers/musl/stdio.c @@ -12,6 +12,7 @@ #include #include "libc.h" +#include "dfs.h" #define STDIO_DEVICE_NAME_MAX 32 @@ -45,7 +46,17 @@ int libc_stdio_set_console(const char* device_name, int mode) std_console = fp; } - if (std_console) return fileno(std_console); + if (std_console) + { + struct dfs_fdtable *fdt = dfs_fdtable_get(); + int fd = fileno(std_console); + + /* set fd (0, 1, 2) */ + fd_associate(fdt, 0, fd_get(fd, 0)); + fd_associate(fdt, 1, fd_get(fd, 0)); + fd_associate(fdt, 2, fd_get(fd, 0)); + return fd; + } return -1; } diff --git a/components/lwp/lwp.c b/components/lwp/lwp.c index 25ba78014e..964c924b0f 100644 --- a/components/lwp/lwp.c +++ b/components/lwp/lwp.c @@ -749,26 +749,22 @@ void lwp_cleanup(struct rt_thread *tid) static void lwp_copy_stdio_fdt(struct rt_lwp *lwp) { - int fd; struct dfs_fd *d; struct dfs_fdtable *lwp_fdt; - struct dfs_fdtable *fdt; - - fd = libc_stdio_get_console(); - fdt = dfs_fdtable_get(); - d = fdt_fd_get(fdt, fd, 0); /* inc 0 ref count */ - - fd = fd - DFS_FD_OFFSET; - if (d == NULL) - { - return; - } lwp_fdt = &lwp->fdt; - lwp_fdt->fds = rt_malloc(sizeof(void *) * (fd + 1)); - rt_memset(lwp_fdt->fds, 0, sizeof(void *) * (fd + 1)); - lwp_fdt->fds[fd] = d; - lwp_fdt->maxfd = fd + 1; + /* init 4 fds */ + lwp_fdt->fds = rt_calloc(4, sizeof(void *)); + if (lwp_fdt->fds) + { + lwp_fdt->maxfd = 4; + d = fd_get(0, 0); + fd_associate(lwp_fdt, 0, d); + d = fd_get(1, 0); + fd_associate(lwp_fdt, 1, d); + d = fd_get(2, 0); + fd_associate(lwp_fdt, 2, d); + } return; } diff --git a/components/lwp/lwp_pid.c b/components/lwp/lwp_pid.c index 4221493624..42f0f92bb4 100644 --- a/components/lwp/lwp_pid.c +++ b/components/lwp/lwp_pid.c @@ -41,23 +41,15 @@ int libc_stdio_get_console(void); static void __exit_files(struct rt_lwp *lwp) { - int consolefd; /* the console fd, which must not be closed */ - - consolefd = libc_stdio_get_console(); - consolefd = consolefd - DFS_FD_OFFSET; - while (lwp->fdt.maxfd > 0) { - if (consolefd != lwp->fdt.maxfd - 1) /* skip the console fd */ - { - struct dfs_fd *d; + struct dfs_fd *d; - d = lwp->fdt.fds[lwp->fdt.maxfd - 1]; - if (d) - { - dfs_file_close(d); - rt_free(d); - } + d = lwp->fdt.fds[lwp->fdt.maxfd - 1]; + if (d) + { + dfs_file_close(d); + fdt_fd_release(&lwp->fdt, d); } lwp->fdt.maxfd --; } diff --git a/components/lwp/lwp_syscall.c b/components/lwp/lwp_syscall.c index 3d63279812..16b27a35ea 100644 --- a/components/lwp/lwp_syscall.c +++ b/components/lwp/lwp_syscall.c @@ -513,10 +513,6 @@ int sys_open(const char *name, int flag, ...) /* syscall: "close" ret: "int" args: "int" */ int sys_close(int fd) { - if ((0 <= fd) && (fd <= DFS_FD_OFFSET)) - { - return 0; - } return close(fd); } -- Gitee From c5d32c93a0ceb1d06338b50fe31bcfac5960cd58 Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Sun, 7 Feb 2021 17:35:22 +0800 Subject: [PATCH 08/12] =?UTF-8?q?=E5=88=A0=E9=99=A4fd=5Fget=E7=9A=84ref=5F?= =?UTF-8?q?inc=5Fnr=E5=8F=82=E6=95=B0=20=E4=BF=AE=E6=94=B9fd=5Frelease?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E7=94=B1dfs=5Ffd=E4=B8=BAfd=E7=B4=A2?= =?UTF-8?q?=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/dfs/include/dfs.h | 8 +-- components/dfs/src/dfs.c | 50 +++++++++---------- components/dfs/src/dfs_posix.c | 46 ++++++++--------- components/dfs/src/poll.c | 2 +- components/libc/compilers/musl/stdio.c | 6 +-- components/lwp/lwp.c | 6 +-- components/lwp/lwp_ipc.c | 6 +-- components/lwp/lwp_pid.c | 10 ++-- components/lwp/lwp_syscall.c | 2 +- components/net/sal_socket/dfs_net/dfs_net.c | 2 +- .../net/sal_socket/socket/net_sockets.c | 12 ++--- 11 files changed, 76 insertions(+), 74 deletions(-) diff --git a/components/dfs/include/dfs.h b/components/dfs/include/dfs.h index e79856e3f6..197f89cc6c 100644 --- a/components/dfs/include/dfs.h +++ b/components/dfs/include/dfs.h @@ -102,13 +102,13 @@ void dfs_fd_unlock(void); /* FD APIs */ int fdt_fd_new(struct dfs_fdtable *fdt); -struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd, int ref_inc_nr); -void fdt_fd_release(struct dfs_fdtable* fdt, struct dfs_fd *fd); +struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd); +void fdt_fd_release(struct dfs_fdtable* fdt, int fd); int fd_new(void); int fd_dup(int fd); int fd_associate(struct dfs_fdtable *fdt, int fd, struct dfs_fd *file); -struct dfs_fd *fd_get(int fd, int ref_inc_nr); -void fd_release(struct dfs_fd *fd); +struct dfs_fd *fd_get(int fd); +void fd_release(int fd); void fd_init(struct dfs_fd *fd); diff --git a/components/dfs/src/dfs.c b/components/dfs/src/dfs.c index 06e4849d2d..6302db1db1 100644 --- a/components/dfs/src/dfs.c +++ b/components/dfs/src/dfs.c @@ -256,7 +256,7 @@ int fd_new(void) * pointer. */ -struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd, int ref_inc_nr) +struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd) { struct dfs_fd *d; @@ -273,19 +273,17 @@ struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd, int ref_inc_nr) return NULL; } - d->ref_count += ref_inc_nr; - dfs_fd_unlock(); return d; } -struct dfs_fd *fd_get(int fd, int ref_inc_nr) +struct dfs_fd *fd_get(int fd) { struct dfs_fdtable *fdt; fdt = dfs_fdtable_get(); - return fdt_fd_get(fdt, fd, ref_inc_nr); + return fdt_fd_get(fdt, fd); } /** @@ -293,55 +291,47 @@ struct dfs_fd *fd_get(int fd, int ref_inc_nr) * * This function will put the file descriptor. */ -void fdt_fd_release(struct dfs_fdtable* fdt, struct dfs_fd *fd) +void fdt_fd_release(struct dfs_fdtable* fdt, int fd) { - int idx = 0; - struct dfs_fd *fd_iter = NULL; + struct dfs_fd *fd_slot = NULL; RT_ASSERT(fdt != NULL); dfs_fd_lock(); - if (fd == NULL) + if ((fd < 0) || (fd >= fdt->maxfd)) { dfs_fd_unlock(); return; } - for (idx = 0; idx < fdt->maxfd; idx++) - { - fd_iter = fdt->fds[idx]; - if (fd_iter == fd) - { - break; - } - } - if (idx == fdt->maxfd) + fd_slot = fdt->fds[fd]; + if (fd_slot == NULL) { dfs_fd_unlock(); return; } + fdt->fds[fd] = NULL; /* check fd */ - RT_ASSERT(fd->magic == DFS_FD_MAGIC); + RT_ASSERT(fd_slot->magic == DFS_FD_MAGIC); - fd->ref_count--; + fd_slot->ref_count--; /* clear this fd entry */ - if (fd->ref_count == 0) + if (fd_slot->ref_count == 0) { - struct dfs_fnode *fnode = fd->fnode; + struct dfs_fnode *fnode = fd_slot->fnode; if (fnode) { fnode->ref_count--; } - rt_free(fd); - fdt->fds[idx] = 0; + rt_free(fd_slot); } dfs_fd_unlock(); } -void fd_release(struct dfs_fd *fd) +void fd_release(int fd) { struct dfs_fdtable *fdt; @@ -372,6 +362,16 @@ int fd_dup(int oldfd) goto exit; } + if (fdt->fds[newfd]) + { + ret = dfs_file_close(fdt->fds[newfd]); + if (ret < 0) + { + goto exit; + } + fd_release(newfd); + } + fdt->fds[newfd] = fdt->fds[oldfd]; /* inc ref_count */ fdt->fds[newfd]->ref_count++; diff --git a/components/dfs/src/dfs_posix.c b/components/dfs/src/dfs_posix.c index 92790d965a..e537bc3484 100644 --- a/components/dfs/src/dfs_posix.c +++ b/components/dfs/src/dfs_posix.c @@ -41,13 +41,13 @@ int open(const char *file, int flags, ...) return -1; } - d = fd_get(fd, 0); + d = fd_get(fd); result = dfs_file_open(d, file, flags); if (result < 0) { /* release the ref-count of fd */ - fd_release(d); + fd_release(fd); rt_set_errno(result); @@ -71,7 +71,7 @@ int close(int fd) int result; struct dfs_fd *d; - d = fd_get(fd, 0); + d = fd_get(fd); if (d == NULL) { rt_set_errno(-EBADF); @@ -88,7 +88,7 @@ int close(int fd) return -1; } - fd_release(d); + fd_release(fd); return 0; } @@ -115,7 +115,7 @@ int read(int fd, void *buf, size_t len) struct dfs_fd *d; /* get the fd */ - d = fd_get(fd, 0); + d = fd_get(fd); if (d == NULL) { rt_set_errno(-EBADF); @@ -155,7 +155,7 @@ int write(int fd, const void *buf, size_t len) struct dfs_fd *d; /* get the fd */ - d = fd_get(fd, 0); + d = fd_get(fd); if (d == NULL) { rt_set_errno(-EBADF); @@ -190,7 +190,7 @@ off_t lseek(int fd, off_t offset, int whence) int result; struct dfs_fd *d; - d = fd_get(fd, 0); + d = fd_get(fd); if (d == NULL) { rt_set_errno(-EBADF); @@ -324,7 +324,7 @@ int fstat(int fildes, struct stat *buf) struct dfs_fd *d; /* get the fd */ - d = fd_get(fildes, 0); + d = fd_get(fildes); if (d == NULL) { rt_set_errno(-EBADF); @@ -367,7 +367,7 @@ int fsync(int fildes) struct dfs_fd *d; /* get the fd */ - d = fd_get(fildes, 0); + d = fd_get(fildes); if (d == NULL) { rt_set_errno(-EBADF); @@ -398,7 +398,7 @@ int fcntl(int fildes, int cmd, ...) struct dfs_fd *d; /* get the fd */ - d = fd_get(fildes, 0); + d = fd_get(fildes); if (d) { void *arg; @@ -463,7 +463,7 @@ int ftruncate(int fd, off_t length) int result; struct dfs_fd *d; - d = fd_get(fd, 0); + d = fd_get(fd); if (d == NULL) { rt_set_errno(-EBADF); @@ -536,20 +536,20 @@ int mkdir(const char *path, mode_t mode) return -1; } - d = fd_get(fd, 0); + d = fd_get(fd); result = dfs_file_open(d, path, O_DIRECTORY | O_CREAT); if (result < 0) { - fd_release(d); + fd_release(fd); rt_set_errno(result); return -1; } dfs_file_close(d); - fd_release(d); + fd_release(fd); return 0; } @@ -606,7 +606,7 @@ DIR *opendir(const char *name) return NULL; } - d = fd_get(fd, 0); + d = fd_get(fd); result = dfs_file_open(d, name, O_RDONLY | O_DIRECTORY); if (result >= 0) @@ -616,7 +616,7 @@ DIR *opendir(const char *name) if (t == NULL) { dfs_file_close(d); - fd_release(d); + fd_release(fd); } else { @@ -629,7 +629,7 @@ DIR *opendir(const char *name) } /* open failed */ - fd_release(d); + fd_release(fd); rt_set_errno(result); return NULL; @@ -650,7 +650,7 @@ struct dirent *readdir(DIR *d) int result; struct dfs_fd *fd; - fd = fd_get(d->fd, 0); + fd = fd_get(d->fd); if (fd == NULL) { rt_set_errno(-EBADF); @@ -698,7 +698,7 @@ long telldir(DIR *d) struct dfs_fd *fd; long result; - fd = fd_get(d->fd, 0); + fd = fd_get(d->fd); if (fd == NULL) { rt_set_errno(-EBADF); @@ -723,7 +723,7 @@ void seekdir(DIR *d, off_t offset) { struct dfs_fd *fd; - fd = fd_get(d->fd, 0); + fd = fd_get(d->fd); if (fd == NULL) { rt_set_errno(-EBADF); @@ -747,7 +747,7 @@ void rewinddir(DIR *d) { struct dfs_fd *fd; - fd = fd_get(d->fd, 0); + fd = fd_get(d->fd); if (fd == NULL) { rt_set_errno(-EBADF); @@ -774,7 +774,7 @@ int closedir(DIR *d) int result; struct dfs_fd *fd; - fd = fd_get(d->fd, 0); + fd = fd_get(d->fd); if (fd == NULL) { rt_set_errno(-EBADF); @@ -783,7 +783,7 @@ int closedir(DIR *d) } result = dfs_file_close(fd); - fd_release(fd); + fd_release(d->fd); rt_free(d); diff --git a/components/dfs/src/poll.c b/components/dfs/src/poll.c index 3a8f4f6570..a2f3cb8074 100644 --- a/components/dfs/src/poll.c +++ b/components/dfs/src/poll.c @@ -128,7 +128,7 @@ static int do_pollfd(struct pollfd *pollfd, rt_pollreq_t *req) if (fd >= 0) { - struct dfs_fd *f = fd_get(fd, 0); + struct dfs_fd *f = fd_get(fd); mask = POLLNVAL; if (f) diff --git a/components/libc/compilers/musl/stdio.c b/components/libc/compilers/musl/stdio.c index d1ebcba52d..7a8013f31f 100644 --- a/components/libc/compilers/musl/stdio.c +++ b/components/libc/compilers/musl/stdio.c @@ -52,9 +52,9 @@ int libc_stdio_set_console(const char* device_name, int mode) int fd = fileno(std_console); /* set fd (0, 1, 2) */ - fd_associate(fdt, 0, fd_get(fd, 0)); - fd_associate(fdt, 1, fd_get(fd, 0)); - fd_associate(fdt, 2, fd_get(fd, 0)); + fd_associate(fdt, 0, fd_get(fd)); + fd_associate(fdt, 1, fd_get(fd)); + fd_associate(fdt, 2, fd_get(fd)); return fd; } diff --git a/components/lwp/lwp.c b/components/lwp/lwp.c index 964c924b0f..ad25a33a8f 100644 --- a/components/lwp/lwp.c +++ b/components/lwp/lwp.c @@ -758,11 +758,11 @@ static void lwp_copy_stdio_fdt(struct rt_lwp *lwp) if (lwp_fdt->fds) { lwp_fdt->maxfd = 4; - d = fd_get(0, 0); + d = fd_get(0); fd_associate(lwp_fdt, 0, d); - d = fd_get(1, 0); + d = fd_get(1); fd_associate(lwp_fdt, 1, d); - d = fd_get(2, 0); + d = fd_get(2); fd_associate(lwp_fdt, 2, d); } diff --git a/components/lwp/lwp_ipc.c b/components/lwp/lwp_ipc.c index c2e4d86845..f9d2fcab99 100644 --- a/components/lwp/lwp_ipc.c +++ b/components/lwp/lwp_ipc.c @@ -795,10 +795,10 @@ static struct dfs_fd *lwp_fd_get(int fdt_type, int fd) { fdt = dfs_fdtable_get(); } - return fdt_fd_get(fdt, fd, 0); + return fdt_fd_get(fdt, fd); } -static void lwp_fd_release(int fdt_type, struct dfs_fd *fd) +static void lwp_fd_release(int fdt_type, int fd) { struct dfs_fdtable *fdt; @@ -837,7 +837,7 @@ static void _chfd_free(int fd, int fdt_type) { return; } - lwp_fd_release(fdt_type, d); + lwp_fd_release(fdt_type, fd); } /* for fops */ diff --git a/components/lwp/lwp_pid.c b/components/lwp/lwp_pid.c index 42f0f92bb4..b96db75c08 100644 --- a/components/lwp/lwp_pid.c +++ b/components/lwp/lwp_pid.c @@ -41,17 +41,19 @@ int libc_stdio_get_console(void); static void __exit_files(struct rt_lwp *lwp) { - while (lwp->fdt.maxfd > 0) + int fd = lwp->fdt.maxfd - 1; + + while (fd >= 0) { struct dfs_fd *d; - d = lwp->fdt.fds[lwp->fdt.maxfd - 1]; + d = lwp->fdt.fds[fd]; if (d) { dfs_file_close(d); - fdt_fd_release(&lwp->fdt, d); + fdt_fd_release(&lwp->fdt, fd); } - lwp->fdt.maxfd --; + fd--; } } diff --git a/components/lwp/lwp_syscall.c b/components/lwp/lwp_syscall.c index 16b27a35ea..48de7776e8 100644 --- a/components/lwp/lwp_syscall.c +++ b/components/lwp/lwp_syscall.c @@ -2335,7 +2335,7 @@ int sys_getdents(int fd, struct libc_dirent *dirp, size_t nbytes) rt_set_errno(ENOMEM); return -1; } - dfs_fd = fd_get(fd, 0); + dfs_fd = fd_get(fd); ret = dfs_file_getdents(dfs_fd, rtt_dirp, nbytes); if (ret) { diff --git a/components/net/sal_socket/dfs_net/dfs_net.c b/components/net/sal_socket/dfs_net/dfs_net.c index 20abd078e1..0d88674105 100644 --- a/components/net/sal_socket/dfs_net/dfs_net.c +++ b/components/net/sal_socket/dfs_net/dfs_net.c @@ -23,7 +23,7 @@ int dfs_net_getsocket(int fd) int socket; struct dfs_fd *_dfs_fd; - _dfs_fd = fd_get(fd, 0); + _dfs_fd = fd_get(fd); if (_dfs_fd == NULL) return -1; if (_dfs_fd->fnode->type != FT_SOCKET) socket = -1; diff --git a/components/net/sal_socket/socket/net_sockets.c b/components/net/sal_socket/socket/net_sockets.c index e9017b3403..7f9f57c4b5 100644 --- a/components/net/sal_socket/socket/net_sockets.c +++ b/components/net/sal_socket/socket/net_sockets.c @@ -37,7 +37,7 @@ int accept(int s, struct sockaddr *addr, socklen_t *addrlen) return -1; } - d = fd_get(fd, 0); + d = fd_get(fd); if(d) { /* this is a socket fd */ @@ -86,7 +86,7 @@ int shutdown(int s, int how) return -1; } - d = fd_get(s, 0); + d = fd_get(s); if (d == NULL) { rt_set_errno(-EBADF); @@ -204,7 +204,7 @@ int socket(int domain, int type, int protocol) return -1; } - d = fd_get(fd, 0); + d = fd_get(fd); /* create socket and then put it to the dfs_fd */ socket = sal_socket(domain, type, protocol); @@ -226,7 +226,7 @@ int socket(int domain, int type, int protocol) else { /* release fd */ - fd_release(d); + fd_release(fd); rt_set_errno(-ENOMEM); @@ -250,7 +250,7 @@ int closesocket(int s) return -1; } - d = fd_get(s, 0); + d = fd_get(s); if (d == RT_NULL) { rt_set_errno(-EBADF); @@ -268,7 +268,7 @@ int closesocket(int s) } /* socket has been closed, delete it from file system fd */ - fd_release(d); + fd_release(s); return error; } -- Gitee From 22e171aaa81fb98c10ef81b19891042d727acaf6 Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Mon, 8 Feb 2021 08:58:13 +0800 Subject: [PATCH 09/12] =?UTF-8?q?add=20sys=5Fdup=20sys=5Fdup2=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dfs/filesystems/cromfs/dfs_cromfs.c | 26 ++-- components/dfs/filesystems/devfs/devfs.c | 2 + components/dfs/filesystems/elmfat/dfs_elm.c | 20 ++- components/dfs/filesystems/jffs2/dfs_jffs2.c | 58 +++++++ components/dfs/include/dfs.h | 1 - components/dfs/src/dfs.c | 145 +++++++++++++----- components/libc/compilers/musl/stdio.c | 8 +- components/lwp/lwp_syscall.c | 5 + 8 files changed, 207 insertions(+), 58 deletions(-) diff --git a/components/dfs/filesystems/cromfs/dfs_cromfs.c b/components/dfs/filesystems/cromfs/dfs_cromfs.c index 9ca472995b..da01e669f2 100644 --- a/components/dfs/filesystems/cromfs/dfs_cromfs.c +++ b/components/dfs/filesystems/cromfs/dfs_cromfs.c @@ -129,7 +129,7 @@ static struct cromfs_avl_struct* cromfs_avl_find(avl_key_t key, struct cromfs_av static void cromfs_avl_rebalance(struct cromfs_avl_struct ***nodeplaces_ptr, int count) { - for ( ;count > 0; count--) + for (;count > 0; count--) { struct cromfs_avl_struct **nodeplace = *--nodeplaces_ptr; struct cromfs_avl_struct *node = *nodeplace; @@ -144,7 +144,8 @@ static void cromfs_avl_rebalance(struct cromfs_avl_struct ***nodeplaces_ptr, int int heightleftright = heightof(nodeleftright); if (heightof(nodeleftleft) >= heightleftright) { - node->avl_left = nodeleftright; nodeleft->avl_right = node; + node->avl_left = nodeleftright; + nodeleft->avl_right = node; nodeleft->avl_height = 1 + (node->avl_height = 1 + heightleftright); *nodeplace = nodeleft; } @@ -166,7 +167,8 @@ static void cromfs_avl_rebalance(struct cromfs_avl_struct ***nodeplaces_ptr, int int heightrightleft = heightof(noderightleft); if (heightof(noderightright) >= heightrightleft) { - node->avl_right = noderightleft; noderight->avl_left = node; + node->avl_right = noderightleft; + noderight->avl_left = node; noderight->avl_height = 1 + (node->avl_height = 1 + heightrightleft); *nodeplace = noderight; } @@ -207,7 +209,8 @@ static void cromfs_avl_remove(struct cromfs_avl_struct *node_to_delete, struct c { return; } - *stack_ptr++ = nodeplace; stack_count++; + *stack_ptr++ = nodeplace; + stack_count++; if (key == node->avl_key) { break; @@ -225,7 +228,8 @@ static void cromfs_avl_remove(struct cromfs_avl_struct *node_to_delete, struct c if (node_to_delete->avl_left == AVL_EMPTY) { *nodeplace_to_delete = node_to_delete->avl_right; - stack_ptr--; stack_count--; + stack_ptr--; + stack_count--; } else { @@ -239,7 +243,8 @@ static void cromfs_avl_remove(struct cromfs_avl_struct *node_to_delete, struct c { break; } - *stack_ptr++ = nodeplace; stack_count++; + *stack_ptr++ = nodeplace; + stack_count++; nodeplace = &node->avl_right; } *nodeplace = node->avl_left; @@ -266,7 +271,8 @@ static void cromfs_avl_insert(struct cromfs_avl_struct *new_node, struct cromfs_ { break; } - *stack_ptr++ = nodeplace; stack_count++; + *stack_ptr++ = nodeplace; + stack_count++; if (key < node->avl_key) { nodeplace = &node->avl_left; @@ -589,12 +595,12 @@ static uint32_t cromfs_lookup(cromfs_info *ci, const char *path, int* is_dir, ui /* skip /// */ while (*subpath_end && *subpath_end == '/') { - subpath_end ++; + subpath_end++; } subpath = subpath_end; while ((*subpath_end != '/') && *subpath_end) { - subpath_end ++; + subpath_end++; } if (*subpath == '\0') { @@ -1087,7 +1093,7 @@ static int dfs_cromfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_ return -EINVAL; } - for (index = 0; index < count && file->pos < file->fnode->size; index ++) + for (index = 0; index < count && file->pos < file->fnode->size; index++) { uint32_t name_size = 0; diff --git a/components/dfs/filesystems/devfs/devfs.c b/components/dfs/filesystems/devfs/devfs.c index f700d0ecc0..364618376b 100644 --- a/components/dfs/filesystems/devfs/devfs.c +++ b/components/dfs/filesystems/devfs/devfs.c @@ -172,7 +172,9 @@ int dfs_device_fs_open(struct dfs_fd *file) device = rt_device_find(&file->fnode->path[1]); if (device == RT_NULL) + { return -ENODEV; + } #ifdef RT_USING_POSIX if (device->fops) diff --git a/components/dfs/filesystems/elmfat/dfs_elm.c b/components/dfs/filesystems/elmfat/dfs_elm.c index b28da54f07..c989a04cb4 100644 --- a/components/dfs/filesystems/elmfat/dfs_elm.c +++ b/components/dfs/filesystems/elmfat/dfs_elm.c @@ -342,7 +342,9 @@ int dfs_elm_open(struct dfs_fd *file) return -ENOENT; drivers_fn = (char *)rt_malloc(256); if (drivers_fn == RT_NULL) + { return -ENOMEM; + } rt_snprintf(drivers_fn, 256, "%d:%s", vol, file->fnode->path); #else @@ -393,18 +395,28 @@ int dfs_elm_open(struct dfs_fd *file) mode = FA_READ; if (file->fnode->flags & O_WRONLY) + { mode |= FA_WRITE; + } if ((file->fnode->flags & O_ACCMODE) & O_RDWR) + { mode |= FA_WRITE; + } /* Opens the file, if it is existing. If not, a new file is created. */ if (file->fnode->flags & O_CREAT) + { mode |= FA_OPEN_ALWAYS; + } /* Creates a new file. If the file is existing, it is truncated and overwritten. */ if (file->fnode->flags & O_TRUNC) + { mode |= FA_CREATE_ALWAYS; + } /* Creates a new file. The function fails if the file is already existing. */ if (file->fnode->flags & O_EXCL) + { mode |= FA_CREATE_NEW; + } /* allocate a fd */ fd = (FIL *)rt_malloc(sizeof(FIL)); @@ -451,7 +463,7 @@ int dfs_elm_close(struct dfs_fd *file) result = FR_OK; if (file->fnode->type == FT_DIRECTORY) { - DIR *dir; + DIR *dir = RT_NULL; dir = (DIR *)(file->data); RT_ASSERT(dir != RT_NULL); @@ -461,7 +473,7 @@ int dfs_elm_close(struct dfs_fd *file) } else if (file->fnode->type == FT_REGULAR) { - FIL *fd; + FIL *fd = RT_NULL; fd = (FIL *)(file->data); RT_ASSERT(fd != RT_NULL); @@ -550,7 +562,9 @@ int dfs_elm_write(struct dfs_fd *file, const void *buf, size_t len) file->pos = fd->fptr; file->fnode->size = f_size(fd); if (result == FR_OK) + { return byte_write; + } return elm_result_to_dfs(result); } @@ -589,7 +603,7 @@ int dfs_elm_lseek(struct dfs_fd *file, off_t offset) else if (file->fnode->type == FT_DIRECTORY) { /* which is a directory */ - DIR *dir; + DIR *dir = RT_NULL; dir = (DIR *)(file->data); RT_ASSERT(dir != RT_NULL); diff --git a/components/dfs/filesystems/jffs2/dfs_jffs2.c b/components/dfs/filesystems/jffs2/dfs_jffs2.c index acf94e7ed7..3ebd193188 100644 --- a/components/dfs/filesystems/jffs2/dfs_jffs2.c +++ b/components/dfs/filesystems/jffs2/dfs_jffs2.c @@ -102,14 +102,20 @@ static int dfs_jffs2_mount(struct dfs_filesystem* fs, for (index = 0; index < DEVICE_PART_MAX; index ++) { if (device_partition[index].dev == RT_NULL) + { break; + } } if (index == DEVICE_PART_MAX) + { return -ENOSPC; + } mte = rt_malloc(sizeof(struct cyg_mtab_entry)); if (mte == RT_NULL) + { return -ENOMEM; + } mte->name = fs->path; mte->fsname = "jffs2"; @@ -192,7 +198,9 @@ static int dfs_jffs2_statfs(struct dfs_filesystem* fs, result = _find_fs(&mte, fs->dev_id); if (result) + { return -ENOENT; + } RT_ASSERT(mte->data != 0); @@ -221,14 +229,20 @@ static int dfs_jffs2_open(struct dfs_fd* file) jffs2_file = rt_malloc(sizeof(cyg_file)); if (jffs2_file == RT_NULL) + { return -ENOMEM; + } /* just escape '/' provided by dfs code */ name = file->fnode->path; if ((name[0] == '/') && (name[1] == 0)) + { name = jffs2_root_path; + } else /* name[0] still will be '/' */ + { name ++; + } result = _find_fs(&mte, fs->dev_id); if (result) @@ -322,7 +336,9 @@ static int dfs_jffs2_close(struct dfs_fd* file) result = jffs2_dir_colse(jffs2_file); rt_mutex_release(&jffs2_lock); if (result) + { return jffs2_result_to_dfs(result); + } rt_free(jffs2_file); return 0; @@ -332,7 +348,9 @@ static int dfs_jffs2_close(struct dfs_fd* file) result = jffs2_file_colse(jffs2_file); rt_mutex_release(&jffs2_lock); if (result) + { return jffs2_result_to_dfs(result); + } /* release memory */ rt_free(jffs2_file); @@ -366,7 +384,9 @@ static int dfs_jffs2_read(struct dfs_fd* file, void* buf, size_t len) result = jffs2_file_read(jffs2_file, &uio_s); rt_mutex_release(&jffs2_lock); if (result) + { return jffs2_result_to_dfs(result); + } /* update position */ file->pos = jffs2_file->f_offset; @@ -398,7 +418,9 @@ static int dfs_jffs2_write(struct dfs_fd* file, result = jffs2_file_write(jffs2_file, &uio_s); rt_mutex_release(&jffs2_lock); if (result) + { return jffs2_result_to_dfs(result); + } /* update position */ file->pos = jffs2_file->f_offset; @@ -427,7 +449,9 @@ static int dfs_jffs2_lseek(struct dfs_fd* file, result = jffs2_file_lseek(jffs2_file, &offset, SEEK_SET); rt_mutex_release(&jffs2_lock); if (result) + { return jffs2_result_to_dfs(result); + } /* update file position */ file->pos = offset; return offset; @@ -479,7 +503,9 @@ static int dfs_jffs2_getdents(struct dfs_fd* file, rt_mutex_release(&jffs2_lock); /* if met a error or all entry are read over, break while*/ if (result || jffs2_d.d_name[0] == 0) + { break; + } #if defined (CYGPKG_FS_JFFS2_RET_DIRENT_DTYPE) switch(jffs2_d.d_type & JFFS2_S_IFMT) @@ -497,17 +523,25 @@ static int dfs_jffs2_getdents(struct dfs_fd* file, if ((file->fnode->path[0] == '/') ) { if (file->fnode->path[1] == 0) + { strcpy(fullname, jffs2_d.d_name); + } else + { rt_sprintf(fullname, "%s/%s", file->fnode->path+1, jffs2_d.d_name); + } } else + { rt_sprintf(fullname, "%s/%s", file->fnode->path, jffs2_d.d_name); + } rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER); result = jffs2_porting_stat(mte, mte->root, fullname, (void *)&s); rt_mutex_release(&jffs2_lock); if (result) + { return jffs2_result_to_dfs(result); + } rt_free(fullname); /* convert to dfs stat structure */ @@ -525,10 +559,14 @@ static int dfs_jffs2_getdents(struct dfs_fd* file, index ++; if (index * sizeof(struct dirent) >= count) + { break; + } } if (result) + { return jffs2_result_to_dfs(result); + } return index * sizeof(struct dirent); } @@ -540,11 +578,15 @@ static int dfs_jffs2_unlink(struct dfs_filesystem* fs, const char* path) result = _find_fs(&mte, fs->dev_id); if (result) + { return -ENOENT; + } /* deal path */ if (path[0] == '/') + { path++; + } /* judge file type, dir is to be delete by rmdir, others by unlink */ rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER); @@ -570,7 +612,9 @@ static int dfs_jffs2_unlink(struct dfs_filesystem* fs, const char* path) } rt_mutex_release(&jffs2_lock); if (result) + { return jffs2_result_to_dfs(result); + } return 0; } @@ -583,17 +627,25 @@ static int dfs_jffs2_rename(struct dfs_filesystem* fs, result = _find_fs(&mte, fs->dev_id); if (result) + { return -ENOENT; + } if (*oldpath == '/') + { oldpath += 1; + } if (*newpath == '/') + { newpath += 1; + } rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER); result = jffs2_rename(mte, mte->root, oldpath, mte->root, newpath); rt_mutex_release(&jffs2_lock); if (result) + { return jffs2_result_to_dfs(result); + } return 0; } @@ -607,18 +659,24 @@ static int dfs_jffs2_stat(struct dfs_filesystem* fs, const char *path, struct st RT_ASSERT(!((path[0] == '/') && (path[1] == 0))); if (path[0] == '/') + { path++; + } result = _find_fs(&mte, fs->dev_id); if (result) + { return -ENOENT; + } rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER); result = jffs2_porting_stat(mte, mte->root, path, (void *)&s); rt_mutex_release(&jffs2_lock); if (result) + { return jffs2_result_to_dfs(result); + } /* convert to dfs stat structure */ switch(s.st_mode & JFFS2_S_IFMT) { diff --git a/components/dfs/include/dfs.h b/components/dfs/include/dfs.h index 197f89cc6c..0662990ad9 100644 --- a/components/dfs/include/dfs.h +++ b/components/dfs/include/dfs.h @@ -105,7 +105,6 @@ int fdt_fd_new(struct dfs_fdtable *fdt); struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd); void fdt_fd_release(struct dfs_fdtable* fdt, int fd); int fd_new(void); -int fd_dup(int fd); int fd_associate(struct dfs_fdtable *fdt, int fd, struct dfs_fd *file); struct dfs_fd *fd_get(int fd); void fd_release(int fd); diff --git a/components/dfs/src/dfs.c b/components/dfs/src/dfs.c index 6302db1db1..c79f350658 100644 --- a/components/dfs/src/dfs.c +++ b/components/dfs/src/dfs.c @@ -143,44 +143,64 @@ void dfs_fd_unlock(void) rt_mutex_release(&fdlock); } -static int fd_slot_alloc(struct dfs_fdtable *fdt, int startfd) +static int fd_slot_expand(struct dfs_fdtable *fdt, int fd) { - int idx = 0; + int nr = 0; + int index = 0; + struct dfs_fd **fds = NULL; - /* find an empty fd slot */ - for (idx = startfd; idx < (int)fdt->maxfd; idx++) + if (fd < fdt->maxfd) { - if (fdt->fds[idx] == RT_NULL) - break; - if (fdt->fds[idx]->ref_count == 0) - break; + return fd; + } + if (fd >= DFS_FD_MAX) + { + return -1; } - /* allocate a larger FD container */ - if (idx >= fdt->maxfd && fdt->maxfd < DFS_FD_MAX) + nr = ((fd + 4) & ~3); + if (nr > DFS_FD_MAX) { - int cnt = 0; - int index = 0; - struct dfs_fd **fds = NULL; + nr = DFS_FD_MAX; + } + fds = (struct dfs_fd **)rt_realloc(fdt->fds, nr * sizeof(struct dfs_fd *)); + if (!fds) + { + return -1; + } - /* increase the number of FD with 4 step length */ - cnt = fdt->maxfd + 4; - cnt = cnt > DFS_FD_MAX ? DFS_FD_MAX : cnt; + /* clean the new allocated fds */ + for (index = fdt->maxfd; index < nr; index++) + { + fds[index] = NULL; + } + fdt->fds = fds; + fdt->maxfd = nr; - fds = (struct dfs_fd **)rt_realloc(fdt->fds, cnt * sizeof(struct dfs_fd *)); - if (fds == NULL) /* return fdt->maxfd */ - { - return fdt->maxfd; - } + return fd; +} - /* clean the new allocated fds */ - for (index = fdt->maxfd; index < cnt; index ++) +static int fd_slot_alloc(struct dfs_fdtable *fdt, int startfd) +{ + int idx = 0; + + /* find an empty fd slot */ + for (idx = startfd; idx < (int)fdt->maxfd; idx++) + { + if (fdt->fds[idx] == RT_NULL) { - fds[index] = NULL; + return idx; } + } - fdt->fds = fds; - fdt->maxfd = cnt; + idx = fdt->maxfd; + if (idx < startfd) + { + idx = startfd; + } + if (fd_slot_expand(fdt, idx) < 0) + { + return -1; } return idx; } @@ -193,18 +213,19 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd) idx = fd_slot_alloc(fdt, startfd); /* allocate 'struct dfs_fd' */ - if (idx < (int)fdt->maxfd && fdt->fds[idx] == RT_NULL) + if (idx < 0) { - fd = (struct dfs_fd *)rt_calloc(1, sizeof(struct dfs_fd)); - if (!fd) - { - return fdt->maxfd; - } - fd->ref_count = 1; - fd->magic = DFS_FD_MAGIC; - fd->fnode = NULL; - fdt->fds[idx] = fd; + return -1; } + fd = (struct dfs_fd *)rt_calloc(1, sizeof(struct dfs_fd)); + if (!fd) + { + return -1; + } + fd->ref_count = 1; + fd->magic = DFS_FD_MAGIC; + fd->fnode = NULL; + fdt->fds[idx] = fd; return idx; } @@ -226,9 +247,8 @@ int fdt_fd_new(struct dfs_fdtable *fdt) idx = fd_alloc(fdt, DFS_STDIO_OFFSET); /* can't find an empty fd entry */ - if (idx == fdt->maxfd) + if (idx < 0) { - idx = -1; LOG_E("DFS fd new is failed! Could not found an empty fd entry."); goto __result; } @@ -261,7 +281,9 @@ struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd) struct dfs_fd *d; if (fd < 0 || fd >= (int)fdt->maxfd) + { return NULL; + } dfs_fd_lock(); d = fdt->fds[fd]; @@ -339,7 +361,7 @@ void fd_release(int fd) fdt_fd_release(fdt, fd); } -int fd_dup(int oldfd) +int sys_dup(int oldfd) { int newfd = -1; struct dfs_fdtable *fdt = NULL; @@ -357,8 +379,50 @@ int fd_dup(int oldfd) } /* get a new fd */ newfd = fd_slot_alloc(fdt, DFS_STDIO_OFFSET); + if (newfd >= 0) + { + fdt->fds[newfd] = fdt->fds[oldfd]; + /* inc ref_count */ + fdt->fds[newfd]->ref_count++; + } +exit: + dfs_fd_unlock(); + return newfd; +} + +int sys_dup2(int oldfd, int newfd) +{ + struct dfs_fdtable *fdt = NULL; + int ret = 0; + int retfd = -1; + + dfs_fd_lock(); + /* check old fd */ + fdt = dfs_fdtable_get(); + if ((oldfd < 0) || (oldfd >= fdt->maxfd)) + { + goto exit; + } + if (!fdt->fds[oldfd]) + { + goto exit; + } + if (newfd < 0) + { + goto exit; + } if (newfd >= fdt->maxfd) { + newfd = fd_slot_expand(fdt, newfd); + if (newfd < 0) + { + goto exit; + } + } + if (fdt->fds[newfd] == fdt->fds[oldfd]) + { + /* ok, return newfd */ + retfd = newfd; goto exit; } @@ -375,9 +439,10 @@ int fd_dup(int oldfd) fdt->fds[newfd] = fdt->fds[oldfd]; /* inc ref_count */ fdt->fds[newfd]->ref_count++; + retfd = newfd; exit: dfs_fd_unlock(); - return newfd; + return retfd; } int fd_associate(struct dfs_fdtable *fdt, int fd, struct dfs_fd *file) diff --git a/components/libc/compilers/musl/stdio.c b/components/libc/compilers/musl/stdio.c index 7a8013f31f..962dfb0e71 100644 --- a/components/libc/compilers/musl/stdio.c +++ b/components/libc/compilers/musl/stdio.c @@ -19,6 +19,7 @@ int _EXFUN(fileno, (FILE *)); static FILE* std_console = NULL; +int sys_dup2(int oldfd, int new); int libc_stdio_set_console(const char* device_name, int mode) { @@ -48,13 +49,12 @@ int libc_stdio_set_console(const char* device_name, int mode) if (std_console) { - struct dfs_fdtable *fdt = dfs_fdtable_get(); int fd = fileno(std_console); /* set fd (0, 1, 2) */ - fd_associate(fdt, 0, fd_get(fd)); - fd_associate(fdt, 1, fd_get(fd)); - fd_associate(fdt, 2, fd_get(fd)); + sys_dup2(fd, 0); + sys_dup2(fd, 1); + sys_dup2(fd, 2); return fd; } diff --git a/components/lwp/lwp_syscall.c b/components/lwp/lwp_syscall.c index 48de7776e8..da518700a3 100644 --- a/components/lwp/lwp_syscall.c +++ b/components/lwp/lwp_syscall.c @@ -2515,6 +2515,9 @@ int sys_clock_getres(clockid_t clk, struct timespec *ts) int sys_futex(int *uaddr, int op, int val, void *timeout, void *uaddr2, int val3); int sys_pmutex(void *umutex, int op, void *arg); +int sys_dup(int oldfd); +int sys_dup2(int oldfd, int new); + const static void* func_table[] = { (void*)sys_exit, /* 01 */ @@ -2665,6 +2668,8 @@ const static void* func_table[] = (void *)sys_clone, /* 130 */ (void *)sys_futex, (void *)sys_pmutex, + (void *)sys_dup, + (void *)sys_dup2, }; const void *lwp_get_sys_api(rt_uint32_t number) -- Gitee From bfd3e0bf7c24cc6daf98eaf439ff12dcb1733610 Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Mon, 8 Feb 2021 10:57:33 +0800 Subject: [PATCH 10/12] =?UTF-8?q?=E6=B6=88=E9=99=A4=E8=AD=A6=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/dfs/filesystems/jffs2/dfs_jffs2.c | 6 ++-- components/dfs/filesystems/nfs/dfs_nfs.c | 5 +-- components/dfs/filesystems/ramfs/dfs_ramfs.c | 2 +- components/dfs/filesystems/romfs/dfs_romfs.c | 2 +- components/dfs/src/dfs.c | 38 ++++++++++---------- components/dfs/src/dfs_file.c | 12 +++---- components/drivers/src/pipe.c | 22 ++++++------ 7 files changed, 39 insertions(+), 48 deletions(-) diff --git a/components/dfs/filesystems/jffs2/dfs_jffs2.c b/components/dfs/filesystems/jffs2/dfs_jffs2.c index 3ebd193188..0a6130de0b 100644 --- a/components/dfs/filesystems/jffs2/dfs_jffs2.c +++ b/components/dfs/filesystems/jffs2/dfs_jffs2.c @@ -241,7 +241,7 @@ static int dfs_jffs2_open(struct dfs_fd* file) } else /* name[0] still will be '/' */ { - name ++; + name++; } result = _find_fs(&mte, fs->dev_id); @@ -520,7 +520,7 @@ static int dfs_jffs2_getdents(struct dfs_fd* file, return -ENOMEM; /* make a right entry */ - if ((file->fnode->path[0] == '/') ) + if (file->fnode->path[0] == '/') { if (file->fnode->path[1] == 0) { @@ -557,7 +557,7 @@ static int dfs_jffs2_getdents(struct dfs_fd* file, d->d_reclen = (rt_uint16_t)sizeof(struct dirent); rt_strncpy(d->d_name, jffs2_d.d_name, d->d_namlen + 1); - index ++; + index++; if (index * sizeof(struct dirent) >= count) { break; diff --git a/components/dfs/filesystems/nfs/dfs_nfs.c b/components/dfs/filesystems/nfs/dfs_nfs.c index 454a2303c5..4b17222ef0 100644 --- a/components/dfs/filesystems/nfs/dfs_nfs.c +++ b/components/dfs/filesystems/nfs/dfs_nfs.c @@ -562,7 +562,6 @@ int nfs_read(struct dfs_fd *file, void *buf, size_t count) if (file->fnode->type == FT_DIRECTORY) return -EISDIR; - RT_ASSERT(file->fnode->data != NULL); struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->fnode->data)); nfs = (struct nfs_filesystem *)(dfs_nfs->data); @@ -679,8 +678,7 @@ int nfs_write(struct dfs_fd *file, const void *buf, size_t count) file->fnode->size = fd->size; } xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res); - } - while (count > 0); + } while (count > 0); xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res); @@ -1085,7 +1083,6 @@ int nfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count) nfs_filesystem *nfs; char *name; - RT_ASSERT(file->fnode->data != NULL); struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->fnode->data)); nfs = (struct nfs_filesystem *)(dfs_nfs->data); diff --git a/components/dfs/filesystems/ramfs/dfs_ramfs.c b/components/dfs/filesystems/ramfs/dfs_ramfs.c index 3e47720a1c..656f971194 100644 --- a/components/dfs/filesystems/ramfs/dfs_ramfs.c +++ b/components/dfs/filesystems/ramfs/dfs_ramfs.c @@ -227,7 +227,7 @@ int dfs_ramfs_open(struct dfs_fd *file) /* remove '/' separator */ name_ptr = file->fnode->path; while (*name_ptr == '/' && *name_ptr) - name_ptr ++; + name_ptr++; strncpy(dirent->name, name_ptr, RAMFS_NAME_MAX); rt_list_init(&(dirent->list)); diff --git a/components/dfs/filesystems/romfs/dfs_romfs.c b/components/dfs/filesystems/romfs/dfs_romfs.c index 455987a363..bbf4bce075 100644 --- a/components/dfs/filesystems/romfs/dfs_romfs.c +++ b/components/dfs/filesystems/romfs/dfs_romfs.c @@ -261,7 +261,7 @@ int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count) return -EINVAL; index = 0; - for (index = 0; index < count && file->pos < file->fnode->size; index ++) + for (index = 0; index < count && file->pos < file->fnode->size; index++) { d = dirp + index; diff --git a/components/dfs/src/dfs.c b/components/dfs/src/dfs.c index c79f350658..39f73cdb3a 100644 --- a/components/dfs/src/dfs.c +++ b/components/dfs/src/dfs.c @@ -145,8 +145,8 @@ void dfs_fd_unlock(void) static int fd_slot_expand(struct dfs_fdtable *fdt, int fd) { - int nr = 0; - int index = 0; + int nr; + int index; struct dfs_fd **fds = NULL; if (fd < fdt->maxfd) @@ -182,7 +182,7 @@ static int fd_slot_expand(struct dfs_fdtable *fdt, int fd) static int fd_slot_alloc(struct dfs_fdtable *fdt, int startfd) { - int idx = 0; + int idx; /* find an empty fd slot */ for (idx = startfd; idx < (int)fdt->maxfd; idx++) @@ -207,7 +207,7 @@ static int fd_slot_alloc(struct dfs_fdtable *fdt, int startfd) static int fd_alloc(struct dfs_fdtable *fdt, int startfd) { - int idx = 0; + int idx; struct dfs_fd *fd = NULL; idx = fd_slot_alloc(fdt, startfd); @@ -238,7 +238,7 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd) */ int fdt_fd_new(struct dfs_fdtable *fdt) { - int idx = 0; + int idx; /* lock filesystem */ dfs_fd_lock(); @@ -250,10 +250,8 @@ int fdt_fd_new(struct dfs_fdtable *fdt) if (idx < 0) { LOG_E("DFS fd new is failed! Could not found an empty fd entry."); - goto __result; } -__result: dfs_fd_unlock(); return idx; } @@ -508,7 +506,7 @@ const char *dfs_subdir(const char *directory, const char *filename) dir = filename + strlen(directory); if ((*dir != '/') && (dir != filename)) { - dir --; + dir--; } return dir; @@ -573,14 +571,14 @@ char *dfs_normalize_path(const char *directory, const char *filename) if (c == '.') { - if (!src[1]) src ++; /* '.' and ends */ + if (!src[1]) src++; /* '.' and ends */ else if (src[1] == '/') { /* './' case */ src += 2; while ((*src == '/') && (*src != '\0')) - src ++; + src++; continue; } else if (src[1] == '.') @@ -597,7 +595,7 @@ char *dfs_normalize_path(const char *directory, const char *filename) src += 3; while ((*src == '/') && (*src != '\0')) - src ++; + src++; goto up_one; } } @@ -605,15 +603,15 @@ char *dfs_normalize_path(const char *directory, const char *filename) /* copy up the next '/' and erase all '/' */ while ((c = *src++) != '\0' && c != '/') - *dst ++ = c; + *dst++ = c; if (c == '/') { - *dst ++ = '/'; + *dst++ = '/'; while (c == '/') c = *src++; - src --; + src--; } else if (!c) break; @@ -621,20 +619,20 @@ char *dfs_normalize_path(const char *directory, const char *filename) continue; up_one: - dst --; + dst--; if (dst < dst0) { rt_free(fullpath); return NULL; } while (dst0 < dst && dst[-1] != '/') - dst --; + dst--; } *dst = '\0'; /* remove '/' in the end of path if exist */ - dst --; + dst--; if ((dst != fullpath) && (*dst == '/')) *dst = '\0'; @@ -689,7 +687,7 @@ int list_fd(void) rt_kprintf("fd type ref magic path\n"); rt_kprintf("-- ------ --- ----- ------\n"); - for (index = 0; index < (int)fd_table->maxfd; index ++) + for (index = 0; index < (int)fd_table->maxfd; index++) { struct dfs_fd *fd = fd_table->fds[index]; @@ -737,12 +735,12 @@ int mount(int argc, char *argv[]) rt_kprintf("filesystem device mountpoint\n"); rt_kprintf("---------- ------ ----------\n"); for (iter = &filesystem_table[0]; - iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++) + iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++) { if ((iter != NULL) && (iter->path != NULL)) { rt_kprintf("%-10s %-6s %-s\n", - iter->ops->name, iter->dev_id->parent.name, iter->path); + iter->ops->name, iter->dev_id->parent.name, iter->path); } } return 0; diff --git a/components/dfs/src/dfs_file.c b/components/dfs/src/dfs_file.c index db299f55ca..a1f7fc83ad 100644 --- a/components/dfs/src/dfs_file.c +++ b/components/dfs/src/dfs_file.c @@ -771,8 +771,7 @@ void ls(const char *pathname) rt_kprintf("BAD file: %s\n", dirent.d_name); rt_free(fullpath); } - } - while (length > 0); + } while (length > 0); dfs_file_close(&fd); } @@ -816,8 +815,7 @@ void cat(const char *filename) buffer[length] = '\0'; rt_kprintf("%s", buffer); } - } - while (length > 0); + } while (length > 0); dfs_file_close(&fd); } @@ -873,8 +871,7 @@ static void copyfile(const char *src, const char *dst) break; } } - } - while (read_bytes > 0); + } while (read_bytes > 0); dfs_file_close(&src_fd); dfs_file_close(&fd); @@ -939,8 +936,7 @@ static void copydir(const char *src, const char *dst) rt_free(src_entry_full); rt_free(dst_entry_full); } - } - while (length > 0); + } while (length > 0); dfs_file_close(&cpfd); } diff --git a/components/drivers/src/pipe.c b/components/drivers/src/pipe.c index 2a09459b6b..efc66e1481 100644 --- a/components/drivers/src/pipe.c +++ b/components/drivers/src/pipe.c @@ -42,17 +42,17 @@ static int pipe_fops_open(struct dfs_fd *fd) switch (fd->fnode->flags & O_ACCMODE) { case O_RDONLY: - pipe->readers ++; + pipe->readers++; break; case O_WRONLY: - pipe->writers ++; + pipe->writers++; break; case O_RDWR: - pipe->readers ++; - pipe->writers ++; + pipe->readers++; + pipe->writers++; break; } - device->ref_count ++; + device->ref_count++; __exit: rt_mutex_release(&(pipe->lock)); @@ -74,14 +74,14 @@ static int pipe_fops_close(struct dfs_fd *fd) switch (fd->fnode->flags & O_ACCMODE) { case O_RDONLY: - pipe->readers --; + pipe->readers--; break; case O_WRONLY: - pipe->writers --; + pipe->writers--; break; case O_RDWR: - pipe->readers --; - pipe->writers --; + pipe->readers--; + pipe->writers--; break; } @@ -101,7 +101,7 @@ static int pipe_fops_close(struct dfs_fd *fd) rt_ringbuffer_destroy(pipe->fifo); pipe->fifo = RT_NULL; } - device->ref_count --; + device->ref_count--; rt_mutex_release(&(pipe->lock)); @@ -564,7 +564,7 @@ int pipe(int fildes[2]) int mkfifo(const char *path, mode_t mode) { rt_pipe_t *pipe; - + pipe = rt_pipe_create(path, PIPE_BUFSZ); if (pipe == RT_NULL) { -- Gitee From e17511081417d41aab43182f6ff3f64d43066ee6 Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Mon, 8 Feb 2021 13:40:18 +0800 Subject: [PATCH 11/12] =?UTF-8?q?=E4=BF=AE=E6=AD=A3sys=5Fread=E5=AF=B9?= =?UTF-8?q?=E5=86=85=E6=A0=B8=E8=BF=94=E5=9B=9E=E5=80=BC=E7=9A=84=E5=88=A4?= =?UTF-8?q?=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/lwp/lwp_syscall.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/lwp/lwp_syscall.c b/components/lwp/lwp_syscall.c index da518700a3..7ea6000d8b 100644 --- a/components/lwp/lwp_syscall.c +++ b/components/lwp/lwp_syscall.c @@ -418,7 +418,7 @@ ssize_t sys_read(int fd, void *buf, size_t nbyte) } ret = read(fd, kmem, nbyte); - if (ret) + if (ret > 0) { lwp_put_to_user(buf, kmem, ret); } -- Gitee From 699d71a0b99461def9c1e462f874bb6233c79f27 Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Mon, 8 Feb 2021 14:06:17 +0800 Subject: [PATCH 12/12] =?UTF-8?q?=E5=8E=BB=E9=99=A4romfs=E4=B8=AD=E4=B8=80?= =?UTF-8?q?=E5=A4=84=E5=86=97=E4=BD=99=E7=9A=84=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/dfs/filesystems/romfs/dfs_romfs.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/components/dfs/filesystems/romfs/dfs_romfs.c b/components/dfs/filesystems/romfs/dfs_romfs.c index bbf4bce075..aae8c10644 100644 --- a/components/dfs/filesystems/romfs/dfs_romfs.c +++ b/components/dfs/filesystems/romfs/dfs_romfs.c @@ -112,9 +112,6 @@ struct romfs_dirent *dfs_romfs_lookup(struct romfs_dirent *root_dirent, const ch else { /* return file dirent */ - if (subpath != NULL) - break; /* not the end of path */ - return &dirent[index]; } } -- Gitee