2022-07-26 08:53:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/vfs/fs_dir.c
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2023-02-02 07:20:22 +01:00
|
|
|
#include <stdio.h>
|
2022-07-26 08:53:39 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2023-02-08 04:29:09 +01:00
|
|
|
#include <limits.h>
|
2022-07-26 08:53:39 +02:00
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
2022-08-05 06:04:37 +02:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
2022-07-26 08:53:39 +02:00
|
|
|
|
|
|
|
#include "inode/inode.h"
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* For the root pseudo-file system, we need retain only the 'next' inode
|
|
|
|
* need for the next readdir() operation. We hold a reference on this
|
|
|
|
* inode so we know that it will persist until closedir is called.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct fs_pseudodir_s
|
|
|
|
{
|
|
|
|
struct fs_dirent_s dir;
|
|
|
|
FAR struct inode *next;
|
|
|
|
};
|
|
|
|
|
2022-07-26 08:53:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-08-05 06:04:37 +02:00
|
|
|
static int dir_open(FAR struct file *filep);
|
2022-07-26 08:53:39 +02:00
|
|
|
static int dir_close(FAR struct file *filep);
|
|
|
|
static ssize_t dir_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static off_t dir_seek(FAR struct file *filep, off_t offset, int whence);
|
2022-08-05 06:04:37 +02:00
|
|
|
static int dir_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
|
2022-07-26 08:53:39 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_dir_fileops =
|
|
|
|
{
|
2022-08-05 06:04:37 +02:00
|
|
|
dir_open, /* open */
|
2022-07-26 08:53:39 +02:00
|
|
|
dir_close, /* close */
|
|
|
|
dir_read, /* read */
|
|
|
|
NULL, /* write */
|
|
|
|
dir_seek, /* seek */
|
2022-08-05 06:04:37 +02:00
|
|
|
dir_ioctl, /* ioctl */
|
2022-07-26 08:53:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct inode g_dir_inode =
|
|
|
|
{
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
{ &g_dir_fileops },
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: open_mountpoint
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle the case where the inode to be opened is within a mountpoint.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* inode -- the inode of the mountpoint to open
|
|
|
|
* relpath -- the relative path within the mountpoint to open
|
|
|
|
* dir -- the dirent structure to be initialized
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, 0 is returned; Otherwise, a negative errno is returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
static int open_mountpoint(FAR struct inode *inode, FAR const char *relpath,
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct fs_dirent_s **dir)
|
2022-07-26 08:53:39 +02:00
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
int ret;
|
|
|
|
|
2022-07-26 08:53:39 +02:00
|
|
|
/* The inode itself as the 'root' of mounted volume. The actually
|
|
|
|
* directory is at relpath into the mounted filesystem.
|
|
|
|
*
|
|
|
|
* Verify that the mountpoint inode supports the opendir() method.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (inode->u.i_mops == NULL || inode->u.i_mops->opendir == NULL ||
|
|
|
|
inode->u.i_mops->readdir == NULL)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
ret = inode->u.i_mops->opendir(inode, relpath, dir);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
(*dir)->fd_root = inode;
|
|
|
|
}
|
2022-07-26 08:53:39 +02:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
return ret;
|
2022-07-26 08:53:39 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: open_pseudodir
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle the case where the inode to be opened is within the top-level
|
|
|
|
* pseudo-file system.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* inode -- the inode of the mountpoint to open
|
|
|
|
* dir -- the dirent structure to be initialized
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2022-08-05 06:31:25 +02:00
|
|
|
* On success, 0 is returned; Otherwise, a negative errno is returned.
|
2022-07-26 08:53:39 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
static int open_pseudodir(FAR struct inode *inode,
|
|
|
|
FAR struct fs_dirent_s **dir)
|
2022-07-26 08:53:39 +02:00
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct fs_pseudodir_s *pdir;
|
|
|
|
|
|
|
|
pdir = kmm_zalloc(sizeof(*pdir));
|
|
|
|
if (pdir == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dir = &pdir->dir;
|
|
|
|
pdir->dir.fd_root = inode; /* Save the inode where we start */
|
|
|
|
pdir->next = inode->i_child; /* This next node for readdir */
|
2022-07-26 08:53:39 +02:00
|
|
|
inode_addref(inode->i_child);
|
2022-08-05 06:31:25 +02:00
|
|
|
return 0;
|
2022-07-26 08:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: seek_pseudodir
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static off_t seek_pseudodir(FAR struct file *filep, off_t offset)
|
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct fs_pseudodir_s *pdir = filep->f_priv;
|
2022-07-26 08:53:39 +02:00
|
|
|
FAR struct inode *curr;
|
|
|
|
FAR struct inode *prev;
|
|
|
|
off_t pos;
|
|
|
|
|
|
|
|
/* Determine a starting point for the seek. If the seek
|
|
|
|
* is "forward" from the current position, then we will
|
|
|
|
* start at the current position. Otherwise, we will
|
|
|
|
* "rewind" to the root dir.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (offset < filep->f_pos)
|
|
|
|
{
|
|
|
|
pos = 0;
|
2022-08-05 06:31:25 +02:00
|
|
|
curr = pdir->dir.fd_root->i_child;
|
2022-07-26 08:53:39 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos = filep->f_pos;
|
2022-08-05 06:31:25 +02:00
|
|
|
curr = pdir->next;
|
2022-07-26 08:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Traverse the peer list starting at the 'root' of the
|
|
|
|
* the list until we find the node at 'offset". If devices
|
|
|
|
* are being registered and unregistered, then this can
|
|
|
|
* be a very unpredictable operation.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
inode_lock();
|
2022-07-26 08:53:39 +02:00
|
|
|
|
|
|
|
for (; curr != NULL && pos != offset; pos++, curr = curr->i_peer);
|
|
|
|
|
|
|
|
/* Now get the inode to vist next time that readdir() is called */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
prev = pdir->next;
|
2022-07-26 08:53:39 +02:00
|
|
|
|
|
|
|
/* The next node to visit (might be null) */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
pdir->next = curr;
|
2022-07-26 08:53:39 +02:00
|
|
|
if (curr != NULL)
|
|
|
|
{
|
|
|
|
/* Increment the reference count on this next node */
|
|
|
|
|
|
|
|
curr->i_crefs++;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
inode_unlock();
|
2022-07-26 08:53:39 +02:00
|
|
|
|
|
|
|
if (prev != NULL)
|
|
|
|
{
|
|
|
|
inode_release(prev);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: seek_mountptdir
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
static off_t seek_mountptdir(FAR struct file *filep, off_t offset)
|
|
|
|
{
|
|
|
|
FAR struct fs_dirent_s *dir = filep->f_priv;
|
|
|
|
FAR struct inode *inode = dir->fd_root;
|
2022-07-26 08:56:41 +02:00
|
|
|
struct dirent entry;
|
2022-07-26 08:53:39 +02:00
|
|
|
off_t pos;
|
|
|
|
|
|
|
|
/* Determine a starting point for the seek. If the seek
|
|
|
|
* is "forward" from the current position, then we will
|
|
|
|
* start at the current position. Otherwise, we will
|
|
|
|
* "rewind" to the root dir.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (offset < filep->f_pos)
|
|
|
|
{
|
|
|
|
if (inode->u.i_mops->rewinddir != NULL)
|
|
|
|
{
|
|
|
|
inode->u.i_mops->rewinddir(inode, dir);
|
|
|
|
pos = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos = filep->f_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is a brute force approach... we will just read
|
|
|
|
* directory entries until we are at the desired position.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (pos < offset)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
ret = inode->u.i_mops->readdir(inode, dir, &entry);
|
2022-07-26 08:53:39 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment the position on each successful read */
|
|
|
|
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: read_pseudodir
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
static int read_pseudodir(FAR struct fs_dirent_s *dir,
|
|
|
|
FAR struct dirent *entry)
|
2022-07-26 08:53:39 +02:00
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct fs_pseudodir_s *pdir = (FAR struct fs_pseudodir_s *)dir;
|
2022-07-26 08:53:39 +02:00
|
|
|
FAR struct inode *prev;
|
|
|
|
|
|
|
|
/* Check if we are at the end of the list */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
if (pdir->next == NULL)
|
2022-07-26 08:53:39 +02:00
|
|
|
{
|
|
|
|
/* End of file and error conditions are not distinguishable with
|
|
|
|
* readdir. Here we return -ENOENT to signal the end of the directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the inode name into the dirent structure */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
strlcpy(entry->d_name, pdir->next->i_name, sizeof(entry->d_name));
|
2022-07-26 08:53:39 +02:00
|
|
|
|
|
|
|
/* If the node has file operations, we will say that it is a file. */
|
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_UNKNOWN;
|
2022-08-05 06:31:25 +02:00
|
|
|
if (pdir->next->u.i_ops != NULL)
|
2022-07-26 08:53:39 +02:00
|
|
|
{
|
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
2022-08-05 06:31:25 +02:00
|
|
|
if (INODE_IS_BLOCK(pdir->next))
|
2022-07-26 08:53:39 +02:00
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_BLK;
|
2022-07-26 08:53:39 +02:00
|
|
|
}
|
2022-08-05 06:31:25 +02:00
|
|
|
else if (INODE_IS_MTD(pdir->next))
|
2022-07-26 08:53:39 +02:00
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_MTD;
|
2022-07-26 08:53:39 +02:00
|
|
|
}
|
2022-08-05 06:31:25 +02:00
|
|
|
else if (INODE_IS_MOUNTPT(pdir->next))
|
2022-07-26 08:53:39 +02:00
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_DIRECTORY;
|
2022-07-26 08:53:39 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
2022-08-05 06:31:25 +02:00
|
|
|
if (INODE_IS_SOFTLINK(pdir->next))
|
2022-07-26 08:53:39 +02:00
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_LINK;
|
2022-07-26 08:53:39 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2022-08-05 06:31:25 +02:00
|
|
|
if (INODE_IS_DRIVER(pdir->next))
|
2022-07-26 08:53:39 +02:00
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_CHR;
|
2022-07-26 08:53:39 +02:00
|
|
|
}
|
2022-08-05 06:31:25 +02:00
|
|
|
else if (INODE_IS_NAMEDSEM(pdir->next))
|
2022-07-26 08:53:39 +02:00
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_SEM;
|
2022-07-26 08:53:39 +02:00
|
|
|
}
|
2022-08-05 06:31:25 +02:00
|
|
|
else if (INODE_IS_MQUEUE(pdir->next))
|
2022-07-26 08:53:39 +02:00
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_MQ;
|
2022-07-26 08:53:39 +02:00
|
|
|
}
|
2022-08-05 06:31:25 +02:00
|
|
|
else if (INODE_IS_SHM(pdir->next))
|
2022-07-26 08:53:39 +02:00
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_SHM;
|
2022-07-26 08:53:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the node has child node(s) or no operations, then we will say that
|
|
|
|
* it is a directory rather than a special file. NOTE: that the node can
|
|
|
|
* be both!
|
|
|
|
*/
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
if (pdir->next->i_child != NULL ||
|
|
|
|
pdir->next->u.i_ops == NULL)
|
2022-07-26 08:53:39 +02:00
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_DIRECTORY;
|
2022-07-26 08:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now get the inode to visit next time that readdir() is called */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
inode_lock();
|
2022-07-26 08:53:39 +02:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
prev = pdir->next;
|
|
|
|
pdir->next = prev->i_peer; /* The next node to visit */
|
2022-07-26 08:53:39 +02:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
if (pdir->next != NULL)
|
2022-07-26 08:53:39 +02:00
|
|
|
{
|
|
|
|
/* Increment the reference count on this next node */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
pdir->next->i_crefs++;
|
2022-07-26 08:53:39 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
inode_unlock();
|
2022-07-26 08:53:39 +02:00
|
|
|
|
|
|
|
if (prev != NULL)
|
|
|
|
{
|
|
|
|
inode_release(prev);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-05 06:04:37 +02:00
|
|
|
static int dir_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct fs_dirent_s *dir = filep->f_priv;
|
|
|
|
|
|
|
|
return dir_allocate(filep, dir->fd_path);
|
|
|
|
}
|
|
|
|
|
2022-07-26 08:53:39 +02:00
|
|
|
static int dir_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct fs_dirent_s *dir = filep->f_priv;
|
|
|
|
FAR struct inode *inode = dir->fd_root;
|
2022-08-05 06:04:37 +02:00
|
|
|
FAR char *relpath = dir->fd_path;
|
2022-07-26 08:53:39 +02:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* This is the 'root' inode of the directory. This means different
|
|
|
|
* things with different filesystems.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
/* The way that we handle the close operation depends on what kind of
|
|
|
|
* root inode we have open.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (INODE_IS_MOUNTPT(inode))
|
|
|
|
{
|
|
|
|
/* The node is a file system mointpoint. Verify that the
|
|
|
|
* mountpoint supports the closedir() method (not an error if it
|
|
|
|
* does not)
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (inode->u.i_mops->closedir != NULL)
|
|
|
|
{
|
|
|
|
ret = inode->u.i_mops->closedir(inode, dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct fs_pseudodir_s *pdir = filep->f_priv;
|
|
|
|
|
2022-07-26 08:53:39 +02:00
|
|
|
/* The node is part of the root pseudo file system, release
|
|
|
|
* our contained reference to the 'next' inode.
|
|
|
|
*/
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
if (pdir->next != NULL)
|
2022-07-26 08:53:39 +02:00
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
inode_release(pdir->next);
|
2022-07-26 08:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then release the container */
|
|
|
|
|
|
|
|
kmm_free(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release our references on the contained 'root' inode */
|
|
|
|
|
|
|
|
inode_release(inode);
|
2023-03-23 07:20:22 +01:00
|
|
|
lib_free(relpath);
|
2022-07-26 08:53:39 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t dir_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct fs_dirent_s *dir = filep->f_priv;
|
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
FAR struct inode *inode = dir->fd_root;
|
|
|
|
#endif
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Verify that we were provided with a valid directory structure */
|
|
|
|
|
|
|
|
if (buffer == NULL || buflen < sizeof(struct dirent))
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The way we handle the readdir depends on the type of inode
|
|
|
|
* that we are dealing with.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
if (INODE_IS_MOUNTPT(inode))
|
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
ret = inode->u.i_mops->readdir(inode, dir,
|
|
|
|
(FAR struct dirent *)buffer);
|
2022-07-26 08:53:39 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* The node is part of the root pseudo file system */
|
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
ret = read_pseudodir(dir, (FAR struct dirent *)buffer);
|
2022-07-26 08:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ret < 0 is an error. Special case: ret = -ENOENT is end of file */
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
if (ret == -ENOENT)
|
|
|
|
{
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
filep->f_pos++;
|
2022-07-26 08:56:41 +02:00
|
|
|
return sizeof(struct dirent);
|
2022-07-26 08:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static off_t dir_seek(FAR struct file *filep, off_t offset, int whence)
|
|
|
|
{
|
|
|
|
off_t pos = 0;
|
|
|
|
|
|
|
|
/* The way we handle the readdir depends on the type of inode
|
|
|
|
* that we are dealing with.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (whence == SEEK_SET)
|
|
|
|
{
|
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
FAR struct fs_dirent_s *dir = filep->f_priv;
|
|
|
|
|
|
|
|
if (INODE_IS_MOUNTPT(dir->fd_root))
|
|
|
|
{
|
|
|
|
pos = seek_mountptdir(filep, offset);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
pos = seek_pseudodir(filep, offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (whence == SEEK_CUR)
|
|
|
|
{
|
|
|
|
return filep->f_pos;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos >= 0)
|
|
|
|
{
|
|
|
|
filep->f_pos = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2022-08-05 06:04:37 +02:00
|
|
|
static int dir_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct fs_dirent_s *dir = filep->f_priv;
|
|
|
|
int ret = -ENOTTY;
|
|
|
|
|
|
|
|
if (cmd == FIOC_FILEPATH)
|
|
|
|
{
|
2023-02-08 04:29:09 +01:00
|
|
|
strlcpy((FAR char *)(uintptr_t)arg, dir->fd_path, PATH_MAX);
|
2022-08-05 06:04:37 +02:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-07-26 08:53:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dir_allocate
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a directory instance and bind it to f_priv of filep.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int dir_allocate(FAR struct file *filep, FAR const char *relpath)
|
|
|
|
{
|
|
|
|
FAR struct fs_dirent_s *dir;
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
2023-02-02 07:20:22 +01:00
|
|
|
char path_prefix[PATH_MAX];
|
2022-08-05 06:31:25 +02:00
|
|
|
int ret;
|
2022-07-26 08:53:39 +02:00
|
|
|
|
|
|
|
/* Is this a node in the pseudo filesystem? Or a mountpoint? */
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
if (INODE_IS_MOUNTPT(inode))
|
|
|
|
{
|
|
|
|
/* Open the directory at the relative path */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
ret = open_mountpoint(inode, relpath, &dir);
|
2022-07-26 08:53:39 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
return ret;
|
2022-07-26 08:53:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
ret = open_pseudodir(inode, &dir);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2022-07-26 08:53:39 +02:00
|
|
|
}
|
|
|
|
|
2023-03-05 14:13:32 +01:00
|
|
|
inode_getpath(inode, path_prefix, sizeof(path_prefix));
|
2023-04-12 14:32:35 +02:00
|
|
|
ret = asprintf(&dir->fd_path, "%s%s/", path_prefix, relpath);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
dir->fd_path = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-05-06 20:42:18 +02:00
|
|
|
filep->f_inode = &g_dir_inode;
|
|
|
|
filep->f_priv = dir;
|
2022-07-26 08:53:39 +02:00
|
|
|
inode_addref(&g_dir_inode);
|
|
|
|
return ret;
|
|
|
|
}
|