processarc.c 48 KB

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