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.
Why Reflinks?¶
Traditional file copying reads source bytes and writes them to the destination. On modern copy-on-write filesystems like Btrfs, XFS, and APFS, reflinks provide a more efficient alternative:
Operation |
Traditional Copy |
Reflink Copy |
|---|---|---|
Bytes transferred |
All file data |
Metadata only |
Time complexity |
O(file size) |
O(1) |
Disk usage |
2x original |
~0 (shared blocks) |
On modification |
Original unchanged |
CoW creates new blocks |
Filesystem Support¶
Filesystem |
Reflink Support |
Notes |
|---|---|---|
Btrfs |
âś… Native |
Full CoW support |
XFS |
âś… Native |
Requires reflink=1 mount option |
APFS |
âś… Native |
macOS 10.13+ |
ext4 |
❌ Fallback |
Falls back to byte copy |
NTFS |
❌ Fallback |
Windows uses shutil.copytree |
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¶
-
libvcs._internal.copy.copytree_reflink(src, dst, ignore=None)¶libvcs._internal.copy.copytree_reflink(src, dst, ignore=None)¶
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:
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:
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 itFallback to
shutil.copytree()- Ifcpfails (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¶
Faster on CoW filesystems - Users on Btrfs/XFS see 10-100x speedup
No regression elsewhere - ext4/Windows users see no performance change
Safe for writable workspaces - Tests can modify files; master stays unchanged
Future-proof - As more systems adopt CoW filesystems, benefits increase