opkg: prevent segfault if option is not found
[project/opkg-lede.git] / libopkg / opkg.c
1 /* opkg.c - the opkg package management system
2
3 Thomas Wood <thomas@openedhand.com>
4
5 Copyright (C) 2008 OpenMoko Inc
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16 */
17
18 #include <config.h>
19 #include <fnmatch.h>
20
21 #include "opkg.h"
22 #include "opkg_conf.h"
23 #include "args.h"
24
25 #include "opkg_install.h"
26 #include "opkg_configure.h"
27 #include "opkg_download.h"
28 #include "opkg_remove.h"
29 #include "opkg_upgrade.h"
30
31 #include "sprintf_alloc.h"
32 #include "file_util.h"
33
34 #include <libbb/libbb.h>
35
36 struct _opkg_t
37 {
38 args_t *args;
39 opkg_conf_t *conf;
40 opkg_option_t *options;
41 };
42
43 #define opkg_assert(expr) if (!(expr)) { \
44 printf ("opkg: file %s: line %d (%s): Assertation '%s' failed",\
45 __FILE__, __LINE__, __PRETTY_FUNCTION__, # expr); abort (); }
46
47 #define progress(percent) if (progress_callback) progress_callback (opkg, percent, user_data);
48
49 /** Private Functions ***/
50
51
52 static int
53 opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
54 {
55 pkg_vec_t *all;
56 int i;
57 pkg_t *pkg;
58 int r, err = 0;
59
60 all = pkg_vec_alloc ();
61 pkg_hash_fetch_available (&conf->pkg_hash, all);
62
63 for (i = 0; i < all->len; i++)
64 {
65 pkg = all->pkgs[i];
66
67 if (pkg_name && fnmatch (pkg_name, pkg->name, 0))
68 continue;
69
70 if (pkg->state_status == SS_UNPACKED)
71 {
72 r = opkg_configure (conf, pkg);
73 if (r == 0)
74 {
75 pkg->state_status = SS_INSTALLED;
76 pkg->parent->state_status = SS_INSTALLED;
77 pkg->state_flag &= ~SF_PREFER;
78 }
79 else
80 {
81 if (!err)
82 err = r;
83 }
84 }
85 }
86
87 pkg_vec_free (all);
88 return err;
89 }
90
91 struct _curl_cb_data
92 {
93 opkg_progress_callback_t cb;
94 opkg_t *opkg;
95 void *user_data;
96 int start_range;
97 int finish_range;
98 };
99
100 int
101 curl_progress_cb (struct _curl_cb_data *cb_data,
102 double t, /* dltotal */
103 double d, /* dlnow */
104 double ultotal,
105 double ulnow)
106 {
107 int p = (t) ? d * 100 / t : 0;
108 static int prev = -1;
109
110 /* prevent the same value being sent twice (can occur due to rounding) */
111 if (p == prev)
112 return 0;
113 prev = p;
114
115 if (t < 1)
116 return 0;
117
118 (cb_data->cb)(cb_data->opkg,
119 cb_data->start_range + (d / t * ((cb_data->finish_range - cb_data->start_range))),
120 cb_data->user_data);
121
122 return 0;
123 }
124
125
126 /*** Public API ***/
127
128 opkg_package_t *
129 opkg_package_new ()
130 {
131
132 opkg_package_t *p;
133
134 p = malloc (sizeof (opkg_package_t));
135 memset (p, 0, sizeof (opkg_package_t));
136
137 return p;
138 }
139
140 opkg_package_t *
141 opkg_package_new_with_values (const char *name, const char *version,
142 const char *arch, const char *desc, const char *tags, int installed)
143 {
144 opkg_package_t *package;
145 package = opkg_package_new ();
146
147 #define sstrdup(x) (x) ? strdup (x) : NULL;
148
149 package->name = sstrdup (name);
150 package->version = sstrdup (version);
151 package->architecture = sstrdup (arch);
152 package->description = sstrdup (desc);
153 package->tags = sstrdup (tags);
154 package->installed = (installed != 0);
155
156 return package;
157 }
158
159 void
160 opkg_package_free (opkg_package_t *p)
161 {
162 free (p->name);
163 free (p->version);
164 free (p->architecture);
165 free (p->description);
166 free (p->tags);
167
168 free (p);
169 }
170
171 opkg_t *
172 opkg_new ()
173 {
174 opkg_t *opkg;
175 opkg = malloc (sizeof (opkg_t));
176
177 opkg->args = malloc (sizeof (args_t));
178 args_init (opkg->args);
179
180 opkg->conf = malloc (sizeof (opkg_conf_t));
181 opkg_conf_init (opkg->conf, opkg->args);
182
183 opkg_init_options_array (opkg->conf, &opkg->options);
184 return opkg;
185 }
186
187 void
188 opkg_free (opkg_t *opkg)
189 {
190 opkg_assert (opkg != NULL);
191
192 opkg_conf_deinit (opkg->conf);
193 args_deinit (opkg->args);
194 }
195
196 int
197 opkg_read_config_files (opkg_t *opkg)
198 {
199 args_t *a;
200 opkg_conf_t *c;
201
202 opkg_assert (opkg != NULL);
203
204 a = opkg->args;
205 c = opkg->conf;
206
207 /* Unfortunatly, the easiest way to re-read the config files right now is to
208 * throw away opkg->conf and start again */
209
210 /* copy the settings we need to keep */
211 a->autoremove = c->autoremove;
212 a->force_depends = c->force_depends;
213 a->force_defaults = c->force_defaults;
214 a->force_overwrite = c->force_overwrite;
215 a->force_downgrade = c->force_downgrade;
216 a->force_reinstall = c->force_reinstall;
217 a->force_removal_of_dependent_packages = c->force_removal_of_dependent_packages;
218 a->force_removal_of_essential_packages = c->force_removal_of_essential_packages;
219 a->nodeps = c->nodeps;
220 a->noaction = c->noaction;
221 a->query_all = c->query_all;
222 a->multiple_providers = c->multiple_providers;
223 a->verbosity = c->verbosity;
224
225 if (c->offline_root)
226 {
227 if (a->offline_root) free (a->offline_root);
228 a->offline_root = strdup (c->offline_root);
229 }
230
231 if (c->offline_root_pre_script_cmd)
232 {
233 if (a->offline_root_pre_script_cmd) free (a->offline_root_pre_script_cmd);
234 a->offline_root_pre_script_cmd = strdup (c->offline_root_pre_script_cmd);
235 }
236
237 if (c->offline_root_post_script_cmd)
238 {
239 if (a->offline_root_post_script_cmd) free (a->offline_root_post_script_cmd);
240 a->offline_root_post_script_cmd = strdup (c->offline_root_post_script_cmd);
241 }
242
243 /* throw away old opkg_conf and start again */
244 opkg_conf_deinit (opkg->conf);
245 opkg_conf_init (opkg->conf, opkg->args);
246
247 free (opkg->options);
248 opkg_init_options_array (opkg->conf, &opkg->options);
249
250 return 0;
251 }
252
253 void
254 opkg_get_option (opkg_t *opkg, char *option, void **value)
255 {
256 int i = 0;
257 opkg_option_t *options;
258
259 opkg_assert (opkg != NULL);
260 opkg_assert (option != NULL);
261 opkg_assert (value != NULL);
262
263 options = opkg->options;
264
265 /* look up the option
266 * TODO: this would be much better as a hash table
267 */
268 while (options[i].name)
269 {
270 if (strcmp (options[i].name, option) != 0)
271 {
272 i++;
273 continue;
274 }
275 }
276
277 /* get the option */
278 switch (options[i].type)
279 {
280 case OPKG_OPT_TYPE_BOOL:
281 *((int *) value) = *((int *) options[i].value);
282 return;
283
284 case OPKG_OPT_TYPE_INT:
285 *((int *) value) = *((int *) options[i].value);
286 return;
287
288 case OPKG_OPT_TYPE_STRING:
289 *((char **)value) = strdup (options[i].value);
290 return;
291 }
292
293 }
294
295 void
296 opkg_set_option (opkg_t *opkg, char *option, void *value)
297 {
298 int i = 0, found = 0;
299 opkg_option_t *options;
300
301 opkg_assert (opkg != NULL);
302 opkg_assert (option != NULL);
303 opkg_assert (value != NULL);
304
305 options = opkg->options;
306
307 /* look up the option
308 * TODO: this would be much better as a hash table
309 */
310 while (options[i].name)
311 {
312 if (strcmp (options[i].name, option) == 0)
313 {
314 found = 1;
315 break;
316 }
317 i++;
318 }
319
320 if (!found)
321 {
322 /* XXX: Warning: Option not found */
323 return;
324 }
325
326 /* set the option */
327 switch (options[i].type)
328 {
329 case OPKG_OPT_TYPE_BOOL:
330 if (*((int *) value) == 0)
331 *((int *)options[i].value) = 0;
332 else
333 *((int *)options[i].value) = 1;
334 return;
335
336 case OPKG_OPT_TYPE_INT:
337 *((int *) options[i].value) = *((int *) value);
338 return;
339
340 case OPKG_OPT_TYPE_STRING:
341 *((char **)options[i].value) = strdup (value);
342 return;
343 }
344
345 }
346
347 int
348 opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
349 {
350 int err;
351 char *package_id = NULL;
352
353 opkg_assert (opkg != NULL);
354 opkg_assert (package_name != NULL);
355
356 progress (0);
357
358 /* download the package */
359 opkg_prepare_url_for_install (opkg->conf, package_name, &package_id);
360
361 progress (50);
362
363 /* ... */
364 pkg_info_preinstall_check (opkg->conf);
365
366 if (!package_id)
367 package_id = strdup (package_name);
368
369 /* unpack the package */
370 if (opkg->conf->multiple_providers)
371 {
372 err = opkg_install_multi_by_name (opkg->conf, package_id);
373 }
374 else
375 {
376 err = opkg_install_by_name (opkg->conf, package_id);
377 }
378
379 if (err)
380 return err;
381
382 progress (75);
383
384 /* run configure scripts, etc. */
385 err = opkg_configure_packages (opkg->conf, NULL);
386 if (err)
387 return err;
388
389 /* write out status files and file lists */
390 opkg_conf_write_status_files (opkg->conf);
391 pkg_write_changed_filelists (opkg->conf);
392
393 progress (100);
394 return 0;
395 }
396
397 int
398 opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
399 {
400 pkg_t *pkg = NULL;
401 pkg_t *pkg_to_remove;
402
403 opkg_assert (opkg != NULL);
404 opkg_assert (package_name != NULL);
405
406 progress (0);
407
408 pkg_info_preinstall_check (opkg->conf);
409
410 pkg_vec_t *installed_pkgs = pkg_vec_alloc ();
411
412 pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed_pkgs);
413
414 progress (25);
415
416 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, package_name);
417
418 if (pkg == NULL)
419 {
420 /* XXX: Error: Package not installed. */
421 return 1;
422 }
423
424 if (pkg->state_status == SS_NOT_INSTALLED)
425 {
426 /* XXX: Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
427 return 1;
428 }
429
430 progress (75);
431
432 if (opkg->conf->restrict_to_default_dest)
433 {
434 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
435 pkg->name,
436 opkg->conf->default_dest);
437 }
438 else
439 {
440 pkg_to_remove = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, pkg->name );
441 }
442
443
444 progress (75);
445
446 opkg_remove_pkg (opkg->conf, pkg_to_remove, 0);
447
448 /* write out status files and file lists */
449 opkg_conf_write_status_files (opkg->conf);
450 pkg_write_changed_filelists (opkg->conf);
451
452
453 progress (100);
454 return 0;
455 }
456
457 int
458 opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
459 {
460 pkg_t *pkg;
461
462 opkg_assert (opkg != NULL);
463 opkg_assert (package_name != NULL);
464
465 progress (0);
466
467 pkg_info_preinstall_check (opkg->conf);
468
469 if (opkg->conf->restrict_to_default_dest)
470 {
471 pkg = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
472 package_name,
473 opkg->conf->default_dest);
474 if (pkg == NULL)
475 {
476 /* XXX: Error: Package not installed in default_dest */
477 return 1;
478 }
479 }
480 else
481 {
482 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash,
483 package_name);
484 }
485
486 if (!pkg)
487 {
488 /* XXX: Error: Package not installed */
489 return 1;
490 }
491
492 progress (25);
493
494 opkg_upgrade_pkg (opkg->conf, pkg);
495 progress (75);
496
497 opkg_configure_packages (opkg->conf, NULL);
498 progress (100);
499 return 0;
500 }
501
502 int
503 opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
504 {
505 pkg_vec_t *installed;
506 int err = 0;
507 int i;
508 pkg_t *pkg;
509
510 opkg_assert (opkg != NULL);
511 progress (0);
512
513 installed = pkg_vec_alloc ();
514 pkg_info_preinstall_check (opkg->conf);
515
516 pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed);
517 for (i = 0; i < installed->len; i++)
518 {
519 pkg = installed->pkgs[i];
520 err += opkg_upgrade_pkg (opkg->conf, pkg);
521 progress (100 * i / installed->len);
522 }
523 pkg_vec_free (installed);
524
525 if (err)
526 return 1;
527
528 err = opkg_configure_packages (opkg->conf, NULL);
529 if (err)
530 return 1;
531
532 progress (100);
533 return 0;
534 }
535
536 int
537 opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
538 {
539 char *tmp;
540 int err;
541 char *lists_dir;
542 pkg_src_list_elt_t *iter;
543 pkg_src_t *src;
544 int sources_list_count, sources_done;
545
546 opkg_assert (opkg != NULL);
547
548 progress (0);
549
550 sprintf_alloc (&lists_dir, "%s",
551 (opkg->conf->restrict_to_default_dest)
552 ? opkg->conf->default_dest->lists_dir
553 : opkg->conf->lists_dir);
554
555 if (!file_is_dir (lists_dir))
556 {
557 if (file_exists (lists_dir))
558 {
559 /* XXX: Error: file exists but is not a directory */
560 free (lists_dir);
561 return 1;
562 }
563
564 err = file_mkdir_hier (lists_dir, 0755);
565 if (err)
566 {
567 /* XXX: Error: failed to create directory */
568 free (lists_dir);
569 return 1;
570 }
571 }
572
573 tmp = strdup ("/tmp/opkg.XXXXXX");
574
575 if (mkdtemp (tmp) == NULL)
576 {
577 /* XXX: Error: could not create temporary file name */
578 free (lists_dir);
579 free (tmp);
580 return 1;
581 }
582
583 /* cout the number of sources so we can give some progress updates */
584 sources_list_count = 0;
585 sources_done = 0;
586 iter = opkg->conf->pkg_src_list.head;
587 while (iter)
588 {
589 sources_list_count++;
590 iter = iter->next;
591 }
592
593 for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next)
594 {
595 char *url, *list_file_name;
596
597 src = iter->data;
598
599 if (src->extra_data) /* debian style? */
600 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
601 src->gzip ? "Packages.gz" : "Packages");
602 else
603 sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
604
605 sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
606 if (src->gzip)
607 {
608 char *tmp_file_name;
609 FILE *in, *out;
610 struct _curl_cb_data cb_data;
611
612 sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
613
614 /* XXX: Note: downloading url */
615
616 cb_data.cb = progress_callback;
617 cb_data.opkg = opkg;
618 cb_data.user_data = user_data;
619 cb_data.start_range = 100 * sources_done / sources_list_count;
620 cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
621
622 err = opkg_download (opkg->conf, url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
623
624 if (err == 0)
625 {
626 /* XXX: Note: Inflating downloaded file */
627 in = fopen (tmp_file_name, "r");
628 out = fopen (list_file_name, "w");
629 if (in && out)
630 unzip (in, out);
631 else
632 err = 1;
633 if (in)
634 fclose (in);
635 if (out)
636 fclose (out);
637 unlink (tmp_file_name);
638 }
639 }
640 else
641 err = opkg_download (opkg->conf, url, list_file_name, NULL, NULL);
642
643 if (err)
644 {
645 /* XXX: Error: download error */
646 }
647 free (url);
648
649 #ifdef HAVE_GPGME
650 /* download detached signitures to verify the package lists */
651 /* get the url for the sig file */
652 if (src->extra_data) /* debian style? */
653 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
654 "Packages.sig");
655 else
656 sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
657
658 /* create temporary file for it */
659 char *tmp_file_name;
660
661 sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig");
662
663 err = opkg_download (opkg->conf, url, tmp_file_name, NULL, NULL);
664 if (err)
665 {
666 /* XXX: Warning: Download failed */
667 }
668 else
669 {
670 int err;
671 err = opkg_verify_file (opkg->conf, list_file_name, tmp_file_name);
672 if (err == 0)
673 {
674 /* XXX: Notice: Signature check passed */
675 }
676 else
677 {
678 /* XXX: Warning: Signature check failed */
679 }
680 }
681 unlink (tmp_file_name);
682 free (tmp_file_name);
683 free (url);
684 #else
685 /* XXX: Note: Signiture check for %s skipped because GPG support was not
686 * enabled in this build
687 */
688 #endif
689 free (list_file_name);
690
691 sources_done++;
692 progress (100 * sources_done / sources_list_count);
693 }
694
695 rmdir (tmp);
696 free (tmp);
697 free (lists_dir);
698
699 return 0;
700 }
701
702
703 int
704 opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
705 {
706 pkg_vec_t *all;
707 int i;
708
709 opkg_assert (opkg);
710 opkg_assert (callback);
711
712 all = pkg_vec_alloc ();
713 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
714 for (i = 0; i < all->len; i++)
715 {
716 pkg_t *pkg;
717 opkg_package_t *package;
718
719 pkg = all->pkgs[i];
720
721 package = opkg_package_new_with_values (
722 pkg->name,
723 pkg->version,
724 pkg->architecture,
725 pkg->description,
726 pkg->tags,
727 (pkg->state_status == SS_INSTALLED));
728
729 callback (opkg, package, user_data);
730 }
731
732 pkg_vec_free (all);
733
734 return 0;
735 }
736