-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathupdate.lua
727 lines (632 loc) · 23.2 KB
/
update.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
-- Copyright (c) 2022 Christopher Antos
-- License: http://opensource.org/licenses/MIT
local github_repo = "chrisant996/clink"
local tag_filename = "clink_updater_tag"
--------------------------------------------------------------------------------
local function log_info(message)
log.info("Clink updater: " .. message, 2--[[stack level; our caller]])
return message
end
local function concat_error(specific, general)
if not general then
return specific
elseif not specific then
return general
else
return general:gsub("%.$", "") .. "; " .. specific
end
end
--------------------------------------------------------------------------------
settings.add("clink.autoupdate", true, "Auto-update the Clink program files", "When enabled, periodically checks for updates for the Clink program files.") -- luacheck: no max line length
settings.add("clink.update_interval", 5, "Days between update checks", "The Clink autoupdater will wait this many days between update checks.") -- luacheck: no max line length
--------------------------------------------------------------------------------
local powershell_exe
local checked_prereqs
local prereq_error
local this_install_type
local this_install_key
local latest_cloud_tag
local is_build_dir = false
local can_use_setup_exe = false
local function parse_version_tag(tag)
local maj, min, pat
maj, min, pat = tag:match("v(%d+)%.(%d+)%.(%d+)")
if not maj then
maj, min = tag:match("v(%d+)%.(%d+)")
end
if maj and min then
return tonumber(maj), tonumber(min), tonumber(pat or 0)
end
end
local function is_rhs_version_newer(lhs, rhs)
local lmaj, lmin, lpat = parse_version_tag(lhs or "")
local rmaj, rmin, rpat = parse_version_tag(rhs or "")
if not lmaj then
return rmaj and true
end
if not rmaj then
return false
end
if rmaj > lmaj then
return true
elseif rmaj < lmaj then
return false
end
if rmin > lmin then
return true
elseif rmin < lmin then
return false
end
if rpat > lpat then
return true
else
return false
end
end
local function make_file_at_path(root, rhs)
if root and rhs then
if root ~= "" and rhs ~= "" then
local ret = path.join(root, rhs)
if os.isfile(ret) then
return '"' .. ret .. '"'
end
end
end
end
local function find_prereqs()
if not checked_prereqs then
local sysroot = os.getenv("systemroot")
powershell_exe = make_file_at_path(sysroot, "System32\\WindowsPowerShell\\v1.0\\powershell.exe")
checked_prereqs = true
if not powershell_exe then
prereq_error = log_info("unable to find PowerShell v5.")
else
local f = io.popen('2>&1 ' .. powershell_exe .. ' -Command "Get-Host | Select-Object Version"')
if not f then
powershell_exe = nil
else
for line in f:lines() do
local ver = line:match("^ *([0-9]+%.[0-9]+)")
if not prereq_error and ver then
if is_rhs_version_newer("v" .. ver, "v5.0") then
powershell_exe = nil
prereq_error = log_info("found PowerShell v" .. ver .. ".")
end
end
end
f:close()
end
end
end
if prereq_error then
return nil, prereq_error
end
return true
end
--------------------------------------------------------------------------------
local function get_update_dir()
local target = os.gettemppath()
if not target or target == "" then
return nil, log_info("unable to get temp path.")
end
target = path.join(target, "clink\\updater")
if not os.isdir(target) then
local ok, err = os.mkdir(target) -- luacheck: no unused
if not ok then
return nil, log_info("unable to create temp path '" .. target .. "'.")
end
end
return target
end
local function get_bin_dir()
local bin_dir = os.getenv("=clink.bin")
if bin_dir == "" then
bin_dir = nil
end
if not bin_dir then
return nil, log_info("unable to find Clink executable file.")
end
return bin_dir
end
local function get_local_tag()
return "v" .. clink.version_major .. "." .. clink.version_minor .. "." .. clink.version_patch
end
local function get_installation_type()
if not this_install_type then
this_install_type, this_install_key = clink._get_installation_type()
end
-- This sets this_install_type to the actual installation type. This
-- returns "zip" if can_use_setup_exe is false, otherwise it returns the
-- actual installation type.
return can_use_setup_exe and this_install_key and this_install_type or "zip"
end
--------------------------------------------------------------------------------
local function collect_output(output, line)
local num = #output
if num < 10 then
table.insert(output, line)
elseif num == 10 then
table.insert(output, "...")
end
end
local function log_output(command, output)
log_info(command)
if #output > 0 then
log_info("output from command:")
for _,line in ipairs(output) do
log_info(" "..line)
end
else
log_info("no output from command.")
end
end
--------------------------------------------------------------------------------
local function delete_files(dir, wild, except)
if except then
except = path.join(dir, except)
end
local t = os.globfiles(path.join(dir, wild))
for _, d in ipairs(t) do
local full = path.join(dir, d)
if not except or not string.equalsi(full, except) then
os.remove(full)
end
end
end
local function unzip(zip, out)
if not out or out == "" or not os.isdir(out) then
return nil, log_info("output directory '" .. tostring(out) .. "' does not exist.")
end
local success_tag = "CLINK-UNZIP-SUCCEEDED"
local failure_tag = "CLINK-UNZIP-FAILED"
local expand_archive = string.format([[$ProgressPreference='SilentlyContinue' ; Expand-Archive -Force -LiteralPath \"%s\" -DestinationPath \"%s\"]], zip, out) -- luacheck: no max line length
local powershell_command = string.format([[try { %s ; echo "%s" } catch { echo "%s" ; echo $_.Exception.Message ; echo "`nALL ERRORS:`n" ; echo $error }]], expand_archive, success_tag, failure_tag) -- luacheck: no max line length
local cmd = string.format([[2>&1 %s -Command "%s"]], powershell_exe, powershell_command)
local f, err = io.popen(cmd)
if not f then
log_info(cmd)
return nil, err
end
local result
local output = {}
local saw_success
local saw_failure
for line in f:lines() do
local utf8 = unicode.fromcodepage(line)
line = utf8 or line
collect_output(output, line)
if line == success_tag then
saw_success = true
elseif line == failure_tag then
saw_failure = true
end
end
if saw_success and not saw_failure then
result = true
end
f:close()
if not result then
log_output(cmd, output)
-- Delete the zip file; it might have been damaged, and re-downloading
-- it might be necessary.
os.remove(zip)
return nil
end
return result
end
local function install_file(from_dir, to_dir, name)
local src = path.join(from_dir, name)
local dst = path.join(to_dir, name)
-- Copy file.
local ok, err, code = os.copy(src, dst)
if ok then
return true
end
-- No? Create a tmp file to reserve a unique name for renaming.
local file, err2, code2 = os.createtmpfile("~clink.", ".old", to_dir, "b")
if not file then
if code2 == 13 then
-- Access denied when trying to create tmp file for renaming may
-- indicate elevation is required.
code = -1
end
return nil, err, code
end
file:close()
-- Delete the temp file so the name is available for renaming.
local tmp = err2
os.remove(tmp)
-- Rename away.
ok, err2, code2 = os.move(dst, tmp) -- luacheck: no unused
if not ok then
os.remove(tmp)
return nil, err, code
end
-- Retry copy.
ok, err2, code2 = os.copy(src, dst) -- luacheck: no unused
if ok then
return true
end
-- Still no? Rename back.
os.move(tmp, dst)
return nil, err, code
end
local function has_update_file()
local update_dir = get_update_dir()
if not update_dir then
return nil
end
local install_type = get_installation_type()
if not install_type then
return nil
end
local latest
local files = os.globfiles(path.join(update_dir, "*." .. install_type), 2)
for _, f in ipairs(files) do
if f.size > 0 and f.type:find("file") then
if is_rhs_version_newer(latest and latest.name, f.name) then
latest = f
end
end
end
if not latest then
return nil
end
return path.join(update_dir, latest.name)
end
local function can_check_for_update(force)
local bin_dir, err = get_bin_dir()
if not bin_dir then
return false, err
end
local t = os.globfiles(path.join(bin_dir, "clink_x??.exe"), true)
if not t or not t[1] or not t[1].type then
err = log_info("could not determine target location.")
return false, err
end
if t[1].type:find("readonly") then
return false, log_info("cannot update because files are readonly.")
end
local lib = path.join(bin_dir, path.getbasename(t[1].name) .. ".lib")
if os.isfile(lib) then
local reason = "autoupdate is disabled for local build directories."
if not is_build_dir then
log_info(reason)
is_build_dir = true
end
return false, reason
end
local tagfile, err = get_update_dir() -- luacheck: ignore 411
if not tagfile then
return false, err
end
tagfile = path.join(tagfile, tag_filename)
if not force then
local f = io.open(tagfile)
if not f then
log_info("updating because there is no record of having updated before.")
return true
end
local local_lastcheck = f:read("*l")
f:close()
if local_lastcheck then
local now = os.time()
local interval_days = settings.get("clink.update_interval")
if interval_days < 1 then
interval_days = 1
end
if tonumber(local_lastcheck) + (interval_days * 24*60*60) > now then
return false, log_info("too soon to check for updates (" .. tonumber(local_lastcheck) .. " vs " .. now .. ").") -- luacheck: no max line length
end
end
end
return true
end
local function internal_check_for_update(force)
local local_tag = get_local_tag()
local install_type = get_installation_type()
-- Use github API to query latest release.
--
-- PowerShell needs -UseBasicParsing to prevent assuming the response is
-- HTML and attempting to use IE to parse the DOM and execute scripts, and
-- and needs to force TLS 1.2 because older versions of PowerShell default
-- to using TLS 1.0.
if force then
print("Checking latest version...")
end
local cloud_tag
local api = string.format([[2>&1 ]] .. powershell_exe .. [[ -Command "$ProgressPreference='SilentlyContinue' ; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 ; Invoke-WebRequest -Headers @{\"cache-control\"=\"no-cache\"} -UseBasicParsing https://api.github.com/repos/%s/releases/latest | Select-Object -ExpandProperty Content"]], github_repo) -- luacheck: no max line length
local f, err = io.popen(api)
if not f then
log_info(api)
return nil, concat_error(err, log_info("unable to query github api."))
end
local latest_update_file
local output = {}
for line in f:lines() do
local utf8 = unicode.fromcodepage(line)
line = utf8 or line
local tag = line:match('"tag_name": *"([^"]-)"')
local match = line:match('"browser_download_url": *"([^"]-%.' .. install_type .. ')"')
if not cloud_tag and tag then
cloud_tag = tag
end
if not latest_update_file and match then
latest_update_file = match
end
collect_output(output, line)
end
f:close()
if not cloud_tag then
log_output(api, output)
return nil, log_info("unable to find latest release.")
elseif not latest_update_file then
log_output(api, output)
return nil, log_info("unable to find latest release " .. install_type .. " file.")
end
latest_cloud_tag = cloud_tag
-- Compare versions.
log_info("latest release is " .. cloud_tag .. ".")
if not is_rhs_version_newer(local_tag, cloud_tag) then
return nil, log_info("no update available; local version " .. local_tag .. " is not older than latest release " .. cloud_tag .. "."), true -- luacheck: no max line length
end
-- Check if already downloaded.
local local_update_file = has_update_file(install_type)
if local_update_file and clink.lower(path.getbasename(local_update_file)) == clink.lower(cloud_tag) then
return local_update_file
end
-- Download latest release update file (zip or exe).
-- Note: Because of github redirection, there's no way to verify whether
-- the file existed on the server. But if it's not a zip file then the
-- expand will fail, and if it's not an exe file then execution will fail.
-- That's good enough.
if force then
print("Downloading latest release " .. cloud_tag .. "...")
end
local_update_file, err = get_update_dir()
if local_update_file then
local_update_file = path.join(local_update_file, cloud_tag .. "." .. install_type)
else
return nil, err
end
log_info("downloading " .. latest_update_file .. " to " .. local_update_file .. ".")
local cmd = string.format([[2>&1 ]] .. powershell_exe .. [[ -Command "$ProgressPreference='SilentlyContinue' ; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 ; Invoke-WebRequest '%s' -OutFile '%s'"]], latest_update_file, local_update_file) -- luacheck: no max line length
f, err = io.popen(cmd)
if not f then
log_info(cmd)
os.remove(local_update_file)
return nil, concat_error(err, log_info("failed to download " .. install_type .. " file."))
end
output = {}
for line in f:lines() do
local utf8 = unicode.fromcodepage(line)
line = utf8 or line
collect_output(output, line)
end
f:close()
local ok = os.isfile(local_update_file)
if ok then
local info = os.globfiles(local_update_file, 2)
if not info or not info[1] or not info[1].size or info[1].size == 0 then
ok = false
end
end
if not ok then
log_output(cmd, output)
return nil, log_info("failed to download " .. install_type .. " file.")
end
return local_update_file
end
local function update_lastcheck(update_dir)
local tagfile = path.join(update_dir, tag_filename)
local f, err = io.open(tagfile, "w")
if f then
f:write(tostring(os.time()) .. "\n")
f:close()
else
log_info(concat_error("unable to record last update time to '" .. tagfile .. "'.", err))
end
end
local function check_for_update(force)
local update_dir, err = get_update_dir()
if not update_dir then
return nil, err
end
-- Protect against concurrent updates.
local guardfile = path.join(update_dir, "updating")
local g, err = io.sopen(guardfile, "w", "w") -- luacheck: ignore 411
if not g then
return nil, log_info("unable to update because another update may be in progress; " .. err), true -- luacheck: no max line length
end
-- Attempt to update.
local ret, err, stop = internal_check_for_update(force) -- luacheck: ignore 411
-- Record last update time.
update_lastcheck(update_dir)
-- Dispose of the concurrency protection.
g:close()
os.remove(guardfile)
return ret, err, stop
end
local function is_update_ready(force)
local exe_path, err = get_bin_dir()
if not exe_path then
return nil, err
end
local update_dir, err = get_update_dir() -- luacheck: ignore 411
if not update_dir then
return nil, err
end
local ok, err = find_prereqs() -- luacheck: ignore 411
if not ok then
return nil, concat_error(err, log_info("autoupdate requires PowerShell v5."))
end
-- Download latest update file, or use update file that's already been
-- downloaded.
local update_file, stop
if force then
local can
can, err = can_check_for_update(force)
if not can then
return nil, err
end
end
update_file, err, stop = check_for_update(force)
if not update_file then
-- If an update is already downloaded, then ignore transient errors
-- that may have occurred while attempting to check or download a new
-- update.
if not stop then
update_file = has_update_file()
end
if not update_file then
-- If no update file is already downloaded, then report the error
-- from the earlier check_for_update().
return nil, err
end
end
-- Verify the update file is newer.
local local_tag = get_local_tag()
local cloud_tag = latest_cloud_tag or path.getbasename(update_file)
if not is_rhs_version_newer(get_local_tag(), cloud_tag) then
return nil, log_info("no update available; local version " .. local_tag .. " is not older than latest release " .. cloud_tag .. ".") -- luacheck: no max line length
end
-- Update is ready.
log_info("update in " .. update_file .. " is ready to be applied.")
return update_file
end
local function apply_zip_update(zip_file, force)
local bin_dir, err = get_bin_dir()
if not bin_dir then
return nil, err
end
local update_dir, err = get_update_dir() -- luacheck: ignore 411
if not update_dir then
return nil, err
end
local cloud_tag = path.getbasename(zip_file)
local expand_dir = path.join(update_dir, cloud_tag)
if force then
print("Expanding zip file...")
end
log_info("expanding " .. zip_file .. " into " .. expand_dir .. ".")
-- Prepare the temp directory; ensure it is empty (avoid copying stray
-- pre-existing files).
delete_files(expand_dir, "*")
os.rmdir(expand_dir)
if os.isdir(expand_dir) then
return nil, log_info("temp path '" .. expand_dir .. "' already exists.")
end
local ok, err = os.mkdir(expand_dir) -- luacheck: no unused, ignore 411
if not ok then
return nil, log_info("unable to create temp path '" .. expand_dir .. "'.")
end
-- Expand the zip file.
local ok, err = unzip(zip_file, expand_dir) -- luacheck: ignore 411
if not ok then
local nope = log_info("failed to unzip the latest release.")
return nil, concat_error(nope, err)
end
-- Apply expanded files.
local t = os.globfiles(path.join(expand_dir, "*"))
for _, f in ipairs(t) do
local code
ok, err, code = install_file(expand_dir, bin_dir, f)
if not ok then
local ret = nil
if code == -1 then
ret = -1 -- Signal that a retry with elevation is needed.
end
return ret, log_info(concat_error("failed to install file '" .. f .. "'.", err))
end
end
-- Update installed version.
if this_install_type == "exe" and this_install_key then
print("Updating registry keys...")
clink._set_install_version(this_install_key, cloud_tag);
end
-- Cleanup.
delete_files(expand_dir, "*")
os.rmdir(expand_dir)
delete_files(update_dir, "*.zip", zip_file)
delete_files(bin_dir, "~clink.*.old")
return 1, log_info("updated Clink to " .. cloud_tag .. ".")
end
local function run_exe_installer(setup_exe)
local bin_dir, err = get_bin_dir()
if not bin_dir then
return nil, err
end
local update_dir, err = get_update_dir() -- luacheck: ignore 411
if not update_dir then
return nil, err
end
local cloud_tag = path.getbasename(setup_exe)
print("Launching the Clink setup program...")
local command = setup_exe .. " /S /D=" .. bin_dir
log_info("launching setup program '" .. command .. "'")
-- PROBLEM: This gets blocked by malware protection.
local ok, what, code = os.execute(command) -- luacheck: no unused
if not ok or code ~= 0 then
return nil, log_info(string.format("setup program %s.", ok and "canceled" or "failed"))
end
-- Cleanup.
delete_files(update_dir, "*.exe", setup_exe)
return 1, log_info("updated Clink to " .. cloud_tag .. ".")
end
local function try_autoupdate()
is_update_ready(false)
end
--------------------------------------------------------------------------------
local function autoupdate()
if not settings.get("clink.autoupdate") then
log_info("clink.autoupdate is false.")
return
end
-- Report if an update is downloaded already.
local update_file = has_update_file()
if update_file and can_check_for_update(true) then
local tag = path.getbasename(update_file)
if is_rhs_version_newer(get_local_tag(), tag) then
clink.print("\x1b[1mClink " .. tag .. " is available.\x1b[m")
print("- To apply the update, run 'clink update'.")
print("- To stop checking for updates, run 'clink set clink.autoupdate false'.")
clink.printreleasesurl("- ")
print("")
end
end
-- Possibly check for a new update.
if can_check_for_update() then
local c = coroutine.create(try_autoupdate)
clink.runcoroutineuntilcomplete(c)
end
end
clink.oninject(autoupdate)
--------------------------------------------------------------------------------
function clink.printreleasesurl(tag)
tag = tag or "\n"
print(tag .. "To view the release notes, visit the Releases page:")
print(string.format(" https://github.com/%s/releases", github_repo))
end
--------------------------------------------------------------------------------
function clink.updatenow(elevated)
latest_cloud_tag = nil
local update_file, err = is_update_ready(true)
if not update_file then
return nil, err
end
local install_type = get_installation_type()
if not elevated and install_type == "zip" and this_install_type == "exe" then
log_info("elevation required to update InstallDir in registry.")
return -1
end
local ext = path.getextension(update_file):lower():match("^%.(.+)$")
if ext ~= install_type then
return nil, log_info(string.format("mismatched update type (%s) and update file (%s).", install_type, ext))
elseif ext == "zip" then
return apply_zip_update(update_file, true)
elseif ext == "exe" then
return run_exe_installer(update_file)
else
return nil, log_info("unable to determine update type from '" .. update_file .. "'.")
end
end