Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/src/install.c
2645 views
1
/*-
2
* Copyright (c) 2011-2016 Baptiste Daroussin <[email protected]>
3
* Copyright (c) 2011-2012 Julien Laffaye <[email protected]>
4
* Copyright (c) 2011-2012 Marin Atanasov Nikolov <[email protected]>
5
* Copyright (c) 2013-2014 Matthew Seaman <[email protected]>
6
* Copyright (c) 2012-2013 Bryan Drewery <[email protected]>
7
* Copyright (c) 2016 Vsevolod Stakhov <[email protected]>
8
* All rights reserved.
9
*
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
12
* are met:
13
* 1. Redistributions of source code must retain the above copyright
14
* notice, this list of conditions and the following disclaimer
15
* in this position and unchanged.
16
* 2. Redistributions in binary form must reproduce the above copyright
17
* notice, this list of conditions and the following disclaimer in the
18
* documentation and/or other materials provided with the distribution.
19
*
20
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
21
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
24
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
*/
31
32
#include <sys/types.h>
33
34
#include <err.h>
35
#include <getopt.h>
36
#include <libgen.h>
37
#include <stdbool.h>
38
#include <stdio.h>
39
#include <string.h>
40
#include <unistd.h>
41
42
#include <pkg.h>
43
44
#include "pkgcli.h"
45
46
void
47
usage_install(void)
48
{
49
fprintf(stderr,
50
"Usage: pkg install [-AfInFMqRUy] [-r reponame] [-Cgix] <pkg-name> ...\n\n");
51
fprintf(stderr, "For more information see 'pkg help install'.\n");
52
}
53
54
int
55
exec_install(int argc, char **argv)
56
{
57
struct pkgdb *db = NULL;
58
struct pkg_jobs *jobs = NULL;
59
int retcode;
60
int status;
61
int updcode = EPKG_OK;
62
int ch;
63
int mode, repo_type;
64
int done = 0;
65
int lock_type = PKGDB_LOCK_ADVISORY;
66
int nbactions = 0;
67
int scriptnoexec = 0;
68
bool rc = true;
69
bool local_only = false;
70
match_t match = MATCH_EXACT;
71
pkg_flags f = PKG_FLAG_NONE | PKG_FLAG_PKG_VERSION_TEST;
72
c_charv_t reponames = vec_init();
73
74
struct option longopts[] = {
75
{ "automatic", no_argument, NULL, 'A' },
76
{ "case-sensitive", no_argument, NULL, 'C' },
77
{ "force", no_argument, NULL, 'f' },
78
{ "fetch-only", no_argument, NULL, 'F' },
79
{ "glob", no_argument, NULL, 'g' },
80
{ "case-insensitive", no_argument, NULL, 'i' },
81
{ "no-scripts", no_argument, NULL, 'I' },
82
{ "script-no-exec", no_argument, &scriptnoexec, 1 },
83
{ "local-only", no_argument, NULL, 'l' },
84
{ "ignore-missing", no_argument, NULL, 'M' },
85
{ "dry-run", no_argument, NULL, 'n' },
86
{ "quiet", no_argument, NULL, 'q' },
87
{ "repository", required_argument, NULL, 'r' },
88
{ "recursive", no_argument, NULL, 'R' },
89
{ "no-repo-update", no_argument, NULL, 'U' },
90
{ "regex", no_argument, NULL, 'x' },
91
{ "register-only", no_argument, NULL, 'X' },
92
{ "yes", no_argument, NULL, 'y' },
93
{ NULL, 0, NULL, 0 },
94
};
95
96
if (STREQ(argv[0], "add")) {
97
auto_update = false;
98
local_only = true;
99
yes = true;
100
quiet = true;
101
}
102
103
while ((ch = getopt_long(argc, argv, "+ACfFgiIlMnqr:RUxy", longopts, NULL)) != -1) {
104
switch (ch) {
105
case 'A':
106
f |= PKG_FLAG_AUTOMATIC;
107
break;
108
case 'C':
109
pkgdb_set_case_sensitivity(true);
110
break;
111
case 'f':
112
f |= PKG_FLAG_FORCE;
113
break;
114
case 'F':
115
f |= PKG_FLAG_SKIP_INSTALL;
116
lock_type = PKGDB_LOCK_READONLY;
117
break;
118
case 'g':
119
match = MATCH_GLOB;
120
break;
121
case 'i':
122
pkgdb_set_case_sensitivity(false);
123
break;
124
case 'I':
125
f |= PKG_FLAG_NOSCRIPT;
126
break;
127
case 'l':
128
local_only = true;
129
auto_update = false;
130
break;
131
case 'M':
132
f |= PKG_FLAG_FORCE_MISSING;
133
break;
134
case 'n':
135
f |= PKG_FLAG_DRY_RUN;
136
lock_type = PKGDB_LOCK_READONLY;
137
dry_run = true;
138
break;
139
case 'q':
140
quiet = true;
141
break;
142
case 'r':
143
vec_push(&reponames, optarg);
144
break;
145
case 'R':
146
f |= PKG_FLAG_RECURSIVE;
147
break;
148
case 'U':
149
auto_update = false;
150
break;
151
case 'x':
152
match = MATCH_REGEX;
153
break;
154
case 'X':
155
f |= PKG_FLAG_REGISTER_ONLY | PKG_FLAG_NOSCRIPT;
156
break;
157
case 'y':
158
yes = true;
159
break;
160
case 0:
161
if (scriptnoexec == 1)
162
f |= PKG_FLAG_NOEXEC;
163
break;
164
default:
165
usage_install();
166
return (EXIT_FAILURE);
167
}
168
}
169
argc -= optind;
170
argv += optind;
171
172
if (argc < 1) {
173
usage_install();
174
return (EXIT_FAILURE);
175
}
176
177
if (dry_run && !auto_update)
178
mode = PKGDB_MODE_READ;
179
else
180
mode = PKGDB_MODE_READ |
181
PKGDB_MODE_WRITE |
182
PKGDB_MODE_CREATE;
183
if (local_only)
184
repo_type = PKGDB_DB_LOCAL;
185
else
186
repo_type = PKGDB_DB_LOCAL|PKGDB_DB_REPO;
187
188
/* It is safe to use both `reponame` and `NULL` here */
189
retcode = pkgdb_access2(mode, repo_type, &reponames);
190
191
if (retcode == EPKG_ENOACCESS && dry_run) {
192
auto_update = false;
193
retcode = pkgdb_access2(PKGDB_MODE_READ,
194
repo_type, &reponames);
195
}
196
197
if (retcode == EPKG_ENOACCESS) {
198
warnx("Insufficient privileges to install packages");
199
return (EXIT_FAILURE);
200
} else if (retcode != EPKG_OK)
201
return (EXIT_FAILURE);
202
203
/* first update the remote repositories if needed */
204
if (auto_update && pkg_repos_total_count() > 0 &&
205
(updcode = pkgcli_update(false, false, &reponames)) != EPKG_OK)
206
return (updcode);
207
208
if (pkgdb_open_all2(&db,
209
local_only ? PKGDB_DEFAULT : PKGDB_MAYBE_REMOTE,
210
&reponames) != EPKG_OK)
211
return (EXIT_FAILURE);
212
213
if (pkgdb_obtain_lock(db, lock_type) != EPKG_OK) {
214
pkgdb_close(db);
215
warnx("Cannot get an advisory lock on a database, it is locked by another process");
216
return (EXIT_FAILURE);
217
}
218
219
status = EXIT_FAILURE;
220
if (pkg_jobs_new(&jobs, PKG_JOBS_INSTALL, db) != EPKG_OK)
221
goto cleanup;
222
223
if (!local_only && reponames.len > 0 &&
224
pkg_jobs_set_repositories(jobs, &reponames) != EPKG_OK)
225
goto cleanup;
226
227
pkg_jobs_set_flags(jobs, f);
228
229
if (pkg_jobs_add(jobs, match, argv, argc) == EPKG_FATAL)
230
goto cleanup;
231
232
if (pkg_jobs_solve(jobs) != EPKG_OK)
233
goto cleanup;
234
235
status = EXIT_SUCCESS;
236
while ((nbactions = pkg_jobs_count(jobs)) > 0) {
237
rc = yes;
238
/* print a summary before applying the jobs */
239
if (!quiet || dry_run) {
240
print_jobs_summary(jobs,
241
"The following %d package(s) will be affected (of %d checked):\n\n",
242
nbactions, pkg_jobs_total(jobs));
243
}
244
if (dry_run)
245
break;
246
247
if (!quiet) {
248
rc = query_yesno(false,
249
"\nProceed with this action? ");
250
}
251
252
if (rc) {
253
retcode = pkg_jobs_apply(jobs);
254
done = 1;
255
if (retcode == EPKG_CONFLICT) {
256
printf("Conflicts with the existing packages "
257
"have been found.\nOne more solver "
258
"iteration is needed to resolve them.\n");
259
continue;
260
}
261
else if (retcode != EPKG_OK) {
262
status = retcode;
263
goto cleanup;
264
}
265
}
266
267
if (messages != NULL) {
268
fflush(messages->fp);
269
printf("%s", messages->buf);
270
}
271
break;
272
}
273
274
if (done == 0 && rc)
275
printf("The most recent versions of packages are already installed\n");
276
277
if (!rc)
278
status = EXIT_FAILURE;
279
280
cleanup:
281
pkgdb_release_lock(db, lock_type);
282
pkg_jobs_free(jobs);
283
pkgdb_close(db);
284
285
if (!dry_run)
286
pkg_cache_full_clean();
287
288
if (!rc && newpkgversion)
289
newpkgversion = false;
290
291
return (status);
292
}
293
294