2011-04-29 00:24:10 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/nxffs/nxffs_dirent.c
|
|
|
|
*
|
2018-01-04 17:54:54 +01:00
|
|
|
* Copyright (C) 2011, 2017-2018 Gregory Nutt. All rights reserved.
|
2012-09-13 20:32:24 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-04-29 00:24:10 +02:00
|
|
|
*
|
|
|
|
* References: Linux/Documentation/filesystems/romfs.txt
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2020-12-05 06:00:05 +01:00
|
|
|
#include <stdint.h>
|
2011-04-29 00:24:10 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2013-11-15 18:22:23 +01:00
|
|
|
#include <nuttx/mtd/mtd.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/dirent.h>
|
2011-04-29 00:24:10 +02:00
|
|
|
|
|
|
|
#include "nxffs.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_opendir
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Open a directory for read access
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
|
|
|
struct nxffs_volume_s *volume;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("relpath: \"%s\"\n", relpath ? relpath : "NULL");
|
2011-04-29 00:24:10 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
|
|
|
|
|
|
|
|
/* Recover the file system state from the NuttX inode instance */
|
|
|
|
|
|
|
|
volume = mountpt->i_private;
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&volume->exclsem);
|
|
|
|
if (ret < 0)
|
2011-04-29 00:24:10 +02:00
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The requested directory must be the volume-relative "root" directory */
|
|
|
|
|
|
|
|
if (relpath && relpath[0] != '\0')
|
|
|
|
{
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto errout_with_semaphore;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the offset to the offset to the first valid inode */
|
|
|
|
|
|
|
|
dir->u.nxffs.nx_offset = volume->inoffset;
|
|
|
|
ret = OK;
|
|
|
|
|
|
|
|
errout_with_semaphore:
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&volume->exclsem);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
2011-04-29 00:24:10 +02:00
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_readdir
|
|
|
|
*
|
|
|
|
* Description: Read the next directory entry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_readdir(FAR struct inode *mountpt, FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
|
|
|
FAR struct nxffs_volume_s *volume;
|
|
|
|
FAR struct nxffs_entry_s entry;
|
|
|
|
off_t offset;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
|
|
|
|
|
|
|
|
/* Recover the file system state from the NuttX inode instance */
|
|
|
|
|
|
|
|
volume = mountpt->i_private;
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&volume->exclsem);
|
|
|
|
if (ret < 0)
|
2011-04-29 00:24:10 +02:00
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the next inode header from the offset */
|
|
|
|
|
|
|
|
offset = dir->u.nxffs.nx_offset;
|
|
|
|
ret = nxffs_nextentry(volume, offset, &entry);
|
|
|
|
|
|
|
|
/* If the read was successful, then handle the reported inode. Note
|
|
|
|
* that when the last inode has been reported, the value -ENOENT will
|
|
|
|
* be returned.. which is correct for the readdir() method.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Return the filename and file type */
|
|
|
|
|
2020-12-05 06:00:05 +01:00
|
|
|
finfo("Offset %jd: \"%s\"\n", (intmax_t)entry.hoffset, entry.name);
|
2011-04-29 00:24:10 +02:00
|
|
|
dir->fd_dir.d_type = DTYPE_FILE;
|
2019-03-01 22:00:00 +01:00
|
|
|
strncpy(dir->fd_dir.d_name, entry.name, NAME_MAX + 1);
|
2011-04-29 00:24:10 +02:00
|
|
|
|
2011-05-05 20:21:44 +02:00
|
|
|
/* Discard this entry and set the next offset. */
|
|
|
|
|
|
|
|
dir->u.nxffs.nx_offset = nxffs_inodeend(volume, &entry);
|
2011-04-29 00:24:10 +02:00
|
|
|
nxffs_freeentry(&entry);
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&volume->exclsem);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
2011-04-29 00:24:10 +02:00
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_rewindir
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Reset directory read to the first entry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_rewinddir(FAR struct inode *mountpt, FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
|
|
|
FAR struct nxffs_volume_s *volume;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Entry\n");
|
2011-04-29 00:24:10 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
|
|
|
|
|
|
|
|
/* Recover the file system state from the NuttX inode instance */
|
|
|
|
|
|
|
|
volume = mountpt->i_private;
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&volume->exclsem);
|
|
|
|
if (ret < 0)
|
2011-04-29 00:24:10 +02:00
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset the offset to the FLASH offset to the first valid inode */
|
|
|
|
|
|
|
|
dir->u.nxffs.nx_offset = volume->inoffset;
|
|
|
|
ret = OK;
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&volume->exclsem);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
2011-04-29 00:24:10 +02:00
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|