forked from clearlinux/swupd-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.c
910 lines (793 loc) · 23.1 KB
/
search.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
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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
/*
* Software Updater - client 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 <[email protected]>
* Timothy C. Pepper <[email protected]>
* Brad Peters <[email protected]>
*
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "config.h"
#include "swupd.h"
char *search_string;
bool display_files = false; /* Just display all files found in Manifest set */
bool csv_format = false;
bool init = false;
static char search_type = '0';
static char scope = '0';
static int num_results = INT_MAX;
/* bundle_result contains the information to print along with
* the internal relevancy score used to sort the output */
struct bundle_result {
char bundle_name[BUNDLE_NAME_MAXLEN];
long topsize;
long size;
double score;
struct list *files;
struct list *includes;
bool is_tracked;
bool seen;
};
/* per-file scoring struct */
struct file_result {
char *filename;
double score;
};
static struct list *results;
/* add a bundle_result to the results list */
static void add_bundle_file_result(char *bundlename, char *filename, double score)
{
struct bundle_result *bundle = NULL;
struct file_result *file;
struct list *ptr;
ptr = results;
while (ptr) {
struct bundle_result *item;
item = ptr->data;
if (strcmp(item->bundle_name, bundlename) == 0) {
bundle = item;
break;
}
ptr = ptr->next;
}
if (!bundle) {
bundle = calloc(sizeof(struct bundle_result), 1);
ON_NULL_ABORT(bundle);
results = list_append_data(results, bundle);
strncpy(bundle->bundle_name, bundlename, BUNDLE_NAME_MAXLEN - 1);
/* record if the bundle is tracked on the system */
bundle->is_tracked = is_tracked_bundle(bundlename);
}
file = calloc(sizeof(struct file_result), 1);
ON_NULL_ABORT(file);
file->filename = strdup_or_die(filename);
bundle->files = list_append_data(bundle->files, file);
file->score = score;
bundle->score += score;
}
static int bundle_cmp(const void *a, const void *b)
{
const struct bundle_result *A, *B;
A = (struct bundle_result *)a;
B = (struct bundle_result *)b;
if (A->score > B->score) {
return -1;
} else if (A->score < B->score) {
return 1;
} else {
return 0;
}
}
static int bundle_size_cmp(const void *a, const void *b)
{
const struct bundle_result *A, *B;
A = (struct bundle_result *)a;
B = (struct bundle_result *)b;
// smaller before larger
if (A->size < B->size) {
return -1;
} else if (A->size > B->size) {
return 1;
} else {
return 0;
}
}
static int file_cmp(const void *a, const void *b)
{
const struct file_result *A, *B;
A = (struct file_result *)a;
B = (struct file_result *)b;
if (A->score > B->score) {
return -1;
} else if (A->score < B->score) {
return 1;
} else {
return 0;
}
}
static void sort_results(void)
{
struct list *ptr;
struct bundle_result *b;
results = list_sort(results, bundle_cmp);
ptr = results;
while (ptr) {
b = ptr->data;
ptr = ptr->next;
b->files = list_sort(b->files, file_cmp);
}
}
static void file_result_free(void *data)
{
struct file_result *result = data;
free(result->filename);
free(result);
}
static void free_bundle_result_data(void *data)
{
struct bundle_result *br = (struct bundle_result *)data;
if (!br) {
return;
}
list_free_list_and_data(br->files, file_result_free);
list_free_list_and_data(br->includes, free);
free(br);
}
/* unmark all bundle_results in bl as seen */
static void unsee_bundle_results(struct list *bl)
{
struct bundle_result *br;
struct list *ptr = list_head(bl);
while (ptr) {
br = ptr->data;
br->seen = false;
ptr = ptr->next;
}
}
/* recursively calculate size from a complete list of bundle_result structs */
static long calculate_size(char *bname, struct list *bundle_info, bool installed)
{
struct list *ptr;
struct bundle_result *bi;
long size = 0;
ptr = list_head(bundle_info);
while (ptr) {
bi = ptr->data;
ptr = ptr->next;
if (bi->seen || strcmp(bname, bi->bundle_name) != 0) {
continue;
}
/* If installed is true the initial call to this recursive
* function was for an installed bundle. In this case we want
* to calculate the contentsize of all included bundles (we
* know they are all installed already) so we can report the
* total installed size on the system.
*
* Otherwise only add the contentsize of bundles not already
* installed on the system. */
if (installed || !is_tracked_bundle(bname)) {
size += bi->topsize;
bi->seen = true;
}
/* add bundle sizes for includes as well */
struct list *ptr2;
ptr2 = list_head(bi->includes);
while (ptr2) {
char *inc = NULL;
inc = ptr2->data;
ptr2 = ptr2->next;
/* recursively add included sizes */
size += calculate_size(inc, bundle_info, installed);
}
}
return size;
}
static void apply_size_penalty(struct list *bundle_info)
{
struct bundle_result *b;
struct list *ptr;
ptr = list_head(results);
while (ptr) {
b = ptr->data;
ptr = ptr->next;
/* unsee all bundles before calculating size */
unsee_bundle_results(bundle_info);
/* calculate bundle size for recursive includes */
b->size = calculate_size(b->bundle_name, bundle_info, b->is_tracked);
/* Arbitrarily assign a score penalty based on how large the bundle is.
* This is set to negative 1/10th of the bundle size. */
b->score -= (0.01 * b->size);
}
}
static void print_csv_results()
{
struct bundle_result *b;
struct list *ptr;
ptr = list_head(results);
while (ptr) {
struct list *fptr;
struct file_result *f;
b = ptr->data;
ptr = ptr->next;
fptr = list_head(b->files);
while (fptr) {
f = fptr->data;
fptr = fptr->next;
printf("%s,%s\n", f->filename, b->bundle_name);
}
}
}
static void print_final_results(bool display_size)
{
struct bundle_result *b;
struct list *ptr;
int counter = 0;
ptr = list_head(results);
if (num_results != INT_MAX) {
printf("Displaying top %d file results per bundle\n\n", num_results);
}
while (ptr) {
struct list *ptr2;
struct file_result *f;
int counter2 = 0;
b = ptr->data;
ptr = ptr->next;
counter++;
/* do not print the size information when the scope is only one bundle ('o')
* because we did not load all bundles and therefore do not have include sizes
* for the result */
printf("Bundle %s\t%s", b->bundle_name, b->is_tracked ? "[installed]\t" : "");
if (display_size) {
printf("(%li MB%s)",
b->size / 1000 / 1000, /* convert from bytes->KB->MB */
b->is_tracked ? " on system" : " to install");
}
putchar('\n');
ptr2 = list_head(b->files);
while (ptr2 && counter2 < num_results) {
f = ptr2->data;
ptr2 = ptr2->next;
printf("\t%s\n", f->filename);
counter2++;
}
if (ptr2) {
printf("\tfile results truncated...\n");
}
printf("\n");
}
}
/* Supported default search paths */
static char *lib_paths[] = {
"/usr/lib",
NULL
};
static char *bin_paths[] = {
"/usr/bin/",
NULL
};
static void print_help(const char *name)
{
fprintf(stderr, "Usage:\n");
fprintf(stderr, " swupd %s [Options] 'search_term'\n", basename((char *)name));
fprintf(stderr, " 'search_term': A substring of a binary, library or filename (default)\n");
fprintf(stderr, " Return: Bundle name : filename matching search term\n\n");
fprintf(stderr, "Help Options:\n");
fprintf(stderr, " -h, --help Display this help\n");
fprintf(stderr, " -l, --library Search paths where libraries are located for a match\n");
fprintf(stderr, " -b, --binary Search paths where binaries are located for a match\n");
fprintf(stderr, " -s, --scope=[query type] 'b' or 'o' for first hit per (b)undle, or one hit total across the (o)s\n");
fprintf(stderr, " -t, --top=[NUM] Only display the top NUM results for each bundle\n");
fprintf(stderr, " -m, --csv Output all results in CSV format (machine-readable)\n");
fprintf(stderr, " -d, --display-files Output full file list, no search done\n");
fprintf(stderr, " -i, --init Download all manifests then return, no search done\n");
fprintf(stderr, " -n, --nosigcheck Do not attempt to enforce certificate or signature checking\n");
fprintf(stderr, " -I, --ignore-time Ignore system/certificate time when validating signature\n");
fprintf(stderr, " -u, --url=[URL] RFC-3986 encoded url for version string and content file downloads\n");
fprintf(stderr, " -c, --contenturl=[URL] RFC-3986 encoded url for content file downloads\n");
fprintf(stderr, " -v, --versionurl=[URL] RFC-3986 encoded url for version string download\n");
fprintf(stderr, " -P, --port=[port #] Port number to connect to at the url for version string and content file downloads\n");
fprintf(stderr, " -p, --path=[PATH...] Use [PATH...] as the path to verify (eg: a chroot or btrfs subvol\n");
fprintf(stderr, " -F, --format=[staging,1,2,etc.] the format suffix for version file downloads\n");
fprintf(stderr, " -S, --statedir Specify alternate swupd state directory\n");
fprintf(stderr, " -C, --certpath Specify alternate path to swupd certificates\n");
}
static const struct option prog_opts[] = {
{ "help", no_argument, 0, 'h' },
{ "url", required_argument, 0, 'u' },
{ "contenturl", required_argument, 0, 'c' },
{ "versionurl", required_argument, 0, 'v' },
{ "library", no_argument, 0, 'l' },
{ "binary", no_argument, 0, 'b' },
{ "scope", required_argument, 0, 's' },
{ "top", required_argument, 0, 't' },
{ "csv", no_argument, 0, 'm' },
{ "port", required_argument, 0, 'P' },
{ "path", required_argument, 0, 'p' },
{ "format", required_argument, 0, 'F' },
{ "init", no_argument, 0, 'i' },
{ "nosigcheck", no_argument, 0, 'n' },
{ "ignore-time", no_argument, 0, 'I' },
{ "display-files", no_argument, 0, 'd' },
{ "statedir", required_argument, 0, 'S' },
{ "certpath", required_argument, 0, 'C' },
{ 0, 0, 0, 0 }
};
static bool parse_options(int argc, char **argv)
{
int opt;
int err;
while ((opt = getopt_long(argc, argv, "hu:c:v:P:p:F:s:t:mlbinIdS:C:", prog_opts, NULL)) != -1) {
switch (opt) {
case '?':
case 'h':
print_help(argv[0]);
exit(0);
case 'u':
if (!optarg) {
fprintf(stderr, "error: invalid --url argument\n\n");
goto err;
}
set_version_url(optarg);
set_content_url(optarg);
break;
case 'c':
if (!optarg) {
fprintf(stderr, "Invalid --contenturl argument\n\n");
goto err;
}
set_content_url(optarg);
break;
case 'v':
if (!optarg) {
fprintf(stderr, "Invalid --versionurl argument\n\n");
goto err;
}
set_version_url(optarg);
break;
case 'P':
if (sscanf(optarg, "%ld", &update_server_port) != 1) {
fprintf(stderr, "Invalid --port argument\n\n");
goto err;
}
break;
case 'p': /* default empty path_prefix verifies the running OS */
if (!optarg || !set_path_prefix(optarg)) {
fprintf(stderr, "Invalid --path argument\n\n");
goto err;
}
break;
case 's':
if (!optarg || (strcmp(optarg, "b") && (strcmp(optarg, "o")))) {
fprintf(stderr, "Invalid --scope argument. Must be 'b' or 'o'\n\n");
goto err;
}
if (!strcmp(optarg, "b")) {
scope = 'b';
} else if (!strcmp(optarg, "o")) {
scope = 'o';
}
break;
case 't':
err = strtoi_err(optarg, NULL, &num_results);
if (err != 0) {
fprintf(stderr, "Invalid --top argument\n\n");
goto err;
}
break;
case 'm':
csv_format = true;
break;
case 'F':
if (!optarg || !set_format_string(optarg)) {
fprintf(stderr, "Invalid --format argument\n\n");
goto err;
}
break;
case 'S':
if (!optarg || !set_state_dir(optarg)) {
fprintf(stderr, "Invalid --statedir argument\n\n");
goto err;
}
break;
case 'l':
if (search_type != '0') {
fprintf(stderr, "Error, cannot specify multiple search types "
"(-l and -b are mutually exclusive)\n");
goto err;
}
search_type = 'l';
break;
case 'i':
init = true;
break;
case 'n':
sigcheck = false;
break;
case 'I':
timecheck = false;
break;
case 'b':
if (search_type != '0') {
fprintf(stderr, "Error, cannot specify multiple search types "
"(-l and -b are mutually exclusive)\n");
goto err;
}
search_type = 'b';
break;
case 'd':
display_files = true;
break;
case 'C':
if (!optarg) {
fprintf(stderr, "Invalid --certpath argument\n\n");
goto err;
}
set_cert_path(optarg);
break;
default:
fprintf(stderr, "Error: unrecognized option: -'%c',\n\n", opt);
goto err;
}
}
if ((optind == argc) && (!init) && (!display_files)) {
fprintf(stderr, "Error: Search term missing\n\n");
print_help(argv[0]);
return false;
}
if ((optind == argc - 1) && (display_files)) {
fprintf(stderr, "Error: Cannot supply a search term and -d, --display-files together\n");
return false;
}
search_string = argv[optind];
if (optind + 1 < argc) {
fprintf(stderr, "Error, only 1 search term supported at a time\n");
return false;
}
return true;
err:
print_help(argv[0]);
return false;
}
/* file_search()
* Attempt to match path and substring of filename. Base op for 'swupd search'
* Path must match exact case, filename is case insensitive
*/
static bool file_search(char *filename, char *path, char *search_term)
{
char *pos;
if (display_files) {
printf(" %s\n", filename);
return false;
}
if (path) {
pos = strstr(filename, path);
if (pos == NULL) {
return false;
}
}
if (path && search_term) {
/* match filename or substring of filename */
if (strcasestr(pos + strlen(path), search_term)) {
return true;
}
}
return false;
}
static double guess_score(char *bundle, char *file, char *search_term)
{
double multiplier = 1.0;
double score = 1.0;
char dirname[PATH_MAX];
char *bname;
// calculate basename
bname = basename(file);
/* The following score and multiplier transformations are experimental
* heuristics and should be updated over time based on results and
* feedback from swupd search usage */
/* dev bundles are less important */
if (strstr(bundle, "-dev")) {
multiplier /= 10;
}
/* files in /usr/bin are more important */
if (strcmp(dirname, "/usr/bin") == 0) {
score += 5;
}
/* files in /usr/include are not as important as other files */
if (strcmp(dirname, "/usr/include") == 0) {
multiplier /= 2;
}
/* if the search term is in the bundle name this is probably the bundle the
* user is looking for. Give it a slight bump. */
if (strstr(bundle, search_term)) {
score += 0.5;
}
/* slightly favor shorter filenames */
score += (2.0 / strlen(file));
/* if the search term matches the basename give it a substantial score bump */
if (strcmp(bname, search_term) == 0) {
multiplier *= 5;
}
return multiplier * score;
}
/* report_finds()
* Report out, respecting verbosity
*/
static void report_find(char *bundle, char *file, char *search_term)
{
double score;
score = guess_score(bundle, file, search_term);
add_bundle_file_result(bundle, file, score);
}
/* do_search()
* Description: Perform a lookup of the specified search string in all Clear manifests
* for the current os release.
*/
static void do_search(struct manifest *MoM, char search_type, char *search_term)
{
struct list *list;
struct list *sublist;
struct file *file;
struct file *subfile;
struct list *bundle_info = NULL;
struct manifest *subman = NULL;
int i;
bool done_with_bundle, done_with_search = false;
bool hit = false;
bool man_load_failures = false;
long hit_count = 0;
list = MoM->manifests;
while (list && !done_with_search) {
file = list->data;
list = list->next;
done_with_bundle = false;
/* Load sub-manifest */
subman = load_manifest(file->last_change, file, MoM, false);
if (!subman) {
fprintf(stderr, "Failed to load manifest %s\n", file->filename);
man_load_failures = true;
continue;
}
/* record contentsize and includes for install size calculation */
struct bundle_result *bundle = NULL;
bundle = calloc(sizeof(struct bundle_result), 1);
ON_NULL_ABORT(bundle);
/* copy relevant information over for future use */
strncpy(bundle->bundle_name, subman->component, BUNDLE_NAME_MAXLEN - 1);
bundle->topsize = subman->contentsize;
/* do a deep copy of the includes list */
bundle->includes = list_deep_clone_strs(subman->includes);
bundle_info = list_prepend_data(bundle_info, bundle);
if (display_files) {
/* Display bundle name. Marked up for pattern matchability */
fprintf(stderr, "--Bundle: %s--\n", file->filename);
}
/* Loop through sub-manifest, searching for files matching the desired pattern */
sublist = subman->files;
while (sublist && !done_with_bundle) {
subfile = sublist->data;
sublist = sublist->next;
if ((!subfile->is_file) && (!subfile->is_link)) {
continue;
}
if (display_files) {
/* Just display filename */
file_search(subfile->filename, NULL, NULL);
} else if (search_type == '0') {
/* Search for exact match, not path addition */
if (file_search(subfile->filename, "", search_term)) {
report_find(file->filename, subfile->filename, search_term);
hit = true;
}
} else if (search_type == 'l') {
/* Check each supported library path for a match */
for (i = 0; lib_paths[i] != NULL; i++) {
if (file_search(subfile->filename, lib_paths[i], search_term)) {
report_find(file->filename, subfile->filename, search_term);
hit = true;
}
}
} else if (search_type == 'b') {
/* Check each supported path for binaries */
for (i = 0; bin_paths[i] != NULL; i++) {
if (file_search(subfile->filename, bin_paths[i], search_term)) {
report_find(file->filename, subfile->filename, search_term);
hit = true;
}
}
} else {
fprintf(stderr, "Unrecognized search type. -b or -l supported\n");
done_with_search = true;
break;
}
/* Determine the level of completion we've reached */
if (hit) {
if (scope == 'b') {
done_with_bundle = true;
} else if (scope == 'o') {
done_with_bundle = true;
done_with_search = true;
}
hit_count++;
}
hit = false;
}
free_manifest(subman);
}
if (!hit_count) {
fprintf(stderr, "Search term not found.\n");
}
bool display_size = (scope != 'o' && !man_load_failures);
if (display_size) {
apply_size_penalty(bundle_info);
}
list_free_list_and_data(bundle_info, free_bundle_result_data);
if (num_results != INT_MAX) {
sort_results();
} else {
results = list_sort(results, bundle_size_cmp);
}
if (csv_format) {
print_csv_results();
} else {
print_final_results(display_size);
}
list_free_list_and_data(results, free_bundle_result_data);
}
static double query_total_download_size(struct list *list)
{
double ret;
double size_sum = 0;
struct file *file = NULL;
char *untard_file = NULL;
char *url = NULL;
while (list) {
file = list->data;
list = list->next;
string_or_die(&untard_file, "%s/%i/Manifest.%s", state_dir, file->last_change,
file->filename);
if (access(untard_file, F_OK) == -1) {
/* Does not exist client-side. Must download */
string_or_die(&url, "%s/%i/Manifest.%s.tar", content_url,
file->last_change, file->filename);
ret = swupd_curl_query_content_size(url);
free_string(&url);
if (ret != -1) {
/* Convert file size from bytes to MB */
ret = ret / 1000000;
size_sum += ret;
} else {
free_string(&untard_file);
return ret;
}
}
free_string(&untard_file);
}
return size_sum;
}
/* download_manifests()
* Description: To search Clear bundles for a particular entry, a complete set of
* manifests must be downloaded. This function does so, asynchronously, using
* the curl_multi interface */
static int download_manifests(struct manifest **MoM, struct list **subs)
{
struct list *list = NULL;
struct file *file = NULL;
char *tarfile, *untard_file, *url = NULL;
struct manifest *subMan = NULL;
int current_version;
int ret = 0;
double size;
bool did_download = false;
current_version = get_current_version(path_prefix);
if (current_version < 0) {
fprintf(stderr, "Error: Unable to determine current OS version\n");
return ECURRENT_VERSION;
}
*MoM = load_mom(current_version, false, false, NULL);
if (!(*MoM)) {
fprintf(stderr, "Cannot load official manifest MoM for version %i\n", current_version);
return EMOM_LOAD;
}
list = (*MoM)->manifests;
size = query_total_download_size(list);
if (size == -1) {
fprintf(stderr, "Downloading manifests. Expect a delay, up to 100MB may be downloaded\n");
} else if (size > 0) {
fprintf(stderr, "Downloading Clear Linux manifests\n");
fprintf(stderr, " %.2f MB total...\n\n", size);
}
while (list) {
file = list->data;
list = list->next;
create_and_append_subscription(subs, file->filename);
string_or_die(&untard_file, "%s/%i/Manifest.%s", state_dir, file->last_change,
file->filename);
string_or_die(&tarfile, "%s/%i/Manifest.%s.tar", state_dir, file->last_change,
file->filename);
if (access(untard_file, F_OK) == -1) {
/* Do download */
subMan = load_manifest(file->last_change, file, *MoM, false);
if (!subMan) {
fprintf(stderr, "Cannot load official manifest MoM for version %i\n", current_version);
} else {
did_download = true;
}
free_manifest(subMan);
}
if (access(untard_file, F_OK) == -1) {
string_or_die(&url, "%s/%i/Manifest.%s.tar", content_url, current_version,
file->filename);
fprintf(stderr, "Error: Failure reading from %s\n", url);
free_string(&url);
}
unlink(tarfile);
free_string(&untard_file);
free_string(&tarfile);
}
if (did_download) {
fprintf(stderr, "Completed manifests download.\n\n");
}
return ret;
}
int search_main(int argc, char **argv)
{
int ret = 0;
struct manifest *MoM = NULL;
struct list *subs = NULL;
if (!parse_options(argc, argv)) {
return EINVALID_OPTION;
}
ret = swupd_init();
if (ret != 0) {
fprintf(stderr, "Failed swupd initialization, exiting now.\n");
return ret;
}
if (!init) {
fprintf(stderr, "Searching for '%s'\n\n", search_string);
}
ret = download_manifests(&MoM, &subs);
if (ret != 0) {
fprintf(stderr, "Error: Failed to download manifests\n");
goto clean_exit;
}
if (init) {
fprintf(stderr, "Successfully retrieved manifests. Exiting\n");
ret = 0;
goto clean_exit;
}
/* Arbitrary upper limit to ensure we aren't getting handed garbage */
if (!display_files &&
((strlen(search_string) <= 0) || (strlen(search_string) > NAME_MAX))) {
fprintf(stderr, "Error - search string invalid\n");
ret = EXIT_FAILURE;
goto clean_exit;
}
do_search(MoM, search_type, search_string);
clean_exit:
free_manifest(MoM);
free_subscriptions(&subs);
swupd_deinit();
return ret;
}