archives.c 53 KB

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