Skip to content

Latest commit

 

History

History
1304 lines (1116 loc) · 44.3 KB

mojosetup_mainline.lua

File metadata and controls

1304 lines (1116 loc) · 44.3 KB
 
May 12, 2007
May 12, 2007
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.
Dec 20, 2006
Dec 20, 2006
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.
Dec 6, 2006
Dec 6, 2006
11
May 1, 2007
May 1, 2007
12
13
-- !!! FIXME: add a --dryrun option.
Dec 12, 2006
Dec 12, 2006
14
local _ = MojoSetup.translate
Dec 6, 2006
Dec 6, 2006
15
May 7, 2007
May 7, 2007
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
local function do_delete(fname)
local retval = false
if fname == nil then
retval = true
else
if MojoSetup.platform.exists(fname) then
if MojoSetup.platform.unlink(fname) then
MojoSetup.loginfo("Deleted '" .. fname .. "'")
retval = true
end
end
end
return retval
end
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
callback(i, max)
end
end
end
end
May 17, 2007
May 17, 2007
47
48
49
50
51
52
53
54
55
56
57
58
59
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...
delete_files(fnames)
end
May 7, 2007
May 7, 2007
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
May 17, 2007
May 17, 2007
83
84
MojoSetup.rollbacks = {} -- just in case this gets called again...
May 7, 2007
May 7, 2007
85
86
87
end
Jan 13, 2008
Jan 13, 2008
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
-- get a linear array of filenames in the manifest.
local function flatten_manifest()
local man = MojoSetup.manifest
local files = {}
if man ~= nil then
for key,items in pairs(man) do
for i,item in ipairs(items) do
local path = item.path
if path ~= nil then
files[#files+1] = path
end
end
end
end
return files
end
May 7, 2007
May 7, 2007
108
109
-- This gets called by fatal()...must be a global function.
function MojoSetup.revertinstall()
May 12, 2007
May 12, 2007
110
if MojoSetup.gui_started then
Jan 12, 2008
Jan 12, 2008
111
MojoSetup.gui.final(_("Incomplete installation. We will revert any changes we made."))
May 12, 2007
May 12, 2007
112
113
end
Jan 12, 2008
Jan 12, 2008
114
MojoSetup.loginfo("Cleaning up half-finished installation...")
May 7, 2007
May 7, 2007
115
May 12, 2007
May 12, 2007
116
-- !!! FIXME: callbacks here.
May 7, 2007
May 7, 2007
117
delete_files(MojoSetup.downloads)
Jan 13, 2008
Jan 13, 2008
118
delete_files(flatten_manifest())
May 7, 2007
May 7, 2007
119
do_rollbacks()
Jan 12, 2008
Jan 12, 2008
120
delete_scratchdirs()
May 7, 2007
May 7, 2007
121
122
end
Dec 20, 2006
Dec 20, 2006
123
May 7, 2007
May 7, 2007
124
125
126
127
128
129
local function calc_percent(current, total)
if total == 0 then
return 0
elseif total < 0 then
return -1
end
May 20, 2007
May 20, 2007
130
131
132
133
134
135
136
137
local retval = MojoSetup.truncatenum((current / total) * 100)
if retval > 100 then
retval = 100
elseif retval < 0 then
retval = 0
end
return retval
May 7, 2007
May 7, 2007
138
end
Apr 22, 2007
Apr 22, 2007
139
May 17, 2007
May 17, 2007
140
Jan 14, 2008
Jan 14, 2008
141
142
local function make_rate_string(rate, bw, total)
local retval = nil
May 17, 2007
May 17, 2007
143
Jan 14, 2008
Jan 14, 2008
144
145
if rate <= 0 then
retval = _("stalled")
May 17, 2007
May 17, 2007
146
else
Jan 14, 2008
Jan 14, 2008
147
148
149
150
151
local ratetype = _("B/s")
if rate > 1024 then
rate = MojoSetup.truncatenum(rate / 1024)
ratetype = _("KB/s")
end
May 17, 2007
May 17, 2007
152
Jan 14, 2008
Jan 14, 2008
153
154
155
156
157
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)
May 17, 2007
May 17, 2007
158
Jan 14, 2008
Jan 14, 2008
159
160
secsleft = string.sub("00" .. (secsleft - (minsleft * 60)), -2)
minsleft = string.sub("00" .. (minsleft - (hoursleft * 60)), -2)
May 17, 2007
May 17, 2007
161
Jan 14, 2008
Jan 14, 2008
162
163
if hoursleft < 10 then
hoursleft = "0" .. hoursleft
May 17, 2007
May 17, 2007
164
else
Jan 14, 2008
Jan 14, 2008
165
hoursleft = tostring(hoursleft)
May 17, 2007
May 17, 2007
166
end
Jan 14, 2008
Jan 14, 2008
167
Jan 15, 2008
Jan 15, 2008
168
retval = MojoSetup.format(_("%0 %1, %2:%3:%4 remaining"),
Jan 14, 2008
Jan 14, 2008
169
170
rate, ratetype,
hoursleft, minsleft, secsleft)
May 17, 2007
May 17, 2007
171
else
Jan 15, 2008
Jan 15, 2008
172
retval = MojoSetup.format(_("%0 %1"), rate, ratetype)
May 17, 2007
May 17, 2007
173
174
end
end
Jan 14, 2008
Jan 14, 2008
175
176
return retval
May 17, 2007
May 17, 2007
177
178
179
end
May 2, 2007
May 2, 2007
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
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
May 3, 2007
May 3, 2007
205
206
207
208
local function close_archive_list(arclist)
for i = #arclist,1,-1 do
MojoSetup.archive.close(arclist[i])
arclist[i] = nil
May 2, 2007
May 2, 2007
209
210
211
212
213
end
end
-- This code's a little nasty...
May 3, 2007
May 3, 2007
214
local function drill_for_archive(archive, path, arclist)
May 4, 2007
May 4, 2007
215
if not MojoSetup.archive.enumerate(archive) then
Jan 15, 2008
Jan 15, 2008
216
MojoSetup.fatal(_("Couldn't enumerate archive"))
May 2, 2007
May 2, 2007
217
218
219
end
local pathtab = split_path(path)
May 2, 2007
May 2, 2007
220
local ent = MojoSetup.archive.enumnext(archive)
May 2, 2007
May 2, 2007
221
222
223
224
225
226
227
228
229
230
231
232
233
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
Jan 15, 2008
Jan 15, 2008
234
MojoSetup.fatal(_("Couldn't open archive"))
May 2, 2007
May 2, 2007
235
end
May 3, 2007
May 3, 2007
236
arclist[#arclist+1] = arc
May 2, 2007
May 2, 2007
237
238
239
if pathtab[i] == nil then
return arc -- this is the end of the path! Done drilling!
end
May 3, 2007
May 3, 2007
240
return drill_for_archive(arc, rebuild_path(pathtab, i), arclist)
May 2, 2007
May 2, 2007
241
242
end
end
May 3, 2007
May 3, 2007
243
ent = MojoSetup.archive.enumnext(archive)
May 2, 2007
May 2, 2007
244
245
end
Jan 15, 2008
Jan 15, 2008
246
MojoSetup.fatal(_("Archive not found"))
May 2, 2007
May 2, 2007
247
248
249
end
Jan 13, 2008
Jan 13, 2008
250
local function install_file(dest, perms, writefn, desc, manifestkey)
May 7, 2007
May 7, 2007
251
-- Upvalued so we don't look these up each time...
May 27, 2007
May 27, 2007
252
local fname = string.gsub(dest, "^.*/", "", 1) -- chop the dirs off...
Jan 14, 2008
Jan 14, 2008
253
local ptype = _("Installing")
Jan 13, 2008
Jan 13, 2008
254
local component = desc
May 10, 2007
May 10, 2007
255
local keepgoing = true
May 8, 2007
May 8, 2007
256
local callback = function(ticks, justwrote, bw, total)
May 17, 2007
May 17, 2007
257
258
259
260
261
local percent = -1
local item = fname
if total >= 0 then
MojoSetup.written = MojoSetup.written + justwrote
percent = calc_percent(MojoSetup.written, MojoSetup.totalwrite)
Jan 14, 2008
Jan 14, 2008
262
item = MojoSetup.format(_("%0: %1%%"), fname, calc_percent(bw, total))
May 17, 2007
May 17, 2007
263
end
May 10, 2007
May 10, 2007
264
265
keepgoing = MojoSetup.gui.progress(ptype, component, percent, item)
return keepgoing
May 7, 2007
May 7, 2007
266
267
end
Jan 13, 2008
Jan 13, 2008
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
-- Add to manifest first, so we can delete it during rollback if i/o fails.
local manifestentry =
{
type = "file",
path = dest,
mode = perms, -- !!! FIXME: perms may be nil...we need a MojoSetup.defaultPermsString()...
}
if manifestkey ~= nil then
if MojoSetup.manifest[manifestkey] == nil then
MojoSetup.manifest[manifestkey] = {}
end
local manifest = MojoSetup.manifest[manifestkey]
manifest[#manifest+1] = manifestentry
end
local written, sums = writefn(callback)
May 27, 2007
May 27, 2007
284
if not written then
May 10, 2007
May 10, 2007
285
286
if not keepgoing then
MojoSetup.logerror("User cancelled install during file write.")
Jan 12, 2008
Jan 12, 2008
287
MojoSetup.fatal()
May 10, 2007
May 10, 2007
288
else
May 27, 2007
May 27, 2007
289
MojoSetup.logerror("Failed to create file '" .. dest .. "'")
May 10, 2007
May 10, 2007
290
291
MojoSetup.fatal(_("File creation failed!"))
end
May 1, 2007
May 1, 2007
292
end
May 27, 2007
May 27, 2007
293
Jan 13, 2008
Jan 13, 2008
294
manifestentry.checksums = sums
May 27, 2007
May 27, 2007
295
Jan 13, 2008
Jan 13, 2008
296
297
298
299
300
301
MojoSetup.loginfo("Created file '" .. dest .. "'")
end
local function install_file_from_archive(dest, archive, perms, desc, manifestkey)
local fn = function(callback)
Jan 16, 2008
Jan 16, 2008
302
return MojoSetup.writefile(archive, dest, perms, nil, callback)
May 27, 2007
May 27, 2007
303
end
Jan 13, 2008
Jan 13, 2008
304
305
return install_file(dest, perms, fn, desc, manifestkey)
end
May 27, 2007
May 27, 2007
306
Jan 13, 2008
Jan 13, 2008
307
308
309
310
311
312
local function install_file_from_string(dest, string, perms, desc, manifestkey)
local fn = function(callback)
return MojoSetup.stringtofile(string, dest, perms, callback)
end
return install_file(dest, perms, fn, desc, manifestkey)
May 1, 2007
May 1, 2007
313
314
315
end
Jan 13, 2008
Jan 13, 2008
316
317
318
-- !!! 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)
May 27, 2007
May 27, 2007
319
320
if not MojoSetup.platform.symlink(dest, lndest) then
MojoSetup.logerror("Failed to create symlink '" .. dest .. "'")
Jan 14, 2008
Jan 14, 2008
321
MojoSetup.fatal(_("Symlink creation failed!"))
May 1, 2007
May 1, 2007
322
end
May 27, 2007
May 27, 2007
323
Jan 13, 2008
Jan 13, 2008
324
325
326
if manifestkey ~= nil then
if MojoSetup.manifest[manifestkey] == nil then
MojoSetup.manifest[manifestkey] = {}
May 27, 2007
May 27, 2007
327
end
Jan 13, 2008
Jan 13, 2008
328
local manifest = MojoSetup.manifest[manifestkey]
May 27, 2007
May 27, 2007
329
330
331
332
333
334
335
manifest[#manifest+1] =
{
type = "symlink",
path = dest,
linkdest = lndest,
}
end
May 27, 2007
May 27, 2007
336
337
MojoSetup.loginfo("Created symlink '" .. dest .. "' -> '" .. lndest .. "'")
May 1, 2007
May 1, 2007
338
339
340
end
Jan 13, 2008
Jan 13, 2008
341
342
343
-- !!! 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)
May 27, 2007
May 27, 2007
344
345
if not MojoSetup.platform.mkdir(dest, perms) then
MojoSetup.logerror("Failed to create dir '" .. dest .. "'")
Jan 14, 2008
Jan 14, 2008
346
MojoSetup.fatal(_("Directory creation failed"))
May 1, 2007
May 1, 2007
347
end
May 27, 2007
May 27, 2007
348
Jan 13, 2008
Jan 13, 2008
349
350
351
if manifestkey ~= nil then
if MojoSetup.manifest[manifestkey] == nil then
MojoSetup.manifest[manifestkey] = {}
May 27, 2007
May 27, 2007
352
end
Jan 13, 2008
Jan 13, 2008
353
local manifest = MojoSetup.manifest[manifestkey]
May 27, 2007
May 27, 2007
354
355
356
357
358
359
360
manifest[#manifest+1] =
{
type = "dir",
path = dest,
mode = perms,
}
end
May 27, 2007
May 27, 2007
361
362
MojoSetup.loginfo("Created directory '" .. dest .. "'")
May 1, 2007
May 1, 2007
363
364
365
end
Jan 13, 2008
Jan 13, 2008
366
local function install_parent_dirs(path, manifestkey)
May 1, 2007
May 1, 2007
367
-- Chop any '/' chars from the end of the string...
May 2, 2007
May 2, 2007
368
path = string.gsub(path, "/+$", "")
May 1, 2007
May 1, 2007
369
370
371
372
-- Build each piece of final path. The gmatch() skips the last element.
local fullpath = ""
for item in string.gmatch(path, "(.-)/") do
May 2, 2007
May 2, 2007
373
if item ~= "" then
May 1, 2007
May 1, 2007
374
375
fullpath = fullpath .. "/" .. item
if not MojoSetup.platform.exists(fullpath) then
Jan 13, 2008
Jan 13, 2008
376
install_directory(fullpath, nil, manifestkey)
May 1, 2007
May 1, 2007
377
378
379
380
381
382
383
384
385
386
387
388
389
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
May 17, 2007
May 17, 2007
390
391
392
393
394
395
396
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.")
Jan 15, 2008
Jan 15, 2008
397
local text = MojoSetup.format(_("File '%0' already exists! Replace?"), dest);
Jan 14, 2008
Jan 14, 2008
398
local ynan = MojoSetup.promptynan(_("Conflict!"), text, true)
May 17, 2007
May 17, 2007
399
400
401
402
403
404
405
406
407
408
409
410
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
May 1, 2007
May 1, 2007
411
412
end
May 17, 2007
May 17, 2007
413
414
-- !!! FIXME: Setup.File.mustoverwrite to override "never"?
May 1, 2007
May 1, 2007
415
416
if allowoverwrite then
local id = #MojoSetup.rollbacks + 1
May 7, 2007
May 7, 2007
417
local f = MojoSetup.rollbackdir .. "/" .. id
May 27, 2007
May 27, 2007
418
install_parent_dirs(f, nil)
May 7, 2007
May 7, 2007
419
MojoSetup.rollbacks[id] = dest
May 1, 2007
May 1, 2007
420
421
422
423
424
425
426
427
428
429
430
if not MojoSetup.movefile(dest, f) then
MojoSetup.fatal(_("Couldn't backup file for rollback"))
end
MojoSetup.loginfo("Moved rollback #" .. id .. ": '" .. dest .. "' -> '" .. f .. "'")
end
end
end
return allowoverwrite
end
May 7, 2007
May 7, 2007
431
local function install_archive_entry(archive, ent, file, option)
May 4, 2007
May 4, 2007
432
local entdest = ent.filename
May 2, 2007
May 2, 2007
433
434
if entdest == nil then return end -- probably can't happen...
May 1, 2007
May 1, 2007
435
436
437
-- Set destination in native filesystem. May be default or explicit.
local dest = file.destination
if dest == nil then
May 2, 2007
May 2, 2007
438
439
440
dest = entdest
else
dest = dest .. "/" .. entdest
May 1, 2007
May 1, 2007
441
442
end
May 18, 2007
May 18, 2007
443
444
local perms = file.permissions -- may be nil
May 1, 2007
May 1, 2007
445
if file.filter ~= nil then
May 18, 2007
May 18, 2007
446
447
448
449
450
local filterperms
dest, filterperms = file.filter(dest)
if filterperms ~= nil then
perms = filterperms
end
May 1, 2007
May 1, 2007
451
452
453
454
455
end
if dest ~= nil then -- Only install if file wasn't filtered out
dest = MojoSetup.destination .. "/" .. dest
if permit_write(dest, ent, file) then
May 27, 2007
May 27, 2007
456
install_parent_dirs(dest, option)
May 1, 2007
May 1, 2007
457
if ent.type == "file" then
Jan 13, 2008
Jan 13, 2008
458
459
local desc = option.description
install_file_from_archive(dest, archive, perms, desc, option)
May 1, 2007
May 1, 2007
460
elseif ent.type == "dir" then
May 27, 2007
May 27, 2007
461
install_directory(dest, perms, option)
May 1, 2007
May 1, 2007
462
elseif ent.type == "symlink" then
May 27, 2007
May 27, 2007
463
install_symlink(dest, ent.linkdest, option)
May 1, 2007
May 1, 2007
464
465
466
467
468
469
470
471
472
else -- !!! FIXME: device nodes, etc...
-- !!! FIXME: should this be fatal?
MojoSetup.fatal(_("Unknown file type in archive"))
end
end
end
end
May 2, 2007
May 2, 2007
473
local function install_archive(archive, file, option)
May 4, 2007
May 4, 2007
474
if not MojoSetup.archive.enumerate(archive) then
Jan 15, 2008
Jan 15, 2008
475
MojoSetup.fatal(_("Couldn't enumerate archive"))
May 1, 2007
May 1, 2007
476
477
end
May 3, 2007
May 3, 2007
478
local isbase = (archive == MojoSetup.archive.base)
May 2, 2007
May 2, 2007
479
480
481
local single_match = true
local wildcards = file.wildcards
May 3, 2007
May 3, 2007
482
483
484
485
486
-- 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
May 2, 2007
May 2, 2007
487
488
489
if type(wildcards) == "string" then
wildcards = { wildcards }
end
May 3, 2007
May 3, 2007
490
491
492
493
494
495
496
497
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
May 2, 2007
May 2, 2007
498
499
500
501
end
end
end
May 1, 2007
May 1, 2007
502
503
local ent = MojoSetup.archive.enumnext(archive)
while ent ~= nil do
May 2, 2007
May 2, 2007
504
505
506
-- If inside GBaseArchive (no URL lead in string), then we
-- want to clip to data/ directory...
if isbase then
May 1, 2007
May 1, 2007
507
508
509
510
511
512
local count
ent.filename, count = string.gsub(ent.filename, "^data/", "", 1)
if count == 0 then
ent.filename = nil
end
end
May 2, 2007
May 2, 2007
513
May 1, 2007
May 1, 2007
514
-- See if we should install this file...
May 3, 2007
May 3, 2007
515
if (ent.filename ~= nil) and (ent.filename ~= "") then
May 2, 2007
May 2, 2007
516
517
518
519
520
521
522
523
524
525
526
527
528
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
May 7, 2007
May 7, 2007
529
install_archive_entry(archive, ent, file, option)
May 2, 2007
May 2, 2007
530
531
if single_match then
break -- no sense in iterating further if we're done.
May 1, 2007
May 1, 2007
532
533
534
535
536
537
538
end
end
end
-- and check the next entry in the archive...
ent = MojoSetup.archive.enumnext(archive)
end
May 2, 2007
May 2, 2007
539
540
end
May 1, 2007
May 1, 2007
541
May 2, 2007
May 2, 2007
542
543
544
545
546
547
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.
May 3, 2007
May 3, 2007
548
549
550
551
552
local function create_basepath_archive(path)
local archive = MojoSetup.archive.fromdir(path)
if archive == nil then
archive = MojoSetup.archive.fromfile(path)
Jan 14, 2008
Jan 14, 2008
553
if archive == nil then
Jan 15, 2008
Jan 15, 2008
554
MojoSetup.fatal(_("Couldn't open archive"))
May 2, 2007
May 2, 2007
555
end
May 3, 2007
May 3, 2007
556
557
558
559
560
561
562
563
564
565
566
567
568
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 = ""
May 9, 2007
May 9, 2007
569
local paths = split_path(basepath)
May 3, 2007
May 3, 2007
570
571
572
573
for i,v in ipairs(paths) do
local knowngood = path
path = path .. "/" .. v
if not MojoSetup.platform.exists(path) then
Jan 14, 2008
Jan 14, 2008
574
if knowngood == "" then
Jan 12, 2008
Jan 12, 2008
575
MojoSetup.fatal(_("Archive not found"))
May 2, 2007
May 2, 2007
576
end
May 3, 2007
May 3, 2007
577
578
579
580
581
582
583
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
May 2, 2007
May 2, 2007
584
585
end
end
May 3, 2007
May 3, 2007
586
587
588
-- wait, the whole thing exists now? Did this just move in?
install_basepath(basepath, file, option) -- try again, I guess...
May 1, 2007
May 1, 2007
589
590
591
592
end
end
May 7, 2007
May 7, 2007
593
594
595
local function set_destination(dest)
MojoSetup.loginfo("Install dest: '" .. dest .. "'")
MojoSetup.destination = dest
Jan 16, 2008
Jan 16, 2008
596
597
598
MojoSetup.metadatadir = MojoSetup.destination .. "/.mojosetup"
MojoSetup.manifestdir = MojoSetup.metadatadir .. "/manifest"
MojoSetup.scratchdir = MojoSetup.metadatadir .. "/tmp"
May 7, 2007
May 7, 2007
599
600
601
602
603
MojoSetup.rollbackdir = MojoSetup.scratchdir .. "/rollbacks"
MojoSetup.downloaddir = MojoSetup.scratchdir .. "/downloads"
end
May 10, 2007
May 10, 2007
604
605
606
607
608
609
610
611
612
613
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
Jan 13, 2008
Jan 13, 2008
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
-- 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
-- plain text 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()
local install = MojoSetup.install
local updateurl = install.updateurl
if updateurl ~= nil then
updateurl = 'update_url="' .. updateurl .. '"'
else
updateurl = ''
end
local retval =
'<?xml version="1.0" encoding="UTF-8"?>\n' ..
'<product name="' .. MojoSetup.install.id .. '" desc="' ..
install.description .. '" xmlversion="1.6" root="' ..
MojoSetup.destination .. '" ' .. updateurl .. '>\n' ..
'\t<component name="Default" version="' .. install.version ..
'" default="yes">\n'
local destlen = string.len(MojoSetup.destination) + 2
for option,items in pairs(MojoSetup.manifest) do
local desc = option
if type(desc) == "table" then -- "meta" etc, or an option table.
desc = option.description
end
retval = retval .. '\t\t<option name="' .. desc .. '">\n'
for i,item in ipairs(items) do
local type = item.type
if type == "dir" then
type = "directory"
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
if path ~= nil then
path = string.sub(path, destlen) -- make it relative.
end
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
local function build_lua_manifest()
local indentation = 0 -- upvalue for serialize()
local function serialize(obj)
local retval = ''
indentation = indentation + 1
local objtype = type(obj)
if objtype == "number" then
retval = tostring(obj)
elseif objtype == "string" then
retval = string.format("%q", obj)
elseif objtype == "table" then
retval = "{\n"
local tab = string.rep("\t", indentation)
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
retval = retval .. tab .. key .. " = " .. serialize(v) .. ",\n"
end
retval = retval .. string.rep("\t", indentation-1) .. "}"
else
MojoSetup.fatal(_("BUG: Unhandled data type"))
end
indentation = indentation - 1
return retval
end
local man = {}
for option,items in pairs(MojoSetup.manifest) do
local desc = option
if type(desc) == "table" then -- "meta" etc, or an option table.
desc = option.description
end
man[desc] = items;
end
local install = MojoSetup.install
return 'package = ' .. serialize {
id = install.id,
description = install.description,
root = MojoSetup.destination,
update_url = install.updateurl,
version = install.version,
manifest = man,
} .. "\n\n"
end
local function build_txt_manifest()
local retval = ''
local destlen = string.len(MojoSetup.destination) + 2
local files = flatten_manifest()
for i,item in ipairs(files) do
local path = item
if path ~= nil then
path = string.sub(path, destlen) -- make it relative.
retval = retval .. path .. "\n"
end
end
return retval
end
local function install_manifests()
-- 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 key = ".mojosetup_metadata."
-- 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!
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"
if MojoSetup.manifest[key] == nil then
MojoSetup.manifest[key] = {}
end
local manifest = MojoSetup.manifest[key]
manifest[#manifest+1] = { type = "file", path = lua_fname, mode = perms }
manifest[#manifest+1] = { type = "file", path = xml_fname, mode = perms }
manifest[#manifest+1] = { type = "file", path = txt_fname, mode = perms }
-- These are probably all duplicated effort, but just in case...
install_parent_dirs(lua_fname, key)
install_parent_dirs(xml_fname, key)
install_parent_dirs(txt_fname, key)
-- now build these things...
local desc = _("Metadata")
install_file_from_string(lua_fname, build_lua_manifest(), perms, desc, nil)
install_file_from_string(xml_fname, build_xml_manifest(), perms, desc, nil)
install_file_from_string(txt_fname, build_txt_manifest(), perms, desc, nil)
end
Dec 20, 2006
Dec 20, 2006
799
local function do_install(install)
May 17, 2007
May 17, 2007
800
MojoSetup.forceoverwrite = nil
May 7, 2007
May 7, 2007
801
802
803
804
MojoSetup.written = 0
MojoSetup.totalwrite = 0
MojoSetup.downloaded = 0
MojoSetup.totaldownload = 0
May 27, 2007
May 27, 2007
805
MojoSetup.install = install
May 7, 2007
May 7, 2007
806
Apr 22, 2007
Apr 22, 2007
807
808
809
810
811
-- !!! 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...
May 10, 2007
May 10, 2007
812
813
814
815
if install.options ~= nil and install.optiongroups ~= nil then
MojoSetup.fatal(_("Config bug: no options"))
end
Apr 22, 2007
Apr 22, 2007
816
817
818
819
820
821
822
823
824
825
826
827
-- 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
MojoSetup.fatal(_("Config bug: duplicate media id"))
end
MojoSetup.media[v.id] = v
end
end
Dec 20, 2006
Dec 20, 2006
828
829
830
831
832
833
834
835
836
837
838
839
-- Build a bunch of functions into a linear array...this lets us move
-- back and forth between stages of the install with customized functions
-- for each bit that have their own unique params embedded as upvalues.
-- So if there are three EULAs to accept, we'll call three variations of
-- the EULA function with three different tables that appear as local
-- variables, and the driver that calls this function will be able to
-- skip back and forth based on user input. This is a cool Lua thing.
local stages = {}
-- First stage: Make sure installer can run. Always fails or steps forward.
if install.precheck ~= nil then
stages[#stages+1] = function ()
May 10, 2007
May 10, 2007
840
run_config_defined_hook(install.precheck, install)
May 10, 2007
May 10, 2007
841
return 1
Dec 20, 2006
Dec 20, 2006
842
843
844
end
end
Jul 2, 2007
Jul 2, 2007
845
846
847
-- Next stage: accept all global EULAs. Rejection of any EULA is considered
-- fatal. These are global EULAs for the install, per-option EULAs come
-- later.
May 12, 2007
May 12, 2007
848
849
850
851
852
853
854
855
856
if install.eulas ~= nil then
for k,eula in pairs(install.eulas) do
local desc = eula.description
local fname = "data/" .. eula.source
-- (desc) and (fname) become an upvalues in this function.
stages[#stages+1] = function (thisstage, maxstage)
local retval = MojoSetup.gui.readme(desc,fname,thisstage,maxstage)
if retval == 1 then
Jul 2, 2007
Jul 2, 2007
857
if not MojoSetup.promptyn(desc, _("Accept this license?"), false) then
May 12, 2007
May 12, 2007
858
859
MojoSetup.fatal(_("You must accept the license before you may install"))
end
May 10, 2007
May 10, 2007
860
end
May 12, 2007
May 12, 2007
861
return retval
Dec 20, 2006
Dec 20, 2006
862
863
864
865
end
end
end
Jul 2, 2007
Jul 2, 2007
866
867
868
869
870
871
872
873
874
875
876
877
878
-- Next stage: show any READMEs.
if install.readmes ~= nil then
for k,readme in pairs(install.readmes) do
local desc = readme.description
-- !!! FIXME: pull from archive?
local fname = "data/" .. readme.source
-- (desc) and (fname) become upvalues in this function.
stages[#stages+1] = function(thisstage, maxstage)
return MojoSetup.gui.readme(desc, fname, thisstage, maxstage)
end
end
end
Dec 28, 2006
Dec 28, 2006
879
880
881
882
-- Next stage: let user choose install destination.
-- The config file can force a destination if it has a really good reason
-- (system drivers that have to go in a specific place, for example),
-- but really really shouldn't in 99% of the cases.
May 7, 2007
May 7, 2007
883
884
if install.destination ~= nil then
set_destination(install.destination)
Apr 22, 2007
Apr 22, 2007
885
else
May 10, 2007
May 10, 2007
886
local recommend = nil
May 16, 2007
May 16, 2007
887
888
889
890
891
892
local recommended_cfg = install.recommended_destinations
if recommended_cfg ~= nil then
if type(recommended_cfg) == "string" then
recommended_cfg = { recommended_cfg }
end
May 10, 2007
May 10, 2007
893
recommend = {}
May 16, 2007
May 16, 2007
894
for i,v in ipairs(recommended_cfg) do
May 10, 2007
May 10, 2007
895
896
897
898
if MojoSetup.platform.isdir(v) then
if MojoSetup.platform.writable(v) then
recommend[#recommend+1] = v .. "/" .. install.id
end
May 10, 2007
May 10, 2007
899
900
end
end
May 10, 2007
May 10, 2007
901
902
903
904
if #recommend == 0 then
recommend = nil
end
May 10, 2007
May 10, 2007
905
906
end
Dec 28, 2006
Dec 28, 2006
907
908
-- (recommend) becomes an upvalue in this function.
stages[#stages+1] = function(thisstage, maxstage)
May 10, 2007
May 10, 2007
909
910
911
912
local rc, dst
rc, dst = MojoSetup.gui.destination(recommend, thisstage, maxstage)
if rc == 1 then
set_destination(dst)
Dec 28, 2006
Dec 28, 2006
913
end
May 10, 2007
May 10, 2007
914
return rc
Dec 28, 2006
Dec 28, 2006
915
916
917
end
end
Dec 20, 2006
Dec 20, 2006
918
-- Next stage: let user choose install options.
Jul 2, 2007
Jul 2, 2007
919
920
921
922
923
924
-- This may not produce a GUI stage if it decides that all options
-- are either required or disabled.
-- (install) becomes an upvalue in this function.
stages[#stages+1] = function(thisstage, maxstage)
-- This does some complex stuff with a hierarchy of tables in C.
return MojoSetup.gui.options(install, thisstage, maxstage)
Dec 27, 2006
Dec 27, 2006
925
end
Dec 20, 2006
Dec 20, 2006
926
Apr 22, 2007
Apr 22, 2007
927
Jul 2, 2007
Jul 2, 2007
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
-- Next stage: accept all option-specific EULAs.
-- Rejection of any EULA will put you back one stage (usually to the
-- install options), but if there is no previous stage, this becomes
-- fatal.
-- This may not produce a GUI stage if there are no selected options with
-- EULAs to accept.
stages[#stages+1] = function(thisstage, maxstage)
local option_eulas = {}
local function find_option_eulas(opts)
local options = opts['options']
if options ~= nil then
for k,v in pairs(options) do
if v.value and v.eulas then
for ek,ev in pairs(v.eulas) do
option_eulas[#option_eulas+1] = ev
end
find_option_eulas(v)
end
end
end
local optiongroups = opts['optiongroups']
if optiongroups ~= nil then
for k,v in pairs(optiongroups) do
if not v.disabled then
find_option_eulas(v)
end
end
end
end
find_option_eulas(install)
for k,eula in pairs(option_eulas) do
local desc = eula.description
local fname = "data/" .. eula.source
local retval = MojoSetup.gui.readme(desc,fname,thisstage,maxstage)
if retval == 1 then
if not MojoSetup.promptyn(desc, _("Accept this license?"), false) then
-- can't go back? We have to die here instead.
if thisstage == 1 then
MojoSetup.fatal(_("You must accept the license before you may install"))
else
retval = -1 -- just step back a stage.
end
end
end
if retval ~= 1 then
return retval
end
end
return 1 -- all licenses were agreed to. Go to the next stage.
end
Apr 22, 2007
Apr 22, 2007
985
986
987
988
-- Next stage: Make sure source list is sane.
-- This is not a GUI stage, it just needs to run between them.
-- This gets a little hairy.
stages[#stages+1] = function(thisstage, maxstage)
May 2, 2007
May 2, 2007
989
local function process_file(option, file)
Apr 22, 2007
Apr 22, 2007
990
-- !!! FIXME: what happens if a file shows up in multiple options?
May 2, 2007
May 2, 2007
991
992
993
994
995
996
997
998
999
1000
local src = file.source
local prot,host,path
if src ~= nil then -- no source, it's in GBaseArchive
prot,host,path = MojoSetup.spliturl(src)
end
if (src == nil) or (prot == "base://") then -- included content?
if MojoSetup.files.included == nil then
MojoSetup.files.included = {}
end
MojoSetup.files.included[file] = option