From 74eade91f588ffa3f4a2ec748fec97f22f44e12c Mon Sep 17 00:00:00 2001 From: Antonio SJ Musumeci Date: Wed, 4 Nov 2026 18:27:07 -0600 Subject: [PATCH] Preserve directory modes when cloning paths across branches - Restore source permissions after ownership to preserve setgid and clear unwanted inherited mode bits. - Add regression coverage for directory modes, group inheritance, and existing destination directories. - Update the branch configuration test to use the current API. - Document metadata preservation when cloning directory paths. --- .../config/functions_categories_policies.md | 7 ++++ src/fs_clonepath.cpp | 8 ++++ tests/tests.cpp | 42 +++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/mkdocs/docs/config/functions_categories_policies.md b/mkdocs/docs/config/functions_categories_policies.md index f306ecb2..6608133a 100644 --- a/mkdocs/docs/config/functions_categories_policies.md +++ b/mkdocs/docs/config/functions_categories_policies.md @@ -41,6 +41,13 @@ is chosen other parts of the system does what is necessary to accomplish the function. Such as the cloning of directories between branches. +When a directory path is cloned onto another branch, mergerfs attempts +to preserve its ownership, permissions (including setgid), timestamps, +and extended attributes. Permissions are restored after ownership so +new files in a setgid directory inherit its group on the new branch. +Directories which already exist on the destination branch are left +unchanged. + When using policies which are based on a branch's available space the branch base path provided is used. Not the full path to the file or directory in question. Meaning that mounts within the branch will not diff --git a/src/fs_clonepath.cpp b/src/fs_clonepath.cpp index 1b7b4793..8331ef8e 100644 --- a/src/fs_clonepath.cpp +++ b/src/fs_clonepath.cpp @@ -21,6 +21,7 @@ #include "errno.h" #include "fs_attr.hpp" #include "fs_clonepath.hpp" +#include "fs_lchmod.hpp" #include "fs_lchown.hpp" #include "fs_lstat.hpp" #include "fs_lutimens.hpp" @@ -111,6 +112,13 @@ fs::clonepath(const fs::path &srcpath_, if(return_metadata_errors_ && (rv < 0)) return rv; + // mkdir(2) on Linux ignores S_ISUID/S_ISGID in mode and may + // inherent S_ISGID from the parent. Explicitly set the mode after + // chown so the clone matches the source. + rv = fs::lchmod_check_on_error(dstpath,st.st_mode); + if(return_metadata_errors_ && (rv < 0)) + return rv; + rv = fs::lutimens(dstpath,st); if(return_metadata_errors_ && (rv < 0)) return rv; diff --git a/tests/tests.cpp b/tests/tests.cpp index 32f77818..7793fbad 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -1,6 +1,7 @@ #include "acutest/acutest.h" #include "config.hpp" +#include "fs_clonepath.hpp" #include "fs_copyfile.hpp" #include "fs_inode.hpp" #include "from_string.hpp" @@ -4200,6 +4201,46 @@ test_rapidhash_withSeed_preserves_default_output() } } +void +test_clonepath_modes() +{ + char tmp[] = "/tmp/mergerfs-clonepath-XXXXXX"; + TEST_ASSERT(::mkdtemp(tmp) != NULL); + const std::filesystem::path root(tmp); + const auto src = root / "src"; + const auto dst = root / "dst"; + std::filesystem::create_directories(src / "d" / "child"); + std::filesystem::create_directory(dst); + + // Use a different group when privileged to verify actual inheritance. + const gid_t group = (::geteuid() == 0) ? 1 : ::getegid(); + TEST_ASSERT(::chown((src / "d").c_str(),::geteuid(),group) == 0); + TEST_ASSERT(::chmod((src / "d").c_str(),02770) == 0); + TEST_ASSERT(::chmod((src / "d" / "child").c_str(),0770) == 0); + TEST_CHECK(fs::clonepath(src.string(),dst.string(),"d/child",true) == 0); + + struct stat st; + TEST_ASSERT(::stat((dst / "d").c_str(),&st) == 0); + TEST_CHECK((st.st_mode & 07777) == 02770); + TEST_CHECK(st.st_gid == group); + TEST_ASSERT(::stat((dst / "d" / "child").c_str(),&st) == 0); + TEST_CHECK((st.st_mode & 07777) == 0770); + + const auto file = dst / "d" / "file"; + const int fd = ::open(file.c_str(),O_CREAT|O_EXCL|O_WRONLY,0600); + TEST_ASSERT(fd >= 0); + TEST_ASSERT(::fstat(fd,&st) == 0); + TEST_CHECK(st.st_gid == group); + ::close(fd); + + // Existing destination directories must remain untouched. + TEST_ASSERT(::chmod((dst / "d").c_str(),0700) == 0); + TEST_CHECK(fs::clonepath(src.string(),dst.string(),"d",true) == 0); + TEST_ASSERT(::stat((dst / "d").c_str(),&st) == 0); + TEST_CHECK((st.st_mode & 07777) == 0700); + std::filesystem::remove_all(root); +} + TEST_LIST = { {"nop",test_nop}, @@ -4394,5 +4435,6 @@ TEST_LIST = {"tp_heavy_repeated_construct_destroy",test_tp_heavy_repeated_construct_destroy}, {"tp_heavy_enqueue_task_mixed_outcomes",test_tp_heavy_enqueue_task_mixed_outcomes}, {"tp_heavy_add_remove_churn_under_enqueue",test_tp_heavy_add_remove_churn_under_enqueue}, + {"clonepath_modes",test_clonepath_modes}, {NULL,NULL} };