processarc.c 52 KB

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