This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
create_update.c
617 lines (524 loc) · 18 KB
/
create_update.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*
* Software Updater - server side
*
* Copyright © 2012-2016 Intel Corporation.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2 or later of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Arjan van de Ven <arjan@linux.intel.com>
* Sebastien Boeuf <sebastien.boeuf@intel.com>
* Regis Merlino <regis.merlino@intel.com>
* Tom Keel <thomas.keel@intel.com>
* Tim Pepper <timothy.c.pepper@linux.intel.com>
*
*/
#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <glib.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
#include "swupd.h"
static void banner(void)
{
printf(PACKAGE_NAME " update creator version " PACKAGE_VERSION "\n");
printf(" Copyright (C) 2012-2016 Intel Corporation\n");
printf("\n");
}
static const struct option prog_opts[] = {
{ "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'v' },
{ "log-stdout", no_argument, 0, 'l' },
{ "osversion", required_argument, 0, 'o' },
{ "minversion", required_argument, 0, 'm' },
{ "format", required_argument, 0, 'F' },
{ "getformat", no_argument, 0, 'g' },
{ "statedir", required_argument, 0, 'S' },
{ 0, 0, 0, 0 }
};
static void print_help(const char *name)
{
printf("Usage:\n");
printf(" %s [OPTION...]\n\n", name);
printf("Help Options:\n");
printf(" -h, --help Show help options\n");
printf(" -v, --version Show software version\n");
printf("\n");
printf("Application Options:\n");
printf(" -l, --log-stdout Write log messages also to stdout\n");
printf(" -o, --osversion The OS version for which to create an update\n");
printf(" -m, --minversion Optional minimum file version to write into manifests per file\n");
printf(" -F, --format Format number for the update\n");
printf(" -g, --getformat Print current format string and exit\n");
printf(" -S, --statedir Optional directory to use for state [ default:=%s ]\n", SWUPD_SERVER_STATE_DIR);
printf("\n");
}
static bool parse_options(int argc, char **argv)
{
int opt;
while ((opt = getopt_long(argc, argv, "hvo:m:F:g:S:", prog_opts, NULL)) != -1) {
switch (opt) {
case '?':
case 'h':
print_help(argv[0]);
return false;
case 'l':
init_log_stdout();
break;
case 'v':
banner();
return false;
case 'o':
// expect positive integer
newversion = strtoull(optarg, NULL, 10);
if (newversion <= 0) {
printf("-o | --osversion == %d, requires value > 0 \n", newversion);
return false;
}
break;
case 'm':
minversion = strtoull(optarg, NULL, 10);
if (minversion < 0) {
printf("-m | --minversion == %d, require value >= 0 \n", minversion);
return false;
}
break;
case 'F':
if (!optarg || !set_format(optarg)) {
printf("Invalid --format argument\n\n");
return false;
}
break;
case 'S':
if (!optarg || !set_state_dir(optarg)) {
printf("Invalid --statedir argument ''%s'\n\n", optarg);
return false;
}
break;
case 'g':
if (format == 0) {
printf("No format specified\n");
} else {
printf("%llu\n", format);
free_globals();
}
exit(0);
}
}
if (!init_globals()) {
return false;
}
return true;
}
static void populate_dirs(int version)
{
char *newversiondir;
char *newversiondircontent = NULL;
string_or_die(&newversiondir, "%s/%d", image_dir, version);
if ((access(newversiondir, F_OK | R_OK) != 0) && (version == 0)) {
char *latestpath = NULL;
string_or_die(&latestpath, "%s/LAST_VER", image_dir);
printf("** %s does not exist... creating and populating\n", newversiondir);
if (mkdir(newversiondir, 0755) != 0) {
printf("Failed to create directory\n");
}
strncat(newversiondir, "/os-core", strlen("/os-core"));
if (mkdir(newversiondir, 0755) != 0) {
printf("Failed to create os-core subdirectory\n");
}
FILE *latestver;
latestver = fopen_exclusive(latestpath);
if (latestver == NULL) {
printf("Failed to create %s\n", latestpath);
free(latestpath);
return;
}
if (fwrite("0\n", 2, 1, latestver) != 1) {
LOG(NULL, "Failed to write latestver", "errno: %d", errno);
}
free(latestpath);
fclose(latestver);
}
/* groups don't exist in version 0 */
if (version != 0) {
char *group;
while (1) {
group = next_group();
if (!group) {
break;
}
string_or_die(&newversiondir, "%s/%d/%s", image_dir, version, group);
string_or_die(&newversiondircontent, "%s/%d/%s.content.txt", image_dir, version, group);
/* Create the bundle directory(s) as needed */
if (access(newversiondir, F_OK | R_OK) != 0 &&
access(newversiondircontent, F_OK | R_OK) != 0) {
printf("%s does not exist...creating\n", group);
if (mkdir(newversiondir, 0755) != 0) {
printf("Failed to create %s subdirectory\n", group);
}
}
}
}
free(newversiondir);
free(newversiondircontent);
}
static int check_build_env(void)
{
char *temp_dir = NULL;
string_or_die(&temp_dir, "%s/%s", state_dir, "/temp");
if (access(temp_dir, F_OK | R_OK) != 0) {
LOG(NULL, "%s", "does not exist...creating directory", temp_dir);
if (mkdir(temp_dir, 0755) != 0) {
printf("Failed to create build directory, EXITING\n");
free(temp_dir);
return -errno;
}
}
free(temp_dir);
return 0;
}
static int check_group_file(void)
{
int ret = -1;
// Ensure the os-core group is defined
while (1) {
char *group = next_group();
if (!group) {
break;
}
if (strcmp(group, "os-core") == 0) {
ret = 0;
break;
}
}
return ret;
}
static bool both_ghosted(struct file *file1, struct file *file2)
{
return (file1->is_ghosted && file2->is_ghosted);
}
static bool both_deleted(struct file *file1, struct file *file2)
{
return (file1->is_deleted && file2->is_deleted);
}
int main(int argc, char **argv)
{
struct manifest *new_core = NULL;
struct manifest *old_core = NULL;
struct manifest *new_MoM = NULL;
struct manifest *old_MoM = NULL;
struct manifest *old_full = NULL;
struct manifest *new_full = NULL;
GHashTable *new_manifests = g_hash_table_new(g_str_hash, g_str_equal);
GHashTable *old_manifests = g_hash_table_new(g_str_hash, g_str_equal);
GList *manifests_last_versions_list = NULL;
int newfiles = 0;
int old_deleted = 0;
int old_ghosted = 0;
struct timeval current_time;
struct timeval previous_time;
int exit_status = EXIT_FAILURE;
int ret;
char *file_path = NULL;
/* keep valgrind working well */
setenv("G_SLICE", "always-malloc", 0);
if (!setlocale(LC_ALL, "")) {
fprintf(stderr, "%s: setlocale() failed\n", argv[0]);
return EXIT_FAILURE;
}
if (!parse_options(argc, argv)) {
free_globals();
return EXIT_FAILURE;
}
banner();
check_root();
ret = check_build_env();
if (ret != 0) {
printf("Failed to setup build environment: ERRNO = %i\n", ret);
goto exit;
}
string_or_die(&file_path, "%s/server.ini", state_dir);
if (!read_configuration_file(file_path)) {
printf("Failed to read %s configuration file!\n", state_dir);
free(file_path);
goto exit;
}
free(file_path);
string_or_die(&file_path, "%s/groups.ini", state_dir);
read_group_file(file_path);
free(file_path);
ret = check_group_file();
if (ret != 0) {
printf("os-core bundle is not listed in groups.ini, this is required.\n");
goto exit;
}
read_current_version("LAST_VER");
printf("Last processed version is %i\n", current_version);
populate_dirs(newversion);
printf("Next version is %i \n", newversion);
init_log("swupd-create-update", "", current_version, newversion);
gettimeofday(&previous_time, NULL);
printf("Entering phase 1: full chroot and manifest preparation\n");
/* Step 1: Create "full" chroot (a union of _everything_ in the build)
* Presume an external entity created chroots for all bundles. */
printf("Syncing bundle chroots with full chroot\n");
chroot_create_full(newversion);
print_elapsed_time("full chroot creation", &previous_time, ¤t_time);
printf("Calculating full manifest (this is expensive/slow)\n");
old_full = manifest_from_file(current_version, "full");
new_full = full_manifest_from_directory(newversion);
apply_heuristics(old_full);
apply_heuristics(new_full);
match_manifests(old_full, new_full);
if (old_full->format < new_full->format) {
old_deleted = remove_deprecated_files(old_full, new_full, both_deleted);
} else {
old_deleted = 0;
}
if (old_deleted > 0) {
LOG(NULL, "", "Old deleted files (%d) removed from full manifest", old_deleted);
printf("Old deleted files (%d) removed from full manifest\n", old_deleted);
}
old_ghosted = remove_deprecated_files(old_full, new_full, both_ghosted);
if (old_ghosted > 0) {
LOG(NULL, "", "Old ghosted files (%d) removed from full manifest", old_ghosted);
printf("Old ghosted files (%d) removed from full manifest", old_ghosted);
}
apply_heuristics(new_full);
print_elapsed_time("full manifest creation", &previous_time, ¤t_time);
printf("Entering phase 2: Doing the os-core bundle\n");
/* Phase 2 : the os-core set */
/* Step 2: Make a manifest for the os-core set */
old_MoM = manifest_from_file(current_version, "MoM");
/* Verify that the new format is not older than the previous format. It
* never makes sense to decrease the format number for the next build. */
if (format < old_MoM->format) {
LOG(NULL, "", "Current format (%llu) must be greater than or equal to previous format (%llu). Exiting",
format, old_MoM->format);
printf("Current format (%llu) must be greater than or equal to previous format (%llu). Exiting\n",
format, old_MoM->format);
goto exit;
}
/* Detect a format bump and add the "update" action to the manifest
* "actions:" field */
GList *actions = NULL;
if (format > old_MoM->format) {
actions = g_list_prepend(actions, "update");
}
new_MoM = alloc_manifest(newversion, "MoM", actions);
old_core = manifest_from_file(manifest_subversion(old_MoM, "os-core"), "os-core");
new_core = sub_manifest_from_directory("os-core", newversion);
add_component_hashes_to_manifest(new_core, new_full);
new_core->prevversion = old_core->version;
apply_heuristics(old_core);
apply_heuristics(new_core);
new_MoM->prevversion = old_MoM->version;
manifests_last_versions_list = get_last_versions_list(newversion, SWUPD_NUM_MANIFEST_DELTAS);
/* Step 3: Compare to the previous core manifest... */
if (match_manifests(old_core, new_core) == 0) {
LOG(NULL, "Core component has not changed, no new manifest", "");
printf("Core component has not changed, no new manifest\n");
/* Step 3b: ... and if nothing is new, we stay at the old version */
new_core->version = old_core->version;
} else {
apply_heuristics(new_core);
/* Step 3c: ... else save the manifest */
type_change_detection(new_core);
#ifdef RENAMES
/* Detect renamed files specifically for os-core */
rename_detection(new_core);
#endif
if (old_core->format < new_core->format) {
old_deleted = remove_deprecated_files(old_core, new_core, both_deleted);
} else {
old_deleted = 0;
}
old_ghosted = remove_deprecated_files(old_core, new_core, both_ghosted);
sort_manifest_by_version(new_core); /* sorts by filename */
/* clean up orphaned renames by marking them as deleted */
clean_renames(new_core);
newfiles = prune_manifest(new_core);
if (newfiles <= 0) {
LOG(NULL, "", "Core component has not changed (after pruning), exiting");
printf("Core component has not changed (after pruning), exiting\n");
goto exit;
}
LOG(NULL, "", "Core component has changes (%d new, %d deleted, %d ghosted), writing out new manifest", newfiles, old_deleted, old_ghosted);
printf("Core component has changes (%d new, %d deleted, %d ghosted), writing out new manifest\n", newfiles, old_deleted, old_ghosted);
if (write_manifest(new_core) != 0) {
LOG(NULL, "", "Core component manifest write failed");
printf("Core component manifest write failed\n");
goto exit;
}
}
nest_manifest(new_MoM, new_core);
/* Phase 3: the functional bundles */
printf("Entering phase 3: The bundles\n");
while (1) {
char *group = next_group();
if (!group) {
break;
}
(void)g_hash_table_insert(new_manifests, group, sub_manifest_from_directory(group, newversion));
(void)g_hash_table_insert(old_manifests, group, manifest_from_file(manifest_subversion(old_MoM, group), group));
}
while (1) {
GList *manifest_includes = NULL;
GList *name_includes;
char *group = next_group();
struct manifest *manifest;
struct manifest *current = NULL;
if (!group) {
break;
}
manifest = g_hash_table_lookup(new_manifests, group);
name_includes = manifest->includes;
while (name_includes) {
char *name = name_includes->data;
name_includes = g_list_next(name_includes);
current = g_hash_table_lookup(new_manifests, name);
// Avoid adding duplicate includes to the list
if (g_list_find(manifest_includes, current) == NULL) {
manifest_includes = g_list_prepend(manifest_includes, current);
}
}
manifest->includes = manifest_includes;
manifest_includes = NULL;
manifest = g_hash_table_lookup(old_manifests, group);
name_includes = manifest->includes;
while (name_includes) {
char *name = name_includes->data;
name_includes = g_list_next(name_includes);
current = g_hash_table_lookup(old_manifests, name);
// Avoid adding duplicate includes to the list
if (g_list_find(manifest_includes, current) == NULL) {
manifest_includes = g_list_prepend(manifest_includes, current);
}
}
manifest->includes = manifest_includes;
}
while (1) {
char *group = next_group();
struct manifest *oldm;
struct manifest *newm;
if (!group) {
break;
}
if (strcmp(group, "os-core") == 0) {
continue;
}
printf("Processing bundle %s\n", group);
/* Step 4: Make a manifest for this functonal group */
oldm = g_hash_table_lookup(old_manifests, group);
newm = g_hash_table_lookup(new_manifests, group);
add_component_hashes_to_manifest(newm, new_full);
apply_heuristics(oldm);
apply_heuristics(newm);
newm->prevversion = oldm->version;
/* add os-core as an included manifest */
if (!manifest_includes(oldm, "os-core")) {
oldm->includes = g_list_prepend(oldm->includes, old_core);
}
if (!manifest_includes(newm, "os-core")) {
newm->includes = g_list_prepend(newm->includes, new_core);
}
/* Step 5: Subtract the core files from the manifest */
subtract_manifests_frontend(oldm, oldm);
subtract_manifests_frontend(newm, newm);
/* Step 6: Compare manifest to the previous version... */
if (match_manifests(oldm, newm) == 0 && !changed_includes(oldm, newm)) {
LOG(NULL, "", "%s components have not changed, no new manifest", group);
printf("%s components have not changed, no new manifest\n", group);
/* Step 6a: if nothing changed, stay at the old version */
newm->version = oldm->version;
} else {
apply_heuristics(newm);
#ifdef RENAMES
/* Detect renamed files specifically for this bundle */
rename_detection(newm);
#endif
/* Step 6b: otherwise, write out the manifest */
if (oldm->format < newm->format) {
old_deleted = remove_deprecated_files(oldm, newm, both_deleted);
} else {
old_deleted = 0;
}
old_ghosted = remove_deprecated_files(oldm, newm, both_ghosted);
sort_manifest_by_version(newm);
type_change_detection(newm);
/* clean up orphaned renames by marking them as deleted */
clean_renames(newm);
newfiles = prune_manifest(newm);
if (newfiles > 0 || old_deleted > 0 || old_ghosted > 0 || changed_includes(oldm, newm)) {
LOG(NULL, "", "%s component has changes (%d new, %d deleted, %d ghosted), writing out new manifest", group, newfiles, old_deleted, old_ghosted);
printf("%s component has changes (%d new, %d deleted, %d ghosted), writing out new manifest\n", group, newfiles, old_deleted, old_ghosted);
if (write_manifest(newm) != 0) {
LOG(NULL, "", "%s component manifest write failed", group);
printf("%s component manifest write failed\n", group);
goto exit;
}
} else {
LOG(NULL, "", "%s component has not changed (after pruning), no new manifest", group);
printf("%s component has not changed (after pruning), no new manifest\n", group);
newm->version = oldm->version;
}
}
nest_manifest(new_MoM, newm);
}
print_elapsed_time("bundle manifest creation", &previous_time, ¤t_time);
printf("Entering phase 4: completion manifests \n");
/* Phase 4 : manifest completion */
/* Step 7: Create the meta-manifest */
sort_manifest_by_version(new_MoM);
if (write_manifest(new_MoM) != 0) {
LOG(NULL, "Failed to write new MoM", "");
printf("Failed to write new MoM\n");
goto exit;
}
print_elapsed_time("MoM creation", &previous_time, ¤t_time);
printf("Entering phase 5: creating download content\n");
/* Phase 5: wrapping up */
//TODO: should be fixup_versions() and preceed all write_manifest() calls
maximize_to_full(new_MoM, new_full);
sort_manifest_by_version(new_full);
/* clean up orphaned renames by marking them as deleted */
clean_renames(new_full);
prune_manifest(new_full);
if (write_manifest(new_full) != 0) {
goto exit;
}
/* Step 8: Prepare delta directory */
prepare_delta_dir(new_full);
print_elapsed_time("finalize update", &previous_time, ¤t_time);
exit_status = EXIT_SUCCESS;
exit:
if (exit_status == EXIT_SUCCESS) {
write_cookiecrumbs_to_download_area(newversion);
//write_new_version("LAST_VER", newversion);
}
release_configuration_data();
release_group_file();
g_list_free(manifests_last_versions_list);
close_log(newversion, exit_status);
printf("Update creation %s\n", exit_status == EXIT_SUCCESS ? "complete" : "failed");
free_globals();
return exit_status;
}