opkg: implement package listing in new libopkg
[oweals/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;
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       break;
315     }
316     i++;
317   }
318
319   /* set the option */
320   switch (options[i].type)
321   {
322   case OPKG_OPT_TYPE_BOOL:
323     if (*((int *) value) == 0)
324       *((int *)options[i].value) = 0;
325     else
326       *((int *)options[i].value) = 1;
327     return;
328
329   case OPKG_OPT_TYPE_INT:
330     *((int *) options[i].value) = *((int *) value);
331     return;
332
333   case OPKG_OPT_TYPE_STRING:
334     *((char **)options[i].value) = strdup (value);
335     return;
336   }
337
338 }
339
340 int
341 opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
342 {
343   int err;
344   char *package_id = NULL;
345
346   opkg_assert (opkg != NULL);
347   opkg_assert (package_name != NULL);
348
349   progress (0);
350
351   /* download the package */
352   opkg_prepare_url_for_install (opkg->conf, package_name, &package_id);
353
354   progress (50);
355
356   /* ... */
357   pkg_info_preinstall_check (opkg->conf);
358
359   if (!package_id)
360     package_id = strdup (package_name);
361
362   /* unpack the package */
363   if (opkg->conf->multiple_providers)
364   {
365     err = opkg_install_multi_by_name (opkg->conf, package_id);
366   }
367   else
368   {
369     err = opkg_install_by_name (opkg->conf, package_id);
370   }
371
372   if (err)
373     return err;
374
375   progress (75);
376
377   /* run configure scripts, etc. */
378   err = opkg_configure_packages (opkg->conf, NULL);
379   if (err)
380     return err;
381
382   /* write out status files and file lists */
383   opkg_conf_write_status_files (opkg->conf);
384   pkg_write_changed_filelists (opkg->conf);
385
386   progress (100);
387   return 0;
388 }
389
390 int
391 opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
392 {
393   pkg_t *pkg = NULL;
394   pkg_t *pkg_to_remove;
395
396   opkg_assert (opkg != NULL);
397   opkg_assert (package_name != NULL);
398
399   progress (0);
400
401   pkg_info_preinstall_check (opkg->conf);
402
403   pkg_vec_t *installed_pkgs = pkg_vec_alloc ();
404
405   pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed_pkgs);
406
407   progress (25);
408
409   pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, package_name);
410
411   if (pkg == NULL)
412   {
413     /* XXX: Error: Package not installed. */
414     return 1;
415   }
416
417   if (pkg->state_status == SS_NOT_INSTALLED)
418   {
419     /* XXX:  Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
420     return 1;
421   }
422
423   progress (75);
424
425   if (opkg->conf->restrict_to_default_dest)
426   {
427     pkg_to_remove = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
428                                                            pkg->name,
429                                                            opkg->conf->default_dest);
430   }
431   else
432   {
433     pkg_to_remove = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, pkg->name );
434   }
435
436
437   progress (75);
438
439   opkg_remove_pkg (opkg->conf, pkg_to_remove, 0);
440
441   /* write out status files and file lists */
442   opkg_conf_write_status_files (opkg->conf);
443   pkg_write_changed_filelists (opkg->conf);
444
445
446   progress (100);
447   return 0;
448 }
449
450 int
451 opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
452 {
453   pkg_t *pkg;
454
455   opkg_assert (opkg != NULL);
456   opkg_assert (package_name != NULL);
457
458   progress (0);
459
460   pkg_info_preinstall_check (opkg->conf);
461
462   if (opkg->conf->restrict_to_default_dest)
463   {
464     pkg = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
465                                                  package_name,
466                                                  opkg->conf->default_dest);
467     if (pkg == NULL)
468     {
469       /* XXX: Error: Package not installed in default_dest */
470       return 1;
471     }
472   }
473   else
474   {
475     pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash,
476                                             package_name);
477   }
478
479   if (!pkg)
480   {
481     /* XXX: Error: Package not installed */
482     return 1;
483   }
484
485   progress (25);
486
487   opkg_upgrade_pkg (opkg->conf, pkg);
488   progress (75);
489
490   opkg_configure_packages (opkg->conf, NULL);
491   progress (100);
492   return 0;
493 }
494
495 int
496 opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
497 {
498   pkg_vec_t *installed;
499   int err = 0;
500   int i;
501   pkg_t *pkg;
502
503   opkg_assert (opkg != NULL);
504   progress (0);
505
506   installed = pkg_vec_alloc ();
507   pkg_info_preinstall_check (opkg->conf);
508
509   pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed);
510   for (i = 0; i < installed->len; i++)
511   {
512     pkg = installed->pkgs[i];
513     err += opkg_upgrade_pkg (opkg->conf, pkg);
514     progress (100 * i / installed->len);
515   }
516   pkg_vec_free (installed);
517
518   if (err)
519     return 1;
520
521   err = opkg_configure_packages (opkg->conf, NULL);
522   if (err)
523     return 1;
524
525   progress (100);
526   return 0;
527 }
528
529 int
530 opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
531 {
532   char *tmp;
533   int err;
534   char *lists_dir;
535   pkg_src_list_elt_t *iter;
536   pkg_src_t *src;
537   int sources_list_count, sources_done;
538
539   opkg_assert (opkg != NULL);
540
541   progress (0);
542
543   sprintf_alloc (&lists_dir, "%s",
544                  (opkg->conf->restrict_to_default_dest)
545                  ? opkg->conf->default_dest->lists_dir
546                  : opkg->conf->lists_dir);
547
548   if (!file_is_dir (lists_dir))
549   {
550     if (file_exists (lists_dir))
551     {
552       /* XXX: Error: file exists but is not a directory */
553       free (lists_dir);
554       return 1;
555     }
556
557     err = file_mkdir_hier (lists_dir, 0755);
558     if (err)
559     {
560       /* XXX: Error: failed to create directory */
561       free (lists_dir);
562       return 1;
563     }
564   }
565
566   tmp = strdup ("/tmp/opkg.XXXXXX");
567
568   if (mkdtemp (tmp) == NULL)
569   {
570     /* XXX: Error: could not create temporary file name */
571     free (lists_dir);
572     free (tmp);
573     return 1;
574   }
575
576   /* cout the number of sources so we can give some progress updates */
577   sources_list_count = 0;
578   sources_done = 0;
579   iter = opkg->conf->pkg_src_list.head;
580   while (iter)
581   {
582     sources_list_count++;
583     iter = iter->next;
584   }
585
586   for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next)
587   {
588     char *url, *list_file_name;
589
590     src = iter->data;
591
592     if (src->extra_data)  /* debian style? */
593       sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
594                      src->gzip ? "Packages.gz" : "Packages");
595     else
596       sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
597
598     sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
599     if (src->gzip)
600     {
601       char *tmp_file_name;
602       FILE *in, *out;
603       struct _curl_cb_data cb_data;
604
605       sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
606
607       /* XXX: Note: downloading url */
608
609       cb_data.cb = progress_callback;
610       cb_data.opkg = opkg;
611       cb_data.user_data = user_data;
612       cb_data.start_range = 100 * sources_done / sources_list_count;
613       cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
614
615       err = opkg_download (opkg->conf, url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
616
617       if (err == 0)
618       {
619         /* XXX: Note: Inflating downloaded file */
620         in = fopen (tmp_file_name, "r");
621         out = fopen (list_file_name, "w");
622         if (in && out)
623           unzip (in, out);
624         else
625           err = 1;
626         if (in)
627           fclose (in);
628         if (out)
629           fclose (out);
630         unlink (tmp_file_name);
631       }
632     }
633     else
634       err = opkg_download (opkg->conf, url, list_file_name, NULL, NULL);
635
636     if (err)
637     {
638       /* XXX: Error: download error */
639     }
640     free (url);
641
642 #ifdef HAVE_GPGME
643     /* download detached signitures to verify the package lists */
644     /* get the url for the sig file */
645     if (src->extra_data)  /* debian style? */
646       sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
647                      "Packages.sig");
648     else
649       sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
650
651     /* create temporary file for it */
652     char *tmp_file_name;
653
654     sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig");
655
656     err = opkg_download (opkg->conf, url, tmp_file_name, NULL, NULL);
657     if (err)
658     {
659       /* XXX: Warning: Download failed */
660     }
661     else
662     {
663       int err;
664       err = opkg_verify_file (opkg->conf, list_file_name, tmp_file_name);
665       if (err == 0)
666       {
667         /* XXX: Notice: Signature check passed */
668       }
669       else
670       {
671         /* XXX: Warning: Signature check failed */
672       }
673     }
674     unlink (tmp_file_name);
675     free (tmp_file_name);
676     free (url);
677 #else
678     /* XXX: Note: Signiture check for %s skipped because GPG support was not
679      * enabled in this build
680      */
681 #endif
682     free (list_file_name);
683
684     sources_done++;
685     progress (100 * sources_done / sources_list_count);
686   }
687
688   rmdir (tmp);
689   free (tmp);
690   free (lists_dir);
691
692   return 0;
693 }
694
695
696 int
697 opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
698 {
699   pkg_vec_t *all;
700   int i;
701
702   opkg_assert (opkg);
703   opkg_assert (callback);
704
705   all = pkg_vec_alloc ();
706   pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
707   for (i = 0; i < all->len; i++)
708   {
709     pkg_t *pkg;
710     opkg_package_t *package;
711
712     pkg = all->pkgs[i];
713
714     package = opkg_package_new_with_values (
715         pkg->name,
716         pkg->version,
717         pkg->architecture,
718         pkg->description,
719         pkg->tags,
720         (pkg->state_status == SS_INSTALLED));
721
722     callback (opkg, package, user_data);
723   }
724
725   pkg_vec_free (all);
726
727   return 0;
728 }
729