divertdb.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * dpkg - main program for package management
  3. * divertdb.c - management of database of diverted files
  4. *
  5. * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2000, 2001 Wichert Akkerman <wakkerma@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #include <pwd.h>
  28. #include <grp.h>
  29. #include <fcntl.h>
  30. #include <unistd.h>
  31. #include <stdlib.h>
  32. #include <dpkg/i18n.h>
  33. #include <dpkg/dpkg.h>
  34. #include <dpkg/dpkg-db.h>
  35. #include "filesdb.h"
  36. #include "main.h"
  37. static struct diversion *diversions = NULL;
  38. static char *diversionsname;
  39. void
  40. ensure_diversions(void)
  41. {
  42. static struct stat sb_prev;
  43. struct stat sb_next;
  44. char linebuf[MAXDIVERTFILENAME];
  45. static FILE *file_prev;
  46. FILE *file;
  47. struct diversion *ov, *oicontest, *oialtname;
  48. if (diversionsname == NULL)
  49. diversionsname = dpkg_db_get_path(DIVERSIONSFILE);
  50. onerr_abort++;
  51. file = fopen(diversionsname, "r");
  52. if (!file) {
  53. if (errno != ENOENT)
  54. ohshite(_("failed to open diversions file"));
  55. } else {
  56. setcloexec(fileno(file), diversionsname);
  57. if (fstat(fileno(file), &sb_next))
  58. ohshite(_("failed to fstat diversions file"));
  59. /*
  60. * We need to keep the database file open so that the
  61. * filesystem cannot reuse the inode number (f.ex. during
  62. * multiple dpkg-divert invocations in a maintainer script),
  63. * otherwise the following check might turn true, and we
  64. * would skip reloading a modified database.
  65. */
  66. if (file_prev &&
  67. sb_prev.st_dev == sb_next.st_dev &&
  68. sb_prev.st_ino == sb_next.st_ino) {
  69. fclose(file);
  70. onerr_abort--;
  71. debug(dbg_general, "%s: same, skipping", __func__);
  72. return;
  73. }
  74. sb_prev = sb_next;
  75. }
  76. if (file_prev)
  77. fclose(file_prev);
  78. file_prev = file;
  79. for (ov = diversions; ov; ov = ov->next) {
  80. ov->useinstead->divert->camefrom->divert = NULL;
  81. ov->useinstead->divert = NULL;
  82. }
  83. diversions = NULL;
  84. if (!file) {
  85. onerr_abort--;
  86. debug(dbg_general, "%s: none, resetting", __func__);
  87. return;
  88. }
  89. debug(dbg_general, "%s: new, (re)loading", __func__);
  90. while (fgets_checked(linebuf, sizeof(linebuf), file, diversionsname) >= 0) {
  91. oicontest = nfmalloc(sizeof(struct diversion));
  92. oialtname = nfmalloc(sizeof(struct diversion));
  93. oialtname->camefrom = findnamenode(linebuf, 0);
  94. oialtname->useinstead = NULL;
  95. fgets_must(linebuf, sizeof(linebuf), file, diversionsname);
  96. oicontest->useinstead = findnamenode(linebuf, 0);
  97. oicontest->camefrom = NULL;
  98. fgets_must(linebuf, sizeof(linebuf), file, diversionsname);
  99. oicontest->pkgset = strcmp(linebuf, ":") ?
  100. pkg_db_find_set(linebuf) : NULL;
  101. oialtname->pkgset = oicontest->pkgset;
  102. if (oialtname->camefrom->divert ||
  103. oicontest->useinstead->divert)
  104. ohshit(_("conflicting diversions involving '%.250s' or '%.250s'"),
  105. oialtname->camefrom->name, oicontest->useinstead->name);
  106. oialtname->camefrom->divert = oicontest;
  107. oicontest->useinstead->divert = oialtname;
  108. oicontest->next = diversions;
  109. diversions = oicontest;
  110. }
  111. onerr_abort--;
  112. }