Create space-saving clones on macOS with Python
9 months ago
- #macOS
- #Python
- #APFS
- APFS on macOS supports space-saving clones, allowing multiple file copies without additional disk space.
- Cloned files share data but can be edited independently, using copy-on-write for efficiency.
- Cloning is faster and uses less disk space than copying, especially beneficial for large files.
- Two main benefits of cloning: reduced disk space usage and faster operation compared to copying.
- Methods to clone files on macOS include using Finder's Duplicate command, `cp -c` on the command line, and the `clonefile()` syscall.
- Python can clone files by shelling out to `cp -c` or by calling the `clonefile()` function using `ctypes`.
- Using `cp -c` in Python is simpler and more readable, with error handling wrapped in a function.
- Directly calling `clonefile()` with `ctypes` is more complex but avoids the overhead of spawning an external process.
- In practice, shelling out to `cp -c` is preferred for its simplicity and fallback to copying if cloning is not supported.
- File cloning significantly improved performance in a project involving large files on an external USB hard drive.