Scripts testsuite output consistency
[pacman-ng.git] / lib / libalpm / add.c
blobedddc31816106613a12c2d3af1c246e1aeea10bd
1 /*
2 * add.c
4 * Copyright (c) 2006-2012 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <limits.h>
25 #include <fcntl.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <stdint.h> /* int64_t */
31 /* libarchive */
32 #include <archive.h>
33 #include <archive_entry.h>
35 /* libalpm */
36 #include "add.h"
37 #include "alpm.h"
38 #include "alpm_list.h"
39 #include "handle.h"
40 #include "trans.h"
41 #include "util.h"
42 #include "log.h"
43 #include "backup.h"
44 #include "package.h"
45 #include "db.h"
46 #include "remove.h"
47 #include "handle.h"
49 /** Add a package to the transaction. */
50 int SYMEXPORT alpm_add_pkg(alpm_handle_t *handle, alpm_pkg_t *pkg)
52 const char *pkgname, *pkgver;
53 alpm_trans_t *trans;
54 alpm_pkg_t *local;
56 /* Sanity checks */
57 CHECK_HANDLE(handle, return -1);
58 ASSERT(pkg != NULL, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1));
59 ASSERT(handle == pkg->handle, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1));
60 trans = handle->trans;
61 ASSERT(trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
62 ASSERT(trans->state == STATE_INITIALIZED,
63 RET_ERR(handle, ALPM_ERR_TRANS_NOT_INITIALIZED, -1));
65 pkgname = pkg->name;
66 pkgver = pkg->version;
68 _alpm_log(handle, ALPM_LOG_DEBUG, "adding package '%s'\n", pkgname);
70 if(_alpm_pkg_find(trans->add, pkgname)) {
71 RET_ERR(handle, ALPM_ERR_TRANS_DUP_TARGET, -1);
74 local = _alpm_db_get_pkgfromcache(handle->db_local, pkgname);
75 if(local) {
76 const char *localpkgname = local->name;
77 const char *localpkgver = local->version;
78 int cmp = _alpm_pkg_compare_versions(pkg, local);
80 if(cmp == 0) {
81 if(trans->flags & ALPM_TRANS_FLAG_NEEDED) {
82 /* with the NEEDED flag, packages up to date are not reinstalled */
83 _alpm_log(handle, ALPM_LOG_WARNING, _("%s-%s is up to date -- skipping\n"),
84 localpkgname, localpkgver);
85 return 0;
86 } else if(!(trans->flags & ALPM_TRANS_FLAG_DOWNLOADONLY)) {
87 _alpm_log(handle, ALPM_LOG_WARNING, _("%s-%s is up to date -- reinstalling\n"),
88 localpkgname, localpkgver);
90 } else if(cmp < 0) {
91 /* local version is newer */
92 _alpm_log(handle, ALPM_LOG_WARNING, _("downgrading package %s (%s => %s)\n"),
93 localpkgname, localpkgver, pkgver);
97 /* add the package to the transaction */
98 pkg->reason = ALPM_PKG_REASON_EXPLICIT;
99 _alpm_log(handle, ALPM_LOG_DEBUG, "adding package %s-%s to the transaction add list\n",
100 pkgname, pkgver);
101 trans->add = alpm_list_add(trans->add, pkg);
103 return 0;
106 static int perform_extraction(alpm_handle_t *handle, struct archive *archive,
107 struct archive_entry *entry, const char *filename, const char *origname)
109 int ret;
110 const int archive_flags = ARCHIVE_EXTRACT_OWNER |
111 ARCHIVE_EXTRACT_PERM |
112 ARCHIVE_EXTRACT_TIME;
114 archive_entry_set_pathname(entry, filename);
116 ret = archive_read_extract(archive, entry, archive_flags);
117 if(ret == ARCHIVE_WARN && archive_errno(archive) != ENOSPC) {
118 /* operation succeeded but a "non-critical" error was encountered */
119 _alpm_log(handle, ALPM_LOG_WARNING, _("warning given when extracting %s (%s)\n"),
120 origname, archive_error_string(archive));
121 } else if(ret != ARCHIVE_OK) {
122 _alpm_log(handle, ALPM_LOG_ERROR, _("could not extract %s (%s)\n"),
123 origname, archive_error_string(archive));
124 alpm_logaction(handle, "error: could not extract %s (%s)\n",
125 origname, archive_error_string(archive));
126 return 1;
128 return 0;
131 static int try_rename(alpm_handle_t *handle, const char *src, const char *dest)
133 if(rename(src, dest)) {
134 _alpm_log(handle, ALPM_LOG_ERROR, _("could not rename %s to %s (%s)\n"),
135 src, dest, strerror(errno));
136 alpm_logaction(handle, "error: could not rename %s to %s (%s)\n",
137 src, dest, strerror(errno));
138 return 1;
140 return 0;
143 static int extract_single_file(alpm_handle_t *handle, struct archive *archive,
144 struct archive_entry *entry, alpm_pkg_t *newpkg, alpm_pkg_t *oldpkg)
146 const char *entryname;
147 mode_t entrymode;
148 char filename[PATH_MAX]; /* the actual file we're extracting */
149 int needbackup = 0, notouch = 0;
150 const char *hash_orig = NULL;
151 char *entryname_orig = NULL;
152 int errors = 0;
154 entryname = archive_entry_pathname(entry);
155 entrymode = archive_entry_mode(entry);
157 if(strcmp(entryname, ".INSTALL") == 0) {
158 /* the install script goes inside the db */
159 snprintf(filename, PATH_MAX, "%s%s-%s/install",
160 _alpm_db_path(handle->db_local), newpkg->name, newpkg->version);
161 archive_entry_set_perm(entry, 0644);
162 } else if(strcmp(entryname, ".CHANGELOG") == 0) {
163 /* the changelog goes inside the db */
164 snprintf(filename, PATH_MAX, "%s%s-%s/changelog",
165 _alpm_db_path(handle->db_local), newpkg->name, newpkg->version);
166 archive_entry_set_perm(entry, 0644);
167 } else if(*entryname == '.') {
168 /* for now, ignore all files starting with '.' that haven't
169 * already been handled (for future possibilities) */
170 _alpm_log(handle, ALPM_LOG_DEBUG, "skipping extraction of '%s'\n", entryname);
171 archive_read_data_skip(archive);
172 return 0;
173 } else {
174 /* build the new entryname relative to handle->root */
175 snprintf(filename, PATH_MAX, "%s%s", handle->root, entryname);
178 /* if a file is in NoExtract then we never extract it */
179 if(alpm_list_find(handle->noextract, entryname, _alpm_fnmatch)) {
180 _alpm_log(handle, ALPM_LOG_DEBUG, "%s is in NoExtract, skipping extraction\n",
181 entryname);
182 alpm_logaction(handle, "note: %s is in NoExtract, skipping extraction\n",
183 entryname);
184 archive_read_data_skip(archive);
185 return 0;
188 /* Check for file existence. This is one of the more crucial parts
189 * to get 'right'. Here are the possibilities, with the filesystem
190 * on the left and the package on the top:
191 * (F=file, N=node, S=symlink, D=dir)
192 * | F/N | S | D
193 * non-existent | 1 | 2 | 3
194 * F/N | 4 | 5 | 6
195 * S | 7 | 8 | 9
196 * D | 10 | 11 | 12
198 * 1,2,3- extract, no magic necessary. lstat (_alpm_lstat) will fail here.
199 * 4,5,6,7,8- conflict checks should have caught this. either overwrite
200 * or backup the file.
201 * 9- follow the symlink, hopefully it is a directory, check it.
202 * 10- file replacing directory- don't allow it.
203 * 11- don't extract symlink- a dir exists here. we don't want links to
204 * links, etc.
205 * 12- skip extraction, dir already exists.
208 /* do both a lstat and a stat, so we can see what symlinks point to */
209 struct stat lsbuf, sbuf;
210 if(_alpm_lstat(filename, &lsbuf) != 0 || stat(filename, &sbuf) != 0) {
211 /* cases 1,2,3: couldn't stat an existing file, skip all backup checks */
212 } else {
213 if(S_ISDIR(lsbuf.st_mode)) {
214 if(S_ISDIR(entrymode)) {
215 /* case 12: existing dir, ignore it */
216 if(lsbuf.st_mode != entrymode) {
217 /* if filesystem perms are different than pkg perms, warn user */
218 mode_t mask = 07777;
219 _alpm_log(handle, ALPM_LOG_WARNING, _("directory permissions differ on %s\n"
220 "filesystem: %o package: %o\n"), entryname, lsbuf.st_mode & mask,
221 entrymode & mask);
222 alpm_logaction(handle, "warning: directory permissions differ on %s\n"
223 "filesystem: %o package: %o\n", entryname, lsbuf.st_mode & mask,
224 entrymode & mask);
226 _alpm_log(handle, ALPM_LOG_DEBUG, "extract: skipping dir extraction of %s\n",
227 entryname);
228 archive_read_data_skip(archive);
229 return 0;
230 } else {
231 /* case 10/11: trying to overwrite dir with file/symlink, don't allow it */
232 _alpm_log(handle, ALPM_LOG_ERROR, _("extract: not overwriting dir with file %s\n"),
233 entryname);
234 archive_read_data_skip(archive);
235 return 1;
237 } else if(S_ISLNK(lsbuf.st_mode) && S_ISDIR(entrymode)) {
238 /* case 9: existing symlink, dir in package */
239 if(S_ISDIR(sbuf.st_mode)) {
240 /* the symlink on FS is to a directory, so we'll use it */
241 _alpm_log(handle, ALPM_LOG_DEBUG, "extract: skipping symlink overwrite of %s\n",
242 entryname);
243 archive_read_data_skip(archive);
244 return 0;
245 } else {
246 /* this is BAD. symlink was not to a directory */
247 _alpm_log(handle, ALPM_LOG_ERROR, _("extract: symlink %s does not point to dir\n"),
248 entryname);
249 archive_read_data_skip(archive);
250 return 1;
252 } else if(S_ISREG(lsbuf.st_mode) && S_ISDIR(entrymode)) {
253 /* case 6: trying to overwrite file with dir */
254 _alpm_log(handle, ALPM_LOG_DEBUG, "extract: overwriting file with dir %s\n",
255 entryname);
256 } else if(S_ISREG(entrymode)) {
257 /* case 4,7: */
258 /* if file is in NoUpgrade, don't touch it */
259 if(alpm_list_find(handle->noupgrade, entryname, _alpm_fnmatch)) {
260 notouch = 1;
261 } else {
262 alpm_backup_t *backup;
263 /* go to the backup array and see if our conflict is there */
264 /* check newpkg first, so that adding backup files is retroactive */
265 backup = _alpm_needbackup(entryname, newpkg);
266 if(backup) {
267 /* if we force hash_orig to be non-NULL retroactive backup works */
268 hash_orig = "";
269 needbackup = 1;
272 /* check oldpkg for a backup entry, store the hash if available */
273 if(oldpkg) {
274 backup = _alpm_needbackup(entryname, oldpkg);
275 if(backup) {
276 hash_orig = backup->hash;
277 needbackup = 1;
282 /* else if(S_ISLNK(entrymode)) */
283 /* case 5,8: don't need to do anything special */
286 /* we need access to the original entryname later after calls to
287 * archive_entry_set_pathname(), so we need to dupe it and free() later */
288 STRDUP(entryname_orig, entryname, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
290 if(needbackup) {
291 char *checkfile;
292 char *hash_local = NULL, *hash_pkg = NULL;
293 size_t len;
295 len = strlen(filename) + 10;
296 MALLOC(checkfile, len,
297 errors++; handle->pm_errno = ALPM_ERR_MEMORY; goto needbackup_cleanup);
298 snprintf(checkfile, len, "%s.paccheck", filename);
300 if(perform_extraction(handle, archive, entry, checkfile, entryname_orig)) {
301 errors++;
302 goto needbackup_cleanup;
305 hash_local = alpm_compute_md5sum(filename);
306 hash_pkg = alpm_compute_md5sum(checkfile);
308 /* update the md5 hash in newpkg's backup (it will be the new orginal) */
309 alpm_list_t *i;
310 for(i = alpm_pkg_get_backup(newpkg); i; i = i->next) {
311 alpm_backup_t *backup = i->data;
312 char *newhash;
313 if(!backup->name || strcmp(backup->name, entryname_orig) != 0) {
314 continue;
316 STRDUP(newhash, hash_pkg, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
317 FREE(backup->hash);
318 backup->hash = newhash;
321 _alpm_log(handle, ALPM_LOG_DEBUG, "checking hashes for %s\n", entryname_orig);
322 _alpm_log(handle, ALPM_LOG_DEBUG, "current: %s\n", hash_local);
323 _alpm_log(handle, ALPM_LOG_DEBUG, "new: %s\n", hash_pkg);
324 _alpm_log(handle, ALPM_LOG_DEBUG, "original: %s\n", hash_orig);
326 if(!oldpkg) {
327 if(hash_local && hash_pkg && strcmp(hash_local, hash_pkg) != 0) {
328 /* looks like we have a local file that has a different hash as the
329 * file in the package, move it to a .pacorig */
330 char *newpath;
331 size_t newlen = strlen(filename) + 9;
332 MALLOC(newpath, newlen,
333 errors++; handle->pm_errno = ALPM_ERR_MEMORY; goto needbackup_cleanup);
334 snprintf(newpath, newlen, "%s.pacorig", filename);
336 /* move the existing file to the "pacorig" */
337 if(try_rename(handle, filename, newpath)) {
338 errors++;
339 errors++;
340 } else {
341 /* rename the file we extracted to the real name */
342 if(try_rename(handle, checkfile, filename)) {
343 errors++;
344 } else {
345 _alpm_log(handle, ALPM_LOG_WARNING, _("%s saved as %s\n"), filename, newpath);
346 alpm_logaction(handle, "warning: %s saved as %s\n", filename, newpath);
349 free(newpath);
350 } else {
351 /* local file is identical to pkg one, so just remove pkg one */
352 unlink(checkfile);
354 } else if(hash_orig) {
355 /* the fun part */
357 if(hash_local && strcmp(hash_orig, hash_local) == 0) {
358 /* installed file has NOT been changed by user */
359 if(hash_pkg && strcmp(hash_orig, hash_pkg) != 0) {
360 _alpm_log(handle, ALPM_LOG_DEBUG, "action: installing new file: %s\n",
361 entryname_orig);
363 if(try_rename(handle, checkfile, filename)) {
364 errors++;
366 } else {
367 /* no sense in installing the same file twice, install
368 * ONLY if the original and package hashes differ */
369 _alpm_log(handle, ALPM_LOG_DEBUG, "action: leaving existing file in place\n");
370 unlink(checkfile);
372 } else if(hash_pkg && strcmp(hash_orig, hash_pkg) == 0) {
373 /* originally installed file and new file are the same - this
374 * implies the case above failed - i.e. the file was changed by a
375 * user */
376 _alpm_log(handle, ALPM_LOG_DEBUG, "action: leaving existing file in place\n");
377 unlink(checkfile);
378 } else if(hash_local && hash_pkg && strcmp(hash_local, hash_pkg) == 0) {
379 /* this would be magical. The above two cases failed, but the
380 * user changes just so happened to make the new file exactly the
381 * same as the one in the package... skip it */
382 _alpm_log(handle, ALPM_LOG_DEBUG, "action: leaving existing file in place\n");
383 unlink(checkfile);
384 } else {
385 char *newpath;
386 size_t newlen = strlen(filename) + 8;
387 _alpm_log(handle, ALPM_LOG_DEBUG, "action: keeping current file and installing"
388 " new one with .pacnew ending\n");
389 MALLOC(newpath, newlen,
390 errors++; handle->pm_errno = ALPM_ERR_MEMORY; goto needbackup_cleanup);
391 snprintf(newpath, newlen, "%s.pacnew", filename);
392 if(try_rename(handle, checkfile, newpath)) {
393 errors++;
394 } else {
395 _alpm_log(handle, ALPM_LOG_WARNING, _("%s installed as %s\n"),
396 filename, newpath);
397 alpm_logaction(handle, "warning: %s installed as %s\n",
398 filename, newpath);
400 free(newpath);
404 needbackup_cleanup:
405 free(checkfile);
406 free(hash_local);
407 free(hash_pkg);
408 } else {
409 /* we didn't need a backup */
410 if(notouch) {
411 /* change the path to a .pacnew extension */
412 _alpm_log(handle, ALPM_LOG_DEBUG, "%s is in NoUpgrade -- skipping\n", filename);
413 _alpm_log(handle, ALPM_LOG_WARNING, _("extracting %s as %s.pacnew\n"), filename, filename);
414 alpm_logaction(handle, "warning: extracting %s as %s.pacnew\n", filename, filename);
415 strncat(filename, ".pacnew", PATH_MAX - strlen(filename));
416 } else {
417 _alpm_log(handle, ALPM_LOG_DEBUG, "extracting %s\n", filename);
420 if(handle->trans->flags & ALPM_TRANS_FLAG_FORCE) {
421 /* if FORCE was used, unlink() each file (whether it's there
422 * or not) before extracting. This prevents the old "Text file busy"
423 * error that crops up if forcing a glibc or pacman upgrade. */
424 unlink(filename);
427 if(perform_extraction(handle, archive, entry, filename, entryname_orig)) {
428 /* error */
429 free(entryname_orig);
430 errors++;
431 return errors;
434 /* calculate an hash if this is in newpkg's backup */
435 alpm_list_t *i;
436 for(i = alpm_pkg_get_backup(newpkg); i; i = i->next) {
437 alpm_backup_t *backup = i->data;
438 char *newhash;
439 if(!backup->name || strcmp(backup->name, entryname_orig) != 0) {
440 continue;
442 _alpm_log(handle, ALPM_LOG_DEBUG, "appending backup entry for %s\n", entryname_orig);
443 newhash = alpm_compute_md5sum(filename);
444 FREE(backup->hash);
445 backup->hash = newhash;
448 free(entryname_orig);
449 return errors;
452 static int commit_single_pkg(alpm_handle_t *handle, alpm_pkg_t *newpkg,
453 size_t pkg_current, size_t pkg_count)
455 int i, ret = 0, errors = 0;
456 int is_upgrade;
457 alpm_pkg_t *oldpkg = NULL;
458 alpm_db_t *db = handle->db_local;
459 alpm_trans_t *trans = handle->trans;
460 const char *pkgfile;
462 ASSERT(trans != NULL, return -1);
464 /* see if this is an upgrade. if so, remove the old package first */
465 alpm_pkg_t *local = _alpm_db_get_pkgfromcache(db, newpkg->name);
466 if(local) {
467 is_upgrade = 1;
469 /* we'll need to save some record for backup checks later */
470 if(_alpm_pkg_dup(local, &oldpkg) == -1) {
471 ret = -1;
472 goto cleanup;
475 /* copy over the install reason */
476 newpkg->reason = alpm_pkg_get_reason(local);
478 EVENT(handle, ALPM_EVENT_UPGRADE_START, newpkg, local);
479 } else {
480 is_upgrade = 0;
481 EVENT(handle, ALPM_EVENT_ADD_START, newpkg, NULL);
484 pkgfile = newpkg->origin_data.file;
486 _alpm_log(handle, ALPM_LOG_DEBUG, "%s package %s-%s\n",
487 is_upgrade ? "upgrading" : "adding", newpkg->name, newpkg->version);
488 /* pre_install/pre_upgrade scriptlet */
489 if(alpm_pkg_has_scriptlet(newpkg) &&
490 !(trans->flags & ALPM_TRANS_FLAG_NOSCRIPTLET)) {
491 const char *scriptlet_name = is_upgrade ? "pre_upgrade" : "pre_install";
493 _alpm_runscriptlet(handle, pkgfile, scriptlet_name,
494 newpkg->version, oldpkg ? oldpkg->version : NULL, 1);
497 /* we override any pre-set reason if we have alldeps or allexplicit set */
498 if(trans->flags & ALPM_TRANS_FLAG_ALLDEPS) {
499 newpkg->reason = ALPM_PKG_REASON_DEPEND;
500 } else if(trans->flags & ALPM_TRANS_FLAG_ALLEXPLICIT) {
501 newpkg->reason = ALPM_PKG_REASON_EXPLICIT;
504 if(oldpkg) {
505 /* set up fake remove transaction */
506 if(_alpm_remove_single_package(handle, oldpkg, newpkg, 0, 0) == -1) {
507 handle->pm_errno = ALPM_ERR_TRANS_ABORT;
508 ret = -1;
509 goto cleanup;
513 /* prepare directory for database entries so permission are correct after
514 changelog/install script installation (FS#12263) */
515 if(_alpm_local_db_prepare(db, newpkg)) {
516 alpm_logaction(handle, "error: could not create database entry %s-%s\n",
517 newpkg->name, newpkg->version);
518 handle->pm_errno = ALPM_ERR_DB_WRITE;
519 ret = -1;
520 goto cleanup;
523 if(!(trans->flags & ALPM_TRANS_FLAG_DBONLY)) {
524 struct archive *archive;
525 struct archive_entry *entry;
526 struct stat buf;
527 int fd, cwdfd;
529 _alpm_log(handle, ALPM_LOG_DEBUG, "extracting files\n");
531 fd = _alpm_open_archive(db->handle, pkgfile, &buf,
532 &archive, ALPM_ERR_PKG_OPEN);
533 if(fd < 0) {
534 ret = -1;
535 goto cleanup;
538 /* save the cwd so we can restore it later */
539 OPEN(cwdfd, ".", O_RDONLY);
540 if(cwdfd < 0) {
541 _alpm_log(handle, ALPM_LOG_ERROR, _("could not get current working directory\n"));
544 /* libarchive requires this for extracting hard links */
545 if(chdir(handle->root) != 0) {
546 _alpm_log(handle, ALPM_LOG_ERROR, _("could not change directory to %s (%s)\n"),
547 handle->root, strerror(errno));
548 archive_read_finish(archive);
549 CLOSE(fd);
550 ret = -1;
551 goto cleanup;
554 /* call PROGRESS once with 0 percent, as we sort-of skip that here */
555 if(is_upgrade) {
556 PROGRESS(handle, ALPM_PROGRESS_UPGRADE_START,
557 newpkg->name, 0, pkg_count, pkg_current);
558 } else {
559 PROGRESS(handle, ALPM_PROGRESS_ADD_START,
560 newpkg->name, 0, pkg_count, pkg_current);
563 for(i = 0; archive_read_next_header(archive, &entry) == ARCHIVE_OK; i++) {
564 int percent;
566 if(newpkg->size != 0) {
567 /* Using compressed size for calculations here, as newpkg->isize is not
568 * exact when it comes to comparing to the ACTUAL uncompressed size
569 * (missing metadata sizes) */
570 int64_t pos = archive_position_compressed(archive);
571 percent = (pos * 100) / newpkg->size;
572 if(percent >= 100) {
573 percent = 100;
575 } else {
576 percent = 0;
579 if(is_upgrade) {
580 PROGRESS(handle, ALPM_PROGRESS_UPGRADE_START,
581 newpkg->name, percent, pkg_count, pkg_current);
582 } else {
583 PROGRESS(handle, ALPM_PROGRESS_ADD_START,
584 newpkg->name, percent, pkg_count, pkg_current);
587 /* extract the next file from the archive */
588 errors += extract_single_file(handle, archive, entry, newpkg, oldpkg);
590 archive_read_finish(archive);
591 CLOSE(fd);
593 /* restore the old cwd if we have it */
594 if(cwdfd >= 0) {
595 if(fchdir(cwdfd) != 0) {
596 _alpm_log(handle, ALPM_LOG_ERROR,
597 _("could not restore working directory (%s)\n"), strerror(errno));
599 CLOSE(cwdfd);
602 if(errors) {
603 ret = -1;
604 if(is_upgrade) {
605 _alpm_log(handle, ALPM_LOG_ERROR, _("problem occurred while upgrading %s\n"),
606 newpkg->name);
607 alpm_logaction(handle, "error: problem occurred while upgrading %s\n",
608 newpkg->name);
609 } else {
610 _alpm_log(handle, ALPM_LOG_ERROR, _("problem occurred while installing %s\n"),
611 newpkg->name);
612 alpm_logaction(handle, "error: problem occurred while installing %s\n",
613 newpkg->name);
618 /* make an install date (in UTC) */
619 newpkg->installdate = time(NULL);
621 _alpm_log(handle, ALPM_LOG_DEBUG, "updating database\n");
622 _alpm_log(handle, ALPM_LOG_DEBUG, "adding database entry '%s'\n", newpkg->name);
624 if(_alpm_local_db_write(db, newpkg, INFRQ_ALL)) {
625 _alpm_log(handle, ALPM_LOG_ERROR, _("could not update database entry %s-%s\n"),
626 newpkg->name, newpkg->version);
627 alpm_logaction(handle, "error: could not update database entry %s-%s\n",
628 newpkg->name, newpkg->version);
629 handle->pm_errno = ALPM_ERR_DB_WRITE;
630 ret = -1;
631 goto cleanup;
634 if(_alpm_db_add_pkgincache(db, newpkg) == -1) {
635 _alpm_log(handle, ALPM_LOG_ERROR, _("could not add entry '%s' in cache\n"),
636 newpkg->name);
639 if(is_upgrade) {
640 PROGRESS(handle, ALPM_PROGRESS_UPGRADE_START,
641 newpkg->name, 100, pkg_count, pkg_current);
642 } else {
643 PROGRESS(handle, ALPM_PROGRESS_ADD_START,
644 newpkg->name, 100, pkg_count, pkg_current);
647 /* run the post-install script if it exists */
648 if(alpm_pkg_has_scriptlet(newpkg)
649 && !(trans->flags & ALPM_TRANS_FLAG_NOSCRIPTLET)) {
650 char *scriptlet = _alpm_local_db_pkgpath(db, newpkg, "install");
651 const char *scriptlet_name = is_upgrade ? "post_upgrade" : "post_install";
653 _alpm_runscriptlet(handle, scriptlet, scriptlet_name,
654 newpkg->version, oldpkg ? oldpkg->version : NULL, 0);
655 free(scriptlet);
658 if(is_upgrade) {
659 EVENT(handle, ALPM_EVENT_UPGRADE_DONE, newpkg, oldpkg);
660 } else {
661 EVENT(handle, ALPM_EVENT_ADD_DONE, newpkg, oldpkg);
664 cleanup:
665 _alpm_pkg_free(oldpkg);
666 return ret;
669 int _alpm_upgrade_packages(alpm_handle_t *handle)
671 size_t pkg_count, pkg_current;
672 int skip_ldconfig = 0, ret = 0;
673 alpm_list_t *targ;
674 alpm_trans_t *trans = handle->trans;
676 if(trans->add == NULL) {
677 return 0;
680 pkg_count = alpm_list_count(trans->add);
681 pkg_current = 1;
683 /* loop through our package list adding/upgrading one at a time */
684 for(targ = trans->add; targ; targ = targ->next) {
685 alpm_pkg_t *newpkg = targ->data;
687 if(handle->trans->state == STATE_INTERRUPTED) {
688 return ret;
691 if(commit_single_pkg(handle, newpkg, pkg_current, pkg_count)) {
692 /* something screwed up on the commit, abort the trans */
693 trans->state = STATE_INTERRUPTED;
694 handle->pm_errno = ALPM_ERR_TRANS_ABORT;
695 /* running ldconfig at this point could possibly screw system */
696 skip_ldconfig = 1;
697 ret = -1;
700 pkg_current++;
703 if(!skip_ldconfig) {
704 /* run ldconfig if it exists */
705 _alpm_ldconfig(handle);
708 return ret;
711 /* vim: set ts=2 sw=2 noet: */