po: Update German man pages translation
[dpkg.git] / src / main / archives.c
blobd7279e1f5b1934533a4d33367c118b2b4da15835
1 /*
2 * dpkg - main program for package management
3 * archives.c - actions that process archive files, mainly unpack
5 * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2000 Wichert Akkerman <wakkerma@debian.org>
7 * Copyright © 2007-2015 Guillem Jover <guillem@debian.org>
8 * Copyright © 2011 Linaro Limited
9 * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
11 * This is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
25 #include <config.h>
26 #include <compat.h>
28 #include <sys/types.h>
29 #include <sys/time.h>
30 #include <sys/stat.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <time.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <obstack.h>
41 #define obstack_chunk_alloc m_malloc
42 #define obstack_chunk_free free
44 #include <dpkg/i18n.h>
45 #include <dpkg/dpkg.h>
46 #include <dpkg/dpkg-db.h>
47 #include <dpkg/pkg.h>
48 #include <dpkg/path.h>
49 #include <dpkg/fdio.h>
50 #include <dpkg/buffer.h>
51 #include <dpkg/subproc.h>
52 #include <dpkg/command.h>
53 #include <dpkg/file.h>
54 #include <dpkg/treewalk.h>
55 #include <dpkg/tarfn.h>
56 #include <dpkg/options.h>
57 #include <dpkg/triglib.h>
58 #include <dpkg/db-ctrl.h>
59 #include <dpkg/db-fsys.h>
61 #include "main.h"
62 #include "archives.h"
63 #include "filters.h"
65 static inline void
66 fd_writeback_init(int fd)
68 /* Ignore the return code as it should be considered equivalent to an
69 * asynchronous hint for the kernel, we are doing an fsync() later on
70 * anyway. */
71 #if defined(SYNC_FILE_RANGE_WRITE)
72 sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WRITE);
73 #elif defined(HAVE_POSIX_FADVISE)
74 posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
75 #endif
78 static struct obstack tar_pool;
79 static bool tar_pool_init = false;
81 /**
82 * Allocate memory from the tar memory pool.
84 static void *
85 tar_pool_alloc(size_t size)
87 if (!tar_pool_init) {
88 obstack_init(&tar_pool);
89 tar_pool_init = true;
92 /* cppcheck-suppress[nullPointerArithmetic]:
93 * False positive, imported module. */
94 return obstack_alloc(&tar_pool, size);
97 /**
98 * Free memory from the tar memory pool.
100 static void
101 tar_pool_free(void *ptr)
103 obstack_free(&tar_pool, ptr);
107 * Release the tar memory pool.
109 static void
110 tar_pool_release(void)
112 if (tar_pool_init) {
113 /* cppcheck-suppress[nullPointerArithmetic,pointerLessThanZero]:
114 * False positive, imported module. */
115 obstack_free(&tar_pool, NULL);
116 tar_pool_init = false;
120 struct fsys_namenode_list *
121 tar_fsys_namenode_queue_push(struct fsys_namenode_queue *queue,
122 struct fsys_namenode *namenode)
124 struct fsys_namenode_list *node;
126 node = tar_pool_alloc(sizeof(*node));
127 node->namenode = namenode;
128 node->next = NULL;
130 *queue->tail = node;
131 queue->tail = &node->next;
133 return node;
136 static void
137 tar_fsys_namenode_queue_pop(struct fsys_namenode_queue *queue,
138 struct fsys_namenode_list **tail_prev,
139 struct fsys_namenode_list *node)
141 tar_pool_free(node);
142 queue->tail = tail_prev;
143 *tail_prev = NULL;
147 * Check if a file or directory will save a package from disappearance.
149 * A package can only be saved by a file or directory which is part
150 * only of itself - it must be neither part of the new package being
151 * installed nor part of any 3rd package (this is important so that
152 * shared directories don't stop packages from disappearing).
154 bool
155 filesavespackage(struct fsys_namenode_list *file,
156 struct pkginfo *pkgtobesaved,
157 struct pkginfo *pkgbeinginstalled)
159 struct fsys_node_pkgs_iter *iter;
160 struct pkginfo *thirdpkg;
162 debug(dbg_eachfiledetail, "filesavespackage file '%s' package %s",
163 file->namenode->name, pkg_name(pkgtobesaved, pnaw_always));
165 /* If the file is a contended one and it's overridden by either
166 * the package we're considering disappearing or the package
167 * we're installing then they're not actually the same file, so
168 * we can't disappear the package - it is saved by this file. */
169 if (file->namenode->divert && file->namenode->divert->useinstead) {
170 struct pkgset *divpkgset;
172 divpkgset = file->namenode->divert->pkgset;
173 if (divpkgset == pkgtobesaved->set || divpkgset == pkgbeinginstalled->set) {
174 debug(dbg_eachfiledetail,"filesavespackage ... diverted -- save!");
175 return true;
178 /* Is the file in the package being installed? If so then it can't save. */
179 if (file->namenode->flags & FNNF_NEW_INARCHIVE) {
180 debug(dbg_eachfiledetail,"filesavespackage ... in new archive -- no save");
181 return false;
183 /* Look for a 3rd package which can take over the file (in case
184 * it's a directory which is shared by many packages. */
185 iter = fsys_node_pkgs_iter_new(file->namenode);
186 while ((thirdpkg = fsys_node_pkgs_iter_next(iter))) {
187 debug(dbg_eachfiledetail, "filesavespackage ... also in %s",
188 pkg_name(thirdpkg, pnaw_always));
190 /* Is this not the package being installed or the one being
191 * checked for disappearance? */
192 if (thirdpkg == pkgbeinginstalled || thirdpkg == pkgtobesaved)
193 continue;
195 /* A Multi-Arch: same package can share files and their presence in a
196 * third package of the same set is not a sign that we can get rid of
197 * it. */
198 if (pkgtobesaved->installed.multiarch == PKG_MULTIARCH_SAME &&
199 thirdpkg->set == pkgtobesaved->set)
200 continue;
202 debug(dbg_eachfiledetail,"filesavespackage ... is 3rd package");
204 /* If !files_list_valid then we have already disappeared this one,
205 * so we should not try to make it take over this shared directory. */
206 if (!thirdpkg->files_list_valid) {
207 debug(dbg_eachfiledetail, "process_archive ... already disappeared!");
208 continue;
211 /* We've found a package that can take this file. */
212 debug(dbg_eachfiledetail, "filesavespackage ... taken -- no save");
213 fsys_node_pkgs_iter_free(iter);
214 return false;
216 fsys_node_pkgs_iter_free(iter);
218 debug(dbg_eachfiledetail, "filesavespackage ... not taken -- save !");
219 return true;
222 static void
223 md5hash_prev_conffile(struct pkginfo *pkg, char *oldhash, const char *oldname,
224 struct fsys_namenode *namenode)
226 struct pkginfo *otherpkg;
227 struct conffile *conff;
229 debug(dbg_conffdetail, "tarobject looking for shared conffile %s",
230 namenode->name);
232 for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
233 if (otherpkg == pkg)
234 continue;
235 /* If we are reinstalling, even if the other package is only unpacked,
236 * we can always make use of the Conffiles hash value from an initial
237 * installation, if that happened at all. */
238 if (otherpkg->status <= PKG_STAT_UNPACKED &&
239 dpkg_version_compare(&otherpkg->installed.version,
240 &otherpkg->configversion) != 0)
241 continue;
242 for (conff = otherpkg->installed.conffiles; conff; conff = conff->next) {
243 if (conffile_is_disappearing(conff))
244 continue;
245 if (strcmp(conff->name, namenode->name) == 0)
246 break;
248 if (conff) {
249 strcpy(oldhash, conff->hash);
250 debug(dbg_conffdetail,
251 "tarobject found shared conffile, from pkg %s (%s); digest=%s",
252 pkg_name(otherpkg, pnaw_always),
253 pkg_status_name(otherpkg), oldhash);
254 break;
258 /* If no package was found with a valid Conffiles field, we make the
259 * risky assumption that the hash of the current .dpkg-new file is
260 * the one of the previously unpacked package. */
261 if (otherpkg == NULL) {
262 md5hash(pkg, oldhash, oldname);
263 debug(dbg_conffdetail,
264 "tarobject found shared conffile, from disk; digest=%s", oldhash);
268 void cu_pathname(int argc, void **argv) {
269 path_remove_tree((char*)(argv[0]));
273 tarfileread(struct tar_archive *tar, char *buf, int len)
275 struct tarcontext *tc = (struct tarcontext *)tar->ctx;
276 int n;
278 n = fd_read(tc->backendpipe, buf, len);
279 if (n < 0)
280 ohshite(_("error reading from dpkg-deb pipe"));
281 return n;
284 static void
285 tarobject_skip_padding(struct tarcontext *tc, struct tar_entry *te)
287 struct dpkg_error err;
288 size_t remainder;
290 remainder = te->size % TARBLKSZ;
291 if (remainder == 0)
292 return;
294 if (fd_skip(tc->backendpipe, TARBLKSZ - remainder, &err) < 0)
295 ohshit(_("cannot skip padding for file '%.255s': %s"), te->name, err.str);
298 static void
299 tarobject_skip_entry(struct tarcontext *tc, struct tar_entry *ti)
301 /* We need to advance the tar file to the next object, so read the
302 * file data and set it to oblivion. */
303 if (ti->type == TAR_FILETYPE_FILE) {
304 struct dpkg_error err;
306 if (fd_skip(tc->backendpipe, ti->size, &err) < 0)
307 ohshit(_("cannot skip file '%.255s' (replaced or excluded?) from pipe: %s"),
308 ti->name, err.str);
309 tarobject_skip_padding(tc, ti);
313 struct varbuf_state fname_state;
314 struct varbuf_state fnametmp_state;
315 struct varbuf_state fnamenew_state;
316 struct varbuf fnamevb;
317 struct varbuf fnametmpvb;
318 struct varbuf fnamenewvb;
319 struct pkg_deconf_list *deconfigure = NULL;
321 static time_t currenttime;
323 static int
324 does_replace(struct pkginfo *new_pkg, struct pkgbin *new_pkgbin,
325 struct pkginfo *old_pkg, struct pkgbin *old_pkgbin)
327 struct dependency *dep;
329 debug(dbg_depcon,"does_replace new=%s old=%s (%s)",
330 pkgbin_name(new_pkg, new_pkgbin, pnaw_always),
331 pkgbin_name(old_pkg, old_pkgbin, pnaw_always),
332 versiondescribe_c(&old_pkgbin->version, vdew_always));
333 for (dep = new_pkgbin->depends; dep; dep = dep->next) {
334 if (dep->type != dep_replaces || dep->list->ed != old_pkg->set)
335 continue;
336 debug(dbg_depcondetail,"does_replace ... found old, version %s",
337 versiondescribe_c(&dep->list->version,vdew_always));
338 if (!versionsatisfied(old_pkgbin, dep->list))
339 continue;
340 /* The test below can only trigger if dep_replaces start having
341 * arch qualifiers different from “any”. */
342 if (!archsatisfied(old_pkgbin, dep->list))
343 continue;
344 debug(dbg_depcon,"does_replace ... yes");
345 return true;
347 debug(dbg_depcon,"does_replace ... no");
348 return false;
351 static void
352 tarobject_extract(struct tarcontext *tc, struct tar_entry *te,
353 const char *path, struct file_stat *st,
354 struct fsys_namenode *namenode)
356 static struct varbuf hardlinkfn;
357 static int fd;
359 struct dpkg_error err;
360 struct fsys_namenode *linknode;
361 char *newhash;
362 int rc;
364 switch (te->type) {
365 case TAR_FILETYPE_FILE:
366 /* We create the file with mode 0 to make sure nobody can do anything with
367 * it until we apply the proper mode, which might be a statoverride. */
368 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0);
369 if (fd < 0)
370 ohshite(_("unable to create '%.255s' (while processing '%.255s')"),
371 path, te->name);
372 push_cleanup(cu_closefd, ehflag_bombout, 1, &fd);
373 debug(dbg_eachfiledetail, "tarobject file open size=%jd",
374 (intmax_t)te->size);
376 /* We try to tell the filesystem how much disk space we are going to
377 * need to let it reduce fragmentation and possibly improve performance,
378 * as we do know the size beforehand. */
379 fd_allocate_size(fd, 0, te->size);
381 newhash = nfmalloc(MD5HASHLEN + 1);
382 if (fd_fd_copy_and_md5(tc->backendpipe, fd, newhash, te->size, &err) < 0)
383 ohshit(_("cannot copy extracted data for '%.255s' to '%.255s': %s"),
384 te->name, fnamenewvb.buf, err.str);
385 namenode->newhash = newhash;
386 debug(dbg_eachfiledetail, "tarobject file digest=%s", namenode->newhash);
388 tarobject_skip_padding(tc, te);
390 fd_writeback_init(fd);
392 if (namenode->statoverride)
393 debug(dbg_eachfile, "tarobject ... stat override, uid=%d, gid=%d, mode=%04o",
394 namenode->statoverride->uid,
395 namenode->statoverride->gid,
396 namenode->statoverride->mode);
397 rc = fchown(fd, st->uid, st->gid);
398 if (forcible_nonroot_error(rc))
399 ohshite(_("error setting ownership of '%.255s'"), te->name);
400 rc = fchmod(fd, st->mode & ~S_IFMT);
401 if (forcible_nonroot_error(rc))
402 ohshite(_("error setting permissions of '%.255s'"), te->name);
404 /* Postpone the fsync, to try to avoid massive I/O degradation. */
405 if (!in_force(FORCE_UNSAFE_IO))
406 namenode->flags |= FNNF_DEFERRED_FSYNC;
408 pop_cleanup(ehflag_normaltidy); /* fd = open(path) */
409 if (close(fd))
410 ohshite(_("error closing/writing '%.255s'"), te->name);
411 break;
412 case TAR_FILETYPE_FIFO:
413 if (mkfifo(path, 0))
414 ohshite(_("error creating pipe '%.255s'"), te->name);
415 debug(dbg_eachfiledetail, "tarobject fifo");
416 break;
417 case TAR_FILETYPE_CHARDEV:
418 if (mknod(path, S_IFCHR, te->dev))
419 ohshite(_("error creating device '%.255s'"), te->name);
420 debug(dbg_eachfiledetail, "tarobject chardev");
421 break;
422 case TAR_FILETYPE_BLOCKDEV:
423 if (mknod(path, S_IFBLK, te->dev))
424 ohshite(_("error creating device '%.255s'"), te->name);
425 debug(dbg_eachfiledetail, "tarobject blockdev");
426 break;
427 case TAR_FILETYPE_HARDLINK:
428 varbuf_set_str(&hardlinkfn, dpkg_fsys_get_dir());
429 linknode = fsys_hash_find_node(te->linkname, FHFF_NONE);
430 varbuf_add_str(&hardlinkfn,
431 namenodetouse(linknode, tc->pkg, &tc->pkg->available)->name);
432 if (linknode->flags & (FNNF_DEFERRED_RENAME | FNNF_NEW_CONFF))
433 varbuf_add_str(&hardlinkfn, DPKGNEWEXT);
434 if (link(hardlinkfn.buf, path))
435 ohshite(_("error creating hard link '%.255s'"), te->name);
436 namenode->newhash = linknode->newhash;
437 debug(dbg_eachfiledetail, "tarobject hardlink digest=%s", namenode->newhash);
438 break;
439 case TAR_FILETYPE_SYMLINK:
440 /* We've already checked for an existing directory. */
441 if (symlink(te->linkname, path))
442 ohshite(_("error creating symbolic link '%.255s'"), te->name);
443 debug(dbg_eachfiledetail, "tarobject symlink creating");
444 break;
445 case TAR_FILETYPE_DIR:
446 /* We've already checked for an existing directory. */
447 if (mkdir(path, 0))
448 ohshite(_("error creating directory '%.255s'"), te->name);
449 debug(dbg_eachfiledetail, "tarobject directory creating");
450 break;
451 default:
452 internerr("unknown tar type '%d', but already checked", te->type);
456 static void
457 tarobject_hash(struct tarcontext *tc, struct tar_entry *te,
458 struct fsys_namenode *namenode)
460 if (te->type == TAR_FILETYPE_FILE) {
461 struct dpkg_error err;
462 char *newhash;
464 newhash = nfmalloc(MD5HASHLEN + 1);
465 if (fd_md5(tc->backendpipe, newhash, te->size, &err) < 0)
466 ohshit(_("cannot compute MD5 digest for file '%.255s' in tar archive: %s"),
467 te->name, err.str);
468 tarobject_skip_padding(tc, te);
470 namenode->newhash = newhash;
471 debug(dbg_eachfiledetail, "tarobject file digest=%s", namenode->newhash);
472 } else if (te->type == TAR_FILETYPE_HARDLINK) {
473 struct fsys_namenode *linknode;
475 linknode = fsys_hash_find_node(te->linkname, FHFF_NONE);
476 namenode->newhash = linknode->newhash;
477 debug(dbg_eachfiledetail, "tarobject hardlink digest=%s", namenode->newhash);
481 static void
482 tarobject_set_mtime(struct tar_entry *te, const char *path)
484 struct timeval tv[2];
486 tv[0].tv_sec = currenttime;
487 tv[0].tv_usec = 0;
488 tv[1].tv_sec = te->mtime;
489 tv[1].tv_usec = 0;
491 if (te->type == TAR_FILETYPE_SYMLINK) {
492 #ifdef HAVE_LUTIMES
493 if (lutimes(path, tv) && errno != ENOSYS)
494 ohshite(_("error setting timestamps of '%.255s'"), path);
495 #endif
496 } else {
497 if (utimes(path, tv))
498 ohshite(_("error setting timestamps of '%.255s'"), path);
502 static void
503 tarobject_set_perms(struct tar_entry *te, const char *path, struct file_stat *st)
505 int rc;
507 if (te->type == TAR_FILETYPE_FILE)
508 return; /* Already handled using the file descriptor. */
510 if (te->type == TAR_FILETYPE_SYMLINK) {
511 rc = lchown(path, st->uid, st->gid);
512 if (forcible_nonroot_error(rc))
513 ohshite(_("error setting ownership of symlink '%.255s'"), path);
514 } else {
515 rc = chown(path, st->uid, st->gid);
516 if (forcible_nonroot_error(rc))
517 ohshite(_("error setting ownership of '%.255s'"), path);
518 rc = chmod(path, st->mode & ~S_IFMT);
519 if (forcible_nonroot_error(rc))
520 ohshite(_("error setting permissions of '%.255s'"), path);
524 static void
525 tarobject_set_se_context(const char *matchpath, const char *path, mode_t mode)
527 dpkg_selabel_set_context(matchpath, path, mode);
530 static void
531 tarobject_matches(struct tarcontext *tc,
532 const char *fn_old, struct stat *stab, char *oldhash,
533 const char *fn_new, struct tar_entry *te,
534 struct fsys_namenode *namenode)
536 struct varbuf linkname = VARBUF_INIT;
537 ssize_t linksize;
538 bool linkmatch;
540 debug(dbg_eachfiledetail, "tarobject matches on-disk object?");
542 switch (te->type) {
543 case TAR_FILETYPE_DIR:
544 /* Nothing to check for a new directory. */
545 return;
546 case TAR_FILETYPE_SYMLINK:
547 /* Symlinks to existing dirs have already been dealt with, only
548 * remain real symlinks where we can compare the target. */
549 if (!S_ISLNK(stab->st_mode))
550 break;
551 linksize = file_readlink(fn_old, &linkname, stab->st_size);
552 if (linksize < 0)
553 ohshite(_("unable to read link '%.255s'"), fn_old);
554 else if (linksize > stab->st_size)
555 ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
556 fn_old, (intmax_t)stab->st_size, linksize);
557 else if (linksize < stab->st_size)
558 warning(_("symbolic link '%.250s' size has changed from %jd to %zd"),
559 fn_old, (intmax_t)stab->st_size, linksize);
560 linkmatch = strcmp(linkname.buf, te->linkname) == 0;
561 varbuf_destroy(&linkname);
562 if (linkmatch)
563 return;
564 break;
565 case TAR_FILETYPE_CHARDEV:
566 if (S_ISCHR(stab->st_mode) && stab->st_rdev == te->dev)
567 return;
568 break;
569 case TAR_FILETYPE_BLOCKDEV:
570 if (S_ISBLK(stab->st_mode) && stab->st_rdev == te->dev)
571 return;
572 break;
573 case TAR_FILETYPE_FIFO:
574 if (S_ISFIFO(stab->st_mode))
575 return;
576 break;
577 case TAR_FILETYPE_HARDLINK:
578 /* Fall through. */
579 case TAR_FILETYPE_FILE:
580 /* Only check metadata for non-conffiles. */
581 if (!(namenode->flags & FNNF_NEW_CONFF) &&
582 !(S_ISREG(stab->st_mode) && te->size == stab->st_size))
583 break;
584 if (strcmp(oldhash, namenode->newhash) == 0)
585 return;
586 break;
587 default:
588 internerr("unknown tar type '%d', but already checked", te->type);
591 forcibleerr(FORCE_OVERWRITE,
592 _("trying to overwrite shared '%.250s', which is different "
593 "from other instances of package %.250s"),
594 namenode->name, pkg_name(tc->pkg, pnaw_nonambig));
597 void setupfnamevbs(const char *filename) {
598 varbuf_rollback(&fname_state);
599 varbuf_add_str(&fnamevb, filename);
601 varbuf_rollback(&fnametmp_state);
602 varbuf_add_str(&fnametmpvb, filename);
603 varbuf_add_str(&fnametmpvb, DPKGTEMPEXT);
605 varbuf_rollback(&fnamenew_state);
606 varbuf_add_str(&fnamenewvb, filename);
607 varbuf_add_str(&fnamenewvb, DPKGNEWEXT);
609 debug(dbg_eachfiledetail, "setupvnamevbs main='%s' tmp='%s' new='%s'",
610 fnamevb.buf, fnametmpvb.buf, fnamenewvb.buf);
613 static bool
614 linktosameexistingdir(const struct tar_entry *ti, const char *fname,
615 struct varbuf *symlinkfn)
617 struct stat oldstab, newstab;
618 int statr;
620 statr= stat(fname, &oldstab);
621 if (statr) {
622 if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
623 ohshite(_("failed to stat (dereference) existing symlink '%.250s'"),
624 fname);
625 return false;
627 if (!S_ISDIR(oldstab.st_mode))
628 return false;
630 /* But is it to the same dir? */
631 if (ti->linkname[0] == '/') {
632 varbuf_set_str(symlinkfn, dpkg_fsys_get_dir());
633 } else {
634 const char *lastslash;
636 lastslash= strrchr(fname, '/');
637 if (lastslash == NULL)
638 internerr("tar entry filename '%s' does not contain '/'", fname);
639 varbuf_set_buf(symlinkfn, fname, (lastslash - fname) + 1);
641 varbuf_add_str(symlinkfn, ti->linkname);
643 statr= stat(symlinkfn->buf, &newstab);
644 if (statr) {
645 if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
646 ohshite(_("failed to stat (dereference) proposed new symlink target"
647 " '%.250s' for symlink '%.250s'"), symlinkfn->buf, fname);
648 return false;
650 if (!S_ISDIR(newstab.st_mode))
651 return false;
652 if (newstab.st_dev != oldstab.st_dev ||
653 newstab.st_ino != oldstab.st_ino)
654 return false;
655 return true;
659 tarobject(struct tar_archive *tar, struct tar_entry *ti)
661 static struct varbuf conffderefn, symlinkfn;
662 const char *usename;
663 struct fsys_namenode *namenode, *usenode;
665 struct conffile *conff;
666 struct tarcontext *tc = tar->ctx;
667 bool existingdir, keepexisting;
668 bool refcounting;
669 char oldhash[MD5HASHLEN + 1];
670 int statr;
671 struct stat stab, stabtmp;
672 struct file_stat nodestat;
673 struct fsys_namenode_list *nifd, **oldnifd;
674 struct pkgset *divpkgset;
675 struct pkginfo *otherpkg;
677 tar_entry_update_from_system(ti);
679 /* Perform some sanity checks on the tar entry. */
680 if (strchr(ti->name, '\n'))
681 ohshit(_("newline not allowed in archive object name '%.255s'"), ti->name);
683 namenode = fsys_hash_find_node(ti->name, FHFF_NONE);
685 if (namenode->flags & FNNF_RM_CONFF_ON_UPGRADE)
686 ohshit(_("conffile '%s' marked for removal on upgrade, shipped in package"),
687 ti->name);
689 /* Append to list of files.
690 * The trailing ‘/’ put on the end of names in tarfiles has already
691 * been stripped by tar_extractor(). */
692 oldnifd = tc->newfiles_queue->tail;
693 nifd = tar_fsys_namenode_queue_push(tc->newfiles_queue, namenode);
694 nifd->namenode->flags |= FNNF_NEW_INARCHIVE;
696 debug(dbg_eachfile,
697 "tarobject ti->name='%s' mode=%lo owner=%u:%u type=%d(%c)"
698 " ti->linkname='%s' namenode='%s' flags=%o instead='%s'",
699 ti->name, (long)ti->stat.mode,
700 (unsigned)ti->stat.uid, (unsigned)ti->stat.gid,
701 ti->type,
702 ti->type >= '0' && ti->type <= '6' ? "-hlcbdp"[ti->type - '0'] : '?',
703 ti->linkname,
704 nifd->namenode->name, nifd->namenode->flags,
705 nifd->namenode->divert && nifd->namenode->divert->useinstead
706 ? nifd->namenode->divert->useinstead->name : "<none>");
708 if (nifd->namenode->divert && nifd->namenode->divert->camefrom) {
709 divpkgset = nifd->namenode->divert->pkgset;
711 if (divpkgset) {
712 forcibleerr(FORCE_OVERWRITE_DIVERTED,
713 _("trying to overwrite '%.250s', which is the "
714 "diverted version of '%.250s' (package: %.100s)"),
715 nifd->namenode->name, nifd->namenode->divert->camefrom->name,
716 divpkgset->name);
717 } else {
718 forcibleerr(FORCE_OVERWRITE_DIVERTED,
719 _("trying to overwrite '%.250s', which is the "
720 "diverted version of '%.250s'"),
721 nifd->namenode->name, nifd->namenode->divert->camefrom->name);
725 if (nifd->namenode->statoverride) {
726 nodestat = *nifd->namenode->statoverride;
727 nodestat.mode |= ti->stat.mode & S_IFMT;
728 } else {
729 nodestat = ti->stat;
732 usenode = namenodetouse(nifd->namenode, tc->pkg, &tc->pkg->available);
733 usename = usenode->name;
735 trig_file_activate(usenode, tc->pkg);
737 if (nifd->namenode->flags & FNNF_NEW_CONFF) {
738 /* If it's a conffile we have to extract it next to the installed
739 * version (i.e. we do the usual link-following). */
740 if (conffderef(tc->pkg, &conffderefn, usename))
741 usename= conffderefn.buf;
742 debug(dbg_conff, "tarobject FNNF_NEW_CONFF deref='%s'", usename);
745 setupfnamevbs(usename);
747 statr= lstat(fnamevb.buf,&stab);
748 if (statr) {
749 /* The lstat failed. */
750 if (errno != ENOENT && errno != ENOTDIR)
751 ohshite(_("unable to stat '%.255s' (which was about to be installed)"),
752 ti->name);
753 /* OK, so it doesn't exist.
754 * However, it's possible that we were in the middle of some other
755 * backup/restore operation and were rudely interrupted.
756 * So, we see if we have .dpkg-tmp, and if so we restore it. */
757 if (rename(fnametmpvb.buf,fnamevb.buf)) {
758 /* Trying to remove a directory or a file on a read-only filesystem,
759 * even if non-existent, always returns EROFS. */
760 if (errno == EROFS) {
761 /* If the file does not exist the access() function will remap the
762 * EROFS into an ENOENT, otherwise restore EROFS to fail with that. */
763 if (access(fnametmpvb.buf, F_OK) == 0)
764 errno = EROFS;
767 if (errno != ENOENT && errno != ENOTDIR)
768 ohshite(_("unable to clean up mess surrounding '%.255s' before "
769 "installing another version"), ti->name);
770 debug(dbg_eachfiledetail,"tarobject nonexistent");
771 } else {
772 debug(dbg_eachfiledetail,"tarobject restored tmp to main");
773 statr= lstat(fnamevb.buf,&stab);
774 if (statr)
775 ohshite(_("unable to stat restored '%.255s' before installing"
776 " another version"), ti->name);
778 } else {
779 debug(dbg_eachfiledetail,"tarobject already exists");
782 /* Check to see if it's a directory or link to one and we don't need to
783 * do anything. This has to be done now so that we don't die due to
784 * a file overwriting conflict. */
785 existingdir = false;
786 switch (ti->type) {
787 case TAR_FILETYPE_SYMLINK:
788 /* If it's already an existing directory, do nothing. */
789 if (!statr && S_ISDIR(stab.st_mode)) {
790 debug(dbg_eachfiledetail, "tarobject symlink exists as directory");
791 existingdir = true;
792 } else if (!statr && S_ISLNK(stab.st_mode)) {
793 if (linktosameexistingdir(ti, fnamevb.buf, &symlinkfn))
794 existingdir = true;
796 break;
797 case TAR_FILETYPE_DIR:
798 /* If it's already an existing directory, do nothing. */
799 if (!stat(fnamevb.buf,&stabtmp) && S_ISDIR(stabtmp.st_mode)) {
800 debug(dbg_eachfiledetail, "tarobject directory exists");
801 existingdir = true;
803 break;
804 case TAR_FILETYPE_FILE:
805 case TAR_FILETYPE_CHARDEV:
806 case TAR_FILETYPE_BLOCKDEV:
807 case TAR_FILETYPE_FIFO:
808 case TAR_FILETYPE_HARDLINK:
809 break;
810 default:
811 ohshit(_("archive contained object '%.255s' of unknown type 0x%x"),
812 ti->name, ti->type);
815 keepexisting = false;
816 refcounting = false;
817 if (!existingdir) {
818 struct fsys_node_pkgs_iter *iter;
820 iter = fsys_node_pkgs_iter_new(nifd->namenode);
821 while ((otherpkg = fsys_node_pkgs_iter_next(iter))) {
822 if (otherpkg == tc->pkg)
823 continue;
824 debug(dbg_eachfile, "tarobject ... found in %s",
825 pkg_name(otherpkg, pnaw_always));
827 /* A pkgset can share files between its instances. Overwriting
828 * is allowed when they are not getting in sync, otherwise the
829 * file content must match the installed file. */
830 if (otherpkg->set == tc->pkg->set &&
831 otherpkg->installed.multiarch == PKG_MULTIARCH_SAME &&
832 tc->pkg->available.multiarch == PKG_MULTIARCH_SAME) {
833 if (statr == 0 && tc->pkgset_getting_in_sync)
834 refcounting = true;
835 debug(dbg_eachfiledetail, "tarobject ... shared with %s %s (syncing=%d)",
836 pkg_name(otherpkg, pnaw_always),
837 versiondescribe_c(&otherpkg->installed.version, vdew_nonambig),
838 tc->pkgset_getting_in_sync);
839 continue;
842 if (nifd->namenode->divert && nifd->namenode->divert->useinstead) {
843 /* Right, so we may be diverting this file. This makes the conflict
844 * OK iff one of us is the diverting package (we don't need to
845 * check for both being the diverting package, obviously). */
846 divpkgset = nifd->namenode->divert->pkgset;
847 debug(dbg_eachfile, "tarobject ... diverted, divpkgset=%s",
848 divpkgset ? divpkgset->name : "<none>");
849 if (otherpkg->set == divpkgset || tc->pkg->set == divpkgset)
850 continue;
853 /* If the new object is a directory and the previous object does
854 * not exist assume it's also a directory and skip further checks.
855 * XXX: Ideally with more information about the installed files we
856 * could perform more clever checks. */
857 if (statr != 0 && ti->type == TAR_FILETYPE_DIR) {
858 debug(dbg_eachfile, "tarobject ... assuming shared directory");
859 continue;
862 ensure_package_clientdata(otherpkg);
864 /* Nope? Hmm, file conflict, perhaps. Check Replaces. */
865 switch (otherpkg->clientdata->replacingfilesandsaid) {
866 case 2:
867 keepexisting = true;
868 /* Fall through. */
869 case 1:
870 continue;
873 /* Is the package with the conflicting file in the “config files only”
874 * state? If so it must be a config file and we can silently take it
875 * over. */
876 if (otherpkg->status == PKG_STAT_CONFIGFILES)
877 continue;
879 /* Perhaps we're removing a conflicting package? */
880 if (otherpkg->clientdata->istobe == PKG_ISTOBE_REMOVE)
881 continue;
883 /* Is the file an obsolete conffile in the other package
884 * and a conffile in the new package? */
885 if ((nifd->namenode->flags & FNNF_NEW_CONFF) &&
886 !statr && S_ISREG(stab.st_mode)) {
887 for (conff = otherpkg->installed.conffiles;
888 conff;
889 conff = conff->next) {
890 if (!(conff->flags & CONFFILE_OBSOLETE))
891 continue;
892 if (strcmp(conff->name, nifd->namenode->name) == 0)
893 break;
895 if (conff) {
896 debug(dbg_eachfiledetail, "tarobject other's obsolete conffile");
897 /* process_archive() will have copied its hash already. */
898 continue;
902 if (does_replace(tc->pkg, &tc->pkg->available,
903 otherpkg, &otherpkg->installed)) {
904 printf(_("Replacing files in old package %s (%s) ...\n"),
905 pkg_name(otherpkg, pnaw_nonambig),
906 versiondescribe(&otherpkg->installed.version, vdew_nonambig));
907 otherpkg->clientdata->replacingfilesandsaid = 1;
908 } else if (does_replace(otherpkg, &otherpkg->installed,
909 tc->pkg, &tc->pkg->available)) {
910 printf(_("Replaced by files in installed package %s (%s) ...\n"),
911 pkg_name(otherpkg, pnaw_nonambig),
912 versiondescribe(&otherpkg->installed.version, vdew_nonambig));
913 otherpkg->clientdata->replacingfilesandsaid = 2;
914 nifd->namenode->flags &= ~FNNF_NEW_INARCHIVE;
915 keepexisting = true;
916 } else {
917 /* At this point we are replacing something without a Replaces. */
918 if (!statr && S_ISDIR(stab.st_mode)) {
919 forcibleerr(FORCE_OVERWRITE_DIR,
920 _("trying to overwrite directory '%.250s' "
921 "in package %.250s %.250s with nondirectory"),
922 nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
923 versiondescribe(&otherpkg->installed.version,
924 vdew_nonambig));
925 } else {
926 forcibleerr(FORCE_OVERWRITE,
927 _("trying to overwrite '%.250s', "
928 "which is also in package %.250s %.250s"),
929 nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
930 versiondescribe(&otherpkg->installed.version,
931 vdew_nonambig));
935 fsys_node_pkgs_iter_free(iter);
938 if (keepexisting) {
939 if (nifd->namenode->flags & FNNF_NEW_CONFF)
940 nifd->namenode->flags |= FNNF_OBS_CONFF;
941 tar_fsys_namenode_queue_pop(tc->newfiles_queue, oldnifd, nifd);
942 tarobject_skip_entry(tc, ti);
943 return 0;
946 if (filter_should_skip(ti)) {
947 nifd->namenode->flags &= ~FNNF_NEW_INARCHIVE;
948 nifd->namenode->flags |= FNNF_FILTERED;
949 tarobject_skip_entry(tc, ti);
951 return 0;
954 if (existingdir)
955 return 0;
957 /* Compute the hash of the previous object, before we might replace it
958 * with the new version on forced overwrites. */
959 if (refcounting) {
960 debug(dbg_eachfiledetail, "tarobject computing on-disk file '%s' digest, refcounting",
961 fnamevb.buf);
962 if (nifd->namenode->flags & FNNF_NEW_CONFF) {
963 md5hash_prev_conffile(tc->pkg, oldhash, fnamenewvb.buf, nifd->namenode);
964 } else if (S_ISREG(stab.st_mode)) {
965 md5hash(tc->pkg, oldhash, fnamevb.buf);
966 } else {
967 strcpy(oldhash, EMPTYHASHFLAG);
971 if (refcounting && !in_force(FORCE_OVERWRITE)) {
972 /* If we are not forced to overwrite the path and are refcounting,
973 * just compute the hash w/o extracting the object. */
974 tarobject_hash(tc, ti, nifd->namenode);
975 } else {
976 /* Now, at this stage we want to make sure neither of .dpkg-new and
977 * .dpkg-tmp are hanging around. */
978 path_remove_tree(fnamenewvb.buf);
979 path_remove_tree(fnametmpvb.buf);
981 /* Now we start to do things that we need to be able to undo
982 * if something goes wrong. Watch out for the CLEANUP comments to
983 * keep an eye on what's installed on the disk at each point. */
984 push_cleanup(cu_installnew, ~ehflag_normaltidy, 1, nifd->namenode);
987 * CLEANUP: Now we either have the old file on the disk, or not, in
988 * its original filename.
991 /* Extract whatever it is as .dpkg-new ... */
992 tarobject_extract(tc, ti, fnamenewvb.buf, &nodestat, nifd->namenode);
995 /* For shared files, check now if the object matches. */
996 if (refcounting)
997 tarobject_matches(tc, fnamevb.buf, &stab, oldhash,
998 fnamenewvb.buf, ti, nifd->namenode);
1000 /* If we didn't extract anything, there's nothing else to do. */
1001 if (refcounting && !in_force(FORCE_OVERWRITE))
1002 return 0;
1004 tarobject_set_perms(ti, fnamenewvb.buf, &nodestat);
1005 tarobject_set_mtime(ti, fnamenewvb.buf);
1006 tarobject_set_se_context(fnamevb.buf, fnamenewvb.buf, nodestat.mode);
1009 * CLEANUP: Now we have extracted the new object in .dpkg-new (or,
1010 * if the file already exists as a directory and we were trying to
1011 * extract a directory or symlink, we returned earlier, so we don't
1012 * need to worry about that here).
1014 * The old file is still in the original filename,
1017 /* First, check to see if it's a conffile. If so we don't install
1018 * it now - we leave it in .dpkg-new for --configure to take care of. */
1019 if (nifd->namenode->flags & FNNF_NEW_CONFF) {
1020 debug(dbg_conffdetail,"tarobject conffile extracted");
1021 nifd->namenode->flags |= FNNF_ELIDE_OTHER_LISTS;
1022 return 0;
1025 /* Now we move the old file out of the way, the backup file will
1026 * be deleted later. */
1027 if (statr) {
1028 /* Don't try to back it up if it didn't exist. */
1029 debug(dbg_eachfiledetail,"tarobject new - no backup");
1030 } else {
1031 if (ti->type == TAR_FILETYPE_DIR || S_ISDIR(stab.st_mode)) {
1032 /* One of the two is a directory - can't do atomic install. */
1033 debug(dbg_eachfiledetail,"tarobject directory, nonatomic");
1034 nifd->namenode->flags |= FNNF_NO_ATOMIC_OVERWRITE;
1035 if (rename(fnamevb.buf,fnametmpvb.buf))
1036 ohshite(_("unable to move aside '%.255s' to install new version"),
1037 ti->name);
1038 } else if (S_ISLNK(stab.st_mode)) {
1039 ssize_t linksize;
1040 int rc;
1042 /* We can't make a symlink with two hardlinks, so we'll have to
1043 * copy it. (Pretend that making a copy of a symlink is the same
1044 * as linking to it.) */
1045 linksize = file_readlink(fnamevb.buf, &symlinkfn, stab.st_size);
1046 if (linksize < 0)
1047 ohshite(_("unable to read link '%.255s'"), ti->name);
1048 else if (linksize > stab.st_size)
1049 ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
1050 fnamevb.buf, (intmax_t)stab.st_size, linksize);
1051 else if (linksize < stab.st_size)
1052 warning(_("symbolic link '%.250s' size has changed from %jd to %zd"),
1053 fnamevb.buf, (intmax_t)stab.st_size, linksize);
1054 if (symlink(symlinkfn.buf,fnametmpvb.buf))
1055 ohshite(_("unable to make backup symlink for '%.255s'"), ti->name);
1056 rc = lchown(fnametmpvb.buf, stab.st_uid, stab.st_gid);
1057 if (forcible_nonroot_error(rc))
1058 ohshite(_("unable to chown backup symlink for '%.255s'"), ti->name);
1059 tarobject_set_se_context(fnamevb.buf, fnametmpvb.buf, stab.st_mode);
1060 } else {
1061 debug(dbg_eachfiledetail, "tarobject nondirectory, 'link' backup");
1062 if (link(fnamevb.buf,fnametmpvb.buf))
1063 ohshite(_("unable to make backup link of '%.255s' before installing new version"),
1064 ti->name);
1069 * CLEANUP: Now the old file is in .dpkg-tmp, and the new file is still
1070 * in .dpkg-new.
1073 if (ti->type == TAR_FILETYPE_FILE || ti->type == TAR_FILETYPE_HARDLINK ||
1074 ti->type == TAR_FILETYPE_SYMLINK) {
1075 nifd->namenode->flags |= FNNF_DEFERRED_RENAME;
1077 debug(dbg_eachfiledetail, "tarobject done and installation deferred");
1078 } else {
1079 if (rename(fnamenewvb.buf, fnamevb.buf))
1080 ohshite(_("unable to install new version of '%.255s'"), ti->name);
1083 * CLEANUP: Now the new file is in the destination file, and the
1084 * old file is in .dpkg-tmp to be cleaned up later. We now need
1085 * to take a different attitude to cleanup, because we need to
1086 * remove the new file.
1089 nifd->namenode->flags |= FNNF_PLACED_ON_DISK;
1090 nifd->namenode->flags |= FNNF_ELIDE_OTHER_LISTS;
1092 debug(dbg_eachfiledetail, "tarobject done and installed");
1095 return 0;
1098 #if defined(SYNC_FILE_RANGE_WAIT_BEFORE)
1099 static void
1100 tar_writeback_barrier(struct fsys_namenode_list *files, struct pkginfo *pkg)
1102 struct fsys_namenode_list *cfile;
1104 for (cfile = files; cfile; cfile = cfile->next) {
1105 struct fsys_namenode *usenode;
1106 int fd;
1108 if (!(cfile->namenode->flags & FNNF_DEFERRED_FSYNC))
1109 continue;
1111 usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
1113 setupfnamevbs(usenode->name);
1115 fd = open(fnamenewvb.buf, O_WRONLY);
1116 if (fd < 0)
1117 ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
1118 /* Ignore the return code as it should be considered equivalent to an
1119 * asynchronous hint for the kernel, we are doing an fsync() later on
1120 * anyway. */
1121 sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WAIT_BEFORE);
1122 if (close(fd))
1123 ohshite(_("error closing/writing '%.255s'"), fnamenewvb.buf);
1126 #else
1127 static void
1128 tar_writeback_barrier(struct fsys_namenode_list *files, struct pkginfo *pkg)
1131 #endif
1133 void
1134 tar_deferred_extract(struct fsys_namenode_list *files, struct pkginfo *pkg)
1136 struct fsys_namenode_list *cfile;
1137 struct fsys_namenode *usenode;
1139 tar_writeback_barrier(files, pkg);
1141 for (cfile = files; cfile; cfile = cfile->next) {
1142 debug(dbg_eachfile, "deferred extract of '%.255s'", cfile->namenode->name);
1144 if (!(cfile->namenode->flags & FNNF_DEFERRED_RENAME))
1145 continue;
1147 usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
1149 setupfnamevbs(usenode->name);
1151 if (cfile->namenode->flags & FNNF_DEFERRED_FSYNC) {
1152 int fd;
1154 debug(dbg_eachfiledetail, "deferred extract needs fsync");
1156 fd = open(fnamenewvb.buf, O_WRONLY);
1157 if (fd < 0)
1158 ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
1159 if (fsync(fd))
1160 ohshite(_("unable to sync file '%.255s'"), fnamenewvb.buf);
1161 if (close(fd))
1162 ohshite(_("error closing/writing '%.255s'"), fnamenewvb.buf);
1164 cfile->namenode->flags &= ~FNNF_DEFERRED_FSYNC;
1167 debug(dbg_eachfiledetail, "deferred extract needs rename");
1169 if (rename(fnamenewvb.buf, fnamevb.buf))
1170 ohshite(_("unable to install new version of '%.255s'"),
1171 cfile->namenode->name);
1173 cfile->namenode->flags &= ~FNNF_DEFERRED_RENAME;
1176 * CLEANUP: Now the new file is in the destination file, and the
1177 * old file is in .dpkg-tmp to be cleaned up later. We now need
1178 * to take a different attitude to cleanup, because we need to
1179 * remove the new file.
1182 cfile->namenode->flags |= FNNF_PLACED_ON_DISK;
1183 cfile->namenode->flags |= FNNF_ELIDE_OTHER_LISTS;
1185 debug(dbg_eachfiledetail, "deferred extract done and installed");
1189 void
1190 enqueue_deconfigure(struct pkginfo *pkg, struct pkginfo *pkg_removal,
1191 enum pkgwant reason)
1193 struct pkg_deconf_list *newdeconf;
1195 ensure_package_clientdata(pkg);
1196 pkg->clientdata->istobe = PKG_ISTOBE_DECONFIGURE;
1197 newdeconf = m_malloc(sizeof(*newdeconf));
1198 newdeconf->next = deconfigure;
1199 newdeconf->pkg = pkg;
1200 newdeconf->pkg_removal = pkg_removal;
1201 newdeconf->reason = reason;
1202 deconfigure = newdeconf;
1205 void
1206 clear_deconfigure_queue(void)
1208 struct pkg_deconf_list *deconf, *deconf_next;
1210 for (deconf = deconfigure; deconf; deconf = deconf_next) {
1211 deconf_next = deconf->next;
1212 free(deconf);
1214 deconfigure = NULL;
1218 * Try if we can deconfigure the package for installation and queue it if so.
1220 * This function gets called in the Breaks context, when trying to install
1221 * a package that might require another to be deconfigured to be able to
1222 * proceed.
1224 * First checks whether the pdep is forced.
1226 * @retval 0 Not possible (why is printed).
1227 * @retval 1 Deconfiguration queued ok (no message printed).
1228 * @retval 2 Forced (no deconfiguration needed, why is printed).
1230 static int
1231 try_deconfigure_can(struct pkginfo *pkg, struct deppossi *pdep,
1232 struct pkginfo *pkg_install, const char *why)
1234 if (force_breaks(pdep)) {
1235 warning(_("ignoring dependency problem with installation of %s:\n%s"),
1236 pkgbin_name(pkg_install, &pkg->available, pnaw_nonambig), why);
1237 return 2;
1238 } else if (f_autodeconf) {
1239 enqueue_deconfigure(pkg, NULL, PKG_WANT_INSTALL);
1240 return 1;
1241 } else {
1242 notice(_("no, cannot proceed with installation of %s (--auto-deconfigure will help):\n%s"),
1243 pkgbin_name(pkg_install, &pkg->available, pnaw_nonambig), why);
1244 return 0;
1249 * Try if we can deconfigure the package for removal and queue it if so.
1251 * This function gets called in the Conflicts+Depends context, when trying
1252 * to install a package that might require another to be fully removed to
1253 * be able to proceed.
1255 * First checks whether the pdep is forced, then if auto-configure is enabled
1256 * we make sure Essential and Protected are not allowed to be removed unless
1257 * forced.
1259 * @retval 0 Not possible (why is printed).
1260 * @retval 1 Deconfiguration queued ok (no message printed).
1261 * @retval 2 Forced (no deconfiguration needed, why is printed).
1263 static int
1264 try_remove_can(struct deppossi *pdep,
1265 struct pkginfo *pkg_removal, const char *why)
1267 struct pkginfo *pkg = pdep->up->up;
1269 if (force_depends(pdep)) {
1270 warning(_("ignoring dependency problem with removal of %s:\n%s"),
1271 pkg_name(pkg_removal, pnaw_nonambig), why);
1272 return 2;
1273 } else if (f_autodeconf) {
1274 if (pkg->installed.essential) {
1275 if (in_force(FORCE_REMOVE_ESSENTIAL)) {
1276 warning(_("considering deconfiguration of essential\n"
1277 " package %s, to enable removal of %s"),
1278 pkg_name(pkg, pnaw_nonambig), pkg_name(pkg_removal, pnaw_nonambig));
1279 } else {
1280 notice(_("no, %s is essential, will not deconfigure\n"
1281 " it in order to enable removal of %s"),
1282 pkg_name(pkg, pnaw_nonambig), pkg_name(pkg_removal, pnaw_nonambig));
1283 return 0;
1286 if (pkg->installed.is_protected) {
1287 if (in_force(FORCE_REMOVE_PROTECTED)) {
1288 warning(_("considering deconfiguration of protected\n"
1289 " package %s, to enable removal of %s"),
1290 pkg_name(pkg, pnaw_nonambig), pkg_name(pkg_removal, pnaw_nonambig));
1291 } else {
1292 notice(_("no, %s is protected, will not deconfigure\n"
1293 " it in order to enable removal of %s"),
1294 pkg_name(pkg, pnaw_nonambig), pkg_name(pkg_removal, pnaw_nonambig));
1295 return 0;
1299 enqueue_deconfigure(pkg, pkg_removal, PKG_WANT_DEINSTALL);
1300 return 1;
1301 } else {
1302 notice(_("no, cannot proceed with removal of %s (--auto-deconfigure will help):\n%s"),
1303 pkg_name(pkg_removal, pnaw_nonambig), why);
1304 return 0;
1308 void check_breaks(struct dependency *dep, struct pkginfo *pkg,
1309 const char *pfilename) {
1310 struct pkginfo *fixbydeconf;
1311 struct varbuf why = VARBUF_INIT;
1312 int ok;
1314 fixbydeconf = NULL;
1315 if (depisok(dep, &why, &fixbydeconf, NULL, false)) {
1316 varbuf_destroy(&why);
1317 return;
1321 if (fixbydeconf && f_autodeconf) {
1322 ensure_package_clientdata(fixbydeconf);
1324 if (fixbydeconf->clientdata->istobe != PKG_ISTOBE_NORMAL)
1325 internerr("package %s being fixed by deconf is not to be normal, "
1326 "is to be %d",
1327 pkg_name(pkg, pnaw_always), fixbydeconf->clientdata->istobe);
1329 notice(_("considering deconfiguration of %s, which would be broken by installation of %s ..."),
1330 pkg_name(fixbydeconf, pnaw_nonambig),
1331 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1333 ok = try_deconfigure_can(fixbydeconf, dep->list, pkg, varbuf_str(&why));
1334 if (ok == 1) {
1335 notice(_("yes, will deconfigure %s (broken by %s)"),
1336 pkg_name(fixbydeconf, pnaw_nonambig),
1337 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1339 } else {
1340 notice(_("regarding %s containing %s:\n%s"), pfilename,
1341 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
1342 varbuf_str(&why));
1343 ok= 0;
1345 varbuf_destroy(&why);
1346 if (ok > 0) return;
1348 if (force_breaks(dep->list)) {
1349 warning(_("ignoring breakage, may proceed anyway!"));
1350 return;
1353 if (fixbydeconf && !f_autodeconf) {
1354 ohshit(_("installing %.250s would break %.250s, and\n"
1355 " deconfiguration is not permitted (--auto-deconfigure might help)"),
1356 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
1357 pkg_name(fixbydeconf, pnaw_nonambig));
1358 } else {
1359 ohshit(_("installing %.250s would break existing software"),
1360 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1364 void check_conflict(struct dependency *dep, struct pkginfo *pkg,
1365 const char *pfilename) {
1366 struct pkginfo *fixbyrm;
1367 struct deppossi *pdep, flagdeppossi = { 0 };
1368 struct varbuf conflictwhy = VARBUF_INIT, removalwhy = VARBUF_INIT;
1369 struct dependency *providecheck;
1371 fixbyrm = NULL;
1372 if (depisok(dep, &conflictwhy, &fixbyrm, NULL, false)) {
1373 varbuf_destroy(&conflictwhy);
1374 varbuf_destroy(&removalwhy);
1375 return;
1377 if (fixbyrm) {
1378 ensure_package_clientdata(fixbyrm);
1379 if (fixbyrm->clientdata->istobe == PKG_ISTOBE_INSTALLNEW) {
1380 fixbyrm= dep->up;
1381 ensure_package_clientdata(fixbyrm);
1383 if (((pkg->available.essential || pkg->available.is_protected) &&
1384 (fixbyrm->installed.essential || fixbyrm->installed.is_protected)) ||
1385 (((fixbyrm->want != PKG_WANT_INSTALL &&
1386 fixbyrm->want != PKG_WANT_HOLD) ||
1387 does_replace(pkg, &pkg->available, fixbyrm, &fixbyrm->installed)) &&
1388 ((!fixbyrm->installed.essential || in_force(FORCE_REMOVE_ESSENTIAL)) ||
1389 (!fixbyrm->installed.is_protected || in_force(FORCE_REMOVE_PROTECTED))))) {
1390 if (fixbyrm->clientdata->istobe != PKG_ISTOBE_NORMAL &&
1391 fixbyrm->clientdata->istobe != PKG_ISTOBE_DECONFIGURE)
1392 internerr("package %s to be fixed by removal is not to be normal "
1393 "nor deconfigure, is to be %d",
1394 pkg_name(pkg, pnaw_always), fixbyrm->clientdata->istobe);
1395 fixbyrm->clientdata->istobe = PKG_ISTOBE_REMOVE;
1396 notice(_("considering removing %s in favour of %s ..."),
1397 pkg_name(fixbyrm, pnaw_nonambig),
1398 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1399 if (!(fixbyrm->status == PKG_STAT_INSTALLED ||
1400 fixbyrm->status == PKG_STAT_TRIGGERSPENDING ||
1401 fixbyrm->status == PKG_STAT_TRIGGERSAWAITED)) {
1402 notice(_("%s is not properly installed; ignoring any dependencies on it"),
1403 pkg_name(fixbyrm, pnaw_nonambig));
1404 pdep = NULL;
1405 } else {
1406 for (pdep = fixbyrm->set->depended.installed;
1407 pdep;
1408 pdep = pdep->rev_next) {
1409 if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
1410 continue;
1411 if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
1412 continue;
1413 if (!try_remove_can(pdep, fixbyrm, varbuf_str(&removalwhy)))
1414 break;
1416 if (!pdep) {
1417 /* If we haven't found a reason not to yet, let's look some more. */
1418 for (providecheck= fixbyrm->installed.depends;
1419 providecheck;
1420 providecheck= providecheck->next) {
1421 if (providecheck->type != dep_provides) continue;
1422 for (pdep = providecheck->list->ed->depended.installed;
1423 pdep;
1424 pdep = pdep->rev_next) {
1425 if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
1426 continue;
1427 if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
1428 continue;
1429 notice(_("may have trouble removing %s, as it provides %s ..."),
1430 pkg_name(fixbyrm, pnaw_nonambig),
1431 providecheck->list->ed->name);
1432 if (!try_remove_can(pdep, fixbyrm, varbuf_str(&removalwhy)))
1433 goto break_from_both_loops_at_once;
1436 break_from_both_loops_at_once:;
1439 if (!pdep && skip_due_to_hold(fixbyrm)) {
1440 pdep= &flagdeppossi;
1442 if (!pdep && (fixbyrm->eflag & PKG_EFLAG_REINSTREQ)) {
1443 if (in_force(FORCE_REMOVE_REINSTREQ)) {
1444 notice(_("package %s requires reinstallation, but will "
1445 "remove anyway as you requested"),
1446 pkg_name(fixbyrm, pnaw_nonambig));
1447 } else {
1448 notice(_("package %s requires reinstallation, will not remove"),
1449 pkg_name(fixbyrm, pnaw_nonambig));
1450 pdep= &flagdeppossi;
1453 if (!pdep) {
1454 /* This conflict is OK - we'll remove the conflictor. */
1455 enqueue_conflictor(fixbyrm);
1456 varbuf_destroy(&conflictwhy); varbuf_destroy(&removalwhy);
1457 notice(_("yes, will remove %s in favour of %s"),
1458 pkg_name(fixbyrm, pnaw_nonambig),
1459 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1460 return;
1462 /* Put it back. */
1463 fixbyrm->clientdata->istobe = PKG_ISTOBE_NORMAL;
1466 notice(_("regarding %s containing %s:\n%s"), pfilename,
1467 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
1468 varbuf_str(&conflictwhy));
1469 if (!force_conflicts(dep->list))
1470 ohshit(_("conflicting packages - not installing %.250s"),
1471 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1472 warning(_("ignoring conflict, may proceed anyway!"));
1473 varbuf_destroy(&conflictwhy);
1475 return;
1478 void cu_cidir(int argc, void **argv) {
1479 char *cidir= (char*)argv[0];
1480 char *cidirrest= (char*)argv[1];
1481 cidirrest[-1] = '\0';
1482 path_remove_tree(cidir);
1483 free(cidir);
1486 void cu_fileslist(int argc, void **argv) {
1487 tar_pool_release();
1491 archivefiles(const char *const *argv)
1493 const char *const *volatile argp;
1494 char **volatile arglist = NULL;
1495 int i;
1496 jmp_buf ejbuf;
1497 enum modstatdb_rw msdbflags;
1499 trigproc_install_hooks();
1501 if (f_noact)
1502 msdbflags = msdbrw_readonly;
1503 else if (cipaction->arg_int == act_avail)
1504 msdbflags = msdbrw_readonly | msdbrw_available_write;
1505 else if (in_force(FORCE_NON_ROOT))
1506 msdbflags = msdbrw_write;
1507 else
1508 msdbflags = msdbrw_needsuperuser;
1510 modstatdb_open(msdbflags);
1512 checkpath();
1513 pkg_infodb_upgrade();
1515 log_message("startup archives %s", cipaction->olong);
1517 if (f_recursive) {
1518 const char *const *ap;
1519 int nfiles = 0;
1521 if (!*argv)
1522 badusage(_("--%s --recursive needs at least one path argument"),cipaction->olong);
1524 for (ap = argv; *ap; ap++) {
1525 struct treeroot *tree;
1526 struct treenode *node;
1528 tree = treewalk_open((const char *)*ap, TREEWALK_FOLLOW_LINKS, NULL);
1530 while ((node = treewalk_next(tree))) {
1531 const char *nodename;
1533 if (!S_ISREG(treenode_get_mode(node)))
1534 continue;
1536 /* Check if it looks like a .deb file. */
1537 nodename = treenode_get_pathname(node);
1538 if (strcmp(nodename + strlen(nodename) - 4, DEBEXT) != 0)
1539 continue;
1541 arglist = m_realloc(arglist, sizeof(char *) * (nfiles + 2));
1542 arglist[nfiles++] = m_strdup(nodename);
1545 treewalk_close(tree);
1548 if (!nfiles)
1549 ohshit(_("searched, but found no packages (files matching *.deb)"));
1551 arglist[nfiles] = NULL;
1552 argp = (const char **volatile)arglist;
1553 } else {
1554 if (!*argv) badusage(_("--%s needs at least one package archive file argument"),
1555 cipaction->olong);
1556 argp= argv;
1559 /* Perform some sanity checks on the passed archives. */
1560 for (i = 0; argp[i]; i++) {
1561 struct stat st;
1563 /* We need the filename to exist. */
1564 if (stat(argp[i], &st) < 0)
1565 ohshite(_("cannot access archive '%s'"), argp[i]);
1567 /* We cannot work with anything that is not a regular file. */
1568 if (!S_ISREG(st.st_mode))
1569 ohshit(_("archive '%s' is not a regular file"), argp[i]);
1572 currenttime = time(NULL);
1574 /* Initialize fname variables contents. */
1576 varbuf_reset(&fnamevb);
1577 varbuf_reset(&fnametmpvb);
1578 varbuf_reset(&fnamenewvb);
1580 varbuf_add_str(&fnamevb, dpkg_fsys_get_dir());
1581 varbuf_add_str(&fnametmpvb, dpkg_fsys_get_dir());
1582 varbuf_add_str(&fnamenewvb, dpkg_fsys_get_dir());
1584 varbuf_snapshot(&fnamevb, &fname_state);
1585 varbuf_snapshot(&fnametmpvb, &fnametmp_state);
1586 varbuf_snapshot(&fnamenewvb, &fnamenew_state);
1588 ensure_diversions();
1589 ensure_statoverrides(STATDB_PARSE_NORMAL);
1591 for (i = 0; argp[i]; i++) {
1592 if (setjmp(ejbuf)) {
1593 pop_error_context(ehflag_bombout);
1594 if (abort_processing)
1595 break;
1596 continue;
1598 push_error_context_jump(&ejbuf, print_error_perarchive, argp[i]);
1600 dpkg_selabel_load();
1602 process_archive(argp[i]);
1603 onerr_abort++;
1604 m_output(stdout, _("<standard output>"));
1605 m_output(stderr, _("<standard error>"));
1606 onerr_abort--;
1608 pop_error_context(ehflag_normaltidy);
1611 dpkg_selabel_close();
1613 if (arglist) {
1614 for (i = 0; arglist[i]; i++)
1615 free(arglist[i]);
1616 free(arglist);
1619 switch (cipaction->arg_int) {
1620 case act_install:
1621 case act_configure:
1622 case act_triggers:
1623 case act_remove:
1624 case act_purge:
1625 process_queue();
1626 case act_unpack:
1627 case act_avail:
1628 break;
1629 default:
1630 internerr("unknown action '%d'", cipaction->arg_int);
1633 trigproc_run_deferred();
1634 modstatdb_shutdown();
1636 return 0;
1640 * Decide whether we want to install a new version of the package.
1642 * @param pkg The package with the version we might want to install
1644 * @retval true If the package should be skipped.
1645 * @retval false If the package should be installed.
1647 bool
1648 wanttoinstall(struct pkginfo *pkg)
1650 int rc;
1652 if (pkg->want != PKG_WANT_INSTALL && pkg->want != PKG_WANT_HOLD) {
1653 if (f_alsoselect) {
1654 printf(_("Selecting previously unselected package %s.\n"),
1655 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1656 return true;
1657 } else {
1658 printf(_("Skipping unselected package %s.\n"),
1659 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1660 return false;
1664 if (pkg->eflag & PKG_EFLAG_REINSTREQ)
1665 return true;
1666 if (pkg->status < PKG_STAT_UNPACKED)
1667 return true;
1669 rc = dpkg_version_compare(&pkg->available.version, &pkg->installed.version);
1670 if (rc > 0) {
1671 return true;
1672 } else if (rc == 0) {
1673 /* Same version fully installed. */
1674 if (f_skipsame && pkg->available.arch == pkg->installed.arch) {
1675 notice(_("version %.250s of %.250s already installed, skipping"),
1676 versiondescribe(&pkg->installed.version, vdew_nonambig),
1677 pkg_name(pkg, pnaw_nonambig));
1678 return false;
1679 } else {
1680 return true;
1682 } else if (in_force(FORCE_DOWNGRADE)) {
1683 warning(_("downgrading %.250s from %.250s to %.250s"),
1684 pkg_name(pkg, pnaw_nonambig),
1685 versiondescribe(&pkg->installed.version, vdew_nonambig),
1686 versiondescribe(&pkg->available.version, vdew_nonambig));
1687 return true;
1688 } else {
1689 notice(_("will not downgrade %.250s from %.250s to %.250s, skipping"),
1690 pkg_name(pkg, pnaw_nonambig),
1691 versiondescribe(&pkg->installed.version, vdew_nonambig),
1692 versiondescribe(&pkg->available.version, vdew_nonambig));
1693 return false;