libdpkg: Deindent an else clause
[dpkg.git] / src / main / unpack.c
blob02c2681d7ecd702897b189193157ea0cd4e5806f
1 /*
2 * dpkg - main program for package management
3 * unpack.c - .deb archive unpacking
5 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2006-2015 Guillem Jover <guillem@debian.org>
7 * Copyright © 2011 Linaro Limited
8 * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
10 * This is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 #include <config.h>
25 #include <compat.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/wait.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <time.h>
34 #include <utime.h>
35 #include <fcntl.h>
36 #include <dirent.h>
37 #include <unistd.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <stdio.h>
42 #include <dpkg/i18n.h>
43 #include <dpkg/c-ctype.h>
44 #include <dpkg/dpkg.h>
45 #include <dpkg/dpkg-db.h>
46 #include <dpkg/pkg.h>
47 #include <dpkg/pkg-queue.h>
48 #include <dpkg/path.h>
49 #include <dpkg/command.h>
50 #include <dpkg/buffer.h>
51 #include <dpkg/subproc.h>
52 #include <dpkg/dir.h>
53 #include <dpkg/tarfn.h>
54 #include <dpkg/options.h>
55 #include <dpkg/db-ctrl.h>
56 #include <dpkg/db-fsys.h>
57 #include <dpkg/triglib.h>
59 #include "file-match.h"
60 #include "main.h"
61 #include "archives.h"
63 static const char *
64 summarize_filename(const char *filename)
66 const char *pfilename;
67 char *pfilenamebuf;
69 for (pfilename = filename;
70 pfilename && strlen(pfilename) > 30 && strchr(pfilename, '/') != NULL;
71 pfilename++)
72 pfilename = strchr(pfilename, '/');
74 if (pfilename && pfilename != filename) {
75 pfilenamebuf = nfmalloc(strlen(pfilename) + 5);
76 sprintf(pfilenamebuf, _(".../%s"), pfilename);
77 pfilename = pfilenamebuf;
78 } else {
79 pfilename = filename;
82 return pfilename;
85 static bool
86 deb_reassemble(const char **filename, const char **pfilename)
88 static char *reasmbuf = NULL;
89 struct stat stab;
90 int status;
91 pid_t pid;
93 if (!reasmbuf)
94 reasmbuf = dpkg_db_get_path(REASSEMBLETMP);
95 if (unlink(reasmbuf) && errno != ENOENT)
96 ohshite(_("error ensuring '%.250s' doesn't exist"), reasmbuf);
98 push_cleanup(cu_pathname, ~0, 1, (void *)reasmbuf);
100 pid = subproc_fork();
101 if (!pid) {
102 execlp(SPLITTER, SPLITTER, "-Qao", reasmbuf, *filename, NULL);
103 ohshite(_("unable to execute %s (%s)"),
104 _("split package reassembly"), SPLITTER);
106 status = subproc_reap(pid, SPLITTER, SUBPROC_RETERROR);
107 switch (status) {
108 case 0:
109 /* It was a part - is it complete? */
110 if (!stat(reasmbuf, &stab)) {
111 /* Yes. */
112 *filename = reasmbuf;
113 *pfilename = _("reassembled package file");
114 break;
115 } else if (errno == ENOENT) {
116 /* No. That's it, we skip it. */
117 return false;
119 case 1:
120 /* No, it wasn't a part. */
121 break;
122 default:
123 ohshit(_("subprocess %s returned error exit status %d"), SPLITTER, status);
126 return true;
129 static void
130 deb_verify(const char *filename)
132 pid_t pid;
134 /* We have to check on every unpack, in case the debsig-verify package
135 * gets installed or removed. */
136 if (!command_in_path(DEBSIGVERIFY))
137 return;
139 printf(_("Authenticating %s ...\n"), filename);
140 fflush(stdout);
141 pid = subproc_fork();
142 if (!pid) {
143 execlp(DEBSIGVERIFY, DEBSIGVERIFY, "-q", filename, NULL);
144 ohshite(_("unable to execute %s (%s)"),
145 _("package signature verification"), DEBSIGVERIFY);
146 } else {
147 int status;
149 status = subproc_reap(pid, "debsig-verify", SUBPROC_NOCHECK);
150 if (!(WIFEXITED(status) && WEXITSTATUS(status) == 0)) {
151 if (!in_force(FORCE_BAD_VERIFY))
152 ohshit(_("verification on package %s failed!"), filename);
153 else
154 notice(_("verification on package %s failed; "
155 "but installing anyway as you requested"), filename);
156 } else {
157 printf(_("passed\n"));
162 static char *
163 get_control_dir(char *cidir)
165 if (f_noact) {
166 char *tmpdir;
168 tmpdir = mkdtemp(path_make_temp_template("dpkg"));
169 if (tmpdir == NULL)
170 ohshite(_("unable to create temporary directory"));
172 cidir = m_realloc(cidir, strlen(tmpdir) + MAXCONTROLFILENAME + 10);
174 strcpy(cidir, tmpdir);
176 free(tmpdir);
177 } else {
178 const char *admindir;
180 admindir = dpkg_db_get_dir();
182 /* The admindir length is always constant on a dpkg execution run. */
183 if (cidir == NULL)
184 cidir = m_malloc(strlen(admindir) + sizeof(CONTROLDIRTMP) +
185 MAXCONTROLFILENAME + 10);
187 /* We want it to be on the same filesystem so that we can
188 * use rename(2) to install the postinst &c. */
189 strcpy(cidir, admindir);
190 strcat(cidir, "/" CONTROLDIRTMP);
192 /* Make sure the control information directory is empty. */
193 path_remove_tree(cidir);
196 strcat(cidir, "/");
198 return cidir;
201 static void
202 pkg_check_depcon(struct pkginfo *pkg, const char *pfilename)
204 struct dependency *dsearch;
205 struct deppossi *psearch;
206 struct pkginfo *fixbytrigaw;
207 static struct varbuf depprobwhy;
209 /* Check if anything is installed that we conflict with, or not installed
210 * that we need. */
211 ensure_package_clientdata(pkg);
212 pkg->clientdata->istobe = PKG_ISTOBE_INSTALLNEW;
214 for (dsearch = pkg->available.depends; dsearch; dsearch = dsearch->next) {
215 switch (dsearch->type) {
216 case dep_conflicts:
217 /* Look for things we conflict with. */
218 check_conflict(dsearch, pkg, pfilename);
219 break;
220 case dep_breaks:
221 /* Look for things we break. */
222 check_breaks(dsearch, pkg, pfilename);
223 break;
224 case dep_provides:
225 /* Look for things that conflict with what we provide. */
226 for (psearch = dsearch->list->ed->depended.installed;
227 psearch;
228 psearch = psearch->rev_next) {
229 if (psearch->up->type != dep_conflicts)
230 continue;
231 check_conflict(psearch->up, pkg, pfilename);
233 break;
234 case dep_suggests:
235 case dep_recommends:
236 case dep_depends:
237 case dep_replaces:
238 case dep_enhances:
239 /* Ignore these here. */
240 break;
241 case dep_predepends:
242 if (!depisok(dsearch, &depprobwhy, NULL, &fixbytrigaw, true)) {
243 if (fixbytrigaw) {
244 while (fixbytrigaw->trigaw.head)
245 trigproc(fixbytrigaw->trigaw.head->pend, TRIGPROC_REQUIRED);
246 } else {
247 varbuf_end_str(&depprobwhy);
248 notice(_("regarding %s containing %s, pre-dependency problem:\n%s"),
249 pfilename, pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
250 depprobwhy.buf);
251 if (!force_depends(dsearch->list))
252 ohshit(_("pre-dependency problem - not installing %.250s"),
253 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
254 warning(_("ignoring pre-dependency problem!"));
260 /* Look for things that conflict with us. */
261 for (psearch = pkg->set->depended.installed; psearch; psearch = psearch->rev_next) {
262 if (psearch->up->type != dep_conflicts)
263 continue;
265 check_conflict(psearch->up, pkg, pfilename);
269 static void
270 pkg_deconfigure_others(struct pkginfo *pkg)
272 struct pkg_deconf_list *deconpil;
274 for (deconpil = deconfigure; deconpil; deconpil = deconpil->next) {
275 struct pkginfo *removing = deconpil->pkg_removal;
277 if (deconpil->reason == PKG_WANT_DEINSTALL) {
278 printf(_("De-configuring %s (%s), to allow removal of %s (%s) ...\n"),
279 pkg_name(deconpil->pkg, pnaw_nonambig),
280 versiondescribe(&deconpil->pkg->installed.version, vdew_nonambig),
281 pkg_name(removing, pnaw_nonambig),
282 versiondescribe(&removing->installed.version, vdew_nonambig));
283 } else if (deconpil->reason == PKG_WANT_INSTALL) {
284 printf(_("De-configuring %s (%s), to allow installation of %s (%s) ...\n"),
285 pkg_name(deconpil->pkg, pnaw_nonambig),
286 versiondescribe(&deconpil->pkg->installed.version, vdew_nonambig),
287 pkg_name(pkg, pnaw_nonambig),
288 versiondescribe(&pkg->available.version, vdew_nonambig));
289 } else {
290 printf(_("De-configuring %s (%s), to allow configuration of %s (%s) ...\n"),
291 pkg_name(deconpil->pkg, pnaw_nonambig),
292 versiondescribe(&deconpil->pkg->installed.version, vdew_nonambig),
293 pkg_name(pkg, pnaw_nonambig),
294 versiondescribe(&pkg->available.version, vdew_nonambig));
297 trig_activate_packageprocessing(deconpil->pkg);
298 pkg_set_status(deconpil->pkg, PKG_STAT_HALFCONFIGURED);
299 modstatdb_note(deconpil->pkg);
301 /* This means that we *either* go and run postinst abort-deconfigure,
302 * *or* queue the package for later configure processing, depending
303 * on which error cleanup route gets taken. */
304 push_cleanup_fallback(cu_prermdeconfigure, ~ehflag_normaltidy,
305 ok_prermdeconfigure, ehflag_normaltidy,
306 3, (void *)deconpil->pkg, (void *)removing, (void *)pkg);
308 if (removing) {
309 maintscript_installed(deconpil->pkg, PRERMFILE, "pre-removal",
310 "deconfigure", "in-favour",
311 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
312 versiondescribe(&pkg->available.version,
313 vdew_nonambig),
314 "removing",
315 pkg_name(removing, pnaw_nonambig),
316 versiondescribe(&removing->installed.version,
317 vdew_nonambig),
318 NULL);
319 } else {
320 maintscript_installed(deconpil->pkg, PRERMFILE, "pre-removal",
321 "deconfigure", "in-favour",
322 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
323 versiondescribe(&pkg->available.version,
324 vdew_nonambig),
325 NULL);
331 * Read the conffiles, and copy the hashes across.
333 static void
334 deb_parse_conffiles(const struct pkginfo *pkg, const char *control_conffiles,
335 struct fsys_namenode_queue *newconffiles)
337 FILE *conff;
338 char conffilenamebuf[MAXCONFFILENAME];
340 conff = fopen(control_conffiles, "r");
341 if (conff == NULL) {
342 if (errno == ENOENT)
343 return;
344 ohshite(_("error trying to open %.250s"), control_conffiles);
347 push_cleanup(cu_closestream, ehflag_bombout, 1, conff);
349 while (fgets(conffilenamebuf, MAXCONFFILENAME - 2, conff)) {
350 struct pkginfo *otherpkg;
351 struct fsys_node_pkgs_iter *iter;
352 struct fsys_namenode *namenode;
353 struct fsys_namenode_list *newconff;
354 struct conffile *searchconff;
355 char *conffilename = conffilenamebuf;
356 char *p;
357 enum fsys_namenode_flags confflags = FNNF_NEW_CONFF;
359 p = conffilename + strlen(conffilename);
360 if (p == conffilename)
361 ohshit(_("conffile file contains an empty line"));
362 if (p[-1] != '\n')
363 ohshit(_("conffile name '%s' is too long, or missing final newline"),
364 conffilename);
365 p = str_rtrim_spaces(conffilename, p);
366 if (p == conffilename)
367 continue;
369 /* Check for conffile flags. */
370 if (conffilename[0] != '/') {
371 char *flag = conffilename;
372 char *flag_end = strchr(flag, ' ');
374 if (flag_end)
375 conffilename = flag_end + 1;
377 /* If no flag separator is found, assume a missing leading slash. */
378 if (flag_end == NULL || (conffilename[0] && conffilename[0] != '/'))
379 ohshit(_("conffile name '%s' is not an absolute pathname"), conffilename);
381 flag_end[0] = '\0';
383 /* Otherwise assume a missing filename after the flag separator. */
384 if (conffilename[0] == '\0')
385 ohshit(_("conffile name missing after flag '%s'"), flag);
387 if (strcmp(flag, "remove-on-upgrade") == 0) {
388 confflags |= FNNF_RM_CONFF_ON_UPGRADE;
389 confflags &= ~FNNF_NEW_CONFF;
390 } else {
391 if (c_isspace(flag[0]))
392 warning(_("line with conffile filename '%s' has leading white spaces"),
393 conffilename);
394 ohshit(_("unknown flag '%s' for conffile '%s'"), flag, conffilename);
398 namenode = fsys_hash_find_node(conffilename, FHFF_NONE);
399 namenode->oldhash = NEWCONFFILEFLAG;
400 newconff = tar_fsys_namenode_queue_push(newconffiles, namenode);
403 * Let's see if any packages have this file.
405 * If they do we check to see if they listed it as a conffile,
406 * and if they did we copy the hash across. Since (for plain
407 * file conffiles, which is the only kind we are supposed to
408 * have) there will only be one package which ‘has’ the file,
409 * this will usually mean we only look in the package which
410 * we are installing now.
412 * The ‘conffiles’ data in the status file is ignored when a
413 * package is not also listed in the file ownership database as
414 * having that file. If several packages are listed as owning
415 * the file we pick one at random.
417 searchconff = NULL;
419 iter = fsys_node_pkgs_iter_new(newconff->namenode);
420 while ((otherpkg = fsys_node_pkgs_iter_next(iter))) {
421 debug(dbg_conffdetail,
422 "process_archive conffile '%s' in package %s - conff ?",
423 newconff->namenode->name, pkg_name(otherpkg, pnaw_always));
424 for (searchconff = otherpkg->installed.conffiles;
425 searchconff && strcmp(newconff->namenode->name, searchconff->name);
426 searchconff = searchconff->next)
427 debug(dbg_conffdetail,
428 "process_archive conffile '%s' in package %s - conff ? not '%s'",
429 newconff->namenode->name, pkg_name(otherpkg, pnaw_always),
430 searchconff->name);
431 if (searchconff) {
432 debug(dbg_conff,
433 "process_archive conffile '%s' package=%s %s hash=%s",
434 newconff->namenode->name, pkg_name(otherpkg, pnaw_always),
435 otherpkg == pkg ? "same" : "different!",
436 searchconff->hash);
437 if (otherpkg == pkg)
438 break;
441 fsys_node_pkgs_iter_free(iter);
443 if (searchconff) {
444 /* We don't copy ‘obsolete’; it's not obsolete in the new package. */
445 newconff->namenode->oldhash = searchconff->hash;
446 } else {
447 debug(dbg_conff, "process_archive conffile '%s' no package, no hash",
448 newconff->namenode->name);
450 newconff->namenode->flags |= confflags;
453 if (ferror(conff))
454 ohshite(_("read error in %.250s"), control_conffiles);
455 pop_cleanup(ehflag_normaltidy); /* conff = fopen() */
456 if (fclose(conff))
457 ohshite(_("error closing %.250s"), control_conffiles);
460 static struct pkg_queue conflictors = PKG_QUEUE_INIT;
462 void
463 enqueue_conflictor(struct pkginfo *pkg)
465 pkg_queue_push(&conflictors, pkg);
468 static void
469 pkg_infodb_remove_file(const char *filename, const char *filetype)
471 if (unlink(filename))
472 ohshite(_("unable to delete control info file '%.250s'"), filename);
474 debug(dbg_scripts, "removal_bulk info unlinked %s", filename);
477 static struct match_node *match_head = NULL;
479 static void
480 pkg_infodb_update_file(const char *filename, const char *filetype)
482 if (strlen(filetype) > MAXCONTROLFILENAME)
483 ohshit(_("old version of package has overly-long info file name starting '%.250s'"),
484 filename);
486 /* We do the list separately. */
487 if (strcmp(filetype, LISTFILE) == 0)
488 return;
490 /* We keep files to rename in a list as doing the rename immediately
491 * might influence the current readdir(), the just renamed file might
492 * be returned a second time as it's actually a new file from the
493 * point of view of the filesystem. */
494 match_head = match_node_new(filename, filetype, match_head);
497 static void
498 pkg_infodb_update(struct pkginfo *pkg, char *cidir, char *cidirrest)
500 struct match_node *match_node;
501 DIR *dsd;
502 struct dirent *de;
504 /* Deallocate the match list in case we aborted previously. */
505 while ((match_node = match_head)) {
506 match_head = match_node->next;
507 match_node_free(match_node);
510 pkg_infodb_foreach(pkg, &pkg->available, pkg_infodb_update_file);
512 while ((match_node = match_head)) {
513 strcpy(cidirrest, match_node->filetype);
515 if (!rename(cidir, match_node->filename)) {
516 debug(dbg_scripts, "process_archive info installed %s as %s",
517 cidir, match_node->filename);
518 } else if (errno == ENOENT) {
519 /* Right, no new version. */
520 if (unlink(match_node->filename))
521 ohshite(_("unable to remove obsolete info file '%.250s'"),
522 match_node->filename);
523 debug(dbg_scripts, "process_archive info unlinked %s",
524 match_node->filename);
525 } else {
526 ohshite(_("unable to install (supposed) new info file '%.250s'"), cidir);
528 match_head = match_node->next;
529 match_node_free(match_node);
532 /* The control directory itself. */
533 cidirrest[0] = '\0';
534 dsd = opendir(cidir);
535 if (!dsd)
536 ohshite(_("unable to open temp control directory"));
537 push_cleanup(cu_closedir, ~0, 1, (void *)dsd);
538 while ((de = readdir(dsd))) {
539 const char *newinfofilename;
541 if (strchr(de->d_name, '.')) {
542 debug(dbg_scripts, "process_archive tmp.ci script/file '%s' contains dot",
543 de->d_name);
544 continue;
546 if (strlen(de->d_name) > MAXCONTROLFILENAME)
547 ohshit(_("package contains overly-long control info file name (starting '%.50s')"),
548 de->d_name);
550 strcpy(cidirrest, de->d_name);
552 /* First we check it's not a directory. */
553 if (rmdir(cidir) == 0)
554 ohshit(_("package control info contained directory '%.250s'"), cidir);
555 else if (errno != ENOTDIR)
556 ohshite(_("package control info rmdir of '%.250s' didn't say not a dir"),
557 de->d_name);
559 /* Ignore the control file. */
560 if (strcmp(de->d_name, CONTROLFILE) == 0) {
561 debug(dbg_scripts, "process_archive tmp.ci script/file '%s' is control",
562 cidir);
563 continue;
565 if (strcmp(de->d_name, LISTFILE) == 0) {
566 warning(_("package %s contained list as info file"),
567 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
568 continue;
571 /* Right, install it */
572 newinfofilename = pkg_infodb_get_file(pkg, &pkg->available, de->d_name);
573 if (rename(cidir, newinfofilename))
574 ohshite(_("unable to install new info file '%.250s' as '%.250s'"),
575 cidir, newinfofilename);
577 debug(dbg_scripts,
578 "process_archive tmp.ci script/file '%s' installed as '%s'",
579 cidir, newinfofilename);
581 pop_cleanup(ehflag_normaltidy); /* closedir */
583 /* If the old and new versions use a different infodb layout, get rid
584 * of the files using the old layout. */
585 if (pkg->installed.multiarch != pkg->available.multiarch &&
586 (pkg->installed.multiarch == PKG_MULTIARCH_SAME ||
587 pkg->available.multiarch == PKG_MULTIARCH_SAME)) {
588 debug(dbg_scripts,
589 "process_archive remove old info files after db layout switch");
590 pkg_infodb_foreach(pkg, &pkg->installed, pkg_infodb_remove_file);
593 dir_sync_path(pkg_infodb_get_dir());
596 static void
597 pkg_remove_conffile_on_upgrade(struct pkginfo *pkg, struct fsys_namenode *namenode)
599 struct pkginfo *otherpkg;
600 struct fsys_namenode *usenode;
601 struct fsys_node_pkgs_iter *iter;
602 struct varbuf cdr = VARBUF_INIT;
603 struct varbuf cdrext = VARBUF_INIT;
604 struct varbuf_state cdrext_state;
605 char currenthash[MD5HASHLEN + 1];
606 int rc;
608 usenode = namenodetouse(namenode, pkg, &pkg->installed);
610 rc = conffderef(pkg, &cdr, usenode->name);
611 if (rc == -1) {
612 debug(dbg_conffdetail, "%s: '%s' conffile dereference error: %s", __func__,
613 namenode->name, strerror(errno));
614 namenode->oldhash = EMPTYHASHFLAG;
615 return;
618 varbuf_set_varbuf(&cdrext, &cdr);
619 varbuf_snapshot(&cdrext, &cdrext_state);
621 iter = fsys_node_pkgs_iter_new(namenode);
622 while ((otherpkg = fsys_node_pkgs_iter_next(iter))) {
623 debug(dbg_conffdetail, "%s: namenode '%s' owned by other %s?",
624 __func__, namenode->name, pkg_name(otherpkg, pnaw_always));
626 if (otherpkg == pkg)
627 continue;
629 debug(dbg_conff, "%s: namenode '%s' owned by other %s, remove-on-upgrade ignored",
630 __func__, namenode->name, pkg_name(otherpkg, pnaw_always));
631 fsys_node_pkgs_iter_free(iter);
632 return;
634 fsys_node_pkgs_iter_free(iter);
636 /* Remove DPKGDISTEXT variant if still present. */
637 varbuf_rollback(&cdrext_state);
638 varbuf_add_str(&cdrext, DPKGDISTEXT);
639 varbuf_end_str(&cdrext);
641 if (unlink(cdrext.buf) < 0 && errno != ENOENT)
642 warning(_("%s: failed to remove '%.250s': %s"),
643 pkg_name(pkg, pnaw_nonambig), cdrext.buf,
644 strerror(errno));
646 md5hash(pkg, currenthash, cdr.buf);
648 /* Has it been already removed (e.g. by local admin)? */
649 if (strcmp(currenthash, NONEXISTENTFLAG) == 0)
650 return;
652 /* For unmodified conffiles, we just remove them. */
653 if (strcmp(currenthash, namenode->oldhash) == 0) {
654 printf(_("Removing obsolete conffile %s ...\n"), cdr.buf);
655 if (unlink(cdr.buf) < 0 && errno != ENOENT)
656 warning(_("%s: failed to remove '%.250s': %s"),
657 pkg_name(pkg, pnaw_nonambig), cdr.buf, strerror(errno));
658 return;
661 /* Otherwise, preserve the modified conffile. */
662 varbuf_rollback(&cdrext_state);
663 varbuf_add_str(&cdrext, DPKGOLDEXT);
664 varbuf_end_str(&cdrext);
666 printf(_("Obsolete conffile '%s' has been modified by you.\n"), cdr.buf);
667 printf(_("Saving as %s ...\n"), cdrext.buf);
668 if (rename(cdr.buf, cdrext.buf) < 0)
669 warning(_("%s: cannot rename obsolete conffile '%s' to '%s': %s"),
670 pkg_name(pkg, pnaw_nonambig),
671 cdr.buf, cdrext.buf, strerror(errno));
674 static void
675 pkg_remove_old_files(struct pkginfo *pkg,
676 struct fsys_namenode_queue *newfiles_queue,
677 struct fsys_namenode_queue *newconffiles)
679 struct fsys_hash_rev_iter rev_iter;
680 struct fsys_namenode_list *cfile;
681 struct fsys_namenode *namenode;
682 struct stat stab, oldfs;
684 /* Before removing any old files, we try to remove obsolete conffiles that
685 * have been requested to be removed during upgrade. These conffiles are
686 * not tracked as part of the package file lists, so removing them here
687 * means we will get their parent directories removed if not present in the
688 * new package without needing to do anything special ourselves. */
689 for (cfile = newconffiles->head; cfile; cfile = cfile->next) {
690 debug(dbg_conffdetail, "%s: removing conffile '%s' for %s?", __func__,
691 cfile->namenode->name, pkg_name(pkg, pnaw_always));
693 if (!(cfile->namenode->flags & FNNF_RM_CONFF_ON_UPGRADE))
694 continue;
696 pkg_remove_conffile_on_upgrade(pkg, cfile->namenode);
699 fsys_hash_rev_iter_init(&rev_iter, pkg->files);
701 while ((namenode = fsys_hash_rev_iter_next(&rev_iter))) {
702 struct fsys_namenode *usenode;
704 if ((namenode->flags & FNNF_NEW_CONFF) ||
705 (namenode->flags & FNNF_RM_CONFF_ON_UPGRADE) ||
706 (namenode->flags & FNNF_NEW_INARCHIVE))
707 continue;
709 usenode = namenodetouse(namenode, pkg, &pkg->installed);
711 varbuf_rollback(&fname_state);
712 varbuf_add_str(&fnamevb, usenode->name);
713 varbuf_end_str(&fnamevb);
715 if (!stat(fnamevb.buf, &stab) && S_ISDIR(stab.st_mode)) {
716 debug(dbg_eachfiledetail, "process_archive: %s is a directory",
717 fnamevb.buf);
718 if (dir_is_used_by_others(namenode, pkg))
719 continue;
722 if (lstat(fnamevb.buf, &oldfs)) {
723 if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
724 warning(_("could not stat old file '%.250s' so not deleting it: %s"),
725 fnamevb.buf, strerror(errno));
726 continue;
728 if (S_ISDIR(oldfs.st_mode)) {
729 trig_path_activate(usenode, pkg);
731 /* Do not try to remove the root directory. */
732 if (strcmp(usenode->name, "/.") == 0)
733 continue;
735 if (rmdir(fnamevb.buf)) {
736 warning(_("unable to delete old directory '%.250s': %s"),
737 namenode->name, strerror(errno));
738 } else if ((namenode->flags & FNNF_OLD_CONFF)) {
739 warning(_("old conffile '%.250s' was an empty directory "
740 "(and has now been deleted)"), namenode->name);
742 } else {
743 struct fsys_namenode_list *sameas = NULL;
744 static struct file_ondisk_id empty_ondisk_id;
745 struct varbuf cfilename = VARBUF_INIT;
748 * Ok, it's an old file, but is it really not in the new package?
749 * It might be known by a different name because of symlinks.
751 * We need to check to make sure, so we stat the file, then compare
752 * it to the new list. If we find a dev/inode match, we assume they
753 * are the same file, and leave it alone. NOTE: we don't check in
754 * other packages for sanity reasons (we don't want to stat _all_
755 * the files on the system).
757 * We run down the list of _new_ files in this package. This keeps
758 * the process a little leaner. We are only worried about new ones
759 * since ones that stayed the same don't really apply here.
762 /* If we can't stat the old or new file, or it's a directory,
763 * we leave it up to the normal code. */
764 debug(dbg_eachfile, "process_archive: checking %s for same files on "
765 "upgrade/downgrade", fnamevb.buf);
767 for (cfile = newfiles_queue->head; cfile; cfile = cfile->next) {
768 /* If the file has been filtered then treat it as if it didn't exist
769 * on the file system. */
770 if (cfile->namenode->flags & FNNF_FILTERED)
771 continue;
773 if (cfile->namenode->file_ondisk_id == NULL) {
774 struct stat tmp_stat;
776 varbuf_set_str(&cfilename, dpkg_fsys_get_dir());
777 varbuf_add_str(&cfilename, cfile->namenode->name);
778 varbuf_end_str(&cfilename);
780 if (lstat(cfilename.buf, &tmp_stat) == 0) {
781 struct file_ondisk_id *file_ondisk_id;
783 file_ondisk_id = nfmalloc(sizeof(*file_ondisk_id));
784 file_ondisk_id->id_dev = tmp_stat.st_dev;
785 file_ondisk_id->id_ino = tmp_stat.st_ino;
786 cfile->namenode->file_ondisk_id = file_ondisk_id;
787 } else {
788 if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
789 ohshite(_("unable to stat other new file '%.250s'"),
790 cfile->namenode->name);
791 cfile->namenode->file_ondisk_id = &empty_ondisk_id;
792 continue;
796 if (cfile->namenode->file_ondisk_id == &empty_ondisk_id)
797 continue;
799 if (oldfs.st_dev == cfile->namenode->file_ondisk_id->id_dev &&
800 oldfs.st_ino == cfile->namenode->file_ondisk_id->id_ino) {
801 if (sameas)
802 warning(_("old file '%.250s' is the same as several new files! "
803 "(both '%.250s' and '%.250s')"), fnamevb.buf,
804 sameas->namenode->name, cfile->namenode->name);
805 sameas = cfile;
806 debug(dbg_eachfile, "process_archive: not removing %s, "
807 "since it matches %s", fnamevb.buf, cfile->namenode->name);
811 varbuf_destroy(&cfilename);
813 if ((namenode->flags & FNNF_OLD_CONFF)) {
814 if (sameas) {
815 if (sameas->namenode->flags & FNNF_NEW_CONFF) {
816 if (strcmp(sameas->namenode->oldhash, NEWCONFFILEFLAG) == 0) {
817 sameas->namenode->oldhash = namenode->oldhash;
818 debug(dbg_eachfile, "process_archive: old conff %s "
819 "is same as new conff %s, copying hash",
820 namenode->name, sameas->namenode->name);
821 } else {
822 debug(dbg_eachfile, "process_archive: old conff %s "
823 "is same as new conff %s but latter already has hash",
824 namenode->name, sameas->namenode->name);
827 } else {
828 debug(dbg_eachfile, "process_archive: old conff %s "
829 "is disappearing", namenode->name);
830 namenode->flags |= FNNF_OBS_CONFF;
831 tar_fsys_namenode_queue_push(newconffiles, namenode);
832 tar_fsys_namenode_queue_push(newfiles_queue, namenode);
834 continue;
837 if (sameas)
838 continue;
840 trig_path_activate(usenode, pkg);
842 if (secure_unlink_statted(fnamevb.buf, &oldfs)) {
843 warning(_("unable to securely remove old file '%.250s': %s"),
844 namenode->name, strerror(errno));
846 } /* !S_ISDIR */
850 static void
851 pkg_update_fields(struct pkginfo *pkg, struct fsys_namenode_queue *newconffiles)
853 struct dependency *newdeplist, **newdeplistlastp;
854 struct dependency *dep;
855 struct conffile **iconffileslastp, *newiconff;
856 struct fsys_namenode_list *cfile;
858 /* The dependencies are the most difficult. We have to build
859 * a whole new forward dependency tree. At least the reverse
860 * links (linking our deppossi's into the reverse chains)
861 * can be done by copy_dependency_links. */
862 newdeplist = NULL;
863 newdeplistlastp = &newdeplist;
864 for (dep = pkg->available.depends; dep; dep = dep->next) {
865 struct deppossi **newpossilastp, *possi;
866 struct dependency *newdep;
868 newdep = nfmalloc(sizeof(*newdep));
869 newdep->up = pkg;
870 newdep->next = NULL;
871 newdep->list = NULL;
872 newpossilastp = &newdep->list;
874 for (possi = dep->list; possi; possi = possi->next) {
875 struct deppossi *newpossi;
877 newpossi = nfmalloc(sizeof(*newpossi));
878 newpossi->up = newdep;
879 newpossi->ed = possi->ed;
880 newpossi->next = NULL;
881 newpossi->rev_next = newpossi->rev_prev = NULL;
882 newpossi->arch_is_implicit = possi->arch_is_implicit;
883 newpossi->arch = possi->arch;
884 newpossi->verrel = possi->verrel;
885 if (possi->verrel != DPKG_RELATION_NONE)
886 newpossi->version = possi->version;
887 else
888 dpkg_version_blank(&newpossi->version);
889 newpossi->cyclebreak = false;
890 *newpossilastp = newpossi;
891 newpossilastp = &newpossi->next;
893 newdep->type = dep->type;
894 *newdeplistlastp = newdep;
895 newdeplistlastp = &newdep->next;
898 /* Right, now we've replicated the forward tree, we
899 * get copy_dependency_links to remove all the old dependency
900 * structures from the reverse links and add the new dependency
901 * structures in instead. It also copies the new dependency
902 * structure pointer for this package into the right field. */
903 copy_dependency_links(pkg, &pkg->installed.depends, newdeplist, 0);
905 /* We copy the text fields. */
906 pkg->installed.essential = pkg->available.essential;
907 pkg->installed.is_protected = pkg->available.is_protected;
908 pkg->installed.multiarch = pkg->available.multiarch;
909 pkg->installed.description = pkg->available.description;
910 pkg->installed.maintainer = pkg->available.maintainer;
911 pkg->installed.source = pkg->available.source;
912 pkg->installed.arch = pkg->available.arch;
913 pkg->installed.pkgname_archqual = pkg->available.pkgname_archqual;
914 pkg->installed.installedsize = pkg->available.installedsize;
915 pkg->installed.version = pkg->available.version;
916 pkg->installed.origin = pkg->available.origin;
917 pkg->installed.bugs = pkg->available.bugs;
919 /* We have to generate our own conffiles structure. */
920 pkg->installed.conffiles = NULL;
921 iconffileslastp = &pkg->installed.conffiles;
922 for (cfile = newconffiles->head; cfile; cfile = cfile->next) {
923 newiconff = nfmalloc(sizeof(*newiconff));
924 newiconff->next = NULL;
925 newiconff->name = nfstrsave(cfile->namenode->name);
926 newiconff->hash = nfstrsave(cfile->namenode->oldhash);
927 newiconff->obsolete = !!(cfile->namenode->flags & FNNF_OBS_CONFF);
928 newiconff->remove_on_upgrade = !!(
929 cfile->namenode->flags & FNNF_RM_CONFF_ON_UPGRADE);
930 *iconffileslastp = newiconff;
931 iconffileslastp = &newiconff->next;
934 /* We can just copy the arbitrary fields list, because it is
935 * never even rearranged. Phew! */
936 pkg->installed.arbs = pkg->available.arbs;
939 static void
940 pkg_disappear(struct pkginfo *pkg, struct pkginfo *infavour)
942 printf(_("(Noting disappearance of %s, which has been completely replaced.)\n"),
943 pkg_name(pkg, pnaw_nonambig));
944 log_action("disappear", pkg, &pkg->installed);
945 debug(dbg_general, "pkg_disappear disappearing %s",
946 pkg_name(pkg, pnaw_always));
948 trig_activate_packageprocessing(pkg);
949 maintscript_installed(pkg, POSTRMFILE,
950 "post-removal script (for disappearance)",
951 "disappear",
952 pkgbin_name(infavour, &infavour->available,
953 pnaw_nonambig),
954 versiondescribe(&infavour->available.version,
955 vdew_nonambig),
956 NULL);
958 /* OK, now we delete all the stuff in the ‘info’ directory ... */
959 debug(dbg_general, "pkg_disappear cleaning info directory");
960 pkg_infodb_foreach(pkg, &pkg->installed, pkg_infodb_remove_file);
961 dir_sync_path(pkg_infodb_get_dir());
963 pkg_set_status(pkg, PKG_STAT_NOTINSTALLED);
964 pkg_set_want(pkg, PKG_WANT_UNKNOWN);
965 pkg_reset_eflags(pkg);
967 dpkg_version_blank(&pkg->configversion);
968 pkgbin_blank(&pkg->installed);
970 pkg->files_list_valid = false;
972 modstatdb_note(pkg);
975 static void
976 pkg_disappear_others(struct pkginfo *pkg)
978 struct pkg_hash_iter *iter;
979 struct pkginfo *otherpkg;
980 struct fsys_namenode_list *cfile;
981 struct deppossi *pdep;
982 struct dependency *providecheck;
983 struct varbuf depprobwhy = VARBUF_INIT;
985 iter = pkg_hash_iter_new();
986 while ((otherpkg = pkg_hash_iter_next_pkg(iter)) != NULL) {
987 ensure_package_clientdata(otherpkg);
989 if (otherpkg == pkg ||
990 otherpkg->status == PKG_STAT_NOTINSTALLED ||
991 otherpkg->status == PKG_STAT_CONFIGFILES ||
992 otherpkg->clientdata->istobe == PKG_ISTOBE_REMOVE ||
993 !otherpkg->files)
994 continue;
996 /* Do not try to disappear other packages from the same set
997 * if they are Multi-Arch: same */
998 if (pkg->installed.multiarch == PKG_MULTIARCH_SAME &&
999 otherpkg->installed.multiarch == PKG_MULTIARCH_SAME &&
1000 otherpkg->set == pkg->set)
1001 continue;
1003 debug(dbg_veryverbose, "process_archive checking disappearance %s",
1004 pkg_name(otherpkg, pnaw_always));
1006 if (otherpkg->clientdata->istobe != PKG_ISTOBE_NORMAL &&
1007 otherpkg->clientdata->istobe != PKG_ISTOBE_DECONFIGURE)
1008 internerr("disappearing package %s is not to be normal or deconfigure, "
1009 "is to be %d",
1010 pkg_name(otherpkg, pnaw_always), otherpkg->clientdata->istobe);
1012 for (cfile = otherpkg->files;
1013 cfile && strcmp(cfile->namenode->name, "/.") == 0;
1014 cfile = cfile->next);
1015 if (!cfile) {
1016 debug(dbg_stupidlyverbose, "process_archive no non-root, no disappear");
1017 continue;
1019 for (cfile = otherpkg->files;
1020 cfile && !filesavespackage(cfile, otherpkg, pkg);
1021 cfile = cfile->next);
1022 if (cfile)
1023 continue;
1025 /* So dependency things will give right answers ... */
1026 otherpkg->clientdata->istobe = PKG_ISTOBE_REMOVE;
1027 debug(dbg_veryverbose, "process_archive disappear checking dependencies");
1028 for (pdep = otherpkg->set->depended.installed;
1029 pdep;
1030 pdep = pdep->rev_next) {
1031 if (pdep->up->type != dep_depends &&
1032 pdep->up->type != dep_predepends &&
1033 pdep->up->type != dep_recommends)
1034 continue;
1036 if (depisok(pdep->up, &depprobwhy, NULL, NULL, false))
1037 continue;
1039 varbuf_end_str(&depprobwhy);
1040 debug(dbg_veryverbose,"process_archive cannot disappear: %s",
1041 depprobwhy.buf);
1042 break;
1044 if (!pdep) {
1045 /* If we haven't found a reason not to yet, let's look some more. */
1046 for (providecheck = otherpkg->installed.depends;
1047 providecheck;
1048 providecheck = providecheck->next) {
1049 if (providecheck->type != dep_provides)
1050 continue;
1052 for (pdep = providecheck->list->ed->depended.installed;
1053 pdep;
1054 pdep = pdep->rev_next) {
1055 if (pdep->up->type != dep_depends &&
1056 pdep->up->type != dep_predepends &&
1057 pdep->up->type != dep_recommends)
1058 continue;
1060 if (depisok(pdep->up, &depprobwhy, NULL, NULL, false))
1061 continue;
1063 varbuf_end_str(&depprobwhy);
1064 debug(dbg_veryverbose,
1065 "process_archive cannot disappear (provides %s): %s",
1066 providecheck->list->ed->name, depprobwhy.buf);
1067 goto break_from_both_loops_at_once;
1070 break_from_both_loops_at_once:;
1072 otherpkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
1073 if (pdep)
1074 continue;
1076 /* No, we're disappearing it. This is the wrong time to go and
1077 * run maintainer scripts and things, as we can't back out. But
1078 * what can we do ? It has to be run this late. */
1079 pkg_disappear(otherpkg, pkg);
1080 } /* while (otherpkg= ... */
1081 pkg_hash_iter_free(iter);
1085 * Check if all instances of a pkgset are getting in sync.
1087 * If that's the case, the extraction is going to ensure consistency
1088 * of shared files.
1090 static bool
1091 pkgset_getting_in_sync(struct pkginfo *pkg)
1093 struct pkginfo *otherpkg;
1095 for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
1096 if (otherpkg == pkg)
1097 continue;
1098 if (otherpkg->status <= PKG_STAT_CONFIGFILES)
1099 continue;
1100 if (dpkg_version_compare(&pkg->available.version,
1101 &otherpkg->installed.version)) {
1102 return false;
1106 return true;
1109 static void
1110 pkg_remove_files_from_others(struct pkginfo *pkg, struct fsys_namenode_list *newfileslist)
1112 struct fsys_namenode_list *cfile;
1113 struct pkginfo *otherpkg;
1115 for (cfile = newfileslist; cfile; cfile = cfile->next) {
1116 struct fsys_node_pkgs_iter *iter;
1117 struct pkgset *divpkgset;
1119 if (!(cfile->namenode->flags & FNNF_ELIDE_OTHER_LISTS))
1120 continue;
1122 if (cfile->namenode->divert && cfile->namenode->divert->useinstead) {
1123 divpkgset = cfile->namenode->divert->pkgset;
1124 if (divpkgset == pkg->set) {
1125 debug(dbg_eachfile,
1126 "process_archive not overwriting any '%s' (overriding, '%s')",
1127 cfile->namenode->name, cfile->namenode->divert->useinstead->name);
1128 continue;
1129 } else {
1130 debug(dbg_eachfile,
1131 "process_archive looking for overwriting '%s' (overridden by %s)",
1132 cfile->namenode->name, divpkgset ? divpkgset->name : "<local>");
1134 } else {
1135 divpkgset = NULL;
1136 debug(dbg_eachfile, "process_archive looking for overwriting '%s'",
1137 cfile->namenode->name);
1140 iter = fsys_node_pkgs_iter_new(cfile->namenode);
1141 while ((otherpkg = fsys_node_pkgs_iter_next(iter))) {
1142 debug(dbg_eachfiledetail, "process_archive ... found in %s",
1143 pkg_name(otherpkg, pnaw_always));
1145 /* A pkgset can share files between instances, so there's no point
1146 * in rewriting the file that's already in place. */
1147 if (otherpkg->set == pkg->set)
1148 continue;
1150 if (otherpkg->set == divpkgset) {
1151 debug(dbg_eachfiledetail, "process_archive ... diverted, skipping");
1152 continue;
1155 if (cfile->namenode->flags & FNNF_NEW_CONFF)
1156 conffile_mark_obsolete(otherpkg, cfile->namenode);
1158 /* If !files_list_valid then it's one of the disappeared packages above
1159 * or we have already updated the files list file, and we don't bother
1160 * with it here, clearly. */
1161 if (!otherpkg->files_list_valid)
1162 continue;
1164 /* Found one. We delete the list entry for this file,
1165 * (and any others in the same package) and then mark the package
1166 * as requiring a reread. */
1167 write_filelist_except(otherpkg, &otherpkg->installed,
1168 otherpkg->files, FNNF_ELIDE_OTHER_LISTS);
1169 debug(dbg_veryverbose, "process_archive overwrote from %s",
1170 pkg_name(otherpkg, pnaw_always));
1172 fsys_node_pkgs_iter_free(iter);
1176 static void
1177 pkg_remove_backup_files(struct pkginfo *pkg, struct fsys_namenode_list *newfileslist)
1179 struct fsys_namenode_list *cfile;
1181 for (cfile = newfileslist; cfile; cfile = cfile->next) {
1182 struct fsys_namenode *usenode;
1184 if (cfile->namenode->flags & FNNF_NEW_CONFF)
1185 continue;
1187 usenode = namenodetouse(cfile->namenode, pkg, &pkg->installed);
1189 /* Do not try to remove backups for the root directory. */
1190 if (strcmp(usenode->name, "/.") == 0)
1191 continue;
1193 varbuf_rollback(&fnametmp_state);
1194 varbuf_add_str(&fnametmpvb, usenode->name);
1195 varbuf_add_str(&fnametmpvb, DPKGTEMPEXT);
1196 varbuf_end_str(&fnametmpvb);
1197 path_remove_tree(fnametmpvb.buf);
1201 void process_archive(const char *filename) {
1202 static const struct tar_operations tf = {
1203 .read = tarfileread,
1204 .extract_file = tarobject,
1205 .link = tarobject,
1206 .symlink = tarobject,
1207 .mkdir = tarobject,
1208 .mknod = tarobject,
1211 /* These need to be static so that we can pass their addresses to
1212 * push_cleanup as arguments to the cu_xxx routines; if an error occurs
1213 * we unwind the stack before processing the cleanup list, and these
1214 * variables had better still exist ... */
1215 static int p1[2];
1216 static enum pkgstatus oldversionstatus;
1217 static struct tarcontext tc;
1219 struct tar_archive tar;
1220 struct dpkg_error err;
1221 enum parsedbflags parsedb_flags;
1222 int rc;
1223 pid_t pid;
1224 struct pkginfo *pkg, *otherpkg;
1225 struct pkg_list *conflictor_iter;
1226 char *cidir = NULL;
1227 char *cidirrest;
1228 char *psize;
1229 const char *pfilename;
1230 struct fsys_namenode_queue newconffiles, newfiles_queue;
1231 struct stat stab;
1233 cleanup_pkg_failed= cleanup_conflictor_failed= 0;
1235 pfilename = summarize_filename(filename);
1237 if (stat(filename, &stab))
1238 ohshite(_("cannot access archive '%s'"), filename);
1240 /* We can't ‘tentatively-reassemble’ packages. */
1241 if (!f_noact) {
1242 if (!deb_reassemble(&filename, &pfilename))
1243 return;
1246 /* Verify the package. */
1247 if (!f_nodebsig)
1248 deb_verify(filename);
1250 /* Get the control information directory. */
1251 cidir = get_control_dir(cidir);
1252 cidirrest = cidir + strlen(cidir);
1253 push_cleanup(cu_cidir, ~0, 2, (void *)cidir, (void *)cidirrest);
1255 pid = subproc_fork();
1256 if (pid == 0) {
1257 cidirrest[-1] = '\0';
1258 execlp(BACKEND, BACKEND, "--control", filename, cidir, NULL);
1259 ohshite(_("unable to execute %s (%s)"),
1260 _("package control information extraction"), BACKEND);
1262 subproc_reap(pid, BACKEND " --control", 0);
1264 /* We want to guarantee the extracted files are on the disk, so that the
1265 * subsequent renames to the info database do not end up with old or zero
1266 * length files in case of a system crash. As neither dpkg-deb nor tar do
1267 * explicit fsync()s, we have to do them here.
1268 * XXX: This could be avoided by switching to an internal tar extractor. */
1269 dir_sync_contents(cidir);
1271 strcpy(cidirrest,CONTROLFILE);
1273 if (cipaction->arg_int == act_avail)
1274 parsedb_flags = pdb_parse_available;
1275 else
1276 parsedb_flags = pdb_parse_binary;
1277 parsedb_flags |= pdb_ignore_archives;
1278 if (in_force(FORCE_BAD_VERSION))
1279 parsedb_flags |= pdb_lax_version_parser;
1281 parsedb(cidir, parsedb_flags, &pkg);
1283 if (!pkg->archives) {
1284 pkg->archives = nfmalloc(sizeof(*pkg->archives));
1285 pkg->archives->next = NULL;
1286 pkg->archives->name = NULL;
1287 pkg->archives->msdosname = NULL;
1288 pkg->archives->md5sum = NULL;
1290 /* Always nfmalloc. Otherwise, we may overwrite some other field (like
1291 * md5sum). */
1292 psize = nfmalloc(30);
1293 sprintf(psize, "%jd", (intmax_t)stab.st_size);
1294 pkg->archives->size = psize;
1296 if (cipaction->arg_int == act_avail) {
1297 printf(_("Recorded info about %s from %s.\n"),
1298 pkgbin_name(pkg, &pkg->available, pnaw_nonambig), pfilename);
1299 pop_cleanup(ehflag_normaltidy);
1300 return;
1303 if (pkg->available.arch->type != DPKG_ARCH_ALL &&
1304 pkg->available.arch->type != DPKG_ARCH_NATIVE &&
1305 pkg->available.arch->type != DPKG_ARCH_FOREIGN)
1306 forcibleerr(FORCE_ARCHITECTURE,
1307 _("package architecture (%s) does not match system (%s)"),
1308 pkg->available.arch->name,
1309 dpkg_arch_get(DPKG_ARCH_NATIVE)->name);
1311 clear_deconfigure_queue();
1312 clear_istobes();
1314 if (wanttoinstall(pkg)) {
1315 pkg_set_want(pkg, PKG_WANT_INSTALL);
1316 } else {
1317 pop_cleanup(ehflag_normaltidy);
1318 return;
1321 /* Deconfigure other instances from a pkgset if they are not in sync. */
1322 for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
1323 if (otherpkg == pkg)
1324 continue;
1325 if (otherpkg->status <= PKG_STAT_HALFCONFIGURED)
1326 continue;
1328 if (dpkg_version_compare(&pkg->available.version,
1329 &otherpkg->installed.version))
1330 enqueue_deconfigure(otherpkg, NULL, PKG_WANT_UNKNOWN);
1333 pkg_check_depcon(pkg, pfilename);
1335 ensure_allinstfiles_available();
1336 fsys_hash_init();
1337 trig_file_interests_ensure();
1339 printf(_("Preparing to unpack %s ...\n"), pfilename);
1341 if (pkg->status != PKG_STAT_NOTINSTALLED &&
1342 pkg->status != PKG_STAT_CONFIGFILES) {
1343 log_action("upgrade", pkg, &pkg->installed);
1344 } else {
1345 log_action("install", pkg, &pkg->available);
1348 if (f_noact) {
1349 pop_cleanup(ehflag_normaltidy);
1350 return;
1354 * OK, we're going ahead.
1357 trig_activate_packageprocessing(pkg);
1358 strcpy(cidirrest, TRIGGERSCIFILE);
1359 trig_parse_ci(cidir, NULL, trig_cicb_statuschange_activate, pkg, &pkg->available);
1361 /* Read the conffiles, and copy the hashes across. */
1362 newconffiles.head = NULL;
1363 newconffiles.tail = &newconffiles.head;
1364 push_cleanup(cu_fileslist, ~0, 0);
1365 strcpy(cidirrest,CONFFILESFILE);
1366 deb_parse_conffiles(pkg, cidir, &newconffiles);
1368 /* All the old conffiles are marked with a flag, so that we don't delete
1369 * them if they seem to disappear completely. */
1370 pkg_conffiles_mark_old(pkg);
1371 for (conflictor_iter = conflictors.head;
1372 conflictor_iter;
1373 conflictor_iter = conflictor_iter->next)
1374 pkg_conffiles_mark_old(conflictor_iter->pkg);
1376 oldversionstatus= pkg->status;
1378 if (oldversionstatus > PKG_STAT_INSTALLED)
1379 internerr("package %s state %d is out-of-bounds",
1380 pkg_name(pkg, pnaw_always), oldversionstatus);
1382 debug(dbg_general,"process_archive oldversionstatus=%s",
1383 statusstrings[oldversionstatus]);
1385 if (oldversionstatus == PKG_STAT_HALFCONFIGURED ||
1386 oldversionstatus == PKG_STAT_TRIGGERSAWAITED ||
1387 oldversionstatus == PKG_STAT_TRIGGERSPENDING ||
1388 oldversionstatus == PKG_STAT_INSTALLED) {
1389 pkg_set_eflags(pkg, PKG_EFLAG_REINSTREQ);
1390 pkg_set_status(pkg, PKG_STAT_HALFCONFIGURED);
1391 modstatdb_note(pkg);
1392 push_cleanup(cu_prermupgrade, ~ehflag_normaltidy, 1, (void *)pkg);
1393 maintscript_fallback(pkg, PRERMFILE, "pre-removal", cidir, cidirrest,
1394 "upgrade", "failed-upgrade");
1395 pkg_set_status(pkg, PKG_STAT_UNPACKED);
1396 oldversionstatus = PKG_STAT_UNPACKED;
1397 modstatdb_note(pkg);
1400 pkg_deconfigure_others(pkg);
1402 for (conflictor_iter = conflictors.head;
1403 conflictor_iter;
1404 conflictor_iter = conflictor_iter->next) {
1405 struct pkginfo *conflictor = conflictor_iter->pkg;
1407 if (!(conflictor->status == PKG_STAT_HALFCONFIGURED ||
1408 conflictor->status == PKG_STAT_TRIGGERSAWAITED ||
1409 conflictor->status == PKG_STAT_TRIGGERSPENDING ||
1410 conflictor->status == PKG_STAT_INSTALLED))
1411 continue;
1413 trig_activate_packageprocessing(conflictor);
1414 pkg_set_status(conflictor, PKG_STAT_HALFCONFIGURED);
1415 modstatdb_note(conflictor);
1416 push_cleanup(cu_prerminfavour, ~ehflag_normaltidy,
1417 2, conflictor, pkg);
1418 maintscript_installed(conflictor, PRERMFILE, "pre-removal",
1419 "remove", "in-favour",
1420 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
1421 versiondescribe(&pkg->available.version,
1422 vdew_nonambig),
1423 NULL);
1424 pkg_set_status(conflictor, PKG_STAT_HALFINSTALLED);
1425 modstatdb_note(conflictor);
1428 pkg_set_eflags(pkg, PKG_EFLAG_REINSTREQ);
1429 if (pkg->status == PKG_STAT_NOTINSTALLED) {
1430 pkg->installed.version= pkg->available.version;
1431 pkg->installed.multiarch = pkg->available.multiarch;
1433 pkg_set_status(pkg, PKG_STAT_HALFINSTALLED);
1434 modstatdb_note(pkg);
1435 if (oldversionstatus == PKG_STAT_NOTINSTALLED) {
1436 push_cleanup(cu_preinstverynew, ~ehflag_normaltidy,
1437 3,(void*)pkg,(void*)cidir,(void*)cidirrest);
1438 maintscript_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest,
1439 "install", NULL);
1440 } else if (oldversionstatus == PKG_STAT_CONFIGFILES) {
1441 push_cleanup(cu_preinstnew, ~ehflag_normaltidy,
1442 3,(void*)pkg,(void*)cidir,(void*)cidirrest);
1443 maintscript_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest,
1444 "install",
1445 versiondescribe(&pkg->installed.version, vdew_nonambig),
1446 versiondescribe(&pkg->available.version, vdew_nonambig),
1447 NULL);
1448 } else {
1449 push_cleanup(cu_preinstupgrade, ~ehflag_normaltidy,
1450 4,(void*)pkg,(void*)cidir,(void*)cidirrest,(void*)&oldversionstatus);
1451 maintscript_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest,
1452 "upgrade",
1453 versiondescribe(&pkg->installed.version, vdew_nonambig),
1454 versiondescribe(&pkg->available.version, vdew_nonambig),
1455 NULL);
1458 if (oldversionstatus == PKG_STAT_NOTINSTALLED ||
1459 oldversionstatus == PKG_STAT_CONFIGFILES) {
1460 printf(_("Unpacking %s (%s) ...\n"),
1461 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
1462 versiondescribe(&pkg->available.version, vdew_nonambig));
1463 } else {
1464 printf(_("Unpacking %s (%s) over (%s) ...\n"),
1465 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
1466 versiondescribe(&pkg->available.version, vdew_nonambig),
1467 versiondescribe(&pkg->installed.version, vdew_nonambig));
1471 * Now we unpack the archive, backing things up as we go.
1472 * For each file, we check to see if it already exists.
1473 * There are several possibilities:
1475 * + We are trying to install a non-directory ...
1476 * - It doesn't exist. In this case we simply extract it.
1477 * - It is a plain file, device, symlink, &c. We do an ‘atomic
1478 * overwrite’ using link() and rename(), but leave a backup copy.
1479 * Later, when we delete the backup, we remove it from any other
1480 * packages' lists.
1481 * - It is a directory. In this case it depends on whether we're
1482 * trying to install a symlink or something else.
1483 * = If we're not trying to install a symlink we move the directory
1484 * aside and extract the node. Later, when we recursively remove
1485 * the backed-up directory, we remove it from any other packages'
1486 * lists.
1487 * = If we are trying to install a symlink we do nothing - ie,
1488 * dpkg will never replace a directory tree with a symlink. This
1489 * is to avoid embarrassing effects such as replacing a directory
1490 * tree with a link to a link to the original directory tree.
1491 * + We are trying to install a directory ...
1492 * - It doesn't exist. We create it with the appropriate modes.
1493 * - It exists as a directory or a symlink to one. We do nothing.
1494 * - It is a plain file or a symlink (other than to a directory).
1495 * We move it aside and create the directory. Later, when we
1496 * delete the backup, we remove it from any other packages' lists.
1498 * Install non-dir Install symlink Install dir
1499 * Exists not X X X
1500 * File/node/symlink LXR LXR BXR
1501 * Directory BXR - -
1503 * X: extract file/node/link/directory
1504 * LX: atomic overwrite leaving backup
1505 * B: ordinary backup
1506 * R: later remove from other packages' lists
1507 * -: do nothing
1509 * After we've done this we go through the remaining things in the
1510 * lists of packages we're trying to remove (including the old
1511 * version of the current package). This happens in reverse order,
1512 * so that we process files before the directories (or symlinks-to-
1513 * directories) containing them.
1515 * + If the thing is a conffile then we leave it alone for the purge
1516 * operation.
1517 * + Otherwise, there are several possibilities too:
1518 * - The listed thing does not exist. We ignore it.
1519 * - The listed thing is a directory or a symlink to a directory.
1520 * We delete it only if it isn't listed in any other package.
1521 * - The listed thing is not a directory, but was part of the package
1522 * that was upgraded, we check to make sure the files aren't the
1523 * same ones from the old package by checking dev/inode
1524 * - The listed thing is not a directory or a symlink to one (ie,
1525 * it's a plain file, device, pipe, &c, or a symlink to one, or a
1526 * dangling symlink). We delete it.
1528 * The removed packages' list becomes empty (of course, the new
1529 * version of the package we're installing will have a new list,
1530 * which replaces the old version's list).
1532 * If at any stage we remove a file from a package's list, and the
1533 * package isn't one we're already processing, and the package's
1534 * list becomes empty as a result, we ‘vanish’ the package. This
1535 * means that we run its postrm with the ‘disappear’ argument, and
1536 * put the package in the ‘not-installed’ state. If it had any
1537 * conffiles, their hashes and ownership will have been transferred
1538 * already, so we just ignore those and forget about them from the
1539 * point of view of the disappearing package.
1541 * NOTE THAT THE OLD POSTRM IS RUN AFTER THE NEW PREINST, since the
1542 * files get replaced ‘as we go’.
1545 m_pipe(p1);
1546 push_cleanup(cu_closepipe, ehflag_bombout, 1, (void *)&p1[0]);
1547 pid = subproc_fork();
1548 if (pid == 0) {
1549 m_dup2(p1[1],1); close(p1[0]); close(p1[1]);
1550 execlp(BACKEND, BACKEND, "--fsys-tarfile", filename, NULL);
1551 ohshite(_("unable to execute %s (%s)"),
1552 _("package filesystem archive extraction"), BACKEND);
1554 close(p1[1]);
1555 p1[1] = -1;
1557 newfiles_queue.head = NULL;
1558 newfiles_queue.tail = &newfiles_queue.head;
1559 tc.newfiles_queue = &newfiles_queue;
1560 push_cleanup(cu_fileslist, ~0, 0);
1561 tc.pkg= pkg;
1562 tc.backendpipe= p1[0];
1563 tc.pkgset_getting_in_sync = pkgset_getting_in_sync(pkg);
1565 /* Setup the tar archive. */
1566 tar.err = DPKG_ERROR_OBJECT;
1567 tar.ctx = &tc;
1568 tar.ops = &tf;
1570 rc = tar_extractor(&tar);
1571 if (rc)
1572 dpkg_error_print(&tar.err,
1573 _("corrupted filesystem tarfile in package archive"));
1574 if (fd_skip(p1[0], -1, &err) < 0)
1575 ohshit(_("cannot zap possible trailing zeros from dpkg-deb: %s"), err.str);
1576 close(p1[0]);
1577 p1[0] = -1;
1578 subproc_reap(pid, BACKEND " --fsys-tarfile", SUBPROC_NOPIPE);
1580 tar_deferred_extract(newfiles_queue.head, pkg);
1582 if (oldversionstatus == PKG_STAT_HALFINSTALLED ||
1583 oldversionstatus == PKG_STAT_UNPACKED) {
1584 /* Packages that were in ‘installed’ and ‘postinstfailed’ have been
1585 * reduced to ‘unpacked’ by now, by the running of the prerm script. */
1586 pkg_set_status(pkg, PKG_STAT_HALFINSTALLED);
1587 modstatdb_note(pkg);
1588 push_cleanup(cu_postrmupgrade, ~ehflag_normaltidy, 1, (void *)pkg);
1589 maintscript_fallback(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
1590 "upgrade", "failed-upgrade");
1593 /* If anything goes wrong while tidying up it's a bit late to do
1594 * anything about it. However, we don't install the new status
1595 * info yet, so that a future dpkg installation will put everything
1596 * right (we hope).
1598 * If something does go wrong later the ‘conflictor’ package will be
1599 * left in the ‘removal_failed’ state. Removing or installing it
1600 * will be impossible if it was required because of the conflict with
1601 * the package we're installing now and (presumably) the dependency
1602 * by other packages. This means that the files it contains in
1603 * common with this package will hang around until we successfully
1604 * get this package installed, after which point we can trust the
1605 * conflicting package's file list, which will have been updated to
1606 * remove any files in this package. */
1607 push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
1609 /* Now we delete all the files that were in the old version of
1610 * the package only, except (old or new) conffiles, which we leave
1611 * alone. */
1612 pkg_remove_old_files(pkg, &newfiles_queue, &newconffiles);
1614 /* OK, now we can write the updated files-in-this package list,
1615 * since we've done away (hopefully) with all the old junk. */
1616 write_filelist_except(pkg, &pkg->available, newfiles_queue.head, 0);
1618 /* Trigger interests may have changed.
1619 * Firstly we go through the old list of interests deleting them.
1620 * Then we go through the new list adding them. */
1621 strcpy(cidirrest, TRIGGERSCIFILE);
1622 trig_parse_ci(pkg_infodb_get_file(pkg, &pkg->installed, TRIGGERSCIFILE),
1623 trig_cicb_interest_delete, NULL, pkg, &pkg->installed);
1624 trig_parse_ci(cidir, trig_cicb_interest_add, NULL, pkg, &pkg->available);
1625 trig_file_interests_save();
1627 /* We also install the new maintainer scripts, and any other
1628 * cruft that may have come along with the package. First
1629 * we go through the existing scripts replacing or removing
1630 * them as appropriate; then we go through the new scripts
1631 * (any that are left) and install them. */
1632 debug(dbg_general, "process_archive updating info directory");
1633 pkg_infodb_update(pkg, cidir, cidirrest);
1635 /* We store now the checksums dynamically computed while unpacking. */
1636 write_filehash_except(pkg, &pkg->available, newfiles_queue.head, 0);
1639 * Update the status database.
1641 * This involves copying each field across from the ‘available’
1642 * to the ‘installed’ half of the pkg structure.
1643 * For some of the fields we have to do a complicated construction
1644 * operation; for others we can just copy the value.
1645 * We tackle the fields in the order they appear, so that
1646 * we don't miss any out :-).
1647 * At least we don't have to copy any strings that are referred
1648 * to, because these are never modified and never freed.
1650 pkg_update_fields(pkg, &newconffiles);
1652 /* In case this was an architecture cross-grade, the in-core pkgset might
1653 * be in an inconsistent state, with two pkginfo entries having the same
1654 * architecture, so let's fix that. Note that this does not lose data,
1655 * as the pkg.available member parsed from the binary should replace the
1656 * to be cleared duplicate in the other instance. */
1657 for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
1658 if (otherpkg == pkg)
1659 continue;
1660 if (otherpkg->installed.arch != pkg->installed.arch)
1661 continue;
1663 if (otherpkg->status != PKG_STAT_NOTINSTALLED)
1664 internerr("other package %s instance in state %s instead of not-installed",
1665 pkg_name(otherpkg, pnaw_always), pkg_status_name(otherpkg));
1667 pkg_blank(otherpkg);
1670 /* Check for disappearing packages:
1671 * We go through all the packages on the system looking for ones
1672 * whose files are entirely part of the one we've just unpacked
1673 * (and which actually *have* some files!).
1675 * Any that we find are removed - we run the postrm with ‘disappear’
1676 * as an argument, and remove their info/... files and status info.
1677 * Conffiles are ignored (the new package had better do something
1678 * with them!). */
1679 pkg_disappear_others(pkg);
1681 /* Delete files from any other packages' lists.
1682 * We have to do this before we claim this package is in any
1683 * sane kind of state, as otherwise we might delete by mistake
1684 * a file that we overwrote, when we remove the package which
1685 * had the version we overwrote. To prevent this we make
1686 * sure that we don't claim this package is OK until we
1687 * have claimed ‘ownership’ of all its files. */
1688 pkg_remove_files_from_others(pkg, newfiles_queue.head);
1690 /* Right, the package we've unpacked is now in a reasonable state.
1691 * The only thing that we have left to do with it is remove
1692 * backup files, and we can leave the user to fix that if and when
1693 * it happens (we leave the reinstall required flag, of course). */
1694 pkg_set_status(pkg, PKG_STAT_UNPACKED);
1695 modstatdb_note(pkg);
1697 /* Now we delete all the backup files that we made when
1698 * extracting the archive - except for files listed as conffiles
1699 * in the new package.
1700 * This time we count it as an error if something goes wrong.
1702 * Note that we don't ever delete things that were in the old
1703 * package as a conffile and don't appear at all in the new.
1704 * They stay recorded as obsolete conffiles and will eventually
1705 * (if not taken over by another package) be forgotten. */
1706 pkg_remove_backup_files(pkg, newfiles_queue.head);
1708 /* OK, we're now fully done with the main package.
1709 * This is quite a nice state, so we don't unwind past here. */
1710 pkg_reset_eflags(pkg);
1711 modstatdb_note(pkg);
1712 push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
1714 /* Only the removal of the conflictor left to do.
1715 * The files list for the conflictor is still a little inconsistent in-core,
1716 * as we have not yet updated the filename->packages mappings; however,
1717 * the package->filenames mapping is. */
1718 while (!pkg_queue_is_empty(&conflictors)) {
1719 struct pkginfo *conflictor = pkg_queue_pop(&conflictors);
1721 /* We need to have the most up-to-date info about which files are
1722 * what ... */
1723 ensure_allinstfiles_available();
1724 printf(_("Removing %s (%s), to allow configuration of %s (%s) ...\n"),
1725 pkg_name(conflictor, pnaw_nonambig),
1726 versiondescribe(&conflictor->installed.version, vdew_nonambig),
1727 pkg_name(pkg, pnaw_nonambig),
1728 versiondescribe(&pkg->installed.version, vdew_nonambig));
1729 removal_bulk(conflictor);
1732 if (cipaction->arg_int == act_install)
1733 enqueue_package_mark_seen(pkg);