processarc.c 41 KB

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