archives.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  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. * Copyright © 2007-2012 Guillem Jover <guillem@debian.org>
  8. * Copyright © 2011 Linaro Limited
  9. * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
  10. *
  11. * This is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include <config.h>
  25. #include <compat.h>
  26. #include <sys/types.h>
  27. #include <sys/time.h>
  28. #include <sys/stat.h>
  29. #include <assert.h>
  30. #include <errno.h>
  31. #include <ctype.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #include <fcntl.h>
  35. #include <unistd.h>
  36. #include <stdint.h>
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. #include <obstack.h>
  40. #define obstack_chunk_alloc m_malloc
  41. #define obstack_chunk_free free
  42. #include <dpkg/i18n.h>
  43. #include <dpkg/dpkg.h>
  44. #include <dpkg/dpkg-db.h>
  45. #include <dpkg/pkg.h>
  46. #include <dpkg/path.h>
  47. #include <dpkg/fdio.h>
  48. #include <dpkg/buffer.h>
  49. #include <dpkg/subproc.h>
  50. #include <dpkg/command.h>
  51. #include <dpkg/file.h>
  52. #include <dpkg/tarfn.h>
  53. #include <dpkg/options.h>
  54. #include <dpkg/triglib.h>
  55. #ifdef WITH_SELINUX
  56. #include <selinux/selinux.h>
  57. #endif
  58. #include "filesdb.h"
  59. #include "main.h"
  60. #include "archives.h"
  61. #include "filters.h"
  62. #include "infodb.h"
  63. static inline void
  64. fd_writeback_init(int fd)
  65. {
  66. /* Ignore the return code as it should be considered equivalent to an
  67. * asynchronous hint for the kernel, we are doing an fsync() later on
  68. * anyway. */
  69. #if defined(SYNC_FILE_RANGE_WRITE)
  70. sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WRITE);
  71. #elif defined(HAVE_POSIX_FADVISE)
  72. posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
  73. #endif
  74. }
  75. static struct obstack tar_obs;
  76. static bool tarobs_init = false;
  77. /**
  78. * Ensure the obstack is properly initialized.
  79. */
  80. static void ensureobstackinit(void) {
  81. if (!tarobs_init) {
  82. obstack_init(&tar_obs);
  83. tarobs_init = true;
  84. }
  85. }
  86. /**
  87. * Destroy the obstack.
  88. */
  89. static void destroyobstack(void) {
  90. if (tarobs_init) {
  91. obstack_free(&tar_obs, NULL);
  92. tarobs_init = false;
  93. }
  94. }
  95. /**
  96. * Check if a file or directory will save a package from disappearance.
  97. *
  98. * A package can only be saved by a file or directory which is part
  99. * only of itself - it must be neither part of the new package being
  100. * installed nor part of any 3rd package (this is important so that
  101. * shared directories don't stop packages from disappearing).
  102. */
  103. bool
  104. filesavespackage(struct fileinlist *file,
  105. struct pkginfo *pkgtobesaved,
  106. struct pkginfo *pkgbeinginstalled)
  107. {
  108. struct filepackages_iterator *iter;
  109. struct pkgset *divpkgset;
  110. struct pkginfo *thirdpkg;
  111. debug(dbg_eachfiledetail, "filesavespackage file '%s' package %s",
  112. file->namenode->name, pkg_name(pkgtobesaved, pnaw_always));
  113. /* If the file is a contended one and it's overridden by either
  114. * the package we're considering disappearing or the package
  115. * we're installing then they're not actually the same file, so
  116. * we can't disappear the package - it is saved by this file. */
  117. if (file->namenode->divert && file->namenode->divert->useinstead) {
  118. divpkgset = file->namenode->divert->pkgset;
  119. if (divpkgset == pkgtobesaved->set || divpkgset == pkgbeinginstalled->set) {
  120. debug(dbg_eachfiledetail,"filesavespackage ... diverted -- save!");
  121. return true;
  122. }
  123. }
  124. /* Is the file in the package being installed? If so then it can't save. */
  125. if (file->namenode->flags & fnnf_new_inarchive) {
  126. debug(dbg_eachfiledetail,"filesavespackage ... in new archive -- no save");
  127. return false;
  128. }
  129. /* Look for a 3rd package which can take over the file (in case
  130. * it's a directory which is shared by many packages. */
  131. iter = filepackages_iter_new(file->namenode);
  132. while ((thirdpkg = filepackages_iter_next(iter))) {
  133. debug(dbg_eachfiledetail, "filesavespackage ... also in %s",
  134. pkg_name(thirdpkg, pnaw_always));
  135. /* Is this not the package being installed or the one being
  136. * checked for disappearance? */
  137. if (thirdpkg == pkgbeinginstalled || thirdpkg == pkgtobesaved)
  138. continue;
  139. /* A Multi-Arch: same package can share files and their presence in a
  140. * third package of the same set is not a sign that we can get rid of
  141. * it. */
  142. if (pkgtobesaved->installed.multiarch == multiarch_same &&
  143. thirdpkg->set == pkgtobesaved->set)
  144. continue;
  145. /* If !fileslistvalid then we've already disappeared this one, so
  146. * we shouldn't try to make it take over this shared directory. */
  147. debug(dbg_eachfiledetail,"filesavespackage ... is 3rd package");
  148. if (!thirdpkg->clientdata->fileslistvalid) {
  149. debug(dbg_eachfiledetail, "process_archive ... already disappeared!");
  150. continue;
  151. }
  152. /* We've found a package that can take this file. */
  153. debug(dbg_eachfiledetail, "filesavespackage ... taken -- no save");
  154. return false;
  155. }
  156. filepackages_iter_free(iter);
  157. debug(dbg_eachfiledetail, "filesavespackage ... not taken -- save !");
  158. return true;
  159. }
  160. static void
  161. md5hash_prev_conffile(struct pkginfo *pkg, char *oldhash, const char *oldname,
  162. struct filenamenode *namenode)
  163. {
  164. struct pkginfo *otherpkg;
  165. struct conffile *conff;
  166. debug(dbg_conffdetail, "tarobject looking for shared conffile %s",
  167. namenode->name);
  168. for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
  169. if (otherpkg == pkg)
  170. continue;
  171. /* The hash in the Conffiles is only meaningful if the package
  172. * configuration has been at least tried. */
  173. if (otherpkg->status <= stat_unpacked)
  174. continue;
  175. for (conff = otherpkg->installed.conffiles; conff; conff = conff->next) {
  176. if (strcmp(conff->name, namenode->name) == 0)
  177. break;
  178. }
  179. if (conff) {
  180. debug(dbg_conffdetail,
  181. "tarobject found shared conffile, from pkg %s (%s); hash=%s",
  182. pkg_name(otherpkg, pnaw_always),
  183. statusinfos[otherpkg->status].name, oldhash);
  184. strcpy(oldhash, conff->hash);
  185. break;
  186. }
  187. }
  188. /* If no package was found with a valid Conffiles field, we make the
  189. * risky assumption that the hash of the current .dpkg-new file is
  190. * the one of the previously unpacked package. */
  191. if (otherpkg == NULL) {
  192. md5hash(pkg, oldhash, oldname);
  193. debug(dbg_conffdetail,
  194. "tarobject found shared conffile, from disk; hash=%s", oldhash);
  195. }
  196. }
  197. void cu_pathname(int argc, void **argv) {
  198. ensure_pathname_nonexisting((char*)(argv[0]));
  199. }
  200. int tarfileread(void *ud, char *buf, int len) {
  201. struct tarcontext *tc= (struct tarcontext*)ud;
  202. int r;
  203. r = fd_read(tc->backendpipe, buf, len);
  204. if (r < 0)
  205. ohshite(_("error reading from dpkg-deb pipe"));
  206. return r;
  207. }
  208. static void
  209. tarobject_skip_padding(struct tarcontext *tc, struct tar_entry *te)
  210. {
  211. size_t r;
  212. r = te->size % TARBLKSZ;
  213. if (r > 0)
  214. fd_skip(tc->backendpipe, TARBLKSZ - r,
  215. _("error reading from dpkg-deb pipe"));
  216. }
  217. static void
  218. tarobject_skip_entry(struct tarcontext *tc, struct tar_entry *ti)
  219. {
  220. /* We need to advance the tar file to the next object, so read the
  221. * file data and set it to oblivion. */
  222. if (ti->type == tar_filetype_file) {
  223. char fnamebuf[256];
  224. fd_skip(tc->backendpipe, ti->size,
  225. _("skipped unpacking file '%.255s' (replaced or excluded?)"),
  226. path_quote_filename(fnamebuf, ti->name, 256));
  227. tarobject_skip_padding(tc, ti);
  228. }
  229. }
  230. int fnameidlu;
  231. struct varbuf fnamevb;
  232. struct varbuf fnametmpvb;
  233. struct varbuf fnamenewvb;
  234. struct pkg_deconf_list *deconfigure = NULL;
  235. static time_t currenttime;
  236. static int
  237. does_replace(struct pkginfo *new_pkg, struct pkgbin *new_pkgbin,
  238. struct pkginfo *old_pkg, struct pkgbin *old_pkgbin)
  239. {
  240. struct dependency *dep;
  241. debug(dbg_depcon,"does_replace new=%s old=%s (%s)",
  242. pkgbin_name(new_pkg, new_pkgbin, pnaw_always),
  243. pkgbin_name(old_pkg, old_pkgbin, pnaw_always),
  244. versiondescribe(&old_pkgbin->version, vdew_always));
  245. for (dep = new_pkgbin->depends; dep; dep = dep->next) {
  246. if (dep->type != dep_replaces || dep->list->ed != old_pkg->set)
  247. continue;
  248. debug(dbg_depcondetail,"does_replace ... found old, version %s",
  249. versiondescribe(&dep->list->version,vdew_always));
  250. if (!versionsatisfied(old_pkgbin, dep->list))
  251. continue;
  252. /* The test below can only trigger if dep_replaces start having
  253. * arch qualifiers different from “any”. */
  254. if (!archsatisfied(old_pkgbin, dep->list))
  255. continue;
  256. debug(dbg_depcon,"does_replace ... yes");
  257. return true;
  258. }
  259. debug(dbg_depcon,"does_replace ... no");
  260. return false;
  261. }
  262. static void
  263. tarobject_extract(struct tarcontext *tc, struct tar_entry *te,
  264. const char *path, struct file_stat *st,
  265. struct filenamenode *namenode)
  266. {
  267. static struct varbuf hardlinkfn;
  268. static int fd;
  269. struct filenamenode *linknode;
  270. char fnamebuf[256];
  271. char *newhash;
  272. switch (te->type) {
  273. case tar_filetype_file:
  274. /* We create the file with mode 0 to make sure nobody can do anything with
  275. * it until we apply the proper mode, which might be a statoverride. */
  276. fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0);
  277. if (fd < 0)
  278. ohshite(_("unable to create `%.255s' (while processing `%.255s')"),
  279. path, te->name);
  280. push_cleanup(cu_closefd, ehflag_bombout, NULL, 0, 1, &fd);
  281. debug(dbg_eachfiledetail, "tarobject file open size=%jd",
  282. (intmax_t)te->size);
  283. newhash = nfmalloc(MD5HASHLEN + 1);
  284. fd_fd_copy_and_md5(tc->backendpipe, fd, newhash, te->size,
  285. _("backend dpkg-deb during `%.255s'"),
  286. path_quote_filename(fnamebuf, te->name, 256));
  287. namenode->newhash = newhash;
  288. debug(dbg_eachfiledetail, "tarobject file hash=%s", namenode->newhash);
  289. tarobject_skip_padding(tc, te);
  290. fd_writeback_init(fd);
  291. if (namenode->statoverride)
  292. debug(dbg_eachfile, "tarobject ... stat override, uid=%d, gid=%d, mode=%04o",
  293. namenode->statoverride->uid,
  294. namenode->statoverride->gid,
  295. namenode->statoverride->mode);
  296. if (fchown(fd, st->uid, st->gid))
  297. ohshite(_("error setting ownership of `%.255s'"), te->name);
  298. if (fchmod(fd, st->mode & ~S_IFMT))
  299. ohshite(_("error setting permissions of `%.255s'"), te->name);
  300. /* Postpone the fsync, to try to avoid massive I/O degradation. */
  301. if (!fc_unsafe_io)
  302. namenode->flags |= fnnf_deferred_fsync;
  303. pop_cleanup(ehflag_normaltidy); /* fd = open(path) */
  304. if (close(fd))
  305. ohshite(_("error closing/writing `%.255s'"), te->name);
  306. break;
  307. case tar_filetype_fifo:
  308. if (mkfifo(path, 0))
  309. ohshite(_("error creating pipe `%.255s'"), te->name);
  310. debug(dbg_eachfiledetail, "tarobject fifo");
  311. break;
  312. case tar_filetype_chardev:
  313. if (mknod(path, S_IFCHR, te->dev))
  314. ohshite(_("error creating device `%.255s'"), te->name);
  315. debug(dbg_eachfiledetail, "tarobject chardev");
  316. break;
  317. case tar_filetype_blockdev:
  318. if (mknod(path, S_IFBLK, te->dev))
  319. ohshite(_("error creating device `%.255s'"), te->name);
  320. debug(dbg_eachfiledetail, "tarobject blockdev");
  321. break;
  322. case tar_filetype_hardlink:
  323. varbuf_reset(&hardlinkfn);
  324. varbuf_add_str(&hardlinkfn, instdir);
  325. varbuf_add_char(&hardlinkfn, '/');
  326. linknode = findnamenode(te->linkname, 0);
  327. varbuf_add_str(&hardlinkfn,
  328. namenodetouse(linknode, tc->pkg, &tc->pkg->available)->name);
  329. if (linknode->flags & (fnnf_deferred_rename | fnnf_new_conff))
  330. varbuf_add_str(&hardlinkfn, DPKGNEWEXT);
  331. varbuf_end_str(&hardlinkfn);
  332. if (link(hardlinkfn.buf, path))
  333. ohshite(_("error creating hard link `%.255s'"), te->name);
  334. namenode->newhash = linknode->newhash;
  335. debug(dbg_eachfiledetail, "tarobject hardlink hash=%s", namenode->newhash);
  336. break;
  337. case tar_filetype_symlink:
  338. /* We've already checked for an existing directory. */
  339. if (symlink(te->linkname, path))
  340. ohshite(_("error creating symbolic link `%.255s'"), te->name);
  341. debug(dbg_eachfiledetail, "tarobject symlink creating");
  342. break;
  343. case tar_filetype_dir:
  344. /* We've already checked for an existing directory. */
  345. if (mkdir(path, 0))
  346. ohshite(_("error creating directory `%.255s'"), te->name);
  347. debug(dbg_eachfiledetail, "tarobject directory creating");
  348. break;
  349. default:
  350. internerr("unknown tar type '%d', but already checked", te->type);
  351. }
  352. }
  353. static void
  354. tarobject_hash(struct tarcontext *tc, struct tar_entry *te,
  355. struct filenamenode *namenode)
  356. {
  357. if (te->type == tar_filetype_file) {
  358. char fnamebuf[256];
  359. char *newhash;
  360. newhash = nfmalloc(MD5HASHLEN + 1);
  361. fd_md5(tc->backendpipe, newhash, te->size,
  362. _("backend dpkg-deb during `%.255s'"),
  363. path_quote_filename(fnamebuf, te->name, 256));
  364. tarobject_skip_padding(tc, te);
  365. namenode->newhash = newhash;
  366. debug(dbg_eachfiledetail, "tarobject file hash=%s", namenode->newhash);
  367. } else if (te->type == tar_filetype_hardlink) {
  368. struct filenamenode *linknode;
  369. linknode = findnamenode(te->linkname, 0);
  370. namenode->newhash = linknode->newhash;
  371. debug(dbg_eachfiledetail, "tarobject hardlink hash=%s", namenode->newhash);
  372. }
  373. }
  374. static void
  375. tarobject_set_mtime(struct tar_entry *te, const char *path)
  376. {
  377. struct timeval tv[2];
  378. tv[0].tv_sec = currenttime;
  379. tv[0].tv_usec = 0;
  380. tv[1].tv_sec = te->mtime;
  381. tv[1].tv_usec = 0;
  382. if (te->type == tar_filetype_symlink) {
  383. #ifdef HAVE_LUTIMES
  384. if (lutimes(path, tv) && errno != ENOSYS)
  385. ohshite(_("error setting timestamps of `%.255s'"), path);
  386. #endif
  387. } else {
  388. if (utimes(path, tv))
  389. ohshite(_("error setting timestamps of `%.255s'"), path);
  390. }
  391. }
  392. static void
  393. tarobject_set_perms(struct tar_entry *te, const char *path, struct file_stat *st)
  394. {
  395. if (te->type == tar_filetype_file)
  396. return; /* Already handled using the file descriptor. */
  397. if (te->type == tar_filetype_symlink) {
  398. if (lchown(path, st->uid, st->gid))
  399. ohshite(_("error setting ownership of symlink `%.255s'"), path);
  400. } else {
  401. if (chown(path, st->uid, st->gid))
  402. ohshite(_("error setting ownership of `%.255s'"), path);
  403. if (chmod(path, st->mode & ~S_IFMT))
  404. ohshite(_("error setting permissions of `%.255s'"), path);
  405. }
  406. }
  407. static void
  408. tarobject_set_se_context(const char *matchpath, const char *path, mode_t mode)
  409. {
  410. #ifdef WITH_SELINUX
  411. static int selinux_enabled = -1;
  412. security_context_t scontext = NULL;
  413. int ret;
  414. /* If there's no file type, just give up. */
  415. if ((mode & S_IFMT) == 0)
  416. return;
  417. /* Set selinux_enabled if it is not already set (singleton). */
  418. if (selinux_enabled < 0)
  419. selinux_enabled = (is_selinux_enabled() > 0);
  420. /* If SE Linux is not enabled just do nothing. */
  421. if (!selinux_enabled)
  422. return;
  423. /* XXX: Well, we could use set_matchpathcon_printf() to redirect the
  424. * errors from the following bit, but that seems too much effort. */
  425. /* Do nothing if we can't figure out what the context is, or if it has
  426. * no context; in which case the default context shall be applied. */
  427. ret = matchpathcon(matchpath, mode & S_IFMT, &scontext);
  428. if (ret == -1 || (ret == 0 && scontext == NULL))
  429. return;
  430. if (strcmp(scontext, "<<none>>") != 0) {
  431. if (lsetfilecon(path, scontext) < 0)
  432. /* XXX: This might need to be fatal instead!? */
  433. perror("Error setting security context for next file object:");
  434. }
  435. freecon(scontext);
  436. #endif /* WITH_SELINUX */
  437. }
  438. static void
  439. tarobject_matches(struct tarcontext *tc,
  440. const char *fn_old, struct stat *stab, char *oldhash,
  441. const char *fn_new, struct tar_entry *te,
  442. struct filenamenode *namenode)
  443. {
  444. char *linkname;
  445. ssize_t linksize;
  446. debug(dbg_eachfiledetail, "tarobject matches on-disk object?");
  447. switch (te->type) {
  448. case tar_filetype_dir:
  449. /* Nothing to check for a new directory. */
  450. return;
  451. case tar_filetype_symlink:
  452. /* Symlinks to existing dirs have already been dealt with, only
  453. * reamin real symlinks where we can compare the target. */
  454. if (!S_ISLNK(stab->st_mode))
  455. break;
  456. linkname = m_malloc(stab->st_size + 1);
  457. linksize = readlink(fn_old, linkname, stab->st_size + 1);
  458. if (linksize < 0)
  459. ohshite(_("unable to read link `%.255s'"), fn_old);
  460. else if (linksize != stab->st_size)
  461. ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
  462. fn_old, stab->st_size, linksize);
  463. linkname[linksize] = '\0';
  464. if (strcmp(linkname, te->linkname) == 0) {
  465. free(linkname);
  466. return;
  467. } else {
  468. free(linkname);
  469. }
  470. break;
  471. case tar_filetype_chardev:
  472. if (S_ISCHR(stab->st_mode) && stab->st_rdev == te->dev)
  473. return;
  474. break;
  475. case tar_filetype_blockdev:
  476. if (S_ISBLK(stab->st_mode) && stab->st_rdev == te->dev)
  477. return;
  478. break;
  479. case tar_filetype_fifo:
  480. if (S_ISFIFO(stab->st_mode))
  481. return;
  482. break;
  483. case tar_filetype_hardlink:
  484. /* Fall through. */
  485. case tar_filetype_file:
  486. /* Only check metadata for non-conffiles. */
  487. if (!(namenode->flags & fnnf_new_conff) &&
  488. !(S_ISREG(stab->st_mode) && te->size == stab->st_size))
  489. break;
  490. if (strcmp(oldhash, namenode->newhash) == 0)
  491. return;
  492. break;
  493. default:
  494. internerr("unknown tar type '%d', but already checked", te->type);
  495. }
  496. forcibleerr(fc_overwrite,
  497. _("trying to overwrite shared '%.250s', which is different "
  498. "from other instances of package %.250s"),
  499. namenode->name, pkg_name(tc->pkg, pnaw_nonambig));
  500. }
  501. void setupfnamevbs(const char *filename) {
  502. varbuf_trunc(&fnamevb, fnameidlu);
  503. varbuf_add_str(&fnamevb, filename);
  504. varbuf_end_str(&fnamevb);
  505. varbuf_trunc(&fnametmpvb, fnameidlu);
  506. varbuf_add_str(&fnametmpvb, filename);
  507. varbuf_add_str(&fnametmpvb, DPKGTEMPEXT);
  508. varbuf_end_str(&fnametmpvb);
  509. varbuf_trunc(&fnamenewvb, fnameidlu);
  510. varbuf_add_str(&fnamenewvb, filename);
  511. varbuf_add_str(&fnamenewvb, DPKGNEWEXT);
  512. varbuf_end_str(&fnamenewvb);
  513. debug(dbg_eachfiledetail, "setupvnamevbs main='%s' tmp='%s' new='%s'",
  514. fnamevb.buf, fnametmpvb.buf, fnamenewvb.buf);
  515. }
  516. /**
  517. * Securely remove a pathname.
  518. *
  519. * This is a secure version of remove(3) using secure_unlink() instead of
  520. * unlink(2).
  521. *
  522. * @retval 0 On success.
  523. * @retval -1 On failure, just like unlink(2) & rmdir(2).
  524. */
  525. int
  526. secure_remove(const char *filename)
  527. {
  528. int r, e;
  529. if (!rmdir(filename)) {
  530. debug(dbg_eachfiledetail, "secure_remove '%s' rmdir OK", filename);
  531. return 0;
  532. }
  533. if (errno != ENOTDIR) {
  534. e= errno;
  535. debug(dbg_eachfiledetail, "secure_remove '%s' rmdir %s", filename,
  536. strerror(e));
  537. errno= e; return -1;
  538. }
  539. r = secure_unlink(filename);
  540. e = errno;
  541. debug(dbg_eachfiledetail, "secure_remove '%s' unlink %s",
  542. filename, r ? strerror(e) : "OK");
  543. errno= e; return r;
  544. }
  545. struct fileinlist *addfiletolist(struct tarcontext *tc,
  546. struct filenamenode *namenode) {
  547. struct fileinlist *nifd;
  548. nifd= obstack_alloc(&tar_obs, sizeof(struct fileinlist));
  549. nifd->namenode= namenode;
  550. nifd->next = NULL;
  551. *tc->newfilesp = nifd;
  552. tc->newfilesp = &nifd->next;
  553. return nifd;
  554. }
  555. static void
  556. remove_file_from_list(struct tarcontext *tc, struct tar_entry *ti,
  557. struct fileinlist **oldnifd,
  558. struct fileinlist *nifd)
  559. {
  560. obstack_free(&tar_obs, nifd);
  561. tc->newfilesp = oldnifd;
  562. *oldnifd = NULL;
  563. }
  564. static bool
  565. linktosameexistingdir(const struct tar_entry *ti, const char *fname,
  566. struct varbuf *symlinkfn)
  567. {
  568. struct stat oldstab, newstab;
  569. int statr;
  570. const char *lastslash;
  571. statr= stat(fname, &oldstab);
  572. if (statr) {
  573. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  574. ohshite(_("failed to stat (dereference) existing symlink `%.250s'"),
  575. fname);
  576. return false;
  577. }
  578. if (!S_ISDIR(oldstab.st_mode))
  579. return false;
  580. /* But is it to the same dir? */
  581. varbuf_reset(symlinkfn);
  582. if (ti->linkname[0] == '/') {
  583. varbuf_add_str(symlinkfn, instdir);
  584. } else {
  585. lastslash= strrchr(fname, '/');
  586. assert(lastslash);
  587. varbuf_add_buf(symlinkfn, fname, (lastslash - fname) + 1);
  588. }
  589. varbuf_add_str(symlinkfn, ti->linkname);
  590. varbuf_end_str(symlinkfn);
  591. statr= stat(symlinkfn->buf, &newstab);
  592. if (statr) {
  593. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  594. ohshite(_("failed to stat (dereference) proposed new symlink target"
  595. " `%.250s' for symlink `%.250s'"), symlinkfn->buf, fname);
  596. return false;
  597. }
  598. if (!S_ISDIR(newstab.st_mode))
  599. return false;
  600. if (newstab.st_dev != oldstab.st_dev ||
  601. newstab.st_ino != oldstab.st_ino)
  602. return false;
  603. return true;
  604. }
  605. int
  606. tarobject(void *ctx, struct tar_entry *ti)
  607. {
  608. static struct varbuf conffderefn, symlinkfn;
  609. const char *usename;
  610. struct filenamenode *usenode;
  611. struct conffile *conff;
  612. struct tarcontext *tc = ctx;
  613. bool existingdir, keepexisting;
  614. bool refcounting;
  615. char oldhash[MD5HASHLEN + 1];
  616. int statr;
  617. ssize_t r;
  618. struct stat stab, stabtmp;
  619. struct file_stat *st;
  620. struct fileinlist *nifd, **oldnifd;
  621. struct pkgset *divpkgset;
  622. struct pkginfo *otherpkg;
  623. ensureobstackinit();
  624. /* Append to list of files.
  625. * The trailing ‘/’ put on the end of names in tarfiles has already
  626. * been stripped by tar_extractor(). */
  627. oldnifd= tc->newfilesp;
  628. nifd= addfiletolist(tc, findnamenode(ti->name, 0));
  629. nifd->namenode->flags |= fnnf_new_inarchive;
  630. debug(dbg_eachfile,
  631. "tarobject ti->name='%s' mode=%lo owner=%u.%u type=%d(%c)"
  632. " ti->linkname='%s' namenode='%s' flags=%o instead='%s'",
  633. ti->name, (long)ti->stat.mode,
  634. (unsigned)ti->stat.uid, (unsigned)ti->stat.gid,
  635. ti->type,
  636. ti->type >= '0' && ti->type <= '6' ? "-hlcbdp"[ti->type - '0'] : '?',
  637. ti->linkname,
  638. nifd->namenode->name, nifd->namenode->flags,
  639. nifd->namenode->divert && nifd->namenode->divert->useinstead
  640. ? nifd->namenode->divert->useinstead->name : "<none>");
  641. if (nifd->namenode->divert && nifd->namenode->divert->camefrom) {
  642. divpkgset = nifd->namenode->divert->pkgset;
  643. if (divpkgset) {
  644. forcibleerr(fc_overwritediverted,
  645. _("trying to overwrite `%.250s', which is the "
  646. "diverted version of `%.250s' (package: %.100s)"),
  647. nifd->namenode->name, nifd->namenode->divert->camefrom->name,
  648. divpkgset->name);
  649. } else {
  650. forcibleerr(fc_overwritediverted,
  651. _("trying to overwrite `%.250s', which is the "
  652. "diverted version of `%.250s'"),
  653. nifd->namenode->name, nifd->namenode->divert->camefrom->name);
  654. }
  655. }
  656. if (nifd->namenode->statoverride)
  657. st = nifd->namenode->statoverride;
  658. else
  659. st = &ti->stat;
  660. usenode = namenodetouse(nifd->namenode, tc->pkg, &tc->pkg->available);
  661. usename = usenode->name + 1; /* Skip the leading '/'. */
  662. trig_file_activate(usenode, tc->pkg);
  663. if (nifd->namenode->flags & fnnf_new_conff) {
  664. /* If it's a conffile we have to extract it next to the installed
  665. * version (i.e. we do the usual link-following). */
  666. if (conffderef(tc->pkg, &conffderefn, usename))
  667. usename= conffderefn.buf;
  668. debug(dbg_conff, "tarobject fnnf_new_conff deref='%s'", usename);
  669. }
  670. setupfnamevbs(usename);
  671. statr= lstat(fnamevb.buf,&stab);
  672. if (statr) {
  673. /* The lstat failed. */
  674. if (errno != ENOENT && errno != ENOTDIR)
  675. ohshite(_("unable to stat `%.255s' (which I was about to install)"),
  676. ti->name);
  677. /* OK, so it doesn't exist.
  678. * However, it's possible that we were in the middle of some other
  679. * backup/restore operation and were rudely interrupted.
  680. * So, we see if we have .dpkg-tmp, and if so we restore it. */
  681. if (rename(fnametmpvb.buf,fnamevb.buf)) {
  682. if (errno != ENOENT && errno != ENOTDIR)
  683. ohshite(_("unable to clean up mess surrounding `%.255s' before "
  684. "installing another version"), ti->name);
  685. debug(dbg_eachfiledetail,"tarobject nonexistent");
  686. } else {
  687. debug(dbg_eachfiledetail,"tarobject restored tmp to main");
  688. statr= lstat(fnamevb.buf,&stab);
  689. if (statr) ohshite(_("unable to stat restored `%.255s' before installing"
  690. " another version"), ti->name);
  691. }
  692. } else {
  693. debug(dbg_eachfiledetail,"tarobject already exists");
  694. }
  695. /* Check to see if it's a directory or link to one and we don't need to
  696. * do anything. This has to be done now so that we don't die due to
  697. * a file overwriting conflict. */
  698. existingdir = false;
  699. switch (ti->type) {
  700. case tar_filetype_symlink:
  701. /* If it's already an existing directory, do nothing. */
  702. if (!statr && S_ISDIR(stab.st_mode)) {
  703. debug(dbg_eachfiledetail, "tarobject symlink exists as directory");
  704. existingdir = true;
  705. } else if (!statr && S_ISLNK(stab.st_mode)) {
  706. if (linktosameexistingdir(ti, fnamevb.buf, &symlinkfn))
  707. existingdir = true;
  708. }
  709. break;
  710. case tar_filetype_dir:
  711. /* If it's already an existing directory, do nothing. */
  712. if (!stat(fnamevb.buf,&stabtmp) && S_ISDIR(stabtmp.st_mode)) {
  713. debug(dbg_eachfiledetail, "tarobject directory exists");
  714. existingdir = true;
  715. }
  716. break;
  717. case tar_filetype_file:
  718. case tar_filetype_chardev:
  719. case tar_filetype_blockdev:
  720. case tar_filetype_fifo:
  721. case tar_filetype_hardlink:
  722. break;
  723. default:
  724. ohshit(_("archive contained object `%.255s' of unknown type 0x%x"),
  725. ti->name, ti->type);
  726. }
  727. keepexisting = false;
  728. refcounting = false;
  729. if (!existingdir) {
  730. struct filepackages_iterator *iter;
  731. iter = filepackages_iter_new(nifd->namenode);
  732. while ((otherpkg = filepackages_iter_next(iter))) {
  733. if (otherpkg == tc->pkg)
  734. continue;
  735. debug(dbg_eachfile, "tarobject ... found in %s",
  736. pkg_name(otherpkg, pnaw_always));
  737. /* A pkgset can share files between its instances. Overwriting
  738. * is allowed when they are not getting in sync, otherwise the
  739. * file content must match the installed file. */
  740. if (otherpkg->set == tc->pkg->set &&
  741. otherpkg->installed.multiarch == multiarch_same &&
  742. tc->pkg->available.multiarch == multiarch_same) {
  743. if (statr == 0 && tc->pkgset_getting_in_sync)
  744. refcounting = true;
  745. debug(dbg_eachfiledetail, "tarobject ... shared with %s %s (syncing=%d)",
  746. pkg_name(otherpkg, pnaw_always),
  747. versiondescribe(&otherpkg->installed.version, vdew_nonambig),
  748. tc->pkgset_getting_in_sync);
  749. continue;
  750. }
  751. if (nifd->namenode->divert && nifd->namenode->divert->useinstead) {
  752. /* Right, so we may be diverting this file. This makes the conflict
  753. * OK iff one of us is the diverting package (we don't need to
  754. * check for both being the diverting package, obviously). */
  755. divpkgset = nifd->namenode->divert->pkgset;
  756. debug(dbg_eachfile, "tarobject ... diverted, divpkgset=%s",
  757. divpkgset ? divpkgset->name : "<none>");
  758. if (otherpkg->set == divpkgset || tc->pkg->set == divpkgset)
  759. continue;
  760. }
  761. /* If the new object is a directory and the previous object does
  762. * not exist assume it's also a directory and skip further checks.
  763. * XXX: Ideally with more information about the installed files we
  764. * could perform more clever checks. */
  765. if (statr != 0 && ti->type == tar_filetype_dir) {
  766. debug(dbg_eachfile, "tarobject ... assuming shared directory");
  767. continue;
  768. }
  769. /* Nope? Hmm, file conflict, perhaps. Check Replaces. */
  770. switch (otherpkg->clientdata->replacingfilesandsaid) {
  771. case 2:
  772. keepexisting = true;
  773. case 1:
  774. continue;
  775. }
  776. /* Is the package with the conflicting file in the “config files only”
  777. * state? If so it must be a config file and we can silenty take it
  778. * over. */
  779. if (otherpkg->status == stat_configfiles)
  780. continue;
  781. /* Perhaps we're removing a conflicting package? */
  782. if (otherpkg->clientdata->istobe == itb_remove)
  783. continue;
  784. /* Is the file an obsolete conffile in the other package
  785. * and a conffile in the new package? */
  786. if ((nifd->namenode->flags & fnnf_new_conff) &&
  787. !statr && S_ISREG(stab.st_mode)) {
  788. for (conff = otherpkg->installed.conffiles;
  789. conff;
  790. conff = conff->next) {
  791. if (!conff->obsolete)
  792. continue;
  793. if (stat(conff->name, &stabtmp)) {
  794. if (errno == ENOENT || errno == ENOTDIR || errno == ELOOP)
  795. continue;
  796. else
  797. ohshite(_("cannot stat file '%s'"), conff->name);
  798. }
  799. if (stabtmp.st_dev == stab.st_dev &&
  800. stabtmp.st_ino == stab.st_ino)
  801. break;
  802. }
  803. if (conff) {
  804. debug(dbg_eachfiledetail, "tarobject other's obsolete conffile");
  805. /* process_archive() will have copied its hash already. */
  806. continue;
  807. }
  808. }
  809. if (does_replace(tc->pkg, &tc->pkg->available,
  810. otherpkg, &otherpkg->installed)) {
  811. printf(_("Replacing files in old package %s ...\n"),
  812. pkg_name(otherpkg, pnaw_nonambig));
  813. otherpkg->clientdata->replacingfilesandsaid = 1;
  814. } else if (does_replace(otherpkg, &otherpkg->installed,
  815. tc->pkg, &tc->pkg->available)) {
  816. printf(_("Replaced by files in installed package %s ...\n"),
  817. pkg_name(otherpkg, pnaw_nonambig));
  818. otherpkg->clientdata->replacingfilesandsaid = 2;
  819. nifd->namenode->flags &= ~fnnf_new_inarchive;
  820. keepexisting = true;
  821. } else {
  822. /* At this point we are replacing something without a Replaces. */
  823. if (!statr && S_ISDIR(stab.st_mode)) {
  824. forcibleerr(fc_overwritedir,
  825. _("trying to overwrite directory '%.250s' "
  826. "in package %.250s %.250s with nondirectory"),
  827. nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
  828. versiondescribe(&otherpkg->installed.version,
  829. vdew_nonambig));
  830. } else {
  831. forcibleerr(fc_overwrite,
  832. _("trying to overwrite '%.250s', "
  833. "which is also in package %.250s %.250s"),
  834. nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
  835. versiondescribe(&otherpkg->installed.version,
  836. vdew_nonambig));
  837. }
  838. }
  839. }
  840. filepackages_iter_free(iter);
  841. }
  842. if (keepexisting) {
  843. remove_file_from_list(tc, ti, oldnifd, nifd);
  844. tarobject_skip_entry(tc, ti);
  845. return 0;
  846. }
  847. if (filter_should_skip(ti)) {
  848. nifd->namenode->flags &= ~fnnf_new_inarchive;
  849. nifd->namenode->flags |= fnnf_filtered;
  850. tarobject_skip_entry(tc, ti);
  851. return 0;
  852. }
  853. if (existingdir)
  854. return 0;
  855. /* Compute the hash of the previous object, before we might replace it
  856. * with the new version on forced overwrites. */
  857. if (refcounting) {
  858. if (nifd->namenode->flags & fnnf_new_conff) {
  859. md5hash_prev_conffile(tc->pkg, oldhash, fnamenewvb.buf, nifd->namenode);
  860. } else if (S_ISREG(stab.st_mode)) {
  861. md5hash(tc->pkg, oldhash, fnamevb.buf);
  862. } else {
  863. strcpy(oldhash, EMPTYHASHFLAG);
  864. }
  865. }
  866. if (refcounting && !fc_overwrite) {
  867. /* If we are not forced to overwrite the path and are refcounting,
  868. * just compute the hash w/o extracting the object. */
  869. tarobject_hash(tc, ti, nifd->namenode);
  870. } else {
  871. /* Now, at this stage we want to make sure neither of .dpkg-new and
  872. * .dpkg-tmp are hanging around. */
  873. ensure_pathname_nonexisting(fnamenewvb.buf);
  874. ensure_pathname_nonexisting(fnametmpvb.buf);
  875. /* Now we start to do things that we need to be able to undo
  876. * if something goes wrong. Watch out for the CLEANUP comments to
  877. * keep an eye on what's installed on the disk at each point. */
  878. push_cleanup(cu_installnew, ~ehflag_normaltidy, NULL, 0, 1, nifd->namenode);
  879. /*
  880. * CLEANUP: Now we either have the old file on the disk, or not, in
  881. * its original filename.
  882. */
  883. /* Extract whatever it is as .dpkg-new ... */
  884. tarobject_extract(tc, ti, fnamenewvb.buf, st, nifd->namenode);
  885. }
  886. /* For shared files, check now if the object matches. */
  887. if (refcounting)
  888. tarobject_matches(tc, fnamevb.buf, &stab, oldhash,
  889. fnamenewvb.buf, ti, nifd->namenode);
  890. /* If we didn't extract anything, there's nothing else to do. */
  891. if (refcounting && !fc_overwrite)
  892. return 0;
  893. tarobject_set_perms(ti, fnamenewvb.buf, st);
  894. tarobject_set_mtime(ti, fnamenewvb.buf);
  895. tarobject_set_se_context(fnamevb.buf, fnamenewvb.buf, st->mode);
  896. /*
  897. * CLEANUP: Now we have extracted the new object in .dpkg-new (or,
  898. * if the file already exists as a directory and we were trying to
  899. * extract a directory or symlink, we returned earlier, so we don't
  900. * need to worry about that here).
  901. *
  902. * The old file is still in the original filename,
  903. */
  904. /* First, check to see if it's a conffile. If so we don't install
  905. * it now - we leave it in .dpkg-new for --configure to take care of. */
  906. if (nifd->namenode->flags & fnnf_new_conff) {
  907. debug(dbg_conffdetail,"tarobject conffile extracted");
  908. nifd->namenode->flags |= fnnf_elide_other_lists;
  909. return 0;
  910. }
  911. /* Now we move the old file out of the way, the backup file will
  912. * be deleted later. */
  913. if (statr) {
  914. /* Don't try to back it up if it didn't exist. */
  915. debug(dbg_eachfiledetail,"tarobject new - no backup");
  916. } else {
  917. if (ti->type == tar_filetype_dir || S_ISDIR(stab.st_mode)) {
  918. /* One of the two is a directory - can't do atomic install. */
  919. debug(dbg_eachfiledetail,"tarobject directory, nonatomic");
  920. nifd->namenode->flags |= fnnf_no_atomic_overwrite;
  921. if (rename(fnamevb.buf,fnametmpvb.buf))
  922. ohshite(_("unable to move aside `%.255s' to install new version"),
  923. ti->name);
  924. } else if (S_ISLNK(stab.st_mode)) {
  925. /* We can't make a symlink with two hardlinks, so we'll have to
  926. * copy it. (Pretend that making a copy of a symlink is the same
  927. * as linking to it.) */
  928. varbuf_reset(&symlinkfn);
  929. varbuf_grow(&symlinkfn, stab.st_size + 1);
  930. r = readlink(fnamevb.buf, symlinkfn.buf, symlinkfn.size);
  931. if (r < 0)
  932. ohshite(_("unable to read link `%.255s'"), ti->name);
  933. else if (r != stab.st_size)
  934. ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
  935. fnamevb.buf, stab.st_size, r);
  936. varbuf_trunc(&symlinkfn, r);
  937. varbuf_end_str(&symlinkfn);
  938. if (symlink(symlinkfn.buf,fnametmpvb.buf))
  939. ohshite(_("unable to make backup symlink for `%.255s'"), ti->name);
  940. if (lchown(fnametmpvb.buf,stab.st_uid,stab.st_gid))
  941. ohshite(_("unable to chown backup symlink for `%.255s'"), ti->name);
  942. tarobject_set_se_context(fnamevb.buf, fnametmpvb.buf, stab.st_mode);
  943. } else {
  944. debug(dbg_eachfiledetail, "tarobject nondirectory, 'link' backup");
  945. if (link(fnamevb.buf,fnametmpvb.buf))
  946. ohshite(_("unable to make backup link of `%.255s' before installing new version"),
  947. ti->name);
  948. }
  949. }
  950. /*
  951. * CLEANUP: Now the old file is in .dpkg-tmp, and the new file is still
  952. * in .dpkg-new.
  953. */
  954. if (ti->type == tar_filetype_file || ti->type == tar_filetype_hardlink ||
  955. ti->type == tar_filetype_symlink) {
  956. nifd->namenode->flags |= fnnf_deferred_rename;
  957. debug(dbg_eachfiledetail, "tarobject done and installation deferred");
  958. } else {
  959. if (rename(fnamenewvb.buf, fnamevb.buf))
  960. ohshite(_("unable to install new version of `%.255s'"), ti->name);
  961. /*
  962. * CLEANUP: Now the new file is in the destination file, and the
  963. * old file is in .dpkg-tmp to be cleaned up later. We now need
  964. * to take a different attitude to cleanup, because we need to
  965. * remove the new file.
  966. */
  967. nifd->namenode->flags |= fnnf_placed_on_disk;
  968. nifd->namenode->flags |= fnnf_elide_other_lists;
  969. debug(dbg_eachfiledetail, "tarobject done and installed");
  970. }
  971. return 0;
  972. }
  973. #if defined(SYNC_FILE_RANGE_WAIT_BEFORE)
  974. static void
  975. tar_writeback_barrier(struct fileinlist *files, struct pkginfo *pkg)
  976. {
  977. struct fileinlist *cfile;
  978. for (cfile = files; cfile; cfile = cfile->next) {
  979. struct filenamenode *usenode;
  980. const char *usename;
  981. int fd;
  982. if (!(cfile->namenode->flags & fnnf_deferred_fsync))
  983. continue;
  984. usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
  985. usename = usenode->name + 1; /* Skip the leading '/'. */
  986. setupfnamevbs(usename);
  987. fd = open(fnamenewvb.buf, O_WRONLY);
  988. if (fd < 0)
  989. ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
  990. /* Ignore the return code as it should be considered equivalent to an
  991. * asynchronous hint for the kernel, we are doing an fsync() later on
  992. * anyway. */
  993. sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WAIT_BEFORE);
  994. if (close(fd))
  995. ohshite(_("error closing/writing `%.255s'"), fnamenewvb.buf);
  996. }
  997. }
  998. #else
  999. static void
  1000. tar_writeback_barrier(struct fileinlist *files, struct pkginfo *pkg)
  1001. {
  1002. }
  1003. #endif
  1004. void
  1005. tar_deferred_extract(struct fileinlist *files, struct pkginfo *pkg)
  1006. {
  1007. struct fileinlist *cfile;
  1008. struct filenamenode *usenode;
  1009. const char *usename;
  1010. tar_writeback_barrier(files, pkg);
  1011. for (cfile = files; cfile; cfile = cfile->next) {
  1012. debug(dbg_eachfile, "deferred extract of '%.255s'", cfile->namenode->name);
  1013. if (!(cfile->namenode->flags & fnnf_deferred_rename))
  1014. continue;
  1015. usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
  1016. usename = usenode->name + 1; /* Skip the leading '/'. */
  1017. setupfnamevbs(usename);
  1018. if (cfile->namenode->flags & fnnf_deferred_fsync) {
  1019. int fd;
  1020. debug(dbg_eachfiledetail, "deferred extract needs fsync");
  1021. fd = open(fnamenewvb.buf, O_WRONLY);
  1022. if (fd < 0)
  1023. ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
  1024. if (fsync(fd))
  1025. ohshite(_("unable to sync file '%.255s'"), fnamenewvb.buf);
  1026. if (close(fd))
  1027. ohshite(_("error closing/writing `%.255s'"), fnamenewvb.buf);
  1028. cfile->namenode->flags &= ~fnnf_deferred_fsync;
  1029. }
  1030. debug(dbg_eachfiledetail, "deferred extract needs rename");
  1031. if (rename(fnamenewvb.buf, fnamevb.buf))
  1032. ohshite(_("unable to install new version of `%.255s'"),
  1033. cfile->namenode->name);
  1034. cfile->namenode->flags &= ~fnnf_deferred_rename;
  1035. /*
  1036. * CLEANUP: Now the new file is in the destination file, and the
  1037. * old file is in .dpkg-tmp to be cleaned up later. We now need
  1038. * to take a different attitude to cleanup, because we need to
  1039. * remove the new file.
  1040. */
  1041. cfile->namenode->flags |= fnnf_placed_on_disk;
  1042. cfile->namenode->flags |= fnnf_elide_other_lists;
  1043. debug(dbg_eachfiledetail, "deferred extract done and installed");
  1044. }
  1045. }
  1046. void
  1047. enqueue_deconfigure(struct pkginfo *pkg, struct pkginfo *pkg_removal)
  1048. {
  1049. struct pkg_deconf_list *newdeconf;
  1050. ensure_package_clientdata(pkg);
  1051. pkg->clientdata->istobe = itb_deconfigure;
  1052. newdeconf = m_malloc(sizeof(struct pkg_deconf_list));
  1053. newdeconf->next = deconfigure;
  1054. newdeconf->pkg = pkg;
  1055. newdeconf->pkg_removal = pkg_removal;
  1056. deconfigure = newdeconf;
  1057. }
  1058. void
  1059. clear_deconfigure_queue(void)
  1060. {
  1061. struct pkg_deconf_list *deconf, *deconf_next;
  1062. for (deconf = deconfigure; deconf; deconf = deconf_next) {
  1063. deconf_next = deconf->next;
  1064. free(deconf);
  1065. }
  1066. deconfigure = NULL;
  1067. }
  1068. /**
  1069. * Try if we can deconfigure the package and queue it if so.
  1070. *
  1071. * Also checks whether the pdep is forced, first, according to force_p.
  1072. * force_p may be NULL in which case nothing is considered forced.
  1073. *
  1074. * Action is a string describing the action which causes the
  1075. * deconfiguration:
  1076. *
  1077. * "removal of <package>" (due to Conflicts+Depends; removal != NULL)
  1078. * "installation of <package>" (due to Breaks; removal == NULL)
  1079. *
  1080. * @retval 0 Not possible (why is printed).
  1081. * @retval 1 Deconfiguration queued ok (no message printed).
  1082. * @retval 2 Forced (no deconfiguration needed, why is printed).
  1083. */
  1084. static int
  1085. try_deconfigure_can(bool (*force_p)(struct deppossi *), struct pkginfo *pkg,
  1086. struct deppossi *pdep, const char *action,
  1087. struct pkginfo *removal, const char *why)
  1088. {
  1089. if (force_p && force_p(pdep)) {
  1090. warning(_("ignoring dependency problem with %s:\n%s"), action, why);
  1091. return 2;
  1092. } else if (f_autodeconf) {
  1093. if (pkg->installed.essential) {
  1094. if (fc_removeessential) {
  1095. warning(_("considering deconfiguration of essential\n"
  1096. " package %s, to enable %s."),
  1097. pkg_name(pkg, pnaw_nonambig), action);
  1098. } else {
  1099. fprintf(stderr, _("dpkg: no, %s is essential, will not deconfigure\n"
  1100. " it in order to enable %s.\n"),
  1101. pkg_name(pkg, pnaw_nonambig), action);
  1102. return 0;
  1103. }
  1104. }
  1105. enqueue_deconfigure(pkg, removal);
  1106. return 1;
  1107. } else {
  1108. fprintf(stderr, _("dpkg: no, cannot proceed with %s (--auto-deconfigure will help):\n%s"),
  1109. action, why);
  1110. return 0;
  1111. }
  1112. }
  1113. static int try_remove_can(struct deppossi *pdep,
  1114. struct pkginfo *fixbyrm,
  1115. const char *why) {
  1116. char action[512];
  1117. sprintf(action, _("removal of %.250s"), pkg_name(fixbyrm, pnaw_nonambig));
  1118. return try_deconfigure_can(force_depends, pdep->up->up, pdep,
  1119. action, fixbyrm, why);
  1120. }
  1121. void check_breaks(struct dependency *dep, struct pkginfo *pkg,
  1122. const char *pfilename) {
  1123. struct pkginfo *fixbydeconf;
  1124. struct varbuf why = VARBUF_INIT;
  1125. int ok;
  1126. fixbydeconf = NULL;
  1127. if (depisok(dep, &why, &fixbydeconf, NULL, false)) {
  1128. varbuf_destroy(&why);
  1129. return;
  1130. }
  1131. varbuf_end_str(&why);
  1132. if (fixbydeconf && f_autodeconf) {
  1133. char action[512];
  1134. ensure_package_clientdata(fixbydeconf);
  1135. assert(fixbydeconf->clientdata->istobe == itb_normal);
  1136. sprintf(action, _("installation of %.250s"),
  1137. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1138. fprintf(stderr, _("dpkg: considering deconfiguration of %s,"
  1139. " which would be broken by %s ...\n"),
  1140. pkg_name(fixbydeconf, pnaw_nonambig), action);
  1141. ok= try_deconfigure_can(force_breaks, fixbydeconf, dep->list,
  1142. action, NULL, why.buf);
  1143. if (ok == 1) {
  1144. fprintf(stderr, _("dpkg: yes, will deconfigure %s (broken by %s).\n"),
  1145. pkg_name(fixbydeconf, pnaw_nonambig),
  1146. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1147. }
  1148. } else {
  1149. fprintf(stderr, _("dpkg: regarding %s containing %s:\n%s"),
  1150. pfilename, pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  1151. why.buf);
  1152. ok= 0;
  1153. }
  1154. varbuf_destroy(&why);
  1155. if (ok > 0) return;
  1156. if (force_breaks(dep->list)) {
  1157. warning(_("ignoring breakage, may proceed anyway!"));
  1158. return;
  1159. }
  1160. if (fixbydeconf && !f_autodeconf) {
  1161. ohshit(_("installing %.250s would break %.250s, and\n"
  1162. " deconfiguration is not permitted (--auto-deconfigure might help)"),
  1163. pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  1164. pkg_name(fixbydeconf, pnaw_nonambig));
  1165. } else {
  1166. ohshit(_("installing %.250s would break existing software"),
  1167. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1168. }
  1169. }
  1170. void check_conflict(struct dependency *dep, struct pkginfo *pkg,
  1171. const char *pfilename) {
  1172. struct pkginfo *fixbyrm;
  1173. struct deppossi *pdep, flagdeppossi;
  1174. struct varbuf conflictwhy = VARBUF_INIT, removalwhy = VARBUF_INIT;
  1175. struct dependency *providecheck;
  1176. fixbyrm = NULL;
  1177. if (depisok(dep, &conflictwhy, &fixbyrm, NULL, false)) {
  1178. varbuf_destroy(&conflictwhy);
  1179. varbuf_destroy(&removalwhy);
  1180. return;
  1181. }
  1182. if (fixbyrm) {
  1183. ensure_package_clientdata(fixbyrm);
  1184. if (fixbyrm->clientdata->istobe == itb_installnew) {
  1185. fixbyrm= dep->up;
  1186. ensure_package_clientdata(fixbyrm);
  1187. }
  1188. if (((pkg->available.essential && fixbyrm->installed.essential) ||
  1189. (((fixbyrm->want != want_install && fixbyrm->want != want_hold) ||
  1190. does_replace(pkg, &pkg->available, fixbyrm, &fixbyrm->installed)) &&
  1191. (!fixbyrm->installed.essential || fc_removeessential)))) {
  1192. assert(fixbyrm->clientdata->istobe == itb_normal || fixbyrm->clientdata->istobe == itb_deconfigure);
  1193. fixbyrm->clientdata->istobe= itb_remove;
  1194. fprintf(stderr, _("dpkg: considering removing %s in favour of %s ...\n"),
  1195. pkg_name(fixbyrm, pnaw_nonambig),
  1196. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1197. if (!(fixbyrm->status == stat_installed ||
  1198. fixbyrm->status == stat_triggerspending ||
  1199. fixbyrm->status == stat_triggersawaited)) {
  1200. fprintf(stderr,
  1201. _("%s is not properly installed - ignoring any dependencies on it.\n"),
  1202. pkg_name(fixbyrm, pnaw_nonambig));
  1203. pdep = NULL;
  1204. } else {
  1205. for (pdep = fixbyrm->set->depended.installed;
  1206. pdep;
  1207. pdep = pdep->rev_next) {
  1208. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  1209. continue;
  1210. if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
  1211. continue;
  1212. varbuf_end_str(&removalwhy);
  1213. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  1214. break;
  1215. }
  1216. if (!pdep) {
  1217. /* If we haven't found a reason not to yet, let's look some more. */
  1218. for (providecheck= fixbyrm->installed.depends;
  1219. providecheck;
  1220. providecheck= providecheck->next) {
  1221. if (providecheck->type != dep_provides) continue;
  1222. for (pdep = providecheck->list->ed->depended.installed;
  1223. pdep;
  1224. pdep = pdep->rev_next) {
  1225. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  1226. continue;
  1227. if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
  1228. continue;
  1229. varbuf_end_str(&removalwhy);
  1230. fprintf(stderr, _("dpkg"
  1231. ": may have trouble removing %s, as it provides %s ...\n"),
  1232. pkg_name(fixbyrm, pnaw_nonambig),
  1233. providecheck->list->ed->name);
  1234. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  1235. goto break_from_both_loops_at_once;
  1236. }
  1237. }
  1238. break_from_both_loops_at_once:;
  1239. }
  1240. }
  1241. if (!pdep && skip_due_to_hold(fixbyrm)) {
  1242. pdep= &flagdeppossi;
  1243. }
  1244. if (!pdep && (fixbyrm->eflag & eflag_reinstreq)) {
  1245. if (fc_removereinstreq) {
  1246. fprintf(stderr, _("dpkg: package %s requires reinstallation, but will"
  1247. " remove anyway as you requested.\n"),
  1248. pkg_name(fixbyrm, pnaw_nonambig));
  1249. } else {
  1250. fprintf(stderr, _("dpkg: package %s requires reinstallation, "
  1251. "will not remove.\n"), pkg_name(fixbyrm, pnaw_nonambig));
  1252. pdep= &flagdeppossi;
  1253. }
  1254. }
  1255. if (!pdep) {
  1256. /* This conflict is OK - we'll remove the conflictor. */
  1257. enqueue_conflictor(pkg, fixbyrm);
  1258. varbuf_destroy(&conflictwhy); varbuf_destroy(&removalwhy);
  1259. fprintf(stderr, _("dpkg: yes, will remove %s in favour of %s.\n"),
  1260. pkg_name(fixbyrm, pnaw_nonambig),
  1261. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1262. return;
  1263. }
  1264. /* Put it back. */
  1265. fixbyrm->clientdata->istobe = itb_normal;
  1266. }
  1267. }
  1268. varbuf_end_str(&conflictwhy);
  1269. fprintf(stderr, _("dpkg: regarding %s containing %s:\n%s"),
  1270. pfilename, pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  1271. conflictwhy.buf);
  1272. if (!force_conflicts(dep->list))
  1273. ohshit(_("conflicting packages - not installing %.250s"),
  1274. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1275. warning(_("ignoring conflict, may proceed anyway!"));
  1276. varbuf_destroy(&conflictwhy);
  1277. return;
  1278. }
  1279. void cu_cidir(int argc, void **argv) {
  1280. char *cidir= (char*)argv[0];
  1281. char *cidirrest= (char*)argv[1];
  1282. cidirrest[-1] = '\0';
  1283. ensure_pathname_nonexisting(cidir);
  1284. }
  1285. void cu_fileslist(int argc, void **argv) {
  1286. destroyobstack();
  1287. }
  1288. int
  1289. archivefiles(const char *const *argv)
  1290. {
  1291. const char *volatile thisarg;
  1292. const char *const *volatile argp;
  1293. jmp_buf ejbuf;
  1294. trigproc_install_hooks();
  1295. modstatdb_open(f_noact ? msdbrw_readonly :
  1296. (cipaction->arg_int == act_avail ? msdbrw_readonly :
  1297. fc_nonroot ? msdbrw_write :
  1298. msdbrw_needsuperuser) |
  1299. msdbrw_available_write);
  1300. checkpath();
  1301. pkg_infodb_upgrade();
  1302. log_message("startup archives %s", cipaction->olong);
  1303. if (f_recursive) {
  1304. int pi[2], nfiles, c, i, r;
  1305. pid_t pid;
  1306. FILE *pf;
  1307. static struct varbuf findoutput;
  1308. const char **arglist;
  1309. char *p;
  1310. if (!*argv)
  1311. badusage(_("--%s --recursive needs at least one path argument"),cipaction->olong);
  1312. m_pipe(pi);
  1313. pid = subproc_fork();
  1314. if (pid == 0) {
  1315. struct command cmd;
  1316. const char *const *ap;
  1317. m_dup2(pi[1],1); close(pi[0]); close(pi[1]);
  1318. command_init(&cmd, FIND, _("find for dpkg --recursive"));
  1319. command_add_args(&cmd, FIND, "-L", NULL);
  1320. for (ap = argv; *ap; ap++) {
  1321. if (strchr(FIND_EXPRSTARTCHARS,(*ap)[0])) {
  1322. char *a;
  1323. m_asprintf(&a, "./%s", *ap);
  1324. command_add_arg(&cmd, a);
  1325. } else {
  1326. command_add_arg(&cmd, (const char *)*ap);
  1327. }
  1328. }
  1329. command_add_args(&cmd, "-name", "*.deb", "-type", "f", "-print0", NULL);
  1330. command_exec(&cmd);
  1331. }
  1332. close(pi[1]);
  1333. nfiles= 0;
  1334. pf= fdopen(pi[0],"r"); if (!pf) ohshite(_("failed to fdopen find's pipe"));
  1335. varbuf_reset(&findoutput);
  1336. while ((c= fgetc(pf)) != EOF) {
  1337. varbuf_add_char(&findoutput, c);
  1338. if (!c) nfiles++;
  1339. }
  1340. if (ferror(pf)) ohshite(_("error reading find's pipe"));
  1341. if (fclose(pf)) ohshite(_("error closing find's pipe"));
  1342. r = subproc_wait_check(pid, "find", PROCNOERR);
  1343. if (r != 0)
  1344. ohshit(_("find for --recursive returned unhandled error %i"),r);
  1345. if (!nfiles)
  1346. ohshit(_("searched, but found no packages (files matching *.deb)"));
  1347. arglist= m_malloc(sizeof(char*)*(nfiles+1));
  1348. p = findoutput.buf;
  1349. for (i = 0; i < nfiles; i++) {
  1350. arglist[i] = p;
  1351. while (*p++ != '\0') ;
  1352. }
  1353. arglist[i] = NULL;
  1354. argp= arglist;
  1355. } else {
  1356. if (!*argv) badusage(_("--%s needs at least one package archive file argument"),
  1357. cipaction->olong);
  1358. argp= argv;
  1359. }
  1360. currenttime = time(NULL);
  1361. /* Initialize fname variables contents. */
  1362. varbuf_reset(&fnamevb);
  1363. varbuf_reset(&fnametmpvb);
  1364. varbuf_reset(&fnamenewvb);
  1365. varbuf_add_str(&fnamevb, instdir);
  1366. varbuf_add_char(&fnamevb, '/');
  1367. varbuf_add_str(&fnametmpvb, instdir);
  1368. varbuf_add_char(&fnametmpvb, '/');
  1369. varbuf_add_str(&fnamenewvb, instdir);
  1370. varbuf_add_char(&fnamenewvb, '/');
  1371. fnameidlu= fnamevb.used;
  1372. ensure_diversions();
  1373. ensure_statoverrides();
  1374. while ((thisarg = *argp++) != NULL) {
  1375. if (setjmp(ejbuf)) {
  1376. pop_error_context(ehflag_bombout);
  1377. if (abort_processing)
  1378. break;
  1379. continue;
  1380. }
  1381. push_error_context_jump(&ejbuf, print_error_perpackage, thisarg);
  1382. process_archive(thisarg);
  1383. onerr_abort++;
  1384. m_output(stdout, _("<standard output>"));
  1385. m_output(stderr, _("<standard error>"));
  1386. onerr_abort--;
  1387. pop_error_context(ehflag_normaltidy);
  1388. }
  1389. switch (cipaction->arg_int) {
  1390. case act_install:
  1391. case act_configure:
  1392. case act_triggers:
  1393. case act_remove:
  1394. case act_purge:
  1395. process_queue();
  1396. case act_unpack:
  1397. case act_avail:
  1398. break;
  1399. default:
  1400. internerr("unknown action '%d'", cipaction->arg_int);
  1401. }
  1402. trigproc_run_deferred();
  1403. modstatdb_shutdown();
  1404. return 0;
  1405. }
  1406. /**
  1407. * Decide whether we want to install a new version of the package.
  1408. *
  1409. * @param pkg The package with the version we might want to install
  1410. *
  1411. * @retval true If the package should be skipped.
  1412. * @retval false If the package should be installed.
  1413. */
  1414. bool
  1415. wanttoinstall(struct pkginfo *pkg)
  1416. {
  1417. int r;
  1418. if (pkg->want != want_install && pkg->want != want_hold) {
  1419. if (f_alsoselect) {
  1420. printf(_("Selecting previously unselected package %s.\n"),
  1421. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1422. pkg_set_want(pkg, want_install);
  1423. return true;
  1424. } else {
  1425. printf(_("Skipping unselected package %s.\n"),
  1426. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1427. return false;
  1428. }
  1429. }
  1430. if (pkg->eflag & eflag_reinstreq)
  1431. return true;
  1432. if (pkg->status < stat_unpacked)
  1433. return true;
  1434. r = dpkg_version_compare(&pkg->available.version, &pkg->installed.version);
  1435. if (r > 0) {
  1436. return true;
  1437. } else if (r == 0) {
  1438. /* Same version fully installed. */
  1439. if (f_skipsame) {
  1440. fprintf(stderr, _("Version %.250s of %.250s already installed, "
  1441. "skipping.\n"),
  1442. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1443. pkg_name(pkg, pnaw_nonambig));
  1444. return false;
  1445. } else {
  1446. return true;
  1447. }
  1448. } else {
  1449. if (fc_downgrade) {
  1450. warning(_("downgrading %.250s from %.250s to %.250s."),
  1451. pkg_name(pkg, pnaw_nonambig),
  1452. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1453. versiondescribe(&pkg->available.version, vdew_nonambig));
  1454. return true;
  1455. } else {
  1456. fprintf(stderr, _("Will not downgrade %.250s from version %.250s "
  1457. "to %.250s, skipping.\n"),
  1458. pkg_name(pkg, pnaw_nonambig),
  1459. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1460. versiondescribe(&pkg->available.version, vdew_nonambig));
  1461. return false;
  1462. }
  1463. }
  1464. }
  1465. struct fileinlist *newconff_append(struct fileinlist ***newconffileslastp_io,
  1466. struct filenamenode *namenode) {
  1467. struct fileinlist *newconff;
  1468. newconff= m_malloc(sizeof(struct fileinlist));
  1469. newconff->next = NULL;
  1470. newconff->namenode= namenode;
  1471. **newconffileslastp_io= newconff;
  1472. *newconffileslastp_io= &newconff->next;
  1473. return newconff;
  1474. }
  1475. /* vi: ts=8 sw=2
  1476. */