diff --git a/src/fs_movefile_and_open.cpp b/src/fs_movefile_and_open.cpp index 7ba3d9e5..fea07f12 100644 --- a/src/fs_movefile_and_open.cpp +++ b/src/fs_movefile_and_open.cpp @@ -139,7 +139,12 @@ _movefile_and_open(const Policy::Create &createFunc_, if(!candidates) return -ENOSPC; - rv = createFunc_(candidates,fusepath_,dst_branch); + // Ask by the parent directory, as create does. The file isn't on the + // destination yet, and the existing path policies (ep*) only pick a + // branch that already has the path they are given. + fusedir = fusepath_.parent_path(); + + rv = createFunc_(candidates,fusedir,dst_branch); if(rv < 0) return rv; if(dst_branch.empty()) @@ -156,8 +161,6 @@ _movefile_and_open(const Policy::Create &createFunc_, if(fs::has_space(dst_branch[0]->path,src_size) == false) return -ENOSPC; - fusedir = fusepath_.parent_path(); - rv = fs::clonepath(src_branch,dst_branch[0]->path,fusedir); if(rv < 0) return -ENOSPC; diff --git a/tests/tests.cpp b/tests/tests.cpp index cb9cbac1..612e5487 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -3785,6 +3785,85 @@ test_fs_movefile_and_open_same_branch() std::filesystem::remove_all(tmp_dir); } +// The create policy is asked where the file goes, as create does, by +// its parent directory. The existing path policies (ep*) only choose a +// branch that already has the path, and the destination has the +// directory but not the file yet. Given the file's own path they found +// nothing, so the write kept failing with ENOSPC. +void +test_fs_movefile_and_open_existing_path_policies() +{ + char tmp_template[] = "/tmp/mergerfs-test-movefile-ep-XXXXXX"; + if(::mkdtemp(tmp_template) == nullptr) + { + TEST_CHECK(false); + return; + } + + const fs::path tmp_dir = tmp_template; + const fs::path src = tmp_dir / "src"; + const fs::path other = tmp_dir / "other"; + const std::string data = "moveonenospc existing path policy\n"; + const struct + { + const char *name; + Policy::CreateImpl *policy; + } policies[] = { + {"epall",&Policies::Create::epall}, + {"epff",&Policies::Create::epff}, + {"eplfs",&Policies::Create::eplfs}, + {"eplus",&Policies::Create::eplus}, + {"epmfs",&Policies::Create::epmfs}, + {"eppfrd",&Policies::Create::eppfrd}, + {"eprand",&Policies::Create::eprand}, + {"newest",&Policies::Create::newest}, + }; + std::error_code ec; + + std::filesystem::create_directories(src / "dir",ec); + TEST_CHECK(!ec); + if(ec) + { + std::filesystem::remove_all(tmp_dir); + return; + } + + // A file in a directory, and one at the root, whose parent is empty. + for(const char *relpath : {"dir/file","rootfile"}) + for(const auto &entry : policies) + { + int fd; + int rv; + Branches branches; + const Policy::Create policy(entry.policy); + + std::filesystem::remove_all(other,ec); + std::filesystem::create_directories(other / "dir",ec); + std::ofstream(src / relpath,std::ios::binary|std::ios::trunc) << data; + fd = ::open((src / relpath).c_str(),O_RDWR); + TEST_CHECK(fd >= 0); + if(fd < 0) + continue; + + branches.minfreespace = 0; + TEST_CHECK(branches.from_string(src.string() + ":" + other.string()) == 0); + Branches::Ptr ptr = branches; + + rv = fs::movefile_and_open(policy,ptr,src,relpath,fd); + TEST_CHECK(rv >= 0); + TEST_MSG("policy: %s; path: %s; rv: %d",entry.name,relpath,rv); + if(rv >= 0) + ::close(rv); + + TEST_CHECK(!std::filesystem::exists(src / relpath)); + TEST_CHECK(_file_contents(other / relpath) == data); + TEST_MSG("policy: %s; path: %s",entry.name,relpath); + ::close(fd); + } + + std::filesystem::remove_all(tmp_dir); +} + // Counts that don't fit an int, or whose queue depth product // overflows, must be rejected rather than throw or wrap. void @@ -4588,6 +4667,7 @@ TEST_LIST = {"fs_copyfile_basic",test_fs_copyfile_basic}, {"fs_copyfile_source_changes_cleanup_tmpfiles",test_fs_copyfile_source_changes_cleanup_tmpfiles}, {"fs_movefile_and_open_same_branch",test_fs_movefile_and_open_same_branch}, + {"fs_movefile_and_open_existing_path_policies",test_fs_movefile_and_open_existing_path_policies}, {"readdir_factory_rejects_oversized_counts",test_readdir_factory_rejects_oversized_counts}, {"statvfs_cache_does_not_cache_failure",test_statvfs_cache_does_not_cache_failure}, {"statvfs_cache_keeps_failure_for_timeout",test_statvfs_cache_keeps_failure_for_timeout},