1 /*
2 * trans.c
3 *
4 * Copyright (c) 2006-2018 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
6 * Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
7 * Copyright (c) 2005 by Christian Hamar <krics@linuxforum.hu>
8 * Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <errno.h>
30 #include <limits.h>
31
32 /* libalpm */
33 #include "trans.h"
34 #include "alpm_list.h"
35 #include "package.h"
36 #include "util.h"
37 #include "log.h"
38 #include "handle.h"
39 #include "remove.h"
40 #include "sync.h"
41 #include "alpm.h"
42 #include "deps.h"
43 #include "hook.h"
44
45 /** \addtogroup alpm_trans Transaction Functions
46 * @brief Functions to manipulate libalpm transactions
47 * @{
48 */
49
50 /** Initialize the transaction. */
alpm_trans_init(alpm_handle_t * handle,int flags)51 int SYMEXPORT alpm_trans_init(alpm_handle_t *handle, int flags)
52 {
53 alpm_trans_t *trans;
54
55 /* Sanity checks */
56 CHECK_HANDLE(handle, return -1);
57 ASSERT(handle->trans == NULL, RET_ERR(handle, ALPM_ERR_TRANS_NOT_NULL, -1));
58
59 /* lock db */
60 if(!(flags & ALPM_TRANS_FLAG_NOLOCK)) {
61 if(_alpm_handle_lock(handle)) {
62 RET_ERR(handle, ALPM_ERR_HANDLE_LOCK, -1);
63 }
64 }
65
66 CALLOC(trans, 1, sizeof(alpm_trans_t), RET_ERR(handle, ALPM_ERR_MEMORY, -1));
67 trans->flags = flags;
68 trans->state = STATE_INITIALIZED;
69
70 handle->trans = trans;
71
72 return 0;
73 }
74
check_arch(alpm_handle_t * handle,alpm_list_t * pkgs)75 static alpm_list_t *check_arch(alpm_handle_t *handle, alpm_list_t *pkgs)
76 {
77 alpm_list_t *i;
78 alpm_list_t *invalid = NULL;
79
80 const char *arch = handle->arch;
81 if(!arch) {
82 return NULL;
83 }
84 for(i = pkgs; i; i = i->next) {
85 alpm_pkg_t *pkg = i->data;
86 const char *pkgarch = alpm_pkg_get_arch(pkg);
87 if(pkgarch && strcmp(pkgarch, arch) && strcmp(pkgarch, "any")) {
88 char *string;
89 const char *pkgname = pkg->name;
90 const char *pkgver = pkg->version;
91 size_t len = strlen(pkgname) + strlen(pkgver) + strlen(pkgarch) + 3;
92 MALLOC(string, len, RET_ERR(handle, ALPM_ERR_MEMORY, invalid));
93 sprintf(string, "%s-%s-%s", pkgname, pkgver, pkgarch);
94 invalid = alpm_list_add(invalid, string);
95 }
96 }
97 return invalid;
98 }
99
100 /** Prepare a transaction. */
alpm_trans_prepare(alpm_handle_t * handle,alpm_list_t ** data)101 int SYMEXPORT alpm_trans_prepare(alpm_handle_t *handle, alpm_list_t **data)
102 {
103 alpm_trans_t *trans;
104
105 /* Sanity checks */
106 CHECK_HANDLE(handle, return -1);
107 ASSERT(data != NULL, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1));
108
109 trans = handle->trans;
110
111 ASSERT(trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
112 ASSERT(trans->state == STATE_INITIALIZED, RET_ERR(handle, ALPM_ERR_TRANS_NOT_INITIALIZED, -1));
113
114 /* If there's nothing to do, return without complaining */
115 if(trans->add == NULL && trans->remove == NULL) {
116 return 0;
117 }
118
119 alpm_list_t *invalid = check_arch(handle, trans->add);
120 if(invalid) {
121 if(data) {
122 *data = invalid;
123 }
124 RET_ERR(handle, ALPM_ERR_PKG_INVALID_ARCH, -1);
125 }
126
127 if(trans->add == NULL) {
128 if(_alpm_remove_prepare(handle, data) == -1) {
129 /* pm_errno is set by _alpm_remove_prepare() */
130 return -1;
131 }
132 } else {
133 if(_alpm_sync_prepare(handle, data) == -1) {
134 /* pm_errno is set by _alpm_sync_prepare() */
135 return -1;
136 }
137 }
138
139
140 if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
141 _alpm_log(handle, ALPM_LOG_DEBUG, "sorting by dependencies\n");
142 if(trans->add) {
143 alpm_list_t *add_orig = trans->add;
144 trans->add = _alpm_sortbydeps(handle, add_orig, trans->remove, 0);
145 alpm_list_free(add_orig);
146 }
147 if(trans->remove) {
148 alpm_list_t *rem_orig = trans->remove;
149 trans->remove = _alpm_sortbydeps(handle, rem_orig, NULL, 1);
150 alpm_list_free(rem_orig);
151 }
152 }
153
154 trans->state = STATE_PREPARED;
155
156 return 0;
157 }
158
159 /** Commit a transaction. */
alpm_trans_commit(alpm_handle_t * handle,alpm_list_t ** data)160 int SYMEXPORT alpm_trans_commit(alpm_handle_t *handle, alpm_list_t **data)
161 {
162 alpm_trans_t *trans;
163 alpm_event_any_t event;
164
165 /* Sanity checks */
166 CHECK_HANDLE(handle, return -1);
167
168 trans = handle->trans;
169
170 ASSERT(trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
171 ASSERT(trans->state == STATE_PREPARED, RET_ERR(handle, ALPM_ERR_TRANS_NOT_PREPARED, -1));
172
173 ASSERT(!(trans->flags & ALPM_TRANS_FLAG_NOLOCK), RET_ERR(handle, ALPM_ERR_TRANS_NOT_LOCKED, -1));
174
175 /* If there's nothing to do, return without complaining */
176 if(trans->add == NULL && trans->remove == NULL) {
177 return 0;
178 }
179
180 if(trans->add) {
181 if(_alpm_sync_load(handle, data) != 0) {
182 /* pm_errno is set by _alpm_sync_load() */
183 return -1;
184 }
185 if(trans->flags & ALPM_TRANS_FLAG_DOWNLOADONLY) {
186 return 0;
187 }
188 if(_alpm_sync_check(handle, data) != 0) {
189 /* pm_errno is set by _alpm_sync_check() */
190 return -1;
191 }
192 }
193
194 if(_alpm_hook_run(handle, ALPM_HOOK_PRE_TRANSACTION) != 0) {
195 RET_ERR(handle, ALPM_ERR_TRANS_HOOK_FAILED, -1);
196 }
197
198 trans->state = STATE_COMMITING;
199
200 alpm_logaction(handle, ALPM_CALLER_PREFIX, "transaction started\n");
201 event.type = ALPM_EVENT_TRANSACTION_START;
202 EVENT(handle, (void *)&event);
203
204 if(trans->add == NULL) {
205 if(_alpm_remove_packages(handle, 1) == -1) {
206 /* pm_errno is set by _alpm_remove_packages() */
207 alpm_errno_t save = handle->pm_errno;
208 alpm_logaction(handle, ALPM_CALLER_PREFIX, "transaction failed\n");
209 handle->pm_errno = save;
210 return -1;
211 }
212 } else {
213 if(_alpm_sync_commit(handle) == -1) {
214 /* pm_errno is set by _alpm_sync_commit() */
215 alpm_errno_t save = handle->pm_errno;
216 alpm_logaction(handle, ALPM_CALLER_PREFIX, "transaction failed\n");
217 handle->pm_errno = save;
218 return -1;
219 }
220 }
221
222 if(trans->state == STATE_INTERRUPTED) {
223 alpm_logaction(handle, ALPM_CALLER_PREFIX, "transaction interrupted\n");
224 } else {
225 event.type = ALPM_EVENT_TRANSACTION_DONE;
226 EVENT(handle, (void *)&event);
227 alpm_logaction(handle, ALPM_CALLER_PREFIX, "transaction completed\n");
228 _alpm_hook_run(handle, ALPM_HOOK_POST_TRANSACTION);
229 }
230
231 trans->state = STATE_COMMITED;
232
233 return 0;
234 }
235
236 /** Interrupt a transaction.
237 * @note Safe to call from inside signal handlers.
238 */
alpm_trans_interrupt(alpm_handle_t * handle)239 int SYMEXPORT alpm_trans_interrupt(alpm_handle_t *handle)
240 {
241 alpm_trans_t *trans;
242
243 /* Sanity checks */
244 CHECK_HANDLE(handle, return -1);
245
246 trans = handle->trans;
247 ASSERT(trans != NULL, RET_ERR_ASYNC_SAFE(handle, ALPM_ERR_TRANS_NULL, -1));
248 ASSERT(trans->state == STATE_COMMITING || trans->state == STATE_INTERRUPTED,
249 RET_ERR_ASYNC_SAFE(handle, ALPM_ERR_TRANS_TYPE, -1));
250
251 trans->state = STATE_INTERRUPTED;
252
253 return 0;
254 }
255
256 /** Release a transaction. */
alpm_trans_release(alpm_handle_t * handle)257 int SYMEXPORT alpm_trans_release(alpm_handle_t *handle)
258 {
259 alpm_trans_t *trans;
260
261 /* Sanity checks */
262 CHECK_HANDLE(handle, return -1);
263
264 trans = handle->trans;
265 ASSERT(trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
266 ASSERT(trans->state != STATE_IDLE, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
267
268 int nolock_flag = trans->flags & ALPM_TRANS_FLAG_NOLOCK;
269
270 _alpm_trans_free(trans);
271 handle->trans = NULL;
272
273 /* unlock db */
274 if(!nolock_flag) {
275 _alpm_handle_unlock(handle);
276 }
277
278 return 0;
279 }
280
281 /** @} */
282
_alpm_trans_free(alpm_trans_t * trans)283 void _alpm_trans_free(alpm_trans_t *trans)
284 {
285 if(trans == NULL) {
286 return;
287 }
288
289 alpm_list_free_inner(trans->unresolvable,
290 (alpm_list_fn_free)_alpm_pkg_free_trans);
291 alpm_list_free(trans->unresolvable);
292 alpm_list_free_inner(trans->add, (alpm_list_fn_free)_alpm_pkg_free_trans);
293 alpm_list_free(trans->add);
294 alpm_list_free_inner(trans->remove, (alpm_list_fn_free)_alpm_pkg_free);
295 alpm_list_free(trans->remove);
296
297 FREELIST(trans->skip_remove);
298
299 FREE(trans);
300 }
301
302 /* A cheap grep for text files, returns 1 if a substring
303 * was found in the text file fn, 0 if it wasn't
304 */
grep(const char * fn,const char * needle)305 static int grep(const char *fn, const char *needle)
306 {
307 FILE *fp;
308 char *ptr;
309
310 if((fp = fopen(fn, "r")) == NULL) {
311 return 0;
312 }
313 while(!feof(fp)) {
314 char line[1024];
315 if(safe_fgets(line, sizeof(line), fp) == NULL) {
316 continue;
317 }
318 if((ptr = strchr(line, '#')) != NULL) {
319 *ptr = '\0';
320 }
321 /* TODO: this will not work if the search string
322 * ends up being split across line reads */
323 if(strstr(line, needle)) {
324 fclose(fp);
325 return 1;
326 }
327 }
328 fclose(fp);
329 return 0;
330 }
331
_alpm_runscriptlet(alpm_handle_t * handle,const char * filepath,const char * script,const char * ver,const char * oldver,int is_archive)332 int _alpm_runscriptlet(alpm_handle_t *handle, const char *filepath,
333 const char *script, const char *ver, const char *oldver, int is_archive)
334 {
335 char arg0[64], arg1[3], cmdline[PATH_MAX];
336 char *argv[] = { arg0, arg1, cmdline, NULL };
337 char *tmpdir, *scriptfn = NULL, *scriptpath;
338 int retval = 0;
339 size_t len;
340
341 if(_alpm_access(handle, NULL, filepath, R_OK) != 0) {
342 _alpm_log(handle, ALPM_LOG_DEBUG, "scriptlet '%s' not found\n", filepath);
343 return 0;
344 }
345
346 if(!is_archive && !grep(filepath, script)) {
347 /* script not found in scriptlet file; we can only short-circuit this early
348 * if it is an actual scriptlet file and not an archive. */
349 return 0;
350 }
351
352 strcpy(arg0, SCRIPTLET_SHELL);
353 strcpy(arg1, "-c");
354
355 /* create a directory in $root/tmp/ for copying/extracting the scriptlet */
356 len = strlen(handle->root) + strlen("tmp/alpm_XXXXXX") + 1;
357 MALLOC(tmpdir, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
358 snprintf(tmpdir, len, "%stmp/", handle->root);
359 if(access(tmpdir, F_OK) != 0) {
360 _alpm_makepath_mode(tmpdir, 01777);
361 }
362 snprintf(tmpdir, len, "%stmp/alpm_XXXXXX", handle->root);
363 if(mkdtemp(tmpdir) == NULL) {
364 _alpm_log(handle, ALPM_LOG_ERROR, _("could not create temp directory\n"));
365 free(tmpdir);
366 return 1;
367 }
368
369 /* either extract or copy the scriptlet */
370 len += strlen("/.INSTALL");
371 MALLOC(scriptfn, len, free(tmpdir); RET_ERR(handle, ALPM_ERR_MEMORY, -1));
372 snprintf(scriptfn, len, "%s/.INSTALL", tmpdir);
373 if(is_archive) {
374 if(_alpm_unpack_single(handle, filepath, tmpdir, ".INSTALL")) {
375 retval = 1;
376 }
377 } else {
378 if(_alpm_copyfile(filepath, scriptfn)) {
379 _alpm_log(handle, ALPM_LOG_ERROR, _("could not copy tempfile to %s (%s)\n"), scriptfn, strerror(errno));
380 retval = 1;
381 }
382 }
383 if(retval == 1) {
384 goto cleanup;
385 }
386
387 if(is_archive && !grep(scriptfn, script)) {
388 /* script not found in extracted scriptlet file */
389 goto cleanup;
390 }
391
392 /* chop off the root so we can find the tmpdir in the chroot */
393 scriptpath = scriptfn + strlen(handle->root) - 1;
394
395 if(oldver) {
396 snprintf(cmdline, PATH_MAX, ". %s; %s %s %s",
397 scriptpath, script, ver, oldver);
398 } else {
399 snprintf(cmdline, PATH_MAX, ". %s; %s %s",
400 scriptpath, script, ver);
401 }
402
403 _alpm_log(handle, ALPM_LOG_DEBUG, "executing \"%s\"\n", cmdline);
404
405 retval = _alpm_run_chroot(handle, SCRIPTLET_SHELL, argv, NULL, NULL);
406
407 cleanup:
408 if(scriptfn && unlink(scriptfn)) {
409 _alpm_log(handle, ALPM_LOG_WARNING,
410 _("could not remove %s\n"), scriptfn);
411 }
412 if(rmdir(tmpdir)) {
413 _alpm_log(handle, ALPM_LOG_WARNING,
414 _("could not remove tmpdir %s\n"), tmpdir);
415 }
416
417 free(scriptfn);
418 free(tmpdir);
419 return retval;
420 }
421
alpm_trans_get_flags(alpm_handle_t * handle)422 int SYMEXPORT alpm_trans_get_flags(alpm_handle_t *handle)
423 {
424 /* Sanity checks */
425 CHECK_HANDLE(handle, return -1);
426 ASSERT(handle->trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
427
428 return handle->trans->flags;
429 }
430
alpm_trans_get_add(alpm_handle_t * handle)431 alpm_list_t SYMEXPORT *alpm_trans_get_add(alpm_handle_t *handle)
432 {
433 /* Sanity checks */
434 CHECK_HANDLE(handle, return NULL);
435 ASSERT(handle->trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, NULL));
436
437 return handle->trans->add;
438 }
439
alpm_trans_get_remove(alpm_handle_t * handle)440 alpm_list_t SYMEXPORT *alpm_trans_get_remove(alpm_handle_t *handle)
441 {
442 /* Sanity checks */
443 CHECK_HANDLE(handle, return NULL);
444 ASSERT(handle->trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, NULL));
445
446 return handle->trans->remove;
447 }
448