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