archives.c 54 KB

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