divertdb.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. void
  40. ensure_diversions(void)
  41. {
  42. static struct varbuf vb;
  43. struct stat stab1, stab2;
  44. char linebuf[MAXDIVERTFILENAME];
  45. FILE *file;
  46. struct diversion *ov, *oicontest, *oialtname;
  47. varbuf_reset(&vb);
  48. varbuf_add_str(&vb, admindir);
  49. varbuf_add_str(&vb, "/" DIVERSIONSFILE);
  50. varbuf_end_str(&vb);
  51. onerr_abort++;
  52. file = fopen(vb.buf,"r");
  53. if (!file) {
  54. if (errno != ENOENT)
  55. ohshite(_("failed to open diversions file"));
  56. if (!diversionsfile) {
  57. onerr_abort--;
  58. return;
  59. }
  60. } else if (diversionsfile) {
  61. if (fstat(fileno(diversionsfile), &stab1))
  62. ohshite(_("failed to fstat previous diversions file"));
  63. if (fstat(fileno(file), &stab2))
  64. ohshite(_("failed to fstat diversions file"));
  65. if (stab1.st_dev == stab2.st_dev &&
  66. stab1.st_ino == stab2.st_ino) {
  67. fclose(file);
  68. onerr_abort--;
  69. return;
  70. }
  71. }
  72. if (diversionsfile)
  73. fclose(diversionsfile);
  74. diversionsfile = file;
  75. setcloexec(fileno(diversionsfile), vb.buf);
  76. for (ov = diversions; ov; ov = ov->next) {
  77. ov->useinstead->divert->camefrom->divert = NULL;
  78. ov->useinstead->divert = NULL;
  79. }
  80. diversions = NULL;
  81. if (!file) {
  82. onerr_abort--;
  83. return;
  84. }
  85. while (fgets_checked(linebuf, sizeof(linebuf), file, vb.buf) >= 0) {
  86. oicontest = nfmalloc(sizeof(struct diversion));
  87. oialtname = nfmalloc(sizeof(struct diversion));
  88. oialtname->camefrom = findnamenode(linebuf, 0);
  89. oialtname->useinstead = NULL;
  90. fgets_must(linebuf, sizeof(linebuf), file, vb.buf);
  91. oicontest->useinstead = findnamenode(linebuf, 0);
  92. oicontest->camefrom = NULL;
  93. fgets_must(linebuf, sizeof(linebuf), file, vb.buf);
  94. oicontest->pkg = oialtname->pkg = strcmp(linebuf, ":") ?
  95. pkg_db_find(linebuf) : NULL;
  96. if (oialtname->camefrom->divert ||
  97. oicontest->useinstead->divert)
  98. ohshit(_("conflicting diversions involving `%.250s' or `%.250s'"),
  99. oialtname->camefrom->name, oicontest->useinstead->name);
  100. oialtname->camefrom->divert = oicontest;
  101. oicontest->useinstead->divert = oialtname;
  102. oicontest->next = diversions;
  103. diversions = oicontest;
  104. }
  105. onerr_abort--;
  106. }