Skip to content

fix(engine): key --write-devices on the local-copy destination - #7796

Merged
oferchen merged 2 commits into
masterfrom
fix/local-write-devices-destination
Sep 9, 2026
Merged

oferchen merged 2 commits into
masterfrom
fix/local-write-devices-destination

Conversation

@oferchen

@oferchen oferchen commented Sep 9, 2026

Copy link
Copy Markdown
Owner

Local copy had no --write-devices predicate at all, so a device node standing at
the destination was never cleared: without the flag oc wrote through the device
where upstream replaces it with a regular file.

The filed premise is refuted

This was filed as "structurally absent, but accidentally correct 3/3". Measured
against real rsync 3.5.0 binaries, 10 of 52 local cells diverge — and one of
them is not a divergence but a failure.

cell rsync 3.5.0 oc before oc after
--inplace, block-dev dest 0, dest REGULAR 0, device written through 0, REGULAR
--append, block-dev dest 0, dest REGULAR 0, written through 0, REGULAR
--inplace --sparse, block dev 0, dest REGULAR exit 23, ftruncate EINVAL 0, REGULAR
--inplace --no-whole-file 0, dest REGULAR 0, written through 0, REGULAR
--inplace -r, device in tree 0, dest REGULAR 0, device kept 0, REGULAR
--inplace, CHAR dev dest 0, dest REGULAR 0, device kept 0, REGULAR
--append, CHAR dev dest 0, dest REGULAR 0, device kept 0, REGULAR
--backup --inplace, dev 0, REGULAR + dst~ = the node 0, device kept matches
-i, dev dest >f+++++++++ >f.sT...... >f+++++++++
--stats, dev dest created files: 1 created files: 0 1

"Written through" is the substantive one: without --write-devices the operator
did not ask to write to the device, and oc did anyway.

Upstream, read first

write_devices is consulted at exactly six sites in 3.5.0. Two were already ported
(options.c:2555 inplace = 1 via #7260; options.c:3150 server args). The
make-way decision is generator.c:2148:

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;

The new predicate is that line's write_devices && stype == FT_DEVICE half and
nothing more; IS_DEVICE is rsync.h:1394 (S_ISCHR || S_ISBLK), so char and
block both count. The statret = -1 reset at :2151 is what makes the itemize row
read as a creation rather than a change — that is why the -i and --stats rows
above move together with the filesystem outcome.

Disposal follows delete.c:227-238: under --backup the node is renamed
(make_backup(fbuf, True)), not hard-linked. A hard link would leave the device
node standing at the destination, which is the whole thing being fixed.

Consistent with #7583: no file->mode → S_IFBLK rewrite anywhere. The decision is
keyed on the destination at every upstream site.

How it was measured

Not by reading. Upstream 3.5.0 and oc-rsync were both cross-built for
aarch64-linux-musl with zig cc and run as root in a Linux VM, on a tmpfs
mount, with the destination a loop device over a 64 KiB backing file — so the bytes
that reach the device are readable back and "written through" is an observation, not
an inference.

Negative controls that agreed before and still agree: --write-devices keeps and
writes the node across plain/-a/--checksum/--update/--whole-file/
--no-whole-file/recursive/empty-source; --ignore-existing and --dry-run leave
it standing; --existing replaces it; regular destinations untouched; a device
source is unaffected across -D, --copy-devices and --write-devices;
--write-devices --partial-dir still exits 1 with upstream's text.

Mutations

mutation kills
predicate always false (the pre-fix shape) 4 integration + 1 unit
predicate ignores --write-devices 2 integration + 1 unit
--ignore-existing ordering guard dropped ignore_existing_leaves_a_device_destination_alone
statret = -1 reset dropped a_replaced_device_destination_itemizes_as_a_creation

Both predicate mutations are killed unprivileged on macOS too, by the unit cells
plus the /dev/null cell — so ordinary CI is not blind to this, despite the
oracle needing root.

Residuals — measured, not fixed, each needs its own decision

  1. --inplace over a FIFO destination hangs indefinitely (upstream: exit 0, node
    replaced). Same missing make-way decision, different disjunct; only the
    FT_DEVICE half is in scope here. This is the most serious of the five.
  2. --write-devices --sparse: oc exits 23 failed to truncate; upstream exits 11
    write failed on %s from sparse_end(), whose own ftruncate is deliberately
    not IS_DEVICE-gated. Exit-code mapping, not the predicate.
  3. --write-devices --preallocate: oc 23, upstream 0 — receiver.c:476's
    (!inplace_sizing || total_size > size_r) uses the device's real size from
    generator.c:2226 get_device_size(), which local copy has no analogue of; and a
    failing do_fallocate is only FWARNING upstream.
  4. --write-devices into a device the caller does not own: oc 23 failed to apply dest_mode, upstream 0 — it skips the chmod when the bits already match. A
    regular-file control shows the mode value itself is correct.
  5. --write-devices --backup: upstream's backup of the kept device is a regular file
    holding the pre-image; oc hard-links the node.

Gates (pinned 1.88.0)

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, -p transfer 2938; the device/obstacle/backup subset 66 passed.
7/7 of the new tests green as root, and 7/7 unprivileged on macOS.

Not run: interop, the upstream testsuite legs, Windows, musl. The Linux binary used
for the oracle was built without the acl feature (no libacl in the zig sysroot),
so no cell exercised ACLs. No expect-manifest is touched.

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.
@oferchen
oferchen enabled auto-merge (squash) September 9, 2026 17:26
@github-actions github-actions Bot added the bug Something isn't working label Sep 9, 2026
@oferchen
oferchen merged commit e91303b into master Sep 9, 2026
75 checks passed
@oferchen
oferchen deleted the fix/local-write-devices-destination branch September 9, 2026 21:10
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