processarc.c 48 KB

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