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 <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>
|
2022-08-05 06:31:25 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2011-04-29 00:24:10 +02:00
|
|
|
|
|
|
|
#include "nxffs.h"
|
2024-09-09 14:28:16 +02:00
|
|
|
#include "fs_heap.h"
|
2011-04-29 00:24:10 +02:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
/****************************************************************************
|
2023-12-03 15:48:00 +01:00
|
|
|
* Private Types
|
2022-08-05 06:31:25 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct nxffs_dir_s
|
|
|
|
{
|
|
|
|
struct fs_dirent_s base;
|
|
|
|
off_t offset;
|
|
|
|
};
|
|
|
|
|
2011-04-29 00:24:10 +02: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 06:31:25 +02:00
|
|
|
FAR struct fs_dirent_s **dir)
|
2011-04-29 00:24:10 +02:00
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct nxffs_volume_s *volume;
|
|
|
|
FAR struct nxffs_dir_s *ndir;
|
2011-04-29 00:24:10 +02:00
|
|
|
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;
|
2024-09-09 14:28:16 +02:00
|
|
|
ndir = fs_heap_zalloc(sizeof(*ndir));
|
2022-08-05 06:31:25 +02:00
|
|
|
if (ndir == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&volume->lock);
|
2017-10-04 23:22:27 +02:00
|
|
|
if (ret < 0)
|
2011-04-29 00:24:10 +02:00
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
goto errout_with_ndir;
|
2011-04-29 00:24:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The requested directory must be the volume-relative "root" directory */
|
|
|
|
|
|
|
|
if (relpath && relpath[0] != '\0')
|
|
|
|
{
|
|
|
|
ret = -ENOENT;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2011-04-29 00:24:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the offset to the offset to the first valid inode */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
ndir->offset = volume->inoffset;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&volume->lock);
|
2022-08-05 06:31:25 +02:00
|
|
|
*dir = &ndir->base;
|
|
|
|
return 0;
|
2011-04-29 00:24:10 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
|
|
|
nxmutex_unlock(&volume->lock);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
errout_with_ndir:
|
2024-09-09 14:28:16 +02:00
|
|
|
fs_heap_free(ndir);
|
2011-04-29 00:24:10 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_closedir
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Close directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_closedir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(dir);
|
2024-09-09 14:28:16 +02:00
|
|
|
fs_heap_free(dir);
|
2022-08-05 06:31:25 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-04-29 00:24:10 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_readdir
|
|
|
|
*
|
|
|
|
* Description: Read the next directory entry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
int nxffs_readdir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir,
|
|
|
|
FAR struct dirent *dentry)
|
2011-04-29 00:24:10 +02:00
|
|
|
{
|
|
|
|
FAR struct nxffs_volume_s *volume;
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct nxffs_dir_s *ndir;
|
2022-07-26 08:56:41 +02:00
|
|
|
struct nxffs_entry_s entry;
|
2011-04-29 00:24:10 +02: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 06:31:25 +02:00
|
|
|
ndir = (FAR struct nxffs_dir_s *)dir;
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&volume->lock);
|
2017-10-04 23:22:27 +02:00
|
|
|
if (ret < 0)
|
2011-04-29 00:24:10 +02:00
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the next inode header from the offset */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
offset = ndir->offset;
|
2011-04-29 00:24:10 +02: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 06:00:05 +01:00
|
|
|
finfo("Offset %jd: \"%s\"\n", (intmax_t)entry.hoffset, entry.name);
|
2022-07-26 08:56:41 +02:00
|
|
|
dentry->d_type = DTYPE_FILE;
|
|
|
|
strlcpy(dentry->d_name, entry.name, sizeof(dentry->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. */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
ndir->offset = nxffs_inodeend(volume, &entry);
|
2011-04-29 00:24:10 +02:00
|
|
|
nxffs_freeentry(&entry);
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&volume->lock);
|
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;
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&volume->lock);
|
2017-10-04 23:22:27 +02:00
|
|
|
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 */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
((FAR struct nxffs_dir_s *)dir)->offset = volume->inoffset;
|
2011-04-29 00:24:10 +02:00
|
|
|
ret = OK;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&volume->lock);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
2011-04-29 00:24:10 +02:00
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|