﻿# Handle short copy_file_range results in a model-cache copier

Treat copy_file_range success as a completed byte prefix, preserve offset ownership and verify the entire expected artifact before publication.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/guide-storage-copy-file-range-short/
Language: en

A cache copier calls copy_file_range once, receives a positive result and publishes the destination as complete. The API promises the returned number of copied bytes, not automatic completion of the entire requested range. A short successful result needs progress accounting. Without it, a perfectly legal API outcome can become an incomplete model artifact exposed under a ready name.

## Preserve the intended range

Record the accepted source identity, starting offsets and required byte count before copying. Source mutation during the operation is a separate consistency problem; choosing an efficient copying API does not create an immutable snapshot. Keep the destination private while work remains, and distinguish preparing a whole artifact from copying one intentional subrange of a larger container file.

## Read the completed prefix

A nonnegative result describes actual copied progress and can be smaller than the request. Advance by the returned count and retain the remaining requirement. Do not repeatedly request the original full count while also moving the start position, or assume a positive return means the loop is done. Progress and completion are different states even when the syscall reports no error.

## Track offset ownership

The API can use explicit offset pointers or the descriptors' current offsets according to its contract. Preserve which form the copier uses. Mixing both assumptions can skip or repeat bytes, especially when descriptors are shared with other work. Coordinate access or use an appropriate isolated offset design rather than trusting a descriptor number to identify a private sequential cursor.

## Handle EOF and failure separately

A zero result can indicate the source range reached its end; that does not satisfy a larger expected artifact length. Errors can also require a supported fallback or failure outcome under the installed filesystem and kernel. Preserve bytes already completed before choosing the next action. Do not restart a fallback blindly at the beginning of a partially prepared destination without explicit replacement or offset rules.

## Publish only verified completion

After the intended range is complete, validate the artifact's accepted length and integrity and apply required durability steps before publication. Test short-progress and interrupted-copy handling in an isolated harness. The useful result makes partial progress explicit. It does not prove zero-copy hardware execution, preserve sparse layout automatically or establish that a copied range represents one coherent model revision without a separate source-identity contract.

## Primary references

[Linux copy_file_range Progress and Offsets](https://man7.org/linux/man-pages/man2/copy_file_range.2.html).

[Linux Open File Descriptions and Directory Handles](https://man7.org/linux/man-pages/man2/open.2.html).

[Linux File and Directory Synchronization](https://man7.org/linux/man-pages/man2/fsync.2.html).

Related reading: [Init container downloads an artifact again: making preparation idempotent](/journal/kubernetes-init-artifact-download/); [Validate safetensors weight artifacts separately from repository code](/journal/safetensors-artifact-validation/).
