Browse Source

Move auto update code into reusable function

master
Macoy Madson 6 months ago
parent
commit
72e18097d5
  1. 106
      src/AutoUpdate.cake

106
src/AutoUpdate.cake

@ -110,6 +110,66 @@
(dynarray-free result-buffer)
(return true))
;; Use CryptographyCLI.cake utility to generate your own keys and signed files. You can set up
;; the .cakedata serving however you want, so long as Curl can download it.
;; Creating an auto-update file:
;; zip Machsearch_Linux-x64.zip machsearch
;; ./cryptography-cli create-signed-file Machsearch_Linux-x64.zip ~/website/updates/Machsearch/Machsearch_Linux-x64.auto-update
;; The download will only occur if the latest version is greater than current-application-version.
(defun auto-update-download (curl (* CURL) public-key (* (unsigned char))
update-cakedata-url (* (const char))
current-application-version int
&return bool)
(var update-metadata auto-update-metadata (array 0))
(unless (auto-update-get-latest-version-metadata
curl
update-cakedata-url
(addr update-metadata))
(fprintf stderr "error: expected server to be running before doing this test\n")
(free-introspect-struct-fields auto-update-metadata--metadata (addr update-metadata) free)
(return false))
(var platform-update-url (* (const char))
(auto-update-get-current-platform-download-url (addr update-metadata)))
(scope ;; Print results
(fprintf stderr "Latest version of %s is %d.\nDownloads:\n"
(field update-metadata name) (field update-metadata latest-version))
(each-item-addr-in-dynarray (field update-metadata downloads) i download (* auto-update-download)
(fprintf stderr "\t%s %s at %s\n" (path download > operating-system)
(path download > architecture)
(path download > url)))
(fprintf stderr "The current platform should download %s\n"
(? platform-update-url platform-update-url "unknown platform")))
(when (>= current-application-version (field update-metadata latest-version))
(fprintf stderr "The application is already up-to-date.\n")
(return true))
(var verified-payload (* (unsigned char)) null)
(var verified-payload-size (unsigned (long long)) 0)
(unless (auto-update-download-and-verify-signature curl platform-update-url public-key
(addr verified-payload)
(addr verified-payload-size))
(free-introspect-struct-fields auto-update-metadata--metadata (addr update-metadata) free)
(return false))
(var output-directory ([] 1024 char) (array 0))
(sprintf output-directory "v%d" (field update-metadata latest-version))
(unless (make-directory output-directory)
(free-introspect-struct-fields auto-update-metadata--metadata (addr update-metadata) free)
(return false))
(unless (decompress-zip-from-memory-to-files
verified-payload (type-cast verified-payload-size size_t)
output-directory)
(free verified-payload)
(free-introspect-struct-fields auto-update-metadata--metadata (addr update-metadata) free)
(return false))
(free verified-payload)
(free-introspect-struct-fields auto-update-metadata--metadata (addr update-metadata) free)
(return true))
(comptime-cond
('auto-test
(c-import "<stdio.h>")
@ -125,6 +185,7 @@
0xf6 0x89 0x17 0xbf 0x69 0xf8 0x16 0xb0 0x61 0xc5 0xd5 0xff 0x56 0xae 0xdb))
(var update-cakedata-url (* (const char))
"https://localhost:8888/updates/Machsearch/machsearch.cakedata")
(var current-version int 0)
(when (!= (curl_global_init CURL_GLOBAL_DEFAULT) 0)
(fprintf stderr "error: Failed to initialize curl\n")
@ -136,54 +197,11 @@
(curl_global_cleanup)
(return 1))
(var update-metadata auto-update-metadata (array 0))
(unless (auto-update-get-latest-version-metadata
curl
update-cakedata-url
(addr update-metadata))
(fprintf stderr "error: expected server to be running before doing this test\n")
(free-introspect-struct-fields auto-update-metadata--metadata (addr update-metadata) free)
(unless (auto-update-download curl macoy-public-key update-cakedata-url current-version)
(curl_easy_cleanup curl)
(curl_global_cleanup)
(return 1))
(var platform-update-url (* (const char))
(auto-update-get-current-platform-download-url (addr update-metadata)))
(scope ;; Print results
(fprintf stderr "Latest version of %s is %d.\nDownloads:\n"
(field update-metadata name) (field update-metadata latest-version))
(each-item-addr-in-dynarray (field update-metadata downloads) i download (* auto-update-download)
(fprintf stderr "\t%s %s at %s\n" (path download > operating-system)
(path download > architecture)
(path download > url)))
(fprintf stderr "The current platform should download %s\n"
(? platform-update-url platform-update-url "unknown platform")))
(var verified-payload (* (unsigned char)) null)
(var verified-payload-size (unsigned (long long)) 0)
(unless (auto-update-download-and-verify-signature curl platform-update-url macoy-public-key
(addr verified-payload)
(addr verified-payload-size))
(free-introspect-struct-fields auto-update-metadata--metadata (addr update-metadata) free)
(curl_easy_cleanup curl)
(curl_global_cleanup)
(return 1))
(curl_easy_cleanup curl)
(curl_global_cleanup)
(var output-directory ([] 1024 char) (array 0))
(sprintf output-directory "v%d" (field update-metadata latest-version))
(unless (make-directory output-directory)
(free-introspect-struct-fields auto-update-metadata--metadata (addr update-metadata) free)
(return 1))
(unless (decompress-zip-from-memory-to-files
verified-payload (type-cast verified-payload-size size_t)
output-directory)
(free verified-payload)
(free-introspect-struct-fields auto-update-metadata--metadata (addr update-metadata) free)
(return 1))
(free verified-payload)
(free-introspect-struct-fields auto-update-metadata--metadata (addr update-metadata) free)
(return 0))))

Loading…
Cancel
Save