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 <xiaoxiang@xiaomi.com>
Change-Id: I1aacfb9e25dc7b3fcb0345ff7b269b1953a01e5b
This commit is contained in:
Xiang Xiao 2021-01-03 01:50:00 +08:00 committed by archer
parent 1e5bfa623a
commit e49bae1300
9 changed files with 158 additions and 320 deletions

View File

@ -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;

View File

@ -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

View File

@ -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

View File

@ -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
{

View File

@ -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);
}
}

View File

@ -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 <nuttx/config.h>
#include <sched.h>
#include <errno.h>
#include <assert.h>
#include <nuttx/fs/fs.h>
#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);
}

View File

@ -1,68 +0,0 @@
/****************************************************************************
* fs/vfs/fs_dupfd2.c
*
* Copyright (C) 2007-2009, 2011-2014, 2017 Gregory Nutt. All rights
* reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <nuttx/config.h>
#include <unistd.h>
#include <sched.h>
#include <errno.h>
#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);
}

View File

@ -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
*

View File

@ -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
{