Copilot commented on code in PR #21780:
URL: https://github.com/apache/nuttx/pull/21780#discussion_r4564223696
##########
fs/unionfs/fs_unionfs.c:
##########
@@ -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;
Review Comment:
The fallback to filesystem 2 happens for any failure from filesystem 1, but
the comment and expected unionfs behavior imply it should only fall back when
the source path is not found on FS1 (typically `-ENOENT`). If FS1 returns
`-EACCES`, `-EIO`, etc., this code will incorrectly try FS2 and may mask the
real error or produce surprising results. Recommendation: only attempt FS2 when
the FS1 lookup returns `-ENOENT` (and otherwise return the original error).
##########
fs/unionfs/fs_unionfs.c:
##########
@@ -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;
Review Comment:
This retries symlink creation on filesystem 2 for *any* failure on
filesystem 1. That can create incorrect results for errors like `-EEXIST` (path
already exists on FS1) or permission errors, potentially creating the link in
FS2 and leaving the union view with conflicting entries. Recommendation: only
fall back to FS2 for errors that indicate 'not applicable on FS1' (e.g., the
unionfs prefix/offsetpath makes it unreachable: `-ENOENT` from
`unionfs_offsetpath`), and otherwise return the FS1 error.
##########
fs/unionfs/fs_unionfs.c:
##########
@@ -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;
Review Comment:
This uses `lstat` to select the backing FS and falls back to FS2 for any
`lstat` failure on FS1, which conflicts with the PR description ('readlink
falls through to the second file system only on `ENOENT`'). It also makes
`readlink` dependent on underlying `lstat` support: if FS1 lacks `lstat`
(`-ENOSYS`) but supports `readlink`, `unionfs_readlink` will never attempt
`readlink` and will return `-ENOSYS` or incorrectly try FS2. Recommendation:
attempt `unionfs_tryreadlink()` on FS1 first and only try FS2 when that returns
`-ENOENT`.
##########
fs/unionfs/fs_unionfs.c:
##########
@@ -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);
+ }
Review Comment:
`unionfs_link()` uses `unionfs_trystat()` to check whether the source exists
on FS1. If `trystat` follows symlinks (as `stat` normally does), hard-linking a
symlink risks selecting the filesystem based on the *symlink target* rather
than the symlink inode itself. This can change link semantics (hard link to
target vs hard link to symlink) and may route the operation to the wrong
backing FS. Recommendation: use an `lstat`-based check (e.g.,
`unionfs_trylstat()` or otherwise ensure the lookup does not follow the final
symlink) when deciding which FS 'holds the source'.
##########
fs/unionfs/fs_unionfs.c:
##########
@@ -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;
Review Comment:
The FS1-then-FS2 retry pattern is repeated across
`link/symlink/readlink/lstat`, and subtle differences in which errors should
fall through (e.g., only `-ENOENT`) are easy to get wrong (as seen in
`readlink`/`symlink`). Consider centralizing the 'try FS0 then FS1 based on
error' policy into a small helper to make the fallthrough rules explicit and
consistent.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]