2007-05-21 00:54:09 +08:00
|
|
|
/****************************************************************************
|
2014-09-29 01:46:11 +08:00
|
|
|
* fs/vfs/fs_close.c
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2021-02-03 21:47:23 +08:00
|
|
|
* 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
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2021-02-03 21:47:23 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2021-02-03 21:47:23 +08:00
|
|
|
* 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.
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2007-05-21 00:54:09 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-05-21 00:54:09 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Included Files
|
2007-05-21 00:54:09 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-12-15 08:18:32 +08:00
|
|
|
|
2007-03-21 00:51:12 +08:00
|
|
|
#include <unistd.h>
|
2007-02-18 07:21:28 +08:00
|
|
|
#include <sched.h>
|
2021-05-18 14:59:14 +08:00
|
|
|
#include <assert.h>
|
2007-02-18 07:21:28 +08:00
|
|
|
#include <errno.h>
|
2023-12-13 23:09:22 +08:00
|
|
|
#include <fcntl.h>
|
2016-12-10 03:49:36 +08:00
|
|
|
|
2012-03-22 02:01:07 +08:00
|
|
|
#include <nuttx/fs/fs.h>
|
2007-09-04 04:34:44 +08:00
|
|
|
|
2014-09-29 21:14:38 +08:00
|
|
|
#include "inode/inode.h"
|
2023-10-23 15:55:00 +08:00
|
|
|
#include "vfs/lock.h"
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-05-21 00:54:09 +08:00
|
|
|
/****************************************************************************
|
2015-10-03 06:30:35 +08:00
|
|
|
* Public Functions
|
2007-05-21 00:54:09 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2021-01-02 23:13:08 +08:00
|
|
|
/****************************************************************************
|
2023-12-13 23:09:22 +08:00
|
|
|
* Name: file_close_without_clear
|
2021-01-02 23:13:08 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2023-12-13 23:09:22 +08:00
|
|
|
* Close a file that was previously opened with file_open(), but without
|
|
|
|
* clear filep.
|
2021-01-02 23:13:08 +08:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* filep - A pointer to a user provided memory location containing the
|
2021-01-03 02:35:06 +08:00
|
|
|
* open file data returned by file_open().
|
2021-01-02 23:13:08 +08:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; A negated errno value is returned on
|
|
|
|
* any failure to indicate the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-12-13 23:09:22 +08:00
|
|
|
int file_close_without_clear(FAR struct file *filep)
|
2021-01-02 23:13:08 +08:00
|
|
|
{
|
|
|
|
struct inode *inode;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
/* Check if the struct file is open (i.e., assigned an inode) */
|
|
|
|
|
|
|
|
if (inode)
|
|
|
|
{
|
2023-10-23 15:55:00 +08:00
|
|
|
file_closelk(filep);
|
|
|
|
|
2021-01-02 23:13:08 +08:00
|
|
|
/* Close the file, driver, or mountpoint. */
|
|
|
|
|
|
|
|
if (inode->u.i_ops && inode->u.i_ops->close)
|
|
|
|
{
|
|
|
|
/* Perform the close operation */
|
|
|
|
|
|
|
|
ret = inode->u.i_ops->close(filep);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And release the inode */
|
|
|
|
|
|
|
|
inode_release(inode);
|
2023-12-13 23:09:22 +08:00
|
|
|
}
|
2021-01-02 23:13:08 +08:00
|
|
|
|
2023-12-13 23:09:22 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: file_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Close a file that was previously opened with file_open().
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* filep - A pointer to a user provided memory location containing the
|
|
|
|
* open file data returned by file_open().
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; A negated errno value is returned on
|
|
|
|
* any failure to indicate the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int file_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = file_close_without_clear(filep);
|
|
|
|
if (ret >= 0 && filep->f_inode)
|
|
|
|
{
|
2021-01-02 23:13:08 +08:00
|
|
|
/* Reset the user file struct instance so that it cannot be reused. */
|
|
|
|
|
2023-12-13 23:09:22 +08:00
|
|
|
filep->f_inode = NULL;
|
|
|
|
|
|
|
|
#ifdef CONFIG_FDCHECK
|
|
|
|
filep->f_tag_fdcheck = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_FDSAN
|
|
|
|
filep->f_tag_fdsan = 0;
|
|
|
|
#endif
|
2021-01-02 23:13:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|