2011-04-29 00:24:10 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/nxffs/nxffs_dirent.c
|
|
|
|
*
|
2021-02-03 14:47:23 +01: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
|
2011-04-29 00:24:10 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-29 00:24:10 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01: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.
|
2011-04-29 00:24:10 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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;
|
2021-12-29 13:17:34 +01:00
|
|
|
strlcpy(dir->fd_dir.d_name, entry.name, sizeof(dir->fd_dir.d_name));
|
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;
|
|
|
|
}
|