processarc.c 49 KB

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