divertdb.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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
  10. * published by the Free Software Foundation; either version 2,
  11. * or (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful, but
  14. * 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
  19. * License along with dpkg; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <dpkg-i18n.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include <string.h>
  29. #include <errno.h>
  30. #include <sys/stat.h>
  31. #include <pwd.h>
  32. #include <grp.h>
  33. #include <sys/types.h>
  34. #include <dpkg.h>
  35. #include <dpkg-db.h>
  36. #include "filesdb.h"
  37. #include "main.h"
  38. static struct diversion *diversions = NULL;
  39. static FILE *diversionsfile = NULL;
  40. void
  41. ensure_diversions(void)
  42. {
  43. static struct varbuf vb;
  44. struct stat stab1, stab2;
  45. char linebuf[MAXDIVERTFILENAME];
  46. FILE *file;
  47. struct diversion *ov, *oicontest, *oialtname;
  48. int l;
  49. varbufreset(&vb);
  50. varbufaddstr(&vb, admindir);
  51. varbufaddstr(&vb, "/" DIVERSIONSFILE);
  52. varbufaddc(&vb, 0);
  53. onerr_abort++;
  54. file = fopen(vb.buf,"r");
  55. if (!file) {
  56. if (errno != ENOENT)
  57. ohshite(_("failed to open diversions file"));
  58. if (!diversionsfile) {
  59. onerr_abort--;
  60. return;
  61. }
  62. } else if (diversionsfile) {
  63. if (fstat(fileno(diversionsfile), &stab1))
  64. ohshite(_("failed to fstat previous diversions file"));
  65. if (fstat(fileno(file), &stab2))
  66. ohshite(_("failed to fstat diversions file"));
  67. if (stab1.st_dev == stab2.st_dev &&
  68. stab1.st_ino == stab2.st_ino) {
  69. fclose(file);
  70. onerr_abort--;
  71. return;
  72. }
  73. }
  74. if (diversionsfile)
  75. fclose(diversionsfile);
  76. diversionsfile = file;
  77. setcloexec(fileno(diversionsfile), vb.buf);
  78. for (ov = diversions; ov; ov = ov->next) {
  79. ov->useinstead->divert->camefrom->divert = NULL;
  80. ov->useinstead->divert = NULL;
  81. }
  82. diversions = NULL;
  83. if (!file) {
  84. onerr_abort--;
  85. return;
  86. }
  87. while ((l = fgets_checked(linebuf, sizeof(linebuf), file, vb.buf)) >= 0) {
  88. oicontest = nfmalloc(sizeof(struct diversion));
  89. oialtname = nfmalloc(sizeof(struct diversion));
  90. oialtname->camefrom = findnamenode(linebuf, 0);
  91. oialtname->useinstead = NULL;
  92. fgets_must(linebuf, sizeof(linebuf), file, vb.buf);
  93. oicontest->useinstead = findnamenode(linebuf, 0);
  94. oicontest->camefrom = NULL;
  95. fgets_must(linebuf, sizeof(linebuf), file, vb.buf);
  96. oicontest->pkg = oialtname->pkg = strcmp(linebuf, ":") ?
  97. findpackage(linebuf) : NULL;
  98. if (oialtname->camefrom->divert ||
  99. oicontest->useinstead->divert)
  100. ohshit(_("conflicting diversions involving `%.250s' or `%.250s'"),
  101. oialtname->camefrom->name, oicontest->useinstead->name);
  102. oialtname->camefrom->divert = oicontest;
  103. oicontest->useinstead->divert = oialtname;
  104. oicontest->next = diversions;
  105. diversions = oicontest;
  106. }
  107. if (ferror(file))
  108. ohshite(_("read error in diversions [i]"));
  109. onerr_abort--;
  110. }