processarc.c 52 KB

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