/
mojosetup_mainline.lua
2280 lines (1939 loc) · 79.2 KB
/
mojosetup_mainline.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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- MojoSetup; a portable, flexible installation application.
--
-- Please see the file LICENSE.txt in the source's root directory.
--
-- This file written by Ryan C. Gordon.
-- This is where most of the magic happens. Everything is initialized, and
-- the user's config script has successfully run. This Lua chunk drives the
-- main application code.
-- !!! FIXME: add a --dryrun option.
local _ = MojoSetup.translate
MojoSetup.metadatakey = ".mojosetup_metadata."
MojoSetup.metadatadesc = _("Metadata")
MojoSetup.metadatadirname = ".mojosetup"
if MojoSetup.info.platform == "windows" then
MojoSetup.controlappname = "mojosetup.exe"
else
MojoSetup.controlappname = "mojosetup"
end
local function badcmdline()
MojoSetup.fatal(_("Invalid command line"))
end
-- This currently counts on (base) ending with a single "/", and not having
-- any strange distortions: "/blah/../blah/.//" would not match "/blah/x",
-- although it _should_ if we parsed it out.
local function make_relative(fname, base)
local baselen = string.len(base)
if string.sub(fname, 0, baselen) == base then
fname = string.sub(fname, baselen+2) -- make it relative.
end
return fname
end
local function manifest_resync(man, fname, _key)
if fname == nil then return end
local fullpath = fname
fname = make_relative(fname, MojoSetup.destination)
if man[fname] == nil then
MojoSetup.logwarning("Tried to resync unknown file '" ..fname.. "' in manifest!")
else
local perms = "0644" -- !!! FIXME MojoSetup.platform.perms(fullpath)
local sums = nil
local lndest = nil
local ftype = nil
if MojoSetup.platform.issymlink(fullpath) then
ftype = "symlink"
-- !!! FIXME: linkdest?
elseif MojoSetup.platform.isdir(fullpath) then
ftype = "dir"
else -- !!! FIXME: other types?
ftype = "file"
sums = MojoSetup.checksum(fullpath)
end
man[fname] =
{
key = _key,
type = ftype,
mode = perms,
checksums = sums,
linkdest = lndest
}
MojoSetup.logwarning("Resync'd file '" ..fname.. "' in manifest")
end
end
-- !!! FIXME: I need to go back from managing everything installed through
-- !!! FIXME: the manifest to a separate table of "things written to disk
-- !!! FIXME: on just this run" ... the existing manifest code can stay as-is,
-- !!! FIXME: rollbacks should be done exclusively from that other table.
-- !!! FIXME: Right now we're relying on dumb stuff like sorting the filenames
-- !!! FIXME: to get a safe deletion order on revert, but we should just
-- !!! FIXME: keep a chronological array instead.
local function manifest_add(man, fname, _key, ftype, mode, sums, lndest)
if (fname ~= nil) and (_key ~= nil) then
fname = make_relative(fname, MojoSetup.destination)
if man[fname] ~= nil then
MojoSetup.logwarning("Overwriting file '" .. fname .. "' in manifest!")
end
man[fname] = {
key = _key,
type = ftype,
mode = perms,
checksums = sums,
linkdest = lndest
}
if MojoSetup.oldfiles[fname] ~= nil then
MojoSetup.oldfiles[fname].seen = true
end
end
end
local function manifest_delete(man, fname)
if fname ~= nil then
fname = make_relative(fname, MojoSetup.destination)
if man[fname] == nil then
MojoSetup.logwarning("Deleting unknown file '" .. fname .. "' from manifest!")
else
man[fname] = nil
end
end
end
local function flatten_list(list)
local retval = list
if type(list) == "table" then
retval = {}
for i,v in ipairs(list) do
if #retval == 0 then
retval[1] = v
else
retval[#retval+1] = ';'
retval[#retval+1] = v
end
end
retval = table.concat(retval)
end
return retval
end
local function do_delete(fname)
local retval = false
if fname == nil then
retval = true
else
-- Try to unlink() first, so we'll catch broken symlinks, then try
-- exists(): if it really wasn't there, we'll call it success anyhow.
if MojoSetup.platform.unlink(fname) then
MojoSetup.loginfo("Deleted '" .. fname .. "'")
retval = true
elseif not MojoSetup.platform.exists(fname) then
retval = true
else
MojoSetup.logerror("Deleting '" .. fname .. "'" .. " failed!")
end
end
return retval
end
local function delete_files(rootdir, filelist, callback, error_is_fatal)
if filelist ~= nil then
local max = #filelist
for i = max,1,-1 do
local fname
if rootdir == nil then
fname = filelist[i]
elseif filelist[i] == "" then
fname = rootdir
else
fname = rootdir .. "/" .. filelist[i]
end
if not do_delete(fname) and error_is_fatal then
MojoSetup.fatal(_("Deletion failed!"))
end
if callback ~= nil then
callback(filelist[i], i, max)
end
end
end
end
local function delete_rollbacks(callback)
if MojoSetup.rollbacks == nil then
return
end
local fnames = {}
local max = #MojoSetup.rollbacks
for id = 1,max,1 do
fnames[id] = id
end
MojoSetup.rollbacks = {} -- just in case this gets called again...
delete_files(MojoSetup.rollbackdir, fnames, callback)
end
local function delete_scratchdirs()
do_delete(MojoSetup.downloaddir)
do_delete(MojoSetup.rollbackdir)
do_delete(MojoSetup.scratchdir) -- must be after dirs it contains!
do_delete(MojoSetup.metadatadir) -- must be last! (and is okay to fail.)
end
local function do_rollbacks()
if MojoSetup.rollbacks == nil then
return
end
local warned = false
local max = #MojoSetup.rollbacks
for id = max,1,-1 do
local src = MojoSetup.rollbackdir .. "/" .. id
local dest = MojoSetup.rollbacks[id]
if not MojoSetup.movefile(src, dest) and not warned then
-- we're already in fatal(), so we can only throw up a msgbox...
MojoSetup.msgbox(_("Serious problem"),
_("Couldn't restore some files. Your existing installation is likely damaged."))
warned = true
end
MojoSetup.loginfo("Restored rollback #" .. id .. ": '" .. dest .. "'")
end
MojoSetup.rollbacks = {} -- just in case this gets called again...
end
-- get a linear array of filenames in the manifest.
local function flatten_manifest(man, postprocess)
local files = {}
if postprocess == nil then
postprocess = function(x) return x end
end
if man ~= nil then
for fname,items in pairs(man) do
fname = postprocess(fname)
if fname ~= nil then
files[#files+1] = fname
end
end
end
table.sort(files, function(a,b) return MojoSetup.strcmp(a,b) < 0 end)
return files
end
-- This gets called by fatal()...must be a global function.
function MojoSetup.revertinstall()
-- (The real revertinstall is set later. This is a stub for startup.)
end
local function calc_percent(current, total)
if total == 0 then
return 0
elseif total < 0 then
return -1
end
local retval = MojoSetup.truncatenum((current / total) * 100)
if retval > 100 then
retval = 100
elseif retval < 0 then
retval = 0
end
return retval
end
local function make_rate_string(rate, bw, total)
local retval = nil
if rate <= 0 then
retval = _("stalled")
else
local ratetype = _("B/s")
if rate > 1024 then
rate = MojoSetup.truncatenum(rate / 1024)
ratetype = _("KB/s")
end
if total > 0 then -- can approximate time left if we know the goal.
local bytesleft = total - bw
local secsleft = MojoSetup.truncatenum(bytesleft / rate)
local minsleft = MojoSetup.truncatenum(secsleft / 60)
local hoursleft = MojoSetup.truncatenum(minsleft / 60)
secsleft = string.sub("00" .. (secsleft - (minsleft * 60)), -2)
minsleft = string.sub("00" .. (minsleft - (hoursleft * 60)), -2)
if hoursleft < 10 then
hoursleft = "0" .. hoursleft
else
hoursleft = tostring(hoursleft)
end
retval = MojoSetup.format(_("%0 %1, %2:%3:%4 remaining"),
rate, ratetype,
hoursleft, minsleft, secsleft)
else
retval = MojoSetup.format(_("%0 %1"), rate, ratetype)
end
end
return retval
end
local function split_path(path)
local retval = {}
for item in string.gmatch(path .. "/", "(.-)/") do
if item ~= "" then
retval[#retval+1] = item
end
end
return retval
end
local function rebuild_path(paths, n)
local retval = paths[n]
n = n + 1
while paths[n] ~= nil do
retval = retval .. "/" .. paths[n]
n = n + 1
end
return retval
end
local function normalize_path(path)
return rebuild_path(split_path(path), 1)
end
local function close_archive_list(arclist)
for i = #arclist,1,-1 do
MojoSetup.archive.close(arclist[i])
arclist[i] = nil
end
end
-- This code's a little nasty...
local function drill_for_archive(archive, path, arclist)
if not MojoSetup.archive.enumerate(archive) then
MojoSetup.fatal(_("Couldn't enumerate archive"))
end
local pathtab = split_path(path)
local ent = MojoSetup.archive.enumnext(archive)
while ent ~= nil do
if ent.type == "file" then
local i = 1
local enttab = split_path(ent.filename)
while (enttab[i] ~= nil) and (enttab[i] == pathtab[i]) do
i = i + 1
end
if enttab[i] == nil then
-- It's a file that makes up some of the specified path.
-- open it as an archive and keep drilling...
local arc = MojoSetup.archive.fromentry(archive)
if arc == nil then
MojoSetup.fatal(_("Couldn't open archive"))
end
arclist[#arclist+1] = arc
if pathtab[i] == nil then
return arc -- this is the end of the path! Done drilling!
end
return drill_for_archive(arc, rebuild_path(pathtab, i), arclist)
end
end
ent = MojoSetup.archive.enumnext(archive)
end
MojoSetup.fatal(_("Archive not found"))
end
local function install_file(dest, perms, writefn, desc, manifestkey)
-- Upvalued so we don't look these up each time...
local fname = string.gsub(dest, "^.*/", "", 1) -- chop the dirs off...
local ptype = _("Installing")
local component = desc
local keepgoing = true
local callback = function(ticks, justwrote, bw, total)
local percent = -1
local item = fname
if total >= 0 then
MojoSetup.written = MojoSetup.written + justwrote
percent = calc_percent(MojoSetup.written, MojoSetup.totalwrite)
item = MojoSetup.format(_("%0: %1%%"), fname, calc_percent(bw, total))
end
keepgoing = MojoSetup.gui.progress(ptype, component, percent, item, true)
return keepgoing
end
-- !!! FIXME: maybe keep a separate list, so we can rollback installs
-- !!! FIXME: that are building on previous installations?
-- Add to manifest first, so we can delete it during rollback if i/o fails.
-- !!! FIXME: perms may be nil...we need a MojoSetup.defaultPermsString()...
manifest_add(MojoSetup.manifest, dest, manifestkey, "file", perms, nil, nil)
MojoSetup.gui.progressitem()
local written, sums = writefn(callback)
if not written then
if not keepgoing then
MojoSetup.logerror("User cancelled install during file write.")
MojoSetup.fatal()
else
MojoSetup.logerror("Failed to create file '" .. dest .. "'")
MojoSetup.fatal(_("File creation failed!"))
end
end
-- Readd it to the manifest, now with a checksum!
if manifestkey ~= nil then
manifest_delete(MojoSetup.manifest, dest)
manifest_add(MojoSetup.manifest, dest, manifestkey, "file", perms, sums, nil)
end
MojoSetup.loginfo("Created file '" .. dest .. "'")
MojoSetup.incrementgarbagecount()
end
local function install_file_from_archive(dest, archive, perms, desc, manifestkey)
local fn = function(callback)
return MojoSetup.writefile(archive, dest, perms, nil, callback)
end
return install_file(dest, perms, fn, desc, manifestkey)
end
local function install_file_from_stringtable(dest, t, perms, desc, manifestkey)
local fn = function(callback)
return MojoSetup.stringtabletofile(t, dest, perms, nil, callback)
end
return install_file(dest, perms, fn, desc, manifestkey)
end
local function install_file_from_string(dest, str, perms, desc, manifestkey)
local fn = function(callback)
return MojoSetup.stringtofile(str, dest, perms, nil, callback)
end
return install_file(dest, perms, fn, desc, manifestkey)
end
local function install_file_from_filesystem(dest, src, perms, desc, manifestkey, maxbytes)
local fn = function(callback)
return MojoSetup.copyfile(src, dest, perms, maxbytes, callback)
end
return install_file(dest, perms, fn, desc, manifestkey)
end
local function install_symlink(dest, lndest, desc, manifestkey)
MojoSetup.gui.progressitem()
-- The size of a symbolic link is the length of the path it points to
MojoSetup.written = MojoSetup.written + string.len(lndest)
local percent = calc_percent(MojoSetup.written, MojoSetup.totalwrite)
MojoSetup.gui.progress(_("Installing"), desc, percent, dest, true)
if not MojoSetup.platform.symlink(dest, lndest) then
MojoSetup.logerror("Failed to create symlink '" .. dest .. "'")
MojoSetup.fatal(_("Symlink creation failed!"))
end
manifest_add(MojoSetup.manifest, dest, manifestkey, "symlink", nil, nil, lndest)
MojoSetup.loginfo("Created symlink '" .. dest .. "' -> '" .. lndest .. "'")
end
local function install_directory(dest, perms, desc, manifestkey)
-- Chop any '/' chars from the end of the string...
dest = string.gsub(dest, "/+$", "")
if desc ~= nil then
MojoSetup.gui.progressitem()
-- Preserve the current completion percentage
local percent = calc_percent(MojoSetup.written, MojoSetup.totalwrite)
MojoSetup.gui.progress(_("Installing"), desc, percent, dest, true)
end
-- The directory may already exist in case of an upgrade.
-- !!! FIXME: We should set the permissions (e.g. 0555) after filling up
-- !!! FIXME: the directory, including if it already exists (upgrade).
if not MojoSetup.platform.mkdir(dest, perms) and not MojoSetup.platform.isdir(dest) then
MojoSetup.logerror("Failed to create dir '" .. dest .. "'")
MojoSetup.fatal(_("Directory creation failed"))
end
manifest_add(MojoSetup.manifest, dest, manifestkey, "directory", perms, nil, nil)
MojoSetup.loginfo("Created directory '" .. dest .. "'")
end
local function install_parent_dirs(path, manifestkey)
-- Chop any '/' chars from the end of the string...
path = string.gsub(path, "/+$", "")
-- Build each piece of final path. The gmatch() skips the last element.
local fullpath = ""
for item in string.gmatch(path, "(.-)/") do
if item ~= "" then
fullpath = fullpath .. "/" .. item
if not MojoSetup.platform.exists(fullpath) then
install_directory(fullpath, nil, nil, manifestkey)
end
end
end
end
local function backup_file(path)
if MojoSetup.platform.isdir(path) and
not MojoSetup.platform.issymlink(path) then
-- We must first create rollbacks for all the files and directories
-- contained in this directory
local dir = MojoSetup.platform.opendir(path)
local dentry = MojoSetup.platform.readdir(dir)
while dentry ~= nil do
backup_file(path .. "/" .. dentry)
dentry = MojoSetup.platform.readdir(dir)
end
MojoSetup.platform.closedir(dir)
end
local id = #MojoSetup.rollbacks + 1
local f = MojoSetup.rollbackdir .. "/" .. id
install_parent_dirs(f, MojoSetup.metadatakey)
MojoSetup.rollbacks[id] = path
if not MojoSetup.movefile(path, f) then
MojoSetup.fatal(MojoSetup.format(_("Couldn't backup '%0' to '%1' for rollback"), path, f))
end
local relpath = make_relative(path, MojoSetup.destination)
if MojoSetup.oldfiles[relpath] ~= nil then
MojoSetup.oldfiles[relpath].seen = 1
MojoSetup.loginfo("marked '" .. relpath .. "' as seen")
else
MojoSetup.loginfo(relpath .. " is not an old file")
end
MojoSetup.loginfo("Created rollback #" .. id .. ": '" .. path .. "'")
end
local function permit_write(dest, filename, type, file)
local allowoverwrite = true
if MojoSetup.platform.exists(dest) or MojoSetup.platform.issymlink(dest) then
if type == "dir" then
allowoverwrite = true
else
if MojoSetup.oldfiles[filename] ~= nil then
allowoverwrite = true
elseif MojoSetup.forceoverwrite ~= nil then
allowoverwrite = MojoSetup.forceoverwrite
else
-- !!! FIXME: option/package-wide overwrite?
allowoverwrite = false
if file ~= nil then
allowoverwrite = file.allowoverwrite
end
if not allowoverwrite then
MojoSetup.loginfo("File '" .. dest .. "' already exists.")
local text = MojoSetup.format(_("File '%0' already exists! Replace?"), dest)
local ynan = MojoSetup.promptynan(_("Conflict!"), text, true)
if ynan == "always" then
MojoSetup.forceoverwrite = true
allowoverwrite = true
elseif ynan == "never" then
MojoSetup.forceoverwrite = false
allowoverwrite = false
elseif ynan == "yes" then
allowoverwrite = true
elseif ynan == "no" then
allowoverwrite = false
end
end
end
-- !!! FIXME: Setup.File.mustoverwrite to override "never"?
if allowoverwrite then
backup_file(dest)
-- Make sure this isn't already in the manifest...
if MojoSetup.manifest[dest] ~= nil then
manifest_delete(MojoSetup.manifest, dest)
end
end
end
end
return allowoverwrite
end
local function install_archive_entity(dest, ent, archive, desc, manifestkey, perms)
install_parent_dirs(dest, manifestkey)
if ent.type == "file" then
install_file_from_archive(dest, archive, perms, desc, manifestkey)
elseif ent.type == "dir" then
install_directory(dest, perms, desc, manifestkey)
elseif ent.type == "symlink" then
install_symlink(dest, ent.linkdest, desc, manifestkey)
else -- !!! FIXME: device nodes, etc...
-- !!! FIXME: should this be fatal?
MojoSetup.fatal(_("Unknown file type in archive"))
end
end
local function install_archive_entry(archive, ent, file, option)
local entdest = ent.filename
if entdest == nil then return end -- probably can't happen...
-- Set destination in native filesystem. May be default or explicit.
local dest = file.destination
if dest == nil then
dest = entdest
else
dest = dest .. "/" .. entdest
end
local perms = file.permissions -- may be nil
if file.filter ~= nil then
local filterperms
dest, filterperms = file.filter(dest, ent)
if filterperms ~= nil then
perms = filterperms
end
end
if dest ~= nil then -- Only install if file wasn't filtered out
dest = MojoSetup.destination .. "/" .. dest
if permit_write(dest, ent.filename, ent.type, file) then
local desc = option.description
install_archive_entity(dest, ent, archive, desc, desc, perms)
end
end
end
local function install_archive(archive, file, option, dataprefix)
if not MojoSetup.archive.enumerate(archive) then
MojoSetup.fatal(_("Couldn't enumerate archive"))
end
local isbase = (archive == MojoSetup.archive.base)
local single_match = true
local wildcards = file.wildcards
-- If there's only one explicit file we're looking for, we don't have to
-- iterate the whole archive...we can stop as soon as we find it.
if wildcards == nil then
single_match = false
else
if type(wildcards) == "string" then
wildcards = { wildcards }
end
if #wildcards > 1 then
single_match = false
else
for i,v in ipairs(wildcards) do
if string.find(v, "[*?]") ~= nil then
single_match = false
break -- no reason to keep iterating...
end
end
end
end
local ent = MojoSetup.archive.enumnext(archive)
while ent ~= nil do
-- If inside GBaseArchive (no URL lead in string), then we
-- want to clip to data/ directory...
if isbase and (string.len(dataprefix) > 0) then
local count
ent.filename, count = string.gsub(ent.filename, "^" .. dataprefix, "", 1)
if count == 0 then
ent.filename = nil
end
end
-- See if we should install this file...
if (ent.filename ~= nil) and (ent.filename ~= "") then
local should_install = false
if wildcards == nil then
should_install = true
else
for i,v in ipairs(wildcards) do
if MojoSetup.wildcardmatch(ent.filename, v) then
should_install = true
break -- no reason to keep iterating...
end
end
end
if should_install then
install_archive_entry(archive, ent, file, option)
if single_match then
break -- no sense in iterating further if we're done.
end
end
end
-- and check the next entry in the archive...
ent = MojoSetup.archive.enumnext(archive)
end
end
local function install_basepath(basepath, file, option, dataprefix)
-- Obviously, we don't want to enumerate the entire physical filesystem,
-- so we'll dig through each path element with MojoPlatform_exists()
-- until we find one that doesn't, then we'll back up and try to open
-- that as a directory, and then a file archive, and start drilling from
-- there. Fun.
local function create_basepath_archive(path)
local archive = MojoSetup.archive.fromdir(path)
if archive == nil then
archive = MojoSetup.archive.fromfile(path)
if archive == nil then
MojoSetup.fatal(_("Couldn't open archive"))
end
end
return archive
end
-- fast path: See if the whole path exists. This is probably the normal
-- case, but it won't work for archives-in-archives.
if MojoSetup.platform.exists(basepath) then
local archive = create_basepath_archive(basepath)
install_archive(archive, file, option, dataprefix)
MojoSetup.archive.close(archive)
else
-- Check for archives-in-archives...
local path = ""
local paths = split_path(basepath)
for i,v in ipairs(paths) do
local knowngood = path
path = path .. "/" .. v
if not MojoSetup.platform.exists(path) then
if knowngood == "" then
MojoSetup.fatal(_("Archive not found"))
end
local archive = create_basepath_archive(knowngood)
local arclist = { archive }
path = rebuild_path(paths, i)
local arc = drill_for_archive(archive, path, arclist)
install_archive(arc, file, option, dataprefix)
close_archive_list(arclist)
return -- we're done here
end
end
-- wait, the whole thing exists now? Did this just move in?
install_basepath(basepath, file, option, dataprefix) -- try again, I guess...
end
end
local function set_destination(dest)
-- Chop any '/' chars from the end of the string...
dest = string.gsub(dest, "/+$", "")
MojoSetup.loginfo("Install dest: '" .. dest .. "'")
MojoSetup.destination = dest
MojoSetup.metadatadir = MojoSetup.destination .. "/" .. MojoSetup.metadatadirname
MojoSetup.controldir = MojoSetup.metadatadir -- .. "/control"
MojoSetup.manifestdir = MojoSetup.metadatadir .. "/manifest"
MojoSetup.scratchdir = MojoSetup.metadatadir .. "/tmp"
MojoSetup.rollbackdir = MojoSetup.scratchdir .. "/rollbacks"
MojoSetup.downloaddir = MojoSetup.scratchdir .. "/downloads"
end
local function run_config_defined_hook(func, pkg)
if func ~= nil then
local errstr = func(pkg)
if errstr == "" then
MojoSetup.fatal(nil)
elseif errstr ~= nil then
MojoSetup.fatal(errstr)
end
end
end
-- The XML manifest is compatible with the loki_setup manifest schema, since
-- it has a reasonable set of data, and allows you to use loki_update or
-- loki_patch with a MojoSetup installation. Please note that we never ever
-- look at this data! You are responsible for updating the other files if
-- you think it'll be important. The Unix MojoSetup uninstaller uses the
-- lua manifest, for example (but loki_uninstall can use the xml one,
-- so if you want, you can just drop in MojoSetup to replace loki_setup and
-- use the Loki tools for everything else.
local function build_xml_manifest(package)
local retval = {};
local function addstr(str)
retval[#retval+1] = str
end
addstr('<?xml version="1.0" encoding="UTF-8"?>\n')
addstr('<product name="')
addstr(package.id)
addstr('" desc="')
addstr(package.description)
addstr('" xmlversion="1.6" root="')
addstr(package.root)
addstr('" ')
if package.update_url ~= nil then
addstr('update_url="')
addstr(package.update_url)
addstr('"')
end
addstr('>\n')
addstr('\t<component name="Default" version="')
addstr(package.version)
addstr('" default="yes">\n')
-- Need to group these by options.
local grouped = {}
for fname,entity in pairs(package.manifest) do
local key = entity.key
if grouped[key] == nil then
grouped[key] = {}
end
entity.path = fname
local list = grouped[key]
list[#list+1] = entity
end
for desc,items in pairs(grouped) do
addstr('\t\t<option name="')
addstr(desc)
addstr('">\n')
for i,item in ipairs(items) do
local type = item.type
if type == "dir" then
type = "directory" -- loki_setup expects this string.
end
-- !!! FIXME: files from archives aren't filling item.mode in
-- !!! FIXME: because it figures out the perms from the archive's
-- !!! FIXME: C struct in native code. Need to get that into Lua.
-- !!! FIXME: (and get the perms uint16/string conversion cleaned up)
local mode = item.mode
if mode == nil then
mode = "0644" -- !!! FIXME
end
local path = item.path
item.path = nil -- we added this when grouping. Remove it now.
addstr('\t\t\t<')
addstr(type)
if type == "file" then
if item.checksums ~= nil then
for k,v in pairs(item.checksums) do
addstr(' ')
addstr(k)
addstr('="')
addstr(v)
addstr('"')
end
end
addstr(' mode="')
addstr(mode)
addstr('"')
elseif type == "directory" then
addstr(' mode="')
addstr(mode)
addstr('"')
elseif type == "symlink" then
addstr(' dest="')
addstr(item.linkdest)
addstr('" mode="0777"')
end
addstr('>')
addstr(path)
addstr('</')
addstr(type)
addstr('>\n')
end
addstr('\t\t</option>\n');
end
addstr('\t</component>\n</product>\n\n')
return retval
end
local function serialize(obj, prefix, postfix)
local retval = {}
local function addstr(str)
retval[#retval+1] = str
end
if prefix ~= nil then
addstr(prefix)
end
local function _serialize(obj, indent)
local objtype = type(obj)
if objtype == "nil" then
addstr("nil")
elseif (objtype == "number") or (objtype == "boolean") then
addstr(tostring(obj))
elseif objtype == "string" then
addstr(string.format("%q", obj))
elseif objtype == "function" then
addstr("load(")
addstr(string.format("%q", string.dump(obj)))
addstr(")")
elseif objtype == "table" then
addstr("{\n")
local tab = string.rep("\t", indent)
for k,v in pairs(obj) do
local key = k
addstr(tab)
if type(key) == "number" then
addstr('[')
addstr(key)
addstr(']')
elseif not string.match(key, "^[_a-zA-Z][_a-zA-Z0-9]*$") then
addstr('[')
addstr(string.format("%q", key))
addstr(']')
else
addstr(key)
end
addstr(" = ")
_serialize(v, indent+1)
addstr(",\n")
end
addstr(string.rep("\t", indent-1))
addstr("}")
else
MojoSetup.logerror("unexpected object to serialize (" ..
objtype .. "): '" .. tostring(obj) .. "'")
MojoSetup.fatal(_("BUG: Unhandled data type"))
end
end
_serialize(obj, 1)
if postfix ~= nil then
addstr(postfix)
end
return retval
end
local function build_lua_manifest(package)
return serialize(package, 'MojoSetup.package = ', '\n\n')
end
local function build_txt_manifest(package)
local flat = flatten_manifest(package.manifest)
local retval = {}
for i,v in pairs(flat) do
retval[#retval+1] = v
retval[#retval+1] = "\n"
end
return retval
end
local function install_control_app(desc, key)
local dst, src
-- We copy the installer binary itself, and any auxiliary files it needs,
-- like this Lua script, to a metadata directory in the installation.
-- Unfortunately, the binary might be a self-extracting installer that
-- has gigabytes of now-unnecessary data appended to it, so we need to
-- decide if that's the case and, if so, extract just the program itself
-- from the start of the file.
local maxbytes = -1 -- copy whole thing by default.
local base = MojoSetup.archive.base
dst = MojoSetup.controldir .. "/" .. MojoSetup.controlappname
src = MojoSetup.info.binarypath
if src == MojoSetup.info.basearchivepath then
maxbytes = MojoSetup.archive.offsetofstart(base)
if maxbytes <= 0 then
MojoSetup.fatal(_("BUG: Unexpected value"))
end