This is an automated email from the ASF dual-hosted git repository.
GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new b1ad9cafbfa fs/unionfs: add link, symlink, readlink and lstat
b1ad9cafbfa is described below
commit b1ad9cafbfabba477a0009b93b84ebefff690393
Author: zhengyu16 <[email protected]>
AuthorDate: Thu Nov 12 17:36:43 2026 +0800
fs/unionfs: add link, symlink, readlink and lstat
unionfs has no link operations, so symlink(), link(), readlink() and
lstat() on a union mount are not supported. Forward them to the
underlying file systems: symlink and link go to the first one that
accepts them, link returns EXDEV across file systems, readlink falls
through to the second file system only on ENOENT, and lstat does not
follow the final link.
Signed-off-by: zhengyu16 <[email protected]>
Signed-off-by: jingfei <[email protected]>
---
fs/unionfs/fs_unionfs.c | 368 +++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 367 insertions(+), 1 deletion(-)
diff --git a/fs/unionfs/fs_unionfs.c b/fs/unionfs/fs_unionfs.c
index f5445d50e3f..d0d9d777728 100644
--- a/fs/unionfs/fs_unionfs.c
+++ b/fs/unionfs/fs_unionfs.c
@@ -132,6 +132,24 @@ static int unionfs_trychstat(FAR struct inode *inode,
FAR const char *relpath,
FAR const char *prefix,
FAR const struct stat *buf, int flags);
+#ifdef CONFIG_FS_LINKS
+static int unionfs_trylink(FAR struct inode *inode,
+ FAR const char *relpath1,
+ FAR const char *relpath2,
+ FAR const char *prefix);
+static int unionfs_trysymlink(FAR struct inode *inode,
+ FAR const char *target,
+ FAR const char *relpath,
+ FAR const char *prefix);
+static ssize_t unionfs_tryreadlink(FAR struct inode *inode,
+ FAR const char *relpath,
+ FAR const char *prefix,
+ FAR char *buf, size_t bufsize);
+static int unionfs_trylstat(FAR struct inode *inode,
+ FAR const char *relpath,
+ FAR const char *prefix,
+ FAR struct stat *buf);
+#endif
static int unionfs_trystatdir(FAR struct inode *inode,
FAR const char *relpath,
FAR const char *prefix);
@@ -202,6 +220,20 @@ static int unionfs_stat(FAR struct inode *mountpt,
static int unionfs_chstat(FAR struct inode *mountpt,
FAR const char *relpath,
FAR const struct stat *buf, int flags);
+#ifdef CONFIG_FS_LINKS
+static int unionfs_link(FAR struct inode *mountpt,
+ FAR const char *relpath1,
+ FAR const char *relpath2);
+static int unionfs_symlink(FAR struct inode *mountpt,
+ FAR const char *target,
+ FAR const char *relpath);
+static ssize_t unionfs_readlink(FAR struct inode *mountpt,
+ FAR const char *relpath,
+ FAR char *buf, size_t bufsize);
+static int unionfs_lstat(FAR struct inode *mountpt,
+ FAR const char *relpath,
+ FAR struct stat *buf);
+#endif
/* Initialization */
@@ -255,7 +287,16 @@ const struct mountpt_operations g_unionfs_operations =
unionfs_rmdir, /* rmdir */
unionfs_rename, /* rename */
unionfs_stat, /* stat */
- unionfs_chstat /* chstat */
+ unionfs_chstat, /* chstat */
+ NULL, /* syncfs */
+ NULL, /* ioctldir */
+ NULL, /* permission */
+#ifdef CONFIG_FS_LINKS
+ unionfs_link, /* link */
+ unionfs_symlink, /* symlink */
+ unionfs_readlink, /* readlink */
+ unionfs_lstat /* lstat */
+#endif
};
/****************************************************************************
@@ -605,6 +646,159 @@ static int unionfs_trychstat(FAR struct inode *inode,
return ops->chstat(inode, trypath, buf, flags);
}
+#ifdef CONFIG_FS_LINKS
+/****************************************************************************
+ * Name: unionfs_trylink
+ ****************************************************************************/
+
+static int unionfs_trylink(FAR struct inode *mountpt,
+ FAR const char *relpath1,
+ FAR const char *relpath2,
+ FAR const char *prefix)
+{
+ FAR const struct mountpt_operations *ops;
+ FAR const char *tryoldpath;
+ FAR const char *trynewpath;
+
+ /* Is source path valid on this file system? */
+
+ tryoldpath = unionfs_offsetpath(relpath1, prefix);
+ if (tryoldpath == NULL)
+ {
+ /* No.. return -ENOENT. This should not fail because the existence
+ * of the file has already been verified.
+ */
+
+ return -ENOENT;
+ }
+
+ /* Is destination path valid on this file system?
+ * REVISIT: There is no logic currently to link across the contained
+ * file systems. We return -EXDEV which is the correct POSIX error for
+ * cross-device links.
+ */
+
+ trynewpath = unionfs_offsetpath(relpath2, prefix);
+ if (trynewpath == NULL)
+ {
+ return -EXDEV;
+ }
+
+ /* Yes.. Try to link the file */
+
+ ops = mountpt->u.i_mops;
+ if (!ops->link)
+ {
+ return -ENOSYS;
+ }
+
+ return ops->link(mountpt, tryoldpath, trynewpath);
+}
+
+/****************************************************************************
+ * Name: unionfs_trysymlink
+ ****************************************************************************/
+
+static int unionfs_trysymlink(FAR struct inode *inode,
+ FAR const char *target,
+ FAR const char *relpath,
+ FAR const char *prefix)
+{
+ FAR const struct mountpt_operations *ops;
+ FAR const char *trypath;
+
+ /* Is the symlink path valid on this file system? */
+
+ trypath = unionfs_offsetpath(relpath, prefix);
+ if (trypath == NULL)
+ {
+ /* No.. return -ENOENT */
+
+ return -ENOENT;
+ }
+
+ /* Yes.. Try to create the symlink. Note: target is NOT offsetpath'd.
+ * It is a relative path from the symlink location and is passed
+ * transparently to the underlying file system.
+ */
+
+ ops = inode->u.i_mops;
+ if (!ops->symlink)
+ {
+ return -ENOSYS;
+ }
+
+ return ops->symlink(inode, target, trypath);
+}
+
+/****************************************************************************
+ * Name: unionfs_tryreadlink
+ ****************************************************************************/
+
+static ssize_t unionfs_tryreadlink(FAR struct inode *inode,
+ FAR const char *relpath,
+ FAR const char *prefix,
+ FAR char *buf, size_t bufsize)
+{
+ FAR const struct mountpt_operations *ops;
+ FAR const char *trypath;
+
+ /* Is this path valid on this file system? */
+
+ trypath = unionfs_offsetpath(relpath, prefix);
+ if (trypath == NULL)
+ {
+ /* No.. return -ENOENT */
+
+ return -ENOENT;
+ }
+
+ /* Yes.. Try to read the link */
+
+ ops = inode->u.i_mops;
+ if (!ops->readlink)
+ {
+ return -ENOSYS;
+ }
+
+ return ops->readlink(inode, trypath, buf, bufsize);
+}
+
+/****************************************************************************
+ * Name: unionfs_trylstat
+ ****************************************************************************/
+
+static int unionfs_trylstat(FAR struct inode *inode,
+ FAR const char *relpath,
+ FAR const char *prefix,
+ FAR struct stat *buf)
+{
+ FAR const struct mountpt_operations *ops;
+ FAR const char *trypath;
+
+ /* Is this path valid on this file system? */
+
+ trypath = unionfs_offsetpath(relpath, prefix);
+ if (trypath == NULL)
+ {
+ /* No.. return -ENOENT */
+
+ return -ENOENT;
+ }
+
+ /* Yes.. Try to lstat the file */
+
+ ops = inode->u.i_mops;
+ if (!ops->lstat)
+ {
+ return -ENOSYS;
+ }
+
+ return ops->lstat(inode, trypath, buf);
+}
+
+#endif
+
/****************************************************************************
* Name: unionfs_trystatdir
****************************************************************************/
@@ -2555,6 +2749,178 @@ static int unionfs_chstat(FAR struct inode *mountpt,
FAR const char *relpath,
return ret;
}
+#ifdef CONFIG_FS_LINKS
+/****************************************************************************
+ * Name: unionfs_link
+ ****************************************************************************/
+
+static int unionfs_link(FAR struct inode *mountpt,
+ FAR const char *relpath1,
+ FAR const char *relpath2)
+{
+ FAR struct unionfs_inode_s *ui;
+ FAR struct unionfs_mountpt_s *um;
+ struct stat buf;
+ int ret;
+
+ finfo("relpath1: %s relpath2: %s\n", relpath1, relpath2);
+
+ /* Recover the union file system data from the struct inode instance */
+
+ DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL &&
+ relpath1 != NULL && relpath2 != NULL);
+ ui = mountpt->i_private;
+
+ /* Is there a file with this name on file system 1 */
+
+ um = &ui->ui_fs[0];
+ ret = unionfs_trystat(um->um_node, relpath1, um->um_prefix, &buf);
+ if (ret >= 0)
+ {
+ return unionfs_trylink(um->um_node, relpath1, relpath2,
+ um->um_prefix);
+ }
+
+ /* File does not exist on file system 1. Try file system 2. */
+
+ um = &ui->ui_fs[1];
+ ret = unionfs_trystat(um->um_node, relpath1, um->um_prefix, &buf);
+ if (ret >= 0)
+ {
+ ret = unionfs_trylink(um->um_node, relpath1, relpath2,
+ um->um_prefix);
+ }
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: unionfs_symlink
+ ****************************************************************************/
+
+static int unionfs_symlink(FAR struct inode *mountpt,
+ FAR const char *target,
+ FAR const char *relpath)
+{
+ FAR struct unionfs_inode_s *ui;
+ FAR struct unionfs_mountpt_s *um;
+ int ret;
+
+ finfo("target: %s relpath: %s\n", target, relpath);
+
+ /* Recover the union file system data from the struct inode instance */
+
+ DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL &&
+ relpath != NULL);
+ ui = mountpt->i_private;
+
+ /* Try to create the symlink on file system 1 */
+
+ um = &ui->ui_fs[0];
+ ret = unionfs_trysymlink(um->um_node, target, relpath, um->um_prefix);
+ if (ret >= 0)
+ {
+ return OK;
+ }
+
+ /* symlink failed on file system 1. Try again on file system 2. */
+
+ um = &ui->ui_fs[1];
+ ret = unionfs_trysymlink(um->um_node, target, relpath, um->um_prefix);
+ if (ret >= 0)
+ {
+ return OK;
+ }
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: unionfs_readlink
+ ****************************************************************************/
+
+static ssize_t unionfs_readlink(FAR struct inode *mountpt,
+ FAR const char *relpath,
+ FAR char *buf, size_t bufsize)
+{
+ FAR struct unionfs_inode_s *ui;
+ FAR struct unionfs_mountpt_s *um;
+ struct stat st;
+ int ret;
+
+ finfo("relpath: %s\n", relpath);
+
+ /* Recover the union file system data from the struct inode instance */
+
+ DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL &&
+ relpath != NULL);
+ ui = mountpt->i_private;
+
+ /* Find which file system contains this path, then readlink on it */
+
+ um = &ui->ui_fs[0];
+ ret = unionfs_trylstat(um->um_node, relpath, um->um_prefix, &st);
+ if (ret >= 0)
+ {
+ return unionfs_tryreadlink(um->um_node, relpath, um->um_prefix,
+ buf, bufsize);
+ }
+
+ /* Path was not found on file system 1. Try file system 2. */
+
+ um = &ui->ui_fs[1];
+ ret = unionfs_trylstat(um->um_node, relpath, um->um_prefix, &st);
+ if (ret >= 0)
+ {
+ return unionfs_tryreadlink(um->um_node, relpath, um->um_prefix,
+ buf, bufsize);
+ }
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: unionfs_lstat
+ ****************************************************************************/
+
+static int unionfs_lstat(FAR struct inode *mountpt, FAR const char *relpath,
+ FAR struct stat *buf)
+{
+ FAR struct unionfs_inode_s *ui;
+ FAR struct unionfs_mountpt_s *um;
+ int ret;
+
+ finfo("relpath: %s\n", relpath);
+
+ /* Recover the union file system data from the struct inode instance */
+
+ DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL &&
+ relpath != NULL);
+ ui = mountpt->i_private;
+
+ /* lstat this path on file system 1 */
+
+ um = &ui->ui_fs[0];
+ ret = unionfs_trylstat(um->um_node, relpath, um->um_prefix, buf);
+ if (ret >= 0)
+ {
+ return OK;
+ }
+
+ /* lstat failed on file system 1. Try again on file system 2. */
+
+ um = &ui->ui_fs[1];
+ ret = unionfs_trylstat(um->um_node, relpath, um->um_prefix, buf);
+ if (ret >= 0)
+ {
+ return OK;
+ }
+
+ return ret;
+}
+
+#endif
+
/****************************************************************************
* Name: unionfs_getmount
****************************************************************************/