2008-09-12 16:46:44 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/fs_closedir.c
|
2007-03-14 23:41:09 +01:00
|
|
|
*
|
2008-09-12 16:46:44 +02:00
|
|
|
* Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
|
2007-03-14 23:41:09 +01:00
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
|
|
|
* 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.
|
2008-09-12 16:46:44 +02:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-03-14 23:41:09 +01:00
|
|
|
* 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.
|
|
|
|
*
|
2008-09-12 16:46:44 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2008-09-12 16:46:44 +02:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Included Files
|
2008-09-12 16:46:44 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <nuttx/fs.h>
|
|
|
|
#include "fs_internal.h"
|
|
|
|
|
2008-09-12 16:46:44 +02:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Private Functions
|
2008-09-12 16:46:44 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2008-09-12 16:46:44 +02:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Public Functions
|
2008-09-12 16:46:44 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2008-09-12 16:46:44 +02:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Name: seekdir
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The closedir() function closes the directory stream
|
|
|
|
* associated with 'dirp'. The directory stream
|
|
|
|
* descriptor 'dirp' is not available after this call.
|
|
|
|
*
|
|
|
|
* Inputs:
|
|
|
|
* dirp -- An instance of type DIR created by a previous
|
|
|
|
* call to opendir();
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* The closedir() function returns 0 on success. On error,
|
|
|
|
* -1 is returned, and errno is set appropriately.
|
|
|
|
*
|
2008-09-12 16:46:44 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
|
|
|
int closedir(FAR DIR *dirp)
|
|
|
|
{
|
|
|
|
struct internal_dir_s *idir = (struct internal_dir_s *)dirp;
|
2007-05-26 21:22:34 +02:00
|
|
|
struct inode *inode;
|
|
|
|
int ret;
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2007-05-26 21:22:34 +02:00
|
|
|
if (!idir || !idir->fd_root)
|
2007-03-14 23:41:09 +01:00
|
|
|
{
|
2007-05-27 20:08:18 +02:00
|
|
|
ret = EBADF;
|
|
|
|
goto errout;
|
2007-03-14 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
2007-05-27 20:08:18 +02:00
|
|
|
/* This is the 'root' inode of the directory. This means different
|
|
|
|
* things wih different filesystems.
|
|
|
|
*/
|
|
|
|
|
|
|
|
inode = idir->fd_root;
|
|
|
|
|
2007-05-26 18:05:59 +02:00
|
|
|
/* The way that we handle the close operation depends on what kind of root
|
|
|
|
* inode we have open.
|
|
|
|
*/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2007-06-09 21:11:00 +02:00
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
2007-05-27 20:08:18 +02:00
|
|
|
if (INODE_IS_MOUNTPT(inode) && !DIRENT_ISPSUEDONODE(idir->fd_flags))
|
2007-03-14 23:41:09 +01:00
|
|
|
{
|
2007-05-26 21:22:34 +02:00
|
|
|
/* The node is a file system mointpoint. Verify that the mountpoint
|
|
|
|
* supports the closedir() method (not an error if it does not)
|
|
|
|
*/
|
2007-05-26 18:05:59 +02:00
|
|
|
|
2007-05-26 21:22:34 +02:00
|
|
|
if (inode->u.i_mops && inode->u.i_mops->closedir)
|
|
|
|
{
|
|
|
|
/* Perform the closedir() operation */
|
|
|
|
|
|
|
|
ret = inode->u.i_mops->closedir(inode, idir);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2007-05-27 20:08:18 +02:00
|
|
|
ret = -ret;
|
|
|
|
goto errout_with_inode;
|
2007-05-26 21:22:34 +02:00
|
|
|
}
|
|
|
|
}
|
2007-05-26 18:05:59 +02:00
|
|
|
}
|
|
|
|
else
|
2007-05-26 21:22:34 +02:00
|
|
|
#endif
|
2007-05-26 18:05:59 +02:00
|
|
|
{
|
|
|
|
/* The node is part of the root psuedo file system, release
|
|
|
|
* our contained reference to the 'next' inode.
|
|
|
|
*/
|
|
|
|
|
2007-05-26 21:22:34 +02:00
|
|
|
if (idir->u.psuedo.fd_next)
|
2007-05-26 18:05:59 +02:00
|
|
|
{
|
2007-05-26 21:22:34 +02:00
|
|
|
inode_release(idir->u.psuedo.fd_next);
|
2007-05-26 18:05:59 +02:00
|
|
|
}
|
2007-03-14 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
2007-05-26 18:05:59 +02:00
|
|
|
/* Release our references on the contained 'root' inode */
|
|
|
|
|
2007-05-27 20:08:18 +02:00
|
|
|
inode_release(idir->fd_root);
|
2007-03-14 23:41:09 +01:00
|
|
|
|
|
|
|
/* Then release the container */
|
|
|
|
|
|
|
|
free(idir);
|
|
|
|
return OK;
|
2007-05-27 20:08:18 +02:00
|
|
|
|
2007-12-02 19:18:59 +01:00
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
2007-05-27 20:08:18 +02:00
|
|
|
errout_with_inode:
|
|
|
|
inode_release(inode);
|
|
|
|
free(idir);
|
2007-12-02 19:18:59 +01:00
|
|
|
#endif
|
2007-05-27 20:08:18 +02:00
|
|
|
|
|
|
|
errout:
|
|
|
|
*get_errno_ptr() = ret;
|
|
|
|
return ERROR;
|
2007-03-14 23:41:09 +01:00
|
|
|
}
|
|
|
|
|