2 * dpkg - main program for package management
3 * divertdb.c - management of database of diverted files
5 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2000, 2001 Wichert Akkerman <wakkerma@debian.org>
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.
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.
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/>.
25 #include <sys/types.h>
36 #include <dpkg/i18n.h>
37 #include <dpkg/dpkg.h>
38 #include <dpkg/dpkg-db.h>
43 static struct diversion *diversions = NULL;
44 static char *diversionsname;
47 ensure_diversions(void)
49 static struct stat sb_prev;
51 char linebuf[MAXDIVERTFILENAME];
52 static FILE *file_prev;
54 struct diversion *ov, *oicontest, *oialtname;
56 if (diversionsname == NULL)
57 diversionsname = dpkg_db_get_path(DIVERSIONSFILE);
61 file = fopen(diversionsname, "r");
64 ohshite(_("failed to open diversions file"));
66 setcloexec(fileno(file), diversionsname);
68 if (fstat(fileno(file), &sb_next))
69 ohshite(_("failed to fstat diversions file"));
72 * We need to keep the database file open so that the
73 * filesystem cannot reuse the inode number (f.ex. during
74 * multiple dpkg-divert invocations in a maintainer script),
75 * otherwise the following check might turn true, and we
76 * would skip reloading a modified database.
79 sb_prev.st_dev == sb_next.st_dev &&
80 sb_prev.st_ino == sb_next.st_ino) {
83 debug(dbg_general, "%s: same, skipping", __func__);
92 for (ov = diversions; ov; ov = ov->next) {
93 ov->useinstead->divert->camefrom->divert = NULL;
94 ov->useinstead->divert = NULL;
99 debug(dbg_general, "%s: none, resetting", __func__);
102 debug(dbg_general, "%s: new, (re)loading", __func__);
104 while (fgets_checked(linebuf, sizeof(linebuf), file, diversionsname) >= 0) {
105 oicontest = nfmalloc(sizeof(struct diversion));
106 oialtname = nfmalloc(sizeof(struct diversion));
108 oialtname->camefrom = findnamenode(linebuf, 0);
109 oialtname->useinstead = NULL;
111 fgets_must(linebuf, sizeof(linebuf), file, diversionsname);
112 oicontest->useinstead = findnamenode(linebuf, 0);
113 oicontest->camefrom = NULL;
115 fgets_must(linebuf, sizeof(linebuf), file, diversionsname);
116 oicontest->pkgset = strcmp(linebuf, ":") ?
117 pkg_db_find_set(linebuf) : NULL;
118 oialtname->pkgset = oicontest->pkgset;
120 if (oialtname->camefrom->divert ||
121 oicontest->useinstead->divert)
122 ohshit(_("conflicting diversions involving '%.250s' or '%.250s'"),
123 oialtname->camefrom->name, oicontest->useinstead->name);
125 oialtname->camefrom->divert = oicontest;
126 oicontest->useinstead->divert = oialtname;
128 oicontest->next = diversions;
129 diversions = oicontest;