processarc.c 47 KB

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