awful debugging hacking
[dpkg] / src / unpack.c
CommitLineData
1479465f
GJ
1/*
2 * dpkg - main program for package management
3 * unpack.c - .deb archive unpacking
4 *
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>
9 *
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.
14 *
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.
19 *
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/>.
22 */
23
24#include <config.h>
25#include <compat.h>
26
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/wait.h>
30
31#include <assert.h>
32#include <errno.h>
33#include <string.h>
34#include <time.h>
35#include <utime.h>
36#include <fcntl.h>
37#include <dirent.h>
38#include <unistd.h>
39#include <stdint.h>
40#include <stdlib.h>
41#include <stdio.h>
42
43#include <dpkg/i18n.h>
44#include <dpkg/c-ctype.h>
45#include <dpkg/dpkg.h>
46#include <dpkg/dpkg-db.h>
47#include <dpkg/pkg.h>
48#include <dpkg/pkg-queue.h>
49#include <dpkg/path.h>
29279ac2 50#include <dpkg/fdio.h>
1479465f
GJ
51#include <dpkg/buffer.h>
52#include <dpkg/subproc.h>
53#include <dpkg/dir.h>
54#include <dpkg/tarfn.h>
55#include <dpkg/options.h>
56#include <dpkg/triglib.h>
57
58#include "filesdb.h"
59#include "file-match.h"
60#include "infodb.h"
61#include "main.h"
62#include "archives.h"
63
64static const char *
65summarize_filename(const char *filename)
66{
67 const char *pfilename;
68 char *pfilenamebuf;
69
70 for (pfilename = filename;
71 pfilename && strlen(pfilename) > 30 && strchr(pfilename, '/') != NULL;
72 pfilename++)
73 pfilename = strchr(pfilename, '/');
74
75 if (pfilename && pfilename != filename) {
76 pfilenamebuf = nfmalloc(strlen(pfilename) + 5);
77 sprintf(pfilenamebuf, _(".../%s"), pfilename);
78 pfilename = pfilenamebuf;
79 } else {
80 pfilename = filename;
81 }
82
83 return pfilename;
84}
85
86static bool
87deb_reassemble(const char **filename, const char **pfilename)
88{
89 static char *reasmbuf = NULL;
90 struct stat stab;
91 int status;
92 pid_t pid;
93
94 if (!reasmbuf)
95 reasmbuf = dpkg_db_get_path(REASSEMBLETMP);
96 if (unlink(reasmbuf) && errno != ENOENT)
97 ohshite(_("error ensuring '%.250s' doesn't exist"), reasmbuf);
98
99 push_cleanup(cu_pathname, ~0, NULL, 0, 1, (void *)reasmbuf);
100
101 pid = subproc_fork();
102 if (!pid) {
103 execlp(SPLITTER, SPLITTER, "-Qao", reasmbuf, *filename, NULL);
104 ohshite(_("unable to execute %s (%s)"),
105 _("split package reassembly"), SPLITTER);
106 }
107 status = subproc_reap(pid, SPLITTER, SUBPROC_RETERROR);
108 switch (status) {
109 case 0:
110 /* It was a part - is it complete? */
111 if (!stat(reasmbuf, &stab)) {
112 /* Yes. */
113 *filename = reasmbuf;
114 *pfilename = _("reassembled package file");
115 break;
116 } else if (errno == ENOENT) {
117 /* No. That's it, we skip it. */
118 return false;
119 }
120 case 1:
121 /* No, it wasn't a part. */
122 break;
123 default:
124 ohshit(_("subprocess %s returned error exit status %d"), SPLITTER, status);
125 }
126
127 return true;
128}
129
130static void
131deb_verify(const char *filename)
132{
133 pid_t pid;
134
135 /* We have to check on every unpack, in case the debsig-verify package
136 * gets installed or removed. */
137 if (!find_command(DEBSIGVERIFY))
138 return;
139
140 printf(_("Authenticating %s ...\n"), filename);
141 fflush(stdout);
142 pid = subproc_fork();
143 if (!pid) {
144 execlp(DEBSIGVERIFY, DEBSIGVERIFY, "-q", filename, NULL);
145 ohshite(_("unable to execute %s (%s)"),
146 _("package signature verification"), DEBSIGVERIFY);
147 } else {
148 int status;
149
150 status = subproc_reap(pid, "debsig-verify", SUBPROC_NOCHECK);
151 if (!(WIFEXITED(status) && WEXITSTATUS(status) == 0)) {
152 if (!fc_badverify)
153 ohshit(_("verification on package %s failed!"), filename);
154 else
155 notice(_("verification on package %s failed; "
156 "but installing anyway as you requested"), filename);
157 } else {
158 printf(_("passed\n"));
159 }
160 }
161}
162
163static char *
164get_control_dir(char *cidir)
165{
166 if (f_noact) {
167 char *tmpdir;
168
169 tmpdir = mkdtemp(path_make_temp_template("dpkg"));
170 if (tmpdir == NULL)
171 ohshite(_("unable to create temporary directory"));
172
173 cidir = m_realloc(cidir, strlen(tmpdir) + MAXCONTROLFILENAME + 10);
174
175 strcpy(cidir, tmpdir);
176
177 free(tmpdir);
178 } else {
179 const char *admindir;
180
181 admindir = dpkg_db_get_dir();
182
183 /* The admindir length is always constant on a dpkg execution run. */
184 if (cidir == NULL)
185 cidir = m_malloc(strlen(admindir) + sizeof(CONTROLDIRTMP) +
186 MAXCONTROLFILENAME + 10);
187
188 /* We want it to be on the same filesystem so that we can
189 * use rename(2) to install the postinst &c. */
190 strcpy(cidir, admindir);
191 strcat(cidir, "/" CONTROLDIRTMP);
192
193 /* Make sure the control information directory is empty. */
194 path_remove_tree(cidir);
195 }
196
197 strcat(cidir, "/");
198
199 return cidir;
200}
201
202static void
203pkg_check_depcon(struct pkginfo *pkg, const char *pfilename)
204{
205 struct dependency *dsearch;
206 struct deppossi *psearch;
207 struct pkginfo *fixbytrigaw;
208 static struct varbuf depprobwhy;
209
210 /* Check if anything is installed that we conflict with, or not installed
211 * that we need. */
212 pkg->clientdata->istobe = PKG_ISTOBE_INSTALLNEW;
213
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);
232 }
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!"));
255 }
256 }
257 }
258 }
259
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;
264
265 check_conflict(psearch->up, pkg, pfilename);
266 }
267}
268
269static void
270pkg_deconfigure_others(struct pkginfo *pkg)
271{
272 struct pkg_deconf_list *deconpil;
273
274 for (deconpil = deconfigure; deconpil; deconpil = deconpil->next) {
275 struct pkginfo *removing = deconpil->pkg_removal;
276
277 if (removing)
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
284 printf(_("De-configuring %s (%s) ...\n"),
285 pkg_name(deconpil->pkg, pnaw_nonambig),
286 versiondescribe(&deconpil->pkg->installed.version, vdew_nonambig));
287
288 trig_activate_packageprocessing(deconpil->pkg);
289 pkg_set_status(deconpil->pkg, PKG_STAT_HALFCONFIGURED);
290 modstatdb_note(deconpil->pkg);
291
292 /* This means that we *either* go and run postinst abort-deconfigure,
293 * *or* queue the package for later configure processing, depending
294 * on which error cleanup route gets taken. */
295 push_cleanup(cu_prermdeconfigure, ~ehflag_normaltidy,
296 ok_prermdeconfigure, ehflag_normaltidy,
297 3, (void *)deconpil->pkg, (void *)removing, (void *)pkg);
298
299 if (removing) {
300 maintscript_installed(deconpil->pkg, PRERMFILE, "pre-removal",
301 "deconfigure", "in-favour",
302 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
303 versiondescribe(&pkg->available.version,
304 vdew_nonambig),
305 "removing",
306 pkg_name(removing, pnaw_nonambig),
307 versiondescribe(&removing->installed.version,
308 vdew_nonambig),
309 NULL);
310 } else {
311 maintscript_installed(deconpil->pkg, PRERMFILE, "pre-removal",
312 "deconfigure", "in-favour",
313 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
314 versiondescribe(&pkg->available.version,
315 vdew_nonambig),
316 NULL);
317 }
318 }
319}
320
321/**
322 * Read the conffiles, and copy the hashes across.
323 */
324static void
325deb_parse_conffiles(struct pkginfo *pkg, const char *control_conffiles,
326 struct filenamenode_queue *newconffiles)
327{
328 FILE *conff;
329 char conffilenamebuf[MAXCONFFILENAME];
330
331 conff = fopen(control_conffiles, "r");
332 if (conff == NULL) {
333 if (errno == ENOENT)
334 return;
335 ohshite(_("error trying to open %.250s"), control_conffiles);
336 }
337
338 push_cleanup(cu_closestream, ehflag_bombout, NULL, 0, 1, conff);
339
340 while (fgets(conffilenamebuf, MAXCONFFILENAME - 2, conff)) {
341 struct pkginfo *otherpkg;
342 struct filepackages_iterator *iter;
343 struct filenamenode *namenode;
344 struct fileinlist *newconff;
345 struct conffile *searchconff;
346 char *p;
347
348 p = conffilenamebuf + strlen(conffilenamebuf);
349 assert(p != conffilenamebuf);
350 if (p[-1] != '\n')
351 ohshit(_("conffile name '%s' is too long, or missing final newline"),
352 conffilenamebuf);
353 while (p > conffilenamebuf && c_isspace(p[-1]))
354 --p;
355 if (p == conffilenamebuf)
356 continue;
357 *p = '\0';
358
359 namenode = findnamenode(conffilenamebuf, 0);
360 namenode->oldhash = NEWCONFFILEFLAG;
361 newconff = tar_filenamenode_queue_push(newconffiles, namenode);
362
363 /*
364 * Let's see if any packages have this file.
365 *
366 * If they do we check to see if they listed it as a conffile,
367 * and if they did we copy the hash across. Since (for plain
368 * file conffiles, which is the only kind we are supposed to
369 * have) there will only be one package which ‘has’ the file,
370 * this will usually mean we only look in the package which
371 * we are installing now.
372 *
373 * The ‘conffiles’ data in the status file is ignored when a
374 * package is not also listed in the file ownership database as
375 * having that file. If several packages are listed as owning
376 * the file we pick one at random.
377 */
378 searchconff = NULL;
379
380 iter = filepackages_iter_new(newconff->namenode);
381 while ((otherpkg = filepackages_iter_next(iter))) {
382 debug(dbg_conffdetail,
383 "process_archive conffile '%s' in package %s - conff ?",
384 newconff->namenode->name, pkg_name(otherpkg, pnaw_always));
385 for (searchconff = otherpkg->installed.conffiles;
386 searchconff && strcmp(newconff->namenode->name, searchconff->name);
387 searchconff = searchconff->next)
388 debug(dbg_conffdetail,
389 "process_archive conffile '%s' in package %s - conff ? not '%s'",
390 newconff->namenode->name, pkg_name(otherpkg, pnaw_always),
391 searchconff->name);
392 if (searchconff) {
393 debug(dbg_conff,
394 "process_archive conffile '%s' package=%s %s hash=%s",
395 newconff->namenode->name, pkg_name(otherpkg, pnaw_always),
396 otherpkg == pkg ? "same" : "different!",
397 searchconff->hash);
398 if (otherpkg == pkg)
399 break;
400 }
401 }
402 filepackages_iter_free(iter);
403
404 if (searchconff) {
405 /* We don't copy ‘obsolete’; it's not obsolete in the new package. */
406 newconff->namenode->oldhash = searchconff->hash;
407 } else {
408 debug(dbg_conff, "process_archive conffile '%s' no package, no hash",
409 newconff->namenode->name);
410 }
411 newconff->namenode->flags |= fnnf_new_conff;
412 }
413
414 if (ferror(conff))
415 ohshite(_("read error in %.250s"), control_conffiles);
416 pop_cleanup(ehflag_normaltidy); /* conff = fopen() */
417 if (fclose(conff))
418 ohshite(_("error closing %.250s"), control_conffiles);
419}
420
421static struct pkg_queue conflictors = PKG_QUEUE_INIT;
422
423void
424enqueue_conflictor(struct pkginfo *pkg)
425{
426 pkg_queue_push(&conflictors, pkg);
427}
428
429static void
430pkg_infodb_remove_file(const char *filename, const char *filetype)
431{
432 if (unlink(filename))
433 ohshite(_("unable to delete control info file '%.250s'"), filename);
434
435 debug(dbg_scripts, "removal_bulk info unlinked %s", filename);
436}
437
438static struct match_node *match_head = NULL;
439
440static void
441pkg_infodb_update_file(const char *filename, const char *filetype)
442{
443 if (strlen(filetype) > MAXCONTROLFILENAME)
444 ohshit(_("old version of package has overly-long info file name starting '%.250s'"),
445 filename);
446
447 /* We do the list separately. */
448 if (strcmp(filetype, LISTFILE) == 0)
449 return;
450
451 /* We keep files to rename in a list as doing the rename immediately
452 * might influence the current readdir(), the just renamed file might
453 * be returned a second time as it's actually a new file from the
454 * point of view of the filesystem. */
455 match_head = match_node_new(filename, filetype, match_head);
456}
457
458static void
459pkg_infodb_update(struct pkginfo *pkg, char *cidir, char *cidirrest)
460{
461 struct match_node *match_node;
462 DIR *dsd;
463 struct dirent *de;
464
465 /* Deallocate the match list in case we aborted previously. */
466 while ((match_node = match_head)) {
467 match_head = match_node->next;
468 match_node_free(match_node);
469 }
470
471 pkg_infodb_foreach(pkg, &pkg->available, pkg_infodb_update_file);
472
473 while ((match_node = match_head)) {
474 strcpy(cidirrest, match_node->filetype);
475
476 if (!rename(cidir, match_node->filename)) {
477 debug(dbg_scripts, "process_archive info installed %s as %s",
478 cidir, match_node->filename);
479 } else if (errno == ENOENT) {
480 /* Right, no new version. */
481 if (unlink(match_node->filename))
482 ohshite(_("unable to remove obsolete info file '%.250s'"),
483 match_node->filename);
484 debug(dbg_scripts, "process_archive info unlinked %s",
485 match_node->filename);
486 } else {
487 ohshite(_("unable to install (supposed) new info file '%.250s'"), cidir);
488 }
489 match_head = match_node->next;
490 match_node_free(match_node);
491 }
492
493 /* The control directory itself. */
494 cidirrest[0] = '\0';
495 dsd = opendir(cidir);
496 if (!dsd)
497 ohshite(_("unable to open temp control directory"));
498 push_cleanup(cu_closedir, ~0, NULL, 0, 1, (void *)dsd);
499 while ((de = readdir(dsd))) {
500 const char *newinfofilename;
501
502 if (strchr(de->d_name, '.')) {
503 debug(dbg_scripts, "process_archive tmp.ci script/file '%s' contains dot",
504 de->d_name);
505 continue;
506 }
507 if (strlen(de->d_name) > MAXCONTROLFILENAME)
508 ohshit(_("package contains overly-long control info file name (starting '%.50s')"),
509 de->d_name);
510
511 strcpy(cidirrest, de->d_name);
512
513 /* First we check it's not a directory. */
514 if (rmdir(cidir) == 0)
515 ohshit(_("package control info contained directory '%.250s'"), cidir);
516 else if (errno != ENOTDIR)
517 ohshite(_("package control info rmdir of '%.250s' didn't say not a dir"),
518 de->d_name);
519
520 /* Ignore the control file. */
521 if (strcmp(de->d_name, CONTROLFILE) == 0) {
522 debug(dbg_scripts, "process_archive tmp.ci script/file '%s' is control",
523 cidir);
524 continue;
525 }
526 if (strcmp(de->d_name, LISTFILE) == 0) {
527 warning(_("package %s contained list as info file"),
528 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
529 continue;
530 }
531
532 /* Right, install it */
533 newinfofilename = pkg_infodb_get_file(pkg, &pkg->available, de->d_name);
534 if (rename(cidir, newinfofilename))
535 ohshite(_("unable to install new info file '%.250s' as '%.250s'"),
536 cidir, newinfofilename);
537
538 debug(dbg_scripts,
539 "process_archive tmp.ci script/file '%s' installed as '%s'",
540 cidir, newinfofilename);
541 }
542 pop_cleanup(ehflag_normaltidy); /* closedir */
543
544 /* If the old and new versions use a different infodb layout, get rid
545 * of the files using the old layout. */
546 if (pkg->installed.multiarch != pkg->available.multiarch &&
547 (pkg->installed.multiarch == PKG_MULTIARCH_SAME ||
548 pkg->available.multiarch == PKG_MULTIARCH_SAME)) {
549 debug(dbg_scripts,
550 "process_archive remove old info files after db layout switch");
551 pkg_infodb_foreach(pkg, &pkg->installed, pkg_infodb_remove_file);
552 }
553
554 dir_sync_path(pkg_infodb_get_dir());
555}
556
557static void
558pkg_remove_old_files(struct pkginfo *pkg,
559 struct filenamenode_queue *newfiles_queue,
560 struct filenamenode_queue *newconffiles)
561{
562 struct reversefilelistiter rev_iter;
563 struct filenamenode *namenode;
564 struct stat stab, oldfs;
565
566 reversefilelist_init(&rev_iter, pkg->clientdata->files);
567
568 while ((namenode = reversefilelist_next(&rev_iter))) {
569 struct filenamenode *usenode;
570
571 if ((namenode->flags & fnnf_new_conff) ||
572 (namenode->flags & fnnf_new_inarchive))
573 continue;
574
575 usenode = namenodetouse(namenode, pkg, &pkg->installed);
576
577 varbuf_rollback(&fnamevb, &fname_state);
578 varbuf_add_str(&fnamevb, usenode->name);
579 varbuf_end_str(&fnamevb);
580
581 if (!stat(namenode->name, &stab) && S_ISDIR(stab.st_mode)) {
582 debug(dbg_eachfiledetail, "process_archive: %s is a directory",
583 namenode->name);
584 if (dir_is_used_by_others(namenode, pkg))
585 continue;
586 }
587
588 if (lstat(fnamevb.buf, &oldfs)) {
589 if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
590 warning(_("could not stat old file '%.250s' so not deleting it: %s"),
591 fnamevb.buf, strerror(errno));
592 continue;
593 }
594 if (S_ISDIR(oldfs.st_mode)) {
595 trig_path_activate(usenode, pkg);
596
597 /* Do not try to remove the root directory. */
598 if (strcmp(usenode->name, "/.") == 0)
599 continue;
600
601 if (rmdir(fnamevb.buf)) {
602 warning(_("unable to delete old directory '%.250s': %s"),
603 namenode->name, strerror(errno));
604 } else if ((namenode->flags & fnnf_old_conff)) {
605 warning(_("old conffile '%.250s' was an empty directory "
606 "(and has now been deleted)"), namenode->name);
607 }
608 } else {
609 struct fileinlist *sameas = NULL;
610 struct fileinlist *cfile;
611 static struct stat empty_stat;
612 struct varbuf cfilename = VARBUF_INIT;
613
614 /*
615 * Ok, it's an old file, but is it really not in the new package?
616 * It might be known by a different name because of symlinks.
617 *
618 * We need to check to make sure, so we stat the file, then compare
619 * it to the new list. If we find a dev/inode match, we assume they
620 * are the same file, and leave it alone. NOTE: we don't check in
621 * other packages for sanity reasons (we don't want to stat _all_
622 * the files on the system).
623 *
624 * We run down the list of _new_ files in this package. This keeps
625 * the process a little leaner. We are only worried about new ones
626 * since ones that stayed the same don't really apply here.
627 */
628
629 /* If we can't stat the old or new file, or it's a directory,
630 * we leave it up to the normal code. */
631 debug(dbg_eachfile, "process_archive: checking %s for same files on "
632 "upgrade/downgrade", fnamevb.buf);
633
634 for (cfile = newfiles_queue->head; cfile; cfile = cfile->next) {
635 /* If the file has been filtered then treat it as if it didn't exist
636 * on the file system. */
637 if (cfile->namenode->flags & fnnf_filtered)
638 continue;
639
640 if (!cfile->namenode->filestat) {
641 struct stat tmp_stat;
642
643 varbuf_reset(&cfilename);
644 varbuf_add_str(&cfilename, instdir);
645 varbuf_add_str(&cfilename, cfile->namenode->name);
646 varbuf_end_str(&cfilename);
647
648 if (lstat(cfilename.buf, &tmp_stat) == 0) {
649 cfile->namenode->filestat = nfmalloc(sizeof(struct stat));
650 memcpy(cfile->namenode->filestat, &tmp_stat, sizeof(struct stat));
651 } else {
652 if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
653 ohshite(_("unable to stat other new file '%.250s'"),
654 cfile->namenode->name);
655 cfile->namenode->filestat = &empty_stat;
656 continue;
657 }
658 }
659
660 if (cfile->namenode->filestat == &empty_stat)
661 continue;
662
663 if (oldfs.st_dev == cfile->namenode->filestat->st_dev &&
664 oldfs.st_ino == cfile->namenode->filestat->st_ino) {
665 if (sameas)
666 warning(_("old file '%.250s' is the same as several new files! "
667 "(both '%.250s' and '%.250s')"), fnamevb.buf,
668 sameas->namenode->name, cfile->namenode->name);
669 sameas = cfile;
670 debug(dbg_eachfile, "process_archive: not removing %s, "
671 "since it matches %s", fnamevb.buf, cfile->namenode->name);
672 }
673 }
674
675 varbuf_destroy(&cfilename);
676
677 if ((namenode->flags & fnnf_old_conff)) {
678 if (sameas) {
679 if (sameas->namenode->flags & fnnf_new_conff) {
680 if (strcmp(sameas->namenode->oldhash, NEWCONFFILEFLAG) == 0) {
681 sameas->namenode->oldhash = namenode->oldhash;
682 debug(dbg_eachfile, "process_archive: old conff %s "
683 "is same as new conff %s, copying hash",
684 namenode->name, sameas->namenode->name);
685 } else {
686 debug(dbg_eachfile, "process_archive: old conff %s "
687 "is same as new conff %s but latter already has hash",
688 namenode->name, sameas->namenode->name);
689 }
690 }
691 } else {
692 debug(dbg_eachfile, "process_archive: old conff %s "
693 "is disappearing", namenode->name);
694 namenode->flags |= fnnf_obs_conff;
695 tar_filenamenode_queue_push(newconffiles, namenode);
696 tar_filenamenode_queue_push(newfiles_queue, namenode);
697 }
698 continue;
699 }
700
701 if (sameas)
702 continue;
703
704 trig_path_activate(usenode, pkg);
705
706 if (secure_unlink_statted(fnamevb.buf, &oldfs)) {
707 warning(_("unable to securely remove old file '%.250s': %s"),
708 namenode->name, strerror(errno));
709 }
710 } /* !S_ISDIR */
711 }
712}
713
714static void
715pkg_update_fields(struct pkginfo *pkg, struct filenamenode_queue *newconffiles)
716{
717 struct dependency *newdeplist, **newdeplistlastp;
718 struct dependency *newdep, *dep;
719 struct deppossi **newpossilastp, *possi, *newpossi;
720 struct conffile **iconffileslastp, *newiconff;
721 struct fileinlist *cfile;
722
723 /* The dependencies are the most difficult. We have to build
724 * a whole new forward dependency tree. At least the reverse
725 * links (linking our deppossi's into the reverse chains)
726 * can be done by copy_dependency_links. */
727 newdeplist = NULL;
728 newdeplistlastp = &newdeplist;
729 for (dep = pkg->available.depends; dep; dep = dep->next) {
730 newdep = nfmalloc(sizeof(struct dependency));
731 newdep->up = pkg;
732 newdep->next = NULL;
733 newdep->list = NULL;
734 newpossilastp = &newdep->list;
735
736 for (possi = dep->list; possi; possi = possi->next) {
737 newpossi = nfmalloc(sizeof(struct deppossi));
738 newpossi->up = newdep;
739 newpossi->ed = possi->ed;
740 newpossi->next = NULL;
741 newpossi->rev_next = newpossi->rev_prev = NULL;
742 newpossi->arch_is_implicit = possi->arch_is_implicit;
743 newpossi->arch = possi->arch;
744 newpossi->verrel = possi->verrel;
745 if (possi->verrel != DPKG_RELATION_NONE)
746 newpossi->version = possi->version;
747 else
748 dpkg_version_blank(&newpossi->version);
749 newpossi->cyclebreak = false;
750 *newpossilastp = newpossi;
751 newpossilastp = &newpossi->next;
752 }
753 newdep->type = dep->type;
754 *newdeplistlastp = newdep;
755 newdeplistlastp = &newdep->next;
756 }
757
758 /* Right, now we've replicated the forward tree, we
759 * get copy_dependency_links to remove all the old dependency
760 * structures from the reverse links and add the new dependency
761 * structures in instead. It also copies the new dependency
762 * structure pointer for this package into the right field. */
763 copy_dependency_links(pkg, &pkg->installed.depends, newdeplist, 0);
764
765 /* We copy the text fields. */
766 pkg->installed.essential = pkg->available.essential;
767 pkg->installed.multiarch = pkg->available.multiarch;
768 pkg->installed.description = pkg->available.description;
769 pkg->installed.maintainer = pkg->available.maintainer;
770 pkg->installed.source = pkg->available.source;
771 pkg->installed.arch = pkg->available.arch;
772 pkg->installed.pkgname_archqual = pkg->available.pkgname_archqual;
773 pkg->installed.installedsize = pkg->available.installedsize;
774 pkg->installed.version = pkg->available.version;
775 pkg->installed.origin = pkg->available.origin;
776 pkg->installed.bugs = pkg->available.bugs;
777
778 /* We have to generate our own conffiles structure. */
779 pkg->installed.conffiles = NULL;
780 iconffileslastp = &pkg->installed.conffiles;
781 for (cfile = newconffiles->head; cfile; cfile = cfile->next) {
782 newiconff = nfmalloc(sizeof(struct conffile));
783 newiconff->next = NULL;
784 newiconff->name = nfstrsave(cfile->namenode->name);
785 newiconff->hash = nfstrsave(cfile->namenode->oldhash);
786 newiconff->obsolete = !!(cfile->namenode->flags & fnnf_obs_conff);
787 *iconffileslastp = newiconff;
788 iconffileslastp = &newiconff->next;
789 }
790
791 /* We can just copy the arbitrary fields list, because it is
792 * never even rearranged. Phew! */
793 pkg->installed.arbs = pkg->available.arbs;
794}
795
796static void
797pkg_disappear(struct pkginfo *pkg, struct pkginfo *infavour)
798{
799 printf(_("(Noting disappearance of %s, which has been completely replaced.)\n"),
800 pkg_name(pkg, pnaw_nonambig));
801 log_action("disappear", pkg, &pkg->installed);
802 debug(dbg_general, "pkg_disappear disappearing %s",
803 pkg_name(pkg, pnaw_always));
804
805 trig_activate_packageprocessing(pkg);
806 maintscript_installed(pkg, POSTRMFILE,
807 "post-removal script (for disappearance)",
808 "disappear",
809 pkgbin_name(infavour, &infavour->available,
810 pnaw_nonambig),
811 versiondescribe(&infavour->available.version,
812 vdew_nonambig),
813 NULL);
814
815 /* OK, now we delete all the stuff in the ‘info’ directory ... */
816 debug(dbg_general, "pkg_disappear cleaning info directory");
817 pkg_infodb_foreach(pkg, &pkg->installed, pkg_infodb_remove_file);
818 dir_sync_path(pkg_infodb_get_dir());
819
820 pkg_set_status(pkg, PKG_STAT_NOTINSTALLED);
821 pkg_set_want(pkg, PKG_WANT_UNKNOWN);
822 pkg_reset_eflags(pkg);
823
824 dpkg_version_blank(&pkg->configversion);
825 pkgbin_blank(&pkg->installed);
826
827 pkg->clientdata->fileslistvalid = false;
828
829 modstatdb_note(pkg);
830}
831
832static void
833pkg_disappear_others(struct pkginfo *pkg)
834{
835 struct pkgiterator *iter;
836 struct pkginfo *otherpkg;
837 struct fileinlist *cfile;
838 struct deppossi *pdep;
839 struct dependency *providecheck;
840 struct varbuf depprobwhy = VARBUF_INIT;
841
842 iter = pkg_db_iter_new();
843 while ((otherpkg = pkg_db_iter_next_pkg(iter)) != NULL) {
844 ensure_package_clientdata(otherpkg);
845
846 if (otherpkg == pkg ||
847 otherpkg->status == PKG_STAT_NOTINSTALLED ||
848 otherpkg->status == PKG_STAT_CONFIGFILES ||
849 otherpkg->clientdata->istobe == PKG_ISTOBE_REMOVE ||
850 !otherpkg->clientdata->files)
851 continue;
852
853 /* Do not try to disappear other packages from the same set
854 * if they are Multi-Arch: same */
855 if (pkg->installed.multiarch == PKG_MULTIARCH_SAME &&
856 otherpkg->installed.multiarch == PKG_MULTIARCH_SAME &&
857 otherpkg->set == pkg->set)
858 continue;
859
860 debug(dbg_veryverbose, "process_archive checking disappearance %s",
861 pkg_name(otherpkg, pnaw_always));
862 assert(otherpkg->clientdata->istobe == PKG_ISTOBE_NORMAL ||
863 otherpkg->clientdata->istobe == PKG_ISTOBE_DECONFIGURE);
864
865 for (cfile = otherpkg->clientdata->files;
866 cfile && strcmp(cfile->namenode->name, "/.") == 0;
867 cfile = cfile->next);
868 if (!cfile) {
869 debug(dbg_stupidlyverbose, "process_archive no non-root, no disappear");
870 continue;
871 }
872 for (cfile = otherpkg->clientdata->files;
873 cfile && !filesavespackage(cfile, otherpkg, pkg);
874 cfile = cfile->next);
875 if (cfile)
876 continue;
877
878 /* So dependency things will give right answers ... */
879 otherpkg->clientdata->istobe = PKG_ISTOBE_REMOVE;
880 debug(dbg_veryverbose, "process_archive disappear checking dependencies");
881 for (pdep = otherpkg->set->depended.installed;
882 pdep;
883 pdep = pdep->rev_next) {
884 if (pdep->up->type != dep_depends &&
885 pdep->up->type != dep_predepends &&
886 pdep->up->type != dep_recommends)
887 continue;
888
889 if (depisok(pdep->up, &depprobwhy, NULL, NULL, false))
890 continue;
891
892 varbuf_end_str(&depprobwhy);
893 debug(dbg_veryverbose,"process_archive cannot disappear: %s",
894 depprobwhy.buf);
895 break;
896 }
897 if (!pdep) {
898 /* If we haven't found a reason not to yet, let's look some more. */
899 for (providecheck = otherpkg->installed.depends;
900 providecheck;
901 providecheck = providecheck->next) {
902 if (providecheck->type != dep_provides)
903 continue;
904
905 for (pdep = providecheck->list->ed->depended.installed;
906 pdep;
907 pdep = pdep->rev_next) {
908 if (pdep->up->type != dep_depends &&
909 pdep->up->type != dep_predepends &&
910 pdep->up->type != dep_recommends)
911 continue;
912
913 if (depisok(pdep->up, &depprobwhy, NULL, NULL, false))
914 continue;
915
916 varbuf_end_str(&depprobwhy);
917 debug(dbg_veryverbose,
918 "process_archive cannot disappear (provides %s): %s",
919 providecheck->list->ed->name, depprobwhy.buf);
920 goto break_from_both_loops_at_once;
921 }
922 }
923 break_from_both_loops_at_once:;
924 }
925 otherpkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
926 if (pdep)
927 continue;
928
929 /* No, we're disappearing it. This is the wrong time to go and
930 * run maintainer scripts and things, as we can't back out. But
931 * what can we do ? It has to be run this late. */
932 pkg_disappear(otherpkg, pkg);
933 } /* while (otherpkg= ... */
934 pkg_db_iter_free(iter);
935}
936
937/**
938 * Check if all instances of a pkgset are getting in sync.
939 *
940 * If that's the case, the extraction is going to ensure consistency
941 * of shared files.
942 */
943static bool
944pkgset_getting_in_sync(struct pkginfo *pkg)
945{
946 struct pkginfo *otherpkg;
947
948 for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
949 if (otherpkg == pkg)
950 continue;
951 if (otherpkg->status <= PKG_STAT_CONFIGFILES)
952 continue;
953 if (dpkg_version_compare(&pkg->available.version,
954 &otherpkg->installed.version)) {
955 return false;
956 }
957 }
958
959 return true;
960}
961
962static void
963pkg_remove_files_from_others(struct pkginfo *pkg, struct fileinlist *newfileslist)
964{
965 struct fileinlist *cfile;
966 struct pkginfo *otherpkg;
967
968 for (cfile = newfileslist; cfile; cfile = cfile->next) {
969 struct filepackages_iterator *iter;
970 struct pkgset *divpkgset;
971
972 if (!(cfile->namenode->flags & fnnf_elide_other_lists))
973 continue;
974
975 if (cfile->namenode->divert && cfile->namenode->divert->useinstead) {
976 divpkgset = cfile->namenode->divert->pkgset;
977 if (divpkgset == pkg->set) {
978 debug(dbg_eachfile,
979 "process_archive not overwriting any '%s' (overriding, '%s')",
980 cfile->namenode->name, cfile->namenode->divert->useinstead->name);
981 continue;
982 } else {
983 debug(dbg_eachfile,
984 "process_archive looking for overwriting '%s' (overridden by %s)",
985 cfile->namenode->name, divpkgset ? divpkgset->name : "<local>");
986 }
987 } else {
988 divpkgset = NULL;
989 debug(dbg_eachfile, "process_archive looking for overwriting '%s'",
990 cfile->namenode->name);
991 }
992
993 iter = filepackages_iter_new(cfile->namenode);
994 while ((otherpkg = filepackages_iter_next(iter))) {
995 debug(dbg_eachfiledetail, "process_archive ... found in %s",
996 pkg_name(otherpkg, pnaw_always));
997
998 /* A pkgset can share files between instances, so there's no point
999 * in rewriting the file that's already in place. */
1000 if (otherpkg->set == pkg->set)
1001 continue;
1002
1003 if (otherpkg->set == divpkgset) {
1004 debug(dbg_eachfiledetail, "process_archive ... diverted, skipping");
1005 continue;
1006 }
1007
1008 if (cfile->namenode->flags & fnnf_new_conff)
1009 conffile_mark_obsolete(otherpkg, cfile->namenode);
1010
1011 /* If !fileslistvalid then it's one of the disappeared packages above
1012 * or we have already updated the files list file, and we don't bother
1013 * with it here, clearly. */
1014 if (!otherpkg->clientdata->fileslistvalid)
1015 continue;
1016
1017 /* Found one. We delete the list entry for this file,
1018 * (and any others in the same package) and then mark the package
1019 * as requiring a reread. */
1020 write_filelist_except(otherpkg, &otherpkg->installed,
1021 otherpkg->clientdata->files, fnnf_elide_other_lists);
1022 ensure_package_clientdata(otherpkg);
1023 debug(dbg_veryverbose, "process_archive overwrote from %s",
1024 pkg_name(otherpkg, pnaw_always));
1025 }
1026 filepackages_iter_free(iter);
1027 }
1028}
1029
1030static void
1031pkg_remove_backup_files(struct pkginfo *pkg, struct fileinlist *newfileslist)
1032{
1033 struct fileinlist *cfile;
1034
1035 for (cfile = newfileslist; cfile; cfile = cfile->next) {
1036 struct filenamenode *usenode;
1037
1038 if (cfile->namenode->flags & fnnf_new_conff)
1039 continue;
1040
1041 usenode = namenodetouse(cfile->namenode, pkg, &pkg->installed);
1042
1043 /* Do not try to remove backups for the root directory. */
1044 if (strcmp(usenode->name, "/.") == 0)
1045 continue;
1046
1047 varbuf_rollback(&fnametmpvb, &fname_state);
1048 varbuf_add_str(&fnametmpvb, usenode->name);
1049 varbuf_add_str(&fnametmpvb, DPKGTEMPEXT);
1050 varbuf_end_str(&fnametmpvb);
1051 path_remove_tree(fnametmpvb.buf);
1052 }
1053}
1054
1055void process_archive(const char *filename) {
1056 static const struct tar_operations tf = {
1057 .read = tarfileread,
1058 .extract_file = tarobject,
1059 .link = tarobject,
1060 .symlink = tarobject,
1061 .mkdir = tarobject,
1062 .mknod = tarobject,
1063 };
1064
1065 /* These need to be static so that we can pass their addresses to
1066 * push_cleanup as arguments to the cu_xxx routines; if an error occurs
1067 * we unwind the stack before processing the cleanup list, and these
1068 * variables had better still exist ... */
1069 static int p1[2];
1070 static enum pkgstatus oldversionstatus;
1071 static struct tarcontext tc;
1072
1073 struct dpkg_error err;
1074 enum parsedbflags parsedb_flags;
1075 int rc;
1076 pid_t pid;
1077 struct pkginfo *pkg, *otherpkg;
1078 struct pkg_list *conflictor_iter;
1079 char *cidir = NULL;
1080 char *cidirrest;
1081 char *psize;
1082 const char *pfilename;
1083 struct filenamenode_queue newconffiles, newfiles_queue;
1084 struct stat stab;
1085
1086 cleanup_pkg_failed= cleanup_conflictor_failed= 0;
1087
1088 pfilename = summarize_filename(filename);
1089
1090 if (stat(filename, &stab))
1091 ohshite(_("cannot access archive '%s'"), filename);
1092
1093 /* We can't ‘tentatively-reassemble’ packages. */
1094 if (!f_noact) {
1095 if (!deb_reassemble(&filename, &pfilename))
1096 return;
1097 }
1098
1099 /* Verify the package. */
1100 if (!f_nodebsig)
1101 deb_verify(filename);
1102
1103 /* Get the control information directory. */
1104 cidir = get_control_dir(cidir);
1105 cidirrest = cidir + strlen(cidir);
1106 push_cleanup(cu_cidir, ~0, NULL, 0, 2, (void *)cidir, (void *)cidirrest);
1107
1108 pid = subproc_fork();
1109 if (pid == 0) {
1110 cidirrest[-1] = '\0';
1111 execlp(BACKEND, BACKEND, "--control", filename, cidir, NULL);
1112 ohshite(_("unable to execute %s (%s)"),
1113 _("package control information extraction"), BACKEND);
1114 }
1115 subproc_reap(pid, BACKEND " --control", 0);
1116
1117 /* We want to guarantee the extracted files are on the disk, so that the
1118 * subsequent renames to the info database do not end up with old or zero
1119 * length files in case of a system crash. As neither dpkg-deb nor tar do
1120 * explicit fsync()s, we have to do them here.
1121 * XXX: This could be avoided by switching to an internal tar extractor. */
1122 dir_sync_contents(cidir);
1123
1124 strcpy(cidirrest,CONTROLFILE);
1125
1126 if (cipaction->arg_int == act_avail)
1127 parsedb_flags = pdb_parse_available;
1128 else
1129 parsedb_flags = pdb_parse_binary;
1130 parsedb_flags |= pdb_ignorefiles;
1131 if (fc_badversion)
1132 parsedb_flags |= pdb_lax_version_parser;
1133
1134 parsedb(cidir, parsedb_flags, &pkg);
1135
1136 if (!pkg->files) {
1137 pkg->files= nfmalloc(sizeof(struct filedetails));
1138 pkg->files->next = NULL;
1139 pkg->files->name = pkg->files->msdosname = pkg->files->md5sum = NULL;
1140 }
1141 /* Always nfmalloc. Otherwise, we may overwrite some other field (like
1142 * md5sum). */
1143 psize = nfmalloc(30);
1144 sprintf(psize, "%jd", (intmax_t)stab.st_size);
1145 pkg->files->size = psize;
1146
1147 if (cipaction->arg_int == act_avail) {
1148 printf(_("Recorded info about %s from %s.\n"),
1149 pkgbin_name(pkg, &pkg->available, pnaw_nonambig), pfilename);
1150 pop_cleanup(ehflag_normaltidy);
1151 return;
1152 }
1153
1154 if (pkg->available.arch->type != DPKG_ARCH_ALL &&
1155 pkg->available.arch->type != DPKG_ARCH_NATIVE &&
1156 pkg->available.arch->type != DPKG_ARCH_FOREIGN)
1157 forcibleerr(fc_architecture,
1158 _("package architecture (%s) does not match system (%s)"),
1159 pkg->available.arch->name,
1160 dpkg_arch_get(DPKG_ARCH_NATIVE)->name);
1161
1162 clear_deconfigure_queue();
1163 clear_istobes();
1164
1165 if (wanttoinstall(pkg)) {
1166 pkg_set_want(pkg, PKG_WANT_INSTALL);
1167 } else {
1168 pop_cleanup(ehflag_normaltidy);
1169 return;
1170 }
1171
1172 /* Deconfigure other instances from a pkgset if they are not in sync. */
1173 for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
1174 if (otherpkg == pkg)
1175 continue;
1176 if (otherpkg->status <= PKG_STAT_HALFCONFIGURED)
1177 continue;
1178
1179 if (dpkg_version_compare(&pkg->available.version,
1180 &otherpkg->installed.version))
1181 enqueue_deconfigure(otherpkg, NULL);
1182 }
1183
1184 pkg_check_depcon(pkg, pfilename);
1185
1186 ensure_allinstfiles_available();
1187 filesdbinit();
1188 trig_file_interests_ensure();
1189
1190 printf(_("Preparing to unpack %s ...\n"), pfilename);
1191
1192 if (pkg->status != PKG_STAT_NOTINSTALLED &&
1193 pkg->status != PKG_STAT_CONFIGFILES) {
1194 log_action("upgrade", pkg, &pkg->installed);
1195 } else {
1196 log_action("install", pkg, &pkg->available);
1197 }
1198
1199 if (f_noact) {
1200 pop_cleanup(ehflag_normaltidy);
1201 return;
1202 }
1203
1204 /*
1205 * OK, we're going ahead.
1206 */
1207
1208 trig_activate_packageprocessing(pkg);
1209 strcpy(cidirrest, TRIGGERSCIFILE);
1210 trig_parse_ci(cidir, NULL, trig_cicb_statuschange_activate, pkg, &pkg->available);
1211
1212 /* Read the conffiles, and copy the hashes across. */
1213 newconffiles.head = NULL;
1214 newconffiles.tail = &newconffiles.head;
1215 push_cleanup(cu_fileslist, ~0, NULL, 0, 0);
1216 strcpy(cidirrest,CONFFILESFILE);
1217 deb_parse_conffiles(pkg, cidir, &newconffiles);
1218
1219 /* All the old conffiles are marked with a flag, so that we don't delete
1220 * them if they seem to disappear completely. */
1221 pkg_conffiles_mark_old(pkg);
1222 for (conflictor_iter = conflictors.head;
1223 conflictor_iter;
1224 conflictor_iter = conflictor_iter->next)
1225 pkg_conffiles_mark_old(conflictor_iter->pkg);
1226
1227 oldversionstatus= pkg->status;
1228
1229 assert(oldversionstatus <= PKG_STAT_INSTALLED);
1230 debug(dbg_general,"process_archive oldversionstatus=%s",
1231 statusstrings[oldversionstatus]);
1232
1233 if (oldversionstatus == PKG_STAT_HALFCONFIGURED ||
1234 oldversionstatus == PKG_STAT_TRIGGERSAWAITED ||
1235 oldversionstatus == PKG_STAT_TRIGGERSPENDING ||
1236 oldversionstatus == PKG_STAT_INSTALLED) {
1237 pkg_set_eflags(pkg, PKG_EFLAG_REINSTREQ);
1238 pkg_set_status(pkg, PKG_STAT_HALFCONFIGURED);
1239 modstatdb_note(pkg);
1240 push_cleanup(cu_prermupgrade, ~ehflag_normaltidy, NULL, 0, 1, (void *)pkg);
1241 if (dpkg_version_compare(&pkg->available.version,
1242 &pkg->installed.version) >= 0)
1243 /* Upgrade or reinstall. */
1244 maintscript_fallback(pkg, PRERMFILE, "pre-removal", cidir, cidirrest,
1245 "upgrade", "failed-upgrade");
1246 else /* Downgrade => no fallback */
1247 maintscript_installed(pkg, PRERMFILE, "pre-removal",
1248 "upgrade",
1249 versiondescribe(&pkg->available.version,
1250 vdew_nonambig),
1251 NULL);
1252 pkg_set_status(pkg, PKG_STAT_UNPACKED);
1253 oldversionstatus = PKG_STAT_UNPACKED;
1254 modstatdb_note(pkg);
1255 }
1256
1257 pkg_deconfigure_others(pkg);
1258
1259 for (conflictor_iter = conflictors.head;
1260 conflictor_iter;
1261 conflictor_iter = conflictor_iter->next) {
1262 struct pkginfo *conflictor = conflictor_iter->pkg;
1263
1264 if (!(conflictor->status == PKG_STAT_HALFCONFIGURED ||
1265 conflictor->status == PKG_STAT_TRIGGERSAWAITED ||
1266 conflictor->status == PKG_STAT_TRIGGERSPENDING ||
1267 conflictor->status == PKG_STAT_INSTALLED))
1268 continue;
1269
1270 trig_activate_packageprocessing(conflictor);
1271 pkg_set_status(conflictor, PKG_STAT_HALFCONFIGURED);
1272 modstatdb_note(conflictor);
1273 push_cleanup(cu_prerminfavour, ~ehflag_normaltidy, NULL, 0,
1274 2, conflictor, pkg);
1275 maintscript_installed(conflictor, PRERMFILE, "pre-removal",
1276 "remove", "in-favour",
1277 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
1278 versiondescribe(&pkg->available.version,
1279 vdew_nonambig),
1280 NULL);
1281 pkg_set_status(conflictor, PKG_STAT_HALFINSTALLED);
1282 modstatdb_note(conflictor);
1283 }
1284
1285 pkg_set_eflags(pkg, PKG_EFLAG_REINSTREQ);
1286 if (pkg->status == PKG_STAT_NOTINSTALLED) {
1287 pkg->installed.version= pkg->available.version;
1288 pkg->installed.multiarch = pkg->available.multiarch;
1289 }
1290 pkg_set_status(pkg, PKG_STAT_HALFINSTALLED);
1291 modstatdb_note(pkg);
1292 if (oldversionstatus == PKG_STAT_NOTINSTALLED) {
1293 push_cleanup(cu_preinstverynew, ~ehflag_normaltidy, NULL, 0,
1294 3,(void*)pkg,(void*)cidir,(void*)cidirrest);
1295 maintscript_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest,
1296 "install", NULL);
1297 } else if (oldversionstatus == PKG_STAT_CONFIGFILES) {
1298 push_cleanup(cu_preinstnew, ~ehflag_normaltidy, NULL, 0,
1299 3,(void*)pkg,(void*)cidir,(void*)cidirrest);
1300 maintscript_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest,
1301 "install",
1302 versiondescribe(&pkg->installed.version, vdew_nonambig),
1303 versiondescribe(&pkg->available.version, vdew_nonambig),
1304 NULL);
1305 } else {
1306 push_cleanup(cu_preinstupgrade, ~ehflag_normaltidy, NULL, 0,
1307 4,(void*)pkg,(void*)cidir,(void*)cidirrest,(void*)&oldversionstatus);
1308 maintscript_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest,
1309 "upgrade",
1310 versiondescribe(&pkg->installed.version, vdew_nonambig),
1311 versiondescribe(&pkg->available.version, vdew_nonambig),
1312 NULL);
1313 }
1314
1315 if (oldversionstatus == PKG_STAT_NOTINSTALLED ||
1316 oldversionstatus == PKG_STAT_CONFIGFILES) {
1317 printf(_("Unpacking %s (%s) ...\n"),
1318 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
1319 versiondescribe(&pkg->available.version, vdew_nonambig));
1320 } else {
1321 printf(_("Unpacking %s (%s) over (%s) ...\n"),
1322 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
1323 versiondescribe(&pkg->available.version, vdew_nonambig),
1324 versiondescribe(&pkg->installed.version, vdew_nonambig));
1325 }
1326
1327 /*
1328 * Now we unpack the archive, backing things up as we go.
1329 * For each file, we check to see if it already exists.
1330 * There are several possibilities:
1331 *
1332 * + We are trying to install a non-directory ...
1333 * - It doesn't exist. In this case we simply extract it.
1334 * - It is a plain file, device, symlink, &c. We do an ‘atomic
1335 * overwrite’ using link() and rename(), but leave a backup copy.
1336 * Later, when we delete the backup, we remove it from any other
1337 * packages' lists.
1338 * - It is a directory. In this case it depends on whether we're
1339 * trying to install a symlink or something else.
1340 * = If we're not trying to install a symlink we move the directory
1341 * aside and extract the node. Later, when we recursively remove
1342 * the backed-up directory, we remove it from any other packages'
1343 * lists.
1344 * = If we are trying to install a symlink we do nothing - ie,
1345 * dpkg will never replace a directory tree with a symlink. This
1346 * is to avoid embarrassing effects such as replacing a directory
1347 * tree with a link to a link to the original directory tree.
1348 * + We are trying to install a directory ...
1349 * - It doesn't exist. We create it with the appropriate modes.
1350 * - It exists as a directory or a symlink to one. We do nothing.
1351 * - It is a plain file or a symlink (other than to a directory).
1352 * We move it aside and create the directory. Later, when we
1353 * delete the backup, we remove it from any other packages' lists.
1354 *
1355 * Install non-dir Install symlink Install dir
1356 * Exists not X X X
1357 * File/node/symlink LXR LXR BXR
1358 * Directory BXR - -
1359 *
1360 * X: extract file/node/link/directory
1361 * LX: atomic overwrite leaving backup
1362 * B: ordinary backup
1363 * R: later remove from other packages' lists
1364 * -: do nothing
1365 *
1366 * After we've done this we go through the remaining things in the
1367 * lists of packages we're trying to remove (including the old
1368 * version of the current package). This happens in reverse order,
1369 * so that we process files before the directories (or symlinks-to-
1370 * directories) containing them.
1371 *
1372 * + If the thing is a conffile then we leave it alone for the purge
1373 * operation.
1374 * + Otherwise, there are several possibilities too:
1375 * - The listed thing does not exist. We ignore it.
1376 * - The listed thing is a directory or a symlink to a directory.
1377 * We delete it only if it isn't listed in any other package.
1378 * - The listed thing is not a directory, but was part of the package
1379 * that was upgraded, we check to make sure the files aren't the
1380 * same ones from the old package by checking dev/inode
1381 * - The listed thing is not a directory or a symlink to one (ie,
1382 * it's a plain file, device, pipe, &c, or a symlink to one, or a
1383 * dangling symlink). We delete it.
1384 *
1385 * The removed packages' list becomes empty (of course, the new
1386 * version of the package we're installing will have a new list,
1387 * which replaces the old version's list).
1388 *
1389 * If at any stage we remove a file from a package's list, and the
1390 * package isn't one we're already processing, and the package's
1391 * list becomes empty as a result, we ‘vanish’ the package. This
1392 * means that we run its postrm with the ‘disappear’ argument, and
1393 * put the package in the ‘not-installed’ state. If it had any
1394 * conffiles, their hashes and ownership will have been transferred
1395 * already, so we just ignore those and forget about them from the
1396 * point of view of the disappearing package.
1397 *
1398 * NOTE THAT THE OLD POSTRM IS RUN AFTER THE NEW PREINST, since the
1399 * files get replaced ‘as we go’.
1400 */
1401
1402 m_pipe(p1);
1403 push_cleanup(cu_closepipe, ehflag_bombout, NULL, 0, 1, (void *)&p1[0]);
1404 pid = subproc_fork();
1405 if (pid == 0) {
1406 m_dup2(p1[1],1); close(p1[0]); close(p1[1]);
1407 execlp(BACKEND, BACKEND, "--fsys-tarfile", filename, NULL);
1408 ohshite(_("unable to execute %s (%s)"),
1409 _("package filesystem archive extraction"), BACKEND);
1410 }
1411 close(p1[1]);
1412 p1[1] = -1;
29279ac2 1413open_shadow(p1[0], "deb-extract-decompressed-in");
1479465f
GJ
1414
1415 newfiles_queue.head = NULL;
1416 newfiles_queue.tail = &newfiles_queue.head;
1417 tc.newfiles_queue = &newfiles_queue;
1418 push_cleanup(cu_fileslist, ~0, NULL, 0, 0);
1419 tc.pkg= pkg;
1420 tc.backendpipe= p1[0];
1421 tc.pkgset_getting_in_sync = pkgset_getting_in_sync(pkg);
1422
1423 rc = tar_extractor(&tc, &tf);
1424 if (rc) {
29279ac2
MW
1425save_shadow("deb-extract-ar", "a");
1426save_shadow("deb-extract-compressed-out", "tar.xz");
1427save_shadow("deb-extract-compressed-in", "tar.xz");
1428save_shadow("deb-extract-decompressed-out", "tar");
1429save_shadow("deb-extract-decompressed-in", "tar");
1479465f
GJ
1430 if (errno) {
1431 ohshite(_("error reading dpkg-deb tar output"));
1432 } else {
1433 ohshit(_("corrupted filesystem tarfile - corrupted package archive"));
1434 }
1435 }
1436 if (fd_skip(p1[0], -1, &err) < 0)
1437 ohshit(_("cannot zap possible trailing zeros from dpkg-deb: %s"), err.str);
29279ac2 1438close_shadow(p1[0]);
1479465f
GJ
1439 close(p1[0]);
1440 p1[0] = -1;
1441 subproc_reap(pid, BACKEND " --fsys-tarfile", SUBPROC_NOPIPE);
1442
1443 tar_deferred_extract(newfiles_queue.head, pkg);
1444
1445 if (oldversionstatus == PKG_STAT_HALFINSTALLED ||
1446 oldversionstatus == PKG_STAT_UNPACKED) {
1447 /* Packages that were in ‘installed’ and ‘postinstfailed’ have been
1448 * reduced to ‘unpacked’ by now, by the running of the prerm script. */
1449 pkg_set_status(pkg, PKG_STAT_HALFINSTALLED);
1450 modstatdb_note(pkg);
1451 push_cleanup(cu_postrmupgrade, ~ehflag_normaltidy, NULL, 0, 1, (void *)pkg);
1452 maintscript_fallback(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
1453 "upgrade", "failed-upgrade");
1454 }
1455
1456 /* If anything goes wrong while tidying up it's a bit late to do
1457 * anything about it. However, we don't install the new status
1458 * info yet, so that a future dpkg installation will put everything
1459 * right (we hope).
1460 *
1461 * If something does go wrong later the ‘conflictor’ package will be
1462 * left in the ‘removal_failed’ state. Removing or installing it
1463 * will be impossible if it was required because of the conflict with
1464 * the package we're installing now and (presumably) the dependency
1465 * by other packages. This means that the files it contains in
1466 * common with this package will hang around until we successfully
1467 * get this package installed, after which point we can trust the
1468 * conflicting package's file list, which will have been updated to
1469 * remove any files in this package. */
1470 push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
1471
1472 /* Now we delete all the files that were in the old version of
1473 * the package only, except (old or new) conffiles, which we leave
1474 * alone. */
1475 pkg_remove_old_files(pkg, &newfiles_queue, &newconffiles);
1476
1477 /* OK, now we can write the updated files-in-this package list,
1478 * since we've done away (hopefully) with all the old junk. */
1479 write_filelist_except(pkg, &pkg->available, newfiles_queue.head, 0);
1480
1481 /* Trigger interests may have changed.
1482 * Firstly we go through the old list of interests deleting them.
1483 * Then we go through the new list adding them. */
1484 strcpy(cidirrest, TRIGGERSCIFILE);
1485 trig_parse_ci(pkg_infodb_get_file(pkg, &pkg->installed, TRIGGERSCIFILE),
1486 trig_cicb_interest_delete, NULL, pkg, &pkg->installed);
1487 trig_parse_ci(cidir, trig_cicb_interest_add, NULL, pkg, &pkg->available);
1488 trig_file_interests_save();
1489
1490 /* We also install the new maintainer scripts, and any other
1491 * cruft that may have come along with the package. First
1492 * we go through the existing scripts replacing or removing
1493 * them as appropriate; then we go through the new scripts
1494 * (any that are left) and install them. */
1495 debug(dbg_general, "process_archive updating info directory");
1496 pkg_infodb_update(pkg, cidir, cidirrest);
1497
1498 /* We store now the checksums dynamically computed while unpacking. */
1499 write_filehash_except(pkg, &pkg->available, newfiles_queue.head, 0);
1500
1501 /*
1502 * Update the status database.
1503 *
1504 * This involves copying each field across from the ‘available’
1505 * to the ‘installed’ half of the pkg structure.
1506 * For some of the fields we have to do a complicated construction
1507 * operation; for others we can just copy the value.
1508 * We tackle the fields in the order they appear, so that
1509 * we don't miss any out :-).
1510 * At least we don't have to copy any strings that are referred
1511 * to, because these are never modified and never freed.
1512 */
1513 pkg_update_fields(pkg, &newconffiles);
1514
1515 /* In case this was an architecture cross-grade, the in-core pkgset might
1516 * be in an inconsistent state, with two pkginfo entries having the same
1517 * architecture, so let's fix that. Note that this does not lose data,
1518 * as the pkg.available member parsed from the binary should replace the
1519 * to be cleared duplicate in the other instance. */
1520 for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
1521 if (otherpkg == pkg)
1522 continue;
1523 if (otherpkg->installed.arch != pkg->installed.arch)
1524 continue;
1525
1526 assert(otherpkg->status == PKG_STAT_NOTINSTALLED);
1527
1528 pkg_blank(otherpkg);
1529 }
1530
1531 /* Check for disappearing packages:
1532 * We go through all the packages on the system looking for ones
1533 * whose files are entirely part of the one we've just unpacked
1534 * (and which actually *have* some files!).
1535 *
1536 * Any that we find are removed - we run the postrm with ‘disappear’
1537 * as an argument, and remove their info/... files and status info.
1538 * Conffiles are ignored (the new package had better do something
1539 * with them!). */
1540 pkg_disappear_others(pkg);
1541
1542 /* Delete files from any other packages' lists.
1543 * We have to do this before we claim this package is in any
1544 * sane kind of state, as otherwise we might delete by mistake
1545 * a file that we overwrote, when we remove the package which
1546 * had the version we overwrote. To prevent this we make
1547 * sure that we don't claim this package is OK until we
1548 * have claimed ‘ownership’ of all its files. */
1549 pkg_remove_files_from_others(pkg, newfiles_queue.head);
1550
1551 /* Right, the package we've unpacked is now in a reasonable state.
1552 * The only thing that we have left to do with it is remove
1553 * backup files, and we can leave the user to fix that if and when
1554 * it happens (we leave the reinstall required flag, of course). */
1555 pkg_set_status(pkg, PKG_STAT_UNPACKED);
1556 modstatdb_note(pkg);
1557
1558 /* Now we delete all the backup files that we made when
1559 * extracting the archive - except for files listed as conffiles
1560 * in the new package.
1561 * This time we count it as an error if something goes wrong.
1562 *
1563 * Note that we don't ever delete things that were in the old
1564 * package as a conffile and don't appear at all in the new.
1565 * They stay recorded as obsolete conffiles and will eventually
1566 * (if not taken over by another package) be forgotten. */
1567 pkg_remove_backup_files(pkg, newfiles_queue.head);
1568
1569 /* OK, we're now fully done with the main package.
1570 * This is quite a nice state, so we don't unwind past here. */
1571 pkg_reset_eflags(pkg);
1572 modstatdb_note(pkg);
1573 push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
1574
1575 /* Only the removal of the conflictor left to do.
1576 * The files list for the conflictor is still a little inconsistent in-core,
1577 * as we have not yet updated the filename->packages mappings; however,
1578 * the package->filenames mapping is. */
1579 while (!pkg_queue_is_empty(&conflictors)) {
1580 struct pkginfo *conflictor = pkg_queue_pop(&conflictors);
1581
1582 /* We need to have the most up-to-date info about which files are
1583 * what ... */
1584 ensure_allinstfiles_available();
1585 removal_bulk(conflictor);
1586 }
1587
1588 if (cipaction->arg_int == act_install)
1589 enqueue_package_mark_seen(pkg);
1590}