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