nuttx/fs: Reorganize the code for close, dup, et. al.

Currently the code is dumped into one massive file; fs_files. Move the
different logical parts into their own files.

Signed-off-by: Ville Juven <ville.juven@unikie.com>
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
Ville Juven 2025-06-03 15:56:08 +08:00 committed by Xiang Xiao
parent 3fe1d1a54d
commit 1e787ea280
7 changed files with 215 additions and 154 deletions

View file

@ -46,10 +46,6 @@
#include <nuttx/spinlock.h>
#include <nuttx/lib/lib.h>
#ifdef CONFIG_FDSAN
# include <android/fdsan.h>
#endif
#ifdef CONFIG_FDCHECK
# include <nuttx/fdcheck.h>
#endif
@ -246,6 +242,10 @@ static void task_fssync(FAR struct tcb_s *tcb, FAR void *arg)
}
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nx_dup3_from_tcb
*
@ -266,8 +266,7 @@ static void task_fssync(FAR struct tcb_s *tcb, FAR void *arg)
*
****************************************************************************/
static int nx_dup3_from_tcb(FAR struct tcb_s *tcb, int fd1, int fd2,
int flags)
int nx_dup3_from_tcb(FAR struct tcb_s *tcb, int fd1, int fd2, int flags)
{
FAR struct filelist *list;
FAR struct file *filep1;
@ -360,10 +359,6 @@ static int nx_dup3_from_tcb(FAR struct tcb_s *tcb, int fd1, int fd2,
#endif
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: files_initlist
*
@ -883,75 +878,6 @@ int nx_dup2_from_tcb(FAR struct tcb_s *tcb, int fd1, int fd2)
return nx_dup3_from_tcb(tcb, fd1, fd2, 0);
}
/****************************************************************************
* Name: nx_dup2
*
* Description:
* nx_dup2() is similar to the standard 'dup2' interface except that is
* not a cancellation point and it does not modify the errno variable.
*
* nx_dup2() is an internal NuttX interface and should not be called from
* applications.
*
* Clone a file descriptor to a specific descriptor number.
*
* Returned Value:
* fd2 is returned on success; a negated errno value is return on
* any failure.
*
****************************************************************************/
int nx_dup2(int fd1, int fd2)
{
return nx_dup2_from_tcb(this_task(), fd1, fd2);
}
/****************************************************************************
* Name: dup2
*
* Description:
* Clone a file descriptor or socket descriptor to a specific descriptor
* number
*
****************************************************************************/
int dup2(int fd1, int fd2)
{
int ret;
ret = nx_dup2(fd1, fd2);
if (ret < 0)
{
set_errno(-ret);
ret = ERROR;
}
return ret;
}
/****************************************************************************
* Name: dup3
*
* Description:
* Clone a file descriptor or socket descriptor to a specific descriptor
* number and specific flags.
*
****************************************************************************/
int dup3(int fd1, int fd2, int flags)
{
int ret;
ret = nx_dup3_from_tcb(this_task(), fd1, fd2, flags);
if (ret < 0)
{
set_errno(-ret);
ret = ERROR;
}
return ret;
}
/****************************************************************************
* Name: nx_close_from_tcb
*
@ -1018,79 +944,6 @@ int nx_close_from_tcb(FAR struct tcb_s *tcb, int fd)
#endif
}
/****************************************************************************
* Name: nx_close
*
* Description:
* nx_close() is similar to the standard 'close' interface except that is
* not a cancellation point and it does not modify the errno variable.
*
* nx_close() is an internal NuttX interface and should not be called from
* applications.
*
* Close an inode (if open)
*
* Returned Value:
* Zero (OK) is returned on success; A negated errno value is returned on
* on any failure.
*
* Assumptions:
* Caller holds the list mutex because the file descriptor will be
* freed.
*
****************************************************************************/
int nx_close(int fd)
{
return nx_close_from_tcb(this_task(), fd);
}
/****************************************************************************
* Name: close
*
* Description:
* close() closes a file descriptor, so that it no longer refers to any
* file and may be reused. Any record locks (see fcntl(2)) held on the file
* it was associated with, and owned by the process, are removed
* (regardless of the file descriptor that was used to obtain the lock).
*
* If fd is the last copy of a particular file descriptor the resources
* associated with it are freed; if the descriptor was the last reference
* to a file which has been removed using unlink(2) the file is deleted.
*
* Input Parameters:
* fd file descriptor to close
*
* Returned Value:
* 0 on success; -1 on error with errno set appropriately.
*
* Assumptions:
*
****************************************************************************/
int close(int fd)
{
int ret;
#ifdef CONFIG_FDSAN
android_fdsan_exchange_owner_tag(fd, 0, 0);
#endif
/* close() is a cancellation point */
enter_cancellation_point();
ret = nx_close(fd);
if (ret < 0)
{
set_errno(-ret);
ret = ERROR;
}
leave_cancellation_point();
return ret;
}
/****************************************************************************
* Name: sync
*

View file

@ -25,6 +25,7 @@ set(SRCS
fs_close.c
fs_dup.c
fs_dup2.c
fs_dup3.c
fs_fcntl.c
fs_epoll.c
fs_fchstat.c

View file

@ -22,8 +22,8 @@
# Common file/socket descriptor support
CSRCS += fs_chstat.c fs_close.c fs_dup.c fs_dup2.c fs_fcntl.c fs_epoll.c
CSRCS += fs_fchstat.c fs_fstat.c fs_fstatfs.c fs_ioctl.c fs_lseek.c
CSRCS += fs_chstat.c fs_close.c fs_dup.c fs_dup2.c fs_dup3.c fs_fcntl.c
CSRCS += fs_epoll.c fs_fchstat.c fs_fstat.c fs_fstatfs.c fs_ioctl.c fs_lseek.c
CSRCS += fs_mkdir.c fs_open.c fs_poll.c fs_pread.c fs_pwrite.c fs_read.c
CSRCS += fs_rename.c fs_rmdir.c fs_select.c fs_sendfile.c fs_stat.c
CSRCS += fs_statfs.c fs_uio.c fs_unlink.c fs_write.c fs_dir.c fs_fsync.c

View file

@ -32,10 +32,16 @@
#include <errno.h>
#include <fcntl.h>
#include <nuttx/cancelpt.h>
#include <nuttx/fs/fs.h>
#ifdef CONFIG_FDSAN
# include <android/fdsan.h>
#endif
#include "notify/notify.h"
#include "inode/inode.h"
#include "sched/sched.h"
#include "vfs/lock.h"
/****************************************************************************
@ -177,3 +183,76 @@ int file_close(FAR struct file *filep)
return ret;
}
/****************************************************************************
* Name: nx_close
*
* Description:
* nx_close() is similar to the standard 'close' interface except that is
* not a cancellation point and it does not modify the errno variable.
*
* nx_close() is an internal NuttX interface and should not be called from
* applications.
*
* Close an inode (if open)
*
* Returned Value:
* Zero (OK) is returned on success; A negated errno value is returned on
* on any failure.
*
* Assumptions:
* Caller holds the list mutex because the file descriptor will be
* freed.
*
****************************************************************************/
int nx_close(int fd)
{
return nx_close_from_tcb(this_task(), fd);
}
/****************************************************************************
* Name: close
*
* Description:
* close() closes a file descriptor, so that it no longer refers to any
* file and may be reused. Any record locks (see fcntl(2)) held on the file
* it was associated with, and owned by the process, are removed
* (regardless of the file descriptor that was used to obtain the lock).
*
* If fd is the last copy of a particular file descriptor the resources
* associated with it are freed; if the descriptor was the last reference
* to a file which has been removed using unlink(2) the file is deleted.
*
* Input Parameters:
* fd file descriptor to close
*
* Returned Value:
* 0 on success; -1 on error with errno set appropriately.
*
* Assumptions:
*
****************************************************************************/
int close(int fd)
{
int ret;
#ifdef CONFIG_FDSAN
android_fdsan_exchange_owner_tag(fd, 0, 0);
#endif
/* close() is a cancellation point */
enter_cancellation_point();
ret = nx_close(fd);
if (ret < 0)
{
set_errno(-ret);
ret = ERROR;
}
leave_cancellation_point();
return ret;
}

View file

@ -35,6 +35,7 @@
#include <fcntl.h>
#include "inode/inode.h"
#include "sched/sched.h"
/****************************************************************************
* Public Functions
@ -197,3 +198,49 @@ int file_dup2(FAR struct file *filep1, FAR struct file *filep2)
{
return file_dup3(filep1, filep2, 0);
}
/****************************************************************************
* Name: nx_dup2
*
* Description:
* nx_dup2() is similar to the standard 'dup2' interface except that is
* not a cancellation point and it does not modify the errno variable.
*
* nx_dup2() is an internal NuttX interface and should not be called from
* applications.
*
* Clone a file descriptor to a specific descriptor number.
*
* Returned Value:
* fd2 is returned on success; a negated errno value is return on
* any failure.
*
****************************************************************************/
int nx_dup2(int fd1, int fd2)
{
return nx_dup2_from_tcb(this_task(), fd1, fd2);
}
/****************************************************************************
* Name: dup2
*
* Description:
* Clone a file descriptor or socket descriptor to a specific descriptor
* number
*
****************************************************************************/
int dup2(int fd1, int fd2)
{
int ret;
ret = nx_dup2(fd1, fd2);
if (ret < 0)
{
set_errno(-ret);
ret = ERROR;
}
return ret;
}

60
fs/vfs/fs_dup3.c Normal file
View file

@ -0,0 +1,60 @@
/****************************************************************************
* fs/vfs/fs_dup3.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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/fs/fs.h>
#include <unistd.h>
#include <sched.h>
#include <errno.h>
#include "sched/sched.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: dup3
*
* Description:
* Clone a file descriptor or socket descriptor to a specific descriptor
* number and specific flags.
*
****************************************************************************/
int dup3(int fd1, int fd2, int flags)
{
int ret;
ret = nx_dup3_from_tcb(this_task(), fd1, fd2, flags);
if (ret < 0)
{
set_errno(-ret);
ret = ERROR;
}
return ret;
}

View file

@ -1027,6 +1027,27 @@ int file_dup(FAR struct file *filep, int minfd, int flags);
int file_dup2(FAR struct file *filep1, FAR struct file *filep2);
/****************************************************************************
* Name: nx_dup3_from_tcb
*
* Description:
* nx_dup3_from_tcb() is similar to the standard 'dup3' interface
* except that is not a cancellation point and it does not modify the
* errno variable.
*
* nx_dup3_from_tcb() is an internal NuttX interface and should not be
* called from applications.
*
* Clone a file descriptor to a specific descriptor number.
*
* Returned Value:
* fd2 is returned on success; a negated errno value is return on
* any failure.
*
****************************************************************************/
int nx_dup3_from_tcb(FAR struct tcb_s *tcb, int fd1, int fd2, int flags);
/****************************************************************************
* Name: nx_dup2_from_tcb
*