archives.c 43 KB

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