opkg: re-arrange source code into sub-directories
[project/opkg-lede.git] / libopkg / opkg_cmd.c
1 /* opkg_cmd.c - the itsy package management system
2
3 Carl D. Worth
4
5 Copyright (C) 2001 University of Southern California
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 <string.h>
19
20 #include "opkg.h"
21 #include <libgen.h>
22 #include <glob.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <dirent.h>
29
30 #include "opkg_conf.h"
31 #include "opkg_cmd.h"
32 #include "opkg_message.h"
33 #include "pkg.h"
34 #include "pkg_dest.h"
35 #include "pkg_parse.h"
36 #include "sprintf_alloc.h"
37 #include "pkg.h"
38 #include "file_util.h"
39 #include "str_util.h"
40 #include "libbb/libbb.h"
41
42 #include <fnmatch.h>
43
44
45 #include "opkg_download.h"
46 #include "opkg_install.h"
47 #include "opkg_upgrade.h"
48 #include "opkg_remove.h"
49 #include "opkg_configure.h"
50 #include "opkg_message.h"
51
52 #ifdef OPKG_LIB
53 #include "libopkg.h"
54 static void *p_userdata = NULL;
55 #endif
56
57 static int opkg_update_cmd(opkg_conf_t *conf, int argc, char **argv);
58 static int opkg_upgrade_cmd(opkg_conf_t *conf, int argc, char **argv);
59 static int opkg_list_cmd(opkg_conf_t *conf, int argc, char **argv);
60 static int opkg_info_cmd(opkg_conf_t *conf, int argc, char **argv);
61 static int opkg_status_cmd(opkg_conf_t *conf, int argc, char **argv);
62 static int opkg_install_pending_cmd(opkg_conf_t *conf, int argc, char **argv);
63 static int opkg_install_cmd(opkg_conf_t *conf, int argc, char **argv);
64 static int opkg_list_installed_cmd(opkg_conf_t *conf, int argc, char **argv);
65 static int opkg_remove_cmd(opkg_conf_t *conf, int argc, char **argv);
66 static int opkg_purge_cmd(opkg_conf_t *conf, int argc, char **argv);
67 static int opkg_flag_cmd(opkg_conf_t *conf, int argc, char **argv);
68 static int opkg_files_cmd(opkg_conf_t *conf, int argc, char **argv);
69 static int opkg_search_cmd(opkg_conf_t *conf, int argc, char **argv);
70 static int opkg_download_cmd(opkg_conf_t *conf, int argc, char **argv);
71 static int opkg_depends_cmd(opkg_conf_t *conf, int argc, char **argv);
72 static int opkg_whatdepends_cmd(opkg_conf_t *conf, int argc, char **argv);
73 static int opkg_whatdepends_recursively_cmd(opkg_conf_t *conf, int argc, char **argv);
74 static int opkg_whatsuggests_cmd(opkg_conf_t *conf, int argc, char **argv);
75 static int opkg_whatrecommends_cmd(opkg_conf_t *conf, int argc, char **argv);
76 static int opkg_whatprovides_cmd(opkg_conf_t *conf, int argc, char **argv);
77 static int opkg_whatconflicts_cmd(opkg_conf_t *conf, int argc, char **argv);
78 static int opkg_whatreplaces_cmd(opkg_conf_t *conf, int argc, char **argv);
79 static int opkg_compare_versions_cmd(opkg_conf_t *conf, int argc, char **argv);
80 static int opkg_print_architecture_cmd(opkg_conf_t *conf, int argc, char **argv);
81 static int opkg_configure_cmd(opkg_conf_t *conf, int argc, char **argv);
82
83 /* XXX: CLEANUP: The usage strings should be incorporated into this
84 array for easier maintenance */
85 static opkg_cmd_t cmds[] = {
86 {"update", 0, (opkg_cmd_fun_t)opkg_update_cmd},
87 {"upgrade", 0, (opkg_cmd_fun_t)opkg_upgrade_cmd},
88 {"list", 0, (opkg_cmd_fun_t)opkg_list_cmd},
89 {"list_installed", 0, (opkg_cmd_fun_t)opkg_list_installed_cmd},
90 {"info", 0, (opkg_cmd_fun_t)opkg_info_cmd},
91 {"flag", 1, (opkg_cmd_fun_t)opkg_flag_cmd},
92 {"status", 0, (opkg_cmd_fun_t)opkg_status_cmd},
93 {"install_pending", 0, (opkg_cmd_fun_t)opkg_install_pending_cmd},
94 {"install", 1, (opkg_cmd_fun_t)opkg_install_cmd},
95 {"remove", 1, (opkg_cmd_fun_t)opkg_remove_cmd},
96 {"purge", 1, (opkg_cmd_fun_t)opkg_purge_cmd},
97 {"configure", 0, (opkg_cmd_fun_t)opkg_configure_cmd},
98 {"files", 1, (opkg_cmd_fun_t)opkg_files_cmd},
99 {"search", 1, (opkg_cmd_fun_t)opkg_search_cmd},
100 {"download", 1, (opkg_cmd_fun_t)opkg_download_cmd},
101 {"compare_versions", 1, (opkg_cmd_fun_t)opkg_compare_versions_cmd},
102 {"compare-versions", 1, (opkg_cmd_fun_t)opkg_compare_versions_cmd},
103 {"print-architecture", 0, (opkg_cmd_fun_t)opkg_print_architecture_cmd},
104 {"print_architecture", 0, (opkg_cmd_fun_t)opkg_print_architecture_cmd},
105 {"print-installation-architecture", 0, (opkg_cmd_fun_t)opkg_print_architecture_cmd},
106 {"print_installation_architecture", 0, (opkg_cmd_fun_t)opkg_print_architecture_cmd},
107 {"depends", 1, (opkg_cmd_fun_t)opkg_depends_cmd},
108 {"whatdepends", 1, (opkg_cmd_fun_t)opkg_whatdepends_cmd},
109 {"whatdependsrec", 1, (opkg_cmd_fun_t)opkg_whatdepends_recursively_cmd},
110 {"whatrecommends", 1, (opkg_cmd_fun_t)opkg_whatrecommends_cmd},
111 {"whatsuggests", 1, (opkg_cmd_fun_t)opkg_whatsuggests_cmd},
112 {"whatprovides", 1, (opkg_cmd_fun_t)opkg_whatprovides_cmd},
113 {"whatreplaces", 1, (opkg_cmd_fun_t)opkg_whatreplaces_cmd},
114 {"whatconflicts", 1, (opkg_cmd_fun_t)opkg_whatconflicts_cmd},
115 };
116
117 int opkg_state_changed;
118 static void write_status_files_if_changed(opkg_conf_t *conf)
119 {
120 if (opkg_state_changed && !conf->noaction) {
121 opkg_message(conf, OPKG_INFO,
122 " writing status file\n");
123 opkg_conf_write_status_files(conf);
124 pkg_write_changed_filelists(conf);
125 } else {
126 opkg_message(conf, OPKG_NOTICE, "Nothing to be done\n");
127 }
128 }
129
130
131 static int num_cmds = sizeof(cmds) / sizeof(opkg_cmd_t);
132
133 opkg_cmd_t *opkg_cmd_find(const char *name)
134 {
135 int i;
136 opkg_cmd_t *cmd;
137
138 for (i=0; i < num_cmds; i++) {
139 cmd = &cmds[i];
140 if (strcmp(name, cmd->name) == 0) {
141 return cmd;
142 }
143 }
144
145 return NULL;
146 }
147
148 #ifdef OPKG_LIB
149 int opkg_cmd_exec(opkg_cmd_t *cmd, opkg_conf_t *conf, int argc, const char **argv, void *userdata)
150 {
151 int result;
152 p_userdata = userdata;
153
154
155 result = (cmd->fun)(conf, argc, argv);
156
157 if ( result != 0 ) {
158 opkg_message(conf, OPKG_NOTICE, "An error ocurred, return value: %d.\n", result);
159 }
160
161 if ( error_list ) {
162 reverse_error_list(&error_list);
163
164 opkg_message(conf, OPKG_NOTICE, "Collected errors:\n");
165 /* Here we print the errors collected and free the list */
166 while (error_list != NULL) {
167 opkg_message(conf, OPKG_NOTICE, "%s",error_list->errmsg);
168 error_list = error_list->next;
169
170 }
171 free_error_list(&error_list);
172
173 }
174
175 p_userdata = NULL;
176 return result;
177 }
178 #else
179 int opkg_cmd_exec(opkg_cmd_t *cmd, opkg_conf_t *conf, int argc, const char **argv)
180 {
181 return (cmd->fun)(conf, argc, argv);
182 }
183 #endif
184
185 static int opkg_update_cmd(opkg_conf_t *conf, int argc, char **argv)
186 {
187 int err;
188 int failures;
189 char *lists_dir;
190 pkg_src_list_elt_t *iter;
191 pkg_src_t *src;
192
193
194 sprintf_alloc(&lists_dir, "%s", conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir);
195
196 if (! file_is_dir(lists_dir)) {
197 if (file_exists(lists_dir)) {
198 opkg_message(conf, OPKG_ERROR,
199 "%s: ERROR: %s exists, but is not a directory\n",
200 __FUNCTION__, lists_dir);
201 free(lists_dir);
202 return EINVAL;
203 }
204 err = file_mkdir_hier(lists_dir, 0755);
205 if (err) {
206 opkg_message(conf, OPKG_ERROR,
207 "%s: ERROR: failed to make directory %s: %s\n",
208 __FUNCTION__, lists_dir, strerror(errno));
209 free(lists_dir);
210 return EINVAL;
211 }
212 }
213
214 failures = 0;
215 for (iter = conf->pkg_src_list.head; iter; iter = iter->next) {
216 char *url, *list_file_name;
217
218 src = iter->data;
219
220 if (src->extra_data) /* debian style? */
221 sprintf_alloc(&url, "%s/%s/%s", src->value, src->extra_data,
222 src->gzip ? "Packages.gz" : "Packages");
223 else
224 sprintf_alloc(&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
225
226 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
227 if (src->gzip) {
228 char *tmp;
229 char *tmp_file_name;
230 FILE *in, *out;
231
232 tmp = strdup ("/tmp/opkg.XXXXXX");
233
234 if (mkdtemp (tmp) == NULL) {
235 perror ("mkdtemp");
236 failures++;
237 continue;
238 }
239
240 sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
241 err = opkg_download(conf, url, tmp_file_name);
242 if (err == 0) {
243 opkg_message (conf, OPKG_NOTICE, "Inflating %s\n", url);
244 in = fopen (tmp_file_name, "r");
245 out = fopen (list_file_name, "w");
246 if (in && out)
247 unzip (in, out);
248 else
249 err = 1;
250 if (in)
251 fclose (in);
252 if (out)
253 fclose (out);
254 unlink (tmp_file_name);
255 rmdir (tmp);
256 free (tmp);
257 }
258 } else
259 err = opkg_download(conf, url, list_file_name);
260 if (err) {
261 failures++;
262 } else {
263 opkg_message(conf, OPKG_NOTICE,
264 "Updated list of available packages in %s\n",
265 list_file_name);
266 }
267 free(url);
268
269 /* download detached signitures to verify the package lists */
270 /* get the url for the sig file */
271 if (src->extra_data) /* debian style? */
272 sprintf_alloc(&url, "%s/%s/%s", src->value, src->extra_data,
273 "Packages.sig");
274 else
275 sprintf_alloc(&url, "%s/%s", src->value, "Packages.sig");
276
277 /* create temporary dir for it */
278 char *tmp, *tmp_file_name;
279 tmp = strdup ("/tmp/opkg.XXXXXX");
280 if (mkdtemp (tmp) == NULL) {
281 perror ("mkdtemp");
282 failures++;
283 continue;
284 }
285 sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig");
286
287 err = opkg_download(conf, url, tmp_file_name);
288 if (err) {
289 failures++;
290 opkg_message (conf, OPKG_NOTICE, "Signature check failed\n");
291 } else {
292 int err;
293 err = opkg_verify_file (list_file_name, tmp_file_name);
294 if (err == 0)
295 opkg_message (conf, OPKG_NOTICE, "Signature check passed\n");
296 else
297 opkg_message (conf, OPKG_NOTICE, "Signature check failed\n");
298 }
299 unlink (tmp_file_name);
300 unlink (tmp);
301 free (tmp_file_name);
302
303 free (url);
304 free(list_file_name);
305 }
306 free(lists_dir);
307
308 #ifdef CONFIG_CLEAR_SW_INSTALL_FLAG
309 #warning here
310 /* clear SW_INSTALL on any package where state is SS_NOT_INSTALLED.
311 * this is a hack to work around poor bookkeeping in old opkg upgrade code
312 * -Jamey 3/1/03
313 */
314 {
315 int i;
316 int changed = 0;
317 pkg_vec_t *available = pkg_vec_alloc();
318 pkg_hash_fetch_available(&conf->pkg_hash, available);
319 opkg_message(conf, OPKG_DEBUG, "Clearing SW_INSTALL for SS_NOT_INSTALLED packages.\n");
320 for (i = 0; i < available->len; i++) {
321 pkg_t *pkg = available->pkgs[i];
322 if (pkg->state_want == SW_INSTALL && pkg->state_status == SS_NOT_INSTALLED) {
323 opkg_message(conf, OPKG_DEBUG, "Clearing SW_INSTALL on package %s.\n", pkg->name);
324 pkg->state_want = SW_UNKNOWN;
325 changed = 1;
326 }
327 }
328 pkg_vec_free(available);
329 if (changed) {
330 write_status_files_if_changed(conf);
331 }
332 }
333 #endif
334
335 return failures;
336 }
337
338
339 /* scan the args passed and cache the local filenames of the packages */
340 int opkg_multiple_files_scan(opkg_conf_t *conf, int argc, char **argv)
341 {
342 int i;
343 int err;
344
345 /*
346 * First scan through package names/urls
347 * For any urls, download the packages and install in database.
348 * For any files, install package info in database.
349 */
350 for (i = 0; i < argc; i ++) {
351 char *filename = argv [i];
352 //char *tmp = basename (tmp);
353 //int tmplen = strlen (tmp);
354
355 //if (strcmp (tmp + (tmplen - strlen (OPKG_PKG_EXTENSION)), OPKG_PKG_EXTENSION) != 0)
356 // continue;
357 //if (strcmp (tmp + (tmplen - strlen (DPKG_PKG_EXTENSION)), DPKG_PKG_EXTENSION) != 0)
358 // continue;
359
360 opkg_message(conf, OPKG_DEBUG2, "Debug mfs: %s \n",filename );
361
362 err = opkg_prepare_url_for_install(conf, filename, &argv[i]);
363 if (err)
364 return err;
365 }
366 return 0;
367 }
368
369 struct opkg_intercept
370 {
371 char *oldpath;
372 char *statedir;
373 };
374
375 typedef struct opkg_intercept *opkg_intercept_t;
376
377 opkg_intercept_t opkg_prep_intercepts(opkg_conf_t *conf)
378 {
379 opkg_intercept_t ctx;
380 char *newpath;
381 int gen;
382
383 ctx = malloc (sizeof (*ctx));
384 ctx->oldpath = strdup (getenv ("PATH"));
385
386 sprintf_alloc (&newpath, "%s/opkg/intercept:%s", DATADIR, ctx->oldpath);
387 setenv ("PATH", newpath, 1);
388 free (newpath);
389
390 gen = 0;
391 retry:
392 sprintf_alloc (&ctx->statedir, "/tmp/opkg-intercept-%d-%d", getpid (), gen);
393 if (mkdir (ctx->statedir, 0770) < 0) {
394 if (errno == EEXIST) {
395 free (ctx->statedir);
396 gen++;
397 goto retry;
398 }
399 perror (ctx->statedir);
400 return NULL;
401 }
402 setenv ("OPKG_INTERCEPT_DIR", ctx->statedir, 1);
403 return ctx;
404 }
405
406 int opkg_finalize_intercepts(opkg_intercept_t ctx)
407 {
408 char *cmd;
409 DIR *dir;
410 int err = 0;
411
412 setenv ("PATH", ctx->oldpath, 1);
413 free (ctx->oldpath);
414
415 dir = opendir (ctx->statedir);
416 if (dir) {
417 struct dirent *de;
418 while (de = readdir (dir), de != NULL) {
419 char *path;
420
421 if (de->d_name[0] == '.')
422 continue;
423
424 sprintf_alloc (&path, "%s/%s", ctx->statedir, de->d_name);
425 if (access (path, X_OK) == 0) {
426 if (system (path)) {
427 err = errno;
428 perror (de->d_name);
429 }
430 }
431 free (path);
432 }
433 } else
434 perror (ctx->statedir);
435
436 sprintf_alloc (&cmd, "rm -rf %s", ctx->statedir);
437 system (cmd);
438 free (cmd);
439
440 free (ctx->statedir);
441 free (ctx);
442
443 return err;
444 }
445
446 int opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
447 {
448 pkg_vec_t *all;
449 int i;
450 pkg_t *pkg;
451 opkg_intercept_t ic;
452 int r, err = 0;
453
454 opkg_message(conf, OPKG_INFO,
455 "Configuring unpacked packages\n");
456 fflush( stdout );
457
458 all = pkg_vec_alloc();
459 pkg_hash_fetch_available(&conf->pkg_hash, all);
460
461 ic = opkg_prep_intercepts (conf);
462
463 for(i = 0; i < all->len; i++) {
464 pkg = all->pkgs[i];
465
466 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
467 continue;
468
469 if (pkg->state_status == SS_UNPACKED) {
470 opkg_message(conf, OPKG_NOTICE,
471 "Configuring %s\n", pkg->name);
472 fflush( stdout );
473 r = opkg_configure(conf, pkg);
474 if (r == 0) {
475 pkg->state_status = SS_INSTALLED;
476 pkg->parent->state_status = SS_INSTALLED;
477 pkg->state_flag &= ~SF_PREFER;
478 } else {
479 if (!err)
480 err = r;
481 }
482 }
483 }
484
485 r = opkg_finalize_intercepts (ic);
486 if (r && !err)
487 err = r;
488
489 pkg_vec_free(all);
490 return err;
491 }
492
493 static opkg_conf_t *global_conf;
494
495 static void sigint_handler(int sig)
496 {
497 signal(sig, SIG_DFL);
498 opkg_message(NULL, OPKG_NOTICE,
499 "opkg: interrupted. writing out status database\n");
500 write_status_files_if_changed(global_conf);
501 exit(128 + sig);
502 }
503
504 static int opkg_install_cmd(opkg_conf_t *conf, int argc, char **argv)
505 {
506 int i;
507 char *arg;
508 int err=0;
509
510 global_conf = conf;
511 signal(SIGINT, sigint_handler);
512
513 /*
514 * Now scan through package names and install
515 */
516 for (i=0; i < argc; i++) {
517 arg = argv[i];
518
519 opkg_message(conf, OPKG_DEBUG2, "Debug install_cmd: %s \n",arg );
520 err = opkg_prepare_url_for_install(conf, arg, &argv[i]);
521 if (err != EINVAL && err != 0)
522 return err;
523 }
524 pkg_info_preinstall_check(conf);
525
526 for (i=0; i < argc; i++) {
527 arg = argv[i];
528 if (conf->multiple_providers)
529 err = opkg_install_multi_by_name(conf, arg);
530 else{
531 err = opkg_install_by_name(conf, arg);
532 }
533 if (err == OPKG_PKG_HAS_NO_CANDIDATE) {
534 opkg_message(conf, OPKG_ERROR,
535 "Cannot find package %s.\n"
536 "Check the spelling or perhaps run 'opkg update'\n",
537 arg);
538 }
539 }
540
541 /* recheck to verify that all dependences are satisfied */
542 if (0) opkg_satisfy_all_dependences(conf);
543
544 opkg_configure_packages(conf, NULL);
545
546 write_status_files_if_changed(conf);
547
548 return err;
549 }
550
551 static int opkg_upgrade_cmd(opkg_conf_t *conf, int argc, char **argv)
552 {
553 int i;
554 pkg_t *pkg;
555 int err;
556
557 global_conf = conf;
558 signal(SIGINT, sigint_handler);
559
560 if (argc) {
561 for (i=0; i < argc; i++) {
562 char *arg = argv[i];
563
564 err = opkg_prepare_url_for_install(conf, arg, &arg);
565 if (err != EINVAL && err != 0)
566 return err;
567 }
568 pkg_info_preinstall_check(conf);
569
570 for (i=0; i < argc; i++) {
571 char *arg = argv[i];
572 if (conf->restrict_to_default_dest) {
573 pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
574 argv[i],
575 conf->default_dest);
576 if (pkg == NULL) {
577 opkg_message(conf, OPKG_NOTICE,
578 "Package %s not installed in %s\n",
579 argv[i], conf->default_dest->name);
580 continue;
581 }
582 } else {
583 pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
584 argv[i]);
585 }
586 if (pkg)
587 opkg_upgrade_pkg(conf, pkg);
588 else {
589 opkg_install_by_name(conf, arg);
590 }
591 }
592 } else {
593 pkg_vec_t *installed = pkg_vec_alloc();
594
595 pkg_info_preinstall_check(conf);
596
597 pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
598 for (i = 0; i < installed->len; i++) {
599 pkg = installed->pkgs[i];
600 opkg_upgrade_pkg(conf, pkg);
601 }
602 pkg_vec_free(installed);
603 }
604
605 /* recheck to verify that all dependences are satisfied */
606 if (0) opkg_satisfy_all_dependences(conf);
607
608 opkg_configure_packages(conf, NULL);
609
610 write_status_files_if_changed(conf);
611
612 return 0;
613 }
614
615 static int opkg_download_cmd(opkg_conf_t *conf, int argc, char **argv)
616 {
617 int i, err;
618 char *arg;
619 pkg_t *pkg;
620
621 pkg_info_preinstall_check(conf);
622 for (i = 0; i < argc; i++) {
623 arg = argv[i];
624
625 pkg = pkg_hash_fetch_best_installation_candidate_by_name(conf, arg);
626 if (pkg == NULL) {
627 opkg_message(conf, OPKG_ERROR,
628 "Cannot find package %s.\n"
629 "Check the spelling or perhaps run 'opkg update'\n",
630 arg);
631 continue;
632 }
633
634 err = opkg_download_pkg(conf, pkg, ".");
635
636 if (err) {
637 opkg_message(conf, OPKG_ERROR,
638 "Failed to download %s\n", pkg->name);
639 } else {
640 opkg_message(conf, OPKG_NOTICE,
641 "Downloaded %s as %s\n",
642 pkg->name, pkg->local_filename);
643 }
644 }
645
646 return 0;
647 }
648
649
650 static int opkg_list_cmd(opkg_conf_t *conf, int argc, char **argv)
651 {
652 int i ;
653 pkg_vec_t *available;
654 pkg_t *pkg;
655 char desc_short[OPKG_LIST_DESCRIPTION_LENGTH];
656 char *newline;
657 char *pkg_name = NULL;
658 char *version_str;
659
660 if (argc > 0) {
661 pkg_name = argv[0];
662 }
663 available = pkg_vec_alloc();
664 pkg_hash_fetch_available(&conf->pkg_hash, available);
665 for (i=0; i < available->len; i++) {
666 pkg = available->pkgs[i];
667 /* if we have package name or pattern and pkg does not match, then skip it */
668 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
669 continue;
670 if (pkg->description) {
671 strncpy(desc_short, pkg->description, OPKG_LIST_DESCRIPTION_LENGTH);
672 } else {
673 desc_short[0] = '\0';
674 }
675 desc_short[OPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
676 newline = strchr(desc_short, '\n');
677 if (newline) {
678 *newline = '\0';
679 }
680 #ifndef OPKG_LIB
681 printf("%s - %s\n", pkg->name, desc_short);
682 #else
683 if (opkg_cb_list) {
684 version_str = pkg_version_str_alloc(pkg);
685 opkg_cb_list(pkg->name,desc_short,
686 version_str,
687 pkg->state_status,
688 p_userdata);
689 free(version_str);
690 }
691 #endif
692 }
693 pkg_vec_free(available);
694
695 return 0;
696 }
697
698
699 static int opkg_list_installed_cmd(opkg_conf_t *conf, int argc, char **argv)
700 {
701 int i ;
702 pkg_vec_t *available;
703 pkg_t *pkg;
704 char desc_short[OPKG_LIST_DESCRIPTION_LENGTH];
705 char *newline;
706 char *pkg_name = NULL;
707 char *version_str;
708
709 if (argc > 0) {
710 pkg_name = argv[0];
711 }
712 available = pkg_vec_alloc();
713 pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
714 for (i=0; i < available->len; i++) {
715 pkg = available->pkgs[i];
716 /* if we have package name or pattern and pkg does not match, then skip it */
717 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
718 continue;
719 if (pkg->description) {
720 strncpy(desc_short, pkg->description, OPKG_LIST_DESCRIPTION_LENGTH);
721 } else {
722 desc_short[0] = '\0';
723 }
724 desc_short[OPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
725 newline = strchr(desc_short, '\n');
726 if (newline) {
727 *newline = '\0';
728 }
729 #ifndef OPKG_LIB
730 printf("%s - %s\n", pkg->name, desc_short);
731 #else
732 if (opkg_cb_list) {
733 version_str = pkg_version_str_alloc(pkg);
734 opkg_cb_list(pkg->name,desc_short,
735 version_str,
736 pkg->state_status,
737 p_userdata);
738 free(version_str);
739 }
740 #endif
741 }
742
743 return 0;
744 }
745
746 static int opkg_info_status_cmd(opkg_conf_t *conf, int argc, char **argv, int installed_only)
747 {
748 int i;
749 pkg_vec_t *available;
750 pkg_t *pkg;
751 char *pkg_name = NULL;
752 char **pkg_fields = NULL;
753 int n_fields = 0;
754 char *buff ; // = (char *)malloc(1);
755
756 if (argc > 0) {
757 pkg_name = argv[0];
758 }
759 if (argc > 1) {
760 pkg_fields = &argv[1];
761 n_fields = argc - 1;
762 }
763
764 available = pkg_vec_alloc();
765 if (installed_only)
766 pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
767 else
768 pkg_hash_fetch_available(&conf->pkg_hash, available);
769 for (i=0; i < available->len; i++) {
770 pkg = available->pkgs[i];
771 if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
772 continue;
773 }
774 #ifndef OPKG_LIB
775 if (n_fields) {
776 for (j = 0; j < n_fields; j++)
777 pkg_print_field(pkg, stdout, pkg_fields[j]);
778 } else {
779 pkg_print_info(pkg, stdout);
780 }
781 #else
782
783 buff = pkg_formatted_info(pkg);
784 if ( buff ) {
785 if (opkg_cb_status) opkg_cb_status(pkg->name,
786 pkg->state_status,
787 buff,
788 p_userdata);
789 /*
790 We should not forget that actually the pointer is allocated.
791 We need to free it :) ( Thanks florian for seeing the error )
792 */
793 free(buff);
794 }
795 #endif
796 if (conf->verbosity > 1) {
797 conffile_list_elt_t *iter;
798 for (iter = pkg->conffiles.head; iter; iter = iter->next) {
799 conffile_t *cf = iter->data;
800 int modified = conffile_has_been_modified(conf, cf);
801 opkg_message(conf, OPKG_NOTICE, "conffile=%s md5sum=%s modified=%d\n",
802 cf->name, cf->value, modified);
803 }
804 }
805 }
806 #ifndef OPKG_LIB
807 if (buff)
808 free(buff);
809 #endif
810 pkg_vec_free(available);
811
812 return 0;
813 }
814
815 static int opkg_info_cmd(opkg_conf_t *conf, int argc, char **argv)
816 {
817 return opkg_info_status_cmd(conf, argc, argv, 0);
818 }
819
820 static int opkg_status_cmd(opkg_conf_t *conf, int argc, char **argv)
821 {
822 return opkg_info_status_cmd(conf, argc, argv, 1);
823 }
824
825 static int opkg_configure_cmd(opkg_conf_t *conf, int argc, char **argv)
826 {
827
828 int err;
829 if (argc > 0) {
830 char *pkg_name = NULL;
831
832 pkg_name = argv[0];
833
834 err = opkg_configure_packages (conf, pkg_name);
835
836 } else {
837 err = opkg_configure_packages (conf, NULL);
838 }
839
840 write_status_files_if_changed(conf);
841
842 return err;
843 }
844
845 static int opkg_install_pending_cmd(opkg_conf_t *conf, int argc, char **argv)
846 {
847 int i, err;
848 char *globpattern;
849 glob_t globbuf;
850
851 sprintf_alloc(&globpattern, "%s/*" OPKG_PKG_EXTENSION, conf->pending_dir);
852 err = glob(globpattern, 0, NULL, &globbuf);
853 free(globpattern);
854 if (err) {
855 return 0;
856 }
857
858 opkg_message(conf, OPKG_NOTICE,
859 "The following packages in %s will now be installed.\n",
860 conf->pending_dir);
861 for (i = 0; i < globbuf.gl_pathc; i++) {
862 opkg_message(conf, OPKG_NOTICE,
863 "%s%s", i == 0 ? "" : " ", globbuf.gl_pathv[i]);
864 }
865 opkg_message(conf, OPKG_NOTICE, "\n");
866 for (i = 0; i < globbuf.gl_pathc; i++) {
867 err = opkg_install_from_file(conf, globbuf.gl_pathv[i]);
868 if (err == 0) {
869 err = unlink(globbuf.gl_pathv[i]);
870 if (err) {
871 opkg_message(conf, OPKG_ERROR,
872 "%s: ERROR: failed to unlink %s: %s\n",
873 __FUNCTION__, globbuf.gl_pathv[i], strerror(err));
874 return err;
875 }
876 }
877 }
878 globfree(&globbuf);
879
880 return err;
881 }
882
883 static int opkg_remove_cmd(opkg_conf_t *conf, int argc, char **argv)
884 {
885 int i,a,done;
886 pkg_t *pkg;
887 pkg_t *pkg_to_remove;
888 pkg_vec_t *available;
889 char *pkg_name = NULL;
890 global_conf = conf;
891 signal(SIGINT, sigint_handler);
892
893 // ENH: Add the "no pkg removed" just in case.
894
895 done = 0;
896
897 available = pkg_vec_alloc();
898 pkg_info_preinstall_check(conf);
899 if ( argc > 0 ) {
900 pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
901 for (i=0; i < argc; i++) {
902 pkg_name = malloc(strlen(argv[i])+2);
903 strcpy(pkg_name,argv[i]);
904 for (a=0; a < available->len; a++) {
905 pkg = available->pkgs[a];
906 if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
907 continue;
908 }
909 if (conf->restrict_to_default_dest) {
910 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
911 pkg->name,
912 conf->default_dest);
913 } else {
914 pkg_to_remove = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name );
915 }
916
917 if (pkg == NULL) {
918 opkg_message(conf, OPKG_ERROR, "Package %s is not installed.\n", pkg->name);
919 continue;
920 }
921 if (pkg->state_status == SS_NOT_INSTALLED) { // Added the control, so every already removed package could be skipped
922 opkg_message(conf, OPKG_ERROR, "Package seems to be %s not installed (STATUS = NOT_INSTALLED).\n", pkg->name);
923 continue;
924 }
925 opkg_remove_pkg(conf, pkg_to_remove,0);
926 done = 1;
927 }
928 free (pkg_name);
929 }
930 pkg_vec_free(available);
931 } else {
932 pkg_vec_t *installed_pkgs = pkg_vec_alloc();
933 int i;
934 int flagged_pkg_count = 0;
935 int removed;
936
937 pkg_hash_fetch_all_installed(&conf->pkg_hash, installed_pkgs);
938
939 for (i = 0; i < installed_pkgs->len; i++) {
940 pkg_t *pkg = installed_pkgs->pkgs[i];
941 if (pkg->state_flag & SF_USER) {
942 flagged_pkg_count++;
943 } else {
944 if (!pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL))
945 opkg_message(conf, OPKG_NOTICE, "Non-user leaf package: %s\n", pkg->name);
946 }
947 }
948 if (!flagged_pkg_count) {
949 opkg_message(conf, OPKG_NOTICE, "No packages flagged as installed by user, \n"
950 "so refusing to uninstall unflagged non-leaf packages\n");
951 return 0;
952 }
953
954 /* find packages not flagged SF_USER (i.e., installed to
955 * satisfy a dependence) and not having any dependents, and
956 * remove them */
957 do {
958 removed = 0;
959 for (i = 0; i < installed_pkgs->len; i++) {
960 pkg_t *pkg = installed_pkgs->pkgs[i];
961 if (!(pkg->state_flag & SF_USER)
962 && !pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL)) {
963 removed++;
964 opkg_message(conf, OPKG_NOTICE, "Removing non-user leaf package %s\n");
965 opkg_remove_pkg(conf, pkg,0);
966 done = 1;
967 }
968 }
969 } while (removed);
970 pkg_vec_free(installed_pkgs);
971 }
972
973 if ( done == 0 )
974 opkg_message(conf, OPKG_NOTICE, "No packages removed.\n");
975
976 write_status_files_if_changed(conf);
977 return 0;
978 }
979
980 static int opkg_purge_cmd(opkg_conf_t *conf, int argc, char **argv)
981 {
982 int i;
983 pkg_t *pkg;
984
985 global_conf = conf;
986 signal(SIGINT, sigint_handler);
987
988 pkg_info_preinstall_check(conf);
989
990 for (i=0; i < argc; i++) {
991 if (conf->restrict_to_default_dest) {
992 pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
993 argv[i],
994 conf->default_dest);
995 } else {
996 pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
997 }
998
999 if (pkg == NULL) {
1000 opkg_message(conf, OPKG_ERROR,
1001 "Package %s is not installed.\n", argv[i]);
1002 continue;
1003 }
1004 opkg_purge_pkg(conf, pkg);
1005 }
1006
1007 write_status_files_if_changed(conf);
1008 return 0;
1009 }
1010
1011 static int opkg_flag_cmd(opkg_conf_t *conf, int argc, char **argv)
1012 {
1013 int i;
1014 pkg_t *pkg;
1015 const char *flags = argv[0];
1016
1017 global_conf = conf;
1018 signal(SIGINT, sigint_handler);
1019
1020 for (i=1; i < argc; i++) {
1021 if (conf->restrict_to_default_dest) {
1022 pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
1023 argv[i],
1024 conf->default_dest);
1025 } else {
1026 pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
1027 }
1028
1029 if (pkg == NULL) {
1030 opkg_message(conf, OPKG_ERROR,
1031 "Package %s is not installed.\n", argv[i]);
1032 continue;
1033 }
1034 if (( strcmp(flags,"hold")==0)||( strcmp(flags,"noprune")==0)||
1035 ( strcmp(flags,"user")==0)||( strcmp(flags,"ok")==0)) {
1036 pkg->state_flag = pkg_state_flag_from_str(flags);
1037 }
1038 /* pb_ asked this feature 03292004 */
1039 /* Actually I will use only this two, but this is an open for various status */
1040 if (( strcmp(flags,"installed")==0)||( strcmp(flags,"unpacked")==0)){
1041 pkg->state_status = pkg_state_status_from_str(flags);
1042 }
1043 opkg_state_changed++;
1044 opkg_message(conf, OPKG_NOTICE,
1045 "Setting flags for package %s to %s\n",
1046 pkg->name, flags);
1047 }
1048
1049 write_status_files_if_changed(conf);
1050 return 0;
1051 }
1052
1053 static int opkg_files_cmd(opkg_conf_t *conf, int argc, char **argv)
1054 {
1055 pkg_t *pkg;
1056 str_list_t *installed_files;
1057 str_list_elt_t *iter;
1058 char *pkg_version;
1059 size_t buff_len = 8192;
1060 size_t used_len;
1061 char *buff ;
1062
1063 buff = (char *)malloc(buff_len);
1064 if ( buff == NULL ) {
1065 fprintf( stderr,"%s: Unable to allocate memory \n",__FUNCTION__);
1066 return ENOMEM;
1067 }
1068
1069 if (argc < 1) {
1070 return EINVAL;
1071 }
1072
1073 pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
1074 argv[0]);
1075 if (pkg == NULL) {
1076 opkg_message(conf, OPKG_ERROR,
1077 "Package %s not installed.\n", argv[0]);
1078 return 0;
1079 }
1080
1081 installed_files = pkg_get_installed_files(pkg);
1082 pkg_version = pkg_version_str_alloc(pkg);
1083
1084 #ifndef OPKG_LIB
1085 printf("Package %s (%s) is installed on %s and has the following files:\n",
1086 pkg->name, pkg_version, pkg->dest->name);
1087 for (iter = installed_files->head; iter; iter = iter->next) {
1088 puts(iter->data);
1089 }
1090 #else
1091 if (buff) {
1092 try_again:
1093 used_len = snprintf(buff, buff_len, "Package %s (%s) is installed on %s and has the following files:\n",
1094 pkg->name, pkg_version, pkg->dest->name) + 1;
1095 if (used_len > buff_len) {
1096 buff_len *= 2;
1097 buff = realloc (buff, buff_len);
1098 goto try_again;
1099 }
1100 for (iter = installed_files->head; iter; iter = iter->next) {
1101 used_len += strlen (iter->data) + 1;
1102 while (buff_len <= used_len) {
1103 buff_len *= 2;
1104 buff = realloc (buff, buff_len);
1105 }
1106 strncat(buff, iter->data, buff_len);
1107 strncat(buff, "\n", buff_len);
1108 }
1109 if (opkg_cb_list) opkg_cb_list(pkg->name,
1110 buff,
1111 pkg_version_str_alloc(pkg),
1112 pkg->state_status,
1113 p_userdata);
1114 free(buff);
1115 }
1116 #endif
1117
1118 free(pkg_version);
1119 pkg_free_installed_files(pkg);
1120
1121 return 0;
1122 }
1123
1124 static int opkg_depends_cmd(opkg_conf_t *conf, int argc, char **argv)
1125 {
1126
1127 if (argc > 0) {
1128 pkg_vec_t *available_pkgs = pkg_vec_alloc();
1129 const char *rel_str = "depends on";
1130 int i;
1131
1132 pkg_info_preinstall_check(conf);
1133
1134 if (conf->query_all)
1135 pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1136 else
1137 pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1138 for (i = 0; i < argc; i++) {
1139 const char *target = argv[i];
1140 int j;
1141
1142 opkg_message(conf, OPKG_ERROR, "target=%s\n", target);
1143
1144 for (j = 0; j < available_pkgs->len; j++) {
1145 pkg_t *pkg = available_pkgs->pkgs[j];
1146 if (fnmatch(target, pkg->name, 0) == 0) {
1147 int k;
1148 int count = pkg->depends_count + pkg->pre_depends_count;
1149 opkg_message(conf, OPKG_ERROR, "What %s (arch=%s) %s\n",
1150 target, pkg->architecture, rel_str);
1151 for (k = 0; k < count; k++) {
1152 compound_depend_t *cdepend = &pkg->depends[k];
1153 int l;
1154 for (l = 0; l < cdepend->possibility_count; l++) {
1155 depend_t *possibility = cdepend->possibilities[l];
1156 opkg_message(conf, OPKG_ERROR, " %s", possibility->pkg->name);
1157 if (conf->verbosity > 0) {
1158 // char *ver = abstract_pkg_version_str_alloc(possibility->pkg);
1159 opkg_message(conf, OPKG_NOTICE, " %s", possibility->version);
1160 if (possibility->version) {
1161 char *typestr = NULL;
1162 switch (possibility->constraint) {
1163 case NONE: typestr = "none"; break;
1164 case EARLIER: typestr = "<"; break;
1165 case EARLIER_EQUAL: typestr = "<="; break;
1166 case EQUAL: typestr = "="; break;
1167 case LATER_EQUAL: typestr = ">="; break;
1168 case LATER: typestr = ">"; break;
1169 }
1170 opkg_message(conf, OPKG_NOTICE, " (%s %s)", typestr, possibility->version);
1171 }
1172 // free(ver);
1173 }
1174 opkg_message(conf, OPKG_ERROR, "\n");
1175 }
1176 }
1177 }
1178 }
1179 }
1180 pkg_vec_free(available_pkgs);
1181 }
1182 return 0;
1183 }
1184
1185 enum what_field_type {
1186 WHATDEPENDS,
1187 WHATCONFLICTS,
1188 WHATPROVIDES,
1189 WHATREPLACES,
1190 WHATRECOMMENDS,
1191 WHATSUGGESTS
1192 };
1193
1194 static int opkg_what_depends_conflicts_cmd(opkg_conf_t *conf, enum what_field_type what_field_type, int recursive, int argc, char **argv)
1195 {
1196
1197 if (argc > 0) {
1198 pkg_vec_t *available_pkgs = pkg_vec_alloc();
1199 const char *rel_str = NULL;
1200 int i;
1201 int changed;
1202
1203 switch (what_field_type) {
1204 case WHATDEPENDS: rel_str = "depends on"; break;
1205 case WHATCONFLICTS: rel_str = "conflicts with"; break;
1206 case WHATSUGGESTS: rel_str = "suggests"; break;
1207 case WHATRECOMMENDS: rel_str = "recommends"; break;
1208 case WHATPROVIDES: rel_str = "provides"; break;
1209 case WHATREPLACES: rel_str = "replaces"; break;
1210 }
1211
1212 if (conf->query_all)
1213 pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1214 else
1215 pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1216
1217 /* mark the root set */
1218 pkg_vec_clear_marks(available_pkgs);
1219 opkg_message(conf, OPKG_NOTICE, "Root set:\n");
1220 for (i = 0; i < argc; i++) {
1221 const char *dependee_pattern = argv[i];
1222 pkg_vec_mark_if_matches(available_pkgs, dependee_pattern);
1223 }
1224 for (i = 0; i < available_pkgs->len; i++) {
1225 pkg_t *pkg = available_pkgs->pkgs[i];
1226 if (pkg->state_flag & SF_MARKED) {
1227 /* mark the parent (abstract) package */
1228 pkg_mark_provides(pkg);
1229 opkg_message(conf, OPKG_NOTICE, " %s\n", pkg->name);
1230 }
1231 }
1232
1233 opkg_message(conf, OPKG_NOTICE, "What %s root set\n", rel_str);
1234 do {
1235 int j;
1236 changed = 0;
1237
1238 for (j = 0; j < available_pkgs->len; j++) {
1239 pkg_t *pkg = available_pkgs->pkgs[j];
1240 int k;
1241 int count = ((what_field_type == WHATCONFLICTS)
1242 ? pkg->conflicts_count
1243 : pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count);
1244 /* skip this package if it is already marked */
1245 if (pkg->parent->state_flag & SF_MARKED) {
1246 continue;
1247 }
1248 for (k = 0; k < count; k++) {
1249 compound_depend_t *cdepend =
1250 (what_field_type == WHATCONFLICTS) ? &pkg->conflicts[k] : &pkg->depends[k];
1251 int l;
1252 for (l = 0; l < cdepend->possibility_count; l++) {
1253 depend_t *possibility = cdepend->possibilities[l];
1254 if (possibility->pkg->state_flag & SF_MARKED) {
1255 /* mark the depending package so we won't visit it again */
1256 pkg->state_flag |= SF_MARKED;
1257 pkg_mark_provides(pkg);
1258 changed++;
1259
1260 opkg_message(conf, OPKG_NOTICE, " %s", pkg->name);
1261 if (conf->verbosity > 0) {
1262 char *ver = pkg_version_str_alloc(pkg);
1263 opkg_message(conf, OPKG_NOTICE, " %s", ver);
1264 opkg_message(conf, OPKG_NOTICE, "\t%s %s", rel_str, possibility->pkg->name);
1265 if (possibility->version) {
1266 char *typestr = NULL;
1267 switch (possibility->constraint) {
1268 case NONE: typestr = "none"; break;
1269 case EARLIER: typestr = "<"; break;
1270 case EARLIER_EQUAL: typestr = "<="; break;
1271 case EQUAL: typestr = "="; break;
1272 case LATER_EQUAL: typestr = ">="; break;
1273 case LATER: typestr = ">"; break;
1274 }
1275 opkg_message(conf, OPKG_NOTICE, " (%s %s)", typestr, possibility->version);
1276 }
1277 free(ver);
1278 if (!pkg_dependence_satisfiable(conf, possibility))
1279 opkg_message(conf, OPKG_NOTICE, " unsatisfiable");
1280 }
1281 opkg_message(conf, OPKG_NOTICE, "\n");
1282 goto next_package;
1283 }
1284 }
1285 }
1286 next_package:
1287 ;
1288 }
1289 } while (changed && recursive);
1290 pkg_vec_free(available_pkgs);
1291 }
1292
1293 return 0;
1294 }
1295
1296 int pkg_mark_provides(pkg_t *pkg)
1297 {
1298 int provides_count = pkg->provides_count;
1299 abstract_pkg_t **provides = pkg->provides;
1300 int i;
1301 pkg->parent->state_flag |= SF_MARKED;
1302 for (i = 0; i < provides_count; i++) {
1303 provides[i]->state_flag |= SF_MARKED;
1304 }
1305 return 0;
1306 }
1307
1308 static int opkg_whatdepends_recursively_cmd(opkg_conf_t *conf, int argc, char **argv)
1309 {
1310 return opkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 1, argc, argv);
1311 }
1312 static int opkg_whatdepends_cmd(opkg_conf_t *conf, int argc, char **argv)
1313 {
1314 return opkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 0, argc, argv);
1315 }
1316
1317 static int opkg_whatsuggests_cmd(opkg_conf_t *conf, int argc, char **argv)
1318 {
1319 return opkg_what_depends_conflicts_cmd(conf, WHATSUGGESTS, 0, argc, argv);
1320 }
1321
1322 static int opkg_whatrecommends_cmd(opkg_conf_t *conf, int argc, char **argv)
1323 {
1324 return opkg_what_depends_conflicts_cmd(conf, WHATRECOMMENDS, 0, argc, argv);
1325 }
1326
1327 static int opkg_whatconflicts_cmd(opkg_conf_t *conf, int argc, char **argv)
1328 {
1329 return opkg_what_depends_conflicts_cmd(conf, WHATCONFLICTS, 0, argc, argv);
1330 }
1331
1332 static int opkg_what_provides_replaces_cmd(opkg_conf_t *conf, enum what_field_type what_field_type, int argc, char **argv)
1333 {
1334
1335 if (argc > 0) {
1336 pkg_vec_t *available_pkgs = pkg_vec_alloc();
1337 const char *rel_str = (what_field_type == WHATPROVIDES ? "provides" : "replaces");
1338 int i;
1339
1340 pkg_info_preinstall_check(conf);
1341
1342 if (conf->query_all)
1343 pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1344 else
1345 pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1346 for (i = 0; i < argc; i++) {
1347 const char *target = argv[i];
1348 int j;
1349
1350 opkg_message(conf, OPKG_ERROR, "What %s %s\n",
1351 rel_str, target);
1352 for (j = 0; j < available_pkgs->len; j++) {
1353 pkg_t *pkg = available_pkgs->pkgs[j];
1354 int k;
1355 int count = (what_field_type == WHATPROVIDES) ? pkg->provides_count : pkg->replaces_count;
1356 for (k = 0; k < count; k++) {
1357 abstract_pkg_t *apkg =
1358 ((what_field_type == WHATPROVIDES)
1359 ? pkg->provides[k]
1360 : pkg->replaces[k]);
1361 if (fnmatch(target, apkg->name, 0) == 0) {
1362 opkg_message(conf, OPKG_ERROR, " %s", pkg->name);
1363 if (strcmp(target, apkg->name) != 0)
1364 opkg_message(conf, OPKG_ERROR, "\t%s %s\n", rel_str, apkg->name);
1365 opkg_message(conf, OPKG_ERROR, "\n");
1366 }
1367 }
1368 }
1369 }
1370 pkg_vec_free(available_pkgs);
1371 }
1372 return 0;
1373 }
1374
1375 static int opkg_whatprovides_cmd(opkg_conf_t *conf, int argc, char **argv)
1376 {
1377 return opkg_what_provides_replaces_cmd(conf, WHATPROVIDES, argc, argv);
1378 }
1379
1380 static int opkg_whatreplaces_cmd(opkg_conf_t *conf, int argc, char **argv)
1381 {
1382 return opkg_what_provides_replaces_cmd(conf, WHATREPLACES, argc, argv);
1383 }
1384
1385 static int opkg_search_cmd(opkg_conf_t *conf, int argc, char **argv)
1386 {
1387 int i;
1388
1389 pkg_vec_t *installed;
1390 pkg_t *pkg;
1391 str_list_t *installed_files;
1392 str_list_elt_t *iter;
1393 char *installed_file;
1394
1395 if (argc < 1) {
1396 return EINVAL;
1397 }
1398
1399 installed = pkg_vec_alloc();
1400 pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
1401
1402 for (i=0; i < installed->len; i++) {
1403 pkg = installed->pkgs[i];
1404
1405 installed_files = pkg_get_installed_files(pkg);
1406
1407 for (iter = installed_files->head; iter; iter = iter->next) {
1408 installed_file = iter->data;
1409 if (fnmatch(argv[0], installed_file, 0)==0) {
1410 #ifndef OPKG_LIB
1411 printf("%s: %s\n", pkg->name, installed_file);
1412 #else
1413 if (opkg_cb_list) opkg_cb_list(pkg->name,
1414 installed_file,
1415 pkg_version_str_alloc(pkg),
1416 pkg->state_status, p_userdata);
1417 #endif
1418 }
1419 }
1420
1421 pkg_free_installed_files(pkg);
1422 }
1423
1424 /* XXX: CLEANUP: It's not obvious from the name of
1425 pkg_hash_fetch_all_installed that we need to call
1426 pkg_vec_free to avoid a memory leak. */
1427 pkg_vec_free(installed);
1428
1429 return 0;
1430 }
1431
1432 static int opkg_compare_versions_cmd(opkg_conf_t *conf, int argc, char **argv)
1433 {
1434 if (argc == 3) {
1435 /* this is a bit gross */
1436 struct pkg p1, p2;
1437 parseVersion(&p1, argv[0]);
1438 parseVersion(&p2, argv[2]);
1439 return pkg_version_satisfied(&p1, &p2, argv[1]);
1440 } else {
1441 opkg_message(conf, OPKG_ERROR,
1442 "opkg compare_versions <v1> <op> <v2>\n"
1443 "<op> is one of <= >= << >> =\n");
1444 return -1;
1445 }
1446 }
1447
1448 #ifndef HOST_CPU_STR
1449 #define HOST_CPU_STR__(X) #X
1450 #define HOST_CPU_STR_(X) HOST_CPU_STR__(X)
1451 #define HOST_CPU_STR HOST_CPU_STR_(HOST_CPU_FOO)
1452 #endif
1453
1454 static int opkg_print_architecture_cmd(opkg_conf_t *conf, int argc, char **argv)
1455 {
1456 nv_pair_list_elt_t *l;
1457
1458 l = conf->arch_list.head;
1459 while (l) {
1460 nv_pair_t *nv = l->data;
1461 printf("arch %s %s\n", nv->name, nv->value);
1462 l = l->next;
1463 }
1464 return 0;
1465 }
1466
1467