/
mojosetup_mainline.lua
1653 lines (1386 loc) · 56.5 KB
1
2
3
4
5
6
7
-- 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.
8
9
10
-- 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.
11
12
13
-- !!! FIXME: add a --dryrun option.
14
local _ = MojoSetup.translate
15
16
17
18
19
20
21
22
23
24
MojoSetup.metadatakey = ".mojosetup_metadata."
MojoSetup.metadatadesc = _("Metadata")
local function badcmdline()
MojoSetup.fatal(_("Invalid command line"))
end
25
26
27
28
29
30
31
32
33
34
35
36
-- 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
37
local function manifest_resync(man, fname, _key)
38
39
40
if fname == nil then return end
local fullpath = fname
41
fname = make_relative(fname, MojoSetup.destination)
42
43
if man[fname] == nil then
44
MojoSetup.logwarning("Tried to resync unknown file '" ..fname.. "' in manifest!")
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
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 = "directory"
else -- !!! FIXME: other types?
ftype = "file"
sums = MojoSetup.checksum(fullpath)
end
man[fname] =
{
key = _key,
type = ftype,
mode = perms,
checksums = sums,
linkdest = lndest
}
70
MojoSetup.logwarning("Resync'd file '" ..fname.. "' in manifest")
71
72
73
74
end
end
75
76
77
78
79
80
81
82
-- !!! 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.
83
local function manifest_add(man, fname, _key, ftype, mode, sums, lndest)
84
if (fname ~= nil) and (_key ~= nil) then
85
local destlen = string.len(MojoSetup.destination)
86
87
if string.sub(fname, 0, destlen) == MojoSetup.destination then
fname = string.sub(fname, destlen+2) -- make it relative.
88
89
90
end
if man[fname] ~= nil then
91
MojoSetup.logwarning("Overwriting file '" .. fname .. "' in manifest!")
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
end
man[fname] = {
key = _key,
type = ftype,
mode = perms,
checksums = sums,
linkdest = lndest
}
end
end
local function manifest_delete(man, fname)
if fname ~= nil then
local destlen = string.len(MojoSetup.destination)
108
109
if string.sub(fname, 0, destlen) == MojoSetup.destination then
fname = string.sub(fname, destlen+2) -- make it relative.
110
111
112
end
if man[fname] == nil then
113
MojoSetup.logwarning("Deleting unknown file '" .. fname .. "' from manifest!")
114
115
116
117
118
119
120
else
man[fname] = nil
end
end
end
121
122
123
124
125
local function do_delete(fname)
local retval = false
if fname == nil then
retval = true
else
126
127
128
129
130
if not MojoSetup.platform.exists(fname) then
retval = true
elseif MojoSetup.platform.unlink(fname) then
MojoSetup.loginfo("Deleted '" .. fname .. "'")
retval = true
131
132
133
134
135
end
end
return retval
end
136
137
138
139
140
141
142
143
144
145
146
local function delete_files(filelist, callback, error_is_fatal)
if filelist ~= nil then
local max = #filelist
for i = max,1,-1 do
local fname = filelist[i]
if not do_delete(fname) and error_is_fatal then
MojoSetup.fatal(_("Deletion failed!"))
end
if callback ~= nil then
147
callback(fname, i, max)
148
149
150
151
152
end
end
end
end
153
154
155
156
157
158
159
160
161
162
local function delete_rollbacks()
if MojoSetup.rollbacks == nil then
return
end
local fnames = {}
local max = #MojoSetup.rollbacks
for id = 1,max,1 do
fnames[id] = MojoSetup.rollbackdir .. "/" .. id
end
MojoSetup.rollbacks = {} -- just in case this gets called again...
163
delete_files(fnames) -- !!! FIXME: callback for gui queue pump?
164
165
end
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
local function delete_scratchdirs()
do_delete(MojoSetup.downloaddir)
do_delete(MojoSetup.rollbackdir)
do_delete(MojoSetup.scratchdir) -- must be last!
end
local function do_rollbacks()
if MojoSetup.rollbacks == nil then
return
end
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) 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."))
end
MojoSetup.loginfo("Restored rollback #" .. id .. ": '" .. src .. "' -> '" .. dest .. "'")
end
189
190
MojoSetup.rollbacks = {} -- just in case this gets called again...
191
192
193
end
194
-- get a linear array of filenames in the manifest.
195
local function flatten_manifest(man, postprocess)
196
local files = {}
197
198
199
if postprocess == nil then
postprocess = function(x) return x end
end
200
if man ~= nil then
201
for fname,items in pairs(man) do
202
files[#files+1] = postprocess(fname)
203
204
205
end
end
206
table.sort(files, function(a,b) return MojoSetup.strcmp(a,b) < 0 end)
207
208
209
210
return files
end
211
212
213
214
215
216
217
218
local function prepend_dest_dir(fname)
if fname == "" then
return MojoSetup.destination
end
return MojoSetup.destination .. "/" .. fname
end
219
220
-- This gets called by fatal()...must be a global function.
function MojoSetup.revertinstall()
221
222
223
224
-- (The real revertinstall is set later. This is a stub for startup.)
end
local function real_revertinstall()
225
if MojoSetup.gui_started then
226
MojoSetup.gui.final(_("Incomplete installation. We will revert any changes we made."))
227
228
end
229
MojoSetup.loginfo("Cleaning up half-finished installation...")
230
231
-- !!! FIXME: callbacks here.
232
delete_files(MojoSetup.downloads)
233
delete_files(flatten_manifest(MojoSetup.manifest, prepend_dest_dir))
234
do_rollbacks()
235
delete_scratchdirs()
236
237
end
238
239
240
241
242
243
244
local function calc_percent(current, total)
if total == 0 then
return 0
elseif total < 0 then
return -1
end
245
246
247
248
249
250
251
252
local retval = MojoSetup.truncatenum((current / total) * 100)
if retval > 100 then
retval = 100
elseif retval < 0 then
retval = 0
end
return retval
253
end
254
255
256
257
local function make_rate_string(rate, bw, total)
local retval = nil
258
259
260
if rate <= 0 then
retval = _("stalled")
261
else
262
263
264
265
266
local ratetype = _("B/s")
if rate > 1024 then
rate = MojoSetup.truncatenum(rate / 1024)
ratetype = _("KB/s")
end
267
268
269
270
271
272
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)
273
274
275
secsleft = string.sub("00" .. (secsleft - (minsleft * 60)), -2)
minsleft = string.sub("00" .. (minsleft - (hoursleft * 60)), -2)
276
277
278
if hoursleft < 10 then
hoursleft = "0" .. hoursleft
279
else
280
hoursleft = tostring(hoursleft)
281
end
282
283
retval = MojoSetup.format(_("%0 %1, %2:%3:%4 remaining"),
284
285
rate, ratetype,
hoursleft, minsleft, secsleft)
286
else
287
retval = MojoSetup.format(_("%0 %1"), rate, ratetype)
288
289
end
end
290
291
return retval
292
293
294
end
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
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
320
321
322
323
local function close_archive_list(arclist)
for i = #arclist,1,-1 do
MojoSetup.archive.close(arclist[i])
arclist[i] = nil
324
325
326
327
328
end
end
-- This code's a little nasty...
329
local function drill_for_archive(archive, path, arclist)
330
if not MojoSetup.archive.enumerate(archive) then
331
MojoSetup.fatal(_("Couldn't enumerate archive"))
332
333
334
end
local pathtab = split_path(path)
335
local ent = MojoSetup.archive.enumnext(archive)
336
337
338
339
340
341
342
343
344
345
346
347
348
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
349
MojoSetup.fatal(_("Couldn't open archive"))
350
end
351
arclist[#arclist+1] = arc
352
353
354
if pathtab[i] == nil then
return arc -- this is the end of the path! Done drilling!
end
355
return drill_for_archive(arc, rebuild_path(pathtab, i), arclist)
356
357
end
end
358
ent = MojoSetup.archive.enumnext(archive)
359
360
end
361
MojoSetup.fatal(_("Archive not found"))
362
363
364
end
365
local function install_file(dest, perms, writefn, desc, manifestkey)
366
-- Upvalued so we don't look these up each time...
367
local fname = string.gsub(dest, "^.*/", "", 1) -- chop the dirs off...
368
local ptype = _("Installing")
369
local component = desc
370
local keepgoing = true
371
local callback = function(ticks, justwrote, bw, total)
372
373
374
375
376
local percent = -1
local item = fname
if total >= 0 then
MojoSetup.written = MojoSetup.written + justwrote
percent = calc_percent(MojoSetup.written, MojoSetup.totalwrite)
377
item = MojoSetup.format(_("%0: %1%%"), fname, calc_percent(bw, total))
378
end
379
380
keepgoing = MojoSetup.gui.progress(ptype, component, percent, item)
return keepgoing
381
382
end
383
384
-- !!! FIXME: maybe keep a separate list, so we can rollback installs
-- !!! FIXME: that are building on previous installations?
385
-- Add to manifest first, so we can delete it during rollback if i/o fails.
386
-- !!! FIXME: perms may be nil...we need a MojoSetup.defaultPermsString()...
387
manifest_add(MojoSetup.manifest, dest, manifestkey, "file", perms, nil, nil)
388
389
local written, sums = writefn(callback)
390
if not written then
391
392
if not keepgoing then
MojoSetup.logerror("User cancelled install during file write.")
393
MojoSetup.fatal()
394
else
395
MojoSetup.logerror("Failed to create file '" .. dest .. "'")
396
397
MojoSetup.fatal(_("File creation failed!"))
end
398
end
399
400
-- Readd it to the manifest, now with a checksum!
401
402
403
404
if manifestkey ~= nil then
manifest_delete(MojoSetup.manifest, dest)
manifest_add(MojoSetup.manifest, dest, manifestkey, "file", perms, sums, nil)
end
405
406
407
408
409
410
411
MojoSetup.loginfo("Created file '" .. dest .. "'")
end
local function install_file_from_archive(dest, archive, perms, desc, manifestkey)
local fn = function(callback)
412
return MojoSetup.writefile(archive, dest, perms, nil, callback)
413
end
414
415
return install_file(dest, perms, fn, desc, manifestkey)
end
416
417
418
419
local function install_file_from_string(dest, string, perms, desc, manifestkey)
local fn = function(callback)
420
return MojoSetup.stringtofile(string, dest, perms, nil, callback)
421
422
end
return install_file(dest, perms, fn, desc, manifestkey)
423
424
425
end
426
427
428
429
430
431
432
433
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
434
435
436
-- !!! FIXME: we should probably pump the GUI queue here, in case there are
-- !!! FIXME: thousands of symlinks in a row or something.
local function install_symlink(dest, lndest, manifestkey)
437
438
if not MojoSetup.platform.symlink(dest, lndest) then
MojoSetup.logerror("Failed to create symlink '" .. dest .. "'")
439
MojoSetup.fatal(_("Symlink creation failed!"))
440
end
441
442
manifest_add(MojoSetup.manifest, dest, manifestkey, "symlink", nil, nil, lndest)
443
MojoSetup.loginfo("Created symlink '" .. dest .. "' -> '" .. lndest .. "'")
444
445
446
end
447
448
449
-- !!! FIXME: we should probably pump the GUI queue here, in case there are
-- !!! FIXME: thousands of dirs in a row or something.
local function install_directory(dest, perms, manifestkey)
450
451
452
-- Chop any '/' chars from the end of the string...
dest = string.gsub(dest, "/+$", "")
453
454
if not MojoSetup.platform.mkdir(dest, perms) then
MojoSetup.logerror("Failed to create dir '" .. dest .. "'")
455
MojoSetup.fatal(_("Directory creation failed"))
456
end
457
458
manifest_add(MojoSetup.manifest, dest, manifestkey, "directory", perms, nil, nil)
459
MojoSetup.loginfo("Created directory '" .. dest .. "'")
460
461
462
end
463
local function install_parent_dirs(path, manifestkey)
464
-- Chop any '/' chars from the end of the string...
465
path = string.gsub(path, "/+$", "")
466
467
468
469
-- Build each piece of final path. The gmatch() skips the last element.
local fullpath = ""
for item in string.gmatch(path, "(.-)/") do
470
if item ~= "" then
471
472
fullpath = fullpath .. "/" .. item
if not MojoSetup.platform.exists(fullpath) then
473
install_directory(fullpath, nil, manifestkey)
474
475
476
477
478
479
480
481
482
483
484
485
486
end
end
end
end
local function permit_write(dest, entinfo, file)
local allowoverwrite = true
if MojoSetup.platform.exists(dest) then
-- never "permit" existing dirs, so they don't rollback.
if entinfo.type == "dir" then
allowoverwrite = false
else
487
488
489
490
491
492
493
if MojoSetup.forceoverwrite ~= nil then
allowoverwrite = MojoSetup.forceoverwrite
else
-- !!! FIXME: option/package-wide overwrite?
allowoverwrite = file.allowoverwrite
if not allowoverwrite then
MojoSetup.loginfo("File '" .. dest .. "' already exists.")
494
local text = MojoSetup.format(_("File '%0' already exists! Replace?"), dest)
495
local ynan = MojoSetup.promptynan(_("Conflict!"), text, true)
496
497
498
499
500
501
502
503
504
505
506
507
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
508
509
end
510
511
-- !!! FIXME: Setup.File.mustoverwrite to override "never"?
512
513
if allowoverwrite then
local id = #MojoSetup.rollbacks + 1
514
local f = MojoSetup.rollbackdir .. "/" .. id
515
install_parent_dirs(f, MojoSetup.metadatakey)
516
MojoSetup.rollbacks[id] = dest
517
518
519
520
if not MojoSetup.movefile(dest, f) then
MojoSetup.fatal(_("Couldn't backup file for rollback"))
end
MojoSetup.loginfo("Moved rollback #" .. id .. ": '" .. dest .. "' -> '" .. f .. "'")
521
522
523
524
525
-- Make sure this isn't already in the manifest...
if MojoSetup.manifest[dest] ~= nil then
manifest_delete(MojoSetup.manifest, dest)
end
526
527
528
529
530
531
532
end
end
end
return allowoverwrite
end
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
local function install_archive_entity(dest, ent, archive, desc, manifestkey)
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, manifestkey)
elseif ent.type == "symlink" then
install_symlink(dest, ent.linkdest, manifestkey)
else -- !!! FIXME: device nodes, etc...
-- !!! FIXME: should this be fatal?
MojoSetup.fatal(_("Unknown file type in archive"))
end
end
549
local function install_archive_entry(archive, ent, file, option)
550
local entdest = ent.filename
551
552
if entdest == nil then return end -- probably can't happen...
553
554
555
-- Set destination in native filesystem. May be default or explicit.
local dest = file.destination
if dest == nil then
556
557
558
dest = entdest
else
dest = dest .. "/" .. entdest
559
560
end
561
562
local perms = file.permissions -- may be nil
563
if file.filter ~= nil then
564
565
566
567
568
local filterperms
dest, filterperms = file.filter(dest)
if filterperms ~= nil then
perms = filterperms
end
569
570
571
572
573
end
if dest ~= nil then -- Only install if file wasn't filtered out
dest = MojoSetup.destination .. "/" .. dest
if permit_write(dest, ent, file) then
574
local desc = option.description
575
install_archive_entity(dest, ent, archive, desc, desc)
576
577
578
579
580
end
end
end
581
local function install_archive(archive, file, option)
582
if not MojoSetup.archive.enumerate(archive) then
583
MojoSetup.fatal(_("Couldn't enumerate archive"))
584
585
end
586
local isbase = (archive == MojoSetup.archive.base)
587
588
589
local single_match = true
local wildcards = file.wildcards
590
591
592
593
594
-- 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
595
596
597
if type(wildcards) == "string" then
wildcards = { wildcards }
end
598
599
600
601
602
603
604
605
if #wildcards > 1 then
single_match = false
else
for k,v in ipairs(wildcards) do
if string.find(v, "[*?]") ~= nil then
single_match = false
break -- no reason to keep iterating...
end
606
607
608
609
end
end
end
610
611
local ent = MojoSetup.archive.enumnext(archive)
while ent ~= nil do
612
613
614
-- If inside GBaseArchive (no URL lead in string), then we
-- want to clip to data/ directory...
if isbase then
615
616
617
618
619
620
local count
ent.filename, count = string.gsub(ent.filename, "^data/", "", 1)
if count == 0 then
ent.filename = nil
end
end
621
622
-- See if we should install this file...
623
if (ent.filename ~= nil) and (ent.filename ~= "") then
624
625
626
627
628
629
630
631
632
633
634
635
636
local should_install = false
if wildcards == nil then
should_install = true
else
for k,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
637
install_archive_entry(archive, ent, file, option)
638
639
if single_match then
break -- no sense in iterating further if we're done.
640
641
642
643
644
645
646
end
end
end
-- and check the next entry in the archive...
ent = MojoSetup.archive.enumnext(archive)
end
647
648
end
649
650
651
652
653
654
655
local function install_basepath(basepath, file, option)
-- 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.
656
657
658
659
660
local function create_basepath_archive(path)
local archive = MojoSetup.archive.fromdir(path)
if archive == nil then
archive = MojoSetup.archive.fromfile(path)
661
if archive == nil then
662
MojoSetup.fatal(_("Couldn't open archive"))
663
end
664
665
666
667
668
669
670
671
672
673
674
675
676
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)
MojoSetup.archive.close(archive)
else
-- Check for archives-in-archives...
local path = ""
677
local paths = split_path(basepath)
678
679
680
681
for i,v in ipairs(paths) do
local knowngood = path
path = path .. "/" .. v
if not MojoSetup.platform.exists(path) then
682
if knowngood == "" then
683
MojoSetup.fatal(_("Archive not found"))
684
end
685
686
687
688
689
690
691
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)
close_archive_list(arclist)
return -- we're done here
692
693
end
end
694
695
696
-- wait, the whole thing exists now? Did this just move in?
install_basepath(basepath, file, option) -- try again, I guess...
697
698
699
700
end
end
701
local function set_destination(dest)
702
703
704
-- Chop any '/' chars from the end of the string...
dest = string.gsub(dest, "/+$", "")
705
706
MojoSetup.loginfo("Install dest: '" .. dest .. "'")
MojoSetup.destination = dest
707
MojoSetup.metadatadir = MojoSetup.destination .. "/.mojosetup"
708
MojoSetup.controldir = MojoSetup.metadatadir -- .. "/control"
709
710
MojoSetup.manifestdir = MojoSetup.metadatadir .. "/manifest"
MojoSetup.scratchdir = MojoSetup.metadatadir .. "/tmp"
711
712
713
714
715
MojoSetup.rollbackdir = MojoSetup.scratchdir .. "/rollbacks"
MojoSetup.downloaddir = MojoSetup.scratchdir .. "/downloads"
end
716
717
718
719
720
721
722
723
724
725
local function run_config_defined_hook(func, install)
if func ~= nil then
local errstr = func(install)
if errstr ~= nil then
MojoSetup.fatal(errstr)
end
end
end
726
727
728
729
730
-- 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
731
-- lua manifest, for example (but loki_uninstall can use the xml one,
732
733
-- so if you want, you can just drop in MojoSetup to replace loki_setup and
-- use the Loki tools for everything else.
734
735
local function build_xml_manifest(package)
local updateurl = package.update_url
736
737
738
739
740
741
742
743
if updateurl ~= nil then
updateurl = 'update_url="' .. updateurl .. '"'
else
updateurl = ''
end
local retval =
'<?xml version="1.0" encoding="UTF-8"?>\n' ..
744
'<product name="' .. package.id .. '" desc="' ..
745
746
package.description .. '" xmlversion="1.6" root="' ..
package.root .. '" ' .. updateurl .. '>\n' ..
747
'\t<component name="Default" version="' .. package.version ..
748
749
'" default="yes">\n'
750
751
-- Need to group these by options.
local grouped = {}
752
for fname,entity in pairs(package.manifest) do
753
local key = entity.key
754
if grouped[key] == nil then
755
grouped[key] = {}
756
end
757
758
759
760
entity.path = fname
local list = grouped[key]
list[#list+1] = entity
end
761
762
for desc,items in pairs(grouped) do
763
764
765
766
767
768
769
770
771
772
773
774
775
776
retval = retval .. '\t\t<option name="' .. desc .. '">\n'
for i,item in ipairs(items) do
local type = item.type
-- !!! 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
777
item.path = nil -- we added this when grouping. Remove it now.
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
retval = retval .. '\t\t\t<' .. type
if type == "file" then
if item.checksums ~= nil then
for k,v in pairs(item.checksums) do
retval = retval .. ' ' .. k .. '="' .. v .. '"'
end
end
retval = retval .. ' mode="' .. mode .. '"'
elseif type == "directory" then
retval = retval .. ' mode="' .. mode .. '"'
elseif type == "symlink" then
retval = retval .. ' dest="' .. item.linkdest .. '" mode="0777"'
end
retval = retval .. '>' .. path .. "</" .. type .. ">\n"
end
retval = retval .. '\t\t</option>\n'
end
retval = retval .. '\t</component>\n'
retval = retval .. '</product>\n\n'
return retval
end
806
807
local function serialize(obj)
local function _serialize(obj, indent)
808
809
local retval = ''
local objtype = type(obj)
810
811
812
if objtype == "nil" then
retval = "nil"
elseif objtype == "number" then
813
814
815
816
817
retval = tostring(obj)
elseif objtype == "string" then
retval = string.format("%q", obj)
elseif objtype == "table" then
retval = "{\n"
818
local tab = string.rep("\t", indent)
819
820
821
822
823
824
825
for k,v in pairs(obj) do
local key = k
if type(key) == "number" then
key = '[' .. key .. ']'
elseif not string.match(key, "^[_a-zA-Z][_a-zA-Z0-9]*$") then
key = '[' .. string.format("%q", key) .. ']'
end
826
827
retval = retval .. tab .. key .. " = " ..
_serialize(v, indent+1) .. ",\n"
828
end
829
retval = retval .. string.rep("\t", indent-1) .. "}"
830
else
831
832
MojoSetup.logerror("unexpected object to serialize (" ..
objtype .. "): '" .. tostring(obj) .. "'")
833
834
835
836
837
MojoSetup.fatal(_("BUG: Unhandled data type"))
end
return retval
end
838
839
840
841
842
843
return _serialize(obj, 1)
end
local function build_lua_manifest(package)
return 'MojoSetup.package = ' .. serialize(package) .. "\n\n"
844
845
846
end
847
local function build_txt_manifest(package)
848
local retval = ''
849
for i,path in ipairs(flatten_manifest(package.manifest)) do
850
retval = retval .. path .. "\n"
851
852
853
854
855
end
return retval
end
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
local function install_control_app(desc, key)
local dst, src
-- We copy the installer binary itself, and any auxillary 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
-- !!! FIXME: This needs an ".exe" appended on Windows.
dst = MojoSetup.controldir .. "/mojosetup"
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
end
local perms = "0755" -- !!! FIXME
879
install_parent_dirs(dst, key)
880
881
882
883
884
885
886
install_file_from_filesystem(dst, src, perms, desc, key, maxbytes)
-- Okay, now we need all the support files.
if not MojoSetup.archive.enumerate(base) then
MojoSetup.fatal(_("Couldn't enumerate archive"))
end
887
local needdirs = { "scripts", "guis", "meta" }
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
local ent = MojoSetup.archive.enumnext(base)
while ent ~= nil do
-- Make sure this is in a directory we want to write out...
local should_write = false
if (ent.filename ~= nil) and (ent.filename ~= "") then
for i,dir in ipairs(needdirs) do
local clipdir = "^" .. dir .. "/"
if string.find(ent.filename, clipdir) ~= nil then
should_write = true
break
end
end
end
if should_write then
dst = MojoSetup.controldir .. "/" .. ent.filename
install_archive_entity(dst, ent, base, desc, key)
end
-- and check the next entry in the archive...
909
ent = MojoSetup.archive.enumnext(base)
910
911
912
913
914
915
916
end
-- okay, we're written out.
end
local function install_manifests(desc, key)
917
918
919
920
921
922
923
924
925
-- We write out a Lua script as a data definition language, a
-- loki_setup-compatible XML manifest, and a straight text file of
-- all the filenames. Take your pick.
local perms = "0644" -- !!! FIXME
local basefname = MojoSetup.manifestdir .. "/" .. MojoSetup.install.id
local lua_fname = basefname .. ".lua"
local xml_fname = basefname .. ".xml"
local txt_fname = basefname .. ".txt"
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
-- We have to cheat and just plug these into the manifest directly, since
-- they won't show up until after we write them out, otherwise.
-- Obviously, we don't have checksums for them, either, as we haven't
-- assembled the data yet!
manifest_add(MojoSetup.manifest, lua_fname, key, "file", perms, nil, nil)
manifest_add(MojoSetup.manifest, xml_fname, key, "file", perms, nil, nil)
manifest_add(MojoSetup.manifest, txt_fname, key, "file", perms, nil, nil)
-- build the "package" table that we serialize, etc.
local package =
{
id = MojoSetup.install.id,
description = MojoSetup.install.description,
root = MojoSetup.destination,
update_url = MojoSetup.install.updateurl,
version = MojoSetup.install.version,
944
945
manifest = MojoSetup.manifest,
splash = MojoSetup.install.splash
946
}
947
948
-- now build these things...
949
install_parent_dirs(lua_fname, key)
950
951
952
install_file_from_string(lua_fname, build_lua_manifest(package), perms, desc, nil)
install_file_from_string(xml_fname, build_xml_manifest(package), perms, desc, nil)
install_file_from_string(txt_fname, build_txt_manifest(package), perms, desc, nil)
953
954
955
end
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
local function start_gui(desc, splashfname)
if splashfname ~= nil then
splashfname = 'meta/' .. splashfname
end
if not MojoSetup.gui.start(desc, splashfname) then
MojoSetup.fatal(_("GUI failed to start"))
end
MojoSetup.gui_started = true
end
local function stop_gui()
MojoSetup.gui.stop()
MojoSetup.gui_started = nil
end
975
local function do_install(install)
976
MojoSetup.forceoverwrite = nil
977
978
979
980
MojoSetup.written = 0
MojoSetup.totalwrite = 0
MojoSetup.downloaded = 0
MojoSetup.totaldownload = 0
981
MojoSetup.install = install
982
983
984
985
986
987
-- !!! FIXME: try to sanity check everything we can here
-- !!! FIXME: (unsupported URLs, bogus media IDs, etc.)
-- !!! FIXME: I would like everything possible to fail here instead of
-- !!! FIXME: when a user happens to pick an option no one tested...
988
if install.options ~= nil and install.optiongroups ~= nil then
989
MojoSetup.fatal(_("BUG: no options"))
990
991
end
992
993
994
995
996
997
-- This is to save us the trouble of a loop every time we have to
-- find media by id...
MojoSetup.media = {}
if install.medias ~= nil then
for k,v in pairs(install.medias) do
if MojoSetup.media[v.id] ~= nil then
998
MojoSetup.fatal(_("BUG: duplicate media id"))
999
1000
end
MojoSetup.media[v.id] = v