Copy Utilities¶

Copy utilities with reflink (copy-on-write) support for optimized directory operations.

Overview¶

This module provides copytree_reflink(), an optimized directory copy function that leverages filesystem-level copy-on-write (CoW) when available, with automatic fallback to standard shutil.copytree() on unsupported filesystems.

Usage¶

from libvcs._internal.copy import copytree_reflink
import pathlib

src = pathlib.Path("/path/to/source")
dst = pathlib.Path("/path/to/destination")

# Simple copy
copytree_reflink(src, dst)

# With ignore patterns
import shutil

copytree_reflink(
    src,
    dst,
    ignore=shutil.ignore_patterns("*.pyc", "__pycache__"),
)

API Reference¶

Copy directory tree using reflink (CoW) if available, fallback to copytree.

On Btrfs/XFS/APFS, this is significantly faster as it only copies metadata. On ext4 and other filesystems, cp –reflink=auto silently falls back to regular copy.

Parameters:
  • src (pathlib.Path) – Source directory to copy.

  • dst (pathlib.Path) – Destination directory (must not exist).

  • ignore (callable, optional) – Passed to shutil.copytree for fallback. For cp, patterns are applied after copy by deleting ignored files.

Returns:

The destination path.

Return type:

pathlib.Path

Examples

>>> import pathlib
>>> src = tmp_path / "source"
>>> src.mkdir()
>>> (src / "file.txt").write_text("hello")
5
>>> dst = tmp_path / "dest"
>>> result = copytree_reflink(src, dst)
>>> (result / "file.txt").read_text()
'hello'

With ignore patterns:

>>> import shutil
>>> src2 = tmp_path / "source2"
>>> src2.mkdir()
>>> (src2 / "keep.txt").write_text("keep")
4
>>> (src2 / "skip.pyc").write_text("skip")
4
>>> dst2 = tmp_path / "dest2"
>>> result2 = copytree_reflink(src2, dst2, ignore=shutil.ignore_patterns("*.pyc"))
>>> (result2 / "keep.txt").exists()
True
>>> (result2 / "skip.pyc").exists()
False

Implementation Details¶

Strategy¶

The function uses a reflink-first + fallback strategy:

  1. Try cp --reflink=auto - On Linux, this command attempts a reflink copy and silently falls back to regular copy if the filesystem doesn’t support it

  2. Fallback to shutil.copytree() - If cp fails (not found, permission issues, or Windows), use Python’s standard library

Ignore Patterns¶

When using ignore patterns with cp --reflink=auto, the approach differs from shutil.copytree():

  • shutil.copytree: Applies patterns during copy (never copies ignored files)

  • cp –reflink: Copies everything, then deletes ignored files

This difference is acceptable because:

  • The overhead of post-copy deletion is minimal for typical ignore patterns

  • The performance gain from reflinks far outweighs this overhead on CoW filesystems

Use in pytest Fixtures¶

This module is used by the *_repo fixtures in libvcs.pytest_plugin to create isolated test workspaces from cached master copies:

# From pytest_plugin.py
from libvcs._internal.copy import copytree_reflink

@pytest.fixture
def git_repo(...):
    # ...
    copytree_reflink(
        master_copy,
        new_checkout_path,
        ignore=shutil.ignore_patterns(".libvcs_master_initialized"),
    )
    # ...

Benefits for Test Fixtures¶

  1. Faster on CoW filesystems - Users on Btrfs/XFS see 10-100x speedup

  2. No regression elsewhere - ext4/Windows users see no performance change

  3. Safe for writable workspaces - Tests can modify files; master stays unchanged

  4. Future-proof - As more systems adopt CoW filesystems, benefits increase