jingfei195887 opened a new pull request, #22448:
URL: https://github.com/apache/nuttx/pull/22448

   ## Summary
   
   Make pseudoFS rename replace an empty destination directory at the requested
   path. Reject a nonempty destination directory with ENOTEMPTY and a
   nondirectory source with EISDIR. Directory-to-file replacement keeps ENOTDIR.
   
   For example, rename("/a", "/b") with two empty directories currently creates
   "/b/a". After this change, "/b" replaces the destination and "/a" disappears.
   The VFS operation no longer implements shell basename expansion.
   
   ## Impact
   
   - This aligns pseudoFS directory targets with rename semantics already used
     for mounted filesystems. Same-node success and mountpoint EXDEV remain.
   - Calls that want move-into-directory behavior must pass the full child path.
     NSH mv currently forwards paths directly; a companion apps fix is being
     prepared to construct that child path explicitly.
   - No public API, Kconfig, build system, or hardware/driver interface changes.
   - The existing remove-then-reserve replacement strategy remains. This change
     does not guarantee destination preservation after a later allocation 
failure.
   - Child-parent relocation is tracked separately by
     [#22423](https://github.com/apache/nuttx/pull/22423). It is not included 
here.
   
   ## Testing
   
   I confirm that changes are verified on the local setup and work as intended.
   
   Linux x86_64, GCC 13.1.0; serial builds restricted to two CPUs.
   Base `a8bb89bf6803f9d67e60696dfaf72c4036c0c789`; head 
`d9d659ff70a27f7a8594ab48590917663adeb43b`. Runtime target: sim:nsh.
   
   - Same-config direct rename regression: 145 baseline
     checks, 13 failures; 142 branch checks, zero failures.
   - Nine cases with attributes and links disabled: 116
     checks, zero failures.
   - Covers empty replacement, nonempty target rejection, file/directory 
mismatch,
     missing destination, self-rename, source/destination symlinks, file 
replacement,
     an open destination directory, and nonempty-source namespace relocation.
   - Each case verifies cleanup. The nonempty-source case explicitly retains the
     old source inode until cleanup, to isolate namespace replacement from the
     separate pending child-parent issue in #22423. It does not verify that fix.
   - Protected qemu-armv7r:pnsh and kernel qemu-armv8a:knsh builds pass with 
pseudoFS
     operations, attributes and links enabled. No cross-mode runtime or hardware
     test was performed. No SMP or concurrent rename stress test was performed.
   - Make/CMake, OSTest, complete-file/commit checkpatch and exact-HEAD gate 
pass.
   
   Reproduce by adding the local app below to apps/examples/renamedircheck,
   configuring sim:nsh with EXAMPLES_RENAMEDIRCHECK, PSEUDOFS_FILE,
   PSEUDOFS_ATTRIBUTES, FS_LINKS, DEBUG_ASSERTIONS, DEBUG_FULLOPT and 
COVERAGE_NONE,
   and running renamedircheck with case arguments 0 through 10. The minimal
   configuration skips link cases 6 and 7. Its wrapper reports warnings for the
   earlier enable arguments overridden by explicit disable arguments; the saved
   .config confirms both options are disabled. Tests are not included in 
production.
   
   Testing logs before change:
   
   <details>
   <summary>Baseline</summary>
   
   ```text
   rev a8bb89bf680 Documentation: Say when an ACK resets the TCP retransmission 
count. build rc=0
   run rc=0 
log=/home/mi/test/nuttx_apache/fs-upstream/pr-rename-directory-target/baseline/run.log
   nsh> renamedircheck 0
   case=0 rename=0 errno=0 expected=0
   FAIL no implicit basename child errno=2
   RENAMEDIR RESULT case=0 checks=13 failures=1
   nsh> echo case-0-status=$?
   case-0-status=1
   nsh> renamedircheck 1
   case=1 rename=0 errno=0 expected=39
   FAIL rename return and errno errno=0
   FAIL source name preserved errno=2
   FAIL no implicit basename child errno=2
   RENAMEDIR RESULT case=1 checks=14 failures=3
   nsh> echo case-1-status=$?
   case-1-status=1
   nsh> renamedircheck 2
   case=2 rename=0 errno=0 expected=21
   FAIL rename return and errno errno=0
   FAIL source name preserved errno=2
   FAIL no implicit basename child errno=2
   RENAMEDIR RESULT case=2 checks=12 failures=3
   nsh> echo case-2-status=$?
   case-2-status=1
   nsh> renamedircheck 3
   case=3 rename=-1 errno=20 expected=20
   RENAMEDIR RESULT case=3 checks=12 failures=0
   nsh> echo case-3-status=$?
   case-3-status=0
   nsh> renamedircheck 4
   case=4 rename=0 errno=0 expected=0
   RENAMEDIR RESULT case=4 checks=11 failures=0
   nsh> echo case-4-status=$?
   case-4-status=0
   nsh> renamedircheck 5
   case=5 rename=0 errno=0 expected=0
   RENAMEDIR RESULT case=5 checks=9 failures=0
   nsh> echo case-5-status=$?
   case-5-status=0
   nsh> renamedircheck 6
   case=6 rename=0 errno=0 expected=21
   FAIL rename return and errno errno=0
   FAIL source name preserved errno=2
   FAIL no implicit basename child errno=2
   RENAMEDIR RESULT case=6 checks=11 failures=3
   nsh> echo case-6-status=$?
   case-6-status=1
   nsh> renamedircheck 7
   case=7 rename=-1 errno=20 expected=20
   RENAMEDIR RESULT case=7 checks=15 failures=0
   nsh> echo case-7-status=$?
   case-7-status=0
   nsh> renamedircheck 8
   case=8 rename=0 errno=0 expected=0
   RENAMEDIR RESULT case=8 checks=14 failures=0
   nsh> echo case-8-status=$?
   case-8-status=0
   nsh> renamedircheck 9
   case=9 rename=0 errno=0 expected=0
   FAIL no implicit basename child errno=2
   RENAMEDIR RESULT case=9 checks=16 failures=1
   nsh> echo case-9-status=$?
   case-9-status=1
   nsh> renamedircheck 10
   case=10 rename=0 errno=0 expected=0
   FAIL no implicit basename child errno=2
   FAIL source subtree at target path errno=2
   RENAMEDIR RESULT case=10 checks=18 failures=2
   nsh> echo case-10-status=$?
   case-10-status=1
   nsh> poweroff
   ```
   
   </details>
   
   Testing logs after change:
   
   <details>
   <summary>branch</summary>
   
   ```text
   rev d9d659ff70a fs/vfs: refine pseudoFS rename directory targets build rc=0
   run rc=0 
log=/home/mi/test/nuttx_apache/fs-upstream/pr-rename-directory-target/branch/run.log
   nsh> renamedircheck 0
   case=0 rename=0 errno=0 expected=0
   RENAMEDIR RESULT case=0 checks=12 failures=0
   nsh> echo case-0-status=$?
   case-0-status=0
   nsh> renamedircheck 1
   case=1 rename=-1 errno=39 expected=39
   RENAMEDIR RESULT case=1 checks=14 failures=0
   nsh> echo case-1-status=$?
   case-1-status=0
   nsh> renamedircheck 2
   case=2 rename=-1 errno=21 expected=21
   RENAMEDIR RESULT case=2 checks=12 failures=0
   nsh> echo case-2-status=$?
   case-2-status=0
   nsh> renamedircheck 3
   case=3 rename=-1 errno=20 expected=20
   RENAMEDIR RESULT case=3 checks=12 failures=0
   nsh> echo case-3-status=$?
   case-3-status=0
   nsh> renamedircheck 4
   case=4 rename=0 errno=0 expected=0
   RENAMEDIR RESULT case=4 checks=11 failures=0
   nsh> echo case-4-status=$?
   case-4-status=0
   nsh> renamedircheck 5
   case=5 rename=0 errno=0 expected=0
   RENAMEDIR RESULT case=5 checks=9 failures=0
   nsh> echo case-5-status=$?
   case-5-status=0
   nsh> renamedircheck 6
   case=6 rename=-1 errno=21 expected=21
   RENAMEDIR RESULT case=6 checks=11 failures=0
   nsh> echo case-6-status=$?
   case-6-status=0
   nsh> renamedircheck 7
   case=7 rename=-1 errno=20 expected=20
   RENAMEDIR RESULT case=7 checks=15 failures=0
   nsh> echo case-7-status=$?
   case-7-status=0
   nsh> renamedircheck 8
   case=8 rename=0 errno=0 expected=0
   RENAMEDIR RESULT case=8 checks=14 failures=0
   nsh> echo case-8-status=$?
   case-8-status=0
   nsh> renamedircheck 9
   case=9 rename=0 errno=0 expected=0
   RENAMEDIR RESULT case=9 checks=15 failures=0
   nsh> echo case-9-status=$?
   case-9-status=0
   nsh> renamedircheck 10
   case=10 rename=0 errno=0 expected=0
   RENAMEDIR RESULT case=10 checks=17 failures=0
   nsh> echo case-10-status=$?
   case-10-status=0
   nsh> poweroff
   ```
   
   </details>
   
   <details>
   <summary>branch-minimal</summary>
   
   ```text
   WARNING: CONFIG_FS_LINKS not enabled after olddefconfig
   WARNING: CONFIG_PSEUDOFS_ATTRIBUTES not enabled after olddefconfig
   rev d9d659ff70a fs/vfs: refine pseudoFS rename directory targets build rc=0
   run rc=0 
log=/home/mi/test/nuttx_apache/fs-upstream/pr-rename-directory-target/branch-minimal/run.log
   nsh> renamedircheck 0
   case=0 rename=0 errno=0 expected=0
   RENAMEDIR RESULT case=0 checks=12 failures=0
   nsh> echo case-0-status=$?
   case-0-status=0
   nsh> renamedircheck 1
   case=1 rename=-1 errno=39 expected=39
   RENAMEDIR RESULT case=1 checks=14 failures=0
   nsh> echo case-1-status=$?
   case-1-status=0
   nsh> renamedircheck 2
   case=2 rename=-1 errno=21 expected=21
   RENAMEDIR RESULT case=2 checks=12 failures=0
   nsh> echo case-2-status=$?
   case-2-status=0
   nsh> renamedircheck 3
   case=3 rename=-1 errno=20 expected=20
   RENAMEDIR RESULT case=3 checks=12 failures=0
   nsh> echo case-3-status=$?
   case-3-status=0
   nsh> renamedircheck 4
   case=4 rename=0 errno=0 expected=0
   RENAMEDIR RESULT case=4 checks=11 failures=0
   nsh> echo case-4-status=$?
   case-4-status=0
   nsh> renamedircheck 5
   case=5 rename=0 errno=0 expected=0
   RENAMEDIR RESULT case=5 checks=9 failures=0
   nsh> echo case-5-status=$?
   case-5-status=0
   nsh> renamedircheck 8
   case=8 rename=0 errno=0 expected=0
   RENAMEDIR RESULT case=8 checks=14 failures=0
   nsh> echo case-8-status=$?
   case-8-status=0
   nsh> renamedircheck 9
   case=9 rename=0 errno=0 expected=0
   RENAMEDIR RESULT case=9 checks=15 failures=0
   nsh> echo case-9-status=$?
   case-9-status=0
   nsh> renamedircheck 10
   case=10 rename=0 errno=0 expected=0
   RENAMEDIR RESULT case=10 checks=17 failures=0
   nsh> echo case-10-status=$?
   case-10-status=0
   nsh> poweroff
   ```
   
   </details>
   
   <details>
   <summary>Final gate</summary>
   
   ```text
   target   : nuttx
   branch   : fs/rename-directory-target
   head     : d9d659ff70a27f7a8594ab48590917663adeb43b
   subject  : fs/vfs: refine pseudoFS rename directory targets
   commits  : 1
   base     : a8bb89bf68 (merge-base with upstream/master)
   behind   : 0 commits of upstream/master
   nuttx    : d9d659ff70
   apps     : a1b03f8926
   options  : CONFIG_PSEUDOFS_FILE CONFIG_PSEUDOFS_ATTRIBUTES CONFIG_FS_LINKS 
CONFIG_FS_NOTIFY CONFIG_DEBUG_ASSERTIONS CONFIG_DEBUG_FULLOPT 
CONFIG_COVERAGE_NONE
   host     : Linux x86_64, gcc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0
   checkpatch: rc=0
   signer   : jingfei <[email protected]>
   commits missing your Signed-off-by: 0
   make build: rc=0
   warnings in changed files: 0
   ostest: Exiting with status 0
   cmake reconfigure: rc=0
   cmake build: rc=0
   worktree tracked changes after: 0
   RESULT: PASS
   ```
   
   </details>
   
   <details>
   <summary>protected</summary>
   
   ```text
   qemu-armv7r:pnsh build PASS
   arm-none-eabi-gcc (Arm GNU Toolchain 13.2.rel1 (Build arm-13.7)) 13.2.1 
20231009
   Copyright (C) 2023 Free Software Foundation, Inc.
   This is free software; see the source for copying conditions.  There is NO
   warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   ```
   
   </details>
   
   <details>
   <summary>kernel</summary>
   
   ```text
   qemu-armv8a:knsh build PASS
   aarch64-none-elf-gcc (GCC) 13.4.0
   Copyright (C) 2023 Free Software Foundation, Inc.
   This is free software; see the source for copying conditions.  There is NO
   warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   ```
   
   </details>
   
   <details>
   <summary>Local test: renamedircheck_main.c</summary>
   
   ```c
   /****************************************************************************
    * apps/examples/renamedircheck/renamedircheck_main.c
    *
    * SPDX-License-Identifier: Apache-2.0
    
****************************************************************************/
   
   /****************************************************************************
    * Included Files
    
****************************************************************************/
   
   #include <nuttx/config.h>
   
   #include <sys/stat.h>
   #include <dirent.h>
   #include <errno.h>
   #include <fcntl.h>
   #include <stdbool.h>
   #include <stdio.h>
   #include <stdlib.h>
   #include <unistd.h>
   
   #include <nuttx/fs/fs.h>
   #include "inode/inode.h"
   
   /****************************************************************************
    * Private Data
    
****************************************************************************/
   
   static unsigned int g_checks;
   static unsigned int g_failures;
   
   /****************************************************************************
    * Private Functions
    
****************************************************************************/
   
   static void check(bool ok, FAR const char *label)
   {
     g_checks++;
     if (!ok)
       {
         g_failures++;
         printf("FAIL %s errno=%d\n", label, errno);
       }
   }
   
   static bool exists(FAR const char *path)
   {
     struct stat st;
   
     return lstat(path, &st) == 0;
   }
   
   static void makefile(FAR const char *path)
   {
     int fd = open(path, O_CREAT | O_WRONLY, 0666);
   
     check(fd >= 0, "create file");
     if (fd >= 0)
       {
         check(close(fd) == 0, "close file");
       }
   }
   
   static void remove_path(FAR const char *path)
   {
     struct stat st;
   
     if (lstat(path, &st) == 0)
       {
         if (S_ISDIR(st.st_mode))
           {
             check(rmdir(path) == 0, "cleanup directory");
           }
         else
           {
             check(unlink(path) == 0, "cleanup file/link");
           }
       }
   }
   
   /****************************************************************************
    * Public Functions
    
****************************************************************************/
   
   int main(int argc, FAR char *argv[])
   {
     FAR struct inode *source = NULL;
     struct inode_search_s search;
     FAR DIR *dir = NULL;
     struct stat st;
     int which = argc > 1 ? atoi(argv[1]) : 0;
     int expected = 0;
     int ret;
     int err;
   
     g_checks = 0;
     g_failures = 0;
     check(!exists("/rdtest"), "fresh namespace");
     if (exists("/rdtest"))
       {
         return 1;
       }
   
     check(mkdir("/rdtest", 0777) == 0, "create test root");
     if (which == 2 || which == 8)
       {
         makefile("/rdtest/src");
       }
   #ifdef CONFIG_FS_LINKS
     else if (which == 6)
       {
         check(symlink("/rdtest/anchor", "/rdtest/src") == 0,
               "create source link");
       }
   #endif
     else
       {
         check(mkdir("/rdtest/src", 0755) == 0, "create source directory");
       }
   
     if (which == 3 || which == 8)
       {
         makefile("/rdtest/dst");
       }
   #ifdef CONFIG_FS_LINKS
     else if (which == 7)
       {
         check(mkdir("/rdtest/anchor", 0777) == 0, "create link target");
         check(symlink("/rdtest/anchor", "/rdtest/dst") == 0,
               "create destination link");
       }
   #endif
     else if (which != 4 && which != 5)
       {
         check(mkdir("/rdtest/dst", 0777) == 0,
               "create destination directory");
       }
   
     if (which == 1)
       {
         check(mkdir("/rdtest/dst/keep", 0777) == 0, "create target child");
         expected = ENOTEMPTY;
       }
     else if (which == 2 || which == 6)
       {
         expected = EISDIR;
       }
     else if (which == 3 || which == 7)
       {
         expected = ENOTDIR;
       }
     else if (which == 9)
       {
         dir = opendir("/rdtest/dst");
         check(dir != NULL, "hold target directory open");
       }
     else if (which == 10)
       {
         check(mkdir("/rdtest/src/child", 0777) == 0, "create source child");
   
         /* Keep the old parent alive while the separate parent-chain fix is
          * pending.  This case checks namespace replacement, not that fix.
          */
   
         ret = inode_search_setup(&search, "/rdtest/src", true);
         check(ret == 0, "prepare source lookup");
         if (ret == 0)
           {
             check(inode_find(&search, &source) == 0, "hold source inode");
             inode_search_release(&search);
           }
       }
   
     errno = 0;
     ret = rename("/rdtest/src", which == 5 ? "/rdtest/src" : "/rdtest/dst");
     err = errno;
     printf("case=%d rename=%d errno=%d expected=%d\n",
            which, ret, err, expected);
     check(expected == 0 ? ret == 0 : ret == -1 && err == expected,
           "rename return and errno");
     if (expected != 0 || which == 5)
       {
         check(exists("/rdtest/src"), "source name preserved");
       }
     else
       {
         check(!exists("/rdtest/src"), "source name removed");
         check(lstat("/rdtest/dst", &st) == 0, "destination exists");
         check(which == 8 ? S_ISREG(st.st_mode) : S_ISDIR(st.st_mode),
               "destination type");
       }
   
     check(!exists("/rdtest/dst/src"), "no implicit basename child");
     if (which == 1)
       {
         check(exists("/rdtest/dst/keep"), "nonempty target child preserved");
       }
   
     if (which == 7)
       {
         check(lstat("/rdtest/dst", &st) == 0 && S_ISLNK(st.st_mode),
               "destination link not followed or removed");
         check(exists("/rdtest/anchor"), "link target preserved");
       }
   
     if (which == 10)
       {
         check(exists("/rdtest/dst/child"), "source subtree at target path");
       }
   
     if (dir != NULL)
       {
         check(readdir(dir) == NULL, "old open target remains empty");
         check(closedir(dir) == 0, "close unlinked target directory");
       }
   
     remove_path("/rdtest/dst/src/child");
     remove_path("/rdtest/dst/child");
     remove_path("/rdtest/src/child");
     remove_path("/rdtest/dst/src");
     remove_path("/rdtest/dst/keep");
     remove_path("/rdtest/src");
     remove_path("/rdtest/dst");
     remove_path("/rdtest/anchor");
     if (source != NULL)
       {
         inode_release(source);
       }
   
     check(rmdir("/rdtest") == 0, "remove test root");
     check(!exists("/rdtest"), "no namespace residue");
     printf("RENAMEDIR RESULT case=%d checks=%u failures=%u\n",
            which, g_checks, g_failures);
     return g_failures != 0;
   }
   ```
   
   </details>
   
   <details>
   <summary>Local test: Kconfig</summary>
   
   ```text
   config EXAMPLES_RENAMEDIRCHECK
        bool "Local pseudoFS rename directory target regression"
        default n
        depends on BUILD_FLAT && !DISABLE_PSEUDOFS_OPERATIONS
   ```
   
   </details>
   
   <details>
   <summary>Local test: Make.defs</summary>
   
   ```text
   ifeq ($(CONFIG_EXAMPLES_RENAMEDIRCHECK),y)
   CONFIGURED_APPS += $(APPDIR)/examples/renamedircheck
   endif
   ```
   
   </details>
   
   <details>
   <summary>Local test: Makefile</summary>
   
   ```text
   include $(APPDIR)/Make.defs
   PROGNAME = renamedircheck
   PRIORITY = SCHED_PRIORITY_DEFAULT
   STACKSIZE = 8192
   MODULE = $(CONFIG_EXAMPLES_RENAMEDIRCHECK)
   MAINSRC = renamedircheck_main.c
   CFLAGS += -I$(TOPDIR)/fs
   include $(APPDIR)/Application.mk
   ```
   
   </details>
   
   ## PR verification Self-Check
   
   - [x] This PR introduces only one functional change.
   - [x] I have updated all required description fields above.
   - [x] My PR follows the contributing guidelines for scope, coding style and 
testing.
   - [ ] My PR is still work in progress (not ready for review).
   - [x] My PR is ready for review and can be safely merged into the codebase.
   


-- 
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]

  • [PR] fs/vfs: refine pseudoFS ... jingfei195887 (via GitHub) via commits

Reply via email to