2011-04-29 06:24:10 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/nxffs/nxffs_dirent.c
|
|
|
|
*
|
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
|
2011-04-29 06:24:10 +08:00
|
|
|
*
|
2021-02-03 21:47:23 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-29 06:24:10 +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.
|
2011-04-29 06:24:10 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2020-12-05 13:00:05 +08:00
|
|
|
#include <stdint.h>
|
2011-04-29 06:24:10 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2012-03-22 02:01:07 +08:00
|
|
|
#include <nuttx/fs/fs.h>
|
2013-11-16 01:22:23 +08:00
|
|
|
#include <nuttx/mtd/mtd.h>
|
2022-08-05 12:31:25 +08:00
|
|
|
#include <nuttx/kmalloc.h>
|
2011-04-29 06:24:10 +08:00
|
|
|
|
|
|
|
#include "nxffs.h"
|
|
|
|
|
2022-08-05 12:31:25 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Type
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct nxffs_dir_s
|
|
|
|
{
|
|
|
|
struct fs_dirent_s base;
|
|
|
|
off_t offset;
|
|
|
|
};
|
|
|
|
|
2011-04-29 06:24:10 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_opendir
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Open a directory for read access
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
|
2022-08-05 12:31:25 +08:00
|
|
|
FAR struct fs_dirent_s **dir)
|
2011-04-29 06:24:10 +08:00
|
|
|
{
|
2022-08-05 12:31:25 +08:00
|
|
|
FAR struct nxffs_volume_s *volume;
|
|
|
|
FAR struct nxffs_dir_s *ndir;
|
2011-04-29 06:24:10 +08:00
|
|
|
int ret;
|
|
|
|
|
2016-06-12 01:59:51 +08:00
|
|
|
finfo("relpath: \"%s\"\n", relpath ? relpath : "NULL");
|
2011-04-29 06:24:10 +08:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
|
|
|
|
|
|
|
|
/* Recover the file system state from the NuttX inode instance */
|
|
|
|
|
|
|
|
volume = mountpt->i_private;
|
2022-08-05 12:31:25 +08:00
|
|
|
ndir = kmm_zalloc(sizeof(*ndir));
|
|
|
|
if (ndir == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
ret = nxmutex_lock(&volume->lock);
|
2017-10-05 05:22:27 +08:00
|
|
|
if (ret < 0)
|
2011-04-29 06:24:10 +08:00
|
|
|
{
|
2022-08-05 12:31:25 +08:00
|
|
|
goto errout_with_ndir;
|
2011-04-29 06:24:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The requested directory must be the volume-relative "root" directory */
|
|
|
|
|
|
|
|
if (relpath && relpath[0] != '\0')
|
|
|
|
{
|
|
|
|
ret = -ENOENT;
|
2022-09-06 14:18:45 +08:00
|
|
|
goto errout_with_lock;
|
2011-04-29 06:24:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the offset to the offset to the first valid inode */
|
|
|
|
|
2022-08-05 12:31:25 +08:00
|
|
|
ndir->offset = volume->inoffset;
|
2022-09-06 14:18:45 +08:00
|
|
|
nxmutex_unlock(&volume->lock);
|
2022-08-05 12:31:25 +08:00
|
|
|
*dir = &ndir->base;
|
|
|
|
return 0;
|
2011-04-29 06:24:10 +08:00
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
errout_with_lock:
|
|
|
|
nxmutex_unlock(&volume->lock);
|
2017-10-05 05:22:27 +08:00
|
|
|
|
2022-08-05 12:31:25 +08:00
|
|
|
errout_with_ndir:
|
|
|
|
kmm_free(ndir);
|
2011-04-29 06:24:10 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-05 12:31:25 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_closedir
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Close directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_closedir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(dir);
|
|
|
|
kmm_free(dir);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-04-29 06:24:10 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_readdir
|
|
|
|
*
|
|
|
|
* Description: Read the next directory entry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-07-26 14:56:41 +08:00
|
|
|
int nxffs_readdir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir,
|
|
|
|
FAR struct dirent *dentry)
|
2011-04-29 06:24:10 +08:00
|
|
|
{
|
|
|
|
FAR struct nxffs_volume_s *volume;
|
2022-08-05 12:31:25 +08:00
|
|
|
FAR struct nxffs_dir_s *ndir;
|
2022-07-26 14:56:41 +08:00
|
|
|
struct nxffs_entry_s entry;
|
2011-04-29 06:24:10 +08:00
|
|
|
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;
|
2022-08-05 12:31:25 +08:00
|
|
|
ndir = (FAR struct nxffs_dir_s *)dir;
|
2022-09-06 14:18:45 +08:00
|
|
|
ret = nxmutex_lock(&volume->lock);
|
2017-10-05 05:22:27 +08:00
|
|
|
if (ret < 0)
|
2011-04-29 06:24:10 +08:00
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the next inode header from the offset */
|
|
|
|
|
2022-08-05 12:31:25 +08:00
|
|
|
offset = ndir->offset;
|
2011-04-29 06:24:10 +08:00
|
|
|
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 13:00:05 +08:00
|
|
|
finfo("Offset %jd: \"%s\"\n", (intmax_t)entry.hoffset, entry.name);
|
2022-07-26 14:56:41 +08:00
|
|
|
dentry->d_type = DTYPE_FILE;
|
|
|
|
strlcpy(dentry->d_name, entry.name, sizeof(dentry->d_name));
|
2011-04-29 06:24:10 +08:00
|
|
|
|
2011-05-06 02:21:44 +08:00
|
|
|
/* Discard this entry and set the next offset. */
|
|
|
|
|
2022-08-05 12:31:25 +08:00
|
|
|
ndir->offset = nxffs_inodeend(volume, &entry);
|
2011-04-29 06:24:10 +08:00
|
|
|
nxffs_freeentry(&entry);
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
nxmutex_unlock(&volume->lock);
|
2017-10-05 05:22:27 +08:00
|
|
|
|
2011-04-29 06:24:10 +08: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-12 01:59:51 +08:00
|
|
|
finfo("Entry\n");
|
2011-04-29 06:24:10 +08:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
|
|
|
|
|
|
|
|
/* Recover the file system state from the NuttX inode instance */
|
|
|
|
|
|
|
|
volume = mountpt->i_private;
|
2022-09-06 14:18:45 +08:00
|
|
|
ret = nxmutex_lock(&volume->lock);
|
2017-10-05 05:22:27 +08:00
|
|
|
if (ret < 0)
|
2011-04-29 06:24:10 +08:00
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset the offset to the FLASH offset to the first valid inode */
|
|
|
|
|
2022-08-05 12:31:25 +08:00
|
|
|
((FAR struct nxffs_dir_s *)dir)->offset = volume->inoffset;
|
2011-04-29 06:24:10 +08:00
|
|
|
ret = OK;
|
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
nxmutex_unlock(&volume->lock);
|
2017-10-05 05:22:27 +08:00
|
|
|
|
2011-04-29 06:24:10 +08:00
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|