libopkg: add opkg_read_config_files() function
[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 (a->offline_root) free (a->offline_root);
135   a->offline_root = strdup (c->offline_root);
136
137   if (a->offline_root_pre_script_cmd) free (a->offline_root_pre_script_cmd);
138   a->offline_root_pre_script_cmd = strdup (c->offline_root_pre_script_cmd);
139
140   if (a->offline_root_post_script_cmd) free (a->offline_root_post_script_cmd);
141   a->offline_root_post_script_cmd = strdup (c->offline_root_post_script_cmd);
142
143   /* throw away old opkg_conf and start again */
144   opkg_conf_deinit (opkg->conf);
145   opkg_conf_init (opkg->conf, opkg->args);
146
147   free (opkg->options);
148   opkg_init_options_array (opkg->conf, &opkg->options);
149
150   return 0;
151 }
152
153 void
154 opkg_get_option (opkg_t *opkg, char *option, void **value)
155 {
156   int i = 0;
157   opkg_option_t *options = opkg->options;
158
159   /* can't store a value in a NULL pointer! */
160   if (!value)
161     return;
162
163   /* look up the option
164    * TODO: this would be much better as a hash table
165    */
166   while (options[i].name)
167   {
168     if (strcmp (options[i].name, option) != 0)
169     {
170       i++;
171       continue;
172     }
173   }
174
175   /* get the option */
176   switch (options[i].type)
177   {
178   case OPKG_OPT_TYPE_BOOL:
179     *((int *) value) = *((int *) options[i].value);
180     return;
181
182   case OPKG_OPT_TYPE_INT:
183     *((int *) value) = *((int *) options[i].value);
184     return;
185
186   case OPKG_OPT_TYPE_STRING:
187     *((char **)value) = strdup (options[i].value);
188     return;
189   }
190
191 }
192
193 void
194 opkg_set_option (opkg_t *opkg, char *option, void *value)
195 {
196   int i = 0;
197   opkg_option_t *options = opkg->options;
198
199   /* NULL values are not defined */
200   if (!value)
201     return;
202
203   /* look up the option
204    * TODO: this would be much better as a hash table
205    */
206   while (options[i].name)
207   {
208     if (strcmp (options[i].name, option) == 0)
209     {
210       break;
211     }
212     i++;
213   }
214
215   /* set the option */
216   switch (options[i].type)
217   {
218   case OPKG_OPT_TYPE_BOOL:
219     if (*((int *) value) == 0)
220       *((int *)options[i].value) = 0;
221     else
222       *((int *)options[i].value) = 1;
223     return;
224
225   case OPKG_OPT_TYPE_INT:
226     *((int *) options[i].value) = *((int *) value);
227     return;
228
229   case OPKG_OPT_TYPE_STRING:
230     *((char **)options[i].value) = strdup (value);
231     return;
232   }
233
234 }
235
236 int
237 opkg_install_package (opkg_t *opkg, char *package_name)
238 {
239   int err;
240
241   pkg_info_preinstall_check (opkg->conf);
242
243   if (opkg->conf->multiple_providers)
244   {
245     err = opkg_install_multi_by_name (opkg->conf, package_name);
246   }
247   else
248   {
249     err = opkg_install_by_name (opkg->conf, package_name);
250   }
251
252   err = opkg_configure_packages (opkg->conf, NULL);
253
254   if (opkg->conf->noaction)
255     return err;
256
257   opkg_conf_write_status_files (opkg->conf);
258   pkg_write_changed_filelists (opkg->conf);
259
260   return err;
261 }
262
263 int
264 opkg_remove_package (opkg_t *opkg, char *package_name)
265 {
266   return 1;
267 }
268
269 int
270 opkg_upgrade_package (opkg_t *opkg, char *package_name)
271 {
272   return 1;
273 }
274
275 int
276 opkg_upgrade_all (opkg_t *opkg)
277 {
278   return 1;
279 }
280
281 int
282 opkg_update_package_lists (opkg_t *opkg)
283 {
284   char *tmp;
285   int err;
286   char *lists_dir;
287   pkg_src_list_elt_t *iter;
288   pkg_src_t *src;
289
290
291   sprintf_alloc (&lists_dir, "%s",
292                  (opkg->conf->restrict_to_default_dest)
293                  ? opkg->conf->default_dest->lists_dir
294                  : opkg->conf->lists_dir);
295
296   if (!file_is_dir (lists_dir))
297   {
298     if (file_exists (lists_dir))
299     {
300       /* XXX: Error: file exists but is not a directory */
301       free (lists_dir);
302       return 1;
303     }
304
305     err = file_mkdir_hier (lists_dir, 0755);
306     if (err)
307     {
308       /* XXX: Error: failed to create directory */
309       free (lists_dir);
310       return 1;
311     }
312   }
313
314   tmp = strdup ("/tmp/opkg.XXXXXX");
315
316   if (mkdtemp (tmp) == NULL)
317   {
318     /* XXX: Error: could not create temporary file name */
319     free (lists_dir);
320     free (tmp);
321     return 1;
322   }
323
324
325   for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next)
326   {
327     char *url, *list_file_name;
328
329     src = iter->data;
330
331     if (src->extra_data)  /* debian style? */
332       sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
333                      src->gzip ? "Packages.gz" : "Packages");
334     else
335       sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
336
337     sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
338     if (src->gzip)
339     {
340       char *tmp_file_name;
341       FILE *in, *out;
342
343       sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
344
345       /* XXX: Note: downloading url */
346       err = opkg_download (opkg->conf, url, tmp_file_name);
347
348       if (err == 0)
349       {
350         /* XXX: Note: Inflating downloaded file */
351         in = fopen (tmp_file_name, "r");
352         out = fopen (list_file_name, "w");
353         if (in && out)
354           unzip (in, out);
355         else
356           err = 1;
357         if (in)
358           fclose (in);
359         if (out)
360           fclose (out);
361         unlink (tmp_file_name);
362       }
363     }
364     else
365       err = opkg_download (opkg->conf, url, list_file_name);
366
367     if (err)
368     {
369       /* XXX: Error: download error */
370     }
371     free (url);
372
373 #ifdef HAVE_GPGME
374     /* download detached signitures to verify the package lists */
375     /* get the url for the sig file */
376     if (src->extra_data)  /* debian style? */
377       sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
378                      "Packages.sig");
379     else
380       sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
381
382     /* create temporary file for it */
383     char *tmp_file_name;
384
385     sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig");
386
387     err = opkg_download (opkg->conf, url, tmp_file_name);
388     if (err)
389     {
390       /* XXX: Warning: Download failed */
391     }
392     else
393     {
394       int err;
395       err = opkg_verify_file (opkg->conf, list_file_name, tmp_file_name);
396       if (err == 0)
397       {
398         /* XXX: Notice: Signature check passed */
399       }
400       else
401       {
402         /* XXX: Warning: Signature check failed */
403       }
404     }
405     unlink (tmp_file_name);
406     free (tmp_file_name);
407     free (url);
408 #else
409     /* XXX: Note: Signiture check for %s skipped because GPG support was not
410      * enabled in this build
411      */
412 #endif
413     free (list_file_name);
414   }
415   rmdir (tmp);
416   free (tmp);
417   free (lists_dir);
418
419   return 0;
420 }