From e9a93ece352361a2e57f2f8715d8cd14cb07db24 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Wed, 4 Nov 2026 16:20:23 +0100 Subject: [PATCH] lib.util.copy(): Require recursive for directories copy() handed a non-recursive copy of a directory to the single-object get() path. For a plain context the backend then fails with a transport error, and for a kvfile store the store root is a branch, not an entry, so the get() fails with a FileNotFoundError naming a transformed address: copying "kvfile:///etc" reported "FileNotFoundError: etc". Require recursive for a directory source that is not fetchable as a whole, as cp requires -r for directories: copy() now raises with a message naming the source and the recursive option, instead of falling through to get(). Add a unit test that covers a directory source for a plain context and a kvfile store. Signed-off-by: Jan Lindemann Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0 --- src/python/jw/pkg/lib/util.py | 8 +++++++- test/unit/python/jw/pkg/lib/util/test.py | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/python/jw/pkg/lib/util.py b/src/python/jw/pkg/lib/util.py index 49851be9..8f108da9 100644 --- a/src/python/jw/pkg/lib/util.py +++ b/src/python/jw/pkg/lib/util.py @@ -259,7 +259,13 @@ async def copy( ret += '/' + basename src_root = '' if ctx.src.chroot else ctx.src.root s_st = await ctx.src.stat(src_root) - if recursive and s_st.is_dir and not s_st.is_gettable: + if s_st.is_dir and not s_st.is_gettable: + # -- A directory is not a single fetchable object, + # and cp copies directories only with -r + if not recursive: + raise Exception( + f'Copying directory "{src}" requires recursive=True' + ) return await __copy_tree(src_root, ret) result = await ctx.src.get(src_root, throw = True) diff --git a/test/unit/python/jw/pkg/lib/util/test.py b/test/unit/python/jw/pkg/lib/util/test.py index 0520ee3b..8771a905 100644 --- a/test/unit/python/jw/pkg/lib/util/test.py +++ b/test/unit/python/jw/pkg/lib/util/test.py @@ -174,6 +174,18 @@ async def run_tests() -> None: except Exception: pass + # -- A directory without recursive fails with a clear error, + # for a plain context and for a kvfile store alike + for src in [ + uri(os.path.join(tmp, 'src/a')), + f'kvfile:///{tmp}/src/a', + ]: + try: + await copy(src, uri(os.path.join(tmp, 'dst/dir-copy'))) + raise AssertionError('expected an exception') + except Exception as e: + assert 'recursive' in str(e), e + def main() -> None: asyncio.run(run_tests()) print('All util copy tests passed') -- 2.56.1