implement --force-postinstall option, this allows forcing the execution of postinstal...
[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 "../libbb/libbb.h"
32
33 enum {
34         ARGS_OPT_FORCE_MAINTAINER = 129,
35         ARGS_OPT_FORCE_DEPENDS,
36         ARGS_OPT_FORCE_OVERWRITE,
37         ARGS_OPT_FORCE_DOWNGRADE,
38         ARGS_OPT_FORCE_REINSTALL,
39         ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES,
40         ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES,
41         ARGS_OPT_FORCE_SPACE,
42         ARGS_OPT_FORCE_POSTINSTALL,
43         ARGS_OPT_NOACTION,
44         ARGS_OPT_DOWNLOAD_ONLY,
45         ARGS_OPT_NODEPS,
46         ARGS_OPT_AUTOREMOVE,
47         ARGS_OPT_CACHE,
48 };
49
50 static struct option long_options[] = {
51         {"query-all", 0, 0, 'A'},
52         {"autoremove", 0, 0, ARGS_OPT_AUTOREMOVE},
53         {"cache", 1, 0, ARGS_OPT_CACHE},
54         {"conf-file", 1, 0, 'f'},
55         {"conf", 1, 0, 'f'},
56         {"dest", 1, 0, 'd'},
57         {"force-maintainer", 0, 0, ARGS_OPT_FORCE_MAINTAINER},
58         {"force_maintainer", 0, 0, ARGS_OPT_FORCE_MAINTAINER},
59         {"force-depends", 0, 0, ARGS_OPT_FORCE_DEPENDS},
60         {"force_depends", 0, 0, ARGS_OPT_FORCE_DEPENDS},
61         {"force-overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE},
62         {"force_overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE},
63         {"force_downgrade", 0, 0, ARGS_OPT_FORCE_DOWNGRADE},
64         {"force-downgrade", 0, 0, ARGS_OPT_FORCE_DOWNGRADE},
65         {"force-reinstall", 0, 0, ARGS_OPT_FORCE_REINSTALL},
66         {"force_reinstall", 0, 0, ARGS_OPT_FORCE_REINSTALL},
67         {"force-space", 0, 0, ARGS_OPT_FORCE_SPACE},
68         {"force_space", 0, 0, ARGS_OPT_FORCE_SPACE},
69         {"recursive", 0, 0, ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
70         {"force-removal-of-dependent-packages", 0, 0,
71                 ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
72         {"force_removal_of_dependent_packages", 0, 0,
73                 ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
74         {"force-removal-of-essential-packages", 0, 0,
75                 ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES},
76         {"force_removal_of_essential_packages", 0, 0,
77                 ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES},
78         {"force-postinstall", 0, 0, ARGS_OPT_FORCE_POSTINSTALL},
79         {"force_postinstall", 0, 0, ARGS_OPT_FORCE_POSTINSTALL},
80         {"noaction", 0, 0, ARGS_OPT_NOACTION},
81         {"download-only", 0, 0, ARGS_OPT_DOWNLOAD_ONLY},
82         {"nodeps", 0, 0, ARGS_OPT_NODEPS},
83         {"offline", 1, 0, 'o'},
84         {"offline-root", 1, 0, 'o'},
85         {"test", 0, 0, ARGS_OPT_NOACTION},
86         {"tmp-dir", 1, 0, 't'},
87         {"tmp_dir", 1, 0, 't'},
88         {"verbosity", 2, 0, 'V'},
89         {"version", 0, 0, 'v'},
90         {0, 0, 0, 0}
91 };
92
93 static int
94 args_parse(int argc, char *argv[])
95 {
96         int c;
97         int option_index = 0;
98         int parse_err = 0;
99
100         while (1) {
101                 c = getopt_long_only(argc, argv, "Ad:f:no:p:t:vV::",
102                                 long_options, &option_index);
103                 if (c == -1)
104                         break;
105
106                 switch (c) {
107                 case 'A':
108                         conf->query_all = 1;
109                         break;
110                 case 'd':
111                         conf->dest_str = xstrdup(optarg);
112                         break;
113                 case 'f':
114                         conf->conf_file = xstrdup(optarg);
115                         break;
116                 case 'o':
117                         conf->offline_root = xstrdup(optarg);
118                         break;
119                 case 't':
120                         conf->tmp_dir = xstrdup(optarg);
121                         break;
122                 case 'v':
123                         printf("opkg version %s\n", VERSION);
124                         exit(0);
125                 case 'V':
126                         conf->verbosity = INFO;
127                         if (optarg != NULL)
128                                 conf->verbosity = atoi(optarg);
129                         break;
130                 case ARGS_OPT_AUTOREMOVE:
131                         conf->autoremove = 1;
132                         break;
133                 case ARGS_OPT_CACHE:
134                         free(conf->cache);
135                         conf->cache = xstrdup(optarg);
136                         break;
137                 case ARGS_OPT_FORCE_MAINTAINER:
138                         conf->force_maintainer = 1;
139                         break;
140                 case ARGS_OPT_FORCE_DEPENDS:
141                         conf->force_depends = 1;
142                         break;
143                 case ARGS_OPT_FORCE_OVERWRITE:
144                         conf->force_overwrite = 1;
145                         break;
146                 case ARGS_OPT_FORCE_DOWNGRADE:
147                         conf->force_downgrade = 1;
148                         break;
149                 case ARGS_OPT_FORCE_REINSTALL:
150                         conf->force_reinstall = 1;
151                         break;
152                 case ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES:
153                         conf->force_removal_of_essential_packages = 1;
154                         break;
155                 case ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES:
156                         conf->force_removal_of_dependent_packages = 1;
157                         break;
158                 case ARGS_OPT_FORCE_SPACE:
159                         conf->force_space = 1;
160                         break;
161                 case ARGS_OPT_FORCE_POSTINSTALL:
162                         conf->force_postinstall = 1;
163                         break;
164                 case ARGS_OPT_NODEPS:
165                         conf->nodeps = 1;
166                         break;
167                 case ARGS_OPT_NOACTION:
168                         conf->noaction = 1;
169                         break;
170         case ARGS_OPT_DOWNLOAD_ONLY:
171                         conf->download_only = 1;
172                         break;
173                 case ':':
174                         parse_err = -1;
175                         break;
176                 case '?':
177                         parse_err = -1;
178                         break;
179                 default:
180                         printf("Confusion: getopt_long returned %d\n", c);
181                 }
182         }
183
184         if (parse_err)
185                 return parse_err;
186         else
187                 return optind;
188 }
189
190 static void
191 usage()
192 {
193         printf("usage: opkg [options...] sub-command [arguments...]\n");
194         printf("where sub-command is one of:\n");
195
196         printf("\nPackage Manipulation:\n");
197         printf("\tupdate                        Update list of available packages\n");
198         printf("\tupgrade                       Upgrade installed packages\n");
199         printf("\tinstall <pkgs>                Install package(s)\n");
200         printf("\tconfigure <pkgs>      Configure unpacked package(s)\n");
201         printf("\tremove <pkgs|regexp>  Remove package(s)\n");
202         printf("\tflag <flag> <pkgs>    Flag package(s)\n");
203         printf("\t <flag>=hold|noprune|user|ok|installed|unpacked (one per invocation)\n");
204
205         printf("\nInformational Commands:\n");
206         printf("\tlist                  List available packages\n");
207         printf("\tlist-installed                List installed packages\n");
208         printf("\tlist-upgradable               List installed and upgradable packages\n");
209         printf("\tfiles <pkg>           List files belonging to <pkg>\n");
210         printf("\tsearch <file|regexp>  List package providing <file>\n");
211         printf("\tinfo [pkg|regexp]     Display all info for <pkg>\n");
212         printf("\tstatus [pkg|regexp]   Display all status for <pkg>\n");
213         printf("\tdownload <pkg>                Download <pkg> to current directory\n");
214         printf("\tcompare-versions <v1> <op> <v2>\n");
215         printf("\t                    compare versions using <= < > >= = << >>\n");
216         printf("\tprint-architecture    List installable package architectures\n");
217         printf("\twhatdepends [-A] [pkgname|pat]+\n");
218         printf("\twhatdependsrec [-A] [pkgname|pat]+\n");
219         printf("\twhatprovides [-A] [pkgname|pat]+\n");
220         printf("\twhatconflicts [-A] [pkgname|pat]+\n");
221         printf("\twhatreplaces [-A] [pkgname|pat]+\n");
222
223         printf("\nOptions:\n");
224         printf("\t-A                    Query all packages not just those installed\n");
225         printf("\t-V[<level>]           Set verbosity level to <level>.\n");
226         printf("\t--verbosity[=<level>] Verbosity levels:\n");
227         printf("\t                              0 errors only\n");
228         printf("\t                              1 normal messages (default)\n");
229         printf("\t                              2 informative messages\n");
230         printf("\t                              3 debug\n");
231         printf("\t                              4 debug level 2\n");
232         printf("\t-f <conf_file>                Use <conf_file> as the opkg configuration file\n");
233         printf("\t--conf <conf_file>\n");
234         printf("\t--cache <directory>   Use a package cache\n");
235         printf("\t-d <dest_name>                Use <dest_name> as the the root directory for\n");
236         printf("\t--dest <dest_name>    package installation, removal, upgrading.\n");
237         printf("                                <dest_name> should be a defined dest name from\n");
238         printf("                                the configuration file, (but can also be a\n");
239         printf("                                directory name in a pinch).\n");
240         printf("\t-o <dir>              Use <dir> as the root directory for\n");
241         printf("\t--offline-root <dir>  offline installation of packages.\n");
242
243         printf("\nForce Options:\n");
244         printf("\t--force-depends               Install/remove despite failed dependencies\n");
245         printf("\t--force-maintainer    Overwrite preexisting config files\n");
246         printf("\t--force-reinstall     Reinstall package(s)\n");
247         printf("\t--force-overwrite     Overwrite files from other package(s)\n");
248         printf("\t--force-downgrade     Allow opkg to downgrade packages\n");
249         printf("\t--force-space         Disable free space checks\n");
250         printf("\t--force-postinstall   Run postinstall scripts even in offline mode\n");
251         printf("\t--noaction            No action -- test only\n");
252         printf("\t--download-only       No action -- download only\n");
253         printf("\t--nodeps              Do not follow dependencies\n");
254         printf("\t--force-removal-of-dependent-packages\n");
255         printf("\t                      Remove package and all dependencies\n");
256         printf("\t--autoremove          Remove packages that were installed\n");
257         printf("\t                      automatically to satisfy dependencies\n");
258         printf("\t-t                    Specify tmp-dir.\n");
259         printf("\t--tmp-dir             Specify tmp-dir.\n");
260
261         printf("\n");
262
263         printf(" regexp could be something like 'pkgname*' '*file*' or similar\n");
264         printf(" e.g. opkg info 'libstd*' or opkg search '*libop*' or opkg remove 'libncur*'\n");
265
266         /* --force-removal-of-essential-packages        Let opkg remove essential packages.
267                 Using this option is almost guaranteed to break your system, hence this option
268                 is not even advertised in the usage statement. */
269
270         exit(1);
271 }
272
273 int
274 main(int argc, char *argv[])
275 {
276         int opts, err = -1;
277         char *cmd_name;
278         opkg_cmd_t *cmd;
279         int nocheckfordirorfile = 0;
280         int noreadfeedsfile = 0;
281
282         conf->verbosity = NOTICE;
283
284         opts = args_parse(argc, argv);
285         if (opts == argc || opts < 0) {
286                 fprintf(stderr, "opkg must have one sub-command argument\n");
287                 usage();
288         }
289
290         cmd_name = argv[opts++];
291
292         if (!strcmp(cmd_name,"print-architecture") ||
293             !strcmp(cmd_name,"print_architecture") ||
294             !strcmp(cmd_name,"print-installation-architecture") ||
295             !strcmp(cmd_name,"print_installation_architecture") )
296                 nocheckfordirorfile = 1;
297
298         if (!strcmp(cmd_name,"flag") ||
299             !strcmp(cmd_name,"configure") ||
300             !strcmp(cmd_name,"remove") ||
301             !strcmp(cmd_name,"files") ||
302             !strcmp(cmd_name,"search") ||
303             !strcmp(cmd_name,"compare_versions") ||
304             !strcmp(cmd_name,"compare-versions") ||
305             !strcmp(cmd_name,"list_installed") ||
306             !strcmp(cmd_name,"list-installed") ||
307             !strcmp(cmd_name,"status") )
308                 noreadfeedsfile = 1;
309
310         cmd = opkg_cmd_find(cmd_name);
311         if (cmd == NULL) {
312                 fprintf(stderr, "%s: unknown sub-command %s\n", argv[0],
313                          cmd_name);
314                 usage();
315         }
316
317         conf->pfm = cmd->pfm;
318
319         if (opkg_conf_init())
320                 goto err0;
321
322         if (!nocheckfordirorfile) {
323                 if (!noreadfeedsfile) {
324                         if (pkg_hash_load_feeds())
325                                 goto err1;
326                 }
327
328                 if (pkg_hash_load_status_files())
329                         goto err1;
330         }
331
332         if (cmd->requires_args && opts == argc) {
333                 fprintf(stderr,
334                          "%s: the ``%s'' command requires at least one argument\n",
335                          argv[0], cmd_name);
336                 usage();
337         }
338
339         err = opkg_cmd_exec(cmd, argc - opts, (const char **) (argv + opts));
340
341 #ifdef HAVE_CURL
342         opkg_curl_cleanup();
343 #endif
344 err1:
345         opkg_conf_deinit();
346
347 err0:
348         print_error_list();
349         free_error_list();
350
351         return err;
352 }