Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/src/fetch.c
2065 views
1
/*-
2
* Copyright (c) 2011-2012 Marin Atanasov Nikolov <[email protected]>
3
* Copyright (c) 2013-2014 Matthew Seaman <[email protected]>
4
* Copyright (c) 2012-2013 Bryan Drewery <[email protected]>
5
* Copyright (c) 2016 Vsevolod Stakhov <[email protected]>
6
* All rights reserved.
7
*
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
10
* are met:
11
* 1. Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer
13
* in this position and unchanged.
14
* 2. Redistributions in binary form must reproduce the above copyright
15
* notice, this list of conditions and the following disclaimer in the
16
* documentation and/or other materials provided with the distribution.
17
*
18
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
19
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
22
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
*/
29
30
#include <sys/types.h>
31
32
#include <err.h>
33
#include <getopt.h>
34
#include <libgen.h>
35
#include <stdbool.h>
36
#include <stdio.h>
37
#include <string.h>
38
#include <unistd.h>
39
40
#include <pkg.h>
41
42
#include "pkgcli.h"
43
44
void
45
usage_fetch(void)
46
{
47
fprintf(stderr, "Usage: pkg fetch [-r reponame] [-o destdir] [-dqUy] "
48
"[-Cgix] <pkg-name> <...>\n");
49
fprintf(stderr, " pkg fetch [-r reponame] [-dqUy] -a\n");
50
fprintf(stderr, " pkg fetch [-r reponame] [-dqUy] -u\n\n");
51
fprintf(stderr, "For more information see 'pkg help fetch'.\n");
52
}
53
54
int
55
exec_fetch(int argc, char **argv)
56
{
57
struct pkgdb *db = NULL;
58
struct pkg_jobs *jobs = NULL;
59
const char *destdir = NULL;
60
int ch;
61
int retcode;
62
int status = EXIT_FAILURE;
63
bool upgrades_for_installed = false, rc, csum_only = false;
64
unsigned mode;
65
match_t match = MATCH_EXACT;
66
pkg_flags f = PKG_FLAG_NONE;
67
c_charv_t reponames = vec_init();
68
69
struct option longopts[] = {
70
{ "all", no_argument, NULL, 'a' },
71
{ "case-sensitive", no_argument, NULL, 'C' },
72
{ "dependencies", no_argument, NULL, 'd' },
73
{ "glob", no_argument, NULL, 'g' },
74
{ "case-insensitive", no_argument, NULL, 'i' },
75
{ "quiet", no_argument, NULL, 'q' },
76
{ "repository", required_argument, NULL, 'r' },
77
{ "available-updates", no_argument, NULL, 'u' },
78
{ "no-repo-update", no_argument, NULL, 'U' },
79
{ "regex", no_argument, NULL, 'x' },
80
{ "yes", no_argument, NULL, 'y' },
81
{ "output", required_argument, NULL, 'o' },
82
{ NULL, 0, NULL, 0 },
83
};
84
85
while ((ch = getopt_long(argc, argv, "+aCdgiqr:Uuxyo:", longopts, NULL)) != -1) {
86
switch (ch) {
87
case 'a':
88
match = MATCH_ALL;
89
break;
90
case 'C':
91
pkgdb_set_case_sensitivity(true);
92
break;
93
case 'd':
94
f |= PKG_FLAG_WITH_DEPS | PKG_FLAG_RECURSIVE;
95
break;
96
case 'g':
97
match = MATCH_GLOB;
98
break;
99
case 'i':
100
pkgdb_set_case_sensitivity(false);
101
break;
102
case 'q':
103
quiet = true;
104
break;
105
case 'r':
106
vec_push(&reponames, optarg);
107
break;
108
case 'u':
109
f |= PKG_FLAG_UPGRADES_FOR_INSTALLED;
110
upgrades_for_installed = true;
111
break;
112
case 'U':
113
auto_update = false;
114
break;
115
case 'x':
116
match = MATCH_REGEX;
117
break;
118
case 'y':
119
yes = true;
120
break;
121
case 'o':
122
f |= PKG_FLAG_FETCH_MIRROR;
123
destdir = optarg;
124
break;
125
default:
126
usage_fetch();
127
return (EXIT_FAILURE);
128
}
129
}
130
argc -= optind;
131
argv += optind;
132
133
if (argc < 1 && match != MATCH_ALL && !upgrades_for_installed) {
134
usage_fetch();
135
return (EXIT_FAILURE);
136
}
137
138
if (match == MATCH_ALL && upgrades_for_installed) {
139
usage_fetch();
140
return (EXIT_FAILURE);
141
}
142
143
if (auto_update)
144
mode = PKGDB_MODE_READ|PKGDB_MODE_WRITE|PKGDB_MODE_CREATE;
145
else
146
mode = PKGDB_MODE_READ;
147
148
retcode = pkgdb_access2(mode, PKGDB_DB_REPO, &reponames);
149
150
if (retcode == EPKG_ENOACCESS) {
151
warnx("Insufficient privileges to access repo catalogue");
152
return (EXIT_FAILURE);
153
} else if (retcode != EPKG_OK)
154
return (EXIT_FAILURE);
155
156
if (upgrades_for_installed) {
157
retcode = pkgdb_access(PKGDB_MODE_READ, PKGDB_DB_LOCAL);
158
159
if (retcode == EPKG_ENOACCESS) {
160
warnx("Insufficient privileges to access the package database");
161
return (EXIT_FAILURE);
162
} else if (retcode != EPKG_OK)
163
return (EXIT_FAILURE);
164
}
165
166
/* first update the remote repositories if needed */
167
if (auto_update &&
168
(retcode = pkgcli_update(false, false, &reponames)) != EPKG_OK)
169
return (retcode);
170
171
if (pkgdb_open_all2(&db, PKGDB_REMOTE, &reponames) != EPKG_OK)
172
return (EXIT_FAILURE);
173
174
if (pkgdb_obtain_lock(db, PKGDB_LOCK_READONLY) != EPKG_OK) {
175
pkgdb_close(db);
176
warnx("Cannot get a read lock on a database, it is locked by another process");
177
return (EXIT_FAILURE);
178
}
179
180
181
if (pkg_jobs_new(&jobs, PKG_JOBS_FETCH, db) != EPKG_OK)
182
goto cleanup;
183
184
if (pkg_jobs_set_repositories(jobs, &reponames) != EPKG_OK)
185
goto cleanup;
186
187
if (destdir != NULL && pkg_jobs_set_destdir(jobs, destdir) != EPKG_OK)
188
goto cleanup;
189
190
pkg_jobs_set_flags(jobs, f);
191
192
if (!upgrades_for_installed &&
193
pkg_jobs_add(jobs, match, argv, argc) != EPKG_OK)
194
goto cleanup;
195
196
if (pkg_jobs_solve(jobs) != EPKG_OK)
197
goto cleanup;
198
199
if (pkg_jobs_count(jobs) == 0)
200
goto cleanup;
201
202
if (!quiet) {
203
204
rc = print_jobs_summary(jobs,
205
"The following packages will be fetched:\n\n");
206
207
if (rc != 0) {
208
rc = query_yesno(false, "\nProceed with fetching "
209
"packages? ");
210
} else {
211
printf("No packages are required to be fetched.\n");
212
rc = query_yesno(false, "Check the integrity of packages "
213
"downloaded? ");
214
csum_only = true;
215
}
216
}
217
else {
218
rc = true;
219
}
220
221
if (!rc || (retcode = pkg_jobs_apply(jobs)) != EPKG_OK)
222
goto cleanup;
223
224
if (csum_only && !quiet)
225
printf("Integrity check was successful.\n");
226
227
status = EXIT_SUCCESS;
228
229
cleanup:
230
pkg_jobs_free(jobs);
231
pkgdb_release_lock(db, PKGDB_LOCK_READONLY);
232
pkgdb_close(db);
233
234
return (status);
235
}
236
237