unpack.c 53 KB

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