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