archives.c 42 KB

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