Repository navigation
fix(receiver): key --write-devices on the destination, not the flist - #7583
Merged
Merged
Conversation
`--write-devices` streams a REGULAR source file's contents into an EXISTING device node, so the sender's file-list entry describes a regular file by construction. Keying the receiver's predicate on `file_entry.is_device()` left it permanently false, the in-place commit ran `set_len()` against the device, and the server failed EINVAL - surfacing to the client as exit 12 where upstream 3.5.0 exits 0. Upstream keys the same predicate on the destination: receiver.c:1170 computes `write_to_device = write_devices && IS_DEVICE(st.st_mode)` from the `do_fstat()` of the opened destination descriptor (receiver.c:1143-1145). That descriptor is opened via do_open_atfd(), which forces O_NOFOLLOW (receiver.c:1076, syscall.c:3835), so `symlink_metadata` is the matching decision here. Only the predicate moves. oc already carries the analogue of upstream's truncate suppression: the in-place `set_len` is gated on `!is_device_target` (disk_commit/process/commit.rs, citing receiver.c:652 `!IS_DEVICE(file->mode)`), and it is fed by this same boolean. Upstream reaches that gate by rewriting `file->mode` to S_IFBLK around `receive_data()` and restoring it immediately after (receiver.c:1256-1271); that save/restore exists because upstream's file_struct was built with no DEV_EXTRA_CNT and a lingering S_IFBLK makes `set_stat_xattr`'s `F_RDEV_P()` read past the allocation. oc carries the decision in a message field instead of mutating shared file-list state, so the rewrite is deliberately NOT ported - it would import that hazard for no gain. The test rides a remote-shell push because the predicate lives on the receiver pipeline; a local copy never reaches it (that is the engine crate's local-copy executor, which has no --write-devices predicate at all) and would pass on the unfixed tree. It self-gates on a runtime probe of whether the platform rejects `ftruncate()` on a device: Linux returns EINVAL, macOS accepts it silently, so the assertion would be vacuous there.
oferchen
added a commit
that referenced
this pull request
Sep 9, 2026
The local-copy executor had no `--write-devices` predicate at all. It reached
upstream's answer only because its DEFAULT write strategy stages into a temp
file and renames over whatever stands at the destination; every strategy that
opens the final name instead - `--inplace`, `--append`, and the `--inplace`
that `--write-devices` itself implies (options.c:2555) - streamed the payload
THROUGH a device node the operator had not authorised writing through.
Upstream decides this once, keyed on the DESTINATION:
generator.c:2148-2153
if (statret == 0 && !(stype == FT_REG || (write_devices && stype == FT_DEVICE))) {
if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_FILE) != 0)
goto cleanup;
statret = -1;
stat_errno = ENOENT;
}
The source is a REGULAR file in both arms - `--write-devices` streams a regular
file's contents into an existing device - so nothing in the entry being sent
identifies the case. That is the same keying the receiver pipeline uses
(crates/transfer/.../candidates.rs, PR #7583), and the reason a predicate
derived from the flist can only ever be wrong.
This adds the DEVICE arm of that condition to the local path as one decision
site next to the directory arm that already lives there. It is
`delete_item()`'s NON-directory branch, which differs from the directory branch
in two ways that both matter: the node is backed up rather than unlinked under
`--backup`, and the backup prefers an outright RENAME
(delete.c:230 `make_backup(fbuf, True)`) instead of the hard-link tier the
generator's own `make_backup(fname, False)` uses - a hard link would leave the
device standing at the destination and the transfer would write through it
after all. The `statret = -1` reset is carried too, so the entry itemizes as a
creation rather than as an update against a node that is no longer there.
`--write-devices` reaches the engine as a flag of its own rather than only as
the `--inplace` it implies: `--inplace` alone never authorises writing through
a device node, so the two cannot share one boolean.
Measured against rsync 3.5.0 (protocol 32) as root on Linux (aarch64, a tmpfs
mounted `dev`, the destination a loop device over a 64 KiB backing file so the
bytes that reach the device are readable afterwards), 52 local cells:
| cell | rsync 3.5.0 | oc before |
|----------------------------------|------------------------|------------------------|
| `--inplace`, device dest | 0, dest REGULAR | 0, device written through |
| `--append`, device dest | 0, dest REGULAR | 0, device written through |
| `--inplace --sparse`, device | 0, dest REGULAR | 23, ftruncate EINVAL |
| `--inplace --no-whole-file` | 0, dest REGULAR | 0, device written through |
| `--inplace -r`, device in tree | 0, dest REGULAR | 0, device kept |
| `--inplace`, CHAR device dest | 0, dest REGULAR | 0, device kept |
| `--append`, CHAR device dest | 0, dest REGULAR | 0, device kept |
| `--backup --inplace`, device | 0, dest REGULAR, `~` the node | 0, device kept |
| `-i`, device dest | `>f+++++++++` | `>f.sT......` |
| `--stats`, device dest | `created files: 1` | `created files: 0` |
All ten now agree. The cells that already agreed are kept as negative
controls and still agree: `--write-devices` keeps and writes the node (plain,
`-a`, `--checksum`, `--update`, `--whole-file`, `--no-whole-file`, recursive),
`--ignore-existing` leaves it standing (generator.c:1780 is tested ahead of the
removal), `--dry-run` leaves it standing while still reporting a creation,
`--existing` still replaces it, a regular destination is untouched, and a device
SOURCE is unaffected in every combination of `-D` / `--copy-devices` /
`--write-devices`.
Three device cells still diverge and are NOT addressed here, each owned
elsewhere:
- `--write-devices --sparse`: upstream exits 11 `write failed on %s` from
`sparse_end()` (receiver.c:639-644, whose own ftruncate is deliberately not
IS_DEVICE-gated); oc exits 23 from the equivalent truncate. The divergence
is the exit-code and message mapping of a failing sparse end, not the
predicate.
- `--write-devices --preallocate`: upstream never attempts the reservation,
because receiver.c:476's `(!inplace_sizing || total_size > size_r)` sees the
device's real size (generator.c:2226 `get_device_size`); oc has no
device-size probe, attempts `fallocate` and exits 23, where upstream would
only have warned (receiver.c:480 `rsyserr(FWARNING, ...)`).
- `--write-devices --backup`: upstream's backup of the kept device is a
regular file holding the pre-image; oc hard-links the node.
Two further findings from the same sweep, both outside this change:
- `--inplace` over a FIFO destination HANGS indefinitely (upstream exits 0
having replaced it). Same missing make-way decision, different disjunct:
only the FT_DEVICE half is ported here.
- `--write-devices` into a device the caller does not own exits 23 with
`failed to apply dest_mode` where upstream exits 0, because upstream skips
the `chmod` when the permission bits already match and `dest_mode()`'s
`exists` arm has already made them match.
Verified on the pinned toolchain (1.88.0): `tools/ci/check_rustfmt_all.py`
clean, `clippy -p engine -p core -p bin --all-targets --no-deps -D warnings`
clean, `nextest -p engine --all-features` 5125 passed, `-p core` 2851 passed,
`-p transfer` 2938 passed.
Mutation-proved by reverting each half and rebuilding for Linux, run as root:
| mutation | tests that failed |
|-----------------------------------------|-------------------|
| predicate always false (pre-fix shape) | inplace_..._replaces, append_..._replaces, ..._itemizes_as_a_creation, ..._is_backed_up_not_unlinked, plus the `write_devices=false` unit cell |
| predicate ignores `--write-devices` | write_devices_keeps_a_device_destination, write_devices_never_makes_way_at_a_shared_device, plus the `write_devices=true` unit cell |
| `--ignore-existing` ordering guard dropped | ignore_existing_leaves_a_device_destination_alone |
| `statret = -1` reset dropped | a_replaced_device_destination_itemizes_as_a_creation |
Most of those cells need `mknod(2)`, so they self-gate and say so out loud on
an unprivileged host; the two that run everywhere - the predicate unit cells and
the `/dev/null` cell - kill both predicate mutations on their own. The
`/dev/null` cell deliberately runs ONLY where device nodes cannot be created:
where they can, a mutated tree unlinks the host's `/dev/null` for real.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
--write-devicesstreams a REGULAR source file's contents into an EXISTING devicenode, so the sender's file-list entry describes a regular file by construction.
The receiver keyed its device predicate on
file_entry.is_device(), which istherefore permanently false: the in-place commit ran
set_len()against thedevice, the server failed
EINVAL, and the client saw exit 12 where upstream3.5.0 exits 0.
Fix
One boolean moves.
is_device_targetis now computed from the DESTINATION'ssymlink_metadata, matching upstream:receiver.c:1170-write_to_device = write_devices && IS_DEVICE(st.st_mode),where
stis thedo_fstat()of the opened destination descriptor(
receiver.c:1143-1145), never the file-list entry.receiver.c:1076/syscall.c:3835- that descriptor is opened viado_open_atfd(), which forcesO_NOFOLLOW, so a symlinked destination is notfollowed;
symlink_metadatais the matching decision.rsync.h:1394-IS_DEVICEisS_ISCHR || S_ISBLK; FIFOs and sockets areIS_SPECIAL(rsync.h:1393) and are NOT device targets.All citations were re-read against the 3.5.0 pin.
What is deliberately NOT ported
Upstream reaches its truncate suppression by rewriting
file->modetoS_IFBLKaround
receive_data()and restoring it immediately after(
receiver.c:1256-1271). That save/restore exists only because upstream'sfile_structis allocated with noDEV_EXTRA_CNT, so a lingeringS_IFBLKmakes
set_stat_xattr'sF_RDEV_P()read past the allocation.oc carries the decision in a message field (
BeginMessage.is_device_target)rather than mutating shared file-list state, and already gates the in-place
set_lenon!is_device_target(disk_commit/process/commit.rs:180, citingreceiver.c:652!IS_DEVICE(file->mode)). Porting the mode rewrite would importupstream's allocation hazard for no gain.
Tests
crates/transfer/tests/write_devices_device_destination.rs- a remote-shell pushthrough a fake rsh, because the predicate lives on the receiver pipeline and a
local copy never reaches it (that is the engine crate's local-copy executor,
which has no
--write-devicespredicate at all and would pass on the unfixedtree). It self-gates on a runtime probe of whether the platform rejects
ftruncate()on a device - Linux returnsEINVAL, macOS accepts it silently -and emits a counted, reason-carrying SKIP rather than a silent pass.
Mutation-proven on Linux, each independently:
2 tests run: 2 passed, 0 skippedfile_entry.is_device()2 tests run: 1 passed, 1 failed- device cellleft: Some(12), right: Some(0); regular-file companion green!begin.is_device_targetremoved2 tests run: 1 passed, 1 failed- same exit 12; companion greenThe second mutation is the non-vacuity proof: it shows the fixture genuinely
reaches the in-place truncate, so the passing cell is not passing by accident.
Gates
cargo fmt --all -- --check: exit 0cargo clippy --workspace --all-targets --all-features --no-deps -- -D warnings: exit 0python3 tools/ci/citation_drift_audit.py: exit 0Why the mode rewrite is a workaround for a constraint oc does not have
Upstream's own comment at
receiver.c:1252-1258says why the save/restore exists:Upstream reuses
file->modeas the signal channel because it has nowhere else toput it, and then must undo the reuse before anything reads the mode again. oc
already carries the same signal as a first-class
BeginMessage.is_device_targetfield, which no other reader interprets and which cannot read past an
allocation. Porting the rewrite would import upstream's hazard to solve a
problem oc does not have. Not ported, deliberately.
The two adjacent upstream rules, reported not dropped
Both are generator-side (
recv_generator); this PR is receiver-side, so neitheris fixed here. Each is scoped out with a reason, and neither is silently dropped.
Obstacle classification —
generator.c:2148if (statret == 0 && !(stype == FT_REG || (write_devices && stype == FT_DEVICE)))then
delete_item(... DEL_FOR_FILE). Upstream deletes a non-regular destinationas an obstacle, exempting a device under
--write-devices.Measured in oc: there is no destination-type classification on the receiver
pipeline at all — every obstacle-removal site (
remove_existing_destination,clear_obstructing_non_directory,clear_partial_dir_obstruction) lives incrates/enginelocal-copy or the backup/partial-dir paths. So the exemption'seffect holds for free on this path (nothing can delete the device), and the
test asserts the device survives. The wider gap — oc not classifying a
non-regular destination on the receiver path at all — is real but is not
--write-devices-specific and belongs in its own task.Device size discovery —
generator.c:2226if (write_devices && IS_DEVICE(sx.st.st_mode) && sx.st.st_size == 0)thenget_device_size(fd, fnamecmp). A device reportsst_size == 0, so without thesubstitution the generator sees a zero-length basis.
Measured in oc:
get_device_sizeexists (generator/context.rs:1337) but isthe sender-side
--copy-devicespath, citingsender.c:404-419andflist.c:1711.write_devicesappears nowhere undercrates/transfer/src/generator/. The receiver-side substitution is genuinelyABSENT. Correctness is unaffected (the transfer completes and the payload lands);
the cost is delta efficiency — a device basis looks 0-length, so the generator
builds no signature and the whole file goes as literal. Scoped out of this PR
because it is an efficiency fix on a different path, needing its own measurement.
Reusable finding: gate device assertions on a runtime probe, not
cfgftruncate()on a device node returnsEINVALon Linux and succeeds silently onmacOS. Any test that asserts on this defect therefore passes vacuously on
macOS. This test probes the behaviour at runtime rather than keying on
cfg(target_os = "linux"): the probe states the actual precondition, keepsworking on a BSD that behaves either way, and needs no root — which removes the
root-fixture problem entirely.