archives.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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. static int fd;
  334. const char *usename;
  335. struct conffile *conff;
  336. struct tarcontext *tc= (struct tarcontext*)ti->UserData;
  337. int statr, i, existingdirectory, keepexisting;
  338. ssize_t r;
  339. struct stat stab, stabtmp;
  340. char databuf[TARBLKSZ];
  341. struct fileinlist *nifd, **oldnifd;
  342. struct pkginfo *divpkg, *otherpkg;
  343. struct filepackages *packageslump;
  344. mode_t am;
  345. ensureobstackinit();
  346. /* Append to list of files.
  347. * The trailing / put on the end of names in tarfiles has already
  348. * been stripped by TarExtractor (lib/tarfn.c).
  349. */
  350. oldnifd= tc->newfilesp;
  351. nifd= addfiletolist(tc, findnamenode(ti->Name, 0));
  352. nifd->namenode->flags |= fnnf_new_inarchive;
  353. debug(dbg_eachfile,
  354. "tarobject ti->Name=`%s' Mode=%lo owner=%u.%u Type=%d(%c)"
  355. " ti->LinkName=`%s' namenode=`%s' flags=%o instead=`%s'",
  356. ti->Name, (long)ti->Mode, (unsigned)ti->UserID, (unsigned)ti->GroupID, ti->Type,
  357. ti->Type == '\0' ? '_' :
  358. ti->Type >= '0' && ti->Type <= '6' ? "-hlcbdp"[ti->Type - '0'] : '?',
  359. ti->LinkName,
  360. nifd->namenode->name, nifd->namenode->flags,
  361. nifd->namenode->divert && nifd->namenode->divert->useinstead
  362. ? nifd->namenode->divert->useinstead->name : "<none>");
  363. if (nifd->namenode->divert && nifd->namenode->divert->camefrom) {
  364. divpkg= nifd->namenode->divert->pkg;
  365. if (divpkg) {
  366. forcibleerr(fc_overwritediverted,
  367. _("trying to overwrite `%.250s', which is the "
  368. "diverted version of `%.250s' (package: %.100s)"),
  369. nifd->namenode->name, nifd->namenode->divert->camefrom->name,
  370. divpkg->name);
  371. } else {
  372. forcibleerr(fc_overwritediverted,
  373. _("trying to overwrite `%.250s', which is the "
  374. "diverted version of `%.250s'"),
  375. nifd->namenode->name, nifd->namenode->divert->camefrom->name);
  376. }
  377. }
  378. usename= namenodetouse(nifd->namenode,tc->pkg)->name + 1; /* Skip the leading `/' */
  379. if (nifd->namenode->flags & fnnf_new_conff) {
  380. /* If it's a conffile we have to extract it next to the installed
  381. * version (ie, we do the usual link-following).
  382. */
  383. if (conffderef(tc->pkg, &conffderefn, usename))
  384. usename= conffderefn.buf;
  385. debug(dbg_conff,"tarobject fnnf_new_conff deref=`%s'",usename);
  386. }
  387. setupfnamevbs(usename);
  388. statr= lstat(fnamevb.buf,&stab);
  389. if (statr) {
  390. /* The lstat failed. */
  391. if (errno != ENOENT && errno != ENOTDIR)
  392. ohshite(_("unable to stat `%.255s' (which I was about to install)"),ti->Name);
  393. /* OK, so it doesn't exist.
  394. * However, it's possible that we were in the middle of some other
  395. * backup/restore operation and were rudely interrupted.
  396. * So, we see if we have .dpkg-tmp, and if so we restore it.
  397. */
  398. if (rename(fnametmpvb.buf,fnamevb.buf)) {
  399. if (errno != ENOENT && errno != ENOTDIR)
  400. ohshite(_("unable to clean up mess surrounding `%.255s' before "
  401. "installing another version"),ti->Name);
  402. debug(dbg_eachfiledetail,"tarobject nonexistent");
  403. } else {
  404. debug(dbg_eachfiledetail,"tarobject restored tmp to main");
  405. statr= lstat(fnamevb.buf,&stab);
  406. if (statr) ohshite(_("unable to stat restored `%.255s' before installing"
  407. " another version"), ti->Name);
  408. }
  409. } else {
  410. debug(dbg_eachfiledetail,"tarobject already exists");
  411. }
  412. /* Check to see if it's a directory or link to one and we don't need to
  413. * do anything. This has to be done now so that we don't die due to
  414. * a file overwriting conflict.
  415. */
  416. existingdirectory= 0;
  417. switch (ti->Type) {
  418. case SymbolicLink:
  419. /* If it's already an existing directory, do nothing. */
  420. if (!statr && S_ISDIR(stab.st_mode)) {
  421. debug(dbg_eachfiledetail,"tarobject SymbolicLink exists as directory");
  422. existingdirectory= 1;
  423. } else if (!statr && S_ISLNK(stab.st_mode)) {
  424. if (linktosameexistingdir(ti, fnamevb.buf, &symlinkfn))
  425. existingdirectory= 1;
  426. }
  427. break;
  428. case Directory:
  429. /* If it's already an existing directory, do nothing. */
  430. if (!stat(fnamevb.buf,&stabtmp) && S_ISDIR(stabtmp.st_mode)) {
  431. debug(dbg_eachfiledetail,"tarobject Directory exists");
  432. existingdirectory= 1;
  433. }
  434. break;
  435. case NormalFile0: case NormalFile1:
  436. case CharacterDevice: case BlockDevice: case FIFO:
  437. case HardLink:
  438. break;
  439. default:
  440. ohshit(_("archive contained object `%.255s' of unknown type 0x%x"),ti->Name,ti->Type);
  441. }
  442. keepexisting= 0;
  443. if (!existingdirectory) {
  444. for (packageslump= nifd->namenode->packages;
  445. packageslump;
  446. packageslump= packageslump->more) {
  447. for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) {
  448. otherpkg= packageslump->pkgs[i];
  449. if (otherpkg == tc->pkg) continue;
  450. debug(dbg_eachfile, "tarobject ... found in %s",otherpkg->name);
  451. if (nifd->namenode->divert && nifd->namenode->divert->useinstead) {
  452. /* Right, so we may be diverting this file. This makes the conflict
  453. * OK iff one of us is the diverting package (we don't need to
  454. * check for both being the diverting package, obviously).
  455. */
  456. divpkg= nifd->namenode->divert->pkg;
  457. debug(dbg_eachfile, "tarobject ... diverted, divpkg=%s",
  458. divpkg ? divpkg->name : "<none>");
  459. if (otherpkg == divpkg || tc->pkg == divpkg) continue;
  460. }
  461. /* Nope ? Hmm, file conflict, perhaps. Check Replaces. */
  462. switch (otherpkg->clientdata->replacingfilesandsaid) {
  463. case 2:
  464. keepexisting= 1;
  465. case 1:
  466. continue;
  467. }
  468. /* Is the package with the conflicting file in the `config files
  469. * only' state ? If so it must be a config file and we can
  470. * silenty take it over.
  471. */
  472. if (otherpkg->status == stat_configfiles) continue;
  473. /* Perhaps we're removing a conflicting package ? */
  474. if (otherpkg->clientdata->istobe == itb_remove) continue;
  475. /* Is the file an obsolete conffile in the other package
  476. * and a conffile in the new package ? */
  477. if ((nifd->namenode->flags & fnnf_new_conff) &&
  478. !statr && S_ISREG(stab.st_mode)) {
  479. for (conff= otherpkg->installed.conffiles;
  480. conff;
  481. conff= conff->next) {
  482. if (!conff->obsolete)
  483. continue;
  484. if (stat(conff->name, &stabtmp))
  485. if (errno == ENOENT || errno == ENOTDIR || errno == ELOOP)
  486. continue;
  487. if (stabtmp.st_dev == stab.st_dev &&
  488. stabtmp.st_ino == stab.st_ino)
  489. break;
  490. }
  491. if (conff)
  492. debug(dbg_eachfiledetail,"tarobject other's obsolete conffile");
  493. /* processarc.c will have copied its hash already. */
  494. continue;
  495. }
  496. if (does_replace(tc->pkg,&tc->pkg->available,otherpkg)) {
  497. printf(_("Replacing files in old package %s ...\n"),otherpkg->name);
  498. otherpkg->clientdata->replacingfilesandsaid= 1;
  499. } else if (does_replace(otherpkg,&otherpkg->installed,tc->pkg)) {
  500. printf(_("Replaced by files in installed package %s ...\n"),
  501. otherpkg->name);
  502. otherpkg->clientdata->replacingfilesandsaid= 2;
  503. keepexisting = 1;
  504. } else {
  505. if (!statr && S_ISDIR(stab.st_mode)) {
  506. forcibleerr(fc_overwritedir, _("trying to overwrite directory `%.250s' "
  507. "in package %.250s with nondirectory"),
  508. nifd->namenode->name,otherpkg->name);
  509. } else {
  510. /* WTA: At this point we are replacing something without a Replaces.
  511. * if the new object is a directory and the previous object does not
  512. * exist assume it's also a directory and don't complain
  513. */
  514. if (! (statr && ti->Type==Directory))
  515. forcibleerr(fc_overwrite,
  516. _("trying to overwrite `%.250s', which is also in package %.250s"),
  517. nifd->namenode->name,otherpkg->name);
  518. }
  519. }
  520. }
  521. }
  522. }
  523. /* Now, at this stage we want to make sure neither of .dpkg-new and .dpkg-tmp
  524. * are hanging around.
  525. */
  526. ensure_pathname_nonexisting(fnamenewvb.buf);
  527. ensure_pathname_nonexisting(fnametmpvb.buf);
  528. if (existingdirectory) return 0;
  529. if (keepexisting) {
  530. obstack_free(&tar_obs, nifd);
  531. tc->newfilesp= oldnifd;
  532. *oldnifd = NULL;
  533. /* We need to advance the tar file to the next object, so read the
  534. * file data and set it to oblivion.
  535. */
  536. if ((ti->Type == NormalFile0) || (ti->Type == NormalFile1)) {
  537. char fnamebuf[256];
  538. fd_null_copy(tc->backendpipe, ti->Size, _("gobble replaced file `%.255s'"),quote_filename(fnamebuf,256,ti->Name));
  539. r= ti->Size % TARBLKSZ;
  540. if (r > 0) r= safe_read(tc->backendpipe,databuf,TARBLKSZ - r);
  541. }
  542. return 0;
  543. }
  544. /* Now we start to do things that we need to be able to undo
  545. * if something goes wrong. Watch out for the CLEANUP comments to
  546. * keep an eye on what's installed on the disk at each point.
  547. */
  548. push_cleanup(cu_installnew, ~ehflag_normaltidy, NULL, 0, 1, (void *)nifd);
  549. /* CLEANUP: Now we either have the old file on the disk, or not, in
  550. * its original filename.
  551. */
  552. #ifdef WITH_SELINUX
  553. /* Set selinux_enabled if it is not already set (singleton) */
  554. if (selinux_enabled < 0)
  555. selinux_enabled = (is_selinux_enabled() > 0);
  556. /* Since selinux is enabled, try and set the context */
  557. if (selinux_enabled > 0) {
  558. /*
  559. * well, we could use
  560. * void set_matchpathcon_printf(void (*f)(const char *fmt, ...));
  561. * to redirect the errors from the following bit, but that
  562. * seems too much effort.
  563. */
  564. /*
  565. * Do nothing if we can't figure out what the context is,
  566. * or if it has no context; in which case the default
  567. * context shall be applied.
  568. */
  569. if( ! ((matchpathcon(fnamevb.buf,
  570. (nifd->namenode->statoverride ?
  571. nifd->namenode->statoverride->mode : ti->Mode)
  572. & ~S_IFMT, &scontext) != 0) ||
  573. (strcmp(scontext, "<<none>>") == 0)))
  574. {
  575. if(setfscreatecon(scontext) < 0)
  576. perror("Error setting security context for file object:");
  577. }
  578. }
  579. #endif /* WITH_SELINUX */
  580. /* Extract whatever it is as .dpkg-new ... */
  581. switch (ti->Type) {
  582. case NormalFile0: case NormalFile1:
  583. /* We create the file with mode 0 to make sure nobody can do anything with
  584. * it until we apply the proper mode, which might be a statoverride.
  585. */
  586. fd= open(fnamenewvb.buf, (O_CREAT|O_EXCL|O_WRONLY), 0);
  587. if (fd < 0) ohshite(_("unable to create `%.255s'"),ti->Name);
  588. push_cleanup(cu_closefd, ehflag_bombout, NULL, 0, 1, &fd);
  589. debug(dbg_eachfiledetail,"tarobject NormalFile[01] open size=%lu",
  590. (unsigned long)ti->Size);
  591. { char fnamebuf[256];
  592. fd_fd_copy(tc->backendpipe, fd, ti->Size, _("backend dpkg-deb during `%.255s'"),quote_filename(fnamebuf,256,ti->Name));
  593. }
  594. r= ti->Size % TARBLKSZ;
  595. if (r > 0) r= safe_read(tc->backendpipe,databuf,TARBLKSZ - r);
  596. if (nifd->namenode->statoverride)
  597. debug(dbg_eachfile, "tarobject ... stat override, uid=%d, gid=%d, mode=%04o",
  598. nifd->namenode->statoverride->uid,
  599. nifd->namenode->statoverride->gid,
  600. nifd->namenode->statoverride->mode);
  601. if (fchown(fd,
  602. nifd->namenode->statoverride ? nifd->namenode->statoverride->uid : ti->UserID,
  603. nifd->namenode->statoverride ? nifd->namenode->statoverride->gid : ti->GroupID))
  604. ohshite(_("error setting ownership of `%.255s'"),ti->Name);
  605. am=(nifd->namenode->statoverride ? nifd->namenode->statoverride->mode : ti->Mode) & ~S_IFMT;
  606. if (fchmod(fd,am))
  607. ohshite(_("error setting permissions of `%.255s'"),ti->Name);
  608. pop_cleanup(ehflag_normaltidy); /* fd= open(fnamenewvb.buf) */
  609. if (close(fd))
  610. ohshite(_("error closing/writing `%.255s'"),ti->Name);
  611. newtarobject_utime(fnamenewvb.buf,ti);
  612. break;
  613. case FIFO:
  614. if (mkfifo(fnamenewvb.buf,0))
  615. ohshite(_("error creating pipe `%.255s'"),ti->Name);
  616. debug(dbg_eachfiledetail,"tarobject FIFO");
  617. newtarobject_allmodes(fnamenewvb.buf,ti, nifd->namenode->statoverride);
  618. break;
  619. case CharacterDevice:
  620. if (mknod(fnamenewvb.buf,S_IFCHR, ti->Device))
  621. ohshite(_("error creating device `%.255s'"),ti->Name);
  622. debug(dbg_eachfiledetail,"tarobject CharacterDevice");
  623. newtarobject_allmodes(fnamenewvb.buf,ti, nifd->namenode->statoverride);
  624. break;
  625. case BlockDevice:
  626. if (mknod(fnamenewvb.buf,S_IFBLK, ti->Device))
  627. ohshite(_("error creating device `%.255s'"),ti->Name);
  628. debug(dbg_eachfiledetail,"tarobject BlockDevice");
  629. newtarobject_allmodes(fnamenewvb.buf,ti, nifd->namenode->statoverride);
  630. break;
  631. case HardLink:
  632. varbufreset(&hardlinkfn);
  633. varbufaddstr(&hardlinkfn,instdir); varbufaddc(&hardlinkfn,'/');
  634. varbufaddstr(&hardlinkfn,ti->LinkName); varbufaddc(&hardlinkfn,0);
  635. if (link(hardlinkfn.buf,fnamenewvb.buf))
  636. ohshite(_("error creating hard link `%.255s'"),ti->Name);
  637. debug(dbg_eachfiledetail,"tarobject HardLink");
  638. newtarobject_allmodes(fnamenewvb.buf,ti, nifd->namenode->statoverride);
  639. break;
  640. case SymbolicLink:
  641. /* We've already cheched for an existing directory. */
  642. if (symlink(ti->LinkName,fnamenewvb.buf))
  643. ohshite(_("error creating symbolic link `%.255s'"),ti->Name);
  644. debug(dbg_eachfiledetail,"tarobject SymbolicLink creating");
  645. #ifdef HAVE_LCHOWN
  646. if (lchown(fnamenewvb.buf,
  647. nifd->namenode->statoverride ? nifd->namenode->statoverride->uid : ti->UserID,
  648. nifd->namenode->statoverride ? nifd->namenode->statoverride->gid : ti->GroupID))
  649. ohshite(_("error setting ownership of symlink `%.255s'"),ti->Name);
  650. #else
  651. if (chown(fnamenewvb.buf,
  652. nifd->namenode->statoverride ? nifd->namenode->statoverride->uid : ti->UserID,
  653. nifd->namenode->statoverride ? nifd->namenode->statoverride->gid : ti->GroupID))
  654. ohshite(_("error setting ownership of symlink `%.255s'"),ti->Name);
  655. #endif
  656. break;
  657. case Directory:
  658. /* We've already checked for an existing directory. */
  659. if (mkdir(fnamenewvb.buf,0))
  660. ohshite(_("error creating directory `%.255s'"),ti->Name);
  661. debug(dbg_eachfiledetail,"tarobject Directory creating");
  662. newtarobject_allmodes(fnamenewvb.buf,ti,nifd->namenode->statoverride);
  663. break;
  664. default:
  665. internerr("bad tar type, but already checked");
  666. }
  667. /* CLEANUP: Now we have extracted the new object in .dpkg-new (or,
  668. * if the file already exists as a directory and we were trying to extract
  669. * a directory or symlink, we returned earlier, so we don't need
  670. * to worry about that here).
  671. *
  672. * The old file is still in the original filename,
  673. */
  674. /* First, check to see if it's a conffile. If so we don't install
  675. * it now - we leave it in .dpkg-new for --configure to take care of
  676. */
  677. if (nifd->namenode->flags & fnnf_new_conff) {
  678. debug(dbg_conffdetail,"tarobject conffile extracted");
  679. nifd->namenode->flags |= fnnf_elide_other_lists;
  680. return 0;
  681. }
  682. /* Now we move the old file out of the way, the backup file will
  683. * be deleted later.
  684. */
  685. if (statr) { /* Don't try to back it up if it didn't exist. */
  686. debug(dbg_eachfiledetail,"tarobject new - no backup");
  687. } else {
  688. if (ti->Type == Directory || S_ISDIR(stab.st_mode)) {
  689. /* One of the two is a directory - can't do atomic install. */
  690. debug(dbg_eachfiledetail,"tarobject directory, nonatomic");
  691. nifd->namenode->flags |= fnnf_no_atomic_overwrite;
  692. if (rename(fnamevb.buf,fnametmpvb.buf))
  693. ohshite(_("unable to move aside `%.255s' to install new version"),ti->Name);
  694. } else if (S_ISLNK(stab.st_mode)) {
  695. /* We can't make a symlink with two hardlinks, so we'll have to copy it.
  696. * (Pretend that making a copy of a symlink is the same as linking to it.)
  697. */
  698. varbufreset(&symlinkfn);
  699. do {
  700. varbufextend(&symlinkfn);
  701. r= readlink(fnamevb.buf,symlinkfn.buf,symlinkfn.size);
  702. if (r<0) ohshite(_("unable to read link `%.255s'"),ti->Name);
  703. } while ((size_t)r == symlinkfn.size);
  704. symlinkfn.used= r; varbufaddc(&symlinkfn,0);
  705. if (symlink(symlinkfn.buf,fnametmpvb.buf))
  706. ohshite(_("unable to make backup symlink for `%.255s'"),ti->Name);
  707. #ifdef HAVE_LCHOWN
  708. if (lchown(fnametmpvb.buf,stab.st_uid,stab.st_gid))
  709. ohshite(_("unable to chown backup symlink for `%.255s'"),ti->Name);
  710. #else
  711. if (chown(fnametmpvb.buf,stab.st_uid,stab.st_gid))
  712. ohshite(_("unable to chown backup symlink for `%.255s'"),ti->Name);
  713. #endif
  714. } else {
  715. debug(dbg_eachfiledetail,"tarobject nondirectory, `link' backup");
  716. if (link(fnamevb.buf,fnametmpvb.buf))
  717. ohshite(_("unable to make backup link of `%.255s' before installing new version"),
  718. ti->Name);
  719. }
  720. }
  721. /* CLEANUP: now the old file is in dpkg-tmp, and the new file is still
  722. * in dpkg-new.
  723. */
  724. #ifdef WITH_SELINUX
  725. /*
  726. * if selinux is enabled, try and set the default security context
  727. * for the renamed file
  728. */
  729. if (selinux_enabled > 0)
  730. if(scontext) {
  731. if(setfscreatecon(scontext) < 0)
  732. perror("Error setting security context for next file object:");
  733. freecon(scontext);
  734. scontext = NULL;
  735. }
  736. #endif /* WITH_SELINUX */
  737. if (rename(fnamenewvb.buf,fnamevb.buf))
  738. ohshite(_("unable to install new version of `%.255s'"),ti->Name);
  739. /* CLEANUP: now the new file is in the destination file, and the
  740. * old file is in dpkg-tmp to be cleaned up later. We now need
  741. * to take a different attitude to cleanup, because we need to
  742. * remove the new file.
  743. */
  744. nifd->namenode->flags |= fnnf_placed_on_disk;
  745. #ifdef WITH_SELINUX
  746. /*
  747. * if selinux is enabled, restore the default security context
  748. */
  749. if (selinux_enabled > 0)
  750. if(setfscreatecon(NULL) < 0)
  751. perror("Error restoring default security context:");
  752. #endif /* WITH_SELINUX */
  753. nifd->namenode->flags |= fnnf_elide_other_lists;
  754. debug(dbg_eachfiledetail,"tarobject done and installed");
  755. return 0;
  756. }
  757. static int try_deconfigure_can(int (*force_p)(struct deppossi*),
  758. struct pkginfo *pkg,
  759. struct deppossi *pdep,
  760. const char *action,
  761. struct pkginfo *removal,
  762. const char *why) {
  763. /* Also checks whether the pdep is forced, first, according to force_p.
  764. * force_p may be 0 in which case nothing is considered forced.
  765. *
  766. * Action is a string describing the action which causes the
  767. * deconfiguration:
  768. * removal of <package> (due to Conflicts+Depends removal!=0)
  769. * installation of <package> (due to Breaks removal==0)
  770. *
  771. * Return values: 2: forced (no deconfiguration needed, why is printed)
  772. * 1: deconfiguration queued ok (no message printed)
  773. * 0: not possible (why is printed)
  774. */
  775. struct packageinlist *newdeconf;
  776. if (force_p && force_p(pdep)) {
  777. fprintf(stderr, _("dpkg: warning - "
  778. "ignoring dependency problem with %s:\n%s"),
  779. action, why);
  780. return 2;
  781. } else if (f_autodeconf) {
  782. if (pkg->installed.essential) {
  783. if (fc_removeessential) {
  784. fprintf(stderr, _("dpkg: warning - considering deconfiguration of essential\n"
  785. " package %s, to enable %s.\n"),
  786. pkg->name, action);
  787. } else {
  788. fprintf(stderr, _("dpkg: no, %s is essential, will not deconfigure\n"
  789. " it in order to enable %s.\n"),
  790. pkg->name, action);
  791. return 0;
  792. }
  793. }
  794. pkg->clientdata->istobe= itb_deconfigure;
  795. newdeconf = m_malloc(sizeof(struct packageinlist));
  796. newdeconf->next= deconfigure;
  797. newdeconf->pkg= pkg;
  798. newdeconf->xinfo= removal;
  799. deconfigure= newdeconf;
  800. return 1;
  801. } else {
  802. fprintf(stderr, _("dpkg: no, cannot proceed with %s (--auto-deconfigure will help):\n%s"),
  803. action, why);
  804. return 0;
  805. }
  806. }
  807. static int try_remove_can(struct deppossi *pdep,
  808. struct pkginfo *fixbyrm,
  809. const char *why) {
  810. char action[512];
  811. sprintf(action, _("removal of %.250s"), fixbyrm->name);
  812. return try_deconfigure_can(force_depends, pdep->up->up, pdep,
  813. action, fixbyrm, why);
  814. }
  815. void check_breaks(struct dependency *dep, struct pkginfo *pkg,
  816. const char *pfilename) {
  817. struct pkginfo *fixbydeconf;
  818. struct varbuf why;
  819. int ok;
  820. varbufinit(&why);
  821. fixbydeconf = NULL;
  822. if (depisok(dep, &why, &fixbydeconf, 0)) {
  823. varbuffree(&why);
  824. return;
  825. }
  826. varbufaddc(&why, 0);
  827. if (fixbydeconf && f_autodeconf) {
  828. char action[512];
  829. ensure_package_clientdata(fixbydeconf);
  830. assert(fixbydeconf->clientdata->istobe == itb_normal);
  831. sprintf(action, _("installation of %.250s"), pkg->name);
  832. fprintf(stderr, _("dpkg: considering deconfiguration of %s,"
  833. " which would be broken by %s ...\n"),
  834. fixbydeconf->name, action);
  835. ok= try_deconfigure_can(force_breaks, fixbydeconf, dep->list,
  836. action, NULL, why.buf);
  837. if (ok == 1) {
  838. fprintf(stderr, _("dpkg: yes, will deconfigure %s (broken by %s).\n"),
  839. fixbydeconf->name, pkg->name);
  840. }
  841. } else {
  842. fprintf(stderr, _("dpkg: regarding %s containing %s:\n%s"),
  843. pfilename, pkg->name, why.buf);
  844. ok= 0;
  845. }
  846. varbuffree(&why);
  847. if (ok > 0) return;
  848. if (force_breaks(dep->list)) {
  849. fprintf(stderr, _("dpkg: warning - ignoring breakage,"
  850. " may proceed anyway !\n"));
  851. return;
  852. }
  853. if (fixbydeconf && !f_autodeconf) {
  854. ohshit(_("installing %.250s would break %.250s, and\n"
  855. " deconfiguration is not permitted (--auto-deconfigure might help)"),
  856. pkg->name, fixbydeconf->name);
  857. } else {
  858. ohshit(_("installing %.250s would break existing software"),
  859. pkg->name);
  860. }
  861. }
  862. void check_conflict(struct dependency *dep, struct pkginfo *pkg,
  863. const char *pfilename) {
  864. struct pkginfo *fixbyrm;
  865. struct deppossi *pdep, flagdeppossi;
  866. struct varbuf conflictwhy, removalwhy;
  867. struct dependency *providecheck;
  868. varbufinit(&conflictwhy);
  869. varbufinit(&removalwhy);
  870. fixbyrm = NULL;
  871. if (depisok(dep, &conflictwhy, &fixbyrm, 0)) {
  872. varbuffree(&conflictwhy);
  873. varbuffree(&removalwhy);
  874. return;
  875. }
  876. if (fixbyrm) {
  877. ensure_package_clientdata(fixbyrm);
  878. if (fixbyrm->clientdata->istobe == itb_installnew) {
  879. fixbyrm= dep->up;
  880. ensure_package_clientdata(fixbyrm);
  881. }
  882. if (((pkg->available.essential && fixbyrm->installed.essential) ||
  883. (((fixbyrm->want != want_install && fixbyrm->want != want_hold) ||
  884. does_replace(pkg,&pkg->available,fixbyrm)) &&
  885. (!fixbyrm->installed.essential || fc_removeessential)))) {
  886. assert(fixbyrm->clientdata->istobe == itb_normal || fixbyrm->clientdata->istobe == itb_deconfigure);
  887. fixbyrm->clientdata->istobe= itb_remove;
  888. fprintf(stderr, _("dpkg: considering removing %s in favour of %s ...\n"),
  889. fixbyrm->name, pkg->name);
  890. if (!(fixbyrm->status == stat_installed ||
  891. fixbyrm->status == stat_triggerspending ||
  892. fixbyrm->status == stat_triggersawaited)) {
  893. fprintf(stderr,
  894. _("%s is not properly installed - ignoring any dependencies on it.\n"),
  895. fixbyrm->name);
  896. pdep = NULL;
  897. } else {
  898. for (pdep= fixbyrm->installed.depended;
  899. pdep;
  900. pdep= pdep->nextrev) {
  901. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  902. continue;
  903. if (depisok(pdep->up, &removalwhy, NULL, 0))
  904. continue;
  905. varbufaddc(&removalwhy,0);
  906. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  907. break;
  908. }
  909. if (!pdep) {
  910. /* If we haven't found a reason not to yet, let's look some more. */
  911. for (providecheck= fixbyrm->installed.depends;
  912. providecheck;
  913. providecheck= providecheck->next) {
  914. if (providecheck->type != dep_provides) continue;
  915. for (pdep= providecheck->list->ed->installed.depended;
  916. pdep;
  917. pdep= pdep->nextrev) {
  918. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  919. continue;
  920. if (depisok(pdep->up, &removalwhy, NULL, 0))
  921. continue;
  922. varbufaddc(&removalwhy,0);
  923. fprintf(stderr, _("dpkg"
  924. ": may have trouble removing %s, as it provides %s ...\n"),
  925. fixbyrm->name, providecheck->list->ed->name);
  926. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  927. goto break_from_both_loops_at_once;
  928. }
  929. }
  930. break_from_both_loops_at_once:;
  931. }
  932. }
  933. if (!pdep && skip_due_to_hold(fixbyrm)) {
  934. pdep= &flagdeppossi;
  935. }
  936. if (!pdep && (fixbyrm->eflag & eflagf_reinstreq)) {
  937. if (fc_removereinstreq) {
  938. fprintf(stderr, _("dpkg: package %s requires reinstallation, but will"
  939. " remove anyway as you request.\n"), fixbyrm->name);
  940. } else {
  941. fprintf(stderr, _("dpkg: package %s requires reinstallation, "
  942. "will not remove.\n"), fixbyrm->name);
  943. pdep= &flagdeppossi;
  944. }
  945. }
  946. if (!pdep) {
  947. if (cflict_index >= MAXCONFLICTORS)
  948. ohshit(_("package %s has too many Conflicts/Replaces pairs"),
  949. pkg->name);
  950. /* This conflict is OK - we'll remove the conflictor. */
  951. conflictor[cflict_index++]= fixbyrm;
  952. varbuffree(&conflictwhy); varbuffree(&removalwhy);
  953. fprintf(stderr, _("dpkg: yes, will remove %s in favour of %s.\n"),
  954. fixbyrm->name, pkg->name);
  955. return;
  956. }
  957. fixbyrm->clientdata->istobe= itb_normal; /* put it back */
  958. }
  959. }
  960. varbufaddc(&conflictwhy,0);
  961. fprintf(stderr, _("dpkg: regarding %s containing %s:\n%s"),
  962. pfilename, pkg->name, conflictwhy.buf);
  963. if (!force_conflicts(dep->list))
  964. ohshit(_("conflicting packages - not installing %.250s"),pkg->name);
  965. fprintf(stderr, _("dpkg: warning - ignoring conflict, may proceed anyway !\n"));
  966. varbuffree(&conflictwhy);
  967. return;
  968. }
  969. void cu_cidir(int argc, void **argv) {
  970. char *cidir= (char*)argv[0];
  971. char *cidirrest= (char*)argv[1];
  972. cidirrest[-1]= 0;
  973. ensure_pathname_nonexisting(cidir);
  974. }
  975. void cu_fileslist(int argc, void **argv) {
  976. destroyobstack();
  977. }
  978. void archivefiles(const char *const *argv) {
  979. const char *volatile thisarg;
  980. const char *const *volatile argp;
  981. jmp_buf ejbuf;
  982. int pi[2], fc, nfiles, c, i, r;
  983. FILE *pf;
  984. static struct varbuf findoutput;
  985. const char **arglist;
  986. char *p;
  987. trigproc_install_hooks();
  988. modstatdb_init(admindir,
  989. f_noact ? msdbrw_readonly
  990. : cipaction->arg == act_avail ? msdbrw_write
  991. : fc_nonroot ? msdbrw_write
  992. : msdbrw_needsuperuser);
  993. checkpath();
  994. log_message("startup archives %s", cipaction->olong);
  995. if (f_recursive) {
  996. if (!*argv)
  997. badusage(_("--%s --recursive needs at least one path argument"),cipaction->olong);
  998. m_pipe(pi);
  999. if (!(fc= m_fork())) {
  1000. const char *const *ap;
  1001. int i;
  1002. m_dup2(pi[1],1); close(pi[0]); close(pi[1]);
  1003. for (i=0, ap=argv; *ap; ap++, i++);
  1004. arglist = m_malloc(sizeof(char *) * (i + 15));
  1005. arglist[0] = FIND;
  1006. for (i=1, ap=argv; *ap; ap++, i++) {
  1007. if (strchr(FIND_EXPRSTARTCHARS,(*ap)[0])) {
  1008. char *a;
  1009. a= m_malloc(strlen(*ap)+10);
  1010. strcpy(a,"./");
  1011. strcat(a,*ap);
  1012. arglist[i] = a;
  1013. } else {
  1014. arglist[i] = (const char *)*ap;
  1015. }
  1016. }
  1017. /* When editing these, make sure that arglist is malloced big enough,
  1018. * above.
  1019. */
  1020. arglist[i++] = "-follow";
  1021. arglist[i++] = "-name";
  1022. arglist[i++] = ARCHIVE_FILENAME_PATTERN;
  1023. arglist[i++] = "-type";
  1024. arglist[i++] = "f";
  1025. arglist[i++] = "-print0";
  1026. arglist[i++] = NULL;
  1027. execvp(FIND, (char *const *)arglist);
  1028. ohshite(_("failed to exec find for --recursive"));
  1029. }
  1030. close(pi[1]);
  1031. nfiles= 0;
  1032. pf= fdopen(pi[0],"r"); if (!pf) ohshite(_("failed to fdopen find's pipe"));
  1033. varbufreset(&findoutput);
  1034. while ((c= fgetc(pf)) != EOF) {
  1035. varbufaddc(&findoutput,c);
  1036. if (!c) nfiles++;
  1037. }
  1038. if (ferror(pf)) ohshite(_("error reading find's pipe"));
  1039. if (fclose(pf)) ohshite(_("error closing find's pipe"));
  1040. r= waitsubproc(fc,"find",PROCNOERR);
  1041. if(!(r==0 || r==1))
  1042. ohshit(_("find for --recursive returned unhandled error %i"),r);
  1043. if (!nfiles)
  1044. ohshit(_("searched, but found no packages (files matching *.deb)"));
  1045. varbufaddc(&findoutput,0);
  1046. varbufaddc(&findoutput,0);
  1047. arglist= m_malloc(sizeof(char*)*(nfiles+1));
  1048. p= findoutput.buf; i=0;
  1049. while (*p) {
  1050. arglist[i++]= p;
  1051. while ((c= *p++) != 0);
  1052. }
  1053. arglist[i] = NULL;
  1054. argp= arglist;
  1055. } else {
  1056. if (!*argv) badusage(_("--%s needs at least one package archive file argument"),
  1057. cipaction->olong);
  1058. argp= argv;
  1059. }
  1060. currenttime = time(NULL);
  1061. varbufinit(&fnamevb);
  1062. varbufinit(&fnametmpvb);
  1063. varbufinit(&fnamenewvb);
  1064. varbufaddstr(&fnamevb,instdir); varbufaddc(&fnamevb,'/');
  1065. varbufaddstr(&fnametmpvb,instdir); varbufaddc(&fnametmpvb,'/');
  1066. varbufaddstr(&fnamenewvb,instdir); varbufaddc(&fnamenewvb,'/');
  1067. fnameidlu= fnamevb.used;
  1068. ensure_diversions();
  1069. ensure_statoverrides();
  1070. while ((thisarg = *argp++) != NULL) {
  1071. if (setjmp(ejbuf)) {
  1072. error_unwind(ehflag_bombout);
  1073. if (onerr_abort > 0) break;
  1074. continue;
  1075. }
  1076. push_error_handler(&ejbuf,print_error_perpackage,thisarg);
  1077. process_archive(thisarg);
  1078. onerr_abort++;
  1079. if (ferror(stdout)) werr("stdout");
  1080. if (ferror(stderr)) werr("stderr");
  1081. onerr_abort--;
  1082. set_error_display(NULL, NULL);
  1083. error_unwind(ehflag_normaltidy);
  1084. }
  1085. switch (cipaction->arg) {
  1086. case act_install:
  1087. case act_configure:
  1088. case act_triggers:
  1089. case act_remove:
  1090. case act_purge:
  1091. process_queue();
  1092. case act_unpack:
  1093. case act_avail:
  1094. break;
  1095. default:
  1096. internerr("unknown action");
  1097. }
  1098. trigproc_run_deferred();
  1099. modstatdb_shutdown();
  1100. }
  1101. int wanttoinstall(struct pkginfo *pkg, const struct versionrevision *ver, int saywhy) {
  1102. /* Decide whether we want to install a new version of the package.
  1103. * ver is the version we might want to install. If saywhy is 1 then
  1104. * if we skip the package we say what we are doing (and, if we are
  1105. * selecting a previously deselected package, say so and actually do
  1106. * the select). want_install returns 0 if the package should be
  1107. * skipped and 1 if it should be installed.
  1108. *
  1109. * ver may be 0, in which case saywhy must be 0 and want_install may
  1110. * also return -1 to mean it doesn't know because it would depend on
  1111. * the version number.
  1112. */
  1113. int r;
  1114. if (pkg->want != want_install && pkg->want != want_hold) {
  1115. if (f_alsoselect) {
  1116. if (saywhy) {
  1117. printf(_("Selecting previously deselected package %s.\n"),pkg->name);
  1118. pkg->want= want_install;
  1119. }
  1120. return 1;
  1121. } else {
  1122. if (saywhy) printf(_("Skipping deselected package %s.\n"),pkg->name);
  1123. return 0;
  1124. }
  1125. }
  1126. if (!(pkg->status == stat_installed ||
  1127. pkg->status == stat_triggersawaited ||
  1128. pkg->status == stat_triggerspending))
  1129. return 1;
  1130. if (!ver) return -1;
  1131. r= versioncompare(ver,&pkg->installed.version);
  1132. if (r > 0) {
  1133. return 1;
  1134. } else if (r == 0) {
  1135. /* Same version fully installed. */
  1136. if (f_skipsame) {
  1137. if (saywhy) fprintf(stderr, _("Version %.250s of %.250s already installed, "
  1138. "skipping.\n"),
  1139. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1140. pkg->name);
  1141. return 0;
  1142. } else {
  1143. return 1;
  1144. }
  1145. } else {
  1146. if (fc_downgrade) {
  1147. if (saywhy) fprintf(stderr, _("%s - warning: downgrading %.250s "
  1148. "from %.250s to %.250s.\n"), DPKG, pkg->name,
  1149. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1150. versiondescribe(&pkg->available.version, vdew_nonambig));
  1151. return 1;
  1152. } else {
  1153. if (saywhy) fprintf(stderr, _("Will not downgrade %.250s from version %.250s "
  1154. "to %.250s, skipping.\n"), pkg->name,
  1155. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1156. versiondescribe(&pkg->available.version, vdew_nonambig));
  1157. return 0;
  1158. }
  1159. }
  1160. }
  1161. struct fileinlist *newconff_append(struct fileinlist ***newconffileslastp_io,
  1162. struct filenamenode *namenode) {
  1163. struct fileinlist *newconff;
  1164. newconff= m_malloc(sizeof(struct fileinlist));
  1165. newconff->next = NULL;
  1166. newconff->namenode= namenode;
  1167. **newconffileslastp_io= newconff;
  1168. *newconffileslastp_io= &newconff->next;
  1169. return newconff;
  1170. }
  1171. /* vi: ts=8 sw=2
  1172. */