4 * Copyright (c) 2006-2011 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>
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.
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.
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/>.
30 #include <sys/types.h>
37 #include "alpm_list.h"
46 /** \addtogroup alpm_trans Transaction Functions
47 * @brief Functions to manipulate libalpm transactions
51 /* Create a lock file */
52 static int make_lock(pmhandle_t
*handle
)
57 ASSERT(handle
->lockfile
!= NULL
, return -1);
59 /* create the dir of the lockfile first */
60 dir
= strdup(handle
->lockfile
);
61 ptr
= strrchr(dir
, '/');
65 if(_alpm_makepath(dir
)) {
72 fd
= open(handle
->lockfile
, O_WRONLY
| O_CREAT
| O_EXCL
, 0000);
73 } while(fd
== -1 && errno
== EINTR
);
75 FILE *f
= fdopen(fd
, "w");
76 fprintf(f
, "%ld\n", (long)getpid());
79 handle
->lckstream
= f
;
85 /* Remove a lock file */
86 static int remove_lock(pmhandle_t
*handle
)
88 if(handle
->lckstream
!= NULL
) {
89 fclose(handle
->lckstream
);
90 handle
->lckstream
= NULL
;
92 if(unlink(handle
->lockfile
) == -1 && errno
!= ENOENT
) {
98 /** Initialize the transaction. */
99 int SYMEXPORT
alpm_trans_init(pmhandle_t
*handle
, pmtransflag_t flags
,
100 alpm_trans_cb_event event
, alpm_trans_cb_conv conv
,
101 alpm_trans_cb_progress progress
)
106 CHECK_HANDLE(handle
, return -1);
107 ASSERT(handle
->trans
== NULL
, RET_ERR(handle
, PM_ERR_TRANS_NOT_NULL
, -1));
110 if(!(flags
& PM_TRANS_FLAG_NOLOCK
)) {
111 if(make_lock(handle
)) {
112 RET_ERR(handle
, PM_ERR_HANDLE_LOCK
, -1);
116 CALLOC(trans
, 1, sizeof(pmtrans_t
), RET_ERR(handle
, PM_ERR_MEMORY
, -1));
117 trans
->flags
= flags
;
118 trans
->cb_event
= event
;
119 trans
->cb_conv
= conv
;
120 trans
->cb_progress
= progress
;
121 trans
->state
= STATE_INITIALIZED
;
123 handle
->trans
= trans
;
128 static alpm_list_t
*check_arch(pmhandle_t
*handle
, alpm_list_t
*pkgs
)
131 alpm_list_t
*invalid
= NULL
;
133 const char *arch
= alpm_option_get_arch(handle
);
137 for(i
= pkgs
; i
; i
= i
->next
) {
138 pmpkg_t
*pkg
= i
->data
;
139 const char *pkgarch
= alpm_pkg_get_arch(pkg
);
140 if(pkgarch
&& strcmp(pkgarch
, arch
) && strcmp(pkgarch
, "any")) {
142 const char *pkgname
= alpm_pkg_get_name(pkg
);
143 const char *pkgver
= alpm_pkg_get_version(pkg
);
144 size_t len
= strlen(pkgname
) + strlen(pkgver
) + strlen(pkgarch
) + 3;
145 MALLOC(string
, len
, RET_ERR(handle
, PM_ERR_MEMORY
, invalid
));
146 sprintf(string
, "%s-%s-%s", pkgname
, pkgver
, pkgarch
);
147 invalid
= alpm_list_add(invalid
, string
);
153 /** Prepare a transaction. */
154 int SYMEXPORT
alpm_trans_prepare(pmhandle_t
*handle
, alpm_list_t
**data
)
159 CHECK_HANDLE(handle
, return -1);
160 ASSERT(data
!= NULL
, RET_ERR(handle
, PM_ERR_WRONG_ARGS
, -1));
162 trans
= handle
->trans
;
164 ASSERT(trans
!= NULL
, RET_ERR(handle
, PM_ERR_TRANS_NULL
, -1));
165 ASSERT(trans
->state
== STATE_INITIALIZED
, RET_ERR(handle
, PM_ERR_TRANS_NOT_INITIALIZED
, -1));
167 /* If there's nothing to do, return without complaining */
168 if(trans
->add
== NULL
&& trans
->remove
== NULL
) {
172 alpm_list_t
*invalid
= check_arch(handle
, trans
->add
);
177 RET_ERR(handle
, PM_ERR_PKG_INVALID_ARCH
, -1);
180 if(trans
->add
== NULL
) {
181 if(_alpm_remove_prepare(handle
, data
) == -1) {
182 /* pm_errno is set by _alpm_remove_prepare() */
186 if(_alpm_sync_prepare(handle
, data
) == -1) {
187 /* pm_errno is set by _alpm_sync_prepare() */
192 trans
->state
= STATE_PREPARED
;
197 /** Commit a transaction. */
198 int SYMEXPORT
alpm_trans_commit(pmhandle_t
*handle
, alpm_list_t
**data
)
203 CHECK_HANDLE(handle
, return -1);
205 trans
= handle
->trans
;
207 ASSERT(trans
!= NULL
, RET_ERR(handle
, PM_ERR_TRANS_NULL
, -1));
208 ASSERT(trans
->state
== STATE_PREPARED
, RET_ERR(handle
, PM_ERR_TRANS_NOT_PREPARED
, -1));
210 ASSERT(!(trans
->flags
& PM_TRANS_FLAG_NOLOCK
), RET_ERR(handle
, PM_ERR_TRANS_NOT_LOCKED
, -1));
212 /* If there's nothing to do, return without complaining */
213 if(trans
->add
== NULL
&& trans
->remove
== NULL
) {
217 trans
->state
= STATE_COMMITING
;
219 if(trans
->add
== NULL
) {
220 if(_alpm_remove_packages(handle
) == -1) {
221 /* pm_errno is set by _alpm_remove_commit() */
225 if(_alpm_sync_commit(handle
, data
) == -1) {
226 /* pm_errno is set by _alpm_sync_commit() */
231 trans
->state
= STATE_COMMITED
;
236 /** Interrupt a transaction. */
237 int SYMEXPORT
alpm_trans_interrupt(pmhandle_t
*handle
)
242 CHECK_HANDLE(handle
, return -1);
244 trans
= handle
->trans
;
245 ASSERT(trans
!= NULL
, RET_ERR(handle
, PM_ERR_TRANS_NULL
, -1));
246 ASSERT(trans
->state
== STATE_COMMITING
|| trans
->state
== STATE_INTERRUPTED
,
247 RET_ERR(handle
, PM_ERR_TRANS_TYPE
, -1));
249 trans
->state
= STATE_INTERRUPTED
;
254 /** Release a transaction. */
255 int SYMEXPORT
alpm_trans_release(pmhandle_t
*handle
)
260 CHECK_HANDLE(handle
, return -1);
262 trans
= handle
->trans
;
263 ASSERT(trans
!= NULL
, RET_ERR(handle
, PM_ERR_TRANS_NULL
, -1));
264 ASSERT(trans
->state
!= STATE_IDLE
, RET_ERR(handle
, PM_ERR_TRANS_NULL
, -1));
266 int nolock_flag
= trans
->flags
& PM_TRANS_FLAG_NOLOCK
;
268 _alpm_trans_free(trans
);
269 handle
->trans
= NULL
;
273 if(remove_lock(handle
)) {
274 _alpm_log(handle
, PM_LOG_WARNING
, _("could not remove lock file %s\n"),
275 alpm_option_get_lockfile(handle
));
276 alpm_logaction(handle
, "warning: could not remove lock file %s\n",
277 alpm_option_get_lockfile(handle
));
286 void _alpm_trans_free(pmtrans_t
*trans
)
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
);
297 FREELIST(trans
->skip_remove
);
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
305 static int grep(const char *fn
, const char *needle
)
309 if((fp
= fopen(fn
, "r")) == NULL
) {
314 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
317 /* TODO: this will not work if the search string
318 * ends up being split across line reads */
319 if(strstr(line
, needle
)) {
328 int _alpm_runscriptlet(pmhandle_t
*handle
, const char *installfn
,
329 const char *script
, const char *ver
, const char *oldver
)
331 char scriptfn
[PATH_MAX
];
332 char cmdline
[PATH_MAX
];
333 char tmpdir
[PATH_MAX
];
334 char *argv
[] = { "sh", "-c", cmdline
, NULL
};
336 int clean_tmpdir
= 0;
339 if(access(installfn
, R_OK
)) {
341 _alpm_log(handle
, PM_LOG_DEBUG
, "scriptlet '%s' not found\n", installfn
);
345 /* creates a directory in $root/tmp/ for copying/extracting the scriptlet */
346 snprintf(tmpdir
, PATH_MAX
, "%stmp/", handle
->root
);
347 if(access(tmpdir
, F_OK
) != 0) {
348 _alpm_makepath_mode(tmpdir
, 01777);
350 snprintf(tmpdir
, PATH_MAX
, "%stmp/alpm_XXXXXX", handle
->root
);
351 if(mkdtemp(tmpdir
) == NULL
) {
352 _alpm_log(handle
, PM_LOG_ERROR
, _("could not create temp directory\n"));
358 /* either extract or copy the scriptlet */
359 snprintf(scriptfn
, PATH_MAX
, "%s/.INSTALL", tmpdir
);
360 if(strcmp(script
, "pre_upgrade") == 0 || strcmp(script
, "pre_install") == 0) {
361 if(_alpm_unpack_single(handle
, installfn
, tmpdir
, ".INSTALL")) {
365 if(_alpm_copyfile(installfn
, scriptfn
)) {
366 _alpm_log(handle
, PM_LOG_ERROR
, _("could not copy tempfile to %s (%s)\n"), scriptfn
, strerror(errno
));
374 /* chop off the root so we can find the tmpdir in the chroot */
375 scriptpath
= scriptfn
+ strlen(handle
->root
) - 1;
377 if(!grep(scriptfn
, script
)) {
378 /* script not found in scriptlet file */
383 snprintf(cmdline
, PATH_MAX
, ". %s; %s %s %s",
384 scriptpath
, script
, ver
, oldver
);
386 snprintf(cmdline
, PATH_MAX
, ". %s; %s %s",
387 scriptpath
, script
, ver
);
390 _alpm_log(handle
, PM_LOG_DEBUG
, "executing \"%s\"\n", cmdline
);
392 retval
= _alpm_run_chroot(handle
, "/bin/sh", argv
);
395 if(clean_tmpdir
&& _alpm_rmrf(tmpdir
)) {
396 _alpm_log(handle
, PM_LOG_WARNING
, _("could not remove tmpdir %s\n"), tmpdir
);
402 pmtransflag_t SYMEXPORT
alpm_trans_get_flags(pmhandle_t
*handle
)
405 CHECK_HANDLE(handle
, return -1);
406 ASSERT(handle
->trans
!= NULL
, RET_ERR(handle
, PM_ERR_TRANS_NULL
, -1));
408 return handle
->trans
->flags
;
411 alpm_list_t SYMEXPORT
*alpm_trans_get_add(pmhandle_t
*handle
)
414 CHECK_HANDLE(handle
, return NULL
);
415 ASSERT(handle
->trans
!= NULL
, RET_ERR(handle
, PM_ERR_TRANS_NULL
, NULL
));
417 return handle
->trans
->add
;
420 alpm_list_t SYMEXPORT
*alpm_trans_get_remove(pmhandle_t
*handle
)
423 CHECK_HANDLE(handle
, return NULL
);
424 ASSERT(handle
->trans
!= NULL
, RET_ERR(handle
, PM_ERR_TRANS_NULL
, NULL
));
426 return handle
->trans
->remove
;
428 /* vim: set ts=2 sw=2 noet: */