processarc.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /*
  2. * dpkg - main program for package management
  3. * processarc.c - the huge function process_archive
  4. *
  5. * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <errno.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <utime.h>
  27. #include <assert.h>
  28. #include <time.h>
  29. #include <ctype.h>
  30. #include <dirent.h>
  31. #include <fcntl.h>
  32. #include <sys/stat.h>
  33. #include <sys/types.h>
  34. #include <sys/wait.h>
  35. #include <config.h>
  36. #include <dpkg.h>
  37. #include <dpkg-db.h>
  38. #include <tarfn.h>
  39. #include <myopt.h>
  40. #include "filesdb.h"
  41. #include "main.h"
  42. #include "archives.h"
  43. void process_archive(const char *filename) {
  44. static const struct TarFunctions tf = {
  45. tarfileread,
  46. tarobject, tarobject, tarobject, tarobject, tarobject
  47. };
  48. /* These need to be static so that we can pass their addresses to
  49. * push_cleanup as arguments to the cu_xxx routines; if an error occurs
  50. * we unwind the stack before processing the cleanup list, and these
  51. * variables had better still exist ...
  52. */
  53. static int p1[2];
  54. static char cidirtmpnambuf[L_tmpnam+100];
  55. static char *cidirbuf=0, *reasmbuf=0;
  56. static struct fileinlist *newconffiles, *newfileslist;
  57. static enum pkgstatus oldversionstatus;
  58. static struct varbuf infofnvb, fnvb, depprobwhy;
  59. static struct tarcontext tc;
  60. int c1, r, admindirlen, i, infodirlen, infodirbaseused, status;
  61. struct pkgiterator *it;
  62. struct pkginfo *pkg, *conflictor, *otherpkg, *divpkg;
  63. char *cidir, *cidirrest, *p;
  64. char pfilenamebuf[35], conffilenamebuf[MAXCONFFILENAME];
  65. const char *pfilename, *newinfofilename;
  66. struct fileinlist *newconff, **newconffileslastp;
  67. struct fileinlist *cfile;
  68. struct reversefilelistiter rlistit;
  69. struct conffile *searchconff, **iconffileslastp, *newiconff;
  70. struct filepackages *packageslump;
  71. struct dependency *dsearch, *newdeplist, **newdeplistlastp;
  72. struct dependency *newdep, *dep, *providecheck;
  73. struct deppossi *psearch, **newpossilastp, *possi, *newpossi, *pdep;
  74. FILE *conff, *tmpf;
  75. DIR *dsd;
  76. struct filenamenode *namenode;
  77. struct dirent *de;
  78. struct stat stab;
  79. struct packageinlist *deconpil, *deconpiltemp;
  80. enum versiondisplayepochwhen needepochs;
  81. cleanup_pkg_failed= cleanup_conflictor_failed= 0;
  82. admindirlen= strlen(admindir);
  83. pfilename= filename;
  84. while (strlen(pfilename) > sizeof(pfilenamebuf)-5) {
  85. pfilename= strchr(pfilename,'/');
  86. if (!pfilename) break;
  87. pfilename++;
  88. }
  89. if (pfilename && pfilename != filename) {
  90. strcpy(pfilenamebuf,".../");
  91. strcat(pfilenamebuf,pfilename);
  92. pfilename= pfilenamebuf;
  93. } else {
  94. pfilename= filename;
  95. }
  96. if (stat(filename,&stab)) ohshite(_("cannot access archive"));
  97. if (!f_noact) {
  98. /* We can't `tentatively-reassemble' packages. */
  99. if (!reasmbuf) {
  100. reasmbuf= m_malloc(admindirlen+sizeof(REASSEMBLETMP)+5);
  101. strcpy(reasmbuf,admindir);
  102. strcat(reasmbuf,"/" REASSEMBLETMP);
  103. }
  104. if (unlink(reasmbuf) && errno != ENOENT)
  105. ohshite(_("error ensuring `%.250s' doesn't exist"),reasmbuf);
  106. push_cleanup(cu_pathname,~0, 0,0, 1,(void*)reasmbuf);
  107. c1= m_fork();
  108. if (!c1) {
  109. execlp(SPLITTER, SPLITTER,"-Qao",reasmbuf,filename,(char*)0);
  110. ohshite(_("failed to exec dpkg-split to see if it's part of a multiparter"));
  111. }
  112. while ((r= waitpid(c1,&status,0)) == -1 && errno == EINTR);
  113. if (r != c1) { onerr_abort++; ohshite(_("wait for dpkg-split failed")); }
  114. switch (WIFEXITED(status) ? WEXITSTATUS(status) : -1) {
  115. case 0:
  116. /* It was a part - is it complete ? */
  117. if (!stat(reasmbuf,&stab)) { /* Yes. */
  118. filename= reasmbuf;
  119. pfilename= "reassembled package file";
  120. break;
  121. } else if (errno == ENOENT) { /* No. That's it, we skip it. */
  122. return;
  123. }
  124. case 1:
  125. /* No, it wasn't a part. */
  126. break;
  127. default:
  128. checksubprocerr(status,SPLITTER,0);
  129. }
  130. }
  131. if (f_noact) {
  132. cidir= cidirtmpnambuf;
  133. if (!tmpnam(cidir)) ohshite(_("unable to get unique filename for control info"));
  134. strcat(cidir,"/");
  135. } else {
  136. /* We want it to be on the same filesystem so that we can
  137. * use rename(2) to install the postinst &c.
  138. */
  139. if (!cidirbuf)
  140. cidirbuf= m_malloc(admindirlen+sizeof(CONTROLDIRTMP)+MAXCONTROLFILENAME+10);
  141. cidir= cidirbuf;
  142. strcpy(cidir,admindir);
  143. strcat(cidir, "/" CONTROLDIRTMP);
  144. }
  145. cidirrest= cidir + strlen(cidir);
  146. assert(*cidir && cidirrest[-1] == '/'); cidirrest[-1]= 0;
  147. ensure_pathname_nonexisting(cidir); cidirrest[-1]= '/';
  148. push_cleanup(cu_cidir,~0, 0,0, 2,(void*)cidir,(void*)cidirrest);
  149. c1= m_fork();
  150. if (!c1) {
  151. cidirrest[-1]= 0;
  152. execlp(BACKEND, BACKEND,"--control",filename,cidir,(char*)0);
  153. ohshite(_("failed to exec dpkg-deb to extract control information"));
  154. }
  155. waitsubproc(c1,BACKEND " --control",0);
  156. strcpy(cidirrest,CONTROLFILE);
  157. parsedb(cidir, pdb_recordavailable|pdb_rejectstatus|pdb_weakclassification,
  158. &pkg,0,0);
  159. pkg->files= nfmalloc(sizeof(struct filedetails));
  160. pkg->files->next= 0;
  161. pkg->files->name= pkg->files->msdosname= pkg->files->md5sum= 0;
  162. pkg->files->size= nfmalloc(30);
  163. sprintf(pkg->files->size,"%lu",(unsigned long)stab.st_size);
  164. if (cipaction->arg == act_avail) {
  165. printf(_("Recorded info about %s from %s.\n"),pkg->name,pfilename);
  166. pop_cleanup(ehflag_normaltidy);
  167. return;
  168. }
  169. if (pkg->available.architecture && *pkg->available.architecture &&
  170. strcmp(pkg->available.architecture,"all") &&
  171. strcmp(pkg->available.architecture,architecture))
  172. forcibleerr(fc_architecture,
  173. _("package architecture (%s) does not match system (%s)"),
  174. pkg->available.architecture,architecture);
  175. if (!pkg->installed.valid) blankpackageperfile(&pkg->installed);
  176. assert(pkg->available.valid);
  177. for (deconpil= deconfigure;
  178. deconpil;
  179. deconpil= deconpiltemp) {
  180. deconpiltemp= deconpil->next;
  181. free(deconpil);
  182. }
  183. deconfigure= 0;
  184. clear_istobes();
  185. if (pkg->want != want_install) {
  186. if (f_alsoselect) {
  187. printf(_("Selecting previously deselected package %s.\n"),pkg->name);
  188. pkg->want= want_install;
  189. } else {
  190. printf(_("Skipping deselected package %s.\n"),pkg->name);
  191. return;
  192. }
  193. }
  194. if (pkg->status == stat_installed) {
  195. r= versioncompare(&pkg->available.version,&pkg->installed.version);
  196. if (r < 0) {
  197. needepochs= epochsdiffer(&pkg->available.version,&pkg->installed.version) ?
  198. vdew_always : vdew_never;
  199. if (fc_downgrade) {
  200. fprintf(stderr, _("dpkg - warning: downgrading %.250s from %.250s to %.250s.\n"),
  201. pkg->name,
  202. versiondescribe(&pkg->installed.version,needepochs),
  203. versiondescribe(&pkg->available.version,needepochs));
  204. } else {
  205. fprintf(stderr, _("Will not downgrade"
  206. " %.250s from version %.250s to %.250s, skipping.\n"),
  207. pkg->name,
  208. versiondescribe(&pkg->installed.version,needepochs),
  209. versiondescribe(&pkg->available.version,needepochs));
  210. pop_cleanup(ehflag_normaltidy);
  211. return;
  212. }
  213. } else if (r == 0 && f_skipsame && /* same version fully installed ? */
  214. pkg->status == stat_installed && !(pkg->eflag &= eflagf_reinstreq)) {
  215. fprintf(stderr, _("Version %.250s of %.250s already installed, skipping.\n"),
  216. versiondescribe(&pkg->installed.version,vdew_never),
  217. pkg->name);
  218. pop_cleanup(ehflag_normaltidy);
  219. return;
  220. }
  221. }
  222. pkg->clientdata->istobe= itb_installnew;
  223. conflictor= 0;
  224. for (dsearch= pkg->available.depends; dsearch; dsearch= dsearch->next) {
  225. switch (dsearch->type) {
  226. case dep_conflicts:
  227. /* Look for things we conflict with. */
  228. check_conflict(dsearch, pkg, pfilename, &conflictor);
  229. break;
  230. case dep_provides:
  231. /* Look for things that conflict with what we provide. */
  232. if (dsearch->list->ed->installed.valid) {
  233. for (psearch= dsearch->list->ed->installed.depended;
  234. psearch;
  235. psearch= psearch->nextrev) {
  236. if (psearch->up->type != dep_conflicts) continue;
  237. check_conflict(psearch->up, pkg, pfilename, &conflictor);
  238. }
  239. }
  240. break;
  241. case dep_suggests: case dep_recommends: case dep_depends: case dep_replaces:
  242. /* Ignore these here. */
  243. break;
  244. case dep_predepends:
  245. if (!depisok(dsearch,&depprobwhy,0,1)) {
  246. varbufaddc(&depprobwhy,0);
  247. fprintf(stderr, _("dpkg: regarding %s containing %s, pre-dependency problem:\n%s"),
  248. pfilename, pkg->name, depprobwhy.buf);
  249. if (!force_depends(dsearch->list))
  250. ohshit(_("pre-dependency problem - not installing %.250s"),pkg->name);
  251. fprintf(stderr, _("dpkg: warning - ignoring pre-dependency problem !\n"));
  252. }
  253. }
  254. }
  255. /* Look for things that conflict with us. */
  256. for (psearch= pkg->installed.depended; psearch; psearch= psearch->nextrev) {
  257. if (psearch->up->type != dep_conflicts) continue;
  258. check_conflict(psearch->up, pkg, pfilename, &conflictor);
  259. }
  260. ensure_allinstfiles_available();
  261. filesdbinit();
  262. if (pkg->status != stat_notinstalled && pkg->status != stat_configfiles)
  263. printf(_("Preparing to replace %s %s (using %s) ...\n"),
  264. pkg->name,
  265. versiondescribe(&pkg->installed.version,vdew_nonambig),
  266. pfilename);
  267. else
  268. printf(_("Unpacking %s (from %s) ...\n"),pkg->name,pfilename);
  269. if (f_noact) {
  270. pop_cleanup(ehflag_normaltidy);
  271. return;
  272. }
  273. /* OK, we're going ahead. First we read the conffiles, and copy the
  274. * hashes across.
  275. */
  276. newconffiles= 0; newconffileslastp= &newconffiles;
  277. push_cleanup(cu_fileslist,~0, 0,0, 1,(void*)&newconffiles);
  278. strcpy(cidirrest,CONFFILESFILE);
  279. conff= fopen(cidir,"r");
  280. if (conff) {
  281. push_cleanup(cu_closefile,ehflag_bombout, 0,0, 1,(void*)conff);
  282. while (fgets(conffilenamebuf,MAXCONFFILENAME-2,conff)) {
  283. p= conffilenamebuf + strlen(conffilenamebuf);
  284. assert(p != conffilenamebuf);
  285. if (p[-1] != '\n')
  286. ohshit(_("name of conffile (starting `%.250s') is too long (>%d characters)"),
  287. conffilenamebuf, MAXCONFFILENAME);
  288. while (p > conffilenamebuf && isspace(p[-1])) --p;
  289. if (p == conffilenamebuf) continue;
  290. *p= 0;
  291. newconff= m_malloc(sizeof(struct fileinlist));
  292. newconff->next= 0;
  293. newconff->namenode= findnamenode(conffilenamebuf);
  294. *newconffileslastp= newconff;
  295. newconffileslastp= &newconff->next;
  296. newconff->namenode->oldhash= NEWCONFFILEFLAG;
  297. /* Let's see if any packages have this file. If they do we
  298. * check to see if they listed it as a conffile, and if they did
  299. * we copy the hash across. Since (for plain file conffiles,
  300. * which is the only kind we are supposed to have) there will
  301. * only be one package which `has' the file, this will usually
  302. * mean we only look in the package which we're installing now.
  303. * The `conffiles' data in the status file is ignored when a
  304. * package isn't also listed in the file ownership database as
  305. * having that file. If several packages are listed as owning
  306. * the file we pick one at random.
  307. */
  308. searchconff= 0;
  309. for (packageslump= newconff->namenode->packages;
  310. packageslump;
  311. packageslump= packageslump->more) {
  312. for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) {
  313. otherpkg= packageslump->pkgs[i];
  314. debug(dbg_conffdetail,"process_archive conffile `%s' in package %s - conff ?",
  315. newconff->namenode->name,otherpkg->name);
  316. for (searchconff= otherpkg->installed.conffiles;
  317. searchconff && strcmp(newconff->namenode->name,searchconff->name);
  318. searchconff= searchconff->next)
  319. debug(dbg_conffdetail,
  320. "process_archive conffile `%s' in package %s - conff ? not `%s'",
  321. newconff->namenode->name,otherpkg->name,searchconff->name);
  322. if (searchconff) {
  323. debug(dbg_conff,"process_archive conffile `%s' package=%s %s hash=%s",
  324. newconff->namenode->name,otherpkg->name,
  325. otherpkg == pkg ? "same" : "different!",
  326. searchconff->hash);
  327. if (otherpkg == pkg) goto xit_conff_hashcopy_srch;
  328. }
  329. }
  330. }
  331. xit_conff_hashcopy_srch:
  332. if (searchconff) {
  333. newconff->namenode->oldhash= searchconff->hash;
  334. } else {
  335. debug(dbg_conff,"process_archive conffile `%s' no package, no hash",
  336. newconff->namenode->name);
  337. }
  338. newconff->namenode->flags |= fnnf_new_conff;
  339. }
  340. if (ferror(conff)) ohshite(_("read error in %.250s"),cidir);
  341. pop_cleanup(ehflag_normaltidy); /* conff= fopen() */
  342. if (fclose(conff)) ohshite(_("error closing %.250s"),cidir);
  343. } else {
  344. if (errno != ENOENT) ohshite(_("error trying to open %.250s"),cidir);
  345. }
  346. /* All the old conffiles are marked with a flag, so that we don't delete
  347. * them if they seem to disappear completely.
  348. */
  349. oldconffsetflags(pkg->installed.conffiles);
  350. if (conflictor) oldconffsetflags(conflictor->installed.conffiles);
  351. oldversionstatus= pkg->status;
  352. assert(oldversionstatus <= stat_configfiles);
  353. debug(dbg_general,"process_archive oldversionstatus=%s conflictor=%s",
  354. statusstrings[oldversionstatus], conflictor ? conflictor->name : "<none>");
  355. if (oldversionstatus == stat_halfconfigured || oldversionstatus == stat_installed) {
  356. pkg->eflag |= eflagf_reinstreq;
  357. pkg->status= stat_halfconfigured;
  358. modstatdb_note(pkg);
  359. push_cleanup(cu_prermupgrade,~ehflag_normaltidy, 0,0, 1,(void*)pkg);
  360. maintainer_script_alternative(pkg, PRERMFILE, "pre-removal", cidir, cidirrest,
  361. "upgrade", "failed-upgrade");
  362. pkg->status= stat_unpacked;
  363. oldversionstatus= stat_unpacked;
  364. modstatdb_note(pkg);
  365. }
  366. if (conflictor &&
  367. (conflictor->status == stat_halfconfigured ||
  368. conflictor->status == stat_installed)) {
  369. for (deconpil= deconfigure; deconpil; deconpil= deconpil->next) {
  370. printf(_("De-configuring %s, so that we can remove %s ...\n"),
  371. deconpil->pkg->name, conflictor->name);
  372. deconpil->pkg->status= stat_halfconfigured;
  373. modstatdb_note(deconpil->pkg);
  374. /* This means that we *either* go and run postinst abort-deconfigure,
  375. * *or* queue the package for later configure processing, depending
  376. * on which error cleanup route gets taken.
  377. */
  378. push_cleanup(cu_prermdeconfigure,~ehflag_normaltidy,
  379. ok_prermdeconfigure,ehflag_normaltidy,
  380. 3,(void*)deconpil->pkg,(void*)conflictor,(void*)pkg);
  381. maintainer_script_installed(deconpil->pkg, PRERMFILE, "pre-removal",
  382. "deconfigure", "in-favour", pkg->name,
  383. versiondescribe(&pkg->available.version,
  384. vdew_nonambig),
  385. "removing", conflictor->name,
  386. versiondescribe(&conflictor->installed.version,
  387. vdew_nonambig),
  388. (char*)0);
  389. }
  390. conflictor->status= stat_halfconfigured;
  391. modstatdb_note(conflictor);
  392. push_cleanup(cu_prerminfavour,~ehflag_normaltidy, 0,0,
  393. 2,(void*)conflictor,(void*)pkg);
  394. maintainer_script_installed(conflictor, PRERMFILE, "pre-removal",
  395. "remove", "in-favour", pkg->name,
  396. versiondescribe(&pkg->available.version,
  397. vdew_nonambig),
  398. (char*)0);
  399. conflictor->status= stat_halfinstalled;
  400. modstatdb_note(conflictor);
  401. }
  402. pkg->eflag |= eflagf_reinstreq;
  403. if (pkg->status == stat_notinstalled)
  404. pkg->installed.version= pkg->available.version;
  405. pkg->status= stat_halfinstalled;
  406. modstatdb_note(pkg);
  407. if (oldversionstatus == stat_notinstalled) {
  408. push_cleanup(cu_preinstverynew,~ehflag_normaltidy, 0,0,
  409. 3,(void*)pkg,(void*)cidir,(void*)cidirrest);
  410. maintainer_script_new(PREINSTFILE, "pre-installation", cidir, cidirrest,
  411. "install", (char*)0);
  412. } else if (oldversionstatus == stat_configfiles) {
  413. push_cleanup(cu_preinstnew,~ehflag_normaltidy, 0,0,
  414. 3,(void*)pkg,(void*)cidir,(void*)cidirrest);
  415. maintainer_script_new(PREINSTFILE, "pre-installation", cidir, cidirrest,
  416. "install", versiondescribe(&pkg->installed.version,
  417. vdew_nonambig),
  418. (char*)0);
  419. } else {
  420. push_cleanup(cu_preinstupgrade,~ehflag_normaltidy, 0,0,
  421. 4,(void*)pkg,(void*)cidir,(void*)cidirrest,(void*)&oldversionstatus);
  422. maintainer_script_new(PREINSTFILE, "pre-installation", cidir, cidirrest,
  423. "upgrade", versiondescribe(&pkg->installed.version,
  424. vdew_nonambig),
  425. (char*)0);
  426. printf(_("Unpacking replacement %.250s ...\n"),pkg->name);
  427. }
  428. /*
  429. * Now we unpack the archive, backing things up as we go.
  430. * For each file, we check to see if it already exists.
  431. * There are several possibilities:
  432. * + We are trying to install a non-directory ...
  433. * - It doesn't exist. In this case we simply extract it.
  434. * - It is a plain file, device, symlink, &c. We do an `atomic
  435. * overwrite' using link() and rename(), but leave a backup copy.
  436. * Later, when we delete the backup, we remove it from any other
  437. * packages' lists.
  438. * - It is a directory. In this case it depends on whether we're
  439. * trying to install a symlink or something else.
  440. * = If we're not trying to install a symlink we move the directory
  441. * aside and extract the node. Later, when we recursively remove
  442. * the backed-up directory, we remove it from any other packages'
  443. * lists.
  444. * = If we are trying to install a symlink we do nothing - ie,
  445. * dpkg will never replace a directory tree with a symlink. This
  446. * is to avoid embarrassing effects such as replacing a directory
  447. * tree with a link to a link to the original directory tree.
  448. * + We are trying to install a directory ...
  449. * - It doesn't exist. We create it with the appropriate modes.
  450. * - It exists as a directory or a symlink to one. We do nothing.
  451. * - It is a plain file or a symlink (other than to a directory).
  452. * We move it aside and create the directory. Later, when we
  453. * delete the backup, we remove it from any other packages' lists.
  454. *
  455. * Install non-dir Install symlink Install dir
  456. * Exists not X X X
  457. * File/node/symlink LXR LXR BXR
  458. * Directory BXR - -
  459. *
  460. * X: extract file/node/link/directory
  461. * LX: atomic overwrite leaving backup
  462. * B: ordinary backup
  463. * R: later remove from other packages' lists
  464. * -: do nothing
  465. *
  466. * After we've done this we go through the remaining things in the
  467. * lists of packages we're trying to remove (including the old
  468. * version of the current package). This happens in reverse order,
  469. * so that we process files before the directories (or symlinks-to-
  470. * directories) containing them.
  471. * + If the thing is a conffile then we leave it alone for the purge
  472. * operation.
  473. * + Otherwise, there are several possibilities too:
  474. * - The listed thing does not exist. We ignore it.
  475. * - The listed thing is a directory or a symlink to a directory.
  476. * We delete it only if it isn't listed in any other package.
  477. * - The listed thing is not a directory or a symlink to one (ie,
  478. * it's a plain file, device, pipe, &c, or a symlink to one, or a
  479. * dangling symlink). We delete it.
  480. * The removed packages' list becomes empty (of course, the new
  481. * version of the package we're installing will have a new list,
  482. * which replaces the old version's list).
  483. *
  484. * If at any stage we remove a file from a package's list, and the
  485. * package isn't one we're already processing, and the package's
  486. * list becomes empty as a result, we `vanish' the package. This
  487. * means that we run its postrm with the `disappear' argument, and
  488. * put the package in the `not-installed' state. Its conffiles are
  489. * ignored and forgotten about.
  490. *
  491. * NOTE THAT THE OLD POSTRM IS RUN AFTER THE NEW PREINST, since the
  492. * files get replaced `as we go'.
  493. */
  494. m_pipe(p1);
  495. push_cleanup(cu_closepipe,ehflag_bombout, 0,0, 1,(void*)&p1[0]);
  496. c1= m_fork();
  497. if (!c1) {
  498. m_dup2(p1[1],1); close(p1[0]); close(p1[1]);
  499. execlp(BACKEND, BACKEND, "--fsys-tarfile", filename, (char*)0);
  500. ohshite(_("unable to exec dpkg-deb to get filesystem archive"));
  501. }
  502. close(p1[1]);
  503. newfileslist= 0; tc.newfilesp= &newfileslist;
  504. push_cleanup(cu_fileslist,~0, 0,0, 1,(void*)&newfileslist);
  505. tc.pkg= pkg;
  506. tc.backendpipe= fdopen(p1[0],"r");
  507. if (!tc.backendpipe) ohshite(_("unable to fdopen dpkg-deb extract pipe"));
  508. push_cleanup(cu_backendpipe,~ehflag_bombout, 0,0, 1,(void*)&tc.backendpipe);
  509. r= TarExtractor((void*)&tc, &tf);
  510. if (r) {
  511. if (errno) {
  512. ohshite(_("error reading dpkg-deb tar output"));
  513. } else if (feof(tc.backendpipe)) {
  514. waitsubproc(c1,BACKEND " --fsys-tarfile (EOF)",1);
  515. ohshit(_("unexpected EOF in filesystem tarfile - corrupted package archive"));
  516. } else {
  517. ohshit(_("corrupted filesystem tarfile - corrupted package archive"));
  518. }
  519. }
  520. tmpf= tc.backendpipe;
  521. tc.backendpipe= 0;
  522. fclose(tmpf);
  523. waitsubproc(c1,BACKEND " --fsys-tarfile",1);
  524. if (oldversionstatus == stat_halfinstalled || oldversionstatus == stat_unpacked) {
  525. /* Packages that were in `installed' and `postinstfailed' have been reduced
  526. * to `unpacked' by now, by the running of the prerm script.
  527. */
  528. pkg->status= stat_halfinstalled;
  529. modstatdb_note(pkg);
  530. push_cleanup(cu_postrmupgrade,~ehflag_normaltidy, 0,0, 1,(void*)pkg);
  531. maintainer_script_alternative(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
  532. "upgrade", "failed-upgrade");
  533. }
  534. /* If anything goes wrong while tidying up it's a bit late to do
  535. * anything about it. However, we don't install the new status
  536. * info yet, so that a future dpkg installation will put everything
  537. * right (we hope).
  538. *
  539. * If something does go wrong later the `conflictor' package will be
  540. * left in the `removal_failed' state. Removing or installing it
  541. * will be impossible if it was required because of the conflict with
  542. * the package we're installing now and (presumably) the dependency
  543. * by other packages. This means that the files it contains in
  544. * common with this package will hang around until we successfully
  545. * get this package installed, after which point we can trust the
  546. * conflicting package's file list, which will have been updated to
  547. * remove any files in this package.
  548. */
  549. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  550. /* Now we delete all the files that were in the old version of
  551. * the package only, except (old or new) conffiles, which we leave
  552. * alone.
  553. */
  554. reversefilelist_init(&rlistit,pkg->clientdata->files);
  555. while ((namenode= reversefilelist_next(&rlistit))) {
  556. if ((namenode->flags & fnnf_old_conff) ||
  557. (namenode->flags & fnnf_new_conff) ||
  558. (namenode->flags & fnnf_new_inarchive))
  559. continue;
  560. if (isdirectoryinuse(namenode,pkg)) continue;
  561. fnamevb.used= fnameidlu;
  562. varbufaddstr(&fnamevb, namenodetouse(namenode,pkg)->name);
  563. varbufaddc(&fnamevb,0);
  564. if (!rmdir(fnamevb.buf)) continue;
  565. if (errno == ENOENT || errno == ELOOP) continue;
  566. if (errno == ENOTDIR) {
  567. if (!unlink(fnamevb.buf)) continue;
  568. if (errno == ENOTDIR) continue;
  569. }
  570. fprintf(stderr,
  571. _("dpkg: warning - unable to delete old file `%.250s': %s\n"),
  572. namenode->name, strerror(errno));
  573. }
  574. /* OK, now we can write the updated files-in-this package list,
  575. * since we've done away (hopefully) with all the old junk.
  576. */
  577. write_filelist_except(pkg,newfileslist,0);
  578. /* We also install the new maintainer scripts, and any other
  579. * cruft that may have come along with the package. First
  580. * we go through the existing scripts replacing or removing
  581. * them as appropriate; then we go through the new scripts
  582. * (any that are left) and install them.
  583. */
  584. debug(dbg_general, "process_archive updating info directory");
  585. varbufreset(&infofnvb);
  586. varbufaddstr(&infofnvb,admindir);
  587. varbufaddstr(&infofnvb,"/" INFODIR "/");
  588. infodirlen= infofnvb.used;
  589. varbufaddc(&infofnvb,0);
  590. dsd= opendir(infofnvb.buf);
  591. if (!dsd) ohshite(_("cannot read info directory"));
  592. push_cleanup(cu_closedir,~0, 0,0, 1,(void*)dsd);
  593. while ((de= readdir(dsd)) != 0) {
  594. debug(dbg_veryverbose, "process_archive info file `%s'", de->d_name);
  595. if (de->d_name[0] == '.') continue; /* ignore dotfiles, including `.' and `..' */
  596. p= strrchr(de->d_name,'.'); if (!p) continue; /* ignore anything odd */
  597. if (strlen(pkg->name) != p-de->d_name ||
  598. strncmp(de->d_name,pkg->name,p-de->d_name)) continue;
  599. debug(dbg_stupidlyverbose, "process_archive info this pkg");
  600. /* Right do we have one ? */
  601. p++; /* skip past the full stop */
  602. if (!strcmp(p,LISTFILE)) continue; /* We do the list separately */
  603. if (strlen(p) > MAXCONTROLFILENAME)
  604. ohshit(_("old version of package has overly-long info file name starting `%.250s'"),
  605. de->d_name);
  606. infofnvb.used= infodirlen;
  607. varbufaddstr(&infofnvb,de->d_name);
  608. varbufaddc(&infofnvb,0);
  609. strcpy(cidirrest,p);
  610. if (!rename(cidir,infofnvb.buf)) {
  611. debug(dbg_scripts, "process_archive info installed %s as %s",
  612. cidir, infofnvb.buf);
  613. } else if (errno == ENOENT) {
  614. /* Right, no new version. */
  615. if (unlink(infofnvb.buf))
  616. ohshite(_("unable to remove obsolete info file `%.250s'"),infofnvb.buf);
  617. debug(dbg_scripts, "process_archive info unlinked %s",infofnvb.buf);
  618. } else {
  619. ohshite(_("unable to install (supposed) new info file `%.250s'"),cidir);
  620. }
  621. }
  622. pop_cleanup(ehflag_normaltidy); /* closedir */
  623. *cidirrest= 0; /* the directory itself */
  624. dsd= opendir(cidir);
  625. if (!dsd) ohshite(_("unable to open temp control directory"));
  626. push_cleanup(cu_closedir,~0, 0,0, 1,(void*)dsd);
  627. while ((de= readdir(dsd))) {
  628. if (strchr(de->d_name,'.')) {
  629. debug(dbg_scripts,"process_archive tmp.ci script/file `%s' contains dot",
  630. de->d_name);
  631. continue;
  632. }
  633. if (strlen(de->d_name) > MAXCONTROLFILENAME)
  634. ohshit(_("package contains overly-long control info file name (starting `%.50s')"),
  635. de->d_name);
  636. strcpy(cidirrest,de->d_name);
  637. /* First we check it's not a directory. */
  638. if (!rmdir(cidir))
  639. ohshit(_("package control info contained directory `%.250s'"),cidir);
  640. else if (errno != ENOTDIR)
  641. ohshite(_("package control info rmdir of `%.250s' didn't say not a dir"),de->d_name);
  642. if (!strcmp(de->d_name,CONTROLFILE)) {
  643. debug(dbg_scripts,"process_archive tmp.ci script/file `%s' is control",cidir);
  644. continue; /* ignore the control file */
  645. }
  646. if (!strcmp(de->d_name,LISTFILE)) {
  647. fprintf(stderr, _("dpkg: warning - package %s"
  648. " contained list as info file"), pkg->name);
  649. continue;
  650. }
  651. /* Right, install it */
  652. newinfofilename= pkgadminfile(pkg,de->d_name);
  653. if (rename(cidir,newinfofilename))
  654. ohshite(_("unable to install new info file `%.250s' as `%.250s'"),
  655. cidir,newinfofilename);
  656. debug(dbg_scripts,"process_archive tmp.ci script/file `%s' installed as `%s'",
  657. cidir, newinfofilename);
  658. }
  659. pop_cleanup(ehflag_normaltidy); /* closedir */
  660. /* Update the status database.
  661. * This involves copying each field across from the `available'
  662. * to the `installed' half of the pkg structure.
  663. * For some of the fields we have to do a complicated construction
  664. * operation; for others we can just copy the value.
  665. * We tackle the fields in the order they appear, so that
  666. * we don't miss any out :-).
  667. * At least we don't have to copy any strings that are referred
  668. * to, because these are never modified and never freed.
  669. */
  670. /* The dependencies are the most difficult. We have to build
  671. * a whole new forward dependency tree. At least the reverse
  672. * links (linking our deppossi's into the reverse chains)
  673. * can be done by copy_dependency_links.
  674. */
  675. newdeplist= 0; newdeplistlastp= &newdeplist;
  676. for (dep= pkg->available.depends; dep; dep= dep->next) {
  677. newdep= nfmalloc(sizeof(struct dependency));
  678. newdep->up= pkg;
  679. newdep->next= 0;
  680. newdep->list= 0; newpossilastp= &newdep->list;
  681. for (possi= dep->list; possi; possi= possi->next) {
  682. newpossi= nfmalloc(sizeof(struct deppossi));
  683. newpossi->up= newdep;
  684. newpossi->ed= possi->ed;
  685. newpossi->next= 0; newpossi->nextrev= newpossi->backrev= 0;
  686. newpossi->verrel= possi->verrel;
  687. if (possi->verrel != dvr_none) newpossi->version= possi->version;
  688. newpossi->cyclebreak= 0;
  689. *newpossilastp= newpossi;
  690. newpossilastp= &newpossi->next;
  691. }
  692. newdep->type= dep->type;
  693. *newdeplistlastp= newdep;
  694. newdeplistlastp= &newdep->next;
  695. }
  696. /* Right, now we've replicated the forward tree, we
  697. * get copy_dependency_links to remove all the old dependency
  698. * structures from the reverse links and add the new dependency
  699. * structures in instead. It also copies the new dependency
  700. * structure pointer for this package into the right field.
  701. */
  702. copy_dependency_links(pkg,&pkg->installed.depends,newdeplist,0);
  703. /* The `depended' pointer in the structure doesn't represent anything
  704. * that is actually specified by this package - it's there so we
  705. * can find out what other packages refer to this one. So,
  706. * we don't copy it. We go straight on to copy the text fields.
  707. */
  708. pkg->installed.essential= pkg->available.essential;
  709. pkg->installed.description= pkg->available.description;
  710. pkg->installed.maintainer= pkg->available.maintainer;
  711. pkg->installed.source= pkg->available.source;
  712. pkg->installed.architecture= 0; /* This is irrelevant in the status file. */
  713. pkg->installed.installedsize= pkg->available.installedsize;
  714. pkg->installed.version= pkg->available.version;
  715. /* We have to generate our own conffiles structure. */
  716. pkg->installed.conffiles= 0; iconffileslastp= &pkg->installed.conffiles;
  717. for (cfile= newconffiles; cfile; cfile= cfile->next) {
  718. newiconff= nfmalloc(sizeof(struct conffile));
  719. newiconff->next= 0;
  720. newiconff->name= nfstrsave(cfile->namenode->name);
  721. newiconff->hash= nfstrsave(cfile->namenode->oldhash);
  722. *iconffileslastp= newiconff;
  723. iconffileslastp= &newiconff->next;
  724. }
  725. /* We can just copy the arbitrary fields list, because it is
  726. * never even rearragned. Phew !
  727. */
  728. pkg->installed.arbs= pkg->available.arbs;
  729. /* Check for disappearing packages:
  730. * We go through all the packages on the system looking for ones
  731. * whose files are entirely part of the one we've just unpacked
  732. * (and which actually *have* some files!).
  733. *
  734. * Any that we find are removed - we run the postrm with `disappear'
  735. * as an argument, and remove their info/... files and status info.
  736. * Conffiles are ignored (the new package had better do something
  737. * with them !).
  738. */
  739. it= iterpkgstart();
  740. while ((otherpkg= iterpkgnext(it)) != 0) {
  741. ensure_package_clientdata(otherpkg);
  742. if (otherpkg == pkg ||
  743. otherpkg == conflictor ||
  744. otherpkg->status == stat_notinstalled ||
  745. otherpkg->status == stat_configfiles ||
  746. !otherpkg->clientdata->files) continue;
  747. debug(dbg_veryverbose, "process_archive checking disappearance %s",otherpkg->name);
  748. assert(otherpkg->clientdata->istobe == itb_normal ||
  749. otherpkg->clientdata->istobe == itb_deconfigure);
  750. for (cfile= otherpkg->clientdata->files;
  751. cfile && !strcmp(cfile->namenode->name,"/.");
  752. cfile= cfile->next);
  753. if (!cfile) {
  754. debug(dbg_stupidlyverbose, "process_archive no non-root, no disappear");
  755. continue;
  756. }
  757. for (cfile= otherpkg->clientdata->files;
  758. cfile && !filesavespackage(cfile,otherpkg,pkg);
  759. cfile= cfile->next);
  760. if (cfile) continue;
  761. /* So dependency things will give right answers ... */
  762. otherpkg->clientdata->istobe= itb_remove;
  763. debug(dbg_veryverbose, "process_archive disappear checking dependencies");
  764. for (pdep= otherpkg->installed.depended;
  765. pdep;
  766. pdep= pdep->nextrev) {
  767. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends &&
  768. pdep->up->type != dep_recommends) continue;
  769. if (depisok(pdep->up, &depprobwhy, 0,0)) continue;
  770. varbufaddc(&depprobwhy,0);
  771. debug(dbg_veryverbose,"process_archive cannot disappear: %s",depprobwhy.buf);
  772. break;
  773. }
  774. if (!pdep) {
  775. /* If we haven't found a reason not to yet, let's look some more. */
  776. for (providecheck= otherpkg->installed.depends;
  777. providecheck;
  778. providecheck= providecheck->next) {
  779. if (providecheck->type != dep_provides) continue;
  780. for (pdep= providecheck->list->ed->installed.depended;
  781. pdep;
  782. pdep= pdep->nextrev) {
  783. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends &&
  784. pdep->up->type != dep_recommends)
  785. continue;
  786. if (depisok(pdep->up, &depprobwhy, 0,0)) continue;
  787. varbufaddc(&depprobwhy,0);
  788. debug(dbg_veryverbose,"process_archive cannot disappear (provides %s): %s",
  789. providecheck->list->ed->name, depprobwhy.buf);
  790. goto break_from_both_loops_at_once;
  791. }
  792. }
  793. break_from_both_loops_at_once:;
  794. }
  795. otherpkg->clientdata->istobe= itb_normal;
  796. if (pdep) continue;
  797. printf(_("(Noting disappearance of %s, which has been completely replaced.)\n"),
  798. otherpkg->name);
  799. debug(dbg_general, "process_archive disappearing %s",otherpkg->name);
  800. /* No, we're disappearing it. This is the wrong time to go and
  801. * run maintainer scripts and things, as we can't back out. But
  802. * what can we do ? It has to be run this late.
  803. */
  804. maintainer_script_installed(otherpkg, POSTRMFILE,
  805. "post-removal script (for disappearance)",
  806. "disappear", pkg->name,
  807. versiondescribe(&pkg->available.version,
  808. vdew_nonambig),
  809. (char*)0);
  810. /* OK, now we delete all the stuff in the `info' directory .. */
  811. varbufreset(&fnvb);
  812. varbufaddstr(&fnvb,admindir);
  813. varbufaddstr(&fnvb,"/" INFODIR);
  814. infodirbaseused= fnvb.used;
  815. varbufaddc(&fnvb,0);
  816. dsd= opendir(fnvb.buf); if (!dsd) ohshite(_("cannot read info directory"));
  817. push_cleanup(cu_closedir,~0, 0,0, 1,(void*)dsd);
  818. debug(dbg_general, "process_archive disappear cleaning info directory");
  819. while ((de= readdir(dsd)) != 0) {
  820. debug(dbg_veryverbose, "process_archive info file `%s'", de->d_name);
  821. if (de->d_name[0] == '.') continue;
  822. p= strrchr(de->d_name,'.'); if (!p) continue;
  823. if (strlen(otherpkg->name) != p-de->d_name ||
  824. strncmp(de->d_name,otherpkg->name,p-de->d_name)) continue;
  825. debug(dbg_stupidlyverbose, "process_archive info this pkg");
  826. fnvb.used= infodirbaseused;
  827. varbufaddstr(&fnvb,de->d_name);
  828. varbufaddc(&fnvb,0);
  829. if (unlink(fnvb.buf))
  830. ohshite(_("unable to delete disappearing control info file `%.250s'"),fnvb.buf);
  831. debug(dbg_scripts, "process_archive info unlinked %s",fnvb.buf);
  832. }
  833. pop_cleanup(ehflag_normaltidy); /* closedir */
  834. otherpkg->status= stat_notinstalled;
  835. otherpkg->want= want_purge;
  836. otherpkg->eflag= eflagv_ok;
  837. otherpkg->installed.depends= 0;
  838. otherpkg->installed.essential= 0;
  839. otherpkg->installed.description= otherpkg->installed.maintainer= 0;
  840. otherpkg->installed.installedsize= otherpkg->installed.source= 0;
  841. otherpkg->installed.conffiles= 0;
  842. blankversion(&otherpkg->installed.version);
  843. otherpkg->installed.arbs= 0;
  844. otherpkg->clientdata->fileslistvalid= 0;
  845. modstatdb_note(otherpkg);
  846. } /* while (otherpkg= ... */
  847. iterpkgend(it);
  848. /* Delete files from any other packages' lists.
  849. * We have to do this before we claim this package is in any
  850. * sane kind of state, as otherwise we might delete by mistake
  851. * a file that we overwrote, when we remove the package which
  852. * had the version we overwrote. To prevent this we make
  853. * sure that we don't claim this package is OK until we
  854. * have claimed `ownership' of all its files.
  855. */
  856. for (cfile= newfileslist; cfile; cfile= cfile->next) {
  857. if (!(cfile->namenode->flags & fnnf_elide_other_lists)) continue;
  858. if (cfile->namenode->divert && cfile->namenode->divert->useinstead) {
  859. divpkg= cfile->namenode->divert->pkg;
  860. if (divpkg == pkg) {
  861. debug(dbg_eachfile,
  862. "process_archive not overwriting any `%s' (overriding, `%s')",
  863. cfile->namenode->name, cfile->namenode->divert->useinstead->name);
  864. continue;
  865. } else {
  866. debug(dbg_eachfile,
  867. "process_archive looking for overwriting `%s' (overridden by %s)",
  868. cfile->namenode->name, divpkg ? divpkg->name : "<local>");
  869. }
  870. } else {
  871. divpkg= 0;
  872. debug(dbg_eachfile, "process_archive looking for overwriting `%s'",
  873. cfile->namenode->name);
  874. }
  875. for (packageslump= cfile->namenode->packages;
  876. packageslump;
  877. packageslump= packageslump->more) {
  878. for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) {
  879. otherpkg= packageslump->pkgs[i];
  880. debug(dbg_eachfiledetail, "process_archive ... found in %s\n",otherpkg->name);
  881. /* If !fileslistvalid then it's one of the disappeared packages above
  882. * and we don't bother with it here, clearly.
  883. */
  884. if (otherpkg == pkg || !otherpkg->clientdata->fileslistvalid) continue;
  885. if (otherpkg == divpkg) {
  886. debug(dbg_eachfiledetail, "process_archive ... diverted, skipping\n");
  887. continue;
  888. }
  889. /* Found one. We delete remove the list entry for this file,
  890. * (and any others in the same package) and then mark the package
  891. * as requiring a reread.
  892. */
  893. write_filelist_except(otherpkg, otherpkg->clientdata->files, 1);
  894. ensure_package_clientdata(otherpkg);
  895. debug(dbg_veryverbose, "process_archive overwrote from %s",otherpkg->name);
  896. }
  897. }
  898. }
  899. /* Right, the package we've unpacked is now in a reasonable state.
  900. * The only thing that we have left to do with it is remove
  901. * backup files, and we can leave the user to fix that if and when
  902. * it happens (we leave the reinstall required flag, of course).
  903. */
  904. pkg->status= stat_unpacked;
  905. modstatdb_note(pkg);
  906. /* Now we delete all the backup files that we made when
  907. * extracting the archive - except for files listed as conffiles
  908. * in the new package.
  909. * This time we count it as an error if something goes wrong.
  910. *
  911. * Note that we don't ever delete things that were in the old
  912. * package as a conffile and don't appear at all in the new.
  913. */
  914. for (cfile= newfileslist; cfile; cfile= cfile->next) {
  915. if (cfile->namenode->flags & fnnf_new_conff) continue;
  916. fnametmpvb.used= fnameidlu;
  917. varbufaddstr(&fnametmpvb,namenodetouse(cfile->namenode,pkg)->name);
  918. varbufaddstr(&fnametmpvb,DPKGTEMPEXT);
  919. varbufaddc(&fnametmpvb,0);
  920. ensure_pathname_nonexisting(fnametmpvb.buf);
  921. }
  922. /* OK, we're now fully done with the main package.
  923. * This is quite a nice state, so we don't unwind past here.
  924. */
  925. pkg->eflag= eflagv_ok;
  926. modstatdb_note(pkg);
  927. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  928. /* Only the removal of the conflictor left to do.
  929. * The files list for the conflictor is still a little inconsistent in-core,
  930. * as we have not yet updated the filename->packages mappings; however,
  931. * the package->filenames mapping is
  932. */
  933. if (conflictor) {
  934. /* We need to have the most up-to-date info about which files are what ... */
  935. ensure_allinstfiles_available();
  936. removal_bulk(conflictor);
  937. }
  938. if (cipaction->arg == act_install) add_to_queue(pkg);
  939. }