Skip to content

feat: apply a binary delta from the running version when the server offers one - #328

Merged
codebytere merged 4 commits into
mainfrom
binary-deltas
Aug 14, 2026
Merged

codebytere merged 4 commits into
mainfrom
binary-deltas

Conversation

@codebytere

Copy link
Copy Markdown
Collaborator

Stacked on #327 (base branch stream-and-resume-downloads); the diff here is only the delta work.

An update may now carry delta: a Sparkle BinaryDelta patch (format 3 or 4) from one earlier CFBundleVersion to this release, with its URL, size and SHA-256:

"delta": { "from_version": "412", "url": "https://…/412-to-413.delta", "sha256": "…", "size": 7340032 }

When from_version is the running application's, Squirrel downloads the patch instead of the ZIP (same streaming, resumable SQRLDownloader), checks size and digest before opening it, applies it to a copy of the running bundle inside the update directory, and sends the result through the existing code signing and ElectronSquirrelPreventDowngrades checks. If the delta is for another version, or downloading, verifying, applying or checking it fails in any way, the full ZIP is downloaded in the same check, so offering a delta can never cost an update. All four keys are required; ShipIt and installation are untouched.

The apply side is Sparkle's own, vendored unmodified under Vendor/SparkleDelta (MIT; format 3/4 reader and applier plus bspatch, ~2.9k lines, pinned to sparkle-project/Sparkle@cfa1190) with a small force-included config header standing in for Sparkle's xcconfig defines; the legacy XAR formats and the create side are left out, so patches are made with Sparkle's BinaryDelta create old.app new.app out.delta from the exact signed bundles that shipped. That keeps one implementation of the format rather than a Squirrel-flavoured second one. objc_direct is turned off for the vendored classes so the tests can link Sparkle's generator against them.

Sizing, for a sense of why: between two consecutive releases of a large Electron app (~230 MB zipped per arch, same Electron version) the format-4 LZMA delta is 9.5 MB, applies in a few seconds, and the result is byte-identical to the shipped bundle and passes codesign --verify --deep --strict and Gatekeeper.

Tests: SQRLUpdaterSpec builds real patches between the two TestApplication fixtures with Sparkle's generator (vendored into the test target only) and drives the out-of-process install twice, once from the delta alone (ZIP never requested) and once with a corrupted delta that must fall back to the ZIP, asserting on the fixture server's request log which files were fetched; SQRLUpdateSpec covers parsing and rejects a delta missing any key. 97 tests pass on macOS 26.5 / Xcode 26.5. README documents the key in both feed formats

Base automatically changed from stream-and-resume-downloads to main August 11, 2026 21:18
@codebytere
codebytere force-pushed the binary-deltas branch 2 times, most recently from 4fe3b78 to 227c7ab Compare August 11, 2026 22:05
…ffers one

An update may carry `delta`: a Sparkle BinaryDelta patch from one earlier
CFBundleVersion to this release, with its URL, size and SHA-256. When
`from_version` is the running application's, Squirrel downloads the
patch instead of the ZIP (same streaming, resumable downloader), checks
size and digest, applies it to a copy of the running bundle in the update
directory, and sends the result through the existing code signing and
version checks. If the patch is for another version, or downloading,
verifying, applying or checking it fails in any way, the full ZIP is
downloaded in the same check, so offering a delta can never cost an
update.

The tests build real patches between the two TestApplication fixtures
with Sparkle's generator (built from the same submodule) and drive
the out-of-process install: once from the delta alone, once with a
corrupted delta that has to fall back to the ZIP, asserting on the fixture
server's request log which files were fetched.

The BinaryDelta apply sources (and, for the specs, the create side) are
compiled out of a Sparkle checkout pinned as a submodule at
Carthage/Checkouts/Sparkle, like Mantle and ReactiveObjC; the only
Squirrel-owned piece is the force-included Squirrel/SparkleDelta/
SparkleDeltaConfig.h that stands in for Sparkle's xcconfig defines.

@MarshallOfSound MarshallOfSound left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went through this the same way as #327 (read it all, then had most of it repro'd against the built framework / real ShipIt). The apply itself holds up, I couldn't get anything bad installed, Sparkle's hashes plus our codesign check do their job. The comments are mostly about the delta path quietly degrading into delta + zip for everyone (modes, NFD, resume) plus one actual regression with locked files. The re-apply on every check one and the locked file one I'd want sorted in this PR, the rest is docs / roll notes. A few small things not worth their own thread: PreventDowngrades failures now cost a delta + clone + apply before the zip produces the exact same error, Sparkle leaks its temp diff in $TMPDIR when extraction fails (upstream), the clone carries the old root's quarantine xattr forward and ShipIt's clearQuarantine skips the root (checked, doesn't translocate, it just differs from a zip install), and the PR description still says this is vendored under Vendor/ when it's a submodule now.

