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