opkg: add some safety macros
[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
91
92 /*** Public API ***/
93
94 opkg_t *
95 opkg_new ()
96 {
97   opkg_t *opkg;
98   opkg = malloc (sizeof (opkg_t));
99
100   opkg->args = malloc (sizeof (args_t));
101   args_init (opkg->args);
102
103   opkg->conf = malloc (sizeof (opkg_conf_t));
104   opkg_conf_init (opkg->conf, opkg->args);
105
106   opkg_init_options_array (opkg->conf, &opkg->options);
107   return opkg;
108 }
109
110 void
111 opkg_free (opkg_t *opkg)
112 {
113   opkg_assert (opkg != NULL);
114
115   opkg_conf_deinit (opkg->conf);
116   args_deinit (opkg->args);
117 }
118
119 int
120 opkg_read_config_files (opkg_t *opkg)
121 {
122   args_t *a;
123   opkg_conf_t *c;
124
125   opkg_assert (opkg != NULL);
126
127   a = opkg->args;
128   c = opkg->conf;
129
130   /* Unfortunatly, the easiest way to re-read the config files right now is to
131    * throw away opkg->conf and start again */
132
133   /* copy the settings we need to keep */
134   a->autoremove = c->autoremove;
135   a->force_depends = c->force_depends;
136   a->force_defaults = c->force_defaults;
137   a->force_overwrite = c->force_overwrite;
138   a->force_downgrade = c->force_downgrade;
139   a->force_reinstall = c->force_reinstall;
140   a->force_removal_of_dependent_packages = c->force_removal_of_dependent_packages;
141   a->force_removal_of_essential_packages = c->force_removal_of_essential_packages;
142   a->nodeps = c->nodeps;
143   a->noaction = c->noaction;
144   a->query_all = c->query_all;
145   a->multiple_providers = c->multiple_providers;
146   a->verbosity = c->verbosity;
147
148   if (c->offline_root)
149   {
150     if (a->offline_root) free (a->offline_root);
151     a->offline_root = strdup (c->offline_root);
152   }
153
154   if (c->offline_root_pre_script_cmd)
155   {
156     if (a->offline_root_pre_script_cmd) free (a->offline_root_pre_script_cmd);
157     a->offline_root_pre_script_cmd = strdup (c->offline_root_pre_script_cmd);
158   }
159
160   if (c->offline_root_post_script_cmd)
161   {
162     if (a->offline_root_post_script_cmd) free (a->offline_root_post_script_cmd);
163     a->offline_root_post_script_cmd = strdup (c->offline_root_post_script_cmd);
164   }
165
166   /* throw away old opkg_conf and start again */
167   opkg_conf_deinit (opkg->conf);
168   opkg_conf_init (opkg->conf, opkg->args);
169
170   free (opkg->options);
171   opkg_init_options_array (opkg->conf, &opkg->options);
172
173   return 0;
174 }
175
176 void
177 opkg_get_option (opkg_t *opkg, char *option, void **value)
178 {
179   int i = 0;
180   opkg_option_t *options;
181
182   opkg_assert (opkg != NULL);
183   opkg_assert (option != NULL);
184   opkg_assert (value != NULL);
185
186   options = opkg->options;
187
188   /* look up the option
189    * TODO: this would be much better as a hash table
190    */
191   while (options[i].name)
192   {
193     if (strcmp (options[i].name, option) != 0)
194     {
195       i++;
196       continue;
197     }
198   }
199
200   /* get the option */
201   switch (options[i].type)
202   {
203   case OPKG_OPT_TYPE_BOOL:
204     *((int *) value) = *((int *) options[i].value);
205     return;
206
207   case OPKG_OPT_TYPE_INT:
208     *((int *) value) = *((int *) options[i].value);
209     return;
210
211   case OPKG_OPT_TYPE_STRING:
212     *((char **)value) = strdup (options[i].value);
213     return;
214   }
215
216 }
217
218 void
219 opkg_set_option (opkg_t *opkg, char *option, void *value)
220 {
221   int i = 0;
222   opkg_option_t *options;
223
224   opkg_assert (opkg != NULL);
225   opkg_assert (option != NULL);
226   opkg_assert (value != NULL);
227
228   options = opkg->options;
229
230   /* look up the option
231    * TODO: this would be much better as a hash table
232    */
233   while (options[i].name)
234   {
235     if (strcmp (options[i].name, option) == 0)
236     {
237       break;
238     }
239     i++;
240   }
241
242   /* set the option */
243   switch (options[i].type)
244   {
245   case OPKG_OPT_TYPE_BOOL:
246     if (*((int *) value) == 0)
247       *((int *)options[i].value) = 0;
248     else
249       *((int *)options[i].value) = 1;
250     return;
251
252   case OPKG_OPT_TYPE_INT:
253     *((int *) options[i].value) = *((int *) value);
254     return;
255
256   case OPKG_OPT_TYPE_STRING:
257     *((char **)options[i].value) = strdup (value);
258     return;
259   }
260
261 }
262
263 int
264 opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
265 {
266   int err;
267   char *package_id = NULL;
268
269   opkg_assert (opkg != NULL);
270   opkg_assert (package_name != NULL);
271
272   progress (0);
273
274   /* download the package */
275   opkg_prepare_url_for_install (opkg->conf, package_name, &package_id);
276
277   progress (50);
278
279   /* ... */
280   pkg_info_preinstall_check (opkg->conf);
281
282   if (!package_id)
283     package_id = strdup (package_name);
284
285   /* unpack the package */
286   if (opkg->conf->multiple_providers)
287   {
288     err = opkg_install_multi_by_name (opkg->conf, package_id);
289   }
290   else
291   {
292     err = opkg_install_by_name (opkg->conf, package_id);
293   }
294
295   if (err)
296     return err;
297
298   progress (75);
299
300   /* run configure scripts, etc. */
301   err = opkg_configure_packages (opkg->conf, NULL);
302   if (err)
303     return err;
304
305   /* write out status files and file lists */
306   opkg_conf_write_status_files (opkg->conf);
307   pkg_write_changed_filelists (opkg->conf);
308
309   progress (100);
310   return 0;
311 }
312
313 int
314 opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
315 {
316   pkg_t *pkg = NULL;
317   pkg_t *pkg_to_remove;
318
319   opkg_assert (opkg != NULL);
320   opkg_assert (package_name != NULL);
321
322   progress (0);
323
324   pkg_info_preinstall_check (opkg->conf);
325
326   pkg_vec_t *installed_pkgs = pkg_vec_alloc ();
327
328   pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed_pkgs);
329
330   progress (25);
331
332   pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, package_name);
333
334   if (pkg == NULL)
335   {
336     /* XXX: Error: Package not installed. */
337     return 1;
338   }
339
340   if (pkg->state_status == SS_NOT_INSTALLED)
341   {
342     /* XXX:  Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
343     return 1;
344   }
345
346   progress (75);
347
348   if (opkg->conf->restrict_to_default_dest)
349   {
350     pkg_to_remove = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
351                                                            pkg->name,
352                                                            opkg->conf->default_dest);
353   }
354   else
355   {
356     pkg_to_remove = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, pkg->name );
357   }
358
359
360   progress (75);
361
362   opkg_remove_pkg (opkg->conf, pkg_to_remove, 0);
363
364   /* write out status files and file lists */
365   opkg_conf_write_status_files (opkg->conf);
366   pkg_write_changed_filelists (opkg->conf);
367
368
369   progress (100);
370   return 0;
371 }
372
373 int
374 opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
375 {
376   return 1;
377 }
378
379 int
380 opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
381 {
382   return 1;
383 }
384
385 int
386 opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
387 {
388   char *tmp;
389   int err;
390   char *lists_dir;
391   pkg_src_list_elt_t *iter;
392   pkg_src_t *src;
393   int sources_list_count, sources_done;
394
395   opkg_assert (opkg != NULL);
396
397   progress (0);
398
399   sprintf_alloc (&lists_dir, "%s",
400                  (opkg->conf->restrict_to_default_dest)
401                  ? opkg->conf->default_dest->lists_dir
402                  : opkg->conf->lists_dir);
403
404   if (!file_is_dir (lists_dir))
405   {
406     if (file_exists (lists_dir))
407     {
408       /* XXX: Error: file exists but is not a directory */
409       free (lists_dir);
410       return 1;
411     }
412
413     err = file_mkdir_hier (lists_dir, 0755);
414     if (err)
415     {
416       /* XXX: Error: failed to create directory */
417       free (lists_dir);
418       return 1;
419     }
420   }
421
422   tmp = strdup ("/tmp/opkg.XXXXXX");
423
424   if (mkdtemp (tmp) == NULL)
425   {
426     /* XXX: Error: could not create temporary file name */
427     free (lists_dir);
428     free (tmp);
429     return 1;
430   }
431
432   /* cout the number of sources so we can give some progress updates */
433   sources_list_count = 0;
434   sources_done = 0;
435   iter = opkg->conf->pkg_src_list.head;
436   while (iter)
437   {
438     sources_list_count++;
439     iter = iter->next;
440   }
441
442   for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next)
443   {
444     char *url, *list_file_name;
445
446     src = iter->data;
447
448     if (src->extra_data)  /* debian style? */
449       sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
450                      src->gzip ? "Packages.gz" : "Packages");
451     else
452       sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
453
454     sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
455     if (src->gzip)
456     {
457       char *tmp_file_name;
458       FILE *in, *out;
459
460       sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
461
462       /* XXX: Note: downloading url */
463       err = opkg_download (opkg->conf, url, tmp_file_name);
464
465       if (err == 0)
466       {
467         /* XXX: Note: Inflating downloaded file */
468         in = fopen (tmp_file_name, "r");
469         out = fopen (list_file_name, "w");
470         if (in && out)
471           unzip (in, out);
472         else
473           err = 1;
474         if (in)
475           fclose (in);
476         if (out)
477           fclose (out);
478         unlink (tmp_file_name);
479       }
480     }
481     else
482       err = opkg_download (opkg->conf, url, list_file_name);
483
484     if (err)
485     {
486       /* XXX: Error: download error */
487     }
488     free (url);
489
490 #ifdef HAVE_GPGME
491     /* download detached signitures to verify the package lists */
492     /* get the url for the sig file */
493     if (src->extra_data)  /* debian style? */
494       sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
495                      "Packages.sig");
496     else
497       sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
498
499     /* create temporary file for it */
500     char *tmp_file_name;
501
502     sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig");
503
504     err = opkg_download (opkg->conf, url, tmp_file_name);
505     if (err)
506     {
507       /* XXX: Warning: Download failed */
508     }
509     else
510     {
511       int err;
512       err = opkg_verify_file (opkg->conf, list_file_name, tmp_file_name);
513       if (err == 0)
514       {
515         /* XXX: Notice: Signature check passed */
516       }
517       else
518       {
519         /* XXX: Warning: Signature check failed */
520       }
521     }
522     unlink (tmp_file_name);
523     free (tmp_file_name);
524     free (url);
525 #else
526     /* XXX: Note: Signiture check for %s skipped because GPG support was not
527      * enabled in this build
528      */
529 #endif
530     free (list_file_name);
531
532     sources_done++;
533     progress (100 * sources_done / sources_list_count);
534   }
535
536   rmdir (tmp);
537   free (tmp);
538   free (lists_dir);
539
540   return 0;
541 }