archives.c 43 KB

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