Skip to content

Commit fffea30

Browse files
authored
feat: apply a binary delta from the running version when the server offers one (#328)
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](https://sparkle-project.org) BinaryDelta patch (format 3 or 4) from one earlier `CFBundleVersion` to this release, with its URL, size and SHA-256: ```json "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
1 parent 61438d0 commit fffea30

16 files changed

Lines changed: 782 additions & 42 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
submodules: false
3636

3737
- name: Init submodules
38-
# Recursive init pulls 15 nested clones; only 8 are used by the
38+
# Recursive init pulls 15 nested clones; only 9 are used by the
3939
# build (the rest are Mantle's / ReactiveObjC's / Quick's own
4040
# test deps).
4141
run: |
@@ -44,6 +44,7 @@ jobs:
4444
Carthage/Checkouts/Nimble \
4545
Carthage/Checkouts/Quick \
4646
Carthage/Checkouts/ReactiveObjC \
47+
Carthage/Checkouts/Sparkle \
4748
Carthage/Checkouts/xcconfigs \
4849
External/OHHTTPStubs
4950
git -C Carthage/Checkouts/Mantle submodule update --init --depth 1 \

‎.gitmodules‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@
1616
[submodule "Carthage/Checkouts/ReactiveObjC"]
1717
path = Carthage/Checkouts/ReactiveObjC
1818
url = https://github.com/ReactiveCocoa/ReactiveObjC.git
19+
[submodule "Carthage/Checkouts/Sparkle"]
20+
path = Carthage/Checkouts/Sparkle
21+
url = https://github.com/sparkle-project/Sparkle.git

‎Carthage/Checkouts/Sparkle‎

Submodule Sparkle added at 79bc9e8

‎LICENSE‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,93 @@ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
1818
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
1919
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2020
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
--------------------------------------------------------------------------------
23+
Squirrel.framework includes Sparkle's BinaryDelta implementation and the bsdiff
24+
sources Sparkle vendors (Carthage/Checkouts/Sparkle). Their notices follow.
25+
--------------------------------------------------------------------------------
26+
27+
Copyright (c) 2006-2013 Andy Matuschak.
28+
Copyright (c) 2009-2013 Elgato Systems GmbH.
29+
Copyright (c) 2011-2014 Kornel Lesiński.
30+
Copyright (c) 2015-2017 Mayur Pawashe.
31+
Copyright (c) 2014 C.W. Betts.
32+
Copyright (c) 2014 Petroules Corporation.
33+
Copyright (c) 2014 Big Nerd Ranch.
34+
All rights reserved.
35+
36+
Permission is hereby granted, free of charge, to any person obtaining a copy of
37+
this software and associated documentation files (the "Software"), to deal in
38+
the Software without restriction, including without limitation the rights to
39+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
40+
the Software, and to permit persons to whom the Software is furnished to do so,
41+
subject to the following conditions:
42+
43+
The above copyright notice and this permission notice shall be included in all
44+
copies or substantial portions of the Software.
45+
46+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
48+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
49+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
50+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
51+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
52+
53+
=================
54+
EXTERNAL LICENSES
55+
=================
56+
57+
bspatch.c and bsdiff.c, from bsdiff 4.3 <http://www.daemonology.net/bsdiff/>:
58+
59+
Copyright 2003-2005 Colin Percival
60+
All rights reserved
61+
62+
Redistribution and use in source and binary forms, with or without
63+
modification, are permitted providing that the following conditions
64+
are met:
65+
1. Redistributions of source code must retain the above copyright
66+
notice, this list of conditions and the following disclaimer.
67+
2. Redistributions in binary form must reproduce the above copyright
68+
notice, this list of conditions and the following disclaimer in the
69+
documentation and/or other materials provided with the distribution.
70+
71+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
72+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
73+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
75+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
79+
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
80+
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
81+
POSSIBILITY OF SUCH DAMAGE.
82+
83+
--
84+
85+
sais.c and sais.h, from sais-lite (2010/08/07) <https://sites.google.com/site/yuta256/sais>:
86+
87+
The sais-lite copyright is as follows:
88+
89+
Copyright (c) 2008-2010 Yuta Mori All Rights Reserved.
90+
91+
Permission is hereby granted, free of charge, to any person
92+
obtaining a copy of this software and associated documentation
93+
files (the "Software"), to deal in the Software without
94+
restriction, including without limitation the rights to use,
95+
copy, modify, merge, publish, distribute, sublicense, and/or sell
96+
copies of the Software, and to permit persons to whom the
97+
Software is furnished to do so, subject to the following
98+
conditions:
99+
100+
The above copyright notice and this permission notice shall be
101+
included in all copies or substantial portions of the Software.
102+
103+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
104+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
105+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
106+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
107+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
108+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
109+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
110+
OTHER DEALINGS IN THE SOFTWARE.

‎README.md‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ repository (under `Carthage/Checkouts/`, a directory name kept from when they
4545
were fetched with Carthage); `git submodule update --init` or `script/bootstrap`
4646
checks them out.
4747

48+
Binary delta support compiles Sparkle's BinaryDelta sources and the bsdiff it
49+
vendors straight out of a third submodule, `Carthage/Checkouts/Sparkle`, pinned
50+
to a Sparkle release tag (currently 2.9.5); nothing of Sparkle is linked as a
51+
framework and applications need not ship it. To move the pin, check out the new
52+
tag in the submodule, build, run the tests, and commit the submodule change; the
53+
files involved are listed under the SparkleDelta group in the Xcode project.
54+
4855
If your application is already using ReactiveObjC, ensure it is using the same
4956
version as Squirrel.
5057

@@ -164,7 +171,13 @@ to the update request provided:
164171
"notes": "Theses are some release notes innit",
165172
"pub_date": "2013-09-18T12:29:53+01:00",
166173
"sha256": "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
167-
"size": 104857600
174+
"size": 104857600,
175+
"delta": {
176+
"from_version": "412",
177+
"url": "https://mycompany.example.com/myapp/releases/412-to-myrelease.delta",
178+
"sha256": "60303ae22b998861bce3b28f33eec1be758a213c86c93c076dbe9f558c11c752",
179+
"size": 7340032
180+
}
168181
}
169182
```
170183

@@ -197,6 +210,31 @@ registered with `+registerClass:` sees the update check but not the download;
197210
set `SQRLDownloader.sessionConfiguration` (with its `protocolClasses`) to
198211
intercept both.
199212

213+
"delta", if present, offers a binary patch from one earlier build to this
214+
release; it needs all four keys ("sha256" as 64 hex digits, "size" positive)
215+
and is logged and ignored otherwise. When "from_version" equals the
216+
running application's `CFBundleVersion`, Squirrel downloads the patch instead
217+
of the ZIP, checks its "size" and "sha256", applies it to a copy of the running
218+
application, and puts the result through the same code signing verification as
219+
an unpacked ZIP. If any of that fails, or "from_version" is anything else, it
220+
downloads the ZIP from "url" in the same check (its `downloadProgress` starting
221+
again from zero), so a server can always include the one delta it has for the
222+
version that asked. A delta that has been applied and staged is not fetched
223+
again by later checks in the same process, and neither is one that downloaded
224+
intact but would not apply or verify.
225+
226+
Patches are [Sparkle](https://sparkle-project.org) BinaryDelta files (format 3
227+
or 4, any `--compression` except `bzip2`), made with Sparkle's
228+
`BinaryDelta create <old.app> <new.app> <patch>`. A patch only applies to a
229+
byte-identical copy of `<old.app>`, file modes included, and ShipIt clears the
230+
group and other write bits of everything it installs; strip them from the app
231+
before signing it (`chmod -R go-w MyApp.app`) so the shipped bundle, the
232+
installed bundle and the trees the patch was made from all agree, otherwise
233+
the patch applies once to a fresh install and never again. Files the patch adds
234+
or rewrites are created with decomposed (NFD) names, so a non-ASCII file name
235+
outside an archive such as `app.asar` can fail code signing verification and
236+
cost a fallback to the ZIP. "from_version" may be a JSON string or number.
237+
200238
## Update File JSON Format
201239

202240
The alternate update technique uses a static JSON file, so you can host update
@@ -253,6 +291,7 @@ file); a single entry is fine.
253291
| `updateTo.version` | — | Echoed into the `update-downloaded` event; conventionally the same as the outer `version`. |
254292
| `updateTo.name` / `notes` / `pub_date` | — | Surfaced to your app for display. `pub_date` must be ISO 8601 if present. |
255293
| `updateTo.sha256` / `size` | — | Digest (hex) and byte size of the `.zip`; a download that does not match is rejected. |
294+
| `updateTo.delta` | — | Optional `{from_version, url, sha256, size}` binary patch from one earlier `CFBundleVersion`; tried first when it matches the running app, with the `.zip` as fallback. |
256295

257296
Point the updater directly at this file's URL — there's no required filename.
258297

0 commit comments

Comments
 (0)