archives.c 45 KB

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