cli: implement --lists-dir
[oweals/opkg-lede.git] / src / opkg-cl.c
1 /* opkg-cl.c - the opkg package management system
2
3    Florian Boor
4    Copyright (C) 2003 kernel concepts
5
6    Carl D. Worth
7    Copyright 2001 University of Southern California
8
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License as
11    published by the Free Software Foundation; either version 2, or (at
12    your option) any later version.
13
14    This program is distributed in the hope that it will be useful, but
15    WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    General Public License for more details.
18
19    opkg command line frontend using libopkg
20 */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <getopt.h>
26
27 #include "opkg_conf.h"
28 #include "opkg_cmd.h"
29 #include "file_util.h"
30 #include "opkg_message.h"
31 #include "opkg_download.h"
32 #include "../libbb/libbb.h"
33
34 enum {
35         ARGS_OPT_FORCE_MAINTAINER = 129,
36         ARGS_OPT_FORCE_DEPENDS,
37         ARGS_OPT_FORCE_OVERWRITE,
38         ARGS_OPT_FORCE_DOWNGRADE,
39         ARGS_OPT_FORCE_REINSTALL,
40         ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES,
41         ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES,
42         ARGS_OPT_FORCE_SPACE,
43         ARGS_OPT_FORCE_POSTINSTALL,
44         ARGS_OPT_FORCE_REMOVE,
45         ARGS_OPT_FORCE_CHECKSUM,
46         ARGS_OPT_ADD_ARCH,
47         ARGS_OPT_ADD_DEST,
48         ARGS_OPT_NOACTION,
49         ARGS_OPT_DOWNLOAD_ONLY,
50         ARGS_OPT_NODEPS,
51         ARGS_OPT_NOCASE,
52         ARGS_OPT_AUTOREMOVE,
53         ARGS_OPT_CACHE,
54         ARGS_OPT_FORCE_SIGNATURE,
55 };
56
57 static struct option long_options[] = {
58         {"query-all", 0, 0, 'A'},
59         {"autoremove", 0, 0, ARGS_OPT_AUTOREMOVE},
60         {"cache", 1, 0, ARGS_OPT_CACHE},
61         {"conf-file", 1, 0, 'f'},
62         {"conf", 1, 0, 'f'},
63         {"dest", 1, 0, 'd'},
64         {"force-maintainer", 0, 0, ARGS_OPT_FORCE_MAINTAINER},
65         {"force_maintainer", 0, 0, ARGS_OPT_FORCE_MAINTAINER},
66         {"force-depends", 0, 0, ARGS_OPT_FORCE_DEPENDS},
67         {"force_depends", 0, 0, ARGS_OPT_FORCE_DEPENDS},
68         {"force-overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE},
69         {"force_overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE},
70         {"force_downgrade", 0, 0, ARGS_OPT_FORCE_DOWNGRADE},
71         {"force-downgrade", 0, 0, ARGS_OPT_FORCE_DOWNGRADE},
72         {"force-reinstall", 0, 0, ARGS_OPT_FORCE_REINSTALL},
73         {"force_reinstall", 0, 0, ARGS_OPT_FORCE_REINSTALL},
74         {"force-space", 0, 0, ARGS_OPT_FORCE_SPACE},
75         {"force_space", 0, 0, ARGS_OPT_FORCE_SPACE},
76         {"recursive", 0, 0, ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
77         {"force-removal-of-dependent-packages", 0, 0,
78                 ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
79         {"force_removal_of_dependent_packages", 0, 0,
80                 ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
81         {"force-removal-of-essential-packages", 0, 0,
82                 ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES},
83         {"force_removal_of_essential_packages", 0, 0,
84                 ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES},
85         {"force-postinstall", 0, 0, ARGS_OPT_FORCE_POSTINSTALL},
86         {"force_postinstall", 0, 0, ARGS_OPT_FORCE_POSTINSTALL},
87         {"force-remove", 0, 0, ARGS_OPT_FORCE_REMOVE},
88         {"force_remove", 0, 0, ARGS_OPT_FORCE_REMOVE},
89         {"force-checksum", 0, 0, ARGS_OPT_FORCE_CHECKSUM},
90         {"force_checksum", 0, 0, ARGS_OPT_FORCE_CHECKSUM},
91         {"force-signature", 0, 0, ARGS_OPT_FORCE_SIGNATURE},
92         {"force_signature", 0, 0, ARGS_OPT_FORCE_SIGNATURE},
93         {"noaction", 0, 0, ARGS_OPT_NOACTION},
94         {"download-only", 0, 0, ARGS_OPT_DOWNLOAD_ONLY},
95         {"nodeps", 0, 0, ARGS_OPT_NODEPS},
96         {"nocase", 0, 0, ARGS_OPT_NOCASE},
97         {"offline", 1, 0, 'o'},
98         {"offline-root", 1, 0, 'o'},
99         {"add-arch", 1, 0, ARGS_OPT_ADD_ARCH},
100         {"add-dest", 1, 0, ARGS_OPT_ADD_DEST},
101         {"test", 0, 0, ARGS_OPT_NOACTION},
102         {"tmp-dir", 1, 0, 't'},
103         {"tmp_dir", 1, 0, 't'},
104         {"lists-dir", 1, 0, 'l'},
105         {"lists_dir", 1, 0, 'l'},
106         {"verbosity", 2, 0, 'V'},
107         {"version", 0, 0, 'v'},
108         {0, 0, 0, 0}
109 };
110
111 static int
112 args_parse(int argc, char *argv[])
113 {
114         int c;
115         int option_index = 0;
116         int parse_err = 0;
117         char *tuple, *targ;
118
119         while (1) {
120                 c = getopt_long_only(argc, argv, "Ad:f:ino:p:l:t:vV::",
121                                 long_options, &option_index);
122                 if (c == -1)
123                         break;
124
125                 switch (c) {
126                 case 'A':
127                         conf->query_all = 1;
128                         break;
129                 case 'd':
130                         conf->dest_str = xstrdup(optarg);
131                         break;
132                 case 'f':
133                         conf->conf_file = xstrdup(optarg);
134                         break;
135                 case 'i':
136                         conf->nocase = FNM_CASEFOLD;
137                         break;
138                 case 'o':
139                         conf->offline_root = xstrdup(optarg);
140                         break;
141                 case 't':
142                         conf->tmp_dir = xstrdup(optarg);
143                         break;
144                 case 'l':
145                         conf->lists_dir = xstrdup(optarg);
146                         break;
147                 case 'v':
148                         printf("opkg version %s\n", VERSION);
149                         exit(0);
150                 case 'V':
151                         conf->verbosity = INFO;
152                         if (optarg != NULL)
153                                 conf->verbosity = atoi(optarg);
154                         break;
155                 case ARGS_OPT_AUTOREMOVE:
156                         conf->autoremove = 1;
157                         break;
158                 case ARGS_OPT_CACHE:
159                         free(conf->cache);
160                         conf->cache = xstrdup(optarg);
161                         break;
162                 case ARGS_OPT_FORCE_MAINTAINER:
163                         conf->force_maintainer = 1;
164                         break;
165                 case ARGS_OPT_FORCE_DEPENDS:
166                         conf->force_depends = 1;
167                         break;
168                 case ARGS_OPT_FORCE_OVERWRITE:
169                         conf->force_overwrite = 1;
170                         break;
171                 case ARGS_OPT_FORCE_DOWNGRADE:
172                         conf->force_downgrade = 1;
173                         break;
174                 case ARGS_OPT_FORCE_REINSTALL:
175                         conf->force_reinstall = 1;
176                         break;
177                 case ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES:
178                         conf->force_removal_of_essential_packages = 1;
179                         break;
180                 case ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES:
181                         conf->force_removal_of_dependent_packages = 1;
182                         break;
183                 case ARGS_OPT_FORCE_SPACE:
184                         conf->force_space = 1;
185                         break;
186                 case ARGS_OPT_FORCE_POSTINSTALL:
187                         conf->force_postinstall = 1;
188                         break;
189                 case ARGS_OPT_FORCE_REMOVE:
190                         conf->force_remove = 1;
191                         break;
192                 case ARGS_OPT_FORCE_CHECKSUM:
193                         conf->force_checksum = 1;
194                         break;
195                 case ARGS_OPT_NODEPS:
196                         conf->nodeps = 1;
197                         break;
198                 case ARGS_OPT_NOCASE:
199                         conf->nocase = FNM_CASEFOLD;
200                         break;
201                 case ARGS_OPT_ADD_ARCH:
202                 case ARGS_OPT_ADD_DEST:
203                         tuple = xstrdup(optarg);
204                         if ((targ = strchr(tuple, ':')) != NULL) {
205                                 *targ++ = 0;
206                                 if ((strlen(tuple) > 0) && (strlen(targ) > 0)) {
207                                         nv_pair_list_append(
208                                                 (c == ARGS_OPT_ADD_ARCH)
209                                                         ? &conf->arch_list : &conf->tmp_dest_list,
210                                                 tuple, targ);
211                                 }
212                         }
213                         free(tuple);
214                         break;
215                 case ARGS_OPT_NOACTION:
216                         conf->noaction = 1;
217                         break;
218         case ARGS_OPT_DOWNLOAD_ONLY:
219                         conf->download_only = 1;
220                         break;
221                 case ARGS_OPT_FORCE_SIGNATURE:
222                         conf->force_signature = 1;
223                         break;
224                 case ':':
225                         parse_err = -1;
226                         break;
227                 case '?':
228                         parse_err = -1;
229                         break;
230                 default:
231                         printf("Confusion: getopt_long returned %d\n", c);
232                 }
233         }
234
235         if(!conf->conf_file && !conf->offline_root)
236                 conf->conf_file = xstrdup("/etc/opkg.conf");
237
238         if (parse_err)
239                 return parse_err;
240         else
241                 return optind;
242 }
243
244 static void
245 usage()
246 {
247         printf("usage: opkg [options...] sub-command [arguments...]\n");
248         printf("where sub-command is one of:\n");
249
250         printf("\nPackage Manipulation:\n");
251         printf("\tupdate                        Update list of available packages\n");
252         printf("\tupgrade <pkgs>                Upgrade packages\n");
253         printf("\tinstall <pkgs>                Install package(s)\n");
254         printf("\tconfigure <pkgs>      Configure unpacked package(s)\n");
255         printf("\tremove <pkgs|regexp>  Remove package(s)\n");
256         printf("\tflag <flag> <pkgs>    Flag package(s)\n");
257         printf("\t <flag>=hold|noprune|user|ok|installed|unpacked (one per invocation)\n");
258
259         printf("\nInformational Commands:\n");
260         printf("\tlist                  List available packages\n");
261         printf("\tlist-installed                List installed packages\n");
262         printf("\tlist-upgradable               List installed and upgradable packages\n");
263         printf("\tlist-changed-conffiles        List user modified configuration files\n");
264         printf("\tfiles <pkg>           List files belonging to <pkg>\n");
265         printf("\tsearch <file|regexp>  List package providing <file>\n");
266         printf("\tfind <regexp>         List packages whose name or description matches <regexp>\n");
267         printf("\tinfo [pkg|regexp]     Display all info for <pkg>\n");
268         printf("\tstatus [pkg|regexp]   Display all status for <pkg>\n");
269         printf("\tdownload <pkg>                Download <pkg> to current directory\n");
270         printf("\tcompare-versions <v1> <op> <v2>\n");
271         printf("\t                    compare versions using <= < > >= = << >>\n");
272         printf("\tprint-architecture    List installable package architectures\n");
273         printf("\tdepends [-A] [pkgname|pat]+\n");
274         printf("\twhatdepends [-A] [pkgname|pat]+\n");
275         printf("\twhatdependsrec [-A] [pkgname|pat]+\n");
276         printf("\twhatrecommends[-A] [pkgname|pat]+\n");
277         printf("\twhatsuggests[-A] [pkgname|pat]+\n");
278         printf("\twhatprovides [-A] [pkgname|pat]+\n");
279         printf("\twhatconflicts [-A] [pkgname|pat]+\n");
280         printf("\twhatreplaces [-A] [pkgname|pat]+\n");
281
282         printf("\nOptions:\n");
283         printf("\t-A                    Query all packages not just those installed\n");
284         printf("\t-V[<level>]           Set verbosity level to <level>.\n");
285         printf("\t--verbosity[=<level>] Verbosity levels:\n");
286         printf("\t                              0 errors only\n");
287         printf("\t                              1 normal messages (default)\n");
288         printf("\t                              2 informative messages\n");
289         printf("\t                              3 debug\n");
290         printf("\t                              4 debug level 2\n");
291         printf("\t-f <conf_file>                Use <conf_file> as the opkg configuration file\n");
292         printf("\t--conf <conf_file>\n");
293         printf("\t--cache <directory>   Use a package cache\n");
294         printf("\t-d <dest_name>                Use <dest_name> as the the root directory for\n");
295         printf("\t--dest <dest_name>    package installation, removal, upgrading.\n");
296         printf("                                <dest_name> should be a defined dest name from\n");
297         printf("                                the configuration file, (but can also be a\n");
298         printf("                                directory name in a pinch).\n");
299         printf("\t-o <dir>              Use <dir> as the root directory for\n");
300         printf("\t--offline-root <dir>  offline installation of packages.\n");
301         printf("\t--add-arch <arch>:<prio>      Register architecture with given priority\n");
302         printf("\t--add-dest <name>:<path>      Register destination with given path\n");
303
304         printf("\nForce Options:\n");
305         printf("\t--force-depends               Install/remove despite failed dependencies\n");
306         printf("\t--force-maintainer    Overwrite preexisting config files\n");
307         printf("\t--force-reinstall     Reinstall package(s)\n");
308         printf("\t--force-overwrite     Overwrite files from other package(s)\n");
309         printf("\t--force-downgrade     Allow opkg to downgrade packages\n");
310         printf("\t--force-space         Disable free space checks\n");
311         printf("\t--force-postinstall   Run postinstall scripts even in offline mode\n");
312         printf("\t--force-remove        Remove package even if prerm script fails\n");
313         printf("\t--force-checksum      Don't fail on checksum mismatches\n");
314         printf("\t--noaction            No action -- test only\n");
315         printf("\t--download-only       No action -- download only\n");
316         printf("\t--nodeps              Do not follow dependencies\n");
317         printf("\t--nocase              Perform case insensitive pattern matching\n");
318         printf("\t--force-removal-of-dependent-packages\n");
319         printf("\t                      Remove package and all dependencies\n");
320         printf("\t--autoremove          Remove packages that were installed\n");
321         printf("\t                      automatically to satisfy dependencies\n");
322         printf("\t-t                    Specify tmp-dir.\n");
323         printf("\t--tmp-dir             Specify tmp-dir.\n");
324         printf("\t-l                    Specify lists-dir.\n");
325         printf("\t--lists-dir           Specify lists-dir.\n");
326
327         printf("\n");
328
329         printf(" regexp could be something like 'pkgname*' '*file*' or similar\n");
330         printf(" e.g. opkg info 'libstd*' or opkg search '*libop*' or opkg remove 'libncur*'\n");
331
332         /* --force-removal-of-essential-packages        Let opkg remove essential packages.
333                 Using this option is almost guaranteed to break your system, hence this option
334                 is not even advertised in the usage statement. */
335
336         exit(1);
337 }
338
339 int
340 main(int argc, char *argv[])
341 {
342         int opts, err = -1;
343         char *cmd_name;
344         opkg_cmd_t *cmd;
345         int nocheckfordirorfile = 0;
346         int noreadfeedsfile = 0;
347
348         if (opkg_conf_init())
349                 goto err0;
350
351         conf->verbosity = NOTICE;
352
353         opts = args_parse(argc, argv);
354         if (opts == argc || opts < 0) {
355                 fprintf(stderr, "opkg must have one sub-command argument\n");
356                 usage();
357         }
358
359         cmd_name = argv[opts++];
360
361         if (!strcmp(cmd_name,"print-architecture") ||
362             !strcmp(cmd_name,"print_architecture") ||
363             !strcmp(cmd_name,"print-installation-architecture") ||
364             !strcmp(cmd_name,"print_installation_architecture") )
365                 nocheckfordirorfile = 1;
366
367         if (!strcmp(cmd_name,"flag") ||
368             !strcmp(cmd_name,"configure") ||
369             !strcmp(cmd_name,"remove") ||
370             !strcmp(cmd_name,"files") ||
371             !strcmp(cmd_name,"search") ||
372             !strcmp(cmd_name,"compare_versions") ||
373             !strcmp(cmd_name,"compare-versions") ||
374             !strcmp(cmd_name,"list_installed") ||
375             !strcmp(cmd_name,"list-installed") ||
376             !strcmp(cmd_name,"list_changed_conffiles") ||
377             !strcmp(cmd_name,"list-changed-conffiles") ||
378             !strcmp(cmd_name,"status") )
379                 noreadfeedsfile = 1;
380
381         cmd = opkg_cmd_find(cmd_name);
382         if (cmd == NULL) {
383                 fprintf(stderr, "%s: unknown sub-command %s\n", argv[0],
384                          cmd_name);
385                 usage();
386         }
387
388         conf->pfm = cmd->pfm;
389
390         if (opkg_conf_load())
391                 goto err0;
392
393         if (!nocheckfordirorfile) {
394                 if (!noreadfeedsfile) {
395                         if (pkg_hash_load_feeds())
396                                 goto err1;
397                 }
398
399                 if (pkg_hash_load_status_files())
400                         goto err1;
401         }
402
403         if (cmd->requires_args && opts == argc) {
404                 fprintf(stderr,
405                          "%s: the ``%s'' command requires at least one argument\n",
406                          argv[0], cmd_name);
407                 usage();
408         }
409
410         err = opkg_cmd_exec(cmd, argc - opts, (const char **) (argv + opts));
411
412 #ifdef HAVE_CURL
413         opkg_curl_cleanup();
414 #endif
415 err1:
416         opkg_conf_deinit();
417
418 err0:
419         print_error_list();
420         free_error_list();
421
422         return err;
423 }