Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/src/install.c
2065 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
{ "yes", no_argument, NULL, 'y' },
92
{ NULL, 0, NULL, 0 },
93
};
94
95
if (STREQ(argv[0], "add")) {
96
auto_update = false;
97
local_only = true;
98
yes = true;
99
quiet = true;
100
}
101
102
while ((ch = getopt_long(argc, argv, "+ACfFgiIlMnqr:RUxy", longopts, NULL)) != -1) {
103
switch (ch) {
104
case 'A':
105
f |= PKG_FLAG_AUTOMATIC;
106
break;
107
case 'C':
108
pkgdb_set_case_sensitivity(true);
109
break;
110
case 'f':
111
f |= PKG_FLAG_FORCE;
112
break;
113
case 'F':
114
f |= PKG_FLAG_SKIP_INSTALL;
115
lock_type = PKGDB_LOCK_READONLY;
116
break;
117
case 'g':
118
match = MATCH_GLOB;
119
break;
120
case 'i':
121
pkgdb_set_case_sensitivity(false);
122
break;
123
case 'I':
124
f |= PKG_FLAG_NOSCRIPT;
125
break;
126
case 'l':
127
local_only = true;
128
auto_update = false;
129
break;
130
case 'M':
131
f |= PKG_FLAG_FORCE_MISSING;
132
break;
133
case 'n':
134
f |= PKG_FLAG_DRY_RUN;
135
lock_type = PKGDB_LOCK_READONLY;
136
dry_run = true;
137
break;
138
case 'q':
139
quiet = true;
140
break;
141
case 'r':
142
vec_push(&reponames, optarg);
143
break;
144
case 'R':
145
f |= PKG_FLAG_RECURSIVE;
146
break;
147
case 'U':
148
auto_update = false;
149
break;
150
case 'x':
151
match = MATCH_REGEX;
152
break;
153
case 'y':
154
yes = true;
155
break;
156
case 0:
157
if (scriptnoexec == 1)
158
f |= PKG_FLAG_NOEXEC;
159
break;
160
default:
161
usage_install();
162
return (EXIT_FAILURE);
163
}
164
}
165
argc -= optind;
166
argv += optind;
167
168
if (argc < 1) {
169
usage_install();
170
return (EXIT_FAILURE);
171
}
172
173
if (dry_run && !auto_update)
174
mode = PKGDB_MODE_READ;
175
else
176
mode = PKGDB_MODE_READ |
177
PKGDB_MODE_WRITE |
178
PKGDB_MODE_CREATE;
179
if (local_only)
180
repo_type = PKGDB_DB_LOCAL;
181
else
182
repo_type = PKGDB_DB_LOCAL|PKGDB_DB_REPO;
183
184
/* It is safe to use both `reponame` and `NULL` here */
185
retcode = pkgdb_access2(mode, repo_type, &reponames);
186
187
if (retcode == EPKG_ENOACCESS && dry_run) {
188
auto_update = false;
189
retcode = pkgdb_access2(PKGDB_MODE_READ,
190
repo_type, &reponames);
191
}
192
193
if (retcode == EPKG_ENOACCESS) {
194
warnx("Insufficient privileges to install packages");
195
return (EXIT_FAILURE);
196
} else if (retcode != EPKG_OK)
197
return (EXIT_FAILURE);
198
199
/* first update the remote repositories if needed */
200
if (auto_update && pkg_repos_total_count() > 0 &&
201
(updcode = pkgcli_update(false, false, &reponames)) != EPKG_OK)
202
return (updcode);
203
204
if (pkgdb_open_all2(&db,
205
local_only ? PKGDB_DEFAULT : PKGDB_MAYBE_REMOTE,
206
&reponames) != EPKG_OK)
207
return (EXIT_FAILURE);
208
209
if (pkgdb_obtain_lock(db, lock_type) != EPKG_OK) {
210
pkgdb_close(db);
211
warnx("Cannot get an advisory lock on a database, it is locked by another process");
212
return (EXIT_FAILURE);
213
}
214
215
status = EXIT_FAILURE;
216
if (pkg_jobs_new(&jobs, PKG_JOBS_INSTALL, db) != EPKG_OK)
217
goto cleanup;
218
219
if (!local_only && reponames.len > 0 &&
220
pkg_jobs_set_repositories(jobs, &reponames) != EPKG_OK)
221
goto cleanup;
222
223
pkg_jobs_set_flags(jobs, f);
224
225
if (pkg_jobs_add(jobs, match, argv, argc) == EPKG_FATAL)
226
goto cleanup;
227
228
if (pkg_jobs_solve(jobs) != EPKG_OK)
229
goto cleanup;
230
231
status = EXIT_SUCCESS;
232
while ((nbactions = pkg_jobs_count(jobs)) > 0) {
233
rc = yes;
234
/* print a summary before applying the jobs */
235
if (!quiet || dry_run) {
236
print_jobs_summary(jobs,
237
"The following %d package(s) will be affected (of %d checked):\n\n",
238
nbactions, pkg_jobs_total(jobs));
239
}
240
if (dry_run)
241
break;
242
243
if (!quiet) {
244
rc = query_yesno(false,
245
"\nProceed with this action? ");
246
}
247
248
if (rc) {
249
retcode = pkg_jobs_apply(jobs);
250
done = 1;
251
if (retcode == EPKG_CONFLICT) {
252
printf("Conflicts with the existing packages "
253
"have been found.\nOne more solver "
254
"iteration is needed to resolve them.\n");
255
continue;
256
}
257
else if (retcode != EPKG_OK) {
258
status = retcode;
259
goto cleanup;
260
}
261
}
262
263
if (messages != NULL) {
264
fflush(messages->fp);
265
printf("%s", messages->buf);
266
}
267
break;
268
}
269
270
if (done == 0 && rc)
271
printf("The most recent versions of packages are already installed\n");
272
273
if (!rc)
274
status = EXIT_FAILURE;
275
276
cleanup:
277
pkgdb_release_lock(db, lock_type);
278
pkg_jobs_free(jobs);
279
pkgdb_close(db);
280
281
if (!dry_run)
282
pkg_cache_full_clean();
283
284
if (!rc && newpkgversion)
285
newpkgversion = false;
286
287
return (status);
288
}
289
290