processarc.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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 (using %s) ...\n",pkg->name,pfilename);
  263. else
  264. printf("Unpacking %s (from %s) ...\n",pkg->name,pfilename);
  265. if (f_noact) {
  266. pop_cleanup(ehflag_normaltidy);
  267. return;
  268. }
  269. /* OK, we're going ahead. First we read the conffiles, and copy the
  270. * hashes across.
  271. */
  272. newconffiles= 0; newconffileslastp= &newconffiles;
  273. push_cleanup(cu_fileslist,~0, 0,0, 1,(void*)&newconffiles);
  274. strcpy(cidirrest,CONFFILESFILE);
  275. conff= fopen(cidir,"r");
  276. if (conff) {
  277. push_cleanup(cu_closefile,ehflag_bombout, 0,0, 1,(void*)conff);
  278. while (fgets(conffilenamebuf,MAXCONFFILENAME-2,conff)) {
  279. p= conffilenamebuf + strlen(conffilenamebuf);
  280. assert(p != conffilenamebuf);
  281. if (p[-1] != '\n')
  282. ohshit("name of conffile (starting `%.250s') is too long (>%d characters)",
  283. conffilenamebuf, MAXCONFFILENAME);
  284. while (p > conffilenamebuf && isspace(p[-1])) --p;
  285. if (p == conffilenamebuf) continue;
  286. *p= 0;
  287. newconff= m_malloc(sizeof(struct fileinlist));
  288. newconff->next= 0;
  289. newconff->namenode= findnamenode(conffilenamebuf);
  290. *newconffileslastp= newconff;
  291. newconffileslastp= &newconff->next;
  292. newconff->namenode->oldhash= NEWCONFFILEFLAG;
  293. /* Let's see if any packages have this file. If they do we
  294. * check to see if they listed it as a conffile, and if they did
  295. * we copy the hash across. Since (for plain file conffiles,
  296. * which is the only kind we are supposed to have) there will
  297. * only be one package which `has' the file, this will usually
  298. * mean we only look in the package which we're installing now.
  299. * The `conffiles' data in the status file is ignored when a
  300. * package isn't also listed in the file ownership database as
  301. * having that file. If several packages are listed as owning
  302. * the file we pick one at random.
  303. */
  304. searchconff= 0;
  305. for (packageslump= newconff->namenode->packages;
  306. packageslump;
  307. packageslump= packageslump->more) {
  308. for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) {
  309. otherpkg= packageslump->pkgs[i];
  310. debug(dbg_conffdetail,"process_archive conffile `%s' in package %s - conff ?",
  311. newconff->namenode->name,otherpkg->name);
  312. for (searchconff= otherpkg->installed.conffiles;
  313. searchconff && strcmp(newconff->namenode->name,searchconff->name);
  314. searchconff= searchconff->next)
  315. debug(dbg_conffdetail,
  316. "process_archive conffile `%s' in package %s - conff ? not `%s'",
  317. newconff->namenode->name,otherpkg->name,searchconff->name);
  318. if (searchconff) {
  319. debug(dbg_conff,"process_archive conffile `%s' package=%s %s hash=%s",
  320. newconff->namenode->name,otherpkg->name,
  321. otherpkg == pkg ? "same" : "different!",
  322. searchconff->hash);
  323. if (otherpkg == pkg) goto xit_conff_hashcopy_srch;
  324. }
  325. }
  326. }
  327. xit_conff_hashcopy_srch:
  328. if (searchconff) {
  329. newconff->namenode->oldhash= searchconff->hash;
  330. } else {
  331. debug(dbg_conff,"process_archive conffile `%s' no package, no hash",
  332. newconff->namenode->name);
  333. }
  334. newconff->namenode->flags |= fnnf_new_conff;
  335. }
  336. if (ferror(conff)) ohshite("read error in %.250s",cidir);
  337. pop_cleanup(ehflag_normaltidy); /* conff= fopen() */
  338. if (fclose(conff)) ohshite("error closing %.250s",cidir);
  339. } else {
  340. if (errno != ENOENT) ohshite("error trying to open %.250s",cidir);
  341. }
  342. /* All the old conffiles are marked with a flag, so that we don't delete
  343. * them if they seem to disappear completely.
  344. */
  345. oldconffsetflags(pkg->installed.conffiles);
  346. if (conflictor) oldconffsetflags(conflictor->installed.conffiles);
  347. oldversionstatus= pkg->status;
  348. assert(oldversionstatus <= stat_configfiles);
  349. debug(dbg_general,"process_archive oldversionstatus=%s conflictor=%s",
  350. statusstrings[oldversionstatus], conflictor ? conflictor->name : "<none>");
  351. if (oldversionstatus == stat_halfconfigured || oldversionstatus == stat_installed) {
  352. pkg->eflag |= eflagf_reinstreq;
  353. pkg->status= stat_halfconfigured;
  354. modstatdb_note(pkg);
  355. push_cleanup(cu_prermupgrade,~ehflag_normaltidy, 0,0, 1,(void*)pkg);
  356. maintainer_script_alternative(pkg, PRERMFILE, "pre-removal", cidir, cidirrest,
  357. "upgrade", "failed-upgrade");
  358. pkg->status= stat_unpacked;
  359. oldversionstatus= stat_unpacked;
  360. modstatdb_note(pkg);
  361. }
  362. if (conflictor &&
  363. (conflictor->status == stat_halfconfigured ||
  364. conflictor->status == stat_installed)) {
  365. for (deconpil= deconfigure; deconpil; deconpil= deconpil->next) {
  366. printf("De-configuring %s, so that we can remove %s ...\n",
  367. deconpil->pkg->name, conflictor->name);
  368. deconpil->pkg->status= stat_halfconfigured;
  369. modstatdb_note(deconpil->pkg);
  370. /* This means that we *either* go and run postinst abort-deconfigure,
  371. * *or* queue the package for later configure processing, depending
  372. * on which error cleanup route gets taken.
  373. */
  374. push_cleanup(cu_prermdeconfigure,~ehflag_normaltidy,
  375. ok_prermdeconfigure,ehflag_normaltidy,
  376. 3,(void*)deconpil->pkg,(void*)conflictor,(void*)pkg);
  377. maintainer_script_installed(deconpil->pkg, PRERMFILE, "pre-removal",
  378. "deconfigure", "in-favour", pkg->name,
  379. versiondescribe(&pkg->available.version,
  380. vdew_nonambig),
  381. "removing", conflictor->name,
  382. versiondescribe(&conflictor->installed.version,
  383. vdew_nonambig),
  384. (char*)0);
  385. }
  386. conflictor->status= stat_halfconfigured;
  387. modstatdb_note(conflictor);
  388. push_cleanup(cu_prerminfavour,~ehflag_normaltidy, 0,0,
  389. 2,(void*)conflictor,(void*)pkg);
  390. maintainer_script_installed(conflictor, PRERMFILE, "pre-removal",
  391. "remove", "in-favour", pkg->name,
  392. versiondescribe(&pkg->available.version,
  393. vdew_nonambig),
  394. (char*)0);
  395. conflictor->status= stat_halfinstalled;
  396. modstatdb_note(conflictor);
  397. }
  398. pkg->eflag |= eflagf_reinstreq;
  399. if (pkg->status == stat_notinstalled)
  400. pkg->installed.version= pkg->available.version;
  401. pkg->status= stat_halfinstalled;
  402. modstatdb_note(pkg);
  403. if (oldversionstatus == stat_notinstalled) {
  404. push_cleanup(cu_preinstverynew,~ehflag_normaltidy, 0,0,
  405. 3,(void*)pkg,(void*)cidir,(void*)cidirrest);
  406. maintainer_script_new(PREINSTFILE, "pre-installation", cidir, cidirrest,
  407. "install", (char*)0);
  408. } else if (oldversionstatus == stat_configfiles) {
  409. push_cleanup(cu_preinstnew,~ehflag_normaltidy, 0,0,
  410. 3,(void*)pkg,(void*)cidir,(void*)cidirrest);
  411. maintainer_script_new(PREINSTFILE, "pre-installation", cidir, cidirrest,
  412. "install", versiondescribe(&pkg->installed.version,
  413. vdew_nonambig),
  414. (char*)0);
  415. } else {
  416. push_cleanup(cu_preinstupgrade,~ehflag_normaltidy, 0,0,
  417. 4,(void*)pkg,(void*)cidir,(void*)cidirrest,(void*)&oldversionstatus);
  418. maintainer_script_new(PREINSTFILE, "pre-installation", cidir, cidirrest,
  419. "upgrade", versiondescribe(&pkg->installed.version,
  420. vdew_nonambig),
  421. (char*)0);
  422. printf("Unpacking replacement %.250s ...\n",pkg->name);
  423. }
  424. /*
  425. * Now we unpack the archive, backing things up as we go.
  426. * For each file, we check to see if it already exists.
  427. * There are several possibilities:
  428. * + We are trying to install a non-directory ...
  429. * - It doesn't exist. In this case we simply extract it.
  430. * - It is a plain file, device, symlink, &c. We do an `atomic
  431. * overwrite' using link() and rename(), but leave a backup copy.
  432. * Later, when we delete the backup, we remove it from any other
  433. * packages' lists.
  434. * - It is a directory. In this case it depends on whether we're
  435. * trying to install a symlink or something else.
  436. * = If we're not trying to install a symlink we move the directory
  437. * aside and extract the node. Later, when we recursively remove
  438. * the backed-up directory, we remove it from any other packages'
  439. * lists.
  440. * = If we are trying to install a symlink we do nothing - ie,
  441. * dpkg will never replace a directory tree with a symlink. This
  442. * is to avoid embarrassing effects such as replacing a directory
  443. * tree with a link to a link to the original directory tree.
  444. * + We are trying to install a directory ...
  445. * - It doesn't exist. We create it with the appropriate modes.
  446. * - It exists as a directory or a symlink to one. We do nothing.
  447. * - It is a plain file or a symlink (other than to a directory).
  448. * We move it aside and create the directory. Later, when we
  449. * delete the backup, we remove it from any other packages' lists.
  450. *
  451. * Install non-dir Install symlink Install dir
  452. * Exists not X X X
  453. * File/node/symlink LXR LXR BXR
  454. * Directory BXR - -
  455. *
  456. * X: extract file/node/link/directory
  457. * LX: atomic overwrite leaving backup
  458. * B: ordinary backup
  459. * R: later remove from other packages' lists
  460. * -: do nothing
  461. *
  462. * After we've done this we go through the remaining things in the
  463. * lists of packages we're trying to remove (including the old
  464. * version of the current package). This happens in reverse order,
  465. * so that we process files before the directories (or symlinks-to-
  466. * directories) containing them.
  467. * + If the thing is a conffile then we leave it alone for the purge
  468. * operation.
  469. * + Otherwise, there are several possibilities too:
  470. * - The listed thing does not exist. We ignore it.
  471. * - The listed thing is a directory or a symlink to a directory.
  472. * We delete it only if it isn't listed in any other package.
  473. * - The listed thing is not a directory or a symlink to one (ie,
  474. * it's a plain file, device, pipe, &c, or a symlink to one, or a
  475. * dangling symlink). We delete it.
  476. * The removed packages' list becomes empty (of course, the new
  477. * version of the package we're installing will have a new list,
  478. * which replaces the old version's list).
  479. *
  480. * If at any stage we remove a file from a package's list, and the
  481. * package isn't one we're already processing, and the package's
  482. * list becomes empty as a result, we `vanish' the package. This
  483. * means that we run its postrm with the `disappear' argument, and
  484. * put the package in the `not-installed' state. Its conffiles are
  485. * ignored and forgotten about.
  486. *
  487. * NOTE THAT THE OLD POSTRM IS RUN AFTER THE NEW PREINST, since the
  488. * files get replaced `as we go'.
  489. */
  490. m_pipe(p1);
  491. push_cleanup(cu_closepipe,ehflag_bombout, 0,0, 1,(void*)&p1[0]);
  492. c1= m_fork();
  493. if (!c1) {
  494. m_dup2(p1[1],1); close(p1[0]); close(p1[1]);
  495. execlp(BACKEND, BACKEND, "--fsys-tarfile", filename, (char*)0);
  496. ohshite("unable to exec " BACKEND " to get filesystem archive");
  497. }
  498. close(p1[1]);
  499. newfileslist= 0; tc.newfilesp= &newfileslist;
  500. push_cleanup(cu_fileslist,~0, 0,0, 1,(void*)&newfileslist);
  501. tc.pkg= pkg;
  502. tc.backendpipe= fdopen(p1[0],"r");
  503. if (!tc.backendpipe) ohshite("unable to fdopen " BACKEND " extract pipe");
  504. push_cleanup(cu_backendpipe,~ehflag_bombout, 0,0, 1,(void*)&tc.backendpipe);
  505. r= TarExtractor((void*)&tc, &tf);
  506. if (r) {
  507. if (errno) {
  508. ohshite("error reading " BACKEND " tar output");
  509. } else if (feof(tc.backendpipe)) {
  510. waitsubproc(c1,BACKEND " --fsys-tarfile (EOF)",1);
  511. ohshit("unexpected EOF in filesystem tarfile - corrupted package archive");
  512. } else {
  513. ohshit("corrupted filesystem tarfile - corrupted package archive");
  514. }
  515. }
  516. tmpf= tc.backendpipe;
  517. tc.backendpipe= 0;
  518. fclose(tmpf);
  519. waitsubproc(c1,BACKEND " --fsys-tarfile",1);
  520. if (oldversionstatus == stat_halfinstalled || oldversionstatus == stat_unpacked) {
  521. /* Packages that were in `installed' and `postinstfailed' have been reduced
  522. * to `unpacked' by now, by the running of the prerm script.
  523. */
  524. pkg->status= stat_halfinstalled;
  525. modstatdb_note(pkg);
  526. push_cleanup(cu_postrmupgrade,~ehflag_normaltidy, 0,0, 1,(void*)pkg);
  527. maintainer_script_alternative(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
  528. "upgrade", "failed-upgrade");
  529. }
  530. /* If anything goes wrong while tidying up it's a bit late to do
  531. * anything about it. However, we don't install the new status
  532. * info yet, so that a future dpkg installation will put everything
  533. * right (we hope).
  534. *
  535. * If something does go wrong later the `conflictor' package will be
  536. * left in the `removal_failed' state. Removing or installing it
  537. * will be impossible if it was required because of the conflict with
  538. * the package we're installing now and (presumably) the dependency
  539. * by other packages. This means that the files it contains in
  540. * common with this package will hang around until we successfully
  541. * get this package installed, after which point we can trust the
  542. * conflicting package's file list, which will have been updated to
  543. * remove any files in this package.
  544. */
  545. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  546. /* Now we delete all the files that were in the old version of
  547. * the package only, except (old or new) conffiles, which we leave
  548. * alone.
  549. */
  550. reversefilelist_init(&rlistit,pkg->clientdata->files);
  551. while ((namenode= reversefilelist_next(&rlistit))) {
  552. if ((namenode->flags & fnnf_old_conff) ||
  553. (namenode->flags & fnnf_new_conff) ||
  554. (namenode->flags & fnnf_new_inarchive))
  555. continue;
  556. if (isdirectoryinuse(namenode,pkg)) continue;
  557. fnamevb.used= fnameidlu;
  558. varbufaddstr(&fnamevb, namenodetouse(namenode,pkg)->name);
  559. varbufaddc(&fnamevb,0);
  560. if (!rmdir(fnamevb.buf)) continue;
  561. if (errno == ENOENT || errno == ELOOP) continue;
  562. if (errno == ENOTDIR) {
  563. if (!unlink(fnamevb.buf)) continue;
  564. if (errno == ENOTDIR) continue;
  565. }
  566. fprintf(stderr,
  567. DPKG ": warning - unable to delete old file `%.250s': %s\n",
  568. namenode->name, strerror(errno));
  569. }
  570. /* OK, now we can write the updated files-in-this package list,
  571. * since we've done away (hopefully) with all the old junk.
  572. */
  573. write_filelist_except(pkg,newfileslist,0);
  574. /* We also install the new maintainer scripts, and any other
  575. * cruft that may have come along with the package. First
  576. * we go through the existing scripts replacing or removing
  577. * them as appropriate; then we go through the new scripts
  578. * (any that are left) and install them.
  579. */
  580. debug(dbg_general, "process_archive updating info directory");
  581. varbufreset(&infofnvb);
  582. varbufaddstr(&infofnvb,admindir);
  583. varbufaddstr(&infofnvb,"/" INFODIR "/");
  584. infodirlen= infofnvb.used;
  585. varbufaddc(&infofnvb,0);
  586. dsd= opendir(infofnvb.buf);
  587. if (!dsd) ohshite("cannot read info directory");
  588. push_cleanup(cu_closedir,~0, 0,0, 1,(void*)dsd);
  589. while ((de= readdir(dsd)) != 0) {
  590. debug(dbg_veryverbose, "process_archive info file `%s'", de->d_name);
  591. if (de->d_name[0] == '.') continue; /* ignore dotfiles, including `.' and `..' */
  592. p= strrchr(de->d_name,'.'); if (!p) continue; /* ignore anything odd */
  593. if (strlen(pkg->name) != p-de->d_name ||
  594. strncmp(de->d_name,pkg->name,p-de->d_name)) continue;
  595. debug(dbg_stupidlyverbose, "process_archive info this pkg");
  596. /* Right do we have one ? */
  597. p++; /* skip past the full stop */
  598. if (!strcmp(p,LISTFILE)) continue; /* We do the list separately */
  599. if (strlen(p) > MAXCONTROLFILENAME)
  600. ohshit("old version of package has overly-long info file name starting `%.250s'",
  601. de->d_name);
  602. infofnvb.used= infodirlen;
  603. varbufaddstr(&infofnvb,de->d_name);
  604. varbufaddc(&infofnvb,0);
  605. strcpy(cidirrest,p);
  606. if (!rename(cidir,infofnvb.buf)) {
  607. debug(dbg_scripts, "process_archive info installed %s as %s",
  608. cidir, infofnvb.buf);
  609. } else if (errno == ENOENT) {
  610. /* Right, no new version. */
  611. if (unlink(infofnvb.buf))
  612. ohshite("unable to remove obsolete info file `%.250s'",infofnvb.buf);
  613. debug(dbg_scripts, "process_archive info unlinked %s",infofnvb.buf);
  614. } else {
  615. ohshite("unable to install (supposed) new info file `%.250s'",cidir);
  616. }
  617. }
  618. pop_cleanup(ehflag_normaltidy); /* closedir */
  619. *cidirrest= 0; /* the directory itself */
  620. dsd= opendir(cidir);
  621. if (!dsd) ohshite("unable to open temp control directory");
  622. push_cleanup(cu_closedir,~0, 0,0, 1,(void*)dsd);
  623. while ((de= readdir(dsd))) {
  624. if (strchr(de->d_name,'.')) {
  625. debug(dbg_scripts,"process_archive tmp.ci script/file `%s' contains dot",
  626. de->d_name);
  627. continue;
  628. }
  629. if (strlen(de->d_name) > MAXCONTROLFILENAME)
  630. ohshit("package contains overly-long control info file name (starting `%.50s')",
  631. de->d_name);
  632. strcpy(cidirrest,de->d_name);
  633. /* First we check it's not a directory. */
  634. if (!rmdir(cidir))
  635. ohshit("package control info contained directory `%.250s'",cidir);
  636. else if (errno != ENOTDIR)
  637. ohshite("package control info rmdir of `%.250s' didn't say not a dir",de->d_name);
  638. if (!strcmp(de->d_name,CONTROLFILE)) {
  639. debug(dbg_scripts,"process_archive tmp.ci script/file `%s' is control",cidir);
  640. continue; /* ignore the control file */
  641. }
  642. if (!strcmp(de->d_name,LISTFILE)) {
  643. fprintf(stderr, DPKG ": warning - package %s"
  644. " contained list as info file", pkg->name);
  645. continue;
  646. }
  647. /* Right, install it */
  648. newinfofilename= pkgadminfile(pkg,de->d_name);
  649. if (rename(cidir,newinfofilename))
  650. ohshite("unable to install new info file `%.250s' as `%.250s'",
  651. cidir,newinfofilename);
  652. debug(dbg_scripts,"process_archive tmp.ci script/file `%s' installed as `%s'",
  653. cidir, newinfofilename);
  654. }
  655. pop_cleanup(ehflag_normaltidy); /* closedir */
  656. /* Update the status database.
  657. * This involves copying each field across from the `available'
  658. * to the `installed' half of the pkg structure.
  659. * For some of the fields we have to do a complicated construction
  660. * operation; for others we can just copy the value.
  661. * We tackle the fields in the order they appear, so that
  662. * we don't miss any out :-).
  663. * At least we don't have to copy any strings that are referred
  664. * to, because these are never modified and never freed.
  665. */
  666. /* The dependencies are the most difficult. We have to build
  667. * a whole new forward dependency tree. At least the reverse
  668. * links (linking our deppossi's into the reverse chains)
  669. * can be done by copy_dependency_links.
  670. */
  671. newdeplist= 0; newdeplistlastp= &newdeplist;
  672. for (dep= pkg->available.depends; dep; dep= dep->next) {
  673. newdep= nfmalloc(sizeof(struct dependency));
  674. newdep->up= pkg;
  675. newdep->next= 0;
  676. newdep->list= 0; newpossilastp= &newdep->list;
  677. for (possi= dep->list; possi; possi= possi->next) {
  678. newpossi= nfmalloc(sizeof(struct deppossi));
  679. newpossi->up= newdep;
  680. newpossi->ed= possi->ed;
  681. newpossi->next= 0; newpossi->nextrev= newpossi->backrev= 0;
  682. newpossi->verrel= possi->verrel;
  683. if (possi->verrel != dvr_none) newpossi->version= possi->version;
  684. newpossi->cyclebreak= 0;
  685. *newpossilastp= newpossi;
  686. newpossilastp= &newpossi->next;
  687. }
  688. newdep->type= dep->type;
  689. *newdeplistlastp= newdep;
  690. newdeplistlastp= &newdep->next;
  691. }
  692. /* Right, now we've replicated the forward tree, we
  693. * get copy_dependency_links to remove all the old dependency
  694. * structures from the reverse links and add the new dependency
  695. * structures in instead. It also copies the new dependency
  696. * structure pointer for this package into the right field.
  697. */
  698. copy_dependency_links(pkg,&pkg->installed.depends,newdeplist,0);
  699. /* The `depended' pointer in the structure doesn't represent anything
  700. * that is actually specified by this package - it's there so we
  701. * can find out what other packages refer to this one. So,
  702. * we don't copy it. We go straight on to copy the text fields.
  703. */
  704. pkg->installed.essential= pkg->available.essential;
  705. pkg->installed.description= pkg->available.description;
  706. pkg->installed.maintainer= pkg->available.maintainer;
  707. pkg->installed.source= pkg->available.source;
  708. pkg->installed.architecture= 0; /* This is irrelevant in the status file. */
  709. pkg->installed.version= pkg->available.version;
  710. /* We have to generate our own conffiles structure. */
  711. pkg->installed.conffiles= 0; iconffileslastp= &pkg->installed.conffiles;
  712. for (cfile= newconffiles; cfile; cfile= cfile->next) {
  713. newiconff= nfmalloc(sizeof(struct conffile));
  714. newiconff->next= 0;
  715. newiconff->name= nfstrsave(cfile->namenode->name);
  716. newiconff->hash= nfstrsave(cfile->namenode->oldhash);
  717. *iconffileslastp= newiconff;
  718. iconffileslastp= &newiconff->next;
  719. }
  720. /* We can just copy the arbitrary fields list, because it is
  721. * never even rearragned. Phew !
  722. */
  723. pkg->installed.arbs= pkg->available.arbs;
  724. /* Check for disappearing packages:
  725. * We go through all the packages on the system looking for ones
  726. * whose files are entirely part of the one we've just unpacked
  727. * (and which actually *have* some files!).
  728. *
  729. * Any that we find are removed - we run the postrm with `disappear'
  730. * as an argument, and remove their info/... files and status info.
  731. * Conffiles are ignored (the new package had better do something
  732. * with them !).
  733. */
  734. it= iterpkgstart();
  735. while ((otherpkg= iterpkgnext(it)) != 0) {
  736. ensure_package_clientdata(otherpkg);
  737. if (otherpkg == pkg ||
  738. otherpkg == conflictor ||
  739. otherpkg->status == stat_notinstalled ||
  740. otherpkg->status == stat_configfiles ||
  741. !otherpkg->clientdata->files) continue;
  742. debug(dbg_veryverbose, "process_archive checking disappearance %s",otherpkg->name);
  743. assert(otherpkg->clientdata->istobe == itb_normal ||
  744. otherpkg->clientdata->istobe == itb_deconfigure);
  745. for (cfile= otherpkg->clientdata->files;
  746. cfile && !strcmp(cfile->namenode->name,"/.");
  747. cfile= cfile->next);
  748. if (!cfile) {
  749. debug(dbg_stupidlyverbose, "process_archive no non-root, no disappear");
  750. continue;
  751. }
  752. for (cfile= otherpkg->clientdata->files;
  753. cfile && !filesavespackage(cfile,otherpkg,pkg);
  754. cfile= cfile->next);
  755. if (cfile) continue;
  756. /* So dependency things will give right answers ... */
  757. otherpkg->clientdata->istobe= itb_remove;
  758. debug(dbg_veryverbose, "process_archive disappear checking dependencies");
  759. for (pdep= otherpkg->installed.depended;
  760. pdep;
  761. pdep= pdep->nextrev) {
  762. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends &&
  763. pdep->up->type != dep_recommends) continue;
  764. if (depisok(pdep->up, &depprobwhy, 0,0)) continue;
  765. varbufaddc(&depprobwhy,0);
  766. debug(dbg_veryverbose,"process_archive cannot disappear: %s",depprobwhy.buf);
  767. break;
  768. }
  769. if (!pdep) {
  770. /* If we haven't found a reason not to yet, let's look some more. */
  771. for (providecheck= otherpkg->installed.depends;
  772. providecheck;
  773. providecheck= providecheck->next) {
  774. if (providecheck->type != dep_provides) continue;
  775. for (pdep= providecheck->list->ed->installed.depended;
  776. pdep;
  777. pdep= pdep->nextrev) {
  778. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends &&
  779. pdep->up->type != dep_recommends)
  780. continue;
  781. if (depisok(pdep->up, &depprobwhy, 0,0)) continue;
  782. varbufaddc(&depprobwhy,0);
  783. debug(dbg_veryverbose,"process_archive cannot disappear (provides %s): %s",
  784. providecheck->list->ed->name, depprobwhy.buf);
  785. goto break_from_both_loops_at_once;
  786. }
  787. }
  788. break_from_both_loops_at_once:;
  789. }
  790. otherpkg->clientdata->istobe= itb_normal;
  791. if (pdep) continue;
  792. printf("(Noting disappearance of %s, which has been completely replaced.)\n",
  793. otherpkg->name);
  794. debug(dbg_general, "process_archive disappearing %s",otherpkg->name);
  795. /* No, we're disappearing it. This is the wrong time to go and
  796. * run maintainer scripts and things, as we can't back out. But
  797. * what can we do ? It has to be run this late.
  798. */
  799. maintainer_script_installed(otherpkg, POSTRMFILE,
  800. "post-removal script (for disappearance)",
  801. "disappear", pkg->name,
  802. versiondescribe(&pkg->available.version,
  803. vdew_nonambig),
  804. (char*)0);
  805. /* OK, now we delete all the stuff in the `info' directory .. */
  806. varbufreset(&fnvb);
  807. varbufaddstr(&fnvb,admindir);
  808. varbufaddstr(&fnvb,"/" INFODIR);
  809. infodirbaseused= fnvb.used;
  810. varbufaddc(&fnvb,0);
  811. dsd= opendir(fnvb.buf); if (!dsd) ohshite("cannot read info directory");
  812. push_cleanup(cu_closedir,~0, 0,0, 1,(void*)dsd);
  813. debug(dbg_general, "process_archive disappear cleaning info directory");
  814. while ((de= readdir(dsd)) != 0) {
  815. debug(dbg_veryverbose, "process_archive info file `%s'", de->d_name);
  816. if (de->d_name[0] == '.') continue;
  817. p= strrchr(de->d_name,'.'); if (!p) continue;
  818. if (strlen(otherpkg->name) != p-de->d_name ||
  819. strncmp(de->d_name,otherpkg->name,p-de->d_name)) continue;
  820. debug(dbg_stupidlyverbose, "process_archive info this pkg");
  821. fnvb.used= infodirbaseused;
  822. varbufaddstr(&fnvb,de->d_name);
  823. varbufaddc(&fnvb,0);
  824. if (unlink(fnvb.buf))
  825. ohshite("unable to delete disappearing control info file `%.250s'",fnvb.buf);
  826. debug(dbg_scripts, "process_archive info unlinked %s",fnvb.buf);
  827. }
  828. pop_cleanup(ehflag_normaltidy); /* closedir */
  829. otherpkg->status= stat_notinstalled;
  830. otherpkg->want= want_purge;
  831. otherpkg->eflag= eflagv_ok;
  832. otherpkg->installed.depends= 0;
  833. otherpkg->installed.essential= 0;
  834. otherpkg->installed.description= otherpkg->installed.maintainer= 0;
  835. otherpkg->installed.conffiles= 0;
  836. blankversion(&otherpkg->installed.version);
  837. otherpkg->installed.arbs= 0;
  838. otherpkg->clientdata->fileslistvalid= 0;
  839. modstatdb_note(otherpkg);
  840. } /* while (otherpkg= ... */
  841. iterpkgend(it);
  842. /* Delete files from any other packages' lists.
  843. * We have to do this before we claim this package is in any
  844. * sane kind of state, as otherwise we might delete by mistake
  845. * a file that we overwrote, when we remove the package which
  846. * had the version we overwrote. To prevent this we make
  847. * sure that we don't claim this package is OK until we
  848. * have claimed `ownership' of all its files.
  849. */
  850. for (cfile= newfileslist; cfile; cfile= cfile->next) {
  851. if (!(cfile->namenode->flags & fnnf_elide_other_lists)) continue;
  852. if (cfile->namenode->divert && cfile->namenode->divert->useinstead) {
  853. divpkg= cfile->namenode->divert->pkg;
  854. if (divpkg == pkg) {
  855. debug(dbg_eachfile,
  856. "process_archive not overwriting any `%s' (overriding, `%s')",
  857. cfile->namenode->name, cfile->namenode->divert->useinstead->name);
  858. continue;
  859. } else {
  860. debug(dbg_eachfile,
  861. "process_archive looking for overwriting `%s' (overridden by %s)",
  862. cfile->namenode->name, divpkg ? divpkg->name : "<local>");
  863. }
  864. } else {
  865. divpkg= 0;
  866. debug(dbg_eachfile, "process_archive looking for overwriting `%s'",
  867. cfile->namenode->name);
  868. }
  869. for (packageslump= cfile->namenode->packages;
  870. packageslump;
  871. packageslump= packageslump->more) {
  872. for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) {
  873. otherpkg= packageslump->pkgs[i];
  874. debug(dbg_eachfiledetail, "process_archive ... found in %s\n",otherpkg->name);
  875. /* If !fileslistvalid then it's one of the disappeared packages above
  876. * and we don't bother with it here, clearly.
  877. */
  878. if (otherpkg == pkg || !otherpkg->clientdata->fileslistvalid) continue;
  879. if (otherpkg == divpkg) {
  880. debug(dbg_eachfiledetail, "process_archive ... diverted, skipping\n");
  881. continue;
  882. }
  883. /* Found one. We delete remove the list entry for this file,
  884. * (and any others in the same package) and then mark the package
  885. * as requiring a reread.
  886. */
  887. write_filelist_except(otherpkg, otherpkg->clientdata->files, 1);
  888. ensure_package_clientdata(otherpkg);
  889. debug(dbg_veryverbose, "process_archive overwrote from %s",otherpkg->name);
  890. }
  891. }
  892. }
  893. /* Right, the package we've unpacked is now in a reasonable state.
  894. * The only thing that we have left to do with it is remove
  895. * backup files, and we can leave the user to fix that if and when
  896. * it happens (we leave the reinstall required flag, of course).
  897. */
  898. pkg->status= stat_unpacked;
  899. modstatdb_note(pkg);
  900. /* Now we delete all the backup files that we made when
  901. * extracting the archive - except for files listed as conffiles
  902. * in the new package.
  903. * This time we count it as an error if something goes wrong.
  904. *
  905. * Note that we don't ever delete things that were in the old
  906. * package as a conffile and don't appear at all in the new.
  907. */
  908. for (cfile= newfileslist; cfile; cfile= cfile->next) {
  909. if (cfile->namenode->flags & fnnf_new_conff) continue;
  910. fnametmpvb.used= fnameidlu;
  911. varbufaddstr(&fnametmpvb,namenodetouse(cfile->namenode,pkg)->name);
  912. varbufaddstr(&fnametmpvb,DPKGTEMPEXT);
  913. varbufaddc(&fnametmpvb,0);
  914. ensure_pathname_nonexisting(fnametmpvb.buf);
  915. }
  916. /* OK, we're now fully done with the main package.
  917. * This is quite a nice state, so we don't unwind past here.
  918. */
  919. pkg->eflag= eflagv_ok;
  920. modstatdb_note(pkg);
  921. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  922. /* Only the removal of the conflictor left to do.
  923. * The files list for the conflictor is still a little inconsistent in-core,
  924. * as we have not yet updated the filename->packages mappings; however,
  925. * the package->filenames mapping is
  926. */
  927. if (conflictor) {
  928. /* We need to have the most up-to-date info about which files are what ... */
  929. ensure_allinstfiles_available();
  930. removal_bulk(conflictor);
  931. }
  932. if (cipaction->arg == act_install) add_to_queue(pkg);
  933. }