processarc.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. /*
  2. * dpkg - main program for package management
  3. * processarc.c - the huge function process_archive
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2006-2011 Guillem Jover <guillem@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <sys/wait.h>
  26. #include <assert.h>
  27. #include <errno.h>
  28. #include <ctype.h>
  29. #include <string.h>
  30. #include <time.h>
  31. #include <utime.h>
  32. #include <fcntl.h>
  33. #include <dirent.h>
  34. #include <unistd.h>
  35. #include <stdint.h>
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. #include <dpkg/i18n.h>
  39. #include <dpkg/dpkg.h>
  40. #include <dpkg/dpkg-db.h>
  41. #include <dpkg/path.h>
  42. #include <dpkg/buffer.h>
  43. #include <dpkg/subproc.h>
  44. #include <dpkg/dir.h>
  45. #include <dpkg/tarfn.h>
  46. #include <dpkg/options.h>
  47. #include <dpkg/triglib.h>
  48. #include "filesdb.h"
  49. #include "infodb.h"
  50. #include "main.h"
  51. #include "archives.h"
  52. static const char *
  53. summarize_filename(const char *filename)
  54. {
  55. const char *pfilename;
  56. char *pfilenamebuf;
  57. for (pfilename = filename;
  58. pfilename && strlen(pfilename) > 30 && strchr(pfilename, '/') != NULL;
  59. pfilename++)
  60. pfilename = strchr(pfilename, '/');
  61. if (pfilename && pfilename != filename) {
  62. pfilenamebuf = nfmalloc(strlen(pfilename) + 5);
  63. sprintf(pfilenamebuf, _(".../%s"), pfilename);
  64. pfilename = pfilenamebuf;
  65. } else {
  66. pfilename = filename;
  67. }
  68. return pfilename;
  69. }
  70. static bool
  71. deb_reassemble(const char **filename, const char **pfilename)
  72. {
  73. static char *reasmbuf = NULL;
  74. struct stat stab;
  75. int status;
  76. pid_t pid;
  77. if (!reasmbuf)
  78. reasmbuf = dpkg_db_get_path(REASSEMBLETMP);
  79. if (unlink(reasmbuf) && errno != ENOENT)
  80. ohshite(_("error ensuring `%.250s' doesn't exist"), reasmbuf);
  81. push_cleanup(cu_pathname, ~0, NULL, 0, 1, (void *)reasmbuf);
  82. pid = subproc_fork();
  83. if (!pid) {
  84. execlp(SPLITTER, SPLITTER, "-Qao", reasmbuf, *filename, NULL);
  85. ohshite(_("unable to execute %s (%s)"),
  86. _("split package reassembly"), SPLITTER);
  87. }
  88. status = subproc_wait(pid, SPLITTER);
  89. switch (WIFEXITED(status) ? WEXITSTATUS(status) : -1) {
  90. case 0:
  91. /* It was a part - is it complete? */
  92. if (!stat(reasmbuf, &stab)) {
  93. /* Yes. */
  94. *filename = reasmbuf;
  95. *pfilename = _("reassembled package file");
  96. break;
  97. } else if (errno == ENOENT) {
  98. /* No. That's it, we skip it. */
  99. return false;
  100. }
  101. case 1:
  102. /* No, it wasn't a part. */
  103. break;
  104. default:
  105. subproc_check(status, SPLITTER, 0);
  106. }
  107. return true;
  108. }
  109. static void
  110. deb_verify(const char *filename)
  111. {
  112. struct stat stab;
  113. pid_t pid;
  114. if (stat(DEBSIGVERIFY, &stab) < 0)
  115. return;
  116. printf(_("Authenticating %s ...\n"), filename);
  117. fflush(stdout);
  118. pid = subproc_fork();
  119. if (!pid) {
  120. execl(DEBSIGVERIFY, DEBSIGVERIFY, "-q", filename, NULL);
  121. ohshite(_("unable to execute %s (%s)"),
  122. _("package signature verification"), DEBSIGVERIFY);
  123. } else {
  124. int status;
  125. status = subproc_wait(pid, "debsig-verify");
  126. if (!(WIFEXITED(status) && WEXITSTATUS(status) == 0)) {
  127. if (!fc_badverify)
  128. ohshit(_("Verification on package %s failed!"), filename);
  129. else
  130. fprintf(stderr, _("Verification on package %s failed,\n"
  131. "but installing anyway as you requested.\n"),
  132. filename);
  133. } else {
  134. printf(_("passed\n"));
  135. }
  136. }
  137. }
  138. #define MAXCONFLICTORS 20
  139. static struct pkginfo *conflictor[MAXCONFLICTORS];
  140. static int cflict_index = 0;
  141. void
  142. push_conflictor(struct pkginfo *pkg, struct pkginfo *pkg_fixbyrm)
  143. {
  144. if (cflict_index >= MAXCONFLICTORS)
  145. ohshit(_("package %s has too many Conflicts/Replaces pairs"), pkg->name);
  146. conflictor[cflict_index++] = pkg_fixbyrm;
  147. }
  148. struct match_node {
  149. struct match_node *next;
  150. char *filetype;
  151. char *filename;
  152. };
  153. static struct match_node *match_head = NULL;
  154. static struct match_node *
  155. match_node_new(const char *name, const char *type, struct match_node *next)
  156. {
  157. struct match_node *node;
  158. node = m_malloc(sizeof(*node));
  159. node->next = next;
  160. node->filename = m_strdup(name);
  161. node->filetype = m_strdup(type);
  162. return node;
  163. }
  164. static void
  165. match_node_free(struct match_node *node)
  166. {
  167. free(node->filetype);
  168. free(node->filename);
  169. free(node);
  170. }
  171. static void
  172. pkg_infodb_update_file(const char *filename, const char *filetype)
  173. {
  174. if (strlen(filetype) > MAXCONTROLFILENAME)
  175. ohshit(_("old version of package has overly-long info file name starting `%.250s'"),
  176. filename);
  177. /* We do the list separately. */
  178. if (strcmp(filetype, LISTFILE) == 0)
  179. return;
  180. /* We keep files to rename in a list as doing the rename immediately
  181. * might influence the current readdir(), the just renamed file might
  182. * be returned a second time as it's actually a new file from the
  183. * point of view of the filesystem. */
  184. match_head = match_node_new(filename, filetype, match_head);
  185. }
  186. static void
  187. pkg_infodb_update(struct pkginfo *pkg, char *cidir, char *cidirrest)
  188. {
  189. struct match_node *match_node;
  190. DIR *dsd;
  191. struct dirent *de;
  192. /* Deallocate the match list in case we aborted previously. */
  193. while ((match_node = match_head)) {
  194. match_head = match_node->next;
  195. match_node_free(match_node);
  196. }
  197. pkg_infodb_foreach(pkg, pkg_infodb_update_file);
  198. while ((match_node = match_head)) {
  199. strcpy(cidirrest, match_node->filetype);
  200. if (!rename(cidir, match_node->filename)) {
  201. debug(dbg_scripts, "process_archive info installed %s as %s",
  202. cidir, match_node->filename);
  203. } else if (errno == ENOENT) {
  204. /* Right, no new version. */
  205. if (unlink(match_node->filename))
  206. ohshite(_("unable to remove obsolete info file `%.250s'"),
  207. match_node->filename);
  208. debug(dbg_scripts, "process_archive info unlinked %s",
  209. match_node->filename);
  210. } else {
  211. ohshite(_("unable to install (supposed) new info file `%.250s'"), cidir);
  212. }
  213. match_head = match_node->next;
  214. match_node_free(match_node);
  215. }
  216. /* The control directory itself. */
  217. cidirrest[0] = '\0';
  218. dsd = opendir(cidir);
  219. if (!dsd)
  220. ohshite(_("unable to open temp control directory"));
  221. push_cleanup(cu_closedir, ~0, NULL, 0, 1, (void *)dsd);
  222. while ((de = readdir(dsd))) {
  223. const char *newinfofilename;
  224. if (strchr(de->d_name, '.')) {
  225. debug(dbg_scripts,"process_archive tmp.ci script/file `%s' contains dot",
  226. de->d_name);
  227. continue;
  228. }
  229. if (strlen(de->d_name) > MAXCONTROLFILENAME)
  230. ohshit(_("package contains overly-long control info file name (starting `%.50s')"),
  231. de->d_name);
  232. strcpy(cidirrest, de->d_name);
  233. /* First we check it's not a directory. */
  234. if (rmdir(cidir) == 0)
  235. ohshit(_("package control info contained directory `%.250s'"), cidir);
  236. else if (errno != ENOTDIR)
  237. ohshite(_("package control info rmdir of `%.250s' didn't say not a dir"),
  238. de->d_name);
  239. /* Ignore the control file. */
  240. if (strcmp(de->d_name, CONTROLFILE) == 0) {
  241. debug(dbg_scripts, "process_archive tmp.ci script/file `%s' is control",
  242. cidir);
  243. continue;
  244. }
  245. if (strcmp(de->d_name, LISTFILE) == 0) {
  246. warning(_("package %s contained list as info file"), pkg->name);
  247. continue;
  248. }
  249. /* Right, install it */
  250. newinfofilename = pkgadminfile(pkg,de->d_name);
  251. if (rename(cidir, newinfofilename))
  252. ohshite(_("unable to install new info file `%.250s' as `%.250s'"),
  253. cidir, newinfofilename);
  254. debug(dbg_scripts,
  255. "process_archive tmp.ci script/file `%s' installed as `%s'",
  256. cidir, newinfofilename);
  257. }
  258. pop_cleanup(ehflag_normaltidy); /* closedir */
  259. dir_sync_path(pkgadmindir());
  260. }
  261. static void
  262. pkg_infodb_remove_file(const char *filename, const char *filetype)
  263. {
  264. if (unlink(filename))
  265. ohshite(_("unable to delete control info file `%.250s'"), filename);
  266. debug(dbg_scripts, "removal_bulk info unlinked %s", filename);
  267. }
  268. static enum parsedbflags
  269. parsedb_force_flags(void)
  270. {
  271. enum parsedbflags flags = 0;
  272. if (fc_badversion)
  273. flags |= pdb_lax_version_parser;
  274. return flags;
  275. }
  276. void process_archive(const char *filename) {
  277. static const struct tar_operations tf = {
  278. .read = tarfileread,
  279. .extract_file = tarobject,
  280. .link = tarobject,
  281. .symlink = tarobject,
  282. .mkdir = tarobject,
  283. .mknod = tarobject,
  284. };
  285. /* These need to be static so that we can pass their addresses to
  286. * push_cleanup as arguments to the cu_xxx routines; if an error occurs
  287. * we unwind the stack before processing the cleanup list, and these
  288. * variables had better still exist ... */
  289. static int p1[2];
  290. static char *cidir = NULL;
  291. static struct fileinlist *newconffiles, *newfileslist;
  292. static enum pkgstatus oldversionstatus;
  293. static struct varbuf depprobwhy;
  294. static struct tarcontext tc;
  295. int r, i;
  296. pid_t pid;
  297. struct pkgiterator *it;
  298. struct pkginfo *pkg, *otherpkg, *divpkg;
  299. char *cidirrest, *p;
  300. char conffilenamebuf[MAXCONFFILENAME];
  301. char *psize;
  302. const char *pfilename;
  303. struct fileinlist *newconff, **newconffileslastp;
  304. struct fileinlist *cfile;
  305. struct reversefilelistiter rlistit;
  306. struct conffile *searchconff, **iconffileslastp, *newiconff;
  307. struct dependency *dsearch, *newdeplist, **newdeplistlastp;
  308. struct dependency *newdep, *dep, *providecheck;
  309. struct deppossi *psearch, **newpossilastp, *possi, *newpossi, *pdep;
  310. FILE *conff;
  311. struct filenamenode *namenode;
  312. struct stat stab, oldfs;
  313. struct pkg_deconf_list *deconpil, *deconpiltemp;
  314. struct pkginfo *fixbytrigaw;
  315. cleanup_pkg_failed= cleanup_conflictor_failed= 0;
  316. pfilename = summarize_filename(filename);
  317. if (stat(filename,&stab)) ohshite(_("cannot access archive"));
  318. /* We can't ‘tentatively-reassemble’ packages. */
  319. if (!f_noact) {
  320. if (!deb_reassemble(&filename, &pfilename))
  321. return;
  322. }
  323. /* Verify the package. */
  324. if (!f_nodebsig)
  325. deb_verify(filename);
  326. if (f_noact) {
  327. char *tmpdir;
  328. tmpdir = mkdtemp(path_make_temp_template("dpkg"));
  329. if (!tmpdir)
  330. ohshite(_("unable to create temporary directory"));
  331. cidir = m_realloc(cidir, strlen(tmpdir) + MAXCONTROLFILENAME + 10);
  332. strcpy(cidir, tmpdir);
  333. strcat(cidir,"/");
  334. cidirrest = cidir + strlen(cidir);
  335. free(tmpdir);
  336. } else {
  337. const char *admindir = dpkg_db_get_dir();
  338. /* We want it to be on the same filesystem so that we can
  339. * use rename(2) to install the postinst &c. */
  340. if (!cidir)
  341. cidir = m_malloc(strlen(admindir) + sizeof(CONTROLDIRTMP) +
  342. MAXCONTROLFILENAME + 10);
  343. strcpy(cidir,admindir);
  344. strcat(cidir, "/" CONTROLDIRTMP);
  345. ensure_pathname_nonexisting(cidir);
  346. strcat(cidir, "/");
  347. cidirrest = cidir + strlen(cidir);
  348. }
  349. push_cleanup(cu_cidir, ~0, NULL, 0, 2, (void *)cidir, (void *)cidirrest);
  350. pid = subproc_fork();
  351. if (pid == 0) {
  352. cidirrest[-1] = '\0';
  353. execlp(BACKEND, BACKEND, "--control", filename, cidir, NULL);
  354. ohshite(_("unable to execute %s (%s)"),
  355. _("package control information extraction"), BACKEND);
  356. }
  357. subproc_wait_check(pid, BACKEND " --control", 0);
  358. /* We want to guarantee the extracted files are on the disk, so that the
  359. * subsequent renames to the info database do not end up with old or zero
  360. * length files in case of a system crash. As neither dpkg-deb nor tar do
  361. * explicit fsync()s, we have to do them here.
  362. * XXX: This could be avoided by switching to an internal tar extractor. */
  363. dir_sync_contents(cidir);
  364. strcpy(cidirrest,CONTROLFILE);
  365. parsedb(cidir, pdb_recordavailable | pdb_rejectstatus | pdb_ignorefiles |
  366. parsedb_force_flags(), &pkg);
  367. if (!pkg->files) {
  368. pkg->files= nfmalloc(sizeof(struct filedetails));
  369. pkg->files->next = NULL;
  370. pkg->files->name = pkg->files->msdosname = pkg->files->md5sum = NULL;
  371. }
  372. /* Always nfmalloc. Otherwise, we may overwrite some other field (like
  373. * md5sum). */
  374. psize = nfmalloc(30);
  375. sprintf(psize, "%jd", (intmax_t)stab.st_size);
  376. pkg->files->size = psize;
  377. if (cipaction->arg_int == act_avail) {
  378. printf(_("Recorded info about %s from %s.\n"),pkg->name,pfilename);
  379. pop_cleanup(ehflag_normaltidy);
  380. return;
  381. }
  382. if (strcmp(pkg->available.arch, "all") &&
  383. strcmp(pkg->available.arch, native_arch))
  384. forcibleerr(fc_architecture,
  385. _("package architecture (%s) does not match system (%s)"),
  386. pkg->available.arch, native_arch);
  387. for (deconpil= deconfigure;
  388. deconpil;
  389. deconpil= deconpiltemp) {
  390. deconpiltemp= deconpil->next;
  391. free(deconpil);
  392. }
  393. deconfigure = NULL;
  394. clear_istobes();
  395. if (!wanttoinstall(pkg)) {
  396. pop_cleanup(ehflag_normaltidy);
  397. return;
  398. }
  399. /* Check if anything is installed that we conflict with, or not installed
  400. * that we need. */
  401. pkg->clientdata->istobe= itb_installnew;
  402. for (dsearch= pkg->available.depends; dsearch; dsearch= dsearch->next) {
  403. switch (dsearch->type) {
  404. case dep_conflicts:
  405. /* Look for things we conflict with. */
  406. check_conflict(dsearch, pkg, pfilename);
  407. break;
  408. case dep_breaks:
  409. /* Look for things we break. */
  410. check_breaks(dsearch, pkg, pfilename);
  411. break;
  412. case dep_provides:
  413. /* Look for things that conflict with what we provide. */
  414. for (psearch = dsearch->list->ed->installed.depended;
  415. psearch;
  416. psearch = psearch->rev_next) {
  417. if (psearch->up->type != dep_conflicts)
  418. continue;
  419. check_conflict(psearch->up, pkg, pfilename);
  420. }
  421. break;
  422. case dep_suggests:
  423. case dep_recommends:
  424. case dep_depends:
  425. case dep_replaces:
  426. case dep_enhances:
  427. /* Ignore these here. */
  428. break;
  429. case dep_predepends:
  430. if (!depisok(dsearch, &depprobwhy, NULL, &fixbytrigaw, true)) {
  431. if (fixbytrigaw) {
  432. while (fixbytrigaw->trigaw.head)
  433. trigproc(fixbytrigaw->trigaw.head->pend);
  434. } else {
  435. varbuf_end_str(&depprobwhy);
  436. fprintf(stderr, _("dpkg: regarding %s containing %s, pre-dependency problem:\n%s"),
  437. pfilename, pkg->name, depprobwhy.buf);
  438. if (!force_depends(dsearch->list))
  439. ohshit(_("pre-dependency problem - not installing %.250s"),pkg->name);
  440. warning(_("ignoring pre-dependency problem!"));
  441. }
  442. }
  443. }
  444. }
  445. /* Look for things that conflict with us. */
  446. for (psearch = pkg->installed.depended; psearch; psearch = psearch->rev_next) {
  447. if (psearch->up->type != dep_conflicts) continue;
  448. check_conflict(psearch->up, pkg, pfilename);
  449. }
  450. ensure_allinstfiles_available();
  451. filesdbinit();
  452. trig_file_interests_ensure();
  453. if (pkg->status != stat_notinstalled && pkg->status != stat_configfiles) {
  454. printf(_("Preparing to replace %s %s (using %s) ...\n"),
  455. pkg->name,
  456. versiondescribe(&pkg->installed.version,vdew_nonambig),
  457. pfilename);
  458. log_action("upgrade", pkg);
  459. } else {
  460. printf(_("Unpacking %s (from %s) ...\n"),pkg->name,pfilename);
  461. log_action("install", pkg);
  462. }
  463. if (f_noact) {
  464. pop_cleanup(ehflag_normaltidy);
  465. return;
  466. }
  467. /*
  468. * OK, we're going ahead.
  469. */
  470. trig_activate_packageprocessing(pkg);
  471. strcpy(cidirrest, TRIGGERSCIFILE);
  472. trig_parse_ci(cidir, NULL, trig_cicb_statuschange_activate, pkg);
  473. /* Read the conffiles, and copy the hashes across. */
  474. newconffiles = NULL;
  475. newconffileslastp = &newconffiles;
  476. push_cleanup(cu_fileslist, ~0, NULL, 0, 0);
  477. strcpy(cidirrest,CONFFILESFILE);
  478. conff= fopen(cidir,"r");
  479. if (conff) {
  480. push_cleanup(cu_closestream, ehflag_bombout, NULL, 0, 1, (void *)conff);
  481. while (fgets(conffilenamebuf,MAXCONFFILENAME-2,conff)) {
  482. struct filepackages_iterator *iter;
  483. p= conffilenamebuf + strlen(conffilenamebuf);
  484. assert(p != conffilenamebuf);
  485. if (p[-1] != '\n')
  486. ohshit(_("name of conffile (starting `%.250s') is too long (>%d characters)"),
  487. conffilenamebuf, MAXCONFFILENAME);
  488. while (p > conffilenamebuf && isspace(p[-1])) --p;
  489. if (p == conffilenamebuf) continue;
  490. *p = '\0';
  491. namenode= findnamenode(conffilenamebuf, 0);
  492. namenode->oldhash= NEWCONFFILEFLAG;
  493. newconff= newconff_append(&newconffileslastp, namenode);
  494. /* Let's see if any packages have this file. If they do we
  495. * check to see if they listed it as a conffile, and if they did
  496. * we copy the hash across. Since (for plain file conffiles,
  497. * which is the only kind we are supposed to have) there will
  498. * only be one package which ‘has’ the file, this will usually
  499. * mean we only look in the package which we're installing now.
  500. * The ‘conffiles’ data in the status file is ignored when a
  501. * package isn't also listed in the file ownership database as
  502. * having that file. If several packages are listed as owning
  503. * the file we pick one at random. */
  504. searchconff = NULL;
  505. iter = filepackages_iter_new(newconff->namenode);
  506. while ((otherpkg = filepackages_iter_next(iter))) {
  507. debug(dbg_conffdetail,
  508. "process_archive conffile `%s' in package %s - conff ?",
  509. newconff->namenode->name, otherpkg->name);
  510. for (searchconff = otherpkg->installed.conffiles;
  511. searchconff && strcmp(newconff->namenode->name, searchconff->name);
  512. searchconff = searchconff->next)
  513. debug(dbg_conffdetail,
  514. "process_archive conffile `%s' in package %s - conff ? not `%s'",
  515. newconff->namenode->name, otherpkg->name, searchconff->name);
  516. if (searchconff) {
  517. debug(dbg_conff,
  518. "process_archive conffile `%s' package=%s %s hash=%s",
  519. newconff->namenode->name, otherpkg->name,
  520. otherpkg == pkg ? "same" : "different!",
  521. searchconff->hash);
  522. if (otherpkg == pkg)
  523. break;
  524. }
  525. }
  526. filepackages_iter_free(iter);
  527. if (searchconff) {
  528. newconff->namenode->oldhash= searchconff->hash;
  529. /* We don't copy ‘obsolete’; it's not obsolete in the new package. */
  530. } else {
  531. debug(dbg_conff,"process_archive conffile `%s' no package, no hash",
  532. newconff->namenode->name);
  533. }
  534. newconff->namenode->flags |= fnnf_new_conff;
  535. }
  536. if (ferror(conff)) ohshite(_("read error in %.250s"),cidir);
  537. pop_cleanup(ehflag_normaltidy); /* conff = fopen() */
  538. if (fclose(conff)) ohshite(_("error closing %.250s"),cidir);
  539. } else {
  540. if (errno != ENOENT) ohshite(_("error trying to open %.250s"),cidir);
  541. }
  542. /* All the old conffiles are marked with a flag, so that we don't delete
  543. * them if they seem to disappear completely. */
  544. oldconffsetflags(pkg->installed.conffiles);
  545. for (i = 0 ; i < cflict_index ; i++) {
  546. oldconffsetflags(conflictor[i]->installed.conffiles);
  547. }
  548. oldversionstatus= pkg->status;
  549. assert(oldversionstatus <= stat_installed);
  550. debug(dbg_general,"process_archive oldversionstatus=%s",
  551. statusstrings[oldversionstatus]);
  552. if (oldversionstatus == stat_halfconfigured ||
  553. oldversionstatus == stat_triggersawaited ||
  554. oldversionstatus == stat_triggerspending ||
  555. oldversionstatus == stat_installed) {
  556. pkg->eflag |= eflag_reinstreq;
  557. pkg->status= stat_halfconfigured;
  558. modstatdb_note(pkg);
  559. push_cleanup(cu_prermupgrade, ~ehflag_normaltidy, NULL, 0, 1, (void *)pkg);
  560. if (versioncompare(&pkg->available.version,
  561. &pkg->installed.version) >= 0) /* Upgrade or reinstall */
  562. maintainer_script_alternative(pkg, PRERMFILE, "pre-removal", cidir, cidirrest,
  563. "upgrade", "failed-upgrade");
  564. else /* Downgrade => no fallback */
  565. maintainer_script_installed(pkg, PRERMFILE, "pre-removal", "upgrade",
  566. versiondescribe(&pkg->available.version,
  567. vdew_nonambig), NULL);
  568. pkg->status= stat_unpacked;
  569. oldversionstatus= stat_unpacked;
  570. modstatdb_note(pkg);
  571. }
  572. for (deconpil= deconfigure; deconpil; deconpil= deconpil->next) {
  573. struct pkginfo *removing = deconpil->pkg_removal;
  574. if (removing)
  575. printf(_("De-configuring %s, to allow removal of %s ...\n"),
  576. deconpil->pkg->name, removing->name);
  577. else
  578. printf(_("De-configuring %s ...\n"), deconpil->pkg->name);
  579. trig_activate_packageprocessing(deconpil->pkg);
  580. deconpil->pkg->status= stat_halfconfigured;
  581. modstatdb_note(deconpil->pkg);
  582. /* This means that we *either* go and run postinst abort-deconfigure,
  583. * *or* queue the package for later configure processing, depending
  584. * on which error cleanup route gets taken. */
  585. push_cleanup(cu_prermdeconfigure, ~ehflag_normaltidy,
  586. ok_prermdeconfigure, ehflag_normaltidy,
  587. 3, (void*)deconpil->pkg, (void*)removing, (void*)pkg);
  588. if (removing) {
  589. maintainer_script_installed(deconpil->pkg, PRERMFILE, "pre-removal",
  590. "deconfigure", "in-favour", pkg->name,
  591. versiondescribe(&pkg->available.version,
  592. vdew_nonambig),
  593. "removing", removing->name,
  594. versiondescribe(&removing->installed.version,
  595. vdew_nonambig),
  596. NULL);
  597. } else {
  598. maintainer_script_installed(deconpil->pkg, PRERMFILE, "pre-removal",
  599. "deconfigure", "in-favour", pkg->name,
  600. versiondescribe(&pkg->available.version,
  601. vdew_nonambig),
  602. NULL);
  603. }
  604. }
  605. for (i = 0 ; i < cflict_index; i++) {
  606. if (!(conflictor[i]->status == stat_halfconfigured ||
  607. conflictor[i]->status == stat_triggersawaited ||
  608. conflictor[i]->status == stat_triggerspending ||
  609. conflictor[i]->status == stat_installed)) continue;
  610. trig_activate_packageprocessing(conflictor[i]);
  611. conflictor[i]->status= stat_halfconfigured;
  612. modstatdb_note(conflictor[i]);
  613. push_cleanup(cu_prerminfavour, ~ehflag_normaltidy, NULL, 0,
  614. 2,(void*)conflictor[i],(void*)pkg);
  615. maintainer_script_installed(conflictor[i], PRERMFILE, "pre-removal",
  616. "remove", "in-favour", pkg->name,
  617. versiondescribe(&pkg->available.version,
  618. vdew_nonambig),
  619. NULL);
  620. conflictor[i]->status= stat_halfinstalled;
  621. modstatdb_note(conflictor[i]);
  622. }
  623. pkg->eflag |= eflag_reinstreq;
  624. if (pkg->status == stat_notinstalled)
  625. pkg->installed.version= pkg->available.version;
  626. pkg->status= stat_halfinstalled;
  627. modstatdb_note(pkg);
  628. if (oldversionstatus == stat_notinstalled) {
  629. push_cleanup(cu_preinstverynew, ~ehflag_normaltidy, NULL, 0,
  630. 3,(void*)pkg,(void*)cidir,(void*)cidirrest);
  631. maintainer_script_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest,
  632. "install", NULL);
  633. } else if (oldversionstatus == stat_configfiles) {
  634. push_cleanup(cu_preinstnew, ~ehflag_normaltidy, NULL, 0,
  635. 3,(void*)pkg,(void*)cidir,(void*)cidirrest);
  636. maintainer_script_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest,
  637. "install", versiondescribe(&pkg->installed.version,
  638. vdew_nonambig),
  639. NULL);
  640. } else {
  641. push_cleanup(cu_preinstupgrade, ~ehflag_normaltidy, NULL, 0,
  642. 4,(void*)pkg,(void*)cidir,(void*)cidirrest,(void*)&oldversionstatus);
  643. maintainer_script_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest,
  644. "upgrade", versiondescribe(&pkg->installed.version,
  645. vdew_nonambig),
  646. NULL);
  647. printf(_("Unpacking replacement %.250s ...\n"),pkg->name);
  648. }
  649. /*
  650. * Now we unpack the archive, backing things up as we go.
  651. * For each file, we check to see if it already exists.
  652. * There are several possibilities:
  653. *
  654. * + We are trying to install a non-directory ...
  655. * - It doesn't exist. In this case we simply extract it.
  656. * - It is a plain file, device, symlink, &c. We do an ‘atomic
  657. * overwrite’ using link() and rename(), but leave a backup copy.
  658. * Later, when we delete the backup, we remove it from any other
  659. * packages' lists.
  660. * - It is a directory. In this case it depends on whether we're
  661. * trying to install a symlink or something else.
  662. * = If we're not trying to install a symlink we move the directory
  663. * aside and extract the node. Later, when we recursively remove
  664. * the backed-up directory, we remove it from any other packages'
  665. * lists.
  666. * = If we are trying to install a symlink we do nothing - ie,
  667. * dpkg will never replace a directory tree with a symlink. This
  668. * is to avoid embarrassing effects such as replacing a directory
  669. * tree with a link to a link to the original directory tree.
  670. * + We are trying to install a directory ...
  671. * - It doesn't exist. We create it with the appropriate modes.
  672. * - It exists as a directory or a symlink to one. We do nothing.
  673. * - It is a plain file or a symlink (other than to a directory).
  674. * We move it aside and create the directory. Later, when we
  675. * delete the backup, we remove it from any other packages' lists.
  676. *
  677. * Install non-dir Install symlink Install dir
  678. * Exists not X X X
  679. * File/node/symlink LXR LXR BXR
  680. * Directory BXR - -
  681. *
  682. * X: extract file/node/link/directory
  683. * LX: atomic overwrite leaving backup
  684. * B: ordinary backup
  685. * R: later remove from other packages' lists
  686. * -: do nothing
  687. *
  688. * After we've done this we go through the remaining things in the
  689. * lists of packages we're trying to remove (including the old
  690. * version of the current package). This happens in reverse order,
  691. * so that we process files before the directories (or symlinks-to-
  692. * directories) containing them.
  693. *
  694. * + If the thing is a conffile then we leave it alone for the purge
  695. * operation.
  696. * + Otherwise, there are several possibilities too:
  697. * - The listed thing does not exist. We ignore it.
  698. * - The listed thing is a directory or a symlink to a directory.
  699. * We delete it only if it isn't listed in any other package.
  700. * - The listed thing is not a directory, but was part of the package
  701. * that was upgraded, we check to make sure the files aren't the
  702. * same ones from the old package by checking dev/inode
  703. * - The listed thing is not a directory or a symlink to one (ie,
  704. * it's a plain file, device, pipe, &c, or a symlink to one, or a
  705. * dangling symlink). We delete it.
  706. *
  707. * The removed packages' list becomes empty (of course, the new
  708. * version of the package we're installing will have a new list,
  709. * which replaces the old version's list).
  710. *
  711. * If at any stage we remove a file from a package's list, and the
  712. * package isn't one we're already processing, and the package's
  713. * list becomes empty as a result, we ‘vanish’ the package. This
  714. * means that we run its postrm with the ‘disappear’ argument, and
  715. * put the package in the ‘not-installed’ state. If it had any
  716. * conffiles, their hashes and ownership will have been transferred
  717. * already, so we just ignore those and forget about them from the
  718. * point of view of the disappearing package.
  719. *
  720. * NOTE THAT THE OLD POSTRM IS RUN AFTER THE NEW PREINST, since the
  721. * files get replaced ‘as we go’.
  722. */
  723. m_pipe(p1);
  724. push_cleanup(cu_closepipe, ehflag_bombout, NULL, 0, 1, (void *)&p1[0]);
  725. pid = subproc_fork();
  726. if (pid == 0) {
  727. m_dup2(p1[1],1); close(p1[0]); close(p1[1]);
  728. execlp(BACKEND, BACKEND, "--fsys-tarfile", filename, NULL);
  729. ohshite(_("unable to execute %s (%s)"),
  730. _("package filesystem archive extraction"), BACKEND);
  731. }
  732. close(p1[1]);
  733. p1[1] = -1;
  734. newfileslist = NULL;
  735. tc.newfilesp = &newfileslist;
  736. push_cleanup(cu_fileslist, ~0, NULL, 0, 0);
  737. tc.pkg= pkg;
  738. tc.backendpipe= p1[0];
  739. r = tar_extractor(&tc, &tf);
  740. if (r) {
  741. if (errno) {
  742. ohshite(_("error reading dpkg-deb tar output"));
  743. } else {
  744. ohshit(_("corrupted filesystem tarfile - corrupted package archive"));
  745. }
  746. }
  747. fd_skip(p1[0], -1, _("dpkg-deb: zap possible trailing zeros"));
  748. close(p1[0]);
  749. p1[0] = -1;
  750. subproc_wait_check(pid, BACKEND " --fsys-tarfile", PROCPIPE);
  751. tar_deferred_extract(newfileslist, pkg);
  752. if (oldversionstatus == stat_halfinstalled || oldversionstatus == stat_unpacked) {
  753. /* Packages that were in ‘installed’ and ‘postinstfailed’ have been
  754. * reduced to ‘unpacked’ by now, by the running of the prerm script. */
  755. pkg->status= stat_halfinstalled;
  756. modstatdb_note(pkg);
  757. push_cleanup(cu_postrmupgrade, ~ehflag_normaltidy, NULL, 0, 1, (void *)pkg);
  758. maintainer_script_alternative(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
  759. "upgrade", "failed-upgrade");
  760. }
  761. /* If anything goes wrong while tidying up it's a bit late to do
  762. * anything about it. However, we don't install the new status
  763. * info yet, so that a future dpkg installation will put everything
  764. * right (we hope).
  765. *
  766. * If something does go wrong later the ‘conflictor’ package will be
  767. * left in the ‘removal_failed’ state. Removing or installing it
  768. * will be impossible if it was required because of the conflict with
  769. * the package we're installing now and (presumably) the dependency
  770. * by other packages. This means that the files it contains in
  771. * common with this package will hang around until we successfully
  772. * get this package installed, after which point we can trust the
  773. * conflicting package's file list, which will have been updated to
  774. * remove any files in this package. */
  775. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  776. /* Now we delete all the files that were in the old version of
  777. * the package only, except (old or new) conffiles, which we leave
  778. * alone. */
  779. reversefilelist_init(&rlistit,pkg->clientdata->files);
  780. while ((namenode= reversefilelist_next(&rlistit))) {
  781. struct filenamenode *usenode;
  782. if ((namenode->flags & fnnf_new_conff) ||
  783. (namenode->flags & fnnf_new_inarchive))
  784. continue;
  785. usenode = namenodetouse(namenode, pkg);
  786. trig_file_activate(usenode, pkg);
  787. varbuf_trunc(&fnamevb, fnameidlu);
  788. varbuf_add_str(&fnamevb, usenode->name);
  789. varbuf_end_str(&fnamevb);
  790. if (!stat(namenode->name,&stab) && S_ISDIR(stab.st_mode)) {
  791. debug(dbg_eachfiledetail, "process_archive: %s is a directory",
  792. namenode->name);
  793. if (dir_is_used_by_others(namenode, pkg))
  794. continue;
  795. }
  796. if (lstat(fnamevb.buf, &oldfs)) {
  797. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  798. warning(_("could not stat old file '%.250s' so not deleting it: %s"),
  799. fnamevb.buf, strerror(errno));
  800. continue;
  801. }
  802. if (S_ISDIR(oldfs.st_mode)) {
  803. if (rmdir(fnamevb.buf)) {
  804. warning(_("unable to delete old directory '%.250s': %s"),
  805. namenode->name, strerror(errno));
  806. } else if ((namenode->flags & fnnf_old_conff)) {
  807. warning(_("old conffile '%.250s' was an empty directory "
  808. "(and has now been deleted)"), namenode->name);
  809. }
  810. } else {
  811. struct fileinlist *sameas = NULL;
  812. static struct stat empty_stat;
  813. struct varbuf cfilename = VARBUF_INIT;
  814. /*
  815. * Ok, it's an old file, but is it really not in the new package?
  816. * It might be known by a different name because of symlinks.
  817. *
  818. * We need to check to make sure, so we stat the file, then compare
  819. * it to the new list. If we find a dev/inode match, we assume they
  820. * are the same file, and leave it alone. NOTE: we don't check in
  821. * other packages for sanity reasons (we don't want to stat _all_
  822. * the files on the system).
  823. *
  824. * We run down the list of _new_ files in this package. This keeps
  825. * the process a little leaner. We are only worried about new ones
  826. * since ones that stayed the same don't really apply here.
  827. */
  828. /* If we can't stat the old or new file, or it's a directory,
  829. * we leave it up to the normal code. */
  830. debug(dbg_eachfile, "process_archive: checking %s for same files on "
  831. "upgrade/downgrade", fnamevb.buf);
  832. for (cfile= newfileslist; cfile; cfile= cfile->next) {
  833. /* If the file has been filtered then treat it as if it didn't exist
  834. * on the file system. */
  835. if (cfile->namenode->flags & fnnf_filtered)
  836. continue;
  837. if (!cfile->namenode->filestat) {
  838. struct stat tmp_stat;
  839. varbuf_reset(&cfilename);
  840. varbuf_add_str(&cfilename, instdir);
  841. varbuf_add_char(&cfilename, '/');
  842. varbuf_add_str(&cfilename, cfile->namenode->name);
  843. varbuf_end_str(&cfilename);
  844. if (lstat(cfilename.buf, &tmp_stat) == 0) {
  845. cfile->namenode->filestat = nfmalloc(sizeof(struct stat));
  846. memcpy(cfile->namenode->filestat, &tmp_stat, sizeof(struct stat));
  847. } else {
  848. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  849. ohshite(_("unable to stat other new file `%.250s'"),
  850. cfile->namenode->name);
  851. cfile->namenode->filestat = &empty_stat;
  852. continue;
  853. }
  854. }
  855. if (cfile->namenode->filestat == &empty_stat)
  856. continue;
  857. if (oldfs.st_dev == cfile->namenode->filestat->st_dev &&
  858. oldfs.st_ino == cfile->namenode->filestat->st_ino) {
  859. if (sameas)
  860. warning(_("old file '%.250s' is the same as several new files! "
  861. "(both '%.250s' and '%.250s')"), fnamevb.buf,
  862. sameas->namenode->name, cfile->namenode->name);
  863. sameas= cfile;
  864. debug(dbg_eachfile, "process_archive: not removing %s,"
  865. " since it matches %s", fnamevb.buf, cfile->namenode->name);
  866. }
  867. }
  868. varbuf_destroy(&cfilename);
  869. if ((namenode->flags & fnnf_old_conff)) {
  870. if (sameas) {
  871. if (sameas->namenode->flags & fnnf_new_conff) {
  872. if (!strcmp(sameas->namenode->oldhash, NEWCONFFILEFLAG)) {
  873. sameas->namenode->oldhash= namenode->oldhash;
  874. debug(dbg_eachfile, "process_archive: old conff %s"
  875. " is same as new conff %s, copying hash",
  876. namenode->name, sameas->namenode->name);
  877. } else {
  878. debug(dbg_eachfile, "process_archive: old conff %s"
  879. " is same as new conff %s but latter already has hash",
  880. namenode->name, sameas->namenode->name);
  881. }
  882. }
  883. } else {
  884. debug(dbg_eachfile, "process_archive: old conff %s"
  885. " is disappearing", namenode->name);
  886. namenode->flags |= fnnf_obs_conff;
  887. newconff_append(&newconffileslastp, namenode);
  888. addfiletolist(&tc, namenode);
  889. }
  890. continue;
  891. }
  892. if (sameas)
  893. continue;
  894. if (secure_unlink_statted(fnamevb.buf, &oldfs)) {
  895. warning(_("unable to securely remove old file '%.250s': %s"),
  896. namenode->name, strerror(errno));
  897. }
  898. } /* !S_ISDIR */
  899. }
  900. /* OK, now we can write the updated files-in-this package list,
  901. * since we've done away (hopefully) with all the old junk. */
  902. write_filelist_except(pkg,newfileslist,0);
  903. /* Trigger interests may have changed.
  904. * Firstly we go through the old list of interests deleting them.
  905. * Then we go through the new list adding them. */
  906. strcpy(cidirrest, TRIGGERSCIFILE);
  907. trig_parse_ci(pkgadminfile(pkg, TRIGGERSCIFILE),
  908. trig_cicb_interest_delete, NULL, pkg);
  909. trig_parse_ci(cidir, trig_cicb_interest_add, NULL, pkg);
  910. trig_file_interests_save();
  911. /* We also install the new maintainer scripts, and any other
  912. * cruft that may have come along with the package. First
  913. * we go through the existing scripts replacing or removing
  914. * them as appropriate; then we go through the new scripts
  915. * (any that are left) and install them. */
  916. debug(dbg_general, "process_archive updating info directory");
  917. pkg_infodb_update(pkg, cidir, cidirrest);
  918. /*
  919. * Update the status database.
  920. *
  921. * This involves copying each field across from the ‘available’
  922. * to the ‘installed’ half of the pkg structure.
  923. * For some of the fields we have to do a complicated construction
  924. * operation; for others we can just copy the value.
  925. * We tackle the fields in the order they appear, so that
  926. * we don't miss any out :-).
  927. * At least we don't have to copy any strings that are referred
  928. * to, because these are never modified and never freed.
  929. */
  930. /* The dependencies are the most difficult. We have to build
  931. * a whole new forward dependency tree. At least the reverse
  932. * links (linking our deppossi's into the reverse chains)
  933. * can be done by copy_dependency_links. */
  934. newdeplist = NULL;
  935. newdeplistlastp = &newdeplist;
  936. for (dep= pkg->available.depends; dep; dep= dep->next) {
  937. newdep= nfmalloc(sizeof(struct dependency));
  938. newdep->up= pkg;
  939. newdep->next = NULL;
  940. newdep->list = NULL;
  941. newpossilastp = &newdep->list;
  942. for (possi= dep->list; possi; possi= possi->next) {
  943. newpossi= nfmalloc(sizeof(struct deppossi));
  944. newpossi->up= newdep;
  945. newpossi->ed= possi->ed;
  946. newpossi->next = NULL;
  947. newpossi->rev_next = newpossi->rev_prev = NULL;
  948. newpossi->verrel= possi->verrel;
  949. if (possi->verrel != dvr_none)
  950. newpossi->version= possi->version;
  951. else
  952. blankversion(&newpossi->version);
  953. newpossi->cyclebreak = false;
  954. *newpossilastp= newpossi;
  955. newpossilastp= &newpossi->next;
  956. }
  957. newdep->type= dep->type;
  958. *newdeplistlastp= newdep;
  959. newdeplistlastp= &newdep->next;
  960. }
  961. /* Right, now we've replicated the forward tree, we
  962. * get copy_dependency_links to remove all the old dependency
  963. * structures from the reverse links and add the new dependency
  964. * structures in instead. It also copies the new dependency
  965. * structure pointer for this package into the right field. */
  966. copy_dependency_links(pkg,&pkg->installed.depends,newdeplist,0);
  967. /* The ‘depended’ pointer in the structure doesn't represent anything
  968. * that is actually specified by this package - it's there so we
  969. * can find out what other packages refer to this one. So,
  970. * we don't copy it. We go straight on to copy the text fields. */
  971. pkg->installed.essential= pkg->available.essential;
  972. pkg->installed.description= pkg->available.description;
  973. pkg->installed.maintainer= pkg->available.maintainer;
  974. pkg->installed.source= pkg->available.source;
  975. pkg->installed.arch = pkg->available.arch;
  976. pkg->installed.installedsize= pkg->available.installedsize;
  977. pkg->installed.version= pkg->available.version;
  978. pkg->installed.origin = pkg->available.origin;
  979. pkg->installed.bugs = pkg->available.bugs;
  980. /* We have to generate our own conffiles structure. */
  981. pkg->installed.conffiles = NULL;
  982. iconffileslastp = &pkg->installed.conffiles;
  983. for (cfile= newconffiles; cfile; cfile= cfile->next) {
  984. newiconff= nfmalloc(sizeof(struct conffile));
  985. newiconff->next = NULL;
  986. newiconff->name= nfstrsave(cfile->namenode->name);
  987. newiconff->hash= nfstrsave(cfile->namenode->oldhash);
  988. newiconff->obsolete= !!(cfile->namenode->flags & fnnf_obs_conff);
  989. *iconffileslastp= newiconff;
  990. iconffileslastp= &newiconff->next;
  991. }
  992. /* We can just copy the arbitrary fields list, because it is
  993. * never even rearranged. Phew! */
  994. pkg->installed.arbs= pkg->available.arbs;
  995. /* Check for disappearing packages:
  996. * We go through all the packages on the system looking for ones
  997. * whose files are entirely part of the one we've just unpacked
  998. * (and which actually *have* some files!).
  999. *
  1000. * Any that we find are removed - we run the postrm with ‘disappear’
  1001. * as an argument, and remove their info/... files and status info.
  1002. * Conffiles are ignored (the new package had better do something
  1003. * with them!). */
  1004. it = pkg_db_iter_new();
  1005. while ((otherpkg = pkg_db_iter_next(it)) != NULL) {
  1006. ensure_package_clientdata(otherpkg);
  1007. if (otherpkg == pkg ||
  1008. otherpkg->status == stat_notinstalled ||
  1009. otherpkg->status == stat_configfiles ||
  1010. otherpkg->clientdata->istobe == itb_remove ||
  1011. !otherpkg->clientdata->files) continue;
  1012. debug(dbg_veryverbose, "process_archive checking disappearance %s",otherpkg->name);
  1013. assert(otherpkg->clientdata->istobe == itb_normal ||
  1014. otherpkg->clientdata->istobe == itb_deconfigure);
  1015. for (cfile= otherpkg->clientdata->files;
  1016. cfile && !strcmp(cfile->namenode->name,"/.");
  1017. cfile= cfile->next);
  1018. if (!cfile) {
  1019. debug(dbg_stupidlyverbose, "process_archive no non-root, no disappear");
  1020. continue;
  1021. }
  1022. for (cfile= otherpkg->clientdata->files;
  1023. cfile && !filesavespackage(cfile,otherpkg,pkg);
  1024. cfile= cfile->next);
  1025. if (cfile) continue;
  1026. /* So dependency things will give right answers ... */
  1027. otherpkg->clientdata->istobe= itb_remove;
  1028. debug(dbg_veryverbose, "process_archive disappear checking dependencies");
  1029. for (pdep= otherpkg->installed.depended;
  1030. pdep;
  1031. pdep = pdep->rev_next) {
  1032. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends &&
  1033. pdep->up->type != dep_recommends) continue;
  1034. if (depisok(pdep->up, &depprobwhy, NULL, NULL, false))
  1035. continue;
  1036. varbuf_end_str(&depprobwhy);
  1037. debug(dbg_veryverbose,"process_archive cannot disappear: %s",depprobwhy.buf);
  1038. break;
  1039. }
  1040. if (!pdep) {
  1041. /* If we haven't found a reason not to yet, let's look some more. */
  1042. for (providecheck= otherpkg->installed.depends;
  1043. providecheck;
  1044. providecheck= providecheck->next) {
  1045. if (providecheck->type != dep_provides) continue;
  1046. for (pdep= providecheck->list->ed->installed.depended;
  1047. pdep;
  1048. pdep = pdep->rev_next) {
  1049. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends &&
  1050. pdep->up->type != dep_recommends)
  1051. continue;
  1052. if (depisok(pdep->up, &depprobwhy, NULL, NULL, false))
  1053. continue;
  1054. varbuf_end_str(&depprobwhy);
  1055. debug(dbg_veryverbose,"process_archive cannot disappear (provides %s): %s",
  1056. providecheck->list->ed->name, depprobwhy.buf);
  1057. goto break_from_both_loops_at_once;
  1058. }
  1059. }
  1060. break_from_both_loops_at_once:;
  1061. }
  1062. otherpkg->clientdata->istobe= itb_normal;
  1063. if (pdep) continue;
  1064. printf(_("(Noting disappearance of %s, which has been completely replaced.)\n"),
  1065. otherpkg->name);
  1066. log_action("disappear", otherpkg);
  1067. debug(dbg_general, "process_archive disappearing %s",otherpkg->name);
  1068. /* No, we're disappearing it. This is the wrong time to go and
  1069. * run maintainer scripts and things, as we can't back out. But
  1070. * what can we do ? It has to be run this late. */
  1071. trig_activate_packageprocessing(otherpkg);
  1072. maintainer_script_installed(otherpkg, POSTRMFILE,
  1073. "post-removal script (for disappearance)",
  1074. "disappear", pkg->name,
  1075. versiondescribe(&pkg->available.version,
  1076. vdew_nonambig),
  1077. NULL);
  1078. /* OK, now we delete all the stuff in the ‘info’ directory .. */
  1079. debug(dbg_general, "process_archive disappear cleaning info directory");
  1080. pkg_infodb_foreach(otherpkg, pkg_infodb_remove_file);
  1081. dir_sync_path(pkgadmindir());
  1082. otherpkg->status= stat_notinstalled;
  1083. otherpkg->want = want_unknown;
  1084. otherpkg->eflag = eflag_ok;
  1085. blankversion(&otherpkg->configversion);
  1086. pkgbin_blank(&otherpkg->installed);
  1087. otherpkg->clientdata->fileslistvalid = false;
  1088. modstatdb_note(otherpkg);
  1089. } /* while (otherpkg= ... */
  1090. pkg_db_iter_free(it);
  1091. /* Delete files from any other packages' lists.
  1092. * We have to do this before we claim this package is in any
  1093. * sane kind of state, as otherwise we might delete by mistake
  1094. * a file that we overwrote, when we remove the package which
  1095. * had the version we overwrote. To prevent this we make
  1096. * sure that we don't claim this package is OK until we
  1097. * have claimed ‘ownership’ of all its files. */
  1098. for (cfile= newfileslist; cfile; cfile= cfile->next) {
  1099. struct filepackages_iterator *iter;
  1100. if (!(cfile->namenode->flags & fnnf_elide_other_lists)) continue;
  1101. if (cfile->namenode->divert && cfile->namenode->divert->useinstead) {
  1102. divpkg= cfile->namenode->divert->pkg;
  1103. if (divpkg == pkg) {
  1104. debug(dbg_eachfile,
  1105. "process_archive not overwriting any `%s' (overriding, `%s')",
  1106. cfile->namenode->name, cfile->namenode->divert->useinstead->name);
  1107. continue;
  1108. } else {
  1109. debug(dbg_eachfile,
  1110. "process_archive looking for overwriting `%s' (overridden by %s)",
  1111. cfile->namenode->name, divpkg ? divpkg->name : "<local>");
  1112. }
  1113. } else {
  1114. divpkg = NULL;
  1115. debug(dbg_eachfile, "process_archive looking for overwriting `%s'",
  1116. cfile->namenode->name);
  1117. }
  1118. iter = filepackages_iter_new(cfile->namenode);
  1119. while ((otherpkg = filepackages_iter_next(iter))) {
  1120. debug(dbg_eachfiledetail, "process_archive ... found in %s", otherpkg->name);
  1121. /* If !fileslistvalid then it's one of the disappeared packages above
  1122. * and we don't bother with it here, clearly. */
  1123. if (otherpkg == pkg || !otherpkg->clientdata->fileslistvalid)
  1124. continue;
  1125. if (otherpkg == divpkg) {
  1126. debug(dbg_eachfiledetail, "process_archive ... diverted, skipping");
  1127. continue;
  1128. }
  1129. /* Found one. We delete remove the list entry for this file,
  1130. * (and any others in the same package) and then mark the package
  1131. * as requiring a reread. */
  1132. write_filelist_except(otherpkg, otherpkg->clientdata->files,
  1133. fnnf_elide_other_lists);
  1134. ensure_package_clientdata(otherpkg);
  1135. debug(dbg_veryverbose, "process_archive overwrote from %s", otherpkg->name);
  1136. }
  1137. filepackages_iter_free(iter);
  1138. }
  1139. /* Right, the package we've unpacked is now in a reasonable state.
  1140. * The only thing that we have left to do with it is remove
  1141. * backup files, and we can leave the user to fix that if and when
  1142. * it happens (we leave the reinstall required flag, of course). */
  1143. pkg->status= stat_unpacked;
  1144. modstatdb_note(pkg);
  1145. /* Now we delete all the backup files that we made when
  1146. * extracting the archive - except for files listed as conffiles
  1147. * in the new package.
  1148. * This time we count it as an error if something goes wrong.
  1149. *
  1150. * Note that we don't ever delete things that were in the old
  1151. * package as a conffile and don't appear at all in the new.
  1152. * They stay recorded as obsolete conffiles and will eventually
  1153. * (if not taken over by another package) be forgotten. */
  1154. for (cfile= newfileslist; cfile; cfile= cfile->next) {
  1155. if (cfile->namenode->flags & fnnf_new_conff) continue;
  1156. varbuf_trunc(&fnametmpvb, fnameidlu);
  1157. varbuf_add_str(&fnametmpvb, namenodetouse(cfile->namenode, pkg)->name);
  1158. varbuf_add_str(&fnametmpvb, DPKGTEMPEXT);
  1159. varbuf_end_str(&fnametmpvb);
  1160. ensure_pathname_nonexisting(fnametmpvb.buf);
  1161. }
  1162. /* OK, we're now fully done with the main package.
  1163. * This is quite a nice state, so we don't unwind past here. */
  1164. pkg->eflag = eflag_ok;
  1165. modstatdb_note(pkg);
  1166. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  1167. /* Only the removal of the conflictor left to do.
  1168. * The files list for the conflictor is still a little inconsistent in-core,
  1169. * as we have not yet updated the filename->packages mappings; however,
  1170. * the package->filenames mapping is. */
  1171. for (i = 0 ; i < cflict_index ; i++) {
  1172. /* We need to have the most up-to-date info about which files are
  1173. * what ... */
  1174. ensure_allinstfiles_available();
  1175. removal_bulk(conflictor[i]);
  1176. }
  1177. if (cipaction->arg_int == act_install)
  1178. add_to_queue(pkg);
  1179. }