processarc.c 48 KB

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