archives.c 44 KB

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