Comment thread Squirrel/SQRLUpdater.m Resolved
Comment thread Squirrel/SQRLUpdater.m Resolved
Comment thread Squirrel/SQRLUpdater.m Resolved
Comment thread Squirrel/SQRLUpdater.m
return [RACSignal startLazilyWithScheduler:[RACScheduler schedulerWithPriority:RACSchedulerPriorityBackground] block:^(id<RACSubscriber> subscriber) {
NSLog(@"Applying delta %@ to %@", deltaFileURL.lastPathComponent, sourceURL.path);
NSError *error = nil;
BOOL applied = applyBinaryDelta(sourceURL.path, patchedURL.path, deltaFileURL.path, NO, ^(double progress){}, &error);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bigger problem with pointing this at the installed app: ShipIt strips the g/o write bits off every file it installs (takeOwnershipOfFileSecurity: in SQRLInstaller) and Sparkle's before hash includes the mode of every file and dir, so if the shipped bundle has a single 0664 / 0775 entry in it (umask 002 CI, stuff pulled out of a tarball into app.asar.unpacked, stock electron itself is clean) the delta only ever applies to a copy that has never been through ShipIt. Repro'd with the actual ShipIt binary, hand installed copy applies, ShipIt installed copy fails the before hash in both format 3 and 4, and the output of a successful delta still carries the bits so the next one fails as well. So for an affected publisher it works exactly once while they're testing it and then it's delta + zip for the whole fleet on every release forever, with an NSLog as the only signal. At minimum the README needs to tell people to chmod -R go-w before signing, but given we control both ends I'd rather we either stop stripping bits we don't need to or normalise on our side.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree it's the sharp edge. The before-hash is over the publisher's tree, so the apply side can't repair a delta made from g+w files; the one normal form both ends can share is what ShipIt produces. README now says to chmod -R go-w before signing so shipped, installed and delta trees agree, and calls out the applies-once-then-never failure mode explicitly. I'd rather keep takeOwnership stripping g/o write than reopen installed apps to group writes. New spec installs v1 through ShipIt first and then applies a delta made from the as-built bundles.

Comment thread Squirrel/SQRLUpdater.m Resolved
Comment thread .gitmodules Resolved
Comment thread README.md Resolved
Comment thread Squirrel/SQRLUpdateDelta.m Resolved
Comment thread SquirrelTests/SQRLUpdaterSpec.m Resolved
… pin Sparkle 2.9.5

- a delta that has been applied and staged is not fetched again by later
  checks (its digest plays the ETag's role), so a check per interval no
  longer re-patches, re-stages and re-emits the same update
- a failed apply falls back to the ZIP in a fresh update directory, after
  clearing file flags on and removing whatever the apply left behind
- delta downloads keep their own resume data (delta.resumedata) so the
  delta and ZIP downloaders stop discarding each other's
- a numeric from_version is accepted as a string; size must be a positive
  integer
- Sparkle submodule pinned to the 2.9.5 tag (same sources), README says how
  to bump it, LICENSE carries the Sparkle, bsdiff and sais-lite notices
- README: strip g/o write bits before signing, NFD names, no bzip2
  containers, progress restarts on fallback; config header comment fixed
- specs: staged-once across two checks, ShipIt-installed copy + delta from
  the shipped bundles, from_version mismatch (delta never requested),
  digest mismatch, header corruption, mid-apply corruption

@MarshallOfSound MarshallOfSound left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-swept 9bd1c83, everything from the first round is sorted (checked the submodule really is the 2.9.5 tag, and the digest memo gets cleared in the right place). One thing left over from the locked file thread plus a few small ones.

Comment thread Squirrel/SQRLUpdater.m Resolved
Comment thread Squirrel/SQRLUpdater.m Resolved
Comment thread Squirrel/SQRLUpdater.m Resolved
Comment thread SquirrelTests/SQRLUpdaterSpec.m Resolved
…able deltas

- the apply copies file flags (a Finder-locked file's uchg) from the running
  app onto its clone; clear them on the patched bundle before verifying and
  staging it, since ShipIt cannot strip xattrs from or replace an immutable
  file, and clear them on a failed apply's leftovers before removing those
- a delta that downloaded intact but would not apply or verify is remembered
  like an applied one, and later checks go straight to the ZIP
- the fresh directory a fallback downloads into is removed again when the
  ZIP path errors or has nothing new; cleanUp stays quiet about a directory
  the fallback already removed
- specs: a locked file the delta leaves alone (installs from the delta), and
  one it rewrites (falls back, and nothing locked is left in update storage)
@codebytere
codebytere merged commit fffea30 into main Aug 14, 2026
6 checks passed
@codebytere
codebytere deleted the binary-deltas branch August 14, 2026 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants