libdpkg: Deindent an else clause
[dpkg.git] / src / main / archives.c
blobb6ca2880de6d04c73d9dff01f2fcb2ae699f7f19
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 (conff->obsolete || conff->remove_on_upgrade)
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 varbuf_end_str(&hardlinkfn);
435 if (link(hardlinkfn.buf, path))
436 ohshite(_("error creating hard link '%.255s'"), te->name);
437 namenode->newhash = linknode->newhash;
438 debug(dbg_eachfiledetail, "tarobject hardlink digest=%s", namenode->newhash);
439 break;
440 case TAR_FILETYPE_SYMLINK:
441 /* We've already checked for an existing directory. */
442 if (symlink(te->linkname, path))
443 ohshite(_("error creating symbolic link '%.255s'"), te->name);
444 debug(dbg_eachfiledetail, "tarobject symlink creating");
445 break;
446 case TAR_FILETYPE_DIR:
447 /* We've already checked for an existing directory. */
448 if (mkdir(path, 0))
449 ohshite(_("error creating directory '%.255s'"), te->name);
450 debug(dbg_eachfiledetail, "tarobject directory creating");
451 break;
452 default:
453 internerr("unknown tar type '%d', but already checked", te->type);
457 static void
458 tarobject_hash(struct tarcontext *tc, struct tar_entry *te,
459 struct fsys_namenode *namenode)
461 if (te->type == TAR_FILETYPE_FILE) {
462 struct dpkg_error err;
463 char *newhash;
465 newhash = nfmalloc(MD5HASHLEN + 1);
466 if (fd_md5(tc->backendpipe, newhash, te->size, &err) < 0)
467 ohshit(_("cannot compute MD5 digest for file '%.255s' in tar archive: %s"),
468 te->name, err.str);
469 tarobject_skip_padding(tc, te);
471 namenode->newhash = newhash;
472 debug(dbg_eachfiledetail, "tarobject file digest=%s", namenode->newhash);
473 } else if (te->type == TAR_FILETYPE_HARDLINK) {
474 struct fsys_namenode *linknode;
476 linknode = fsys_hash_find_node(te->linkname, FHFF_NONE);
477 namenode->newhash = linknode->newhash;
478 debug(dbg_eachfiledetail, "tarobject hardlink digest=%s", namenode->newhash);
482 static void
483 tarobject_set_mtime(struct tar_entry *te, const char *path)
485 struct timeval tv[2];
487 tv[0].tv_sec = currenttime;
488 tv[0].tv_usec = 0;
489 tv[1].tv_sec = te->mtime;
490 tv[1].tv_usec = 0;
492 if (te->type == TAR_FILETYPE_SYMLINK) {
493 #ifdef HAVE_LUTIMES
494 if (lutimes(path, tv) && errno != ENOSYS)
495 ohshite(_("error setting timestamps of '%.255s'"), path);
496 #endif
497 } else {
498 if (utimes(path, tv))
499 ohshite(_("error setting timestamps of '%.255s'"), path);
503 static void
504 tarobject_set_perms(struct tar_entry *te, const char *path, struct file_stat *st)
506 int rc;
508 if (te->type == TAR_FILETYPE_FILE)
509 return; /* Already handled using the file descriptor. */
511 if (te->type == TAR_FILETYPE_SYMLINK) {
512 rc = lchown(path, st->uid, st->gid);
513 if (forcible_nonroot_error(rc))
514 ohshite(_("error setting ownership of symlink '%.255s'"), path);
515 } else {
516 rc = chown(path, st->uid, st->gid);
517 if (forcible_nonroot_error(rc))
518 ohshite(_("error setting ownership of '%.255s'"), path);
519 rc = chmod(path, st->mode & ~S_IFMT);
520 if (forcible_nonroot_error(rc))
521 ohshite(_("error setting permissions of '%.255s'"), path);
525 static void
526 tarobject_set_se_context(const char *matchpath, const char *path, mode_t mode)
528 dpkg_selabel_set_context(matchpath, path, mode);
531 static void
532 tarobject_matches(struct tarcontext *tc,
533 const char *fn_old, struct stat *stab, char *oldhash,
534 const char *fn_new, struct tar_entry *te,
535 struct fsys_namenode *namenode)
537 struct varbuf linkname = VARBUF_INIT;
538 ssize_t linksize;
539 bool linkmatch;
541 debug(dbg_eachfiledetail, "tarobject matches on-disk object?");
543 switch (te->type) {
544 case TAR_FILETYPE_DIR:
545 /* Nothing to check for a new directory. */
546 return;
547 case TAR_FILETYPE_SYMLINK:
548 /* Symlinks to existing dirs have already been dealt with, only
549 * remain real symlinks where we can compare the target. */
550 if (!S_ISLNK(stab->st_mode))
551 break;
552 linksize = file_readlink(fn_old, &linkname, stab->st_size);
553 if (linksize < 0)
554 ohshite(_("unable to read link '%.255s'"), fn_old);
555 else if (linksize > stab->st_size)
556 ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
557 fn_old, (intmax_t)stab->st_size, linksize);
558 else if (linksize < stab->st_size)
559 warning(_("symbolic link '%.250s' size has changed from %jd to %zd"),
560 fn_old, (intmax_t)stab->st_size, linksize);
561 linkmatch = strcmp(linkname.buf, te->linkname) == 0;
562 varbuf_destroy(&linkname);
563 if (linkmatch)
564 return;
565 break;
566 case TAR_FILETYPE_CHARDEV:
567 if (S_ISCHR(stab->st_mode) && stab->st_rdev == te->dev)
568 return;
569 break;
570 case TAR_FILETYPE_BLOCKDEV:
571 if (S_ISBLK(stab->st_mode) && stab->st_rdev == te->dev)
572 return;
573 break;
574 case TAR_FILETYPE_FIFO:
575 if (S_ISFIFO(stab->st_mode))
576 return;
577 break;
578 case TAR_FILETYPE_HARDLINK:
579 /* Fall through. */
580 case TAR_FILETYPE_FILE:
581 /* Only check metadata for non-conffiles. */
582 if (!(namenode->flags & FNNF_NEW_CONFF) &&
583 !(S_ISREG(stab->st_mode) && te->size == stab->st_size))
584 break;
585 if (strcmp(oldhash, namenode->newhash) == 0)
586 return;
587 break;
588 default:
589 internerr("unknown tar type '%d', but already checked", te->type);
592 forcibleerr(FORCE_OVERWRITE,
593 _("trying to overwrite shared '%.250s', which is different "
594 "from other instances of package %.250s"),
595 namenode->name, pkg_name(tc->pkg, pnaw_nonambig));
598 void setupfnamevbs(const char *filename) {
599 varbuf_rollback(&fname_state);
600 varbuf_add_str(&fnamevb, filename);
601 varbuf_end_str(&fnamevb);
603 varbuf_rollback(&fnametmp_state);
604 varbuf_add_str(&fnametmpvb, filename);
605 varbuf_add_str(&fnametmpvb, DPKGTEMPEXT);
606 varbuf_end_str(&fnametmpvb);
608 varbuf_rollback(&fnamenew_state);
609 varbuf_add_str(&fnamenewvb, filename);
610 varbuf_add_str(&fnamenewvb, DPKGNEWEXT);
611 varbuf_end_str(&fnamenewvb);
613 debug(dbg_eachfiledetail, "setupvnamevbs main='%s' tmp='%s' new='%s'",
614 fnamevb.buf, fnametmpvb.buf, fnamenewvb.buf);
617 static bool
618 linktosameexistingdir(const struct tar_entry *ti, const char *fname,
619 struct varbuf *symlinkfn)
621 struct stat oldstab, newstab;
622 int statr;
624 statr= stat(fname, &oldstab);
625 if (statr) {
626 if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
627 ohshite(_("failed to stat (dereference) existing symlink '%.250s'"),
628 fname);
629 return false;
631 if (!S_ISDIR(oldstab.st_mode))
632 return false;
634 /* But is it to the same dir? */
635 if (ti->linkname[0] == '/') {
636 varbuf_set_str(symlinkfn, dpkg_fsys_get_dir());
637 } else {
638 const char *lastslash;
640 lastslash= strrchr(fname, '/');
641 if (lastslash == NULL)
642 internerr("tar entry filename '%s' does not contain '/'", fname);
643 varbuf_set_buf(symlinkfn, fname, (lastslash - fname) + 1);
645 varbuf_add_str(symlinkfn, ti->linkname);
646 varbuf_end_str(symlinkfn);
648 statr= stat(symlinkfn->buf, &newstab);
649 if (statr) {
650 if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
651 ohshite(_("failed to stat (dereference) proposed new symlink target"
652 " '%.250s' for symlink '%.250s'"), symlinkfn->buf, fname);
653 return false;
655 if (!S_ISDIR(newstab.st_mode))
656 return false;
657 if (newstab.st_dev != oldstab.st_dev ||
658 newstab.st_ino != oldstab.st_ino)
659 return false;
660 return true;
664 tarobject(struct tar_archive *tar, struct tar_entry *ti)
666 static struct varbuf conffderefn, symlinkfn;
667 const char *usename;
668 struct fsys_namenode *namenode, *usenode;
670 struct conffile *conff;
671 struct tarcontext *tc = tar->ctx;
672 bool existingdir, keepexisting;
673 bool refcounting;
674 char oldhash[MD5HASHLEN + 1];
675 int statr;
676 struct stat stab, stabtmp;
677 struct file_stat nodestat;
678 struct fsys_namenode_list *nifd, **oldnifd;
679 struct pkgset *divpkgset;
680 struct pkginfo *otherpkg;
682 tar_entry_update_from_system(ti);
684 /* Perform some sanity checks on the tar entry. */
685 if (strchr(ti->name, '\n'))
686 ohshit(_("newline not allowed in archive object name '%.255s'"), ti->name);
688 namenode = fsys_hash_find_node(ti->name, FHFF_NONE);
690 if (namenode->flags & FNNF_RM_CONFF_ON_UPGRADE)
691 ohshit(_("conffile '%s' marked for removal on upgrade, shipped in package"),
692 ti->name);
694 /* Append to list of files.
695 * The trailing ‘/’ put on the end of names in tarfiles has already
696 * been stripped by tar_extractor(). */
697 oldnifd = tc->newfiles_queue->tail;
698 nifd = tar_fsys_namenode_queue_push(tc->newfiles_queue, namenode);
699 nifd->namenode->flags |= FNNF_NEW_INARCHIVE;
701 debug(dbg_eachfile,
702 "tarobject ti->name='%s' mode=%lo owner=%u:%u type=%d(%c)"
703 " ti->linkname='%s' namenode='%s' flags=%o instead='%s'",
704 ti->name, (long)ti->stat.mode,
705 (unsigned)ti->stat.uid, (unsigned)ti->stat.gid,
706 ti->type,
707 ti->type >= '0' && ti->type <= '6' ? "-hlcbdp"[ti->type - '0'] : '?',
708 ti->linkname,
709 nifd->namenode->name, nifd->namenode->flags,
710 nifd->namenode->divert && nifd->namenode->divert->useinstead
711 ? nifd->namenode->divert->useinstead->name : "<none>");
713 if (nifd->namenode->divert && nifd->namenode->divert->camefrom) {
714 divpkgset = nifd->namenode->divert->pkgset;
716 if (divpkgset) {
717 forcibleerr(FORCE_OVERWRITE_DIVERTED,
718 _("trying to overwrite '%.250s', which is the "
719 "diverted version of '%.250s' (package: %.100s)"),
720 nifd->namenode->name, nifd->namenode->divert->camefrom->name,
721 divpkgset->name);
722 } else {
723 forcibleerr(FORCE_OVERWRITE_DIVERTED,
724 _("trying to overwrite '%.250s', which is the "
725 "diverted version of '%.250s'"),
726 nifd->namenode->name, nifd->namenode->divert->camefrom->name);
730 if (nifd->namenode->statoverride) {
731 nodestat = *nifd->namenode->statoverride;
732 nodestat.mode |= ti->stat.mode & S_IFMT;
733 } else {
734 nodestat = ti->stat;
737 usenode = namenodetouse(nifd->namenode, tc->pkg, &tc->pkg->available);
738 usename = usenode->name;
740 trig_file_activate(usenode, tc->pkg);
742 if (nifd->namenode->flags & FNNF_NEW_CONFF) {
743 /* If it's a conffile we have to extract it next to the installed
744 * version (i.e. we do the usual link-following). */
745 if (conffderef(tc->pkg, &conffderefn, usename))
746 usename= conffderefn.buf;
747 debug(dbg_conff, "tarobject FNNF_NEW_CONFF deref='%s'", usename);
750 setupfnamevbs(usename);
752 statr= lstat(fnamevb.buf,&stab);
753 if (statr) {
754 /* The lstat failed. */
755 if (errno != ENOENT && errno != ENOTDIR)
756 ohshite(_("unable to stat '%.255s' (which was about to be installed)"),
757 ti->name);
758 /* OK, so it doesn't exist.
759 * However, it's possible that we were in the middle of some other
760 * backup/restore operation and were rudely interrupted.
761 * So, we see if we have .dpkg-tmp, and if so we restore it. */
762 if (rename(fnametmpvb.buf,fnamevb.buf)) {
763 /* Trying to remove a directory or a file on a read-only filesystem,
764 * even if non-existent, always returns EROFS. */
765 if (errno == EROFS) {
766 /* If the file does not exist the access() function will remap the
767 * EROFS into an ENOENT, otherwise restore EROFS to fail with that. */
768 if (access(fnametmpvb.buf, F_OK) == 0)
769 errno = EROFS;
772 if (errno != ENOENT && errno != ENOTDIR)
773 ohshite(_("unable to clean up mess surrounding '%.255s' before "
774 "installing another version"), ti->name);
775 debug(dbg_eachfiledetail,"tarobject nonexistent");
776 } else {
777 debug(dbg_eachfiledetail,"tarobject restored tmp to main");
778 statr= lstat(fnamevb.buf,&stab);
779 if (statr)
780 ohshite(_("unable to stat restored '%.255s' before installing"
781 " another version"), ti->name);
783 } else {
784 debug(dbg_eachfiledetail,"tarobject already exists");
787 /* Check to see if it's a directory or link to one and we don't need to
788 * do anything. This has to be done now so that we don't die due to
789 * a file overwriting conflict. */
790 existingdir = false;
791 switch (ti->type) {
792 case TAR_FILETYPE_SYMLINK:
793 /* If it's already an existing directory, do nothing. */
794 if (!statr && S_ISDIR(stab.st_mode)) {
795 debug(dbg_eachfiledetail, "tarobject symlink exists as directory");
796 existingdir = true;
797 } else if (!statr && S_ISLNK(stab.st_mode)) {
798 if (linktosameexistingdir(ti, fnamevb.buf, &symlinkfn))
799 existingdir = true;
801 break;
802 case TAR_FILETYPE_DIR:
803 /* If it's already an existing directory, do nothing. */
804 if (!stat(fnamevb.buf,&stabtmp) && S_ISDIR(stabtmp.st_mode)) {
805 debug(dbg_eachfiledetail, "tarobject directory exists");
806 existingdir = true;
808 break;
809 case TAR_FILETYPE_FILE:
810 case TAR_FILETYPE_CHARDEV:
811 case TAR_FILETYPE_BLOCKDEV:
812 case TAR_FILETYPE_FIFO:
813 case TAR_FILETYPE_HARDLINK:
814 break;
815 default:
816 ohshit(_("archive contained object '%.255s' of unknown type 0x%x"),
817 ti->name, ti->type);
820 keepexisting = false;
821 refcounting = false;
822 if (!existingdir) {
823 struct fsys_node_pkgs_iter *iter;
825 iter = fsys_node_pkgs_iter_new(nifd->namenode);
826 while ((otherpkg = fsys_node_pkgs_iter_next(iter))) {
827 if (otherpkg == tc->pkg)
828 continue;
829 debug(dbg_eachfile, "tarobject ... found in %s",
830 pkg_name(otherpkg, pnaw_always));
832 /* A pkgset can share files between its instances. Overwriting
833 * is allowed when they are not getting in sync, otherwise the
834 * file content must match the installed file. */
835 if (otherpkg->set == tc->pkg->set &&
836 otherpkg->installed.multiarch == PKG_MULTIARCH_SAME &&
837 tc->pkg->available.multiarch == PKG_MULTIARCH_SAME) {
838 if (statr == 0 && tc->pkgset_getting_in_sync)
839 refcounting = true;
840 debug(dbg_eachfiledetail, "tarobject ... shared with %s %s (syncing=%d)",
841 pkg_name(otherpkg, pnaw_always),
842 versiondescribe_c(&otherpkg->installed.version, vdew_nonambig),
843 tc->pkgset_getting_in_sync);
844 continue;
847 if (nifd->namenode->divert && nifd->namenode->divert->useinstead) {
848 /* Right, so we may be diverting this file. This makes the conflict
849 * OK iff one of us is the diverting package (we don't need to
850 * check for both being the diverting package, obviously). */
851 divpkgset = nifd->namenode->divert->pkgset;
852 debug(dbg_eachfile, "tarobject ... diverted, divpkgset=%s",
853 divpkgset ? divpkgset->name : "<none>");
854 if (otherpkg->set == divpkgset || tc->pkg->set == divpkgset)
855 continue;
858 /* If the new object is a directory and the previous object does
859 * not exist assume it's also a directory and skip further checks.
860 * XXX: Ideally with more information about the installed files we
861 * could perform more clever checks. */
862 if (statr != 0 && ti->type == TAR_FILETYPE_DIR) {
863 debug(dbg_eachfile, "tarobject ... assuming shared directory");
864 continue;
867 ensure_package_clientdata(otherpkg);
869 /* Nope? Hmm, file conflict, perhaps. Check Replaces. */
870 switch (otherpkg->clientdata->replacingfilesandsaid) {
871 case 2:
872 keepexisting = true;
873 /* Fall through. */
874 case 1:
875 continue;
878 /* Is the package with the conflicting file in the “config files only”
879 * state? If so it must be a config file and we can silently take it
880 * over. */
881 if (otherpkg->status == PKG_STAT_CONFIGFILES)
882 continue;
884 /* Perhaps we're removing a conflicting package? */
885 if (otherpkg->clientdata->istobe == PKG_ISTOBE_REMOVE)
886 continue;
888 /* Is the file an obsolete conffile in the other package
889 * and a conffile in the new package? */
890 if ((nifd->namenode->flags & FNNF_NEW_CONFF) &&
891 !statr && S_ISREG(stab.st_mode)) {
892 for (conff = otherpkg->installed.conffiles;
893 conff;
894 conff = conff->next) {
895 if (!conff->obsolete)
896 continue;
897 if (strcmp(conff->name, nifd->namenode->name) == 0)
898 break;
900 if (conff) {
901 debug(dbg_eachfiledetail, "tarobject other's obsolete conffile");
902 /* process_archive() will have copied its hash already. */
903 continue;
907 if (does_replace(tc->pkg, &tc->pkg->available,
908 otherpkg, &otherpkg->installed)) {
909 printf(_("Replacing files in old package %s (%s) ...\n"),
910 pkg_name(otherpkg, pnaw_nonambig),
911 versiondescribe(&otherpkg->installed.version, vdew_nonambig));
912 otherpkg->clientdata->replacingfilesandsaid = 1;
913 } else if (does_replace(otherpkg, &otherpkg->installed,
914 tc->pkg, &tc->pkg->available)) {
915 printf(_("Replaced by files in installed package %s (%s) ...\n"),
916 pkg_name(otherpkg, pnaw_nonambig),
917 versiondescribe(&otherpkg->installed.version, vdew_nonambig));
918 otherpkg->clientdata->replacingfilesandsaid = 2;
919 nifd->namenode->flags &= ~FNNF_NEW_INARCHIVE;
920 keepexisting = true;
921 } else {
922 /* At this point we are replacing something without a Replaces. */
923 if (!statr && S_ISDIR(stab.st_mode)) {
924 forcibleerr(FORCE_OVERWRITE_DIR,
925 _("trying to overwrite directory '%.250s' "
926 "in package %.250s %.250s with nondirectory"),
927 nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
928 versiondescribe(&otherpkg->installed.version,
929 vdew_nonambig));
930 } else {
931 forcibleerr(FORCE_OVERWRITE,
932 _("trying to overwrite '%.250s', "
933 "which is also in package %.250s %.250s"),
934 nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
935 versiondescribe(&otherpkg->installed.version,
936 vdew_nonambig));
940 fsys_node_pkgs_iter_free(iter);
943 if (keepexisting) {
944 if (nifd->namenode->flags & FNNF_NEW_CONFF)
945 nifd->namenode->flags |= FNNF_OBS_CONFF;
946 tar_fsys_namenode_queue_pop(tc->newfiles_queue, oldnifd, nifd);
947 tarobject_skip_entry(tc, ti);
948 return 0;
951 if (filter_should_skip(ti)) {
952 nifd->namenode->flags &= ~FNNF_NEW_INARCHIVE;
953 nifd->namenode->flags |= FNNF_FILTERED;
954 tarobject_skip_entry(tc, ti);
956 return 0;
959 if (existingdir)
960 return 0;
962 /* Compute the hash of the previous object, before we might replace it
963 * with the new version on forced overwrites. */
964 if (refcounting) {
965 debug(dbg_eachfiledetail, "tarobject computing on-disk file '%s' digest, refcounting",
966 fnamevb.buf);
967 if (nifd->namenode->flags & FNNF_NEW_CONFF) {
968 md5hash_prev_conffile(tc->pkg, oldhash, fnamenewvb.buf, nifd->namenode);
969 } else if (S_ISREG(stab.st_mode)) {
970 md5hash(tc->pkg, oldhash, fnamevb.buf);
971 } else {
972 strcpy(oldhash, EMPTYHASHFLAG);
976 if (refcounting && !in_force(FORCE_OVERWRITE)) {
977 /* If we are not forced to overwrite the path and are refcounting,
978 * just compute the hash w/o extracting the object. */
979 tarobject_hash(tc, ti, nifd->namenode);
980 } else {
981 /* Now, at this stage we want to make sure neither of .dpkg-new and
982 * .dpkg-tmp are hanging around. */
983 path_remove_tree(fnamenewvb.buf);
984 path_remove_tree(fnametmpvb.buf);
986 /* Now we start to do things that we need to be able to undo
987 * if something goes wrong. Watch out for the CLEANUP comments to
988 * keep an eye on what's installed on the disk at each point. */
989 push_cleanup(cu_installnew, ~ehflag_normaltidy, 1, nifd->namenode);
992 * CLEANUP: Now we either have the old file on the disk, or not, in
993 * its original filename.
996 /* Extract whatever it is as .dpkg-new ... */
997 tarobject_extract(tc, ti, fnamenewvb.buf, &nodestat, nifd->namenode);
1000 /* For shared files, check now if the object matches. */
1001 if (refcounting)
1002 tarobject_matches(tc, fnamevb.buf, &stab, oldhash,
1003 fnamenewvb.buf, ti, nifd->namenode);
1005 /* If we didn't extract anything, there's nothing else to do. */
1006 if (refcounting && !in_force(FORCE_OVERWRITE))
1007 return 0;
1009 tarobject_set_perms(ti, fnamenewvb.buf, &nodestat);
1010 tarobject_set_mtime(ti, fnamenewvb.buf);
1011 tarobject_set_se_context(fnamevb.buf, fnamenewvb.buf, nodestat.mode);
1014 * CLEANUP: Now we have extracted the new object in .dpkg-new (or,
1015 * if the file already exists as a directory and we were trying to
1016 * extract a directory or symlink, we returned earlier, so we don't
1017 * need to worry about that here).
1019 * The old file is still in the original filename,
1022 /* First, check to see if it's a conffile. If so we don't install
1023 * it now - we leave it in .dpkg-new for --configure to take care of. */
1024 if (nifd->namenode->flags & FNNF_NEW_CONFF) {
1025 debug(dbg_conffdetail,"tarobject conffile extracted");
1026 nifd->namenode->flags |= FNNF_ELIDE_OTHER_LISTS;
1027 return 0;
1030 /* Now we move the old file out of the way, the backup file will
1031 * be deleted later. */
1032 if (statr) {
1033 /* Don't try to back it up if it didn't exist. */
1034 debug(dbg_eachfiledetail,"tarobject new - no backup");
1035 } else {
1036 if (ti->type == TAR_FILETYPE_DIR || S_ISDIR(stab.st_mode)) {
1037 /* One of the two is a directory - can't do atomic install. */
1038 debug(dbg_eachfiledetail,"tarobject directory, nonatomic");
1039 nifd->namenode->flags |= FNNF_NO_ATOMIC_OVERWRITE;
1040 if (rename(fnamevb.buf,fnametmpvb.buf))
1041 ohshite(_("unable to move aside '%.255s' to install new version"),
1042 ti->name);
1043 } else if (S_ISLNK(stab.st_mode)) {
1044 ssize_t linksize;
1045 int rc;
1047 /* We can't make a symlink with two hardlinks, so we'll have to
1048 * copy it. (Pretend that making a copy of a symlink is the same
1049 * as linking to it.) */
1050 linksize = file_readlink(fnamevb.buf, &symlinkfn, stab.st_size);
1051 if (linksize < 0)
1052 ohshite(_("unable to read link '%.255s'"), ti->name);
1053 else if (linksize > stab.st_size)
1054 ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
1055 fnamevb.buf, (intmax_t)stab.st_size, linksize);
1056 else if (linksize < stab.st_size)
1057 warning(_("symbolic link '%.250s' size has changed from %jd to %zd"),
1058 fnamevb.buf, (intmax_t)stab.st_size, linksize);
1059 if (symlink(symlinkfn.buf,fnametmpvb.buf))
1060 ohshite(_("unable to make backup symlink for '%.255s'"), ti->name);
1061 rc = lchown(fnametmpvb.buf, stab.st_uid, stab.st_gid);
1062 if (forcible_nonroot_error(rc))
1063 ohshite(_("unable to chown backup symlink for '%.255s'"), ti->name);
1064 tarobject_set_se_context(fnamevb.buf, fnametmpvb.buf, stab.st_mode);
1065 } else {
1066 debug(dbg_eachfiledetail, "tarobject nondirectory, 'link' backup");
1067 if (link(fnamevb.buf,fnametmpvb.buf))
1068 ohshite(_("unable to make backup link of '%.255s' before installing new version"),
1069 ti->name);
1074 * CLEANUP: Now the old file is in .dpkg-tmp, and the new file is still
1075 * in .dpkg-new.
1078 if (ti->type == TAR_FILETYPE_FILE || ti->type == TAR_FILETYPE_HARDLINK ||
1079 ti->type == TAR_FILETYPE_SYMLINK) {
1080 nifd->namenode->flags |= FNNF_DEFERRED_RENAME;
1082 debug(dbg_eachfiledetail, "tarobject done and installation deferred");
1083 } else {
1084 if (rename(fnamenewvb.buf, fnamevb.buf))
1085 ohshite(_("unable to install new version of '%.255s'"), ti->name);
1088 * CLEANUP: Now the new file is in the destination file, and the
1089 * old file is in .dpkg-tmp to be cleaned up later. We now need
1090 * to take a different attitude to cleanup, because we need to
1091 * remove the new file.
1094 nifd->namenode->flags |= FNNF_PLACED_ON_DISK;
1095 nifd->namenode->flags |= FNNF_ELIDE_OTHER_LISTS;
1097 debug(dbg_eachfiledetail, "tarobject done and installed");
1100 return 0;
1103 #if defined(SYNC_FILE_RANGE_WAIT_BEFORE)
1104 static void
1105 tar_writeback_barrier(struct fsys_namenode_list *files, struct pkginfo *pkg)
1107 struct fsys_namenode_list *cfile;
1109 for (cfile = files; cfile; cfile = cfile->next) {
1110 struct fsys_namenode *usenode;
1111 int fd;
1113 if (!(cfile->namenode->flags & FNNF_DEFERRED_FSYNC))
1114 continue;
1116 usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
1118 setupfnamevbs(usenode->name);
1120 fd = open(fnamenewvb.buf, O_WRONLY);
1121 if (fd < 0)
1122 ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
1123 /* Ignore the return code as it should be considered equivalent to an
1124 * asynchronous hint for the kernel, we are doing an fsync() later on
1125 * anyway. */
1126 sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WAIT_BEFORE);
1127 if (close(fd))
1128 ohshite(_("error closing/writing '%.255s'"), fnamenewvb.buf);
1131 #else
1132 static void
1133 tar_writeback_barrier(struct fsys_namenode_list *files, struct pkginfo *pkg)
1136 #endif
1138 void
1139 tar_deferred_extract(struct fsys_namenode_list *files, struct pkginfo *pkg)
1141 struct fsys_namenode_list *cfile;
1142 struct fsys_namenode *usenode;
1144 tar_writeback_barrier(files, pkg);
1146 for (cfile = files; cfile; cfile = cfile->next) {
1147 debug(dbg_eachfile, "deferred extract of '%.255s'", cfile->namenode->name);
1149 if (!(cfile->namenode->flags & FNNF_DEFERRED_RENAME))
1150 continue;
1152 usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
1154 setupfnamevbs(usenode->name);
1156 if (cfile->namenode->flags & FNNF_DEFERRED_FSYNC) {
1157 int fd;
1159 debug(dbg_eachfiledetail, "deferred extract needs fsync");
1161 fd = open(fnamenewvb.buf, O_WRONLY);
1162 if (fd < 0)
1163 ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
1164 if (fsync(fd))
1165 ohshite(_("unable to sync file '%.255s'"), fnamenewvb.buf);
1166 if (close(fd))
1167 ohshite(_("error closing/writing '%.255s'"), fnamenewvb.buf);
1169 cfile->namenode->flags &= ~FNNF_DEFERRED_FSYNC;
1172 debug(dbg_eachfiledetail, "deferred extract needs rename");
1174 if (rename(fnamenewvb.buf, fnamevb.buf))
1175 ohshite(_("unable to install new version of '%.255s'"),
1176 cfile->namenode->name);
1178 cfile->namenode->flags &= ~FNNF_DEFERRED_RENAME;
1181 * CLEANUP: Now the new file is in the destination file, and the
1182 * old file is in .dpkg-tmp to be cleaned up later. We now need
1183 * to take a different attitude to cleanup, because we need to
1184 * remove the new file.
1187 cfile->namenode->flags |= FNNF_PLACED_ON_DISK;
1188 cfile->namenode->flags |= FNNF_ELIDE_OTHER_LISTS;
1190 debug(dbg_eachfiledetail, "deferred extract done and installed");
1194 void
1195 enqueue_deconfigure(struct pkginfo *pkg, struct pkginfo *pkg_removal,
1196 enum pkgwant reason)
1198 struct pkg_deconf_list *newdeconf;
1200 ensure_package_clientdata(pkg);
1201 pkg->clientdata->istobe = PKG_ISTOBE_DECONFIGURE;
1202 newdeconf = m_malloc(sizeof(*newdeconf));
1203 newdeconf->next = deconfigure;
1204 newdeconf->pkg = pkg;
1205 newdeconf->pkg_removal = pkg_removal;
1206 newdeconf->reason = reason;
1207 deconfigure = newdeconf;
1210 void
1211 clear_deconfigure_queue(void)
1213 struct pkg_deconf_list *deconf, *deconf_next;
1215 for (deconf = deconfigure; deconf; deconf = deconf_next) {
1216 deconf_next = deconf->next;
1217 free(deconf);
1219 deconfigure = NULL;
1223 * Try if we can deconfigure the package for installation and queue it if so.
1225 * This function gets called in the Breaks context, when trying to install
1226 * a package that might require another to be deconfigured to be able to
1227 * proceed.
1229 * First checks whether the pdep is forced.
1231 * @retval 0 Not possible (why is printed).
1232 * @retval 1 Deconfiguration queued ok (no message printed).
1233 * @retval 2 Forced (no deconfiguration needed, why is printed).
1235 static int
1236 try_deconfigure_can(struct pkginfo *pkg, struct deppossi *pdep,
1237 struct pkginfo *pkg_install, const char *why)
1239 if (force_breaks(pdep)) {
1240 warning(_("ignoring dependency problem with installation of %s:\n%s"),
1241 pkgbin_name(pkg_install, &pkg->available, pnaw_nonambig), why);
1242 return 2;
1243 } else if (f_autodeconf) {
1244 enqueue_deconfigure(pkg, NULL, PKG_WANT_INSTALL);
1245 return 1;
1246 } else {
1247 notice(_("no, cannot proceed with installation of %s (--auto-deconfigure will help):\n%s"),
1248 pkgbin_name(pkg_install, &pkg->available, pnaw_nonambig), why);
1249 return 0;
1254 * Try if we can deconfigure the package for removal and queue it if so.
1256 * This function gets called in the Conflicts+Depends context, when trying
1257 * to install a package that might require another to be fully removed to
1258 * be able to proceed.
1260 * First checks whether the pdep is forced, then if auto-configure is enabled
1261 * we make sure Essential and Protected are not allowed to be removed unless
1262 * forced.
1264 * @retval 0 Not possible (why is printed).
1265 * @retval 1 Deconfiguration queued ok (no message printed).
1266 * @retval 2 Forced (no deconfiguration needed, why is printed).
1268 static int
1269 try_remove_can(struct deppossi *pdep,
1270 struct pkginfo *pkg_removal, const char *why)
1272 struct pkginfo *pkg = pdep->up->up;
1274 if (force_depends(pdep)) {
1275 warning(_("ignoring dependency problem with removal of %s:\n%s"),
1276 pkg_name(pkg_removal, pnaw_nonambig), why);
1277 return 2;
1278 } else if (f_autodeconf) {
1279 if (pkg->installed.essential) {
1280 if (in_force(FORCE_REMOVE_ESSENTIAL)) {
1281 warning(_("considering deconfiguration of essential\n"
1282 " package %s, to enable removal of %s"),
1283 pkg_name(pkg, pnaw_nonambig), pkg_name(pkg_removal, pnaw_nonambig));
1284 } else {
1285 notice(_("no, %s is essential, will not deconfigure\n"
1286 " it in order to enable removal of %s"),
1287 pkg_name(pkg, pnaw_nonambig), pkg_name(pkg_removal, pnaw_nonambig));
1288 return 0;
1291 if (pkg->installed.is_protected) {
1292 if (in_force(FORCE_REMOVE_PROTECTED)) {
1293 warning(_("considering deconfiguration of protected\n"
1294 " package %s, to enable removal of %s"),
1295 pkg_name(pkg, pnaw_nonambig), pkg_name(pkg_removal, pnaw_nonambig));
1296 } else {
1297 notice(_("no, %s is protected, will not deconfigure\n"
1298 " it in order to enable removal of %s"),
1299 pkg_name(pkg, pnaw_nonambig), pkg_name(pkg_removal, pnaw_nonambig));
1300 return 0;
1304 enqueue_deconfigure(pkg, pkg_removal, PKG_WANT_DEINSTALL);
1305 return 1;
1306 } else {
1307 notice(_("no, cannot proceed with removal of %s (--auto-deconfigure will help):\n%s"),
1308 pkg_name(pkg_removal, pnaw_nonambig), why);
1309 return 0;
1313 void check_breaks(struct dependency *dep, struct pkginfo *pkg,
1314 const char *pfilename) {
1315 struct pkginfo *fixbydeconf;
1316 struct varbuf why = VARBUF_INIT;
1317 int ok;
1319 fixbydeconf = NULL;
1320 if (depisok(dep, &why, &fixbydeconf, NULL, false)) {
1321 varbuf_destroy(&why);
1322 return;
1325 varbuf_end_str(&why);
1327 if (fixbydeconf && f_autodeconf) {
1328 ensure_package_clientdata(fixbydeconf);
1330 if (fixbydeconf->clientdata->istobe != PKG_ISTOBE_NORMAL)
1331 internerr("package %s being fixed by deconf is not to be normal, "
1332 "is to be %d",
1333 pkg_name(pkg, pnaw_always), fixbydeconf->clientdata->istobe);
1335 notice(_("considering deconfiguration of %s, which would be broken by installation of %s ..."),
1336 pkg_name(fixbydeconf, pnaw_nonambig),
1337 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1339 ok = try_deconfigure_can(fixbydeconf, dep->list, pkg, why.buf);
1340 if (ok == 1) {
1341 notice(_("yes, will deconfigure %s (broken by %s)"),
1342 pkg_name(fixbydeconf, pnaw_nonambig),
1343 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1345 } else {
1346 notice(_("regarding %s containing %s:\n%s"), pfilename,
1347 pkgbin_name(pkg, &pkg->available, pnaw_nonambig), why.buf);
1348 ok= 0;
1350 varbuf_destroy(&why);
1351 if (ok > 0) return;
1353 if (force_breaks(dep->list)) {
1354 warning(_("ignoring breakage, may proceed anyway!"));
1355 return;
1358 if (fixbydeconf && !f_autodeconf) {
1359 ohshit(_("installing %.250s would break %.250s, and\n"
1360 " deconfiguration is not permitted (--auto-deconfigure might help)"),
1361 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
1362 pkg_name(fixbydeconf, pnaw_nonambig));
1363 } else {
1364 ohshit(_("installing %.250s would break existing software"),
1365 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1369 void check_conflict(struct dependency *dep, struct pkginfo *pkg,
1370 const char *pfilename) {
1371 struct pkginfo *fixbyrm;
1372 struct deppossi *pdep, flagdeppossi = { 0 };
1373 struct varbuf conflictwhy = VARBUF_INIT, removalwhy = VARBUF_INIT;
1374 struct dependency *providecheck;
1376 fixbyrm = NULL;
1377 if (depisok(dep, &conflictwhy, &fixbyrm, NULL, false)) {
1378 varbuf_destroy(&conflictwhy);
1379 varbuf_destroy(&removalwhy);
1380 return;
1382 if (fixbyrm) {
1383 ensure_package_clientdata(fixbyrm);
1384 if (fixbyrm->clientdata->istobe == PKG_ISTOBE_INSTALLNEW) {
1385 fixbyrm= dep->up;
1386 ensure_package_clientdata(fixbyrm);
1388 if (((pkg->available.essential || pkg->available.is_protected) &&
1389 (fixbyrm->installed.essential || fixbyrm->installed.is_protected)) ||
1390 (((fixbyrm->want != PKG_WANT_INSTALL &&
1391 fixbyrm->want != PKG_WANT_HOLD) ||
1392 does_replace(pkg, &pkg->available, fixbyrm, &fixbyrm->installed)) &&
1393 ((!fixbyrm->installed.essential || in_force(FORCE_REMOVE_ESSENTIAL)) ||
1394 (!fixbyrm->installed.is_protected || in_force(FORCE_REMOVE_PROTECTED))))) {
1395 if (fixbyrm->clientdata->istobe != PKG_ISTOBE_NORMAL &&
1396 fixbyrm->clientdata->istobe != PKG_ISTOBE_DECONFIGURE)
1397 internerr("package %s to be fixed by removal is not to be normal "
1398 "nor deconfigure, is to be %d",
1399 pkg_name(pkg, pnaw_always), fixbyrm->clientdata->istobe);
1400 fixbyrm->clientdata->istobe = PKG_ISTOBE_REMOVE;
1401 notice(_("considering removing %s in favour of %s ..."),
1402 pkg_name(fixbyrm, pnaw_nonambig),
1403 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1404 if (!(fixbyrm->status == PKG_STAT_INSTALLED ||
1405 fixbyrm->status == PKG_STAT_TRIGGERSPENDING ||
1406 fixbyrm->status == PKG_STAT_TRIGGERSAWAITED)) {
1407 notice(_("%s is not properly installed; ignoring any dependencies on it"),
1408 pkg_name(fixbyrm, pnaw_nonambig));
1409 pdep = NULL;
1410 } else {
1411 for (pdep = fixbyrm->set->depended.installed;
1412 pdep;
1413 pdep = pdep->rev_next) {
1414 if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
1415 continue;
1416 if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
1417 continue;
1418 varbuf_end_str(&removalwhy);
1419 if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
1420 break;
1422 if (!pdep) {
1423 /* If we haven't found a reason not to yet, let's look some more. */
1424 for (providecheck= fixbyrm->installed.depends;
1425 providecheck;
1426 providecheck= providecheck->next) {
1427 if (providecheck->type != dep_provides) continue;
1428 for (pdep = providecheck->list->ed->depended.installed;
1429 pdep;
1430 pdep = pdep->rev_next) {
1431 if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
1432 continue;
1433 if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
1434 continue;
1435 varbuf_end_str(&removalwhy);
1436 notice(_("may have trouble removing %s, as it provides %s ..."),
1437 pkg_name(fixbyrm, pnaw_nonambig),
1438 providecheck->list->ed->name);
1439 if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
1440 goto break_from_both_loops_at_once;
1443 break_from_both_loops_at_once:;
1446 if (!pdep && skip_due_to_hold(fixbyrm)) {
1447 pdep= &flagdeppossi;
1449 if (!pdep && (fixbyrm->eflag & PKG_EFLAG_REINSTREQ)) {
1450 if (in_force(FORCE_REMOVE_REINSTREQ)) {
1451 notice(_("package %s requires reinstallation, but will "
1452 "remove anyway as you requested"),
1453 pkg_name(fixbyrm, pnaw_nonambig));
1454 } else {
1455 notice(_("package %s requires reinstallation, will not remove"),
1456 pkg_name(fixbyrm, pnaw_nonambig));
1457 pdep= &flagdeppossi;
1460 if (!pdep) {
1461 /* This conflict is OK - we'll remove the conflictor. */
1462 enqueue_conflictor(fixbyrm);
1463 varbuf_destroy(&conflictwhy); varbuf_destroy(&removalwhy);
1464 notice(_("yes, will remove %s in favour of %s"),
1465 pkg_name(fixbyrm, pnaw_nonambig),
1466 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1467 return;
1469 /* Put it back. */
1470 fixbyrm->clientdata->istobe = PKG_ISTOBE_NORMAL;
1473 varbuf_end_str(&conflictwhy);
1474 notice(_("regarding %s containing %s:\n%s"), pfilename,
1475 pkgbin_name(pkg, &pkg->available, pnaw_nonambig), conflictwhy.buf);
1476 if (!force_conflicts(dep->list))
1477 ohshit(_("conflicting packages - not installing %.250s"),
1478 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1479 warning(_("ignoring conflict, may proceed anyway!"));
1480 varbuf_destroy(&conflictwhy);
1482 return;
1485 void cu_cidir(int argc, void **argv) {
1486 char *cidir= (char*)argv[0];
1487 char *cidirrest= (char*)argv[1];
1488 cidirrest[-1] = '\0';
1489 path_remove_tree(cidir);
1490 free(cidir);
1493 void cu_fileslist(int argc, void **argv) {
1494 tar_pool_release();
1498 archivefiles(const char *const *argv)
1500 const char *const *volatile argp;
1501 char **volatile arglist = NULL;
1502 int i;
1503 jmp_buf ejbuf;
1504 enum modstatdb_rw msdbflags;
1506 trigproc_install_hooks();
1508 if (f_noact)
1509 msdbflags = msdbrw_readonly;
1510 else if (cipaction->arg_int == act_avail)
1511 msdbflags = msdbrw_readonly | msdbrw_available_write;
1512 else if (in_force(FORCE_NON_ROOT))
1513 msdbflags = msdbrw_write;
1514 else
1515 msdbflags = msdbrw_needsuperuser;
1517 modstatdb_open(msdbflags);
1519 checkpath();
1520 pkg_infodb_upgrade();
1522 log_message("startup archives %s", cipaction->olong);
1524 if (f_recursive) {
1525 const char *const *ap;
1526 int nfiles = 0;
1528 if (!*argv)
1529 badusage(_("--%s --recursive needs at least one path argument"),cipaction->olong);
1531 for (ap = argv; *ap; ap++) {
1532 struct treeroot *tree;
1533 struct treenode *node;
1535 tree = treewalk_open((const char *)*ap, TREEWALK_FOLLOW_LINKS, NULL);
1537 while ((node = treewalk_next(tree))) {
1538 const char *nodename;
1540 if (!S_ISREG(treenode_get_mode(node)))
1541 continue;
1543 /* Check if it looks like a .deb file. */
1544 nodename = treenode_get_pathname(node);
1545 if (strcmp(nodename + strlen(nodename) - 4, DEBEXT) != 0)
1546 continue;
1548 arglist = m_realloc(arglist, sizeof(char *) * (nfiles + 2));
1549 arglist[nfiles++] = m_strdup(nodename);
1552 treewalk_close(tree);
1555 if (!nfiles)
1556 ohshit(_("searched, but found no packages (files matching *.deb)"));
1558 arglist[nfiles] = NULL;
1559 argp = (const char **volatile)arglist;
1560 } else {
1561 if (!*argv) badusage(_("--%s needs at least one package archive file argument"),
1562 cipaction->olong);
1563 argp= argv;
1566 /* Perform some sanity checks on the passed archives. */
1567 for (i = 0; argp[i]; i++) {
1568 struct stat st;
1570 /* We need the filename to exist. */
1571 if (stat(argp[i], &st) < 0)
1572 ohshite(_("cannot access archive '%s'"), argp[i]);
1574 /* We cannot work with anything that is not a regular file. */
1575 if (!S_ISREG(st.st_mode))
1576 ohshit(_("archive '%s' is not a regular file"), argp[i]);
1579 currenttime = time(NULL);
1581 /* Initialize fname variables contents. */
1583 varbuf_reset(&fnamevb);
1584 varbuf_reset(&fnametmpvb);
1585 varbuf_reset(&fnamenewvb);
1587 varbuf_add_str(&fnamevb, dpkg_fsys_get_dir());
1588 varbuf_add_str(&fnametmpvb, dpkg_fsys_get_dir());
1589 varbuf_add_str(&fnamenewvb, dpkg_fsys_get_dir());
1591 varbuf_snapshot(&fnamevb, &fname_state);
1592 varbuf_snapshot(&fnametmpvb, &fnametmp_state);
1593 varbuf_snapshot(&fnamenewvb, &fnamenew_state);
1595 ensure_diversions();
1596 ensure_statoverrides(STATDB_PARSE_NORMAL);
1598 for (i = 0; argp[i]; i++) {
1599 if (setjmp(ejbuf)) {
1600 pop_error_context(ehflag_bombout);
1601 if (abort_processing)
1602 break;
1603 continue;
1605 push_error_context_jump(&ejbuf, print_error_perarchive, argp[i]);
1607 dpkg_selabel_load();
1609 process_archive(argp[i]);
1610 onerr_abort++;
1611 m_output(stdout, _("<standard output>"));
1612 m_output(stderr, _("<standard error>"));
1613 onerr_abort--;
1615 pop_error_context(ehflag_normaltidy);
1618 dpkg_selabel_close();
1620 if (arglist) {
1621 for (i = 0; arglist[i]; i++)
1622 free(arglist[i]);
1623 free(arglist);
1626 switch (cipaction->arg_int) {
1627 case act_install:
1628 case act_configure:
1629 case act_triggers:
1630 case act_remove:
1631 case act_purge:
1632 process_queue();
1633 case act_unpack:
1634 case act_avail:
1635 break;
1636 default:
1637 internerr("unknown action '%d'", cipaction->arg_int);
1640 trigproc_run_deferred();
1641 modstatdb_shutdown();
1643 return 0;
1647 * Decide whether we want to install a new version of the package.
1649 * @param pkg The package with the version we might want to install
1651 * @retval true If the package should be skipped.
1652 * @retval false If the package should be installed.
1654 bool
1655 wanttoinstall(struct pkginfo *pkg)
1657 int rc;
1659 if (pkg->want != PKG_WANT_INSTALL && pkg->want != PKG_WANT_HOLD) {
1660 if (f_alsoselect) {
1661 printf(_("Selecting previously unselected package %s.\n"),
1662 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1663 return true;
1664 } else {
1665 printf(_("Skipping unselected package %s.\n"),
1666 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1667 return false;
1671 if (pkg->eflag & PKG_EFLAG_REINSTREQ)
1672 return true;
1673 if (pkg->status < PKG_STAT_UNPACKED)
1674 return true;
1676 rc = dpkg_version_compare(&pkg->available.version, &pkg->installed.version);
1677 if (rc > 0) {
1678 return true;
1679 } else if (rc == 0) {
1680 /* Same version fully installed. */
1681 if (f_skipsame && pkg->available.arch == pkg->installed.arch) {
1682 notice(_("version %.250s of %.250s already installed, skipping"),
1683 versiondescribe(&pkg->installed.version, vdew_nonambig),
1684 pkg_name(pkg, pnaw_nonambig));
1685 return false;
1686 } else {
1687 return true;
1689 } else {
1690 if (in_force(FORCE_DOWNGRADE)) {
1691 warning(_("downgrading %.250s from %.250s to %.250s"),
1692 pkg_name(pkg, pnaw_nonambig),
1693 versiondescribe(&pkg->installed.version, vdew_nonambig),
1694 versiondescribe(&pkg->available.version, vdew_nonambig));
1695 return true;
1696 } else {
1697 notice(_("will not downgrade %.250s from %.250s to %.250s, skipping"),
1698 pkg_name(pkg, pnaw_nonambig),
1699 versiondescribe(&pkg->installed.version, vdew_nonambig),
1700 versiondescribe(&pkg->available.version, vdew_nonambig));
1701 return false;