archives.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. /*
  2. * dpkg - main program for package management
  3. * archives.c - actions that process archive files, mainly unpack
  4. *
  5. * Copyright (C) 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright (C) 2000 Wichert Akkerman <wakkerma@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2,
  11. * or (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with dpkg; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <config.h>
  23. #include <errno.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <utime.h>
  29. #include <assert.h>
  30. #include <time.h>
  31. #include <ctype.h>
  32. #include <fcntl.h>
  33. #include <sys/stat.h>
  34. #include <sys/types.h>
  35. #include <obstack.h>
  36. #define obstack_chunk_alloc m_malloc
  37. #define obstack_chunk_free free
  38. #include <dpkg.h>
  39. #include <dpkg-db.h>
  40. #include <tarfn.h>
  41. #include <myopt.h>
  42. #ifdef WITH_SELINUX
  43. #include <selinux/selinux.h>
  44. static int selinux_enabled=-1;
  45. static security_context_t scontext = NULL;
  46. #endif
  47. #include "filesdb.h"
  48. #include "main.h"
  49. #include "archives.h"
  50. #define MAXCONFLICTORS 20
  51. struct pkginfo *conflictor[MAXCONFLICTORS];
  52. int cflict_index = 0;
  53. /* snprintf(3) doesn't work if format contains %.<nnn>s and an argument has
  54. * invalid char for locale, then it returns -1.
  55. * ohshite() is ok, but fd_fd_copy(), which is used in tarobject() in this
  56. * file, is not ok, because
  57. * - fd_fd_copy() == buffer_copy_setup() [include/dpkg.h]
  58. * - buffer_copy_setup() uses varbufvprintf(&v, desc, al); [lib/mlib.c]
  59. * - varbufvprintf() fails and memory exausted, because it call
  60. * fmt = "backend dpkg-deb during `%.255s'
  61. * arg may contain some invalid char, for example,
  62. * /usr/share/doc/console-tools/examples/unicode/\342\231\252\342\231\254
  63. * in console-tools.
  64. * In this case, if user uses some locale which doesn't support \342\231...,
  65. * vsnprintf() always returns -1 and varbufextend() get called again
  66. * and again until memory is exausted and it aborts.
  67. *
  68. * So, we need to escape invalid char, probably as in
  69. * tar-1.13.19/lib/quotearg.c: quotearg_buffer_restyled()
  70. * but here I escape all 8bit chars, in order to be simple.
  71. * - ukai@debian.or.jp
  72. */
  73. static char *
  74. quote_filename(char *buf, int size, char *s)
  75. {
  76. char *r = buf;
  77. while (size > 0) {
  78. switch (*s) {
  79. case '\0':
  80. *buf = '\0';
  81. return r;
  82. case '\\':
  83. *buf++ = '\\';
  84. *buf++ = '\\';
  85. size -= 2;
  86. break;
  87. default:
  88. if (((*s) & 0x80) == 0) {
  89. *buf++ = *s++;
  90. --size;
  91. } else {
  92. if (size > 4) {
  93. sprintf(buf, "\\%03o", *(unsigned char *)s);
  94. size -= 4;
  95. buf += 4;
  96. s++;
  97. } else {
  98. /* buffer full */
  99. *buf = '\0'; /* XXX */
  100. return r;
  101. }
  102. }
  103. }
  104. }
  105. *buf = '\0'; /* XXX */
  106. return r;
  107. }
  108. /* special routine to handle partial reads from the tarfile */
  109. static int safe_read(int fd, void *buf, int len)
  110. {
  111. int r, have= 0;
  112. char *p = (char *)buf;
  113. while (have < len) {
  114. if ((r= read(fd,p,len-have))==-1) {
  115. if (errno==EINTR || errno==EAGAIN) continue;
  116. return r;
  117. }
  118. if (r==0)
  119. break;
  120. have+= r;
  121. p+= r;
  122. }
  123. return have;
  124. }
  125. static struct obstack tar_obs;
  126. static int tarobs_init= 0;
  127. /* ensure the obstack is properly initialized */
  128. static void ensureobstackinit(void) {
  129. if (!tarobs_init) {
  130. obstack_init(&tar_obs);
  131. tarobs_init= 1;
  132. }
  133. }
  134. /* destroy the obstack */
  135. static void destroyobstack(void) {
  136. if (tarobs_init) {
  137. obstack_free(&tar_obs, NULL);
  138. tarobs_init= 0;
  139. }
  140. }
  141. int filesavespackage(struct fileinlist *file, struct pkginfo *pkgtobesaved,
  142. struct pkginfo *pkgbeinginstalled) {
  143. struct pkginfo *divpkg, *thirdpkg;
  144. struct filepackages *packageslump;
  145. int i;
  146. debug(dbg_eachfiledetail,"filesavespackage file `%s' package %s",
  147. file->namenode->name,pkgtobesaved->name);
  148. /* A package can only be saved by a file or directory which is part
  149. * only of itself - it must be neither part of the new package being
  150. * installed nor part of any 3rd package (this is important so that
  151. * shared directories don't stop packages from disappearing).
  152. */
  153. /* If the file is a contended one and it's overridden by either
  154. * the package we're considering disappearing or the package
  155. * we're installing then they're not actually the same file, so
  156. * we can't disappear the package - it is saved by this file.
  157. */
  158. if (file->namenode->divert && file->namenode->divert->useinstead) {
  159. divpkg= file->namenode->divert->pkg;
  160. if (divpkg == pkgtobesaved || divpkg == pkgbeinginstalled) {
  161. debug(dbg_eachfiledetail,"filesavespackage ... diverted -- save!");
  162. return 1;
  163. }
  164. }
  165. /* Is the file in the package being installed ? If so then it can't save.
  166. */
  167. if (file->namenode->flags & fnnf_new_inarchive) {
  168. debug(dbg_eachfiledetail,"filesavespackage ... in new archive -- no save");
  169. return 0;
  170. }
  171. /* Look for a 3rd package which can take over the file (in case
  172. * it's a directory which is shared by many packages.
  173. */
  174. for (packageslump= file->namenode->packages;
  175. packageslump;
  176. packageslump= packageslump->more) {
  177. for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) {
  178. thirdpkg= packageslump->pkgs[i];
  179. debug(dbg_eachfiledetail, "filesavespackage ... also in %s",
  180. thirdpkg->name);
  181. /* Is this not the package being installed or the one being
  182. * checked for disappearance ?
  183. */
  184. if (thirdpkg == pkgbeinginstalled || thirdpkg == pkgtobesaved) continue;
  185. /* If !fileslistvalid then we've already disappeared this one, so
  186. * we shouldn't try to make it take over this shared directory.
  187. */
  188. debug(dbg_eachfiledetail,"filesavespackage ... is 3rd package");
  189. if (!thirdpkg->clientdata->fileslistvalid) {
  190. debug(dbg_eachfiledetail,
  191. _("process_archive ... already disappeared !"));
  192. continue;
  193. }
  194. /* We've found a package that can take this file. */
  195. debug(dbg_eachfiledetail, "filesavespackage ... taken -- no save");
  196. return 0;
  197. }
  198. }
  199. debug(dbg_eachfiledetail, "filesavespackage ... not taken -- save !");
  200. return 1;
  201. }
  202. void cu_pathname(int argc, void **argv) {
  203. ensure_pathname_nonexisting((char*)(argv[0]));
  204. }
  205. int tarfileread(void *ud, char *buf, int len) {
  206. struct tarcontext *tc= (struct tarcontext*)ud;
  207. int r;
  208. if ((r= safe_read(tc->backendpipe,buf,len)) == -1)
  209. ohshite(_("error reading from dpkg-deb pipe"));
  210. return r;
  211. }
  212. void
  213. tarfile_skip_one_forward(struct TarInfo *ti,
  214. struct fileinlist **oldnifd,
  215. struct fileinlist *nifd)
  216. {
  217. struct tarcontext *tc = (struct tarcontext *)ti->UserData;
  218. size_t r;
  219. char databuf[TARBLKSZ];
  220. obstack_free(&tar_obs, nifd);
  221. tc->newfilesp = oldnifd;
  222. *oldnifd = NULL;
  223. /* We need to advance the tar file to the next object, so read the
  224. * file data and set it to oblivion.
  225. */
  226. if ((ti->Type == NormalFile0) || (ti->Type == NormalFile1)) {
  227. char fnamebuf[256];
  228. fd_null_copy(tc->backendpipe, ti->Size,
  229. _("skipped unpacking file '%.255s' (replaced or excluded?)"),
  230. quote_filename(fnamebuf, 256, ti->Name));
  231. r = ti->Size % TARBLKSZ;
  232. if (r > 0)
  233. r = safe_read(tc->backendpipe, databuf, TARBLKSZ - r);
  234. }
  235. }
  236. int fnameidlu;
  237. struct varbuf fnamevb;
  238. struct varbuf fnametmpvb;
  239. struct varbuf fnamenewvb;
  240. struct packageinlist *deconfigure = NULL;
  241. static time_t currenttime;
  242. static int does_replace(struct pkginfo *newpigp,
  243. struct pkginfoperfile *newpifp,
  244. struct pkginfo *oldpigp) {
  245. struct dependency *dep;
  246. debug(dbg_depcon,"does_replace new=%s old=%s (%s)",newpigp->name,
  247. oldpigp->name,versiondescribe(&oldpigp->installed.version,
  248. vdew_always));
  249. for (dep= newpifp->depends; dep; dep= dep->next) {
  250. if (dep->type != dep_replaces || dep->list->ed != oldpigp) continue;
  251. debug(dbg_depcondetail,"does_replace ... found old, version %s",
  252. versiondescribe(&dep->list->version,vdew_always));
  253. if (!versionsatisfied(&oldpigp->installed,dep->list)) continue;
  254. debug(dbg_depcon,"does_replace ... yes");
  255. return 1;
  256. }
  257. debug(dbg_depcon,"does_replace ... no");
  258. return 0;
  259. }
  260. static void newtarobject_utime(const char *path, struct TarInfo *ti) {
  261. struct utimbuf utb;
  262. utb.actime= currenttime;
  263. utb.modtime= ti->ModTime;
  264. if (utime(path,&utb))
  265. ohshite(_("error setting timestamps of `%.255s'"),ti->Name);
  266. }
  267. static void newtarobject_allmodes(const char *path, struct TarInfo *ti, struct filestatoverride* statoverride) {
  268. if (chown(path,
  269. statoverride ? statoverride->uid : ti->UserID,
  270. statoverride ? statoverride->gid : ti->GroupID))
  271. ohshite(_("error setting ownership of `%.255s'"),ti->Name);
  272. if (chmod(path,(statoverride ? statoverride->mode : ti->Mode) & ~S_IFMT))
  273. ohshite(_("error setting permissions of `%.255s'"),ti->Name);
  274. newtarobject_utime(path,ti);
  275. }
  276. void setupfnamevbs(const char *filename) {
  277. fnamevb.used= fnameidlu;
  278. varbufaddstr(&fnamevb,filename);
  279. varbufaddc(&fnamevb,0);
  280. fnametmpvb.used= fnameidlu;
  281. varbufaddstr(&fnametmpvb,filename);
  282. varbufaddstr(&fnametmpvb,DPKGTEMPEXT);
  283. varbufaddc(&fnametmpvb,0);
  284. fnamenewvb.used= fnameidlu;
  285. varbufaddstr(&fnamenewvb,filename);
  286. varbufaddstr(&fnamenewvb,DPKGNEWEXT);
  287. varbufaddc(&fnamenewvb,0);
  288. debug(dbg_eachfiledetail, "setupvnamevbs main=`%s' tmp=`%s' new=`%s'",
  289. fnamevb.buf, fnametmpvb.buf, fnamenewvb.buf);
  290. }
  291. int unlinkorrmdir(const char *filename) {
  292. /* Returns 0 on success or -1 on failure, just like unlink & rmdir */
  293. int r, e;
  294. if (!rmdir(filename)) {
  295. debug(dbg_eachfiledetail,"unlinkorrmdir `%s' rmdir OK",filename);
  296. return 0;
  297. }
  298. if (errno != ENOTDIR) {
  299. e= errno;
  300. debug(dbg_eachfiledetail,"unlinkorrmdir `%s' rmdir %s",filename,strerror(e));
  301. errno= e; return -1;
  302. }
  303. r= unlink(filename); e= errno;
  304. debug(dbg_eachfiledetail,"unlinkorrmdir `%s' unlink %s",
  305. filename, r ? strerror(e) : "OK");
  306. errno= e; return r;
  307. }
  308. struct fileinlist *addfiletolist(struct tarcontext *tc,
  309. struct filenamenode *namenode) {
  310. struct fileinlist *nifd;
  311. nifd= obstack_alloc(&tar_obs, sizeof(struct fileinlist));
  312. nifd->namenode= namenode;
  313. nifd->next = NULL;
  314. *tc->newfilesp = nifd;
  315. tc->newfilesp = &nifd->next;
  316. return nifd;
  317. }
  318. static int linktosameexistingdir(const struct TarInfo *ti,
  319. const char *fname,
  320. struct varbuf *symlinkfn) {
  321. struct stat oldstab, newstab;
  322. int statr;
  323. const char *lastslash;
  324. statr= stat(fname, &oldstab);
  325. if (statr) {
  326. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  327. ohshite(_("failed to stat (dereference) existing symlink `%.250s'"),
  328. fname);
  329. return 0;
  330. }
  331. if (!S_ISDIR(oldstab.st_mode)) return 0;
  332. /* But is it to the same dir ? */
  333. varbufreset(symlinkfn);
  334. if (ti->LinkName[0] == '/') {
  335. varbufaddstr(symlinkfn, instdir);
  336. } else {
  337. lastslash= strrchr(fname, '/');
  338. assert(lastslash);
  339. varbufaddbuf(symlinkfn, fname, (lastslash - fname) + 1);
  340. }
  341. varbufaddstr(symlinkfn, ti->LinkName);
  342. varbufaddc(symlinkfn, 0);
  343. statr= stat(symlinkfn->buf, &newstab);
  344. if (statr) {
  345. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  346. ohshite(_("failed to stat (dereference) proposed new symlink target"
  347. " `%.250s' for symlink `%.250s'"), symlinkfn->buf, fname);
  348. return 0;
  349. }
  350. if (!S_ISDIR(newstab.st_mode)) return 0;
  351. if (newstab.st_dev != oldstab.st_dev ||
  352. newstab.st_ino != oldstab.st_ino) return 0;
  353. return 1;
  354. }
  355. int tarobject(struct TarInfo *ti) {
  356. static struct varbuf conffderefn, hardlinkfn, symlinkfn;
  357. static int fd;
  358. const char *usename;
  359. struct conffile *conff;
  360. struct tarcontext *tc= (struct tarcontext*)ti->UserData;
  361. int statr, i, existingdirectory, keepexisting;
  362. ssize_t r;
  363. struct stat stab, stabtmp;
  364. char databuf[TARBLKSZ];
  365. struct fileinlist *nifd, **oldnifd;
  366. struct pkginfo *divpkg, *otherpkg;
  367. struct filepackages *packageslump;
  368. mode_t am;
  369. ensureobstackinit();
  370. /* Append to list of files.
  371. * The trailing / put on the end of names in tarfiles has already
  372. * been stripped by TarExtractor (lib/tarfn.c).
  373. */
  374. oldnifd= tc->newfilesp;
  375. nifd= addfiletolist(tc, findnamenode(ti->Name, 0));
  376. nifd->namenode->flags |= fnnf_new_inarchive;
  377. debug(dbg_eachfile,
  378. "tarobject ti->Name=`%s' Mode=%lo owner=%u.%u Type=%d(%c)"
  379. " ti->LinkName=`%s' namenode=`%s' flags=%o instead=`%s'",
  380. ti->Name, (long)ti->Mode, (unsigned)ti->UserID, (unsigned)ti->GroupID, ti->Type,
  381. ti->Type == '\0' ? '_' :
  382. ti->Type >= '0' && ti->Type <= '6' ? "-hlcbdp"[ti->Type - '0'] : '?',
  383. ti->LinkName,
  384. nifd->namenode->name, nifd->namenode->flags,
  385. nifd->namenode->divert && nifd->namenode->divert->useinstead
  386. ? nifd->namenode->divert->useinstead->name : "<none>");
  387. if (nifd->namenode->divert && nifd->namenode->divert->camefrom) {
  388. divpkg= nifd->namenode->divert->pkg;
  389. if (divpkg) {
  390. forcibleerr(fc_overwritediverted,
  391. _("trying to overwrite `%.250s', which is the "
  392. "diverted version of `%.250s' (package: %.100s)"),
  393. nifd->namenode->name, nifd->namenode->divert->camefrom->name,
  394. divpkg->name);
  395. } else {
  396. forcibleerr(fc_overwritediverted,
  397. _("trying to overwrite `%.250s', which is the "
  398. "diverted version of `%.250s'"),
  399. nifd->namenode->name, nifd->namenode->divert->camefrom->name);
  400. }
  401. }
  402. usename= namenodetouse(nifd->namenode,tc->pkg)->name + 1; /* Skip the leading `/' */
  403. if (nifd->namenode->flags & fnnf_new_conff) {
  404. /* If it's a conffile we have to extract it next to the installed
  405. * version (ie, we do the usual link-following).
  406. */
  407. if (conffderef(tc->pkg, &conffderefn, usename))
  408. usename= conffderefn.buf;
  409. debug(dbg_conff,"tarobject fnnf_new_conff deref=`%s'",usename);
  410. }
  411. setupfnamevbs(usename);
  412. statr= lstat(fnamevb.buf,&stab);
  413. if (statr) {
  414. /* The lstat failed. */
  415. if (errno != ENOENT && errno != ENOTDIR)
  416. ohshite(_("unable to stat `%.255s' (which I was about to install)"),ti->Name);
  417. /* OK, so it doesn't exist.
  418. * However, it's possible that we were in the middle of some other
  419. * backup/restore operation and were rudely interrupted.
  420. * So, we see if we have .dpkg-tmp, and if so we restore it.
  421. */
  422. if (rename(fnametmpvb.buf,fnamevb.buf)) {
  423. if (errno != ENOENT && errno != ENOTDIR)
  424. ohshite(_("unable to clean up mess surrounding `%.255s' before "
  425. "installing another version"),ti->Name);
  426. debug(dbg_eachfiledetail,"tarobject nonexistent");
  427. } else {
  428. debug(dbg_eachfiledetail,"tarobject restored tmp to main");
  429. statr= lstat(fnamevb.buf,&stab);
  430. if (statr) ohshite(_("unable to stat restored `%.255s' before installing"
  431. " another version"), ti->Name);
  432. }
  433. } else {
  434. debug(dbg_eachfiledetail,"tarobject already exists");
  435. }
  436. /* Check to see if it's a directory or link to one and we don't need to
  437. * do anything. This has to be done now so that we don't die due to
  438. * a file overwriting conflict.
  439. */
  440. existingdirectory= 0;
  441. switch (ti->Type) {
  442. case SymbolicLink:
  443. /* If it's already an existing directory, do nothing. */
  444. if (!statr && S_ISDIR(stab.st_mode)) {
  445. debug(dbg_eachfiledetail,"tarobject SymbolicLink exists as directory");
  446. existingdirectory= 1;
  447. } else if (!statr && S_ISLNK(stab.st_mode)) {
  448. if (linktosameexistingdir(ti, fnamevb.buf, &symlinkfn))
  449. existingdirectory= 1;
  450. }
  451. break;
  452. case Directory:
  453. /* If it's already an existing directory, do nothing. */
  454. if (!stat(fnamevb.buf,&stabtmp) && S_ISDIR(stabtmp.st_mode)) {
  455. debug(dbg_eachfiledetail,"tarobject Directory exists");
  456. existingdirectory= 1;
  457. }
  458. break;
  459. case NormalFile0: case NormalFile1:
  460. case CharacterDevice: case BlockDevice: case FIFO:
  461. case HardLink:
  462. break;
  463. default:
  464. ohshit(_("archive contained object `%.255s' of unknown type 0x%x"),ti->Name,ti->Type);
  465. }
  466. keepexisting= 0;
  467. if (!existingdirectory) {
  468. for (packageslump= nifd->namenode->packages;
  469. packageslump;
  470. packageslump= packageslump->more) {
  471. for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) {
  472. otherpkg= packageslump->pkgs[i];
  473. if (otherpkg == tc->pkg) continue;
  474. debug(dbg_eachfile, "tarobject ... found in %s",otherpkg->name);
  475. if (nifd->namenode->divert && nifd->namenode->divert->useinstead) {
  476. /* Right, so we may be diverting this file. This makes the conflict
  477. * OK iff one of us is the diverting package (we don't need to
  478. * check for both being the diverting package, obviously).
  479. */
  480. divpkg= nifd->namenode->divert->pkg;
  481. debug(dbg_eachfile, "tarobject ... diverted, divpkg=%s",
  482. divpkg ? divpkg->name : "<none>");
  483. if (otherpkg == divpkg || tc->pkg == divpkg) continue;
  484. }
  485. /* Nope ? Hmm, file conflict, perhaps. Check Replaces. */
  486. switch (otherpkg->clientdata->replacingfilesandsaid) {
  487. case 2:
  488. keepexisting= 1;
  489. case 1:
  490. continue;
  491. }
  492. /* Is the package with the conflicting file in the `config files
  493. * only' state ? If so it must be a config file and we can
  494. * silenty take it over.
  495. */
  496. if (otherpkg->status == stat_configfiles) continue;
  497. /* Perhaps we're removing a conflicting package ? */
  498. if (otherpkg->clientdata->istobe == itb_remove) continue;
  499. /* Is the file an obsolete conffile in the other package
  500. * and a conffile in the new package ? */
  501. if ((nifd->namenode->flags & fnnf_new_conff) &&
  502. !statr && S_ISREG(stab.st_mode)) {
  503. for (conff= otherpkg->installed.conffiles;
  504. conff;
  505. conff= conff->next) {
  506. if (!conff->obsolete)
  507. continue;
  508. if (stat(conff->name, &stabtmp))
  509. if (errno == ENOENT || errno == ENOTDIR || errno == ELOOP)
  510. continue;
  511. if (stabtmp.st_dev == stab.st_dev &&
  512. stabtmp.st_ino == stab.st_ino)
  513. break;
  514. }
  515. if (conff)
  516. debug(dbg_eachfiledetail,"tarobject other's obsolete conffile");
  517. /* processarc.c will have copied its hash already. */
  518. continue;
  519. }
  520. if (does_replace(tc->pkg,&tc->pkg->available,otherpkg)) {
  521. printf(_("Replacing files in old package %s ...\n"),otherpkg->name);
  522. otherpkg->clientdata->replacingfilesandsaid= 1;
  523. } else if (does_replace(otherpkg,&otherpkg->installed,tc->pkg)) {
  524. printf(_("Replaced by files in installed package %s ...\n"),
  525. otherpkg->name);
  526. otherpkg->clientdata->replacingfilesandsaid= 2;
  527. keepexisting = 1;
  528. } else {
  529. if (!statr && S_ISDIR(stab.st_mode)) {
  530. forcibleerr(fc_overwritedir, _("trying to overwrite directory `%.250s' "
  531. "in package %.250s with nondirectory"),
  532. nifd->namenode->name,otherpkg->name);
  533. } else {
  534. /* WTA: At this point we are replacing something without a Replaces.
  535. * if the new object is a directory and the previous object does not
  536. * exist assume it's also a directory and don't complain
  537. */
  538. if (! (statr && ti->Type==Directory))
  539. forcibleerr(fc_overwrite,
  540. _("trying to overwrite `%.250s', which is also in package %.250s"),
  541. nifd->namenode->name,otherpkg->name);
  542. }
  543. }
  544. }
  545. }
  546. }
  547. /* Now, at this stage we want to make sure neither of .dpkg-new and .dpkg-tmp
  548. * are hanging around.
  549. */
  550. ensure_pathname_nonexisting(fnamenewvb.buf);
  551. ensure_pathname_nonexisting(fnametmpvb.buf);
  552. if (existingdirectory) return 0;
  553. if (keepexisting) {
  554. tarfile_skip_one_forward(ti, oldnifd, nifd);
  555. return 0;
  556. }
  557. /* Now we start to do things that we need to be able to undo
  558. * if something goes wrong. Watch out for the CLEANUP comments to
  559. * keep an eye on what's installed on the disk at each point.
  560. */
  561. push_cleanup(cu_installnew, ~ehflag_normaltidy, NULL, 0, 1, (void *)nifd);
  562. /* CLEANUP: Now we either have the old file on the disk, or not, in
  563. * its original filename.
  564. */
  565. #ifdef WITH_SELINUX
  566. /* Set selinux_enabled if it is not already set (singleton) */
  567. if (selinux_enabled < 0)
  568. selinux_enabled = (is_selinux_enabled() > 0);
  569. /* Since selinux is enabled, try and set the context */
  570. if (selinux_enabled > 0) {
  571. /*
  572. * well, we could use
  573. * void set_matchpathcon_printf(void (*f)(const char *fmt, ...));
  574. * to redirect the errors from the following bit, but that
  575. * seems too much effort.
  576. */
  577. /*
  578. * Do nothing if we can't figure out what the context is,
  579. * or if it has no context; in which case the default
  580. * context shall be applied.
  581. */
  582. if( ! ((matchpathcon(fnamevb.buf,
  583. (nifd->namenode->statoverride ?
  584. nifd->namenode->statoverride->mode : ti->Mode)
  585. & ~S_IFMT, &scontext) != 0) ||
  586. (strcmp(scontext, "<<none>>") == 0)))
  587. {
  588. if(setfscreatecon(scontext) < 0)
  589. perror("Error setting security context for file object:");
  590. }
  591. }
  592. #endif /* WITH_SELINUX */
  593. /* Extract whatever it is as .dpkg-new ... */
  594. switch (ti->Type) {
  595. case NormalFile0: case NormalFile1:
  596. /* We create the file with mode 0 to make sure nobody can do anything with
  597. * it until we apply the proper mode, which might be a statoverride.
  598. */
  599. fd= open(fnamenewvb.buf, (O_CREAT|O_EXCL|O_WRONLY), 0);
  600. if (fd < 0)
  601. ohshite(_("unable to create `%.255s' (while processing `%.255s')"), fnamenewvb.buf, ti->Name);
  602. push_cleanup(cu_closefd, ehflag_bombout, NULL, 0, 1, &fd);
  603. debug(dbg_eachfiledetail,"tarobject NormalFile[01] open size=%lu",
  604. (unsigned long)ti->Size);
  605. { char fnamebuf[256];
  606. fd_fd_copy(tc->backendpipe, fd, ti->Size, _("backend dpkg-deb during `%.255s'"),quote_filename(fnamebuf,256,ti->Name));
  607. }
  608. r= ti->Size % TARBLKSZ;
  609. if (r > 0) r= safe_read(tc->backendpipe,databuf,TARBLKSZ - r);
  610. if (nifd->namenode->statoverride)
  611. debug(dbg_eachfile, "tarobject ... stat override, uid=%d, gid=%d, mode=%04o",
  612. nifd->namenode->statoverride->uid,
  613. nifd->namenode->statoverride->gid,
  614. nifd->namenode->statoverride->mode);
  615. if (fchown(fd,
  616. nifd->namenode->statoverride ? nifd->namenode->statoverride->uid : ti->UserID,
  617. nifd->namenode->statoverride ? nifd->namenode->statoverride->gid : ti->GroupID))
  618. ohshite(_("error setting ownership of `%.255s'"),ti->Name);
  619. am=(nifd->namenode->statoverride ? nifd->namenode->statoverride->mode : ti->Mode) & ~S_IFMT;
  620. if (fchmod(fd,am))
  621. ohshite(_("error setting permissions of `%.255s'"),ti->Name);
  622. pop_cleanup(ehflag_normaltidy); /* fd= open(fnamenewvb.buf) */
  623. if (close(fd))
  624. ohshite(_("error closing/writing `%.255s'"),ti->Name);
  625. newtarobject_utime(fnamenewvb.buf,ti);
  626. break;
  627. case FIFO:
  628. if (mkfifo(fnamenewvb.buf,0))
  629. ohshite(_("error creating pipe `%.255s'"),ti->Name);
  630. debug(dbg_eachfiledetail,"tarobject FIFO");
  631. newtarobject_allmodes(fnamenewvb.buf,ti, nifd->namenode->statoverride);
  632. break;
  633. case CharacterDevice:
  634. if (mknod(fnamenewvb.buf,S_IFCHR, ti->Device))
  635. ohshite(_("error creating device `%.255s'"),ti->Name);
  636. debug(dbg_eachfiledetail,"tarobject CharacterDevice");
  637. newtarobject_allmodes(fnamenewvb.buf,ti, nifd->namenode->statoverride);
  638. break;
  639. case BlockDevice:
  640. if (mknod(fnamenewvb.buf,S_IFBLK, ti->Device))
  641. ohshite(_("error creating device `%.255s'"),ti->Name);
  642. debug(dbg_eachfiledetail,"tarobject BlockDevice");
  643. newtarobject_allmodes(fnamenewvb.buf,ti, nifd->namenode->statoverride);
  644. break;
  645. case HardLink:
  646. varbufreset(&hardlinkfn);
  647. varbufaddstr(&hardlinkfn,instdir); varbufaddc(&hardlinkfn,'/');
  648. varbufaddstr(&hardlinkfn,ti->LinkName); varbufaddc(&hardlinkfn,0);
  649. if (link(hardlinkfn.buf,fnamenewvb.buf))
  650. ohshite(_("error creating hard link `%.255s'"),ti->Name);
  651. debug(dbg_eachfiledetail,"tarobject HardLink");
  652. newtarobject_allmodes(fnamenewvb.buf,ti, nifd->namenode->statoverride);
  653. break;
  654. case SymbolicLink:
  655. /* We've already cheched for an existing directory. */
  656. if (symlink(ti->LinkName,fnamenewvb.buf))
  657. ohshite(_("error creating symbolic link `%.255s'"),ti->Name);
  658. debug(dbg_eachfiledetail,"tarobject SymbolicLink creating");
  659. #ifdef HAVE_LCHOWN
  660. if (lchown(fnamenewvb.buf,
  661. nifd->namenode->statoverride ? nifd->namenode->statoverride->uid : ti->UserID,
  662. nifd->namenode->statoverride ? nifd->namenode->statoverride->gid : ti->GroupID))
  663. ohshite(_("error setting ownership of symlink `%.255s'"),ti->Name);
  664. #else
  665. if (chown(fnamenewvb.buf,
  666. nifd->namenode->statoverride ? nifd->namenode->statoverride->uid : ti->UserID,
  667. nifd->namenode->statoverride ? nifd->namenode->statoverride->gid : ti->GroupID))
  668. ohshite(_("error setting ownership of symlink `%.255s'"),ti->Name);
  669. #endif
  670. break;
  671. case Directory:
  672. /* We've already checked for an existing directory. */
  673. if (mkdir(fnamenewvb.buf,0))
  674. ohshite(_("error creating directory `%.255s'"),ti->Name);
  675. debug(dbg_eachfiledetail,"tarobject Directory creating");
  676. newtarobject_allmodes(fnamenewvb.buf,ti,nifd->namenode->statoverride);
  677. break;
  678. default:
  679. internerr("bad tar type, but already checked");
  680. }
  681. /* CLEANUP: Now we have extracted the new object in .dpkg-new (or,
  682. * if the file already exists as a directory and we were trying to extract
  683. * a directory or symlink, we returned earlier, so we don't need
  684. * to worry about that here).
  685. *
  686. * The old file is still in the original filename,
  687. */
  688. /* First, check to see if it's a conffile. If so we don't install
  689. * it now - we leave it in .dpkg-new for --configure to take care of
  690. */
  691. if (nifd->namenode->flags & fnnf_new_conff) {
  692. debug(dbg_conffdetail,"tarobject conffile extracted");
  693. nifd->namenode->flags |= fnnf_elide_other_lists;
  694. return 0;
  695. }
  696. /* Now we move the old file out of the way, the backup file will
  697. * be deleted later.
  698. */
  699. if (statr) { /* Don't try to back it up if it didn't exist. */
  700. debug(dbg_eachfiledetail,"tarobject new - no backup");
  701. } else {
  702. if (ti->Type == Directory || S_ISDIR(stab.st_mode)) {
  703. /* One of the two is a directory - can't do atomic install. */
  704. debug(dbg_eachfiledetail,"tarobject directory, nonatomic");
  705. nifd->namenode->flags |= fnnf_no_atomic_overwrite;
  706. if (rename(fnamevb.buf,fnametmpvb.buf))
  707. ohshite(_("unable to move aside `%.255s' to install new version"),ti->Name);
  708. } else if (S_ISLNK(stab.st_mode)) {
  709. /* We can't make a symlink with two hardlinks, so we'll have to copy it.
  710. * (Pretend that making a copy of a symlink is the same as linking to it.)
  711. */
  712. varbufreset(&symlinkfn);
  713. do {
  714. varbufextend(&symlinkfn);
  715. r= readlink(fnamevb.buf,symlinkfn.buf,symlinkfn.size);
  716. if (r<0) ohshite(_("unable to read link `%.255s'"),ti->Name);
  717. } while ((size_t)r == symlinkfn.size);
  718. symlinkfn.used= r; varbufaddc(&symlinkfn,0);
  719. if (symlink(symlinkfn.buf,fnametmpvb.buf))
  720. ohshite(_("unable to make backup symlink for `%.255s'"),ti->Name);
  721. #ifdef HAVE_LCHOWN
  722. if (lchown(fnametmpvb.buf,stab.st_uid,stab.st_gid))
  723. ohshite(_("unable to chown backup symlink for `%.255s'"),ti->Name);
  724. #else
  725. if (chown(fnametmpvb.buf,stab.st_uid,stab.st_gid))
  726. ohshite(_("unable to chown backup symlink for `%.255s'"),ti->Name);
  727. #endif
  728. } else {
  729. debug(dbg_eachfiledetail,"tarobject nondirectory, `link' backup");
  730. if (link(fnamevb.buf,fnametmpvb.buf))
  731. ohshite(_("unable to make backup link of `%.255s' before installing new version"),
  732. ti->Name);
  733. }
  734. }
  735. /* CLEANUP: now the old file is in dpkg-tmp, and the new file is still
  736. * in dpkg-new.
  737. */
  738. #ifdef WITH_SELINUX
  739. /*
  740. * if selinux is enabled, try and set the default security context
  741. * for the renamed file
  742. */
  743. if (selinux_enabled > 0)
  744. if(scontext) {
  745. if(setfscreatecon(scontext) < 0)
  746. perror("Error setting security context for next file object:");
  747. freecon(scontext);
  748. scontext = NULL;
  749. }
  750. #endif /* WITH_SELINUX */
  751. if (rename(fnamenewvb.buf,fnamevb.buf))
  752. ohshite(_("unable to install new version of `%.255s'"),ti->Name);
  753. /* CLEANUP: now the new file is in the destination file, and the
  754. * old file is in dpkg-tmp to be cleaned up later. We now need
  755. * to take a different attitude to cleanup, because we need to
  756. * remove the new file.
  757. */
  758. nifd->namenode->flags |= fnnf_placed_on_disk;
  759. #ifdef WITH_SELINUX
  760. /*
  761. * if selinux is enabled, restore the default security context
  762. */
  763. if (selinux_enabled > 0)
  764. if(setfscreatecon(NULL) < 0)
  765. perror("Error restoring default security context:");
  766. #endif /* WITH_SELINUX */
  767. nifd->namenode->flags |= fnnf_elide_other_lists;
  768. debug(dbg_eachfiledetail,"tarobject done and installed");
  769. return 0;
  770. }
  771. static int try_deconfigure_can(int (*force_p)(struct deppossi*),
  772. struct pkginfo *pkg,
  773. struct deppossi *pdep,
  774. const char *action,
  775. struct pkginfo *removal,
  776. const char *why) {
  777. /* Also checks whether the pdep is forced, first, according to force_p.
  778. * force_p may be 0 in which case nothing is considered forced.
  779. *
  780. * Action is a string describing the action which causes the
  781. * deconfiguration:
  782. * removal of <package> (due to Conflicts+Depends removal!=0)
  783. * installation of <package> (due to Breaks removal==0)
  784. *
  785. * Return values: 2: forced (no deconfiguration needed, why is printed)
  786. * 1: deconfiguration queued ok (no message printed)
  787. * 0: not possible (why is printed)
  788. */
  789. struct packageinlist *newdeconf;
  790. if (force_p && force_p(pdep)) {
  791. fprintf(stderr, _("dpkg: warning - "
  792. "ignoring dependency problem with %s:\n%s"),
  793. action, why);
  794. return 2;
  795. } else if (f_autodeconf) {
  796. if (pkg->installed.essential) {
  797. if (fc_removeessential) {
  798. fprintf(stderr, _("dpkg: warning - considering deconfiguration of essential\n"
  799. " package %s, to enable %s.\n"),
  800. pkg->name, action);
  801. } else {
  802. fprintf(stderr, _("dpkg: no, %s is essential, will not deconfigure\n"
  803. " it in order to enable %s.\n"),
  804. pkg->name, action);
  805. return 0;
  806. }
  807. }
  808. pkg->clientdata->istobe= itb_deconfigure;
  809. newdeconf = m_malloc(sizeof(struct packageinlist));
  810. newdeconf->next= deconfigure;
  811. newdeconf->pkg= pkg;
  812. newdeconf->xinfo= removal;
  813. deconfigure= newdeconf;
  814. return 1;
  815. } else {
  816. fprintf(stderr, _("dpkg: no, cannot proceed with %s (--auto-deconfigure will help):\n%s"),
  817. action, why);
  818. return 0;
  819. }
  820. }
  821. static int try_remove_can(struct deppossi *pdep,
  822. struct pkginfo *fixbyrm,
  823. const char *why) {
  824. char action[512];
  825. sprintf(action, _("removal of %.250s"), fixbyrm->name);
  826. return try_deconfigure_can(force_depends, pdep->up->up, pdep,
  827. action, fixbyrm, why);
  828. }
  829. void check_breaks(struct dependency *dep, struct pkginfo *pkg,
  830. const char *pfilename) {
  831. struct pkginfo *fixbydeconf;
  832. struct varbuf why = VARBUF_INIT;
  833. int ok;
  834. fixbydeconf = NULL;
  835. if (depisok(dep, &why, &fixbydeconf, 0)) {
  836. varbuffree(&why);
  837. return;
  838. }
  839. varbufaddc(&why, 0);
  840. if (fixbydeconf && f_autodeconf) {
  841. char action[512];
  842. ensure_package_clientdata(fixbydeconf);
  843. assert(fixbydeconf->clientdata->istobe == itb_normal);
  844. sprintf(action, _("installation of %.250s"), pkg->name);
  845. fprintf(stderr, _("dpkg: considering deconfiguration of %s,"
  846. " which would be broken by %s ...\n"),
  847. fixbydeconf->name, action);
  848. ok= try_deconfigure_can(force_breaks, fixbydeconf, dep->list,
  849. action, NULL, why.buf);
  850. if (ok == 1) {
  851. fprintf(stderr, _("dpkg: yes, will deconfigure %s (broken by %s).\n"),
  852. fixbydeconf->name, pkg->name);
  853. }
  854. } else {
  855. fprintf(stderr, _("dpkg: regarding %s containing %s:\n%s"),
  856. pfilename, pkg->name, why.buf);
  857. ok= 0;
  858. }
  859. varbuffree(&why);
  860. if (ok > 0) return;
  861. if (force_breaks(dep->list)) {
  862. fprintf(stderr, _("dpkg: warning - ignoring breakage,"
  863. " may proceed anyway !\n"));
  864. return;
  865. }
  866. if (fixbydeconf && !f_autodeconf) {
  867. ohshit(_("installing %.250s would break %.250s, and\n"
  868. " deconfiguration is not permitted (--auto-deconfigure might help)"),
  869. pkg->name, fixbydeconf->name);
  870. } else {
  871. ohshit(_("installing %.250s would break existing software"),
  872. pkg->name);
  873. }
  874. }
  875. void check_conflict(struct dependency *dep, struct pkginfo *pkg,
  876. const char *pfilename) {
  877. struct pkginfo *fixbyrm;
  878. struct deppossi *pdep, flagdeppossi;
  879. struct varbuf conflictwhy = VARBUF_INIT, removalwhy = VARBUF_INIT;
  880. struct dependency *providecheck;
  881. fixbyrm = NULL;
  882. if (depisok(dep, &conflictwhy, &fixbyrm, 0)) {
  883. varbuffree(&conflictwhy);
  884. varbuffree(&removalwhy);
  885. return;
  886. }
  887. if (fixbyrm) {
  888. ensure_package_clientdata(fixbyrm);
  889. if (fixbyrm->clientdata->istobe == itb_installnew) {
  890. fixbyrm= dep->up;
  891. ensure_package_clientdata(fixbyrm);
  892. }
  893. if (((pkg->available.essential && fixbyrm->installed.essential) ||
  894. (((fixbyrm->want != want_install && fixbyrm->want != want_hold) ||
  895. does_replace(pkg,&pkg->available,fixbyrm)) &&
  896. (!fixbyrm->installed.essential || fc_removeessential)))) {
  897. assert(fixbyrm->clientdata->istobe == itb_normal || fixbyrm->clientdata->istobe == itb_deconfigure);
  898. fixbyrm->clientdata->istobe= itb_remove;
  899. fprintf(stderr, _("dpkg: considering removing %s in favour of %s ...\n"),
  900. fixbyrm->name, pkg->name);
  901. if (!(fixbyrm->status == stat_installed ||
  902. fixbyrm->status == stat_triggerspending ||
  903. fixbyrm->status == stat_triggersawaited)) {
  904. fprintf(stderr,
  905. _("%s is not properly installed - ignoring any dependencies on it.\n"),
  906. fixbyrm->name);
  907. pdep = NULL;
  908. } else {
  909. for (pdep= fixbyrm->installed.depended;
  910. pdep;
  911. pdep= pdep->nextrev) {
  912. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  913. continue;
  914. if (depisok(pdep->up, &removalwhy, NULL, 0))
  915. continue;
  916. varbufaddc(&removalwhy,0);
  917. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  918. break;
  919. }
  920. if (!pdep) {
  921. /* If we haven't found a reason not to yet, let's look some more. */
  922. for (providecheck= fixbyrm->installed.depends;
  923. providecheck;
  924. providecheck= providecheck->next) {
  925. if (providecheck->type != dep_provides) continue;
  926. for (pdep= providecheck->list->ed->installed.depended;
  927. pdep;
  928. pdep= pdep->nextrev) {
  929. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  930. continue;
  931. if (depisok(pdep->up, &removalwhy, NULL, 0))
  932. continue;
  933. varbufaddc(&removalwhy,0);
  934. fprintf(stderr, _("dpkg"
  935. ": may have trouble removing %s, as it provides %s ...\n"),
  936. fixbyrm->name, providecheck->list->ed->name);
  937. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  938. goto break_from_both_loops_at_once;
  939. }
  940. }
  941. break_from_both_loops_at_once:;
  942. }
  943. }
  944. if (!pdep && skip_due_to_hold(fixbyrm)) {
  945. pdep= &flagdeppossi;
  946. }
  947. if (!pdep && (fixbyrm->eflag & eflagf_reinstreq)) {
  948. if (fc_removereinstreq) {
  949. fprintf(stderr, _("dpkg: package %s requires reinstallation, but will"
  950. " remove anyway as you request.\n"), fixbyrm->name);
  951. } else {
  952. fprintf(stderr, _("dpkg: package %s requires reinstallation, "
  953. "will not remove.\n"), fixbyrm->name);
  954. pdep= &flagdeppossi;
  955. }
  956. }
  957. if (!pdep) {
  958. if (cflict_index >= MAXCONFLICTORS)
  959. ohshit(_("package %s has too many Conflicts/Replaces pairs"),
  960. pkg->name);
  961. /* This conflict is OK - we'll remove the conflictor. */
  962. conflictor[cflict_index++]= fixbyrm;
  963. varbuffree(&conflictwhy); varbuffree(&removalwhy);
  964. fprintf(stderr, _("dpkg: yes, will remove %s in favour of %s.\n"),
  965. fixbyrm->name, pkg->name);
  966. return;
  967. }
  968. fixbyrm->clientdata->istobe= itb_normal; /* put it back */
  969. }
  970. }
  971. varbufaddc(&conflictwhy,0);
  972. fprintf(stderr, _("dpkg: regarding %s containing %s:\n%s"),
  973. pfilename, pkg->name, conflictwhy.buf);
  974. if (!force_conflicts(dep->list))
  975. ohshit(_("conflicting packages - not installing %.250s"),pkg->name);
  976. fprintf(stderr, _("dpkg: warning - ignoring conflict, may proceed anyway !\n"));
  977. varbuffree(&conflictwhy);
  978. return;
  979. }
  980. void cu_cidir(int argc, void **argv) {
  981. char *cidir= (char*)argv[0];
  982. char *cidirrest= (char*)argv[1];
  983. cidirrest[-1]= 0;
  984. ensure_pathname_nonexisting(cidir);
  985. }
  986. void cu_fileslist(int argc, void **argv) {
  987. destroyobstack();
  988. }
  989. void archivefiles(const char *const *argv) {
  990. const char *volatile thisarg;
  991. const char *const *volatile argp;
  992. jmp_buf ejbuf;
  993. int pi[2], fc, nfiles, c, i, r;
  994. FILE *pf;
  995. static struct varbuf findoutput;
  996. const char **arglist;
  997. char *p;
  998. trigproc_install_hooks();
  999. modstatdb_init(admindir,
  1000. f_noact ? msdbrw_readonly
  1001. : cipaction->arg == act_avail ? msdbrw_write
  1002. : fc_nonroot ? msdbrw_write
  1003. : msdbrw_needsuperuser);
  1004. checkpath();
  1005. log_message("startup archives %s", cipaction->olong);
  1006. if (f_recursive) {
  1007. if (!*argv)
  1008. badusage(_("--%s --recursive needs at least one path argument"),cipaction->olong);
  1009. m_pipe(pi);
  1010. if (!(fc= m_fork())) {
  1011. const char *const *ap;
  1012. int i;
  1013. m_dup2(pi[1],1); close(pi[0]); close(pi[1]);
  1014. for (i=0, ap=argv; *ap; ap++, i++);
  1015. arglist = m_malloc(sizeof(char *) * (i + 15));
  1016. arglist[0] = FIND;
  1017. for (i=1, ap=argv; *ap; ap++, i++) {
  1018. if (strchr(FIND_EXPRSTARTCHARS,(*ap)[0])) {
  1019. char *a;
  1020. a= m_malloc(strlen(*ap)+10);
  1021. strcpy(a,"./");
  1022. strcat(a,*ap);
  1023. arglist[i] = a;
  1024. } else {
  1025. arglist[i] = (const char *)*ap;
  1026. }
  1027. }
  1028. /* When editing these, make sure that arglist is malloced big enough,
  1029. * above.
  1030. */
  1031. arglist[i++] = "-follow";
  1032. arglist[i++] = "-name";
  1033. arglist[i++] = ARCHIVE_FILENAME_PATTERN;
  1034. arglist[i++] = "-type";
  1035. arglist[i++] = "f";
  1036. arglist[i++] = "-print0";
  1037. arglist[i++] = NULL;
  1038. execvp(FIND, (char *const *)arglist);
  1039. ohshite(_("failed to exec find for --recursive"));
  1040. }
  1041. close(pi[1]);
  1042. nfiles= 0;
  1043. pf= fdopen(pi[0],"r"); if (!pf) ohshite(_("failed to fdopen find's pipe"));
  1044. varbufreset(&findoutput);
  1045. while ((c= fgetc(pf)) != EOF) {
  1046. varbufaddc(&findoutput,c);
  1047. if (!c) nfiles++;
  1048. }
  1049. if (ferror(pf)) ohshite(_("error reading find's pipe"));
  1050. if (fclose(pf)) ohshite(_("error closing find's pipe"));
  1051. r= waitsubproc(fc,"find",PROCNOERR);
  1052. if(!(r==0 || r==1))
  1053. ohshit(_("find for --recursive returned unhandled error %i"),r);
  1054. if (!nfiles)
  1055. ohshit(_("searched, but found no packages (files matching *.deb)"));
  1056. varbufaddc(&findoutput,0);
  1057. varbufaddc(&findoutput,0);
  1058. arglist= m_malloc(sizeof(char*)*(nfiles+1));
  1059. p= findoutput.buf; i=0;
  1060. while (*p) {
  1061. arglist[i++]= p;
  1062. while ((c= *p++) != 0);
  1063. }
  1064. arglist[i] = NULL;
  1065. argp= arglist;
  1066. } else {
  1067. if (!*argv) badusage(_("--%s needs at least one package archive file argument"),
  1068. cipaction->olong);
  1069. argp= argv;
  1070. }
  1071. currenttime = time(NULL);
  1072. /* Initialize fname variables contents. */
  1073. varbufreset(&fnamevb);
  1074. varbufreset(&fnametmpvb);
  1075. varbufreset(&fnamenewvb);
  1076. varbufaddstr(&fnamevb,instdir); varbufaddc(&fnamevb,'/');
  1077. varbufaddstr(&fnametmpvb,instdir); varbufaddc(&fnametmpvb,'/');
  1078. varbufaddstr(&fnamenewvb,instdir); varbufaddc(&fnamenewvb,'/');
  1079. fnameidlu= fnamevb.used;
  1080. ensure_diversions();
  1081. ensure_statoverrides();
  1082. while ((thisarg = *argp++) != NULL) {
  1083. if (setjmp(ejbuf)) {
  1084. error_unwind(ehflag_bombout);
  1085. if (onerr_abort > 0) break;
  1086. continue;
  1087. }
  1088. push_error_handler(&ejbuf,print_error_perpackage,thisarg);
  1089. process_archive(thisarg);
  1090. onerr_abort++;
  1091. if (ferror(stdout)) werr("stdout");
  1092. if (ferror(stderr)) werr("stderr");
  1093. onerr_abort--;
  1094. set_error_display(NULL, NULL);
  1095. error_unwind(ehflag_normaltidy);
  1096. }
  1097. switch (cipaction->arg) {
  1098. case act_install:
  1099. case act_configure:
  1100. case act_triggers:
  1101. case act_remove:
  1102. case act_purge:
  1103. process_queue();
  1104. case act_unpack:
  1105. case act_avail:
  1106. break;
  1107. default:
  1108. internerr("unknown action");
  1109. }
  1110. trigproc_run_deferred();
  1111. modstatdb_shutdown();
  1112. }
  1113. int wanttoinstall(struct pkginfo *pkg, const struct versionrevision *ver, int saywhy) {
  1114. /* Decide whether we want to install a new version of the package.
  1115. * ver is the version we might want to install. If saywhy is 1 then
  1116. * if we skip the package we say what we are doing (and, if we are
  1117. * selecting a previously deselected package, say so and actually do
  1118. * the select). want_install returns 0 if the package should be
  1119. * skipped and 1 if it should be installed.
  1120. *
  1121. * ver may be 0, in which case saywhy must be 0 and want_install may
  1122. * also return -1 to mean it doesn't know because it would depend on
  1123. * the version number.
  1124. */
  1125. int r;
  1126. if (pkg->want != want_install && pkg->want != want_hold) {
  1127. if (f_alsoselect) {
  1128. if (saywhy) {
  1129. printf(_("Selecting previously deselected package %s.\n"),pkg->name);
  1130. pkg->want= want_install;
  1131. }
  1132. return 1;
  1133. } else {
  1134. if (saywhy) printf(_("Skipping deselected package %s.\n"),pkg->name);
  1135. return 0;
  1136. }
  1137. }
  1138. if (!(pkg->status == stat_installed ||
  1139. pkg->status == stat_triggersawaited ||
  1140. pkg->status == stat_triggerspending))
  1141. return 1;
  1142. if (!ver) return -1;
  1143. r= versioncompare(ver,&pkg->installed.version);
  1144. if (r > 0) {
  1145. return 1;
  1146. } else if (r == 0) {
  1147. /* Same version fully installed. */
  1148. if (f_skipsame) {
  1149. if (saywhy) fprintf(stderr, _("Version %.250s of %.250s already installed, "
  1150. "skipping.\n"),
  1151. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1152. pkg->name);
  1153. return 0;
  1154. } else {
  1155. return 1;
  1156. }
  1157. } else {
  1158. if (fc_downgrade) {
  1159. if (saywhy) fprintf(stderr, _("%s - warning: downgrading %.250s "
  1160. "from %.250s to %.250s.\n"), DPKG, pkg->name,
  1161. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1162. versiondescribe(&pkg->available.version, vdew_nonambig));
  1163. return 1;
  1164. } else {
  1165. if (saywhy) fprintf(stderr, _("Will not downgrade %.250s from version %.250s "
  1166. "to %.250s, skipping.\n"), pkg->name,
  1167. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1168. versiondescribe(&pkg->available.version, vdew_nonambig));
  1169. return 0;
  1170. }
  1171. }
  1172. }
  1173. struct fileinlist *newconff_append(struct fileinlist ***newconffileslastp_io,
  1174. struct filenamenode *namenode) {
  1175. struct fileinlist *newconff;
  1176. newconff= m_malloc(sizeof(struct fileinlist));
  1177. newconff->next = NULL;
  1178. newconff->namenode= namenode;
  1179. **newconffileslastp_io= newconff;
  1180. *newconffileslastp_io= &newconff->next;
  1181. return newconff;
  1182. }
  1183. /* vi: ts=8 sw=2
  1184. */