doc: Move Perl version baseline as the first perl coding style subsection
[dpkg.git] / src / main / archives.c
blob0062600c25064d8c97653b36114bb98566cf4c6f
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 return obstack_alloc(&tar_pool, size);
95 /**
96 * Free memory from the tar memory pool.
98 static void
99 tar_pool_free(void *ptr)
101 obstack_free(&tar_pool, ptr);
105 * Release the tar memory pool.
107 static void
108 tar_pool_release(void)
110 if (tar_pool_init) {
111 obstack_free(&tar_pool, NULL);
112 tar_pool_init = false;
116 struct fsys_namenode_list *
117 tar_fsys_namenode_queue_push(struct fsys_namenode_queue *queue,
118 struct fsys_namenode *namenode)
120 struct fsys_namenode_list *node;
122 node = tar_pool_alloc(sizeof(*node));
123 node->namenode = namenode;
124 node->next = NULL;
126 *queue->tail = node;
127 queue->tail = &node->next;
129 return node;
132 static void
133 tar_fsys_namenode_queue_pop(struct fsys_namenode_queue *queue,
134 struct fsys_namenode_list **tail_prev,
135 struct fsys_namenode_list *node)
137 tar_pool_free(node);
138 queue->tail = tail_prev;
139 *tail_prev = NULL;
143 * Check if a file or directory will save a package from disappearance.
145 * A package can only be saved by a file or directory which is part
146 * only of itself - it must be neither part of the new package being
147 * installed nor part of any 3rd package (this is important so that
148 * shared directories don't stop packages from disappearing).
150 bool
151 filesavespackage(struct fsys_namenode_list *file,
152 struct pkginfo *pkgtobesaved,
153 struct pkginfo *pkgbeinginstalled)
155 struct fsys_node_pkgs_iter *iter;
156 struct pkginfo *thirdpkg;
158 debug(dbg_eachfiledetail, "filesavespackage file '%s' package %s",
159 file->namenode->name, pkg_name(pkgtobesaved, pnaw_always));
161 /* If the file is a contended one and it's overridden by either
162 * the package we're considering disappearing or the package
163 * we're installing then they're not actually the same file, so
164 * we can't disappear the package - it is saved by this file. */
165 if (file->namenode->divert && file->namenode->divert->useinstead) {
166 struct pkgset *divpkgset;
168 divpkgset = file->namenode->divert->pkgset;
169 if (divpkgset == pkgtobesaved->set || divpkgset == pkgbeinginstalled->set) {
170 debug(dbg_eachfiledetail,"filesavespackage ... diverted -- save!");
171 return true;
174 /* Is the file in the package being installed? If so then it can't save. */
175 if (file->namenode->flags & FNNF_NEW_INARCHIVE) {
176 debug(dbg_eachfiledetail,"filesavespackage ... in new archive -- no save");
177 return false;
179 /* Look for a 3rd package which can take over the file (in case
180 * it's a directory which is shared by many packages. */
181 iter = fsys_node_pkgs_iter_new(file->namenode);
182 while ((thirdpkg = fsys_node_pkgs_iter_next(iter))) {
183 debug(dbg_eachfiledetail, "filesavespackage ... also in %s",
184 pkg_name(thirdpkg, pnaw_always));
186 /* Is this not the package being installed or the one being
187 * checked for disappearance? */
188 if (thirdpkg == pkgbeinginstalled || thirdpkg == pkgtobesaved)
189 continue;
191 /* A Multi-Arch: same package can share files and their presence in a
192 * third package of the same set is not a sign that we can get rid of
193 * it. */
194 if (pkgtobesaved->installed.multiarch == PKG_MULTIARCH_SAME &&
195 thirdpkg->set == pkgtobesaved->set)
196 continue;
198 debug(dbg_eachfiledetail,"filesavespackage ... is 3rd package");
200 /* If !files_list_valid then we have already disappeared this one,
201 * so we should not try to make it take over this shared directory. */
202 if (!thirdpkg->files_list_valid) {
203 debug(dbg_eachfiledetail, "process_archive ... already disappeared!");
204 continue;
207 /* We've found a package that can take this file. */
208 debug(dbg_eachfiledetail, "filesavespackage ... taken -- no save");
209 fsys_node_pkgs_iter_free(iter);
210 return false;
212 fsys_node_pkgs_iter_free(iter);
214 debug(dbg_eachfiledetail, "filesavespackage ... not taken -- save !");
215 return true;
218 static void
219 md5hash_prev_conffile(struct pkginfo *pkg, char *oldhash, const char *oldname,
220 struct fsys_namenode *namenode)
222 struct pkginfo *otherpkg;
223 struct conffile *conff;
225 debug(dbg_conffdetail, "tarobject looking for shared conffile %s",
226 namenode->name);
228 for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
229 if (otherpkg == pkg)
230 continue;
231 /* If we are reinstalling, even if the other package is only unpacked,
232 * we can always make use of the Conffiles hash value from an initial
233 * installation, if that happened at all. */
234 if (otherpkg->status <= PKG_STAT_UNPACKED &&
235 dpkg_version_compare(&otherpkg->installed.version,
236 &otherpkg->configversion) != 0)
237 continue;
238 for (conff = otherpkg->installed.conffiles; conff; conff = conff->next) {
239 if (conff->obsolete || conff->remove_on_upgrade)
240 continue;
241 if (strcmp(conff->name, namenode->name) == 0)
242 break;
244 if (conff) {
245 strcpy(oldhash, conff->hash);
246 debug(dbg_conffdetail,
247 "tarobject found shared conffile, from pkg %s (%s); digest=%s",
248 pkg_name(otherpkg, pnaw_always),
249 pkg_status_name(otherpkg), oldhash);
250 break;
254 /* If no package was found with a valid Conffiles field, we make the
255 * risky assumption that the hash of the current .dpkg-new file is
256 * the one of the previously unpacked package. */
257 if (otherpkg == NULL) {
258 md5hash(pkg, oldhash, oldname);
259 debug(dbg_conffdetail,
260 "tarobject found shared conffile, from disk; digest=%s", oldhash);
264 void cu_pathname(int argc, void **argv) {
265 path_remove_tree((char*)(argv[0]));
269 tarfileread(struct tar_archive *tar, char *buf, int len)
271 struct tarcontext *tc = (struct tarcontext *)tar->ctx;
272 int r;
274 r = fd_read(tc->backendpipe, buf, len);
275 if (r < 0)
276 ohshite(_("error reading from dpkg-deb pipe"));
277 return r;
280 static void
281 tarobject_skip_padding(struct tarcontext *tc, struct tar_entry *te)
283 struct dpkg_error err;
284 size_t remainder;
286 remainder = te->size % TARBLKSZ;
287 if (remainder == 0)
288 return;
290 if (fd_skip(tc->backendpipe, TARBLKSZ - remainder, &err) < 0)
291 ohshit(_("cannot skip padding for file '%.255s': %s"), te->name, err.str);
294 static void
295 tarobject_skip_entry(struct tarcontext *tc, struct tar_entry *ti)
297 /* We need to advance the tar file to the next object, so read the
298 * file data and set it to oblivion. */
299 if (ti->type == TAR_FILETYPE_FILE) {
300 struct dpkg_error err;
302 if (fd_skip(tc->backendpipe, ti->size, &err) < 0)
303 ohshit(_("cannot skip file '%.255s' (replaced or excluded?) from pipe: %s"),
304 ti->name, err.str);
305 tarobject_skip_padding(tc, ti);
309 struct varbuf_state fname_state;
310 struct varbuf_state fnametmp_state;
311 struct varbuf_state fnamenew_state;
312 struct varbuf fnamevb;
313 struct varbuf fnametmpvb;
314 struct varbuf fnamenewvb;
315 struct pkg_deconf_list *deconfigure = NULL;
317 static time_t currenttime;
319 static int
320 does_replace(struct pkginfo *new_pkg, struct pkgbin *new_pkgbin,
321 struct pkginfo *old_pkg, struct pkgbin *old_pkgbin)
323 struct dependency *dep;
325 debug(dbg_depcon,"does_replace new=%s old=%s (%s)",
326 pkgbin_name(new_pkg, new_pkgbin, pnaw_always),
327 pkgbin_name(old_pkg, old_pkgbin, pnaw_always),
328 versiondescribe_c(&old_pkgbin->version, vdew_always));
329 for (dep = new_pkgbin->depends; dep; dep = dep->next) {
330 if (dep->type != dep_replaces || dep->list->ed != old_pkg->set)
331 continue;
332 debug(dbg_depcondetail,"does_replace ... found old, version %s",
333 versiondescribe_c(&dep->list->version,vdew_always));
334 if (!versionsatisfied(old_pkgbin, dep->list))
335 continue;
336 /* The test below can only trigger if dep_replaces start having
337 * arch qualifiers different from “any”. */
338 if (!archsatisfied(old_pkgbin, dep->list))
339 continue;
340 debug(dbg_depcon,"does_replace ... yes");
341 return true;
343 debug(dbg_depcon,"does_replace ... no");
344 return false;
347 static void
348 tarobject_extract(struct tarcontext *tc, struct tar_entry *te,
349 const char *path, struct file_stat *st,
350 struct fsys_namenode *namenode)
352 static struct varbuf hardlinkfn;
353 static int fd;
355 struct dpkg_error err;
356 struct fsys_namenode *linknode;
357 char *newhash;
358 int rc;
360 switch (te->type) {
361 case TAR_FILETYPE_FILE:
362 /* We create the file with mode 0 to make sure nobody can do anything with
363 * it until we apply the proper mode, which might be a statoverride. */
364 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0);
365 if (fd < 0)
366 ohshite(_("unable to create '%.255s' (while processing '%.255s')"),
367 path, te->name);
368 push_cleanup(cu_closefd, ehflag_bombout, 1, &fd);
369 debug(dbg_eachfiledetail, "tarobject file open size=%jd",
370 (intmax_t)te->size);
372 /* We try to tell the filesystem how much disk space we are going to
373 * need to let it reduce fragmentation and possibly improve performance,
374 * as we do know the size beforehand. */
375 fd_allocate_size(fd, 0, te->size);
377 newhash = nfmalloc(MD5HASHLEN + 1);
378 if (fd_fd_copy_and_md5(tc->backendpipe, fd, newhash, te->size, &err) < 0)
379 ohshit(_("cannot copy extracted data for '%.255s' to '%.255s': %s"),
380 te->name, fnamenewvb.buf, err.str);
381 namenode->newhash = newhash;
382 debug(dbg_eachfiledetail, "tarobject file digest=%s", namenode->newhash);
384 tarobject_skip_padding(tc, te);
386 fd_writeback_init(fd);
388 if (namenode->statoverride)
389 debug(dbg_eachfile, "tarobject ... stat override, uid=%d, gid=%d, mode=%04o",
390 namenode->statoverride->uid,
391 namenode->statoverride->gid,
392 namenode->statoverride->mode);
393 rc = fchown(fd, st->uid, st->gid);
394 if (forcible_nonroot_error(rc))
395 ohshite(_("error setting ownership of '%.255s'"), te->name);
396 rc = fchmod(fd, st->mode & ~S_IFMT);
397 if (forcible_nonroot_error(rc))
398 ohshite(_("error setting permissions of '%.255s'"), te->name);
400 /* Postpone the fsync, to try to avoid massive I/O degradation. */
401 if (!in_force(FORCE_UNSAFE_IO))
402 namenode->flags |= FNNF_DEFERRED_FSYNC;
404 pop_cleanup(ehflag_normaltidy); /* fd = open(path) */
405 if (close(fd))
406 ohshite(_("error closing/writing '%.255s'"), te->name);
407 break;
408 case TAR_FILETYPE_FIFO:
409 if (mkfifo(path, 0))
410 ohshite(_("error creating pipe '%.255s'"), te->name);
411 debug(dbg_eachfiledetail, "tarobject fifo");
412 break;
413 case TAR_FILETYPE_CHARDEV:
414 if (mknod(path, S_IFCHR, te->dev))
415 ohshite(_("error creating device '%.255s'"), te->name);
416 debug(dbg_eachfiledetail, "tarobject chardev");
417 break;
418 case TAR_FILETYPE_BLOCKDEV:
419 if (mknod(path, S_IFBLK, te->dev))
420 ohshite(_("error creating device '%.255s'"), te->name);
421 debug(dbg_eachfiledetail, "tarobject blockdev");
422 break;
423 case TAR_FILETYPE_HARDLINK:
424 varbuf_reset(&hardlinkfn);
425 varbuf_add_str(&hardlinkfn, dpkg_fsys_get_dir());
426 linknode = fsys_hash_find_node(te->linkname, 0);
427 varbuf_add_str(&hardlinkfn,
428 namenodetouse(linknode, tc->pkg, &tc->pkg->available)->name);
429 if (linknode->flags & (FNNF_DEFERRED_RENAME | FNNF_NEW_CONFF))
430 varbuf_add_str(&hardlinkfn, DPKGNEWEXT);
431 varbuf_end_str(&hardlinkfn);
432 if (link(hardlinkfn.buf, path))
433 ohshite(_("error creating hard link '%.255s'"), te->name);
434 namenode->newhash = linknode->newhash;
435 debug(dbg_eachfiledetail, "tarobject hardlink digest=%s", namenode->newhash);
436 break;
437 case TAR_FILETYPE_SYMLINK:
438 /* We've already checked for an existing directory. */
439 if (symlink(te->linkname, path))
440 ohshite(_("error creating symbolic link '%.255s'"), te->name);
441 debug(dbg_eachfiledetail, "tarobject symlink creating");
442 break;
443 case TAR_FILETYPE_DIR:
444 /* We've already checked for an existing directory. */
445 if (mkdir(path, 0))
446 ohshite(_("error creating directory '%.255s'"), te->name);
447 debug(dbg_eachfiledetail, "tarobject directory creating");
448 break;
449 default:
450 internerr("unknown tar type '%d', but already checked", te->type);
454 static void
455 tarobject_hash(struct tarcontext *tc, struct tar_entry *te,
456 struct fsys_namenode *namenode)
458 if (te->type == TAR_FILETYPE_FILE) {
459 struct dpkg_error err;
460 char *newhash;
462 newhash = nfmalloc(MD5HASHLEN + 1);
463 if (fd_md5(tc->backendpipe, newhash, te->size, &err) < 0)
464 ohshit(_("cannot compute MD5 digest for file '%.255s' in tar archive: %s"),
465 te->name, err.str);
466 tarobject_skip_padding(tc, te);
468 namenode->newhash = newhash;
469 debug(dbg_eachfiledetail, "tarobject file digest=%s", namenode->newhash);
470 } else if (te->type == TAR_FILETYPE_HARDLINK) {
471 struct fsys_namenode *linknode;
473 linknode = fsys_hash_find_node(te->linkname, 0);
474 namenode->newhash = linknode->newhash;
475 debug(dbg_eachfiledetail, "tarobject hardlink digest=%s", namenode->newhash);
479 static void
480 tarobject_set_mtime(struct tar_entry *te, const char *path)
482 struct timeval tv[2];
484 tv[0].tv_sec = currenttime;
485 tv[0].tv_usec = 0;
486 tv[1].tv_sec = te->mtime;
487 tv[1].tv_usec = 0;
489 if (te->type == TAR_FILETYPE_SYMLINK) {
490 #ifdef HAVE_LUTIMES
491 if (lutimes(path, tv) && errno != ENOSYS)
492 ohshite(_("error setting timestamps of '%.255s'"), path);
493 #endif
494 } else {
495 if (utimes(path, tv))
496 ohshite(_("error setting timestamps of '%.255s'"), path);
500 static void
501 tarobject_set_perms(struct tar_entry *te, const char *path, struct file_stat *st)
503 int rc;
505 if (te->type == TAR_FILETYPE_FILE)
506 return; /* Already handled using the file descriptor. */
508 if (te->type == TAR_FILETYPE_SYMLINK) {
509 rc = lchown(path, st->uid, st->gid);
510 if (forcible_nonroot_error(rc))
511 ohshite(_("error setting ownership of symlink '%.255s'"), path);
512 } else {
513 rc = chown(path, st->uid, st->gid);
514 if (forcible_nonroot_error(rc))
515 ohshite(_("error setting ownership of '%.255s'"), path);
516 rc = chmod(path, st->mode & ~S_IFMT);
517 if (forcible_nonroot_error(rc))
518 ohshite(_("error setting permissions of '%.255s'"), path);
522 static void
523 tarobject_set_se_context(const char *matchpath, const char *path, mode_t mode)
525 dpkg_selabel_set_context(matchpath, path, mode);
528 static void
529 tarobject_matches(struct tarcontext *tc,
530 const char *fn_old, struct stat *stab, char *oldhash,
531 const char *fn_new, struct tar_entry *te,
532 struct fsys_namenode *namenode)
534 char *linkname;
535 ssize_t linksize;
537 debug(dbg_eachfiledetail, "tarobject matches on-disk object?");
539 switch (te->type) {
540 case TAR_FILETYPE_DIR:
541 /* Nothing to check for a new directory. */
542 return;
543 case TAR_FILETYPE_SYMLINK:
544 /* Symlinks to existing dirs have already been dealt with, only
545 * remain real symlinks where we can compare the target. */
546 if (!S_ISLNK(stab->st_mode))
547 break;
548 linkname = m_malloc(stab->st_size + 1);
549 linksize = readlink(fn_old, linkname, stab->st_size + 1);
550 if (linksize < 0)
551 ohshite(_("unable to read link '%.255s'"), fn_old);
552 else if (linksize > stab->st_size)
553 ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
554 fn_old, (intmax_t)stab->st_size, linksize);
555 else if (linksize < stab->st_size)
556 warning(_("symbolic link '%.250s' size has changed from %jd to %zd"),
557 fn_old, (intmax_t)stab->st_size, linksize);
558 linkname[linksize] = '\0';
559 if (strcmp(linkname, te->linkname) == 0) {
560 free(linkname);
561 return;
562 } else {
563 free(linkname);
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 varbuf_reset(symlinkfn);
636 if (ti->linkname[0] == '/') {
637 varbuf_add_str(symlinkfn, dpkg_fsys_get_dir());
638 } else {
639 const char *lastslash;
641 lastslash= strrchr(fname, '/');
642 if (lastslash == NULL)
643 internerr("tar entry filename '%s' does not contain '/'", fname);
644 varbuf_add_buf(symlinkfn, fname, (lastslash - fname) + 1);
646 varbuf_add_str(symlinkfn, ti->linkname);
647 varbuf_end_str(symlinkfn);
649 statr= stat(symlinkfn->buf, &newstab);
650 if (statr) {
651 if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
652 ohshite(_("failed to stat (dereference) proposed new symlink target"
653 " '%.250s' for symlink '%.250s'"), symlinkfn->buf, fname);
654 return false;
656 if (!S_ISDIR(newstab.st_mode))
657 return false;
658 if (newstab.st_dev != oldstab.st_dev ||
659 newstab.st_ino != oldstab.st_ino)
660 return false;
661 return true;
665 tarobject(struct tar_archive *tar, struct tar_entry *ti)
667 static struct varbuf conffderefn, symlinkfn;
668 const char *usename;
669 struct fsys_namenode *namenode, *usenode;
671 struct conffile *conff;
672 struct tarcontext *tc = tar->ctx;
673 bool existingdir, keepexisting;
674 bool refcounting;
675 char oldhash[MD5HASHLEN + 1];
676 int statr;
677 ssize_t r;
678 struct stat stab, stabtmp;
679 struct file_stat nodestat;
680 struct fsys_namenode_list *nifd, **oldnifd;
681 struct pkgset *divpkgset;
682 struct pkginfo *otherpkg;
684 tar_entry_update_from_system(ti);
686 /* Perform some sanity checks on the tar entry. */
687 if (strchr(ti->name, '\n'))
688 ohshit(_("newline not allowed in archive object name '%.255s'"), ti->name);
690 namenode = fsys_hash_find_node(ti->name, 0);
692 if (namenode->flags & FNNF_RM_CONFF_ON_UPGRADE)
693 ohshit(_("conffile '%s' marked for removal on upgrade, shipped in package"),
694 ti->name);
696 /* Append to list of files.
697 * The trailing ‘/’ put on the end of names in tarfiles has already
698 * been stripped by tar_extractor(). */
699 oldnifd = tc->newfiles_queue->tail;
700 nifd = tar_fsys_namenode_queue_push(tc->newfiles_queue, namenode);
701 nifd->namenode->flags |= FNNF_NEW_INARCHIVE;
703 debug(dbg_eachfile,
704 "tarobject ti->name='%s' mode=%lo owner=%u:%u type=%d(%c)"
705 " ti->linkname='%s' namenode='%s' flags=%o instead='%s'",
706 ti->name, (long)ti->stat.mode,
707 (unsigned)ti->stat.uid, (unsigned)ti->stat.gid,
708 ti->type,
709 ti->type >= '0' && ti->type <= '6' ? "-hlcbdp"[ti->type - '0'] : '?',
710 ti->linkname,
711 nifd->namenode->name, nifd->namenode->flags,
712 nifd->namenode->divert && nifd->namenode->divert->useinstead
713 ? nifd->namenode->divert->useinstead->name : "<none>");
715 if (nifd->namenode->divert && nifd->namenode->divert->camefrom) {
716 divpkgset = nifd->namenode->divert->pkgset;
718 if (divpkgset) {
719 forcibleerr(FORCE_OVERWRITE_DIVERTED,
720 _("trying to overwrite '%.250s', which is the "
721 "diverted version of '%.250s' (package: %.100s)"),
722 nifd->namenode->name, nifd->namenode->divert->camefrom->name,
723 divpkgset->name);
724 } else {
725 forcibleerr(FORCE_OVERWRITE_DIVERTED,
726 _("trying to overwrite '%.250s', which is the "
727 "diverted version of '%.250s'"),
728 nifd->namenode->name, nifd->namenode->divert->camefrom->name);
732 if (nifd->namenode->statoverride) {
733 nodestat = *nifd->namenode->statoverride;
734 nodestat.mode |= ti->stat.mode & S_IFMT;
735 } else {
736 nodestat = ti->stat;
739 usenode = namenodetouse(nifd->namenode, tc->pkg, &tc->pkg->available);
740 usename = usenode->name;
742 trig_file_activate(usenode, tc->pkg);
744 if (nifd->namenode->flags & FNNF_NEW_CONFF) {
745 /* If it's a conffile we have to extract it next to the installed
746 * version (i.e. we do the usual link-following). */
747 if (conffderef(tc->pkg, &conffderefn, usename))
748 usename= conffderefn.buf;
749 debug(dbg_conff, "tarobject FNNF_NEW_CONFF deref='%s'", usename);
752 setupfnamevbs(usename);
754 statr= lstat(fnamevb.buf,&stab);
755 if (statr) {
756 /* The lstat failed. */
757 if (errno != ENOENT && errno != ENOTDIR)
758 ohshite(_("unable to stat '%.255s' (which was about to be installed)"),
759 ti->name);
760 /* OK, so it doesn't exist.
761 * However, it's possible that we were in the middle of some other
762 * backup/restore operation and were rudely interrupted.
763 * So, we see if we have .dpkg-tmp, and if so we restore it. */
764 if (rename(fnametmpvb.buf,fnamevb.buf)) {
765 if (errno != ENOENT && errno != ENOTDIR)
766 ohshite(_("unable to clean up mess surrounding '%.255s' before "
767 "installing another version"), ti->name);
768 debug(dbg_eachfiledetail,"tarobject nonexistent");
769 } else {
770 debug(dbg_eachfiledetail,"tarobject restored tmp to main");
771 statr= lstat(fnamevb.buf,&stab);
772 if (statr)
773 ohshite(_("unable to stat restored '%.255s' before installing"
774 " another version"), ti->name);
776 } else {
777 debug(dbg_eachfiledetail,"tarobject already exists");
780 /* Check to see if it's a directory or link to one and we don't need to
781 * do anything. This has to be done now so that we don't die due to
782 * a file overwriting conflict. */
783 existingdir = false;
784 switch (ti->type) {
785 case TAR_FILETYPE_SYMLINK:
786 /* If it's already an existing directory, do nothing. */
787 if (!statr && S_ISDIR(stab.st_mode)) {
788 debug(dbg_eachfiledetail, "tarobject symlink exists as directory");
789 existingdir = true;
790 } else if (!statr && S_ISLNK(stab.st_mode)) {
791 if (linktosameexistingdir(ti, fnamevb.buf, &symlinkfn))
792 existingdir = true;
794 break;
795 case TAR_FILETYPE_DIR:
796 /* If it's already an existing directory, do nothing. */
797 if (!stat(fnamevb.buf,&stabtmp) && S_ISDIR(stabtmp.st_mode)) {
798 debug(dbg_eachfiledetail, "tarobject directory exists");
799 existingdir = true;
801 break;
802 case TAR_FILETYPE_FILE:
803 case TAR_FILETYPE_CHARDEV:
804 case TAR_FILETYPE_BLOCKDEV:
805 case TAR_FILETYPE_FIFO:
806 case TAR_FILETYPE_HARDLINK:
807 break;
808 default:
809 ohshit(_("archive contained object '%.255s' of unknown type 0x%x"),
810 ti->name, ti->type);
813 keepexisting = false;
814 refcounting = false;
815 if (!existingdir) {
816 struct fsys_node_pkgs_iter *iter;
818 iter = fsys_node_pkgs_iter_new(nifd->namenode);
819 while ((otherpkg = fsys_node_pkgs_iter_next(iter))) {
820 if (otherpkg == tc->pkg)
821 continue;
822 debug(dbg_eachfile, "tarobject ... found in %s",
823 pkg_name(otherpkg, pnaw_always));
825 /* A pkgset can share files between its instances. Overwriting
826 * is allowed when they are not getting in sync, otherwise the
827 * file content must match the installed file. */
828 if (otherpkg->set == tc->pkg->set &&
829 otherpkg->installed.multiarch == PKG_MULTIARCH_SAME &&
830 tc->pkg->available.multiarch == PKG_MULTIARCH_SAME) {
831 if (statr == 0 && tc->pkgset_getting_in_sync)
832 refcounting = true;
833 debug(dbg_eachfiledetail, "tarobject ... shared with %s %s (syncing=%d)",
834 pkg_name(otherpkg, pnaw_always),
835 versiondescribe_c(&otherpkg->installed.version, vdew_nonambig),
836 tc->pkgset_getting_in_sync);
837 continue;
840 if (nifd->namenode->divert && nifd->namenode->divert->useinstead) {
841 /* Right, so we may be diverting this file. This makes the conflict
842 * OK iff one of us is the diverting package (we don't need to
843 * check for both being the diverting package, obviously). */
844 divpkgset = nifd->namenode->divert->pkgset;
845 debug(dbg_eachfile, "tarobject ... diverted, divpkgset=%s",
846 divpkgset ? divpkgset->name : "<none>");
847 if (otherpkg->set == divpkgset || tc->pkg->set == divpkgset)
848 continue;
851 /* If the new object is a directory and the previous object does
852 * not exist assume it's also a directory and skip further checks.
853 * XXX: Ideally with more information about the installed files we
854 * could perform more clever checks. */
855 if (statr != 0 && ti->type == TAR_FILETYPE_DIR) {
856 debug(dbg_eachfile, "tarobject ... assuming shared directory");
857 continue;
860 ensure_package_clientdata(otherpkg);
862 /* Nope? Hmm, file conflict, perhaps. Check Replaces. */
863 switch (otherpkg->clientdata->replacingfilesandsaid) {
864 case 2:
865 keepexisting = true;
866 /* Fall through. */
867 case 1:
868 continue;
871 /* Is the package with the conflicting file in the “config files only”
872 * state? If so it must be a config file and we can silently take it
873 * over. */
874 if (otherpkg->status == PKG_STAT_CONFIGFILES)
875 continue;
877 /* Perhaps we're removing a conflicting package? */
878 if (otherpkg->clientdata->istobe == PKG_ISTOBE_REMOVE)
879 continue;
881 /* Is the file an obsolete conffile in the other package
882 * and a conffile in the new package? */
883 if ((nifd->namenode->flags & FNNF_NEW_CONFF) &&
884 !statr && S_ISREG(stab.st_mode)) {
885 for (conff = otherpkg->installed.conffiles;
886 conff;
887 conff = conff->next) {
888 if (!conff->obsolete)
889 continue;
890 if (strcmp(conff->name, nifd->namenode->name) == 0)
891 break;
893 if (conff) {
894 debug(dbg_eachfiledetail, "tarobject other's obsolete conffile");
895 /* process_archive() will have copied its hash already. */
896 continue;
900 if (does_replace(tc->pkg, &tc->pkg->available,
901 otherpkg, &otherpkg->installed)) {
902 printf(_("Replacing files in old package %s (%s) ...\n"),
903 pkg_name(otherpkg, pnaw_nonambig),
904 versiondescribe(&otherpkg->installed.version, vdew_nonambig));
905 otherpkg->clientdata->replacingfilesandsaid = 1;
906 } else if (does_replace(otherpkg, &otherpkg->installed,
907 tc->pkg, &tc->pkg->available)) {
908 printf(_("Replaced by files in installed package %s (%s) ...\n"),
909 pkg_name(otherpkg, pnaw_nonambig),
910 versiondescribe(&otherpkg->installed.version, vdew_nonambig));
911 otherpkg->clientdata->replacingfilesandsaid = 2;
912 nifd->namenode->flags &= ~FNNF_NEW_INARCHIVE;
913 keepexisting = true;
914 } else {
915 /* At this point we are replacing something without a Replaces. */
916 if (!statr && S_ISDIR(stab.st_mode)) {
917 forcibleerr(FORCE_OVERWRITE_DIR,
918 _("trying to overwrite directory '%.250s' "
919 "in package %.250s %.250s with nondirectory"),
920 nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
921 versiondescribe(&otherpkg->installed.version,
922 vdew_nonambig));
923 } else {
924 forcibleerr(FORCE_OVERWRITE,
925 _("trying to overwrite '%.250s', "
926 "which is also in package %.250s %.250s"),
927 nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
928 versiondescribe(&otherpkg->installed.version,
929 vdew_nonambig));
933 fsys_node_pkgs_iter_free(iter);
936 if (keepexisting) {
937 if (nifd->namenode->flags & FNNF_NEW_CONFF)
938 nifd->namenode->flags |= FNNF_OBS_CONFF;
939 tar_fsys_namenode_queue_pop(tc->newfiles_queue, oldnifd, nifd);
940 tarobject_skip_entry(tc, ti);
941 return 0;
944 if (filter_should_skip(ti)) {
945 nifd->namenode->flags &= ~FNNF_NEW_INARCHIVE;
946 nifd->namenode->flags |= FNNF_FILTERED;
947 tarobject_skip_entry(tc, ti);
949 return 0;
952 if (existingdir)
953 return 0;
955 /* Compute the hash of the previous object, before we might replace it
956 * with the new version on forced overwrites. */
957 if (refcounting) {
958 debug(dbg_eachfiledetail, "tarobject computing on-disk file '%s' digest, refcounting",
959 fnamevb.buf);
960 if (nifd->namenode->flags & FNNF_NEW_CONFF) {
961 md5hash_prev_conffile(tc->pkg, oldhash, fnamenewvb.buf, nifd->namenode);
962 } else if (S_ISREG(stab.st_mode)) {
963 md5hash(tc->pkg, oldhash, fnamevb.buf);
964 } else {
965 strcpy(oldhash, EMPTYHASHFLAG);
969 if (refcounting && !in_force(FORCE_OVERWRITE)) {
970 /* If we are not forced to overwrite the path and are refcounting,
971 * just compute the hash w/o extracting the object. */
972 tarobject_hash(tc, ti, nifd->namenode);
973 } else {
974 /* Now, at this stage we want to make sure neither of .dpkg-new and
975 * .dpkg-tmp are hanging around. */
976 path_remove_tree(fnamenewvb.buf);
977 path_remove_tree(fnametmpvb.buf);
979 /* Now we start to do things that we need to be able to undo
980 * if something goes wrong. Watch out for the CLEANUP comments to
981 * keep an eye on what's installed on the disk at each point. */
982 push_cleanup(cu_installnew, ~ehflag_normaltidy, 1, nifd->namenode);
985 * CLEANUP: Now we either have the old file on the disk, or not, in
986 * its original filename.
989 /* Extract whatever it is as .dpkg-new ... */
990 tarobject_extract(tc, ti, fnamenewvb.buf, &nodestat, nifd->namenode);
993 /* For shared files, check now if the object matches. */
994 if (refcounting)
995 tarobject_matches(tc, fnamevb.buf, &stab, oldhash,
996 fnamenewvb.buf, ti, nifd->namenode);
998 /* If we didn't extract anything, there's nothing else to do. */
999 if (refcounting && !in_force(FORCE_OVERWRITE))
1000 return 0;
1002 tarobject_set_perms(ti, fnamenewvb.buf, &nodestat);
1003 tarobject_set_mtime(ti, fnamenewvb.buf);
1004 tarobject_set_se_context(fnamevb.buf, fnamenewvb.buf, nodestat.mode);
1007 * CLEANUP: Now we have extracted the new object in .dpkg-new (or,
1008 * if the file already exists as a directory and we were trying to
1009 * extract a directory or symlink, we returned earlier, so we don't
1010 * need to worry about that here).
1012 * The old file is still in the original filename,
1015 /* First, check to see if it's a conffile. If so we don't install
1016 * it now - we leave it in .dpkg-new for --configure to take care of. */
1017 if (nifd->namenode->flags & FNNF_NEW_CONFF) {
1018 debug(dbg_conffdetail,"tarobject conffile extracted");
1019 nifd->namenode->flags |= FNNF_ELIDE_OTHER_LISTS;
1020 return 0;
1023 /* Now we move the old file out of the way, the backup file will
1024 * be deleted later. */
1025 if (statr) {
1026 /* Don't try to back it up if it didn't exist. */
1027 debug(dbg_eachfiledetail,"tarobject new - no backup");
1028 } else {
1029 if (ti->type == TAR_FILETYPE_DIR || S_ISDIR(stab.st_mode)) {
1030 /* One of the two is a directory - can't do atomic install. */
1031 debug(dbg_eachfiledetail,"tarobject directory, nonatomic");
1032 nifd->namenode->flags |= FNNF_NO_ATOMIC_OVERWRITE;
1033 if (rename(fnamevb.buf,fnametmpvb.buf))
1034 ohshite(_("unable to move aside '%.255s' to install new version"),
1035 ti->name);
1036 } else if (S_ISLNK(stab.st_mode)) {
1037 int rc;
1039 /* We can't make a symlink with two hardlinks, so we'll have to
1040 * copy it. (Pretend that making a copy of a symlink is the same
1041 * as linking to it.) */
1042 varbuf_reset(&symlinkfn);
1043 varbuf_grow(&symlinkfn, stab.st_size + 1);
1044 r = readlink(fnamevb.buf, symlinkfn.buf, symlinkfn.size);
1045 if (r < 0)
1046 ohshite(_("unable to read link '%.255s'"), ti->name);
1047 else if (r > stab.st_size)
1048 ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
1049 fnamevb.buf, (intmax_t)stab.st_size, r);
1050 else if (r < stab.st_size)
1051 warning(_("symbolic link '%.250s' size has changed from %jd to %zd"),
1052 fnamevb.buf, (intmax_t)stab.st_size, r);
1053 varbuf_trunc(&symlinkfn, r);
1054 varbuf_end_str(&symlinkfn);
1055 if (symlink(symlinkfn.buf,fnametmpvb.buf))
1056 ohshite(_("unable to make backup symlink for '%.255s'"), ti->name);
1057 rc = lchown(fnametmpvb.buf, stab.st_uid, stab.st_gid);
1058 if (forcible_nonroot_error(rc))
1059 ohshite(_("unable to chown backup symlink for '%.255s'"), ti->name);
1060 tarobject_set_se_context(fnamevb.buf, fnametmpvb.buf, stab.st_mode);
1061 } else {
1062 debug(dbg_eachfiledetail, "tarobject nondirectory, 'link' backup");
1063 if (link(fnamevb.buf,fnametmpvb.buf))
1064 ohshite(_("unable to make backup link of '%.255s' before installing new version"),
1065 ti->name);
1070 * CLEANUP: Now the old file is in .dpkg-tmp, and the new file is still
1071 * in .dpkg-new.
1074 if (ti->type == TAR_FILETYPE_FILE || ti->type == TAR_FILETYPE_HARDLINK ||
1075 ti->type == TAR_FILETYPE_SYMLINK) {
1076 nifd->namenode->flags |= FNNF_DEFERRED_RENAME;
1078 debug(dbg_eachfiledetail, "tarobject done and installation deferred");
1079 } else {
1080 if (rename(fnamenewvb.buf, fnamevb.buf))
1081 ohshite(_("unable to install new version of '%.255s'"), ti->name);
1084 * CLEANUP: Now the new file is in the destination file, and the
1085 * old file is in .dpkg-tmp to be cleaned up later. We now need
1086 * to take a different attitude to cleanup, because we need to
1087 * remove the new file.
1090 nifd->namenode->flags |= FNNF_PLACED_ON_DISK;
1091 nifd->namenode->flags |= FNNF_ELIDE_OTHER_LISTS;
1093 debug(dbg_eachfiledetail, "tarobject done and installed");
1096 return 0;
1099 #if defined(SYNC_FILE_RANGE_WAIT_BEFORE)
1100 static void
1101 tar_writeback_barrier(struct fsys_namenode_list *files, struct pkginfo *pkg)
1103 struct fsys_namenode_list *cfile;
1105 for (cfile = files; cfile; cfile = cfile->next) {
1106 struct fsys_namenode *usenode;
1107 int fd;
1109 if (!(cfile->namenode->flags & FNNF_DEFERRED_FSYNC))
1110 continue;
1112 usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
1114 setupfnamevbs(usenode->name);
1116 fd = open(fnamenewvb.buf, O_WRONLY);
1117 if (fd < 0)
1118 ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
1119 /* Ignore the return code as it should be considered equivalent to an
1120 * asynchronous hint for the kernel, we are doing an fsync() later on
1121 * anyway. */
1122 sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WAIT_BEFORE);
1123 if (close(fd))
1124 ohshite(_("error closing/writing '%.255s'"), fnamenewvb.buf);
1127 #else
1128 static void
1129 tar_writeback_barrier(struct fsys_namenode_list *files, struct pkginfo *pkg)
1132 #endif
1134 void
1135 tar_deferred_extract(struct fsys_namenode_list *files, struct pkginfo *pkg)
1137 struct fsys_namenode_list *cfile;
1138 struct fsys_namenode *usenode;
1140 tar_writeback_barrier(files, pkg);
1142 for (cfile = files; cfile; cfile = cfile->next) {
1143 debug(dbg_eachfile, "deferred extract of '%.255s'", cfile->namenode->name);
1145 if (!(cfile->namenode->flags & FNNF_DEFERRED_RENAME))
1146 continue;
1148 usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
1150 setupfnamevbs(usenode->name);
1152 if (cfile->namenode->flags & FNNF_DEFERRED_FSYNC) {
1153 int fd;
1155 debug(dbg_eachfiledetail, "deferred extract needs fsync");
1157 fd = open(fnamenewvb.buf, O_WRONLY);
1158 if (fd < 0)
1159 ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
1160 if (fsync(fd))
1161 ohshite(_("unable to sync file '%.255s'"), fnamenewvb.buf);
1162 if (close(fd))
1163 ohshite(_("error closing/writing '%.255s'"), fnamenewvb.buf);
1165 cfile->namenode->flags &= ~FNNF_DEFERRED_FSYNC;
1168 debug(dbg_eachfiledetail, "deferred extract needs rename");
1170 if (rename(fnamenewvb.buf, fnamevb.buf))
1171 ohshite(_("unable to install new version of '%.255s'"),
1172 cfile->namenode->name);
1174 cfile->namenode->flags &= ~FNNF_DEFERRED_RENAME;
1177 * CLEANUP: Now the new file is in the destination file, and the
1178 * old file is in .dpkg-tmp to be cleaned up later. We now need
1179 * to take a different attitude to cleanup, because we need to
1180 * remove the new file.
1183 cfile->namenode->flags |= FNNF_PLACED_ON_DISK;
1184 cfile->namenode->flags |= FNNF_ELIDE_OTHER_LISTS;
1186 debug(dbg_eachfiledetail, "deferred extract done and installed");
1190 void
1191 enqueue_deconfigure(struct pkginfo *pkg, struct pkginfo *pkg_removal,
1192 enum pkgwant reason)
1194 struct pkg_deconf_list *newdeconf;
1196 ensure_package_clientdata(pkg);
1197 pkg->clientdata->istobe = PKG_ISTOBE_DECONFIGURE;
1198 newdeconf = m_malloc(sizeof(*newdeconf));
1199 newdeconf->next = deconfigure;
1200 newdeconf->pkg = pkg;
1201 newdeconf->pkg_removal = pkg_removal;
1202 newdeconf->reason = reason;
1203 deconfigure = newdeconf;
1206 void
1207 clear_deconfigure_queue(void)
1209 struct pkg_deconf_list *deconf, *deconf_next;
1211 for (deconf = deconfigure; deconf; deconf = deconf_next) {
1212 deconf_next = deconf->next;
1213 free(deconf);
1215 deconfigure = NULL;
1219 * Try if we can deconfigure the package for installation and queue it if so.
1221 * This function gets called in the Breaks context, when trying to install
1222 * a package that might require another to be deconfigured to be able to
1223 * proceed.
1225 * First checks whether the pdep is forced.
1227 * @retval 0 Not possible (why is printed).
1228 * @retval 1 Deconfiguration queued ok (no message printed).
1229 * @retval 2 Forced (no deconfiguration needed, why is printed).
1231 static int
1232 try_deconfigure_can(struct pkginfo *pkg, struct deppossi *pdep,
1233 struct pkginfo *pkg_install, const char *why)
1235 if (force_breaks(pdep)) {
1236 warning(_("ignoring dependency problem with installation of %s:\n%s"),
1237 pkgbin_name(pkg_install, &pkg->available, pnaw_nonambig), why);
1238 return 2;
1239 } else if (f_autodeconf) {
1240 enqueue_deconfigure(pkg, NULL, PKG_WANT_INSTALL);
1241 return 1;
1242 } else {
1243 notice(_("no, cannot proceed with installation of %s (--auto-deconfigure will help):\n%s"),
1244 pkgbin_name(pkg_install, &pkg->available, pnaw_nonambig), why);
1245 return 0;
1250 * Try if we can deconfigure the package for removal and queue it if so.
1252 * This function gets called in the Conflicts+Depends context, when trying
1253 * to install a package that might require another to be fully removed to
1254 * be able to proceed.
1256 * First checks whether the pdep is forced, then if auto-configure is enabled
1257 * we make sure Essential and Protected are not allowed to be removed unless
1258 * forced.
1260 * @retval 0 Not possible (why is printed).
1261 * @retval 1 Deconfiguration queued ok (no message printed).
1262 * @retval 2 Forced (no deconfiguration needed, why is printed).
1264 static int
1265 try_remove_can(struct deppossi *pdep,
1266 struct pkginfo *pkg_removal, const char *why)
1268 struct pkginfo *pkg = pdep->up->up;
1270 if (force_depends(pdep)) {
1271 warning(_("ignoring dependency problem with removal of %s:\n%s"),
1272 pkg_name(pkg_removal, pnaw_nonambig), why);
1273 return 2;
1274 } else if (f_autodeconf) {
1275 if (pkg->installed.essential) {
1276 if (in_force(FORCE_REMOVE_ESSENTIAL)) {
1277 warning(_("considering deconfiguration of essential\n"
1278 " package %s, to enable removal of %s"),
1279 pkg_name(pkg, pnaw_nonambig), pkg_name(pkg_removal, pnaw_nonambig));
1280 } else {
1281 notice(_("no, %s is essential, will not deconfigure\n"
1282 " it in order to enable removal of %s"),
1283 pkg_name(pkg, pnaw_nonambig), pkg_name(pkg_removal, pnaw_nonambig));
1284 return 0;
1287 if (pkg->installed.is_protected) {
1288 if (in_force(FORCE_REMOVE_PROTECTED)) {
1289 warning(_("considering deconfiguration of protected\n"
1290 " package %s, to enable removal of %s"),
1291 pkg_name(pkg, pnaw_nonambig), pkg_name(pkg_removal, pnaw_nonambig));
1292 } else {
1293 notice(_("no, %s is protected, will not deconfigure\n"
1294 " it in order to enable removal of %s"),
1295 pkg_name(pkg, pnaw_nonambig), pkg_name(pkg_removal, pnaw_nonambig));
1296 return 0;
1300 enqueue_deconfigure(pkg, pkg_removal, PKG_WANT_DEINSTALL);
1301 return 1;
1302 } else {
1303 notice(_("no, cannot proceed with removal of %s (--auto-deconfigure will help):\n%s"),
1304 pkg_name(pkg_removal, pnaw_nonambig), why);
1305 return 0;
1309 void check_breaks(struct dependency *dep, struct pkginfo *pkg,
1310 const char *pfilename) {
1311 struct pkginfo *fixbydeconf;
1312 struct varbuf why = VARBUF_INIT;
1313 int ok;
1315 fixbydeconf = NULL;
1316 if (depisok(dep, &why, &fixbydeconf, NULL, false)) {
1317 varbuf_destroy(&why);
1318 return;
1321 varbuf_end_str(&why);
1323 if (fixbydeconf && f_autodeconf) {
1324 ensure_package_clientdata(fixbydeconf);
1326 if (fixbydeconf->clientdata->istobe != PKG_ISTOBE_NORMAL)
1327 internerr("package %s being fixed by deconf is not to be normal, "
1328 "is to be %d",
1329 pkg_name(pkg, pnaw_always), fixbydeconf->clientdata->istobe);
1331 notice(_("considering deconfiguration of %s, which would be broken by installation of %s ..."),
1332 pkg_name(fixbydeconf, pnaw_nonambig),
1333 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1335 ok = try_deconfigure_can(fixbydeconf, dep->list, pkg, why.buf);
1336 if (ok == 1) {
1337 notice(_("yes, will deconfigure %s (broken by %s)"),
1338 pkg_name(fixbydeconf, pnaw_nonambig),
1339 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1341 } else {
1342 notice(_("regarding %s containing %s:\n%s"), pfilename,
1343 pkgbin_name(pkg, &pkg->available, pnaw_nonambig), why.buf);
1344 ok= 0;
1346 varbuf_destroy(&why);
1347 if (ok > 0) return;
1349 if (force_breaks(dep->list)) {
1350 warning(_("ignoring breakage, may proceed anyway!"));
1351 return;
1354 if (fixbydeconf && !f_autodeconf) {
1355 ohshit(_("installing %.250s would break %.250s, and\n"
1356 " deconfiguration is not permitted (--auto-deconfigure might help)"),
1357 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
1358 pkg_name(fixbydeconf, pnaw_nonambig));
1359 } else {
1360 ohshit(_("installing %.250s would break existing software"),
1361 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1365 void check_conflict(struct dependency *dep, struct pkginfo *pkg,
1366 const char *pfilename) {
1367 struct pkginfo *fixbyrm;
1368 struct deppossi *pdep, flagdeppossi = { 0 };
1369 struct varbuf conflictwhy = VARBUF_INIT, removalwhy = VARBUF_INIT;
1370 struct dependency *providecheck;
1372 fixbyrm = NULL;
1373 if (depisok(dep, &conflictwhy, &fixbyrm, NULL, false)) {
1374 varbuf_destroy(&conflictwhy);
1375 varbuf_destroy(&removalwhy);
1376 return;
1378 if (fixbyrm) {
1379 ensure_package_clientdata(fixbyrm);
1380 if (fixbyrm->clientdata->istobe == PKG_ISTOBE_INSTALLNEW) {
1381 fixbyrm= dep->up;
1382 ensure_package_clientdata(fixbyrm);
1384 if (((pkg->available.essential || pkg->available.is_protected) &&
1385 (fixbyrm->installed.essential || fixbyrm->installed.is_protected)) ||
1386 (((fixbyrm->want != PKG_WANT_INSTALL &&
1387 fixbyrm->want != PKG_WANT_HOLD) ||
1388 does_replace(pkg, &pkg->available, fixbyrm, &fixbyrm->installed)) &&
1389 ((!fixbyrm->installed.essential || in_force(FORCE_REMOVE_ESSENTIAL)) ||
1390 (!fixbyrm->installed.is_protected || in_force(FORCE_REMOVE_PROTECTED))))) {
1391 if (fixbyrm->clientdata->istobe != PKG_ISTOBE_NORMAL &&
1392 fixbyrm->clientdata->istobe != PKG_ISTOBE_DECONFIGURE)
1393 internerr("package %s to be fixed by removal is not to be normal "
1394 "nor deconfigure, is to be %d",
1395 pkg_name(pkg, pnaw_always), fixbyrm->clientdata->istobe);
1396 fixbyrm->clientdata->istobe = PKG_ISTOBE_REMOVE;
1397 notice(_("considering removing %s in favour of %s ..."),
1398 pkg_name(fixbyrm, pnaw_nonambig),
1399 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1400 if (!(fixbyrm->status == PKG_STAT_INSTALLED ||
1401 fixbyrm->status == PKG_STAT_TRIGGERSPENDING ||
1402 fixbyrm->status == PKG_STAT_TRIGGERSAWAITED)) {
1403 notice(_("%s is not properly installed; ignoring any dependencies on it"),
1404 pkg_name(fixbyrm, pnaw_nonambig));
1405 pdep = NULL;
1406 } else {
1407 for (pdep = fixbyrm->set->depended.installed;
1408 pdep;
1409 pdep = pdep->rev_next) {
1410 if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
1411 continue;
1412 if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
1413 continue;
1414 varbuf_end_str(&removalwhy);
1415 if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
1416 break;
1418 if (!pdep) {
1419 /* If we haven't found a reason not to yet, let's look some more. */
1420 for (providecheck= fixbyrm->installed.depends;
1421 providecheck;
1422 providecheck= providecheck->next) {
1423 if (providecheck->type != dep_provides) continue;
1424 for (pdep = providecheck->list->ed->depended.installed;
1425 pdep;
1426 pdep = pdep->rev_next) {
1427 if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
1428 continue;
1429 if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
1430 continue;
1431 varbuf_end_str(&removalwhy);
1432 notice(_("may have trouble removing %s, as it provides %s ..."),
1433 pkg_name(fixbyrm, pnaw_nonambig),
1434 providecheck->list->ed->name);
1435 if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
1436 goto break_from_both_loops_at_once;
1439 break_from_both_loops_at_once:;
1442 if (!pdep && skip_due_to_hold(fixbyrm)) {
1443 pdep= &flagdeppossi;
1445 if (!pdep && (fixbyrm->eflag & PKG_EFLAG_REINSTREQ)) {
1446 if (in_force(FORCE_REMOVE_REINSTREQ)) {
1447 notice(_("package %s requires reinstallation, but will "
1448 "remove anyway as you requested"),
1449 pkg_name(fixbyrm, pnaw_nonambig));
1450 } else {
1451 notice(_("package %s requires reinstallation, will not remove"),
1452 pkg_name(fixbyrm, pnaw_nonambig));
1453 pdep= &flagdeppossi;
1456 if (!pdep) {
1457 /* This conflict is OK - we'll remove the conflictor. */
1458 enqueue_conflictor(fixbyrm);
1459 varbuf_destroy(&conflictwhy); varbuf_destroy(&removalwhy);
1460 notice(_("yes, will remove %s in favour of %s"),
1461 pkg_name(fixbyrm, pnaw_nonambig),
1462 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1463 return;
1465 /* Put it back. */
1466 fixbyrm->clientdata->istobe = PKG_ISTOBE_NORMAL;
1469 varbuf_end_str(&conflictwhy);
1470 notice(_("regarding %s containing %s:\n%s"), pfilename,
1471 pkgbin_name(pkg, &pkg->available, pnaw_nonambig), conflictwhy.buf);
1472 if (!force_conflicts(dep->list))
1473 ohshit(_("conflicting packages - not installing %.250s"),
1474 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1475 warning(_("ignoring conflict, may proceed anyway!"));
1476 varbuf_destroy(&conflictwhy);
1478 return;
1481 void cu_cidir(int argc, void **argv) {
1482 char *cidir= (char*)argv[0];
1483 char *cidirrest= (char*)argv[1];
1484 cidirrest[-1] = '\0';
1485 path_remove_tree(cidir);
1486 free(cidir);
1489 void cu_fileslist(int argc, void **argv) {
1490 tar_pool_release();
1494 archivefiles(const char *const *argv)
1496 const char *const *volatile argp;
1497 char **volatile arglist = NULL;
1498 int i;
1499 jmp_buf ejbuf;
1500 enum modstatdb_rw msdbflags;
1502 trigproc_install_hooks();
1504 if (f_noact)
1505 msdbflags = msdbrw_readonly;
1506 else if (cipaction->arg_int == act_avail)
1507 msdbflags = msdbrw_readonly | msdbrw_available_write;
1508 else if (in_force(FORCE_NON_ROOT))
1509 msdbflags = msdbrw_write;
1510 else
1511 msdbflags = msdbrw_needsuperuser;
1513 modstatdb_open(msdbflags);
1515 checkpath();
1516 pkg_infodb_upgrade();
1518 log_message("startup archives %s", cipaction->olong);
1520 if (f_recursive) {
1521 const char *const *ap;
1522 int nfiles = 0;
1524 if (!*argv)
1525 badusage(_("--%s --recursive needs at least one path argument"),cipaction->olong);
1527 for (ap = argv; *ap; ap++) {
1528 struct treeroot *tree;
1529 struct treenode *node;
1531 tree = treewalk_open((const char *)*ap, TREEWALK_FOLLOW_LINKS, NULL);
1533 while ((node = treewalk_next(tree))) {
1534 const char *nodename;
1536 if (!S_ISREG(treenode_get_mode(node)))
1537 continue;
1539 /* Check if it looks like a .deb file. */
1540 nodename = treenode_get_pathname(node);
1541 if (strcmp(nodename + strlen(nodename) - 4, ".deb") != 0)
1542 continue;
1544 arglist = m_realloc(arglist, sizeof(char *) * (nfiles + 2));
1545 arglist[nfiles++] = m_strdup(nodename);
1548 treewalk_close(tree);
1551 if (!nfiles)
1552 ohshit(_("searched, but found no packages (files matching *.deb)"));
1554 arglist[nfiles] = NULL;
1555 argp = (const char **volatile)arglist;
1556 } else {
1557 if (!*argv) badusage(_("--%s needs at least one package archive file argument"),
1558 cipaction->olong);
1559 argp= argv;
1562 /* Perform some sanity checks on the passed archives. */
1563 for (i = 0; argp[i]; i++) {
1564 struct stat st;
1566 /* We need the filename to exist. */
1567 if (stat(argp[i], &st) < 0)
1568 ohshite(_("cannot access archive '%s'"), argp[i]);
1570 /* We cannot work with anything that is not a regular file. */
1571 if (!S_ISREG(st.st_mode))
1572 ohshit(_("archive '%s' is not a regular file"), argp[i]);
1575 currenttime = time(NULL);
1577 /* Initialize fname variables contents. */
1579 varbuf_reset(&fnamevb);
1580 varbuf_reset(&fnametmpvb);
1581 varbuf_reset(&fnamenewvb);
1583 varbuf_add_str(&fnamevb, dpkg_fsys_get_dir());
1584 varbuf_add_str(&fnametmpvb, dpkg_fsys_get_dir());
1585 varbuf_add_str(&fnamenewvb, dpkg_fsys_get_dir());
1587 varbuf_snapshot(&fnamevb, &fname_state);
1588 varbuf_snapshot(&fnametmpvb, &fnametmp_state);
1589 varbuf_snapshot(&fnamenewvb, &fnamenew_state);
1591 ensure_diversions();
1592 ensure_statoverrides(STATDB_PARSE_NORMAL);
1594 for (i = 0; argp[i]; i++) {
1595 if (setjmp(ejbuf)) {
1596 pop_error_context(ehflag_bombout);
1597 if (abort_processing)
1598 break;
1599 continue;
1601 push_error_context_jump(&ejbuf, print_error_perarchive, argp[i]);
1603 dpkg_selabel_load();
1605 process_archive(argp[i]);
1606 onerr_abort++;
1607 m_output(stdout, _("<standard output>"));
1608 m_output(stderr, _("<standard error>"));
1609 onerr_abort--;
1611 pop_error_context(ehflag_normaltidy);
1614 dpkg_selabel_close();
1616 if (arglist) {
1617 for (i = 0; arglist[i]; i++)
1618 free(arglist[i]);
1619 free(arglist);
1622 switch (cipaction->arg_int) {
1623 case act_install:
1624 case act_configure:
1625 case act_triggers:
1626 case act_remove:
1627 case act_purge:
1628 process_queue();
1629 case act_unpack:
1630 case act_avail:
1631 break;
1632 default:
1633 internerr("unknown action '%d'", cipaction->arg_int);
1636 trigproc_run_deferred();
1637 modstatdb_shutdown();
1639 return 0;
1643 * Decide whether we want to install a new version of the package.
1645 * @param pkg The package with the version we might want to install
1647 * @retval true If the package should be skipped.
1648 * @retval false If the package should be installed.
1650 bool
1651 wanttoinstall(struct pkginfo *pkg)
1653 int rc;
1655 if (pkg->want != PKG_WANT_INSTALL && pkg->want != PKG_WANT_HOLD) {
1656 if (f_alsoselect) {
1657 printf(_("Selecting previously unselected package %s.\n"),
1658 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1659 return true;
1660 } else {
1661 printf(_("Skipping unselected package %s.\n"),
1662 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1663 return false;
1667 if (pkg->eflag & PKG_EFLAG_REINSTREQ)
1668 return true;
1669 if (pkg->status < PKG_STAT_UNPACKED)
1670 return true;
1672 rc = dpkg_version_compare(&pkg->available.version, &pkg->installed.version);
1673 if (rc > 0) {
1674 return true;
1675 } else if (rc == 0) {
1676 /* Same version fully installed. */
1677 if (f_skipsame && pkg->available.arch == pkg->installed.arch) {
1678 notice(_("version %.250s of %.250s already installed, skipping"),
1679 versiondescribe(&pkg->installed.version, vdew_nonambig),
1680 pkg_name(pkg, pnaw_nonambig));
1681 return false;
1682 } else {
1683 return true;
1685 } else {
1686 if (in_force(FORCE_DOWNGRADE)) {
1687 warning(_("downgrading %.250s from %.250s to %.250s"),
1688 pkg_name(pkg, pnaw_nonambig),
1689 versiondescribe(&pkg->installed.version, vdew_nonambig),
1690 versiondescribe(&pkg->available.version, vdew_nonambig));
1691 return true;
1692 } else {
1693 notice(_("will not downgrade %.250s from %.250s to %.250s, skipping"),
1694 pkg_name(pkg, pnaw_nonambig),
1695 versiondescribe(&pkg->installed.version, vdew_nonambig),
1696 versiondescribe(&pkg->available.version, vdew_nonambig));
1697 return false;