Skip to content

Latest commit

 

History

History
2115 lines (1784 loc) · 72.4 KB

mojosetup_mainline.lua

File metadata and controls

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