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