opkg: add progress callbacks to libopkg api
[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
29 #include "sprintf_alloc.h"
30 #include "file_util.h"
31
32 #include <libbb/libbb.h>
33
34 struct _opkg_t
35 {
36   args_t *args;
37   opkg_conf_t *conf;
38   opkg_option_t *options;
39 };
40
41 /** Private Functions ***/
42
43
44 static int
45 opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
46 {
47   pkg_vec_t *all;
48   int i;
49   pkg_t *pkg;
50   int r, err = 0;
51
52   all = pkg_vec_alloc ();
53   pkg_hash_fetch_available (&conf->pkg_hash, all);
54
55   for (i = 0; i < all->len; i++)
56   {
57     pkg = all->pkgs[i];
58
59     if (pkg_name && fnmatch (pkg_name, pkg->name, 0))
60       continue;
61
62     if (pkg->state_status == SS_UNPACKED)
63     {
64       r = opkg_configure (conf, pkg);
65       if (r == 0)
66       {
67         pkg->state_status = SS_INSTALLED;
68         pkg->parent->state_status = SS_INSTALLED;
69         pkg->state_flag &= ~SF_PREFER;
70       }
71       else
72       {
73         if (!err)
74           err = r;
75       }
76     }
77   }
78
79   pkg_vec_free (all);
80   return err;
81 }
82
83
84
85 /*** Public API ***/
86
87 opkg_t *
88 opkg_new ()
89 {
90   opkg_t *opkg;
91   opkg = malloc (sizeof (opkg_t));
92
93   opkg->args = malloc (sizeof (args_t));
94   args_init (opkg->args);
95
96   opkg->conf = malloc (sizeof (opkg_conf_t));
97   opkg_conf_init (opkg->conf, opkg->args);
98
99   opkg_init_options_array (opkg->conf, &opkg->options);
100   return opkg;
101 }
102
103 void
104 opkg_free (opkg_t *opkg)
105 {
106   opkg_conf_deinit (opkg->conf);
107   args_deinit (opkg->args);
108 }
109
110 int
111 opkg_read_config_files (opkg_t *opkg)
112 {
113   args_t *a = opkg->args;
114   opkg_conf_t *c = opkg->conf;
115
116   /* Unfortunatly, the easiest way to re-read the config files right now is to
117    * throw away opkg->conf and start again */
118
119   /* copy the settings we need to keep */
120   a->autoremove = c->autoremove;
121   a->force_depends = c->force_depends;
122   a->force_defaults = c->force_defaults;
123   a->force_overwrite = c->force_overwrite;
124   a->force_downgrade = c->force_downgrade;
125   a->force_reinstall = c->force_reinstall;
126   a->force_removal_of_dependent_packages = c->force_removal_of_dependent_packages;
127   a->force_removal_of_essential_packages = c->force_removal_of_essential_packages;
128   a->nodeps = c->nodeps;
129   a->noaction = c->noaction;
130   a->query_all = c->query_all;
131   a->multiple_providers = c->multiple_providers;
132   a->verbosity = c->verbosity;
133
134   if (c->offline_root)
135   {
136     if (a->offline_root) free (a->offline_root);
137     a->offline_root = strdup (c->offline_root);
138   }
139
140   if (c->offline_root_pre_script_cmd)
141   {
142     if (a->offline_root_pre_script_cmd) free (a->offline_root_pre_script_cmd);
143     a->offline_root_pre_script_cmd = strdup (c->offline_root_pre_script_cmd);
144   }
145
146   if (c->offline_root_post_script_cmd)
147   {
148     if (a->offline_root_post_script_cmd) free (a->offline_root_post_script_cmd);
149     a->offline_root_post_script_cmd = strdup (c->offline_root_post_script_cmd);
150   }
151
152   /* throw away old opkg_conf and start again */
153   opkg_conf_deinit (opkg->conf);
154   opkg_conf_init (opkg->conf, opkg->args);
155
156   free (opkg->options);
157   opkg_init_options_array (opkg->conf, &opkg->options);
158
159   return 0;
160 }
161
162 void
163 opkg_get_option (opkg_t *opkg, char *option, void **value)
164 {
165   int i = 0;
166   opkg_option_t *options = opkg->options;
167
168   /* can't store a value in a NULL pointer! */
169   if (!value)
170     return;
171
172   /* look up the option
173    * TODO: this would be much better as a hash table
174    */
175   while (options[i].name)
176   {
177     if (strcmp (options[i].name, option) != 0)
178     {
179       i++;
180       continue;
181     }
182   }
183
184   /* get the option */
185   switch (options[i].type)
186   {
187   case OPKG_OPT_TYPE_BOOL:
188     *((int *) value) = *((int *) options[i].value);
189     return;
190
191   case OPKG_OPT_TYPE_INT:
192     *((int *) value) = *((int *) options[i].value);
193     return;
194
195   case OPKG_OPT_TYPE_STRING:
196     *((char **)value) = strdup (options[i].value);
197     return;
198   }
199
200 }
201
202 void
203 opkg_set_option (opkg_t *opkg, char *option, void *value)
204 {
205   int i = 0;
206   opkg_option_t *options = opkg->options;
207
208   /* NULL values are not defined */
209   if (!value)
210     return;
211
212   /* look up the option
213    * TODO: this would be much better as a hash table
214    */
215   while (options[i].name)
216   {
217     if (strcmp (options[i].name, option) == 0)
218     {
219       break;
220     }
221     i++;
222   }
223
224   /* set the option */
225   switch (options[i].type)
226   {
227   case OPKG_OPT_TYPE_BOOL:
228     if (*((int *) value) == 0)
229       *((int *)options[i].value) = 0;
230     else
231       *((int *)options[i].value) = 1;
232     return;
233
234   case OPKG_OPT_TYPE_INT:
235     *((int *) options[i].value) = *((int *) value);
236     return;
237
238   case OPKG_OPT_TYPE_STRING:
239     *((char **)options[i].value) = strdup (value);
240     return;
241   }
242
243 }
244
245 int
246 opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
247 {
248   int err;
249   char *package_id;
250
251   progress_callback (opkg, 0, user_data);
252
253   /* download the package */
254   opkg_prepare_url_for_install (opkg->conf, package_name, &package_id);
255   progress_callback (opkg, 50, user_data);
256
257   /* ... */
258   pkg_info_preinstall_check (opkg->conf);
259
260   /* unpack the package */
261   if (opkg->conf->multiple_providers)
262   {
263     err = opkg_install_multi_by_name (opkg->conf, package_id);
264   }
265   else
266   {
267     err = opkg_install_by_name (opkg->conf, package_id);
268   }
269
270   progress_callback (opkg, 75, user_data);
271
272   /* run configure scripts, etc. */
273   err = opkg_configure_packages (opkg->conf, NULL);
274
275   /* write out status files and file lists */
276   opkg_conf_write_status_files (opkg->conf);
277   pkg_write_changed_filelists (opkg->conf);
278
279   progress_callback (opkg, 100, user_data);
280   return err;
281 }
282
283 int
284 opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
285 {
286   return 1;
287 }
288
289 int
290 opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
291 {
292   return 1;
293 }
294
295 int
296 opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
297 {
298   return 1;
299 }
300
301 int
302 opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
303 {
304   char *tmp;
305   int err;
306   char *lists_dir;
307   pkg_src_list_elt_t *iter;
308   pkg_src_t *src;
309   int sources_list_count, sources_done;
310
311   progress_callback (opkg, 0, user_data);
312
313   sprintf_alloc (&lists_dir, "%s",
314                  (opkg->conf->restrict_to_default_dest)
315                  ? opkg->conf->default_dest->lists_dir
316                  : opkg->conf->lists_dir);
317
318   if (!file_is_dir (lists_dir))
319   {
320     if (file_exists (lists_dir))
321     {
322       /* XXX: Error: file exists but is not a directory */
323       free (lists_dir);
324       return 1;
325     }
326
327     err = file_mkdir_hier (lists_dir, 0755);
328     if (err)
329     {
330       /* XXX: Error: failed to create directory */
331       free (lists_dir);
332       return 1;
333     }
334   }
335
336   tmp = strdup ("/tmp/opkg.XXXXXX");
337
338   if (mkdtemp (tmp) == NULL)
339   {
340     /* XXX: Error: could not create temporary file name */
341     free (lists_dir);
342     free (tmp);
343     return 1;
344   }
345
346   /* cout the number of sources so we can give some progress updates */
347   sources_list_count = 0;
348   sources_done = 0;
349   iter = opkg->conf->pkg_src_list.head;
350   while (iter)
351   {
352     sources_list_count++;
353     iter = iter->next;
354   }
355
356   for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next)
357   {
358     char *url, *list_file_name;
359
360     src = iter->data;
361
362     if (src->extra_data)  /* debian style? */
363       sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
364                      src->gzip ? "Packages.gz" : "Packages");
365     else
366       sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
367
368     sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
369     if (src->gzip)
370     {
371       char *tmp_file_name;
372       FILE *in, *out;
373
374       sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
375
376       /* XXX: Note: downloading url */
377       err = opkg_download (opkg->conf, url, tmp_file_name);
378
379       if (err == 0)
380       {
381         /* XXX: Note: Inflating downloaded file */
382         in = fopen (tmp_file_name, "r");
383         out = fopen (list_file_name, "w");
384         if (in && out)
385           unzip (in, out);
386         else
387           err = 1;
388         if (in)
389           fclose (in);
390         if (out)
391           fclose (out);
392         unlink (tmp_file_name);
393       }
394     }
395     else
396       err = opkg_download (opkg->conf, url, list_file_name);
397
398     if (err)
399     {
400       /* XXX: Error: download error */
401     }
402     free (url);
403
404 #ifdef HAVE_GPGME
405     /* download detached signitures to verify the package lists */
406     /* get the url for the sig file */
407     if (src->extra_data)  /* debian style? */
408       sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
409                      "Packages.sig");
410     else
411       sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
412
413     /* create temporary file for it */
414     char *tmp_file_name;
415
416     sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig");
417
418     err = opkg_download (opkg->conf, url, tmp_file_name);
419     if (err)
420     {
421       /* XXX: Warning: Download failed */
422     }
423     else
424     {
425       int err;
426       err = opkg_verify_file (opkg->conf, list_file_name, tmp_file_name);
427       if (err == 0)
428       {
429         /* XXX: Notice: Signature check passed */
430       }
431       else
432       {
433         /* XXX: Warning: Signature check failed */
434       }
435     }
436     unlink (tmp_file_name);
437     free (tmp_file_name);
438     free (url);
439 #else
440     /* XXX: Note: Signiture check for %s skipped because GPG support was not
441      * enabled in this build
442      */
443 #endif
444     free (list_file_name);
445
446     sources_done++;
447     progress_callback (opkg, 100 * sources_done / sources_list_count, user_data);
448   }
449   rmdir (tmp);
450   free (tmp);
451   free (lists_dir);
452
453   return 0;
454 }