Skip to content

fix(receiver): key --write-devices on the destination, not the flist - #7583

Merged
oferchen merged 1 commit into
masterfrom
fix/write-devices-dest-keyed
Sep 1, 2026
Merged

oferchen merged 1 commit into
masterfrom
fix/write-devices-dest-keyed

Conversation

@oferchen

@oferchen oferchen commented Sep 1, 2026 •

Copy link
Copy Markdown
Owner

Problem

--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.
The receiver keyed its device predicate on file_entry.is_device(), which is
therefore permanently false: the in-place commit ran set_len() against the
device, the server failed EINVAL, and the client saw exit 12 where upstream
3.5.0 exits 0.

Fix

One boolean moves. is_device_target is now computed from the DESTINATION's
symlink_metadata, matching upstream:

  • receiver.c:1170 - write_to_device = write_devices && IS_DEVICE(st.st_mode),
    where st is the do_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 via
    do_open_atfd(), which forces O_NOFOLLOW, so a symlinked destination is not
    followed; symlink_metadata is the matching decision.
  • rsync.h:1394 - IS_DEVICE is S_ISCHR || S_ISBLK; FIFOs and sockets are
    IS_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->mode to S_IFBLK
around receive_data() and restoring it immediately after
(receiver.c:1256-1271). That save/restore exists only because upstream's
file_struct is allocated with no DEV_EXTRA_CNT, so a lingering S_IFBLK
makes set_stat_xattr's F_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_len on !is_device_target (disk_commit/process/commit.rs:180, citing
receiver.c:652 !IS_DEVICE(file->mode)). Porting the mode rewrite would import
upstream's allocation hazard for no gain.

Tests

crates/transfer/tests/write_devices_device_destination.rs - a remote-shell push
through 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-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 -
and emits a counted, reason-carrying SKIP rather than a silent pass.

Mutation-proven on Linux, each independently:

mutation result
none (the fix) 2 tests run: 2 passed, 0 skipped
predicate reverted to file_entry.is_device() 2 tests run: 1 passed, 1 failed - device cell left: Some(12), right: Some(0); regular-file companion green
existing truncate gate !begin.is_device_target removed 2 tests run: 1 passed, 1 failed - same exit 12; companion green

The 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 0
  • cargo clippy --workspace --all-targets --all-features --no-deps -- -D warnings: exit 0
  • python3 tools/ci/citation_drift_audit.py: exit 0
  • targeted nextest: as tabled above (full workspace left to CI)

Why the mode rewrite is a workaround for a constraint oc does not have

Upstream's own comment at receiver.c:1252-1258 says why the save/restore exists:

--write-devices writes a regular source file's content into an existing
destination device. Flip file->mode to S_IFBLK just for receive_data()'s
ftruncate gate (!IS_DEVICE), then restore it right after -- the file_struct
was built as a regular file with no DEV_EXTRA_CNT, so leaving it S_IFBLK
would make set_stat_xattr's F_RDEV_P() read past the allocation.

Upstream reuses file->mode as the signal channel because it has nowhere else to
put 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_target
field, 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 neither
is fixed here. Each is scoped out with a reason, and neither is silently dropped.

Obstacle classification — generator.c:2148
if (statret == 0 && !(stype == FT_REG || (write_devices && stype == FT_DEVICE)))
then delete_item(... DEL_FOR_FILE). Upstream deletes a non-regular destination
as 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 in
crates/engine local-copy or the backup/partial-dir paths. So the exemption's
effect 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:2226
if (write_devices && IS_DEVICE(sx.st.st_mode) && sx.st.st_size == 0) then
get_device_size(fd, fnamecmp). A device reports st_size == 0, so without the
substitution the generator sees a zero-length basis.
Measured in oc: get_device_size exists (generator/context.rs:1337) but is
the sender-side --copy-devices path, citing sender.c:404-419 and
flist.c:1711. write_devices appears nowhere under
crates/transfer/src/generator/. The receiver-side substitution is genuinely
ABSENT. 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 cfg

ftruncate() on a device node returns EINVAL on Linux and succeeds silently on
macOS. 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, keeps
working on a BSD that behaves either way, and needs no root — which removes the
root-fixture problem entirely.

`--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.
@github-actions github-actions Bot added the bug Something isn't working label Sep 1, 2026
@oferchen
oferchen merged commit 92c8fb2 into master Sep 1, 2026
70 of 71 checks passed
@oferchen
oferchen deleted the fix/write-devices-dest-keyed branch September 1, 2026 12:17
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant