archives.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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 <iwj10@cus.cam.ac.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <errno.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <utime.h>
  27. #include <assert.h>
  28. #include <time.h>
  29. #include <ctype.h>
  30. #include <fcntl.h>
  31. #include <sys/stat.h>
  32. #include <sys/types.h>
  33. #include <config.h>
  34. #include <dpkg.h>
  35. #include <dpkg-db.h>
  36. #include <tarfn.h>
  37. #include <myopt.h>
  38. #include "filesdb.h"
  39. #include "main.h"
  40. #include "archives.h"
  41. int filesavespackage(struct fileinlist *file, struct pkginfo *pkgtobesaved,
  42. struct pkginfo *pkgbeinginstalled) {
  43. struct pkginfo *divpkg, *thirdpkg;
  44. struct filepackages *packageslump;
  45. int i;
  46. debug(dbg_eachfiledetail,"filesavespackage file `%s' package %s",
  47. file->namenode->name,pkgtobesaved->name);
  48. /* A package can only be saved by a file or directory which is part
  49. * only of itself - it must be neither part of the new package being
  50. * installed nor part of any 3rd package (this is important so that
  51. * shared directories don't stop packages from disappearing).
  52. */
  53. /* Is the file in the package being installed ? If so then it can't save.
  54. */
  55. if (file->namenode->flags & fnnf_new_inarchive) {
  56. debug(dbg_eachfiledetail,"filesavespackage ... in new archive -- no save");
  57. return 0;
  58. }
  59. /* If the file is a contended one and it's overridden by either
  60. * the package we're considering disappearing or the package
  61. * we're installing then they're not actually the same file, so
  62. * we can't disappear the package - it is saved by this file.
  63. */
  64. if (file->namenode->divert && file->namenode->divert->useinstead) {
  65. divpkg= file->namenode->divert->pkg;
  66. if (divpkg == pkgtobesaved || divpkg == pkgbeinginstalled) {
  67. debug(dbg_eachfiledetail,"filesavespackage ... diverted -- save!");
  68. return 1;
  69. }
  70. }
  71. /* Look for a 3rd package which can take over the file (in case
  72. * it's a directory which is shared by many packages.
  73. */
  74. for (packageslump= file->namenode->packages;
  75. packageslump;
  76. packageslump= packageslump->more) {
  77. for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) {
  78. thirdpkg= packageslump->pkgs[i];
  79. debug(dbg_eachfiledetail, "filesavespackage ... also in %s",
  80. thirdpkg->name);
  81. /* Is this not the package being installed or the one being
  82. * checked for disappearance ?
  83. */
  84. if (thirdpkg == pkgbeinginstalled || thirdpkg == pkgtobesaved) continue;
  85. /* If !fileslistvalid then we've already disappeared this one, so
  86. * we shouldn't try to make it take over this shared directory.
  87. */
  88. debug(dbg_eachfiledetail,"filesavespackage ... is 3rd package");
  89. if (!thirdpkg->clientdata->fileslistvalid) {
  90. debug(dbg_eachfiledetail,
  91. _("process_archive ... already disappeared !"));
  92. continue;
  93. }
  94. /* We've found a package that can take this file. */
  95. debug(dbg_eachfiledetail, "filesavespackage ... taken -- no save");
  96. return 0;
  97. }
  98. }
  99. debug(dbg_eachfiledetail, "filesavespackage ... not taken -- save !");
  100. return 1;
  101. }
  102. void cu_pathname(int argc, void **argv) {
  103. ensure_pathname_nonexisting((char*)(argv[0]));
  104. }
  105. void cu_backendpipe(int argc, void **argv) {
  106. FILE *f= *(FILE**)argv[0];
  107. if (f) fclose(f);
  108. }
  109. int tarfileread(void *ud, char *buf, int len) {
  110. struct tarcontext *tc= (struct tarcontext*)ud;
  111. int r;
  112. r= fread(buf,1,len,tc->backendpipe);
  113. if (r != len && ferror(tc->backendpipe))
  114. ohshite(_("error reading from dpkg-deb pipe"));
  115. return r;
  116. }
  117. int fnameidlu;
  118. struct varbuf fnamevb;
  119. struct varbuf fnametmpvb;
  120. struct varbuf fnamenewvb;
  121. struct packageinlist *deconfigure= 0;
  122. static time_t currenttime;
  123. static int does_replace(struct pkginfo *newpigp,
  124. struct pkginfoperfile *newpifp,
  125. struct pkginfo *oldpigp) {
  126. struct dependency *dep;
  127. debug(dbg_depcon,"does_replace new=%s old=%s (%s)",newpigp->name,
  128. oldpigp->name,versiondescribe(&oldpigp->installed.version,
  129. vdew_always));
  130. for (dep= newpifp->depends; dep; dep= dep->next) {
  131. if (dep->type != dep_replaces || dep->list->ed != oldpigp) continue;
  132. debug(dbg_depcondetail,"does_replace ... found old, version %s",
  133. versiondescribe(&dep->list->version,vdew_always));
  134. if (!versionsatisfied(&oldpigp->installed,dep->list)) continue;
  135. debug(dbg_depcon,"does_replace ... yes");
  136. return 1;
  137. }
  138. debug(dbg_depcon,"does_replace ... no");
  139. return 0;
  140. }
  141. static void newtarobject_utime(const char *path, struct TarInfo *ti) {
  142. struct utimbuf utb;
  143. utb.actime= currenttime;
  144. utb.modtime= ti->ModTime;
  145. if (utime(path,&utb))
  146. ohshite(_("error setting timestamps of `%.255s'"),ti->Name);
  147. }
  148. static void newtarobject_allmodes(const char *path, struct TarInfo *ti) {
  149. if (chown(path,ti->UserID,ti->GroupID))
  150. ohshite(_("error setting ownership of `%.255s'"),ti->Name);
  151. if (chmod(path,ti->Mode & ~S_IFMT))
  152. ohshite(_("error setting permissions of `%.255s'"),ti->Name);
  153. newtarobject_utime(path,ti);
  154. }
  155. void setupfnamevbs(const char *filename) {
  156. fnamevb.used= fnameidlu;
  157. varbufaddstr(&fnamevb,filename);
  158. varbufaddc(&fnamevb,0);
  159. fnametmpvb.used= fnameidlu;
  160. varbufaddstr(&fnametmpvb,filename);
  161. varbufaddstr(&fnametmpvb,DPKGTEMPEXT);
  162. varbufaddc(&fnametmpvb,0);
  163. fnamenewvb.used= fnameidlu;
  164. varbufaddstr(&fnamenewvb,filename);
  165. varbufaddstr(&fnamenewvb,DPKGNEWEXT);
  166. varbufaddc(&fnamenewvb,0);
  167. debug(dbg_eachfiledetail, "setupvnamevbs main=`%s' tmp=`%s' new=`%s'",
  168. fnamevb.buf, fnametmpvb.buf, fnamenewvb.buf);
  169. }
  170. int unlinkorrmdir(const char *filename) {
  171. /* Returns 0 on success or -1 on failure, just like unlink & rmdir */
  172. int r, e;
  173. if (!rmdir(filename)) {
  174. debug(dbg_eachfiledetail,"unlinkorrmdir `%s' rmdir OK",filename);
  175. return 0;
  176. }
  177. if (errno != ENOTDIR) {
  178. e= errno;
  179. debug(dbg_eachfiledetail,"unlinkorrmdir `%s' rmdir %s",filename,strerror(e));
  180. errno= e; return -1;
  181. }
  182. r= unlink(filename); e= errno;
  183. debug(dbg_eachfiledetail,"unlinkorrmdir `%s' unlink %s",
  184. filename, r ? strerror(e) : "OK");
  185. errno= e; return r;
  186. }
  187. int tarobject(struct TarInfo *ti) {
  188. static struct varbuf conffderefn, hardlinkfn, symlinkfn;
  189. const char *usename;
  190. struct tarcontext *tc= (struct tarcontext*)ti->UserData;
  191. int statr, fd, r, i, existingdirectory;
  192. struct stat stab, stabd;
  193. size_t sz, wsz;
  194. FILE *thefile;
  195. char databuf[TARBLKSZ];
  196. struct fileinlist *nifd;
  197. struct pkginfo *divpkg, *otherpkg;
  198. struct filepackages *packageslump;
  199. /* Append to list of files.
  200. * The trailing / put on the end of names in tarfiles has already
  201. * been stripped by TarExtractor (lib/tarfn.c).
  202. */
  203. nifd= m_malloc(sizeof(struct fileinlist));
  204. nifd->namenode= findnamenode(ti->Name, 0);
  205. nifd->next= 0; *tc->newfilesp= nifd; tc->newfilesp= &nifd->next;
  206. nifd->namenode->flags |= fnnf_new_inarchive;
  207. debug(dbg_eachfile,
  208. _("tarobject ti->Name=`%s' Mode=%lo owner=%u.%u Type=%d(%c)"
  209. " ti->LinkName=`%s' namenode=`%s' flags=%o instead=`%s'"),
  210. ti->Name, (long)ti->Mode, (unsigned)ti->UserID, (unsigned)ti->GroupID, ti->Type,
  211. ti->Type == '\0' ? '_' :
  212. ti->Type >= '0' && ti->Type <= '6' ? "-hlcbdp"[ti->Type - '0'] : '?',
  213. ti->LinkName,
  214. nifd->namenode->name, nifd->namenode->flags,
  215. nifd->namenode->divert && nifd->namenode->divert->useinstead
  216. ? nifd->namenode->divert->useinstead->name : "<none>");
  217. if (nifd->namenode->divert && nifd->namenode->divert->camefrom) {
  218. divpkg= nifd->namenode->divert->pkg;
  219. forcibleerr(fc_overwritediverted,
  220. _("trying to overwrite `%.250s', which is the "
  221. "diverted version of `%.250s'%.10s%.100s%.10s"),
  222. nifd->namenode->name,
  223. nifd->namenode->divert->camefrom->name,
  224. divpkg ? _(" (package: ") : "",
  225. divpkg ? divpkg->name : "",
  226. divpkg ? ")" : "");
  227. }
  228. usename= namenodetouse(nifd->namenode,tc->pkg)->name + 1; /* Skip the leading `/' */
  229. if (nifd->namenode->flags & fnnf_new_conff) {
  230. /* If it's a conffile we have to extract it next to the installed
  231. * version (ie, we do the usual link-following).
  232. */
  233. if (conffderef(tc->pkg, &conffderefn, usename))
  234. usename= conffderefn.buf;
  235. debug(dbg_conff,"tarobject fnnf_new_conff deref=`%s'",usename);
  236. }
  237. setupfnamevbs(usename);
  238. statr= lstat(fnamevb.buf,&stab);
  239. if (statr) {
  240. /* The lstat failed. */
  241. if (errno != ENOENT && errno != ENOTDIR)
  242. ohshite(_("unable to stat `%.255s' (which I was about to install)"),ti->Name);
  243. /* OK, so it doesn't exist.
  244. * However, it's possible that we were in the middle of some other
  245. * backup/restore operation and were rudely interrupted.
  246. * So, we see if we have .dpkg-tmp, and if so we restore it.
  247. */
  248. if (rename(fnametmpvb.buf,fnamevb.buf)) {
  249. if (errno != ENOENT && errno != ENOTDIR)
  250. ohshite(_("unable to clean up mess surrounding `%.255s' before "
  251. "installing another version"),ti->Name);
  252. debug(dbg_eachfiledetail,"tarobject nonexistent");
  253. } else {
  254. debug(dbg_eachfiledetail,"tarobject restored tmp to main");
  255. statr= lstat(fnamevb.buf,&stab);
  256. if (statr) ohshite(_("unable to stat restored `%.255s' before installing"
  257. " another version"), ti->Name);
  258. }
  259. } else {
  260. debug(dbg_eachfiledetail,"tarobject already exists");
  261. }
  262. /* Check to see if it's a directory or link to one and we don't need to
  263. * do anything. This has to be done now so that we don't die due to
  264. * a file overwriting conflict.
  265. */
  266. existingdirectory= 0;
  267. switch (ti->Type) {
  268. case SymbolicLink:
  269. /* If it's already an existing directory, do nothing. */
  270. if (!statr && S_ISDIR(stab.st_mode)) {
  271. debug(dbg_eachfiledetail,"tarobject SymbolicLink exists as directory");
  272. existingdirectory= 1;
  273. }
  274. break;
  275. case Directory:
  276. /* If it's already an existing directory, do nothing. */
  277. if (!stat(fnamevb.buf,&stabd) && S_ISDIR(stabd.st_mode)) {
  278. debug(dbg_eachfiledetail,"tarobject Directory exists");
  279. existingdirectory= 1;
  280. }
  281. break;
  282. case NormalFile0: case NormalFile1:
  283. case CharacterDevice: case BlockDevice: case FIFO:
  284. case HardLink:
  285. break;
  286. default:
  287. ohshit(_("archive contained object `%.255s' of unknown type 0x%x"),ti->Name,ti->Type);
  288. }
  289. if (!existingdirectory) {
  290. for (packageslump= nifd->namenode->packages;
  291. packageslump;
  292. packageslump= packageslump->more) {
  293. for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) {
  294. otherpkg= packageslump->pkgs[i];
  295. if (otherpkg == tc->pkg) continue;
  296. debug(dbg_eachfile, "tarobject ... found in %s",otherpkg->name);
  297. if (nifd->namenode->divert && nifd->namenode->divert->useinstead) {
  298. /* Right, so we may be diverting this file. This makes the conflict
  299. * OK iff one of us is the diverting package (we don't need to
  300. * check for both being the diverting package, obviously).
  301. */
  302. divpkg= nifd->namenode->divert->pkg;
  303. debug(dbg_eachfile, "tarobject ... diverted, divpkg=%s",
  304. divpkg ? divpkg->name : "<none>");
  305. if (otherpkg == divpkg || tc->pkg == divpkg) continue;
  306. }
  307. /* Nope ? Hmm, file conflict, perhaps. Check Replaces. */
  308. if (otherpkg->clientdata->replacingfilesandsaid) continue;
  309. /* Is the package with the conflicting file in the `config files
  310. * only' state ? If so it must be a config file and we can
  311. * silenty take it over.
  312. */
  313. if (otherpkg->status == stat_configfiles) continue;
  314. /* Perhaps we're removing a conflicting package ? */
  315. if (otherpkg->clientdata->istobe == itb_remove) continue;
  316. if (does_replace(tc->pkg,&tc->pkg->available,otherpkg)) {
  317. printf(_("Replacing files in old package %s ...\n"),otherpkg->name);
  318. otherpkg->clientdata->replacingfilesandsaid= 1;
  319. } else {
  320. if (S_ISDIR(stab.st_mode)) {
  321. forcibleerr(fc_overwritedir, _("trying to overwrite directory `%.250s' "
  322. "in package %.250s with nondirectory"),
  323. nifd->namenode->name,otherpkg->name);
  324. } else {
  325. /* WTA: At this point we are replacing something without a Replaces.
  326. * if the new object is a directory and the previous object does not
  327. * exist assume it's also a directory and don't complain
  328. */
  329. if (! (statr && ti->Type==Directory))
  330. forcibleerr(fc_overwrite,
  331. _("trying to overwrite `%.250s', which is also in package %.250s"),
  332. nifd->namenode->name,otherpkg->name);
  333. }
  334. }
  335. }
  336. }
  337. }
  338. /* Now, at this stage we want to make sure neither of .dpkg-new and .dpkg-tmp
  339. * are hanging around.
  340. */
  341. ensure_pathname_nonexisting(fnamenewvb.buf);
  342. ensure_pathname_nonexisting(fnametmpvb.buf);
  343. if (existingdirectory) return 0;
  344. /* Now we start to do things that we need to be able to undo
  345. * if something goes wrong.
  346. */
  347. push_cleanup(cu_installnew,~ehflag_normaltidy, 0,0, 1,(void*)nifd);
  348. /* Extract whatever it is as .dpkg-new ... */
  349. switch (ti->Type) {
  350. case NormalFile0: case NormalFile1:
  351. fd= open(fnamenewvb.buf, O_CREAT|O_EXCL|O_WRONLY,
  352. ti->Mode & (S_IRUSR|S_IRGRP|S_IROTH));
  353. if (fd < 0) ohshite("unable to create `%.255s'",ti->Name);
  354. thefile= fdopen(fd,"w");
  355. if (!thefile) { close(fd); ohshite(_("unable to fdopen for `%.255s'"),ti->Name); }
  356. push_cleanup(cu_closefile,ehflag_bombout, 0,0, 1,(void*)thefile);
  357. debug(dbg_eachfiledetail,"tarobject NormalFile[01] open size=%lu",
  358. (unsigned long)ti->Size);
  359. for (sz= ti->Size; sz > 0; sz -= wsz) {
  360. wsz= sz > TARBLKSZ ? TARBLKSZ : sz;
  361. r= fread(databuf,1,TARBLKSZ,tc->backendpipe);
  362. if (r<TARBLKSZ) {
  363. if (ferror(tc->backendpipe)) {
  364. ohshite(_("error reading dpkg-deb during `%.255s'"),ti->Name);
  365. } else {
  366. errno= 0;
  367. return -1;
  368. }
  369. }
  370. if (fwrite(databuf,1,wsz,thefile) != wsz)
  371. ohshite(_("error writing to `%.255s'"),ti->Name);
  372. }
  373. if (fchown(fd,ti->UserID,ti->GroupID))
  374. ohshite(_("error setting ownership of `%.255s'"),ti->Name);
  375. /* We flush the stream here to avoid any future
  376. * writes, which will mask any setuid or setgid
  377. * attemps by fchmod below.
  378. */
  379. if (fflush(thefile) == EOF)
  380. ohshite(_("error flushing `%.255s'"),ti->Name);
  381. if (fchmod(fd,ti->Mode & ~S_IFMT))
  382. ohshite(_("error setting permissions of `%.255s'"),ti->Name);
  383. pop_cleanup(ehflag_normaltidy); /* thefile= fdopen(fd) */
  384. if (fclose(thefile))
  385. ohshite(_("error closing/writing `%.255s'"),ti->Name);
  386. newtarobject_utime(fnamenewvb.buf,ti);
  387. break;
  388. case FIFO:
  389. if (mkfifo(fnamenewvb.buf,ti->Mode & S_IFMT))
  390. ohshite(_("error creating pipe `%.255s'"),ti->Name);
  391. debug(dbg_eachfiledetail,"tarobject FIFO");
  392. newtarobject_allmodes(fnamenewvb.buf,ti);
  393. break;
  394. case CharacterDevice: case BlockDevice:
  395. if (mknod(fnamenewvb.buf,ti->Mode & S_IFMT,ti->Device))
  396. ohshite(_("error creating device `%.255s'"),ti->Name);
  397. debug(dbg_eachfiledetail,"tarobject CharacterDevice|BlockDevice");
  398. newtarobject_allmodes(fnamenewvb.buf,ti);
  399. break;
  400. case HardLink:
  401. varbufreset(&hardlinkfn);
  402. varbufaddstr(&hardlinkfn,instdir); varbufaddc(&hardlinkfn,'/');
  403. varbufaddstr(&hardlinkfn,ti->LinkName); varbufaddc(&hardlinkfn,0);
  404. if (link(hardlinkfn.buf,fnamenewvb.buf))
  405. ohshite(_("error creating hard link `%.255s'"),ti->Name);
  406. debug(dbg_eachfiledetail,"tarobject HardLink");
  407. newtarobject_allmodes(fnamenewvb.buf,ti);
  408. break;
  409. case SymbolicLink:
  410. /* We've already cheched for an existing directory. */
  411. if (symlink(ti->LinkName,fnamenewvb.buf))
  412. ohshite(_("error creating symbolic link `%.255s'"),ti->Name);
  413. debug(dbg_eachfiledetail,"tarobject SymbolicLink creating");
  414. #ifdef HAVE_LCHOWN
  415. if (lchown(fnamenewvb.buf,ti->UserID,ti->GroupID))
  416. #else
  417. if (chown(fnamenewvb.buf,ti->UserID,ti->GroupID))
  418. #endif
  419. ohshite(_("error setting ownership of symlink `%.255s'"),ti->Name);
  420. break;
  421. case Directory:
  422. /* We've already checked for an existing directory. */
  423. if (mkdir(fnamenewvb.buf,
  424. ti->Mode & (S_IRUSR|S_IRGRP|S_IROTH | S_IXUSR|S_IXGRP|S_IXOTH)))
  425. ohshite(_("error creating directory `%.255s'"),ti->Name);
  426. debug(dbg_eachfiledetail,"tarobject Directory creating");
  427. newtarobject_allmodes(fnamenewvb.buf,ti);
  428. break;
  429. default:
  430. internerr("bad tar type, but already checked");
  431. }
  432. /*
  433. * Now we have extracted the new object in .dpkg-new (or, if the
  434. * file already exists as a directory and we were trying to extract
  435. * a directory or symlink, we returned earlier, so we don't need
  436. * to worry about that here).
  437. */
  438. /* First, check to see if it's a conffile. If so we don't install
  439. * it now - we leave it in .dpkg-new for --configure to take care of
  440. */
  441. if (nifd->namenode->flags & fnnf_new_conff) {
  442. debug(dbg_conffdetail,"tarobject conffile extracted");
  443. nifd->namenode->flags |= fnnf_elide_other_lists;
  444. return 0;
  445. }
  446. /* Now we install it. If we can do an atomic overwrite we do so.
  447. * If not we move aside the old file and then install the new.
  448. * The backup file will be deleted later.
  449. */
  450. if (statr) { /* Don't try to back it up if it didn't exist. */
  451. debug(dbg_eachfiledetail,"tarobject new - no backup");
  452. } else {
  453. if (ti->Type == Directory || S_ISDIR(stab.st_mode)) {
  454. /* One of the two is a directory - can't do atomic install. */
  455. debug(dbg_eachfiledetail,"tarobject directory, nonatomic");
  456. nifd->namenode->flags |= fnnf_no_atomic_overwrite;
  457. if (rename(fnamevb.buf,fnametmpvb.buf))
  458. ohshite(_("unable to move aside `%.255s' to install new version"),ti->Name);
  459. } else if (S_ISLNK(stab.st_mode)) {
  460. /* We can't make a symlink with two hardlinks, so we'll have to copy it.
  461. * (Pretend that making a copy of a symlink is the same as linking to it.)
  462. */
  463. varbufreset(&symlinkfn);
  464. do {
  465. varbufextend(&symlinkfn);
  466. r= readlink(fnamevb.buf,symlinkfn.buf,symlinkfn.size);
  467. if (r<0) ohshite("unable to read link `%.255s'",ti->Name);
  468. } while (r == symlinkfn.size);
  469. symlinkfn.used= r; varbufaddc(&symlinkfn,0);
  470. if (symlink(symlinkfn.buf,fnametmpvb.buf))
  471. ohshite(_("unable to make backup symlink for `%.255s'"),ti->Name);
  472. #ifdef HAVE_LCHOWN
  473. if (lchown(fnametmpvb.buf,stab.st_uid,stab.st_gid))
  474. #else
  475. if (chown(fnametmpvb.buf,stab.st_uid,stab.st_gid))
  476. #endif
  477. ohshite(_("unable to chown backup symlink for `%.255s'"),ti->Name);
  478. } else {
  479. debug(dbg_eachfiledetail,"tarobject nondirectory, `link' backup");
  480. if (link(fnamevb.buf,fnametmpvb.buf))
  481. ohshite(_("unable to make backup link of `%.255s' before installing new version"),
  482. ti->Name);
  483. }
  484. }
  485. if (rename(fnamenewvb.buf,fnamevb.buf))
  486. ohshite(_("unable to install new version of `%.255s'"),ti->Name);
  487. nifd->namenode->flags |= fnnf_elide_other_lists;
  488. debug(dbg_eachfiledetail,"tarobject done and installed");
  489. return 0;
  490. }
  491. static int try_remove_can(struct deppossi *pdep,
  492. struct pkginfo *fixbyrm,
  493. const char *why) {
  494. struct packageinlist *newdeconf;
  495. if (force_depends(pdep)) {
  496. fprintf(stderr, _("dpkg: warning - "
  497. "ignoring dependency problem with removal of %s:\n%s"),
  498. fixbyrm->name, why);
  499. return 1;
  500. } else if (f_autodeconf) {
  501. if (pdep->up->up->installed.essential) {
  502. if (fc_removeessential) {
  503. fprintf(stderr, _("dpkg: warning - considering deconfiguration of essential\n"
  504. " package %s, to enable removal of %s.\n"),
  505. pdep->up->up->name,fixbyrm->name);
  506. } else {
  507. fprintf(stderr, _("dpkg: no, %s is essential, will not deconfigure\n"
  508. " it in order to enable removal of %s.\n"),
  509. pdep->up->up->name,fixbyrm->name);
  510. return 0;
  511. }
  512. }
  513. pdep->up->up->clientdata->istobe= itb_deconfigure;
  514. newdeconf= m_malloc(sizeof(struct packageinlist));
  515. newdeconf->next= deconfigure;
  516. newdeconf->pkg= pdep->up->up;
  517. deconfigure= newdeconf;
  518. return 1;
  519. } else {
  520. fprintf(stderr, _("dpkg: no, cannot remove %s (--auto-deconfigure will help):\n%s"),
  521. fixbyrm->name, why);
  522. return 0;
  523. }
  524. }
  525. void check_conflict(struct dependency *dep, struct pkginfo *pkg,
  526. const char *pfilename, struct conflict **conflictorp) {
  527. struct pkginfo *fixbyrm;
  528. struct deppossi *pdep, flagdeppossi;
  529. struct varbuf conflictwhy, removalwhy;
  530. struct dependency *providecheck;
  531. varbufinit(&conflictwhy);
  532. varbufinit(&removalwhy);
  533. for ( ; *conflictorp && (*conflictorp)->next ; *conflictorp= (*conflictorp)->next );
  534. fixbyrm= 0;
  535. if (depisok(dep, &conflictwhy, &fixbyrm, 0)) {
  536. varbuffree(&conflictwhy);
  537. varbuffree(&removalwhy);
  538. return;
  539. }
  540. if (fixbyrm) {
  541. ensure_package_clientdata(fixbyrm);
  542. if (fixbyrm->clientdata->istobe == itb_installnew) {
  543. fixbyrm= dep->up;
  544. ensure_package_clientdata(fixbyrm);
  545. }
  546. if (((pkg->available.essential && fixbyrm->installed.essential) ||
  547. (((fixbyrm->want != want_install && fixbyrm->want != want_hold) ||
  548. does_replace(pkg,&pkg->available,fixbyrm)) &&
  549. (!fixbyrm->installed.essential || fc_removeessential)))) {
  550. assert(fixbyrm->clientdata->istobe == itb_normal);
  551. fixbyrm->clientdata->istobe= itb_remove;
  552. fprintf(stderr, _("dpkg: considering removing %s in favour of %s ...\n"),
  553. fixbyrm->name, pkg->name);
  554. if (fixbyrm->status != stat_installed) {
  555. fprintf(stderr,
  556. _("%s is not properly installed - ignoring any dependencies on it.\n"),
  557. fixbyrm->name);
  558. pdep= 0;
  559. } else {
  560. for (pdep= fixbyrm->installed.depended;
  561. pdep;
  562. pdep= pdep->nextrev) {
  563. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  564. continue;
  565. if (depisok(pdep->up, &removalwhy, 0,0)) continue;
  566. varbufaddc(&removalwhy,0);
  567. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  568. break;
  569. }
  570. if (!pdep) {
  571. /* If we haven't found a reason not to yet, let's look some more. */
  572. for (providecheck= fixbyrm->installed.depends;
  573. providecheck;
  574. providecheck= providecheck->next) {
  575. if (providecheck->type != dep_provides) continue;
  576. for (pdep= providecheck->list->ed->installed.depended;
  577. pdep;
  578. pdep= pdep->nextrev) {
  579. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  580. continue;
  581. if (depisok(pdep->up, &removalwhy, 0,0)) continue;
  582. varbufaddc(&removalwhy,0);
  583. fprintf(stderr, _("dpkg"
  584. ": may have trouble removing %s, as it provides %s ...\n"),
  585. fixbyrm->name, providecheck->list->ed->name);
  586. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  587. goto break_from_both_loops_at_once;
  588. }
  589. }
  590. break_from_both_loops_at_once:;
  591. }
  592. }
  593. if (!pdep && skip_due_to_hold(fixbyrm)) {
  594. pdep= &flagdeppossi;
  595. }
  596. if (!pdep && (fixbyrm->eflag & eflagf_reinstreq)) {
  597. if (fc_removereinstreq) {
  598. fprintf(stderr, _("dpkg: package %s requires reinstallation, but will"
  599. " remove anyway as you request.\n"), fixbyrm->name);
  600. } else {
  601. fprintf(stderr, _("dpkg: package %s requires reinstallation, "
  602. "will not remove.\n"), fixbyrm->name);
  603. pdep= &flagdeppossi;
  604. }
  605. }
  606. if (!pdep) {
  607. /* This conflict is OK - we'll remove the conflictor. */
  608. if (*conflictorp) {
  609. (*conflictorp)->next= nfmalloc(sizeof(struct conflict));
  610. (*conflictorp)->next->cflict= fixbyrm;
  611. (*conflictorp)->next->next= 0;
  612. } else {
  613. *conflictorp= nfmalloc(sizeof(struct conflict));
  614. (*conflictorp)->next= 0;
  615. (*conflictorp)->cflict= fixbyrm;
  616. }
  617. varbuffree(&conflictwhy); varbuffree(&removalwhy);
  618. fprintf(stderr, _("dpkg: yes, will remove %s in favour of %s.\n"),
  619. fixbyrm->name, pkg->name);
  620. return;
  621. }
  622. fixbyrm->clientdata->istobe= itb_normal; /* put it back */
  623. }
  624. }
  625. varbufaddc(&conflictwhy,0);
  626. fprintf(stderr, _("dpkg: regarding %s containing %s:\n%s"),
  627. pfilename, pkg->name, conflictwhy.buf);
  628. if (!force_conflicts(dep->list))
  629. ohshit(_("conflicting packages - not installing %.250s"),pkg->name);
  630. fprintf(stderr, _("dpkg: warning - ignoring conflict, may proceed anyway !\n"));
  631. varbuffree(&conflictwhy);
  632. return;
  633. }
  634. void cu_cidir(int argc, void **argv) {
  635. char *cidir= (char*)argv[0];
  636. char *cidirrest= (char*)argv[1];
  637. cidirrest[-1]= 0;
  638. ensure_pathname_nonexisting(cidir);
  639. }
  640. void cu_fileslist(int argc, void **argv) {
  641. struct fileinlist **headp= (struct fileinlist**)argv[0];
  642. struct fileinlist *current, *next;
  643. for (current= *headp; current; current= next) {
  644. next= current->next;
  645. free(current);
  646. }
  647. }
  648. void archivefiles(const char *const *argv) {
  649. const char *volatile thisarg;
  650. const char *const *volatile argp;
  651. jmp_buf ejbuf;
  652. int pi[2], fc, nfiles, c, i;
  653. FILE *pf;
  654. static struct varbuf findoutput;
  655. const char **arglist;
  656. char *p;
  657. modstatdb_init(admindir,
  658. f_noact ? msdbrw_readonly
  659. : cipaction->arg == act_avail ? msdbrw_write
  660. : fc_nonroot ? msdbrw_write
  661. : msdbrw_needsuperuser);
  662. checkpath();
  663. if (f_recursive) {
  664. if (!*argv)
  665. badusage(_("--%s --recursive needs at least one path argument"),cipaction->olong);
  666. m_pipe(pi);
  667. if (!(fc= m_fork())) {
  668. const char *const *ap;
  669. int i;
  670. m_dup2(pi[1],1); close(pi[0]);
  671. for (i=0, ap=argv; *ap; ap++, i++);
  672. arglist= m_malloc(sizeof(char*)*(i+15));
  673. arglist[0]= FIND;
  674. for (i=1, ap=argv; *ap; ap++, i++) {
  675. if (strchr(FIND_EXPRSTARTCHARS,(*ap)[0])) {
  676. char *a;
  677. a= m_malloc(strlen(*ap)+10);
  678. strcpy(a,"./");
  679. strcat(a,*ap);
  680. arglist[i]= a;
  681. } else {
  682. arglist[i]= *ap;
  683. }
  684. }
  685. arglist[i++]= "-follow"; /* When editing these, make sure that */
  686. arglist[i++]= "-name"; /* arglist is mallocd big enough, above. */
  687. arglist[i++]= ARCHIVE_FILENAME_PATTERN;
  688. arglist[i++]= "-type";
  689. arglist[i++]= "f";
  690. arglist[i++]= "-print0";
  691. arglist[i++]= 0;
  692. execvp(FIND, (char* const*)arglist);
  693. ohshite(_("failed to exec find for --recursive"));
  694. }
  695. nfiles= 0;
  696. pf= fdopen(pi[0],"r"); if (!pf) ohshite(_("failed to fdopen find's pipe"));
  697. close(pi[1]);
  698. varbufreset(&findoutput);
  699. while ((c= fgetc(pf)) != EOF) {
  700. varbufaddc(&findoutput,c);
  701. if (!c) nfiles++;
  702. }
  703. if (ferror(pf)) ohshite(_("error reading find's pipe"));
  704. if (fclose(pf)) ohshite(_("error closing find's pipe"));
  705. waitsubproc(fc,"find",0);
  706. if (!nfiles) ohshit(_("searched, but found no packages (files matching "
  707. "*.deb"));
  708. varbufaddc(&findoutput,0);
  709. varbufaddc(&findoutput,0);
  710. arglist= m_malloc(sizeof(char*)*(nfiles+1));
  711. p= findoutput.buf; i=0;
  712. while (*p) {
  713. arglist[i++]= p;
  714. while ((c= *p++) != 0);
  715. }
  716. arglist[i]= 0;
  717. argp= arglist;
  718. } else {
  719. if (!*argv) badusage(_("--%s needs at least one package archive file argument"),
  720. cipaction->olong);
  721. argp= argv;
  722. }
  723. currenttime= time(0);
  724. varbufaddstr(&fnamevb,instdir); varbufaddc(&fnamevb,'/');
  725. varbufaddstr(&fnametmpvb,instdir); varbufaddc(&fnametmpvb,'/');
  726. varbufaddstr(&fnamenewvb,instdir); varbufaddc(&fnamenewvb,'/');
  727. fnameidlu= fnamevb.used;
  728. ensure_diversions();
  729. while ((thisarg= *argp++) != 0) {
  730. if (setjmp(ejbuf)) {
  731. error_unwind(ehflag_bombout);
  732. if (onerr_abort > 0) break;
  733. continue;
  734. }
  735. push_error_handler(&ejbuf,print_error_perpackage,thisarg);
  736. process_archive(thisarg);
  737. onerr_abort++;
  738. if (ferror(stdout)) werr("stdout");
  739. if (ferror(stderr)) werr("stderr");
  740. onerr_abort--;
  741. set_error_display(0,0);
  742. error_unwind(ehflag_normaltidy);
  743. }
  744. switch (cipaction->arg) {
  745. case act_install:
  746. case act_configure:
  747. case act_remove:
  748. case act_purge:
  749. process_queue();
  750. case act_unpack:
  751. case act_avail:
  752. break;
  753. default:
  754. internerr("unknown action");
  755. }
  756. modstatdb_shutdown();
  757. }
  758. int wanttoinstall(struct pkginfo *pkg, const struct versionrevision *ver, int saywhy) {
  759. /* Decide whether we want to install a new version of the package.
  760. * ver is the version we might want to install. If saywhy is 1 then
  761. * if we skip the package we say what we are doing (and, if we are
  762. * selecting a previously deselected package, say so and actually do
  763. * the select). want_install returns 0 if the package should be
  764. * skipped and 1 if it should be installed.
  765. *
  766. * ver may be 0, in which case saywhy must be 0 and want_install may
  767. * also return -1 to mean it doesn't know because it would depend on
  768. * the version number.
  769. */
  770. enum versiondisplayepochwhen needepochs;
  771. int r;
  772. if (pkg->want != want_install) {
  773. if (f_alsoselect) {
  774. if (saywhy) {
  775. printf(_("Selecting previously deselected package %s.\n"),pkg->name);
  776. pkg->want= want_install;
  777. }
  778. return 1;
  779. } else {
  780. if (saywhy) printf(_("Skipping deselected package %s.\n"),pkg->name);
  781. return 0;
  782. }
  783. }
  784. if (pkg->status != stat_installed) return 1;
  785. if (!ver) return -1;
  786. r= versioncompare(ver,&pkg->installed.version);
  787. if (r > 0) {
  788. return 1;
  789. } else if (r == 0) {
  790. if (f_skipsame && /* same version fully installed ? */
  791. pkg->status == stat_installed && !(pkg->eflag &= eflagf_reinstreq)) {
  792. if (saywhy) fprintf(stderr, _("Version %.250s of %.250s already installed, "
  793. "skipping.\n"),
  794. versiondescribe(&pkg->installed.version,vdew_never),
  795. pkg->name);
  796. return 0;
  797. } else {
  798. return 1;
  799. }
  800. } else {
  801. needepochs= epochsdiffer(&pkg->available.version,&pkg->installed.version) ?
  802. vdew_always : vdew_never;
  803. if (fc_downgrade) {
  804. if (saywhy) fprintf(stderr, _("%s - warning: downgrading %.250s "
  805. "from %.250s to %.250s.\n"), DPKG, pkg->name,
  806. versiondescribe(&pkg->installed.version,needepochs),
  807. versiondescribe(&pkg->available.version,needepochs));
  808. return 1;
  809. } else {
  810. if (saywhy) fprintf(stderr, _("Will not downgrade %.250s from version %.250s "
  811. "to %.250s, skipping.\n"), pkg->name,
  812. versiondescribe(&pkg->installed.version,needepochs),
  813. versiondescribe(&pkg->available.version,needepochs));
  814. return 0;
  815. }
  816. }
  817. }