-
Notifications
You must be signed in to change notification settings - Fork 121
/
transaction_commit.c
417 lines (387 loc) · 12.3 KB
/
transaction_commit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*-
* Copyright (c) 2009-2015 Juan Romero Pardines.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <limits.h>
#include <locale.h>
#include "xbps_api_impl.h"
/**
* @file lib/transaction_commit.c
* @brief Transaction handling routines
* @defgroup transaction Transaction handling functions
*
* The following image shows off the full transaction dictionary returned
* by xbps_transaction_prepare().
*
* @image html images/xbps_transaction_dictionary.png
*
* Legend:
* - <b>Salmon bg box</b>: The transaction dictionary.
* - <b>White bg box</b>: mandatory objects.
* - <b>Grey bg box</b>: optional objects.
* - <b>Green bg box</b>: possible value set in the object, only one of them
* will be set.
*
* Text inside of white boxes are the key associated with the object, its
* data type is specified on its edge, i.e string, array, integer, dictionary.
*/
static int
check_binpkgs(struct xbps_handle *xhp, xbps_object_iterator_t iter)
{
xbps_object_t obj;
struct xbps_repo *repo;
const char *pkgver, *repoloc, *trans, *sha256;
char *binfile;
int rv = 0;
while ((obj = xbps_object_iterator_next(iter)) != NULL) {
xbps_dictionary_get_cstring_nocopy(obj, "transaction", &trans);
if ((strcmp(trans, "remove") == 0) ||
(strcmp(trans, "hold") == 0) ||
(strcmp(trans, "configure") == 0))
continue;
xbps_dictionary_get_cstring_nocopy(obj, "repository", &repoloc);
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
binfile = xbps_repository_pkg_path(xhp, obj);
if (binfile == NULL) {
rv = ENOMEM;
break;
}
/*
* For pkgs in local repos check the sha256 hash.
* For pkgs in remote repos check the RSA signature.
*/
if ((repo = xbps_rpool_get_repo(repoloc)) == NULL) {
rv = errno;
xbps_dbg_printf(xhp, "%s: failed to get repository "
"%s: %s\n", pkgver, repoloc, strerror(errno));
break;
}
if (repo->is_remote) {
/* remote repo */
xbps_set_cb_state(xhp, XBPS_STATE_VERIFY, 0, pkgver,
"%s: verifying RSA signature...", pkgver);
if (!xbps_verify_file_signature(repo, binfile)) {
char *sigfile;
rv = EPERM;
xbps_set_cb_state(xhp, XBPS_STATE_VERIFY_FAIL, rv, pkgver,
"%s: the RSA signature is not valid!", pkgver);
xbps_set_cb_state(xhp, XBPS_STATE_VERIFY_FAIL, rv, pkgver,
"%s: removed pkg archive and its signature.", pkgver);
(void)remove(binfile);
sigfile = xbps_xasprintf("%s.sig", binfile);
(void)remove(sigfile);
free(sigfile);
free(binfile);
break;
}
} else {
/* local repo */
xbps_set_cb_state(xhp, XBPS_STATE_VERIFY, 0, pkgver,
"%s: verifying SHA256 hash...", pkgver);
xbps_dictionary_get_cstring_nocopy(obj, "filename-sha256", &sha256);
if ((rv = xbps_file_hash_check(binfile, sha256)) != 0) {
xbps_set_cb_state(xhp, XBPS_STATE_VERIFY_FAIL, rv, pkgver,
"%s: SHA256 hash is not valid: %s", pkgver, strerror(rv));
free(binfile);
break;
}
}
free(binfile);
}
xbps_object_iterator_reset(iter);
return rv;
}
static int
download_binpkgs(struct xbps_handle *xhp, xbps_object_iterator_t iter)
{
xbps_object_t obj;
const char *pkgver, *arch, *fetchstr, *repoloc, *trans;
char *file, *sigfile;
int rv = 0;
while ((obj = xbps_object_iterator_next(iter)) != NULL) {
xbps_dictionary_get_cstring_nocopy(obj, "transaction", &trans);
if ((strcmp(trans, "remove") == 0) ||
(strcmp(trans, "hold") == 0) ||
(strcmp(trans, "configure") == 0))
continue;
xbps_dictionary_get_cstring_nocopy(obj, "repository", &repoloc);
if (!xbps_repository_is_remote(repoloc))
continue;
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
xbps_dictionary_get_cstring_nocopy(obj, "architecture", &arch);
/*
* Download binary package.
*/
if ((file = xbps_repository_pkg_path(xhp, obj)) == NULL) {
rv = EINVAL;
break;
}
if (access(file, R_OK) == -1) {
xbps_set_cb_state(xhp, XBPS_STATE_DOWNLOAD, 0, pkgver,
"Downloading `%s' package (from `%s')...", pkgver, repoloc);
if ((rv = xbps_fetch_file(xhp, file, NULL)) == -1) {
rv = fetchLastErrCode ? fetchLastErrCode : errno;
fetchstr = xbps_fetch_error_string();
xbps_set_cb_state(xhp, XBPS_STATE_DOWNLOAD_FAIL, rv,
pkgver, "[trans] failed to download `%s' package from `%s': %s",
pkgver, repoloc, fetchstr ? fetchstr : strerror(rv));
free(file);
break;
}
rv = 0;
}
/*
* Download binary package signature.
*/
sigfile = xbps_xasprintf("%s.sig", file);
free(file);
file = NULL;
if (access(sigfile, R_OK) == -1) {
xbps_set_cb_state(xhp, XBPS_STATE_DOWNLOAD, 0, pkgver,
"Downloading `%s' signature (from `%s')...", pkgver, repoloc);
file = xbps_xasprintf("%s/%s.%s.xbps.sig", repoloc, pkgver, arch);
if ((rv = xbps_fetch_file(xhp, file, NULL)) == -1) {
rv = fetchLastErrCode ? fetchLastErrCode : errno;
fetchstr = xbps_fetch_error_string();
xbps_set_cb_state(xhp, XBPS_STATE_DOWNLOAD_FAIL, rv,
pkgver, "[trans] failed to download `%s' signature from `%s': %s",
pkgver, repoloc, fetchstr ? fetchstr : strerror(rv));
free(sigfile);
free(file);
break;
}
rv = 0;
}
free(sigfile);
if (file != NULL)
free(file);
}
xbps_object_iterator_reset(iter);
return rv;
}
int
xbps_transaction_commit(struct xbps_handle *xhp)
{
xbps_object_t obj;
xbps_object_iterator_t iter;
const char *pkgver, *tract;
int rv = 0;
bool update;
setlocale(LC_ALL, "");
assert(xbps_object_type(xhp->transd) == XBPS_TYPE_DICTIONARY);
/*
* Create cachedir if necessary.
*/
if (xbps_mkpath(xhp->cachedir, 0755) == -1) {
if (errno != EEXIST) {
xbps_set_cb_state(xhp, XBPS_STATE_TRANS_FAIL,
errno, NULL,
"[trans] cannot create cachedir `%s': %s",
xhp->cachedir, strerror(errno));
return errno;
}
}
if (chdir(xhp->cachedir) == -1) {
xbps_set_cb_state(xhp, XBPS_STATE_TRANS_FAIL,
errno, NULL,
"[trans] failed to change dir to cachedir `%s': %s",
xhp->cachedir, strerror(errno));
return errno;
}
iter = xbps_array_iter_from_dict(xhp->transd, "packages");
if (iter == NULL)
return EINVAL;
/*
* Download binary packages (if they come from a remote repository).
*/
xbps_set_cb_state(xhp, XBPS_STATE_TRANS_DOWNLOAD, 0, NULL, NULL);
if ((rv = download_binpkgs(xhp, iter)) != 0) {
xbps_dbg_printf(xhp, "[trans] failed to download binpkgs: "
"%s\n", strerror(rv));
goto out;
}
/*
* Check binary package integrity.
*/
xbps_set_cb_state(xhp, XBPS_STATE_TRANS_VERIFY, 0, NULL, NULL);
if ((rv = check_binpkgs(xhp, iter)) != 0) {
xbps_dbg_printf(xhp, "[trans] failed to check binpkgs: "
"%s\n", strerror(rv));
goto out;
}
/*
* Install, update, configure or remove packages as specified
* in the transaction dictionary.
*/
xbps_set_cb_state(xhp, XBPS_STATE_TRANS_RUN, 0, NULL, NULL);
/*
* Create rootdir if necessary.
*/
if (xbps_mkpath(xhp->rootdir, 0750) == -1) {
rv = errno;
if (rv != EEXIST) {
xbps_set_cb_state(xhp, XBPS_STATE_TRANS_FAIL, errno, xhp->rootdir,
"[trans] failed to create rootdir `%s': %s",
xhp->rootdir, strerror(rv));
goto out;
}
}
if (chdir(xhp->rootdir) == -1) {
rv = errno;
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, rv, xhp->rootdir,
"[trans] failed to chdir to rootdir `%s': %s",
xhp->rootdir, strerror(errno));
goto out;
}
while ((obj = xbps_object_iterator_next(iter)) != NULL) {
xbps_dictionary_get_cstring_nocopy(obj, "transaction", &tract);
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
if (strcmp(tract, "remove") == 0) {
/*
* Remove package.
*/
update = false;
xbps_dictionary_get_bool(obj, "remove-and-update", &update);
rv = xbps_remove_pkg(xhp, pkgver, update);
if (rv != 0) {
xbps_dbg_printf(xhp, "[trans] failed to "
"remove %s: %s\n", pkgver, strerror(rv));
goto out;
}
continue;
} else if (strcmp(tract, "configure") == 0) {
/*
* Reconfigure pending package.
*/
rv = xbps_configure_pkg(xhp, pkgver, false, false);
if (rv != 0) {
xbps_dbg_printf(xhp, "[trans] failed to "
"configure %s: %s\n", pkgver, strerror(rv));
goto out;
}
continue;
} else if (strcmp(tract, "update") == 0) {
/*
* Update a package: execute pre-remove action of
* existing package before unpacking new version.
*/
xbps_set_cb_state(xhp, XBPS_STATE_UPDATE, 0, pkgver, NULL);
rv = xbps_remove_pkg(xhp, pkgver, true);
if (rv != 0) {
xbps_set_cb_state(xhp,
XBPS_STATE_UPDATE_FAIL,
rv, pkgver,
"%s: [trans] failed to update "
"package `%s'", pkgver,
strerror(rv));
goto out;
}
} else if (strcmp(tract, "hold") == 0) {
/*
* Package is on hold mode, ignore it.
*/
continue;
} else {
/* Install a package */
xbps_set_cb_state(xhp, XBPS_STATE_INSTALL, 0,
pkgver, NULL);
}
/*
* Unpack binary package.
*/
if ((rv = xbps_unpack_binary_pkg(xhp, obj)) != 0) {
xbps_dbg_printf(xhp, "[trans] failed to unpack "
"%s: %s\n", pkgver, strerror(rv));
goto out;
}
/*
* Register package.
*/
if ((rv = xbps_register_pkg(xhp, obj)) != 0) {
xbps_dbg_printf(xhp, "[trans] failed to register "
"%s: %s\n", pkgver, strerror(rv));
goto out;
}
}
/* if there are no packages to install or update we are done */
if (!xbps_dictionary_get(xhp->transd, "total-update-pkgs") &&
!xbps_dictionary_get(xhp->transd, "total-install-pkgs"))
goto out;
if (xhp->target_arch && strcmp(xhp->native_arch, xhp->target_arch)) {
/* if installing packages for target_arch, don't configure anything */
goto out;
/* do not configure packages if only unpacking is desired */
} else if (xhp->flags & XBPS_FLAG_UNPACK_ONLY) {
goto out;
}
xbps_object_iterator_reset(iter);
/* Force a pkgdb write for all unpacked pkgs in transaction */
(void)xbps_pkgdb_update(xhp, true, true);
/*
* Configure all unpacked packages.
*/
xbps_set_cb_state(xhp, XBPS_STATE_TRANS_CONFIGURE, 0, NULL, NULL);
while ((obj = xbps_object_iterator_next(iter)) != NULL) {
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
xbps_dictionary_get_cstring_nocopy(obj, "transaction", &tract);
if ((strcmp(tract, "remove") == 0) ||
(strcmp(tract, "hold") == 0) ||
(strcmp(tract, "configure") == 0)) {
xbps_dbg_printf(xhp, "%s: skipping configuration for "
"%s: %s\n", __func__, pkgver, tract);
continue;
}
update = false;
if (strcmp(tract, "update") == 0)
update = true;
rv = xbps_configure_pkg(xhp, pkgver, false, update);
if (rv != 0) {
xbps_dbg_printf(xhp, "%s: configure failed for "
"%s: %s\n", __func__, pkgver, strerror(rv));
goto out;
}
/*
* Notify client callback when a package has been
* installed or updated.
*/
if (update) {
xbps_set_cb_state(xhp, XBPS_STATE_UPDATE_DONE, 0,
pkgver, NULL);
} else {
xbps_set_cb_state(xhp, XBPS_STATE_INSTALL_DONE, 0,
pkgver, NULL);
}
}
out:
xbps_object_iterator_release(iter);
/* Force a pkgdb write for all unpacked pkgs in transaction */
(void)xbps_pkgdb_update(xhp, true, true);
return rv;
}