archives.c 42 KB

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