From e49bae1300e7b34f11278f492a91746c4154b8c2 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Sun, 3 Jan 2021 01:50:00 +0800 Subject: [PATCH] fs: Remove fs_dupfd and fs_dupfd2 internal functions let's call either nx_dup/nx_dup2 or file_dup/file_dup2 instead just like other fs api: xxx->nx_xxx->file_xxx Signed-off-by: Xiang Xiao Change-Id: I1aacfb9e25dc7b3fcb0345ff7b269b1953a01e5b --- fs/inode/fs_files.c | 96 +------------------------ fs/inode/inode.h | 4 +- fs/vfs/Make.defs | 2 +- fs/vfs/fs_dup.c | 60 ++++++++++++++-- fs/vfs/fs_dup2.c | 94 +++++++++++++++++++++++- fs/vfs/fs_dupfd.c | 111 ----------------------------- fs/vfs/fs_dupfd2.c | 68 ------------------ include/nuttx/fs/fs.h | 39 +--------- sched/group/group_setupidlefiles.c | 4 +- 9 files changed, 158 insertions(+), 320 deletions(-) delete mode 100644 fs/vfs/fs_dupfd.c delete mode 100644 fs/vfs/fs_dupfd2.c diff --git a/fs/inode/fs_files.c b/fs/inode/fs_files.c index 52980187c7..62682e7cc2 100644 --- a/fs/inode/fs_files.c +++ b/fs/inode/fs_files.c @@ -116,98 +116,6 @@ void files_releaselist(FAR struct filelist *list) nxsem_destroy(&list->fl_sem); } -/**************************************************************************** - * Name: file_dup2 - * - * Description: - * Assign an inode to a specific files structure. This is the heart of - * dup2. - * - * Equivalent to the non-standard fs_dupfd2() function except that it - * accepts struct file instances instead of file descriptors and it does - * not set the errno variable. - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is return on - * any failure. - * - ****************************************************************************/ - -int file_dup2(FAR struct file *filep1, FAR struct file *filep2) -{ - FAR struct inode *inode; - struct file temp; - int ret; - - if (filep1 == NULL || filep1->f_inode == NULL || filep2 == NULL) - { - return -EBADF; - } - - if (filep1 == filep2) - { - return OK; - } - - /* Increment the reference count on the contained inode */ - - inode = filep1->f_inode; - ret = inode_addref(inode); - if (ret < 0) - { - return ret; - } - - /* Then clone the file structure */ - - temp.f_oflags = filep1->f_oflags; - temp.f_pos = filep1->f_pos; - temp.f_inode = inode; - temp.f_priv = NULL; - - /* Call the open method on the file, driver, mountpoint so that it - * can maintain the correct open counts. - */ - - if (inode->u.i_ops && inode->u.i_ops->open) - { -#ifndef CONFIG_DISABLE_MOUNTPOINT - if (INODE_IS_MOUNTPT(inode)) - { - /* Dup the open file on the in the new file structure */ - - ret = inode->u.i_mops->dup(filep1, &temp); - } - else -#endif - { - /* (Re-)open the pseudo file or device driver */ - - ret = inode->u.i_ops->open(&temp); - } - - /* Handle open failures */ - - if (ret < 0) - { - inode_release(inode); - return ret; - } - } - - /* If there is already an inode contained in the new file structure, - * close the file and release the inode. - */ - - ret = file_close(filep2); - DEBUGASSERT(ret == 0); - - /* Return the file structure */ - - memcpy(filep2, &temp, sizeof(temp)); - return OK; -} - /**************************************************************************** * Name: files_allocate * @@ -255,7 +163,7 @@ int files_allocate(FAR struct inode *inode, int oflags, off_t pos, } /**************************************************************************** - * Name: files_dupfd2 + * Name: files_dup2 * * Description: * Clone a file descriptor to a specific descriptor number. @@ -266,7 +174,7 @@ int files_allocate(FAR struct inode *inode, int oflags, off_t pos, * ****************************************************************************/ -int files_dupfd2(int fd1, int fd2) +int files_dup2(int fd1, int fd2) { FAR struct filelist *list; int ret; diff --git a/fs/inode/inode.h b/fs/inode/inode.h index f2c696e626..07681e9e8c 100644 --- a/fs/inode/inode.h +++ b/fs/inode/inode.h @@ -390,7 +390,7 @@ int files_allocate(FAR struct inode *inode, int oflags, off_t pos, FAR void *priv, int minfd); /**************************************************************************** - * Name: files_dupfd2 + * Name: files_dup2 * * Description: * Clone a file descriptor to a specific descriptor number. @@ -401,7 +401,7 @@ int files_allocate(FAR struct inode *inode, int oflags, off_t pos, * ****************************************************************************/ -int files_dupfd2(int fd1, int fd2); +int files_dup2(int fd1, int fd2); /**************************************************************************** * Name: files_close diff --git a/fs/vfs/Make.defs b/fs/vfs/Make.defs index 8a664be3b6..3b92ef6807 100644 --- a/fs/vfs/Make.defs +++ b/fs/vfs/Make.defs @@ -35,7 +35,7 @@ # Common file/socket descriptor support -CSRCS += fs_close.c fs_dup.c fs_dup2.c fs_fcntl.c fs_dupfd.c fs_dupfd2.c +CSRCS += fs_close.c fs_dup.c fs_dup2.c fs_fcntl.c CSRCS += fs_epoll.c fs_fstat.c fs_fstatfs.c fs_getfilep.c fs_ioctl.c CSRCS += fs_lseek.c fs_mkdir.c fs_open.c fs_poll.c fs_read.c fs_rename.c CSRCS += fs_rmdir.c fs_statfs.c fs_stat.c fs_select.c fs_unlink.c fs_write.c diff --git a/fs/vfs/fs_dup.c b/fs/vfs/fs_dup.c index 5848877ea2..2dd3d556e3 100644 --- a/fs/vfs/fs_dup.c +++ b/fs/vfs/fs_dup.c @@ -50,6 +50,47 @@ * Public Functions ****************************************************************************/ +/**************************************************************************** + * Name: file_dup + * + * Description: + * Equivalent to the standard dup() function except that it + * accepts a struct file instance instead of a file descriptor. + * + * Returned Value: + * The new file descriptor is returned on success; a negated errno value + * is returned on any failure. + * + ****************************************************************************/ + +int file_dup(FAR struct file *filep, int minfd) +{ + struct file filep2; + int fd2; + int ret; + + /* Let file_dup2() do the real work */ + + memset(&filep2, 0, sizeof(filep2)); + ret = file_dup2(filep, &filep2); + if (ret < 0) + { + return ret; + } + + /* Then allocate a new file descriptor for the inode */ + + fd2 = files_allocate(filep2.f_inode, filep2.f_oflags, + filep2.f_pos, filep2.f_priv, minfd); + if (fd2 < 0) + { + file_close(&filep2); + return fd2; + } + + return fd2; +} + /**************************************************************************** * Name: nx_dup * @@ -74,11 +115,22 @@ int nx_dup(int fd) if (fd < CONFIG_NFILE_DESCRIPTORS) { - /* Its a valid file descriptor.. dup the file descriptor using any - * other file descriptor. - */ + FAR struct file *filep; + int ret; - return fs_dupfd(fd, 0); + /* Get the file structure corresponding to the file descriptor. */ + + ret = fs_getfilep(fd, &filep); + if (ret < 0) + { + return ret; + } + + DEBUGASSERT(filep != NULL); + + /* Let file_dup() do the real work */ + + return file_dup(filep, 0); } else { diff --git a/fs/vfs/fs_dup2.c b/fs/vfs/fs_dup2.c index 33ccf8fc39..c59b04a7b7 100644 --- a/fs/vfs/fs_dup2.c +++ b/fs/vfs/fs_dup2.c @@ -50,6 +50,98 @@ * Public Functions ****************************************************************************/ +/**************************************************************************** + * Name: file_dup2 + * + * Description: + * Assign an inode to a specific files structure. This is the heart of + * dup2. + * + * Equivalent to the non-standard dup2() function except that it + * accepts struct file instances instead of file descriptors and it does + * not set the errno variable. + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is return on + * any failure. + * + ****************************************************************************/ + +int file_dup2(FAR struct file *filep1, FAR struct file *filep2) +{ + FAR struct inode *inode; + struct file temp; + int ret; + + if (filep1 == NULL || filep1->f_inode == NULL || filep2 == NULL) + { + return -EBADF; + } + + if (filep1 == filep2) + { + return OK; + } + + /* Increment the reference count on the contained inode */ + + inode = filep1->f_inode; + ret = inode_addref(inode); + if (ret < 0) + { + return ret; + } + + /* Then clone the file structure */ + + temp.f_oflags = filep1->f_oflags; + temp.f_pos = filep1->f_pos; + temp.f_inode = inode; + temp.f_priv = NULL; + + /* Call the open method on the file, driver, mountpoint so that it + * can maintain the correct open counts. + */ + + if (inode->u.i_ops && inode->u.i_ops->open) + { +#ifndef CONFIG_DISABLE_MOUNTPOINT + if (INODE_IS_MOUNTPT(inode)) + { + /* Dup the open file on the in the new file structure */ + + ret = inode->u.i_mops->dup(filep1, &temp); + } + else +#endif + { + /* (Re-)open the pseudo file or device driver */ + + ret = inode->u.i_ops->open(&temp); + } + + /* Handle open failures */ + + if (ret < 0) + { + inode_release(inode); + return ret; + } + } + + /* If there is already an inode contained in the new file structure, + * close the file and release the inode. + */ + + ret = file_close(filep2); + DEBUGASSERT(ret == 0); + + /* Return the file structure */ + + memcpy(filep2, &temp, sizeof(temp)); + return OK; +} + /**************************************************************************** * Name: nx_dup2 * @@ -98,7 +190,7 @@ int nx_dup2(int fd1, int fd2) /* Its a valid file descriptor.. dup the file descriptor. */ - return fs_dupfd2(fd1, fd2); + return files_dup2(fd1, fd2); } } diff --git a/fs/vfs/fs_dupfd.c b/fs/vfs/fs_dupfd.c deleted file mode 100644 index fdf73d94cb..0000000000 --- a/fs/vfs/fs_dupfd.c +++ /dev/null @@ -1,111 +0,0 @@ -/**************************************************************************** - * fs/vfs/fs_dupfd.c - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. The - * ASF licenses this file to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include -#include -#include - -#include - -#include "inode/inode.h" - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: file_dup - * - * Description: - * Equivalent to the non-standard fs_dupfd() function except that it - * accepts a struct file instance instead of a file descriptor. - * - * Returned Value: - * The new file descriptor is returned on success; a negated errno value - * is returned on any failure. - * - ****************************************************************************/ - -int file_dup(FAR struct file *filep, int minfd) -{ - struct file filep2; - int fd2; - int ret; - - /* Let file_dup2() do the real work */ - - memset(&filep2, 0, sizeof(filep2)); - ret = file_dup2(filep, &filep2); - if (ret < 0) - { - return ret; - } - - /* Then allocate a new file descriptor for the inode */ - - fd2 = files_allocate(filep2.f_inode, filep2.f_oflags, - filep2.f_pos, filep2.f_priv, minfd); - if (fd2 < 0) - { - file_close(&filep2); - return fd2; - } - - return fd2; -} - -/**************************************************************************** - * Name: fs_dupfd - * - * Description: - * Clone a file descriptor 'fd' to an arbitrary descriptor number (any - * value greater than or equal to 'minfd'). - * - * Returned Value: - * The new file descriptor is returned on success; a negated errno value - * is returned on any failure. - * - ****************************************************************************/ - -int fs_dupfd(int fd, int minfd) -{ - FAR struct file *filep; - int ret; - - /* Get the file structure corresponding to the file descriptor. */ - - ret = fs_getfilep(fd, &filep); - if (ret < 0) - { - return ret; - } - - DEBUGASSERT(filep != NULL); - - /* Let file_dup() do the real work */ - - return file_dup(filep, minfd); -} diff --git a/fs/vfs/fs_dupfd2.c b/fs/vfs/fs_dupfd2.c deleted file mode 100644 index 68fcc544e7..0000000000 --- a/fs/vfs/fs_dupfd2.c +++ /dev/null @@ -1,68 +0,0 @@ -/**************************************************************************** - * fs/vfs/fs_dupfd2.c - * - * Copyright (C) 2007-2009, 2011-2014, 2017 Gregory Nutt. All rights - * reserved. - * Author: Gregory Nutt - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name NuttX nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include -#include -#include - -#include "inode/inode.h" - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: fs_dupfd2 - * - * Description: - * Clone a file descriptor to a specific descriptor number. - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is return on - * any failure. - * - ****************************************************************************/ - -int fs_dupfd2(int fd1, int fd2) -{ - return files_dupfd2(fd1, fd2); -} diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h index 5087ee9e67..4bba51ad24 100644 --- a/include/nuttx/fs/fs.h +++ b/include/nuttx/fs/fs.h @@ -714,7 +714,7 @@ void files_releaselist(FAR struct filelist *list); * Name: file_dup * * Description: - * Equivalent to the non-standard fs_dupfd() function except that it + * Equivalent to the standard dup() function except that it * accepts a struct file instance instead of a file descriptor. * * Returned Value: @@ -725,24 +725,6 @@ void files_releaselist(FAR struct filelist *list); int file_dup(FAR struct file *filep, int minfd); -/**************************************************************************** - * Name: fs_dupfd - * - * Description: - * Clone a file descriptor 'fd' to an arbitrary descriptor number (any - * value greater than or equal to 'minfd'). - * - * This alternative naming is used when dup could operate on both file and - * socket descriptors to avoid drawing unused socket support into the link. - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned on - * any failure. - * - ****************************************************************************/ - -int fs_dupfd(int fd, int minfd); - /**************************************************************************** * Name: nx_dup * @@ -768,7 +750,7 @@ int nx_dup(int fd); * Assign an inode to a specific files structure. This is the heart of * dup2. * - * Equivalent to the non-standard fs_dupfd2() function except that it + * Equivalent to the non-standard dup2() function except that it * accepts struct file instances instead of file descriptors. * * Returned Value: @@ -779,23 +761,6 @@ int nx_dup(int fd); int file_dup2(FAR struct file *filep1, FAR struct file *filep2); -/**************************************************************************** - * Name: fs_dupfd2 - * - * Description: - * Clone a file descriptor to a specific descriptor number. - * - * This alternative naming is used when dup2 could operate on both file and - * socket descriptors to avoid drawing unused socket support into the link. - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is return on - * any failure. - * - ****************************************************************************/ - -int fs_dupfd2(int fd1, int fd2); - /**************************************************************************** * Name: nx_dup2 * diff --git a/sched/group/group_setupidlefiles.c b/sched/group/group_setupidlefiles.c index b1a0a9dd01..ff8e8c54ba 100644 --- a/sched/group/group_setupidlefiles.c +++ b/sched/group/group_setupidlefiles.c @@ -101,8 +101,8 @@ int group_setupidlefiles(FAR struct task_tcb_s *tcb) { /* Successfully opened /dev/console as stdin (fd == 0) */ - fs_dupfd2(0, 1); - fs_dupfd2(0, 2); + nx_dup2(0, 1); + nx_dup2(0, 2); } else {