divertdb.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * dpkg - main program for package management
  3. * divertdb.c - management of database of diverted files
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@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 <http://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 FILE *diversionsfile = NULL;
  39. static char *diversionsname;
  40. void
  41. ensure_diversions(void)
  42. {
  43. struct stat stab1, stab2;
  44. char linebuf[MAXDIVERTFILENAME];
  45. FILE *file;
  46. struct diversion *ov, *oicontest, *oialtname;
  47. if (diversionsname != NULL)
  48. free(diversionsname);
  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. if (!diversionsfile) {
  56. onerr_abort--;
  57. return;
  58. }
  59. } else if (diversionsfile) {
  60. if (fstat(fileno(diversionsfile), &stab1))
  61. ohshite(_("failed to fstat previous diversions file"));
  62. if (fstat(fileno(file), &stab2))
  63. ohshite(_("failed to fstat diversions file"));
  64. if (stab1.st_dev == stab2.st_dev &&
  65. stab1.st_ino == stab2.st_ino) {
  66. fclose(file);
  67. onerr_abort--;
  68. return;
  69. }
  70. }
  71. if (diversionsfile)
  72. fclose(diversionsfile);
  73. diversionsfile = file;
  74. setcloexec(fileno(diversionsfile), diversionsname);
  75. for (ov = diversions; ov; ov = ov->next) {
  76. ov->useinstead->divert->camefrom->divert = NULL;
  77. ov->useinstead->divert = NULL;
  78. }
  79. diversions = NULL;
  80. if (!file) {
  81. onerr_abort--;
  82. return;
  83. }
  84. while (fgets_checked(linebuf, sizeof(linebuf), file, diversionsname) >= 0) {
  85. oicontest = nfmalloc(sizeof(struct diversion));
  86. oialtname = nfmalloc(sizeof(struct diversion));
  87. oialtname->camefrom = findnamenode(linebuf, 0);
  88. oialtname->useinstead = NULL;
  89. fgets_must(linebuf, sizeof(linebuf), file, diversionsname);
  90. oicontest->useinstead = findnamenode(linebuf, 0);
  91. oicontest->camefrom = NULL;
  92. fgets_must(linebuf, sizeof(linebuf), file, diversionsname);
  93. oicontest->pkg = oialtname->pkg = strcmp(linebuf, ":") ?
  94. pkg_db_find(linebuf) : NULL;
  95. if (oialtname->camefrom->divert ||
  96. oicontest->useinstead->divert)
  97. ohshit(_("conflicting diversions involving `%.250s' or `%.250s'"),
  98. oialtname->camefrom->name, oicontest->useinstead->name);
  99. oialtname->camefrom->divert = oicontest;
  100. oicontest->useinstead->divert = oialtname;
  101. oicontest->next = diversions;
  102. diversions = oicontest;
  103. }
  104. onerr_abort--;
  105. }