processarc.c 48 KB

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