opkg: run pre-install check before listing upgradable packages to ensure all
[project/opkg-lede.git] / libopkg / libopkg.c
1 /* opkglib.c - the opkg package management system
2
3 Florina Boor
4
5 Copyright (C) 2003 kernel concepts
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 "includes.h"
19 #include "libopkg.h"
20
21 #include "args.h"
22 #include "opkg_conf.h"
23 #include "opkg_cmd.h"
24 #include "file_util.h"
25
26 #include "opkg_message.h"
27 #include "opkg_error.h"
28
29 opkg_status_callback opkg_cb_status = NULL;
30 opkg_list_callback opkg_cb_list = NULL;
31
32
33 int
34 opkg_init (opkg_message_callback mcall,
35 opkg_response_callback rcall,
36 args_t * args)
37 {
38 opkg_cb_message = mcall;
39 opkg_cb_response = rcall;
40
41 args_init (args);
42
43 return 0;
44 }
45
46
47 int
48 opkg_deinit (args_t * args)
49 {
50 args_deinit (args);
51 opkg_cb_message = NULL;
52 opkg_cb_response = NULL;
53
54 /* place other cleanup stuff here */
55
56 return 0;
57 }
58
59
60 int
61 opkg_packages_list(args_t *args,
62 const char *packages,
63 opkg_list_callback cblist,
64 void *userdata)
65 {
66 opkg_cmd_t *cmd;
67 opkg_conf_t opkg_conf;
68 int err;
69
70 err = opkg_conf_init (&opkg_conf, args);
71 if (err)
72 {
73 return err;
74 }
75
76 opkg_cb_list = cblist;
77 /* we need to do this because of static declarations,
78 * maybe a good idea to change */
79 cmd = opkg_cmd_find ("list");
80 if (packages)
81 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &packages, userdata);
82 else
83 err = opkg_cmd_exec (cmd, &opkg_conf, 0, NULL, userdata);
84 opkg_cb_list = NULL;
85 opkg_conf_deinit (&opkg_conf);
86 return (err);
87 }
88
89
90 int
91 opkg_packages_status(args_t *args,
92 const char *packages,
93 opkg_status_callback cbstatus,
94 void *userdata)
95 {
96 opkg_cmd_t *cmd;
97 opkg_conf_t opkg_conf;
98 int err;
99
100 err = opkg_conf_init (&opkg_conf, args);
101 if (err)
102 {
103 return err;
104 }
105
106 opkg_cb_status = cbstatus;
107
108 /* we need to do this because of static declarations,
109 * maybe a good idea to change */
110 cmd = opkg_cmd_find ("status");
111 if (packages)
112 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &packages, userdata);
113 else
114 err = opkg_cmd_exec (cmd, &opkg_conf, 0, NULL, userdata);
115
116 opkg_cb_status = NULL;
117 opkg_conf_deinit (&opkg_conf);
118 return (err);
119 }
120
121
122 int
123 opkg_packages_info(args_t *args,
124 const char *packages,
125 opkg_status_callback cbstatus,
126 void *userdata)
127 {
128 opkg_cmd_t *cmd;
129 opkg_conf_t opkg_conf;
130 int err;
131
132 err = opkg_conf_init (&opkg_conf, args);
133 if (err)
134 {
135 return err;
136 }
137
138 opkg_cb_status = cbstatus;
139
140 /* we need to do this because of static declarations,
141 * maybe a good idea to change */
142 cmd = opkg_cmd_find ("info");
143 if (packages)
144 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &packages, userdata);
145 else
146 err = opkg_cmd_exec (cmd, &opkg_conf, 0, NULL, userdata);
147
148 opkg_cb_status = NULL;
149 opkg_conf_deinit (&opkg_conf);
150 return (err);
151 }
152
153
154 int
155 opkg_packages_install (args_t * args, const char *name)
156 {
157 opkg_cmd_t *cmd;
158 opkg_conf_t opkg_conf;
159 int err;
160
161 /* this error should be handled in application */
162 if (!name || !strlen (name))
163 return (-1);
164
165 err = opkg_conf_init (&opkg_conf, args);
166 if (err)
167 {
168 return err;
169 }
170
171 /* we need to do this because of static declarations,
172 * maybe a good idea to change */
173 cmd = opkg_cmd_find ("install");
174 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &name, NULL);
175
176 opkg_conf_deinit(&opkg_conf);
177 return (err);
178 }
179
180
181 int
182 opkg_packages_remove(args_t *args, const char *name, int purge)
183 {
184 opkg_cmd_t *cmd;
185 opkg_conf_t opkg_conf;
186 int err;
187
188 /* this error should be handled in application */
189 if (!name || !strlen (name))
190 return (-1);
191
192 err = opkg_conf_init (&opkg_conf, args);
193 if (err)
194 {
195 return err;
196 }
197
198 /* we need to do this because of static declarations,
199 * maybe a good idea to change */
200 if (purge)
201 cmd = opkg_cmd_find ("purge");
202 else
203 cmd = opkg_cmd_find ("remove");
204
205 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &name, NULL);
206
207 opkg_conf_deinit(&opkg_conf);
208 return (err);
209 }
210
211
212 int
213 opkg_lists_update(args_t *args)
214 {
215 opkg_cmd_t *cmd;
216 opkg_conf_t opkg_conf;
217 int err;
218
219 err = opkg_conf_init (&opkg_conf, args);
220 if (err)
221 {
222 return err;
223 }
224
225 /* we need to do this because of static declarations,
226 * maybe a good idea to change */
227 cmd = opkg_cmd_find ("update");
228
229 err = opkg_cmd_exec (cmd, &opkg_conf, 0, NULL, NULL);
230
231 opkg_conf_deinit(&opkg_conf);
232 return (err);
233 }
234
235
236 int
237 opkg_packages_upgrade(args_t *args)
238 {
239 opkg_cmd_t *cmd;
240 opkg_conf_t opkg_conf;
241 int err;
242
243 err = opkg_conf_init (&opkg_conf, args);
244 if (err)
245 {
246 return err;
247 }
248
249 /* we need to do this because of static declarations,
250 * maybe a good idea to change */
251 cmd = opkg_cmd_find ("upgrade");
252
253 err = opkg_cmd_exec (cmd, &opkg_conf, 0, NULL, NULL);
254
255 opkg_conf_deinit(&opkg_conf);
256 return (err);
257 }
258
259
260 int
261 opkg_packages_download (args_t * args, const char *name)
262 {
263 opkg_cmd_t *cmd;
264 opkg_conf_t opkg_conf;
265 int err;
266
267 /* this error should be handled in application */
268 if (!name || !strlen (name))
269 return (-1);
270
271 err = opkg_conf_init (&opkg_conf, args);
272 if (err)
273 {
274 return err;
275 }
276
277 /* we need to do this because of static declarations,
278 * maybe a good idea to change */
279 cmd = opkg_cmd_find ("download");
280 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &name, NULL);
281
282 opkg_conf_deinit(&opkg_conf);
283 return (err);
284 }
285
286
287 int
288 opkg_package_files(args_t *args,
289 const char *name,
290 opkg_list_callback cblist,
291 void *userdata)
292 {
293 opkg_cmd_t *cmd;
294 opkg_conf_t opkg_conf;
295 int err;
296
297 /* this error should be handled in application */
298 if (!name || !strlen (name))
299 return (-1);
300
301 err = opkg_conf_init (&opkg_conf, args);
302 if (err)
303 {
304 return err;
305 }
306
307 opkg_cb_list = cblist;
308
309 /* we need to do this because of static declarations,
310 * maybe a good idea to change */
311 cmd = opkg_cmd_find ("files");
312
313 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &name, userdata);
314
315 opkg_cb_list = NULL;
316 opkg_conf_deinit(&opkg_conf);
317 return (err);
318 }
319
320
321 int
322 opkg_file_search(args_t *args,
323 const char *file,
324 opkg_list_callback cblist,
325 void *userdata)
326 {
327 opkg_cmd_t *cmd;
328 opkg_conf_t opkg_conf;
329 int err;
330
331 /* this error should be handled in application */
332 if (!file || !strlen (file))
333 return (-1);
334
335 err = opkg_conf_init (&opkg_conf, args);
336 if (err)
337 {
338 return err;
339 }
340
341 opkg_cb_list = cblist;
342
343 /* we need to do this because of static declarations,
344 * maybe a good idea to change */
345 cmd = opkg_cmd_find ("search");
346 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &file, userdata);
347
348 opkg_cb_list = NULL;
349 opkg_conf_deinit(&opkg_conf);
350 return(err);
351 }
352
353
354 int
355 opkg_file_what(args_t *args, const char *file, const char* command)
356 {
357 opkg_cmd_t *cmd;
358 opkg_conf_t opkg_conf;
359 int err;
360
361 /* this error should be handled in application */
362 if (!file || !strlen (file))
363 return (-1);
364
365 err = opkg_conf_init (&opkg_conf, args);
366 if (err)
367 {
368 return err;
369 }
370
371 /* we need to do this because of static declarations,
372 * maybe a good idea to change */
373 cmd = opkg_cmd_find (command);
374 err = opkg_cmd_exec (cmd, &opkg_conf, 1, &file, NULL);
375
376 opkg_conf_deinit(&opkg_conf);
377 return(err);
378 }
379
380 #define opkg_package_whatdepends(args,file) opkg_file_what(args,file,"whatdepends")
381 #define opkg_package_whatrecommends(args, file) opkg_file_what(args,file,"whatrecommends")
382 #define opkg_package_whatprovides(args, file) opkg_file_what(args,file,"whatprovides")
383 #define opkg_package_whatconflicts(args, file) opkg_file_what(args,file,"whatconflicts")
384 #define opkg_package_whatreplaces(args, file) opkg_file_what(args,file,"whatreplaces")
385
386
387 int default_opkg_message_callback(opkg_conf_t *conf, message_level_t level,
388 char *msg)
389 {
390 if (conf && (conf->verbosity < level)) {
391 return 0;
392 } else {
393 if ( level == OPKG_ERROR ){
394 push_error_list(&error_list, msg);
395 } else
396 printf(msg);
397 }
398 return 0;
399 }
400
401 int default_opkg_list_callback(char *name, char *desc, char *version,
402 pkg_state_status_t status, void *userdata)
403 {
404 if (desc)
405 printf("%s - %s - %s\n", name, version, desc);
406 else
407 printf("%s - %s\n", name, version);
408 return 0;
409 }
410
411 int default_opkg_files_callback(char *name, char *desc, char *version,
412 pkg_state_status_t status, void *userdata)
413 {
414 if (desc)
415 printf("%s\n", desc);
416 return 0;
417 }
418
419 int default_opkg_status_callback(char *name, int istatus, char *desc,
420 void *userdata)
421 {
422 printf("%s\n", desc);
423 return 0;
424 }
425
426 char* default_opkg_response_callback(char *question)
427 {
428 char *response = NULL;
429 printf(question);
430 fflush(stdout);
431 do {
432 response = (char *)file_read_line_alloc(stdin);
433 } while (response == NULL);
434 return response;
435 }
436
437 /* This is used for backward compatibility */
438 int
439 opkg_op (int argc, char *argv[])
440 {
441 int err, optind;
442 args_t args;
443 char *cmd_name;
444 opkg_cmd_t *cmd;
445 opkg_conf_t opkg_conf;
446
447 args_init (&args);
448
449 optind = args_parse (&args, argc, argv);
450 if (optind == argc || optind < 0)
451 {
452 args_usage ("opkg must have one sub-command argument");
453 }
454
455 cmd_name = argv[optind++];
456 /* Pigi: added a flag to disable the checking of structures if the command does not need to
457 read anything from there.
458 */
459 if ( !strcmp(cmd_name,"print-architecture") ||
460 !strcmp(cmd_name,"print_architecture") ||
461 !strcmp(cmd_name,"print-installation-architecture") ||
462 !strcmp(cmd_name,"print_installation_architecture") )
463 args.nocheckfordirorfile = 1;
464
465 /* Pigi: added a flag to disable the reading of feed files if the command does not need to
466 read anything from there.
467 */
468 if ( !strcmp(cmd_name,"flag") ||
469 !strcmp(cmd_name,"configure") ||
470 !strcmp(cmd_name,"remove") ||
471 !strcmp(cmd_name,"files") ||
472 !strcmp(cmd_name,"search") ||
473 !strcmp(cmd_name,"compare_versions") ||
474 !strcmp(cmd_name,"compare-versions") ||
475 !strcmp(cmd_name,"list_installed") ||
476 !strcmp(cmd_name,"list-installed") ||
477 !strcmp(cmd_name,"status") )
478 args.noreadfeedsfile = 1;
479
480 opkg_cb_message = default_opkg_message_callback;
481 opkg_cb_response = default_opkg_response_callback;
482 opkg_cb_status = default_opkg_status_callback;
483
484
485 err = opkg_conf_init (&opkg_conf, &args);
486 if (err)
487 {
488 return err;
489 }
490
491 args_deinit (&args);
492
493 if ( strcmp(cmd_name, "files")==0)
494 opkg_cb_list = default_opkg_files_callback;
495 else
496 opkg_cb_list = default_opkg_list_callback;
497
498 cmd = opkg_cmd_find (cmd_name);
499 if (cmd == NULL)
500 {
501 fprintf (stderr, "%s: unknown sub-command %s\n", argv[0],
502 cmd_name);
503 args_usage (NULL);
504 }
505
506 if (cmd->requires_args && optind == argc)
507 {
508 fprintf (stderr,
509 "%s: the ``%s'' command requires at least one argument\n",
510 __FUNCTION__, cmd_name);
511 args_usage (NULL);
512 }
513
514 err = opkg_cmd_exec (cmd, &opkg_conf, argc - optind, (const char **) (argv + optind), NULL);
515
516 opkg_conf_deinit (&opkg_conf);
517
518 return err;
519 }