po: Update German man pages translation
[dpkg.git] / lib / dpkg / db-fsys-divert.c
blobbf0b7ff5a232b83beb10b6f730e82d0e9d5017e3
1 /*
2 * libdpkg - Debian packaging suite library routines
3 * db-fsys-divert.c - management of filesystem diverted files database
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/>.
22 #include <config.h>
23 #include <compat.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <pwd.h>
31 #include <grp.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <stdlib.h>
36 #include <dpkg/i18n.h>
37 #include <dpkg/dpkg.h>
38 #include <dpkg/dpkg-db.h>
39 #include <dpkg/debug.h>
40 #include <dpkg/db-fsys.h>
42 static struct fsys_diversion *diversions = NULL;
43 static char *diversionsname;
45 void
46 ensure_diversions(void)
48 static struct stat sb_prev;
49 struct stat sb_next;
50 char linebuf[MAXDIVERTFILENAME];
51 static FILE *file_prev;
52 FILE *file;
53 struct fsys_diversion *ov, *oicontest, *oialtname;
55 if (diversionsname == NULL)
56 diversionsname = dpkg_db_get_path(DIVERSIONSFILE);
58 onerr_abort++;
60 file = fopen(diversionsname, "r");
61 if (!file) {
62 if (errno != ENOENT)
63 ohshite(_("failed to open diversions file"));
64 } else {
65 setcloexec(fileno(file), diversionsname);
67 if (fstat(fileno(file), &sb_next))
68 ohshite(_("failed to fstat diversions file"));
71 * We need to keep the database file open so that the
72 * filesystem cannot reuse the inode number (f.ex. during
73 * multiple dpkg-divert invocations in a maintainer script),
74 * otherwise the following check might turn true, and we
75 * would skip reloading a modified database.
77 if (file_prev &&
78 sb_prev.st_dev == sb_next.st_dev &&
79 sb_prev.st_ino == sb_next.st_ino) {
80 fclose(file);
81 onerr_abort--;
82 debug(dbg_general, "%s: same, skipping", __func__);
83 return;
85 sb_prev = sb_next;
87 if (file_prev)
88 fclose(file_prev);
89 file_prev = file;
91 for (ov = diversions; ov; ov = ov->next) {
92 ov->useinstead->divert->camefrom->divert = NULL;
93 ov->useinstead->divert = NULL;
95 diversions = NULL;
96 if (!file) {
97 onerr_abort--;
98 debug(dbg_general, "%s: none, resetting", __func__);
99 return;
101 debug(dbg_general, "%s: new, (re)loading", __func__);
103 while (fgets_checked(linebuf, sizeof(linebuf), file, diversionsname) >= 0) {
104 oicontest = nfmalloc(sizeof(*oicontest));
105 oialtname = nfmalloc(sizeof(*oialtname));
107 oialtname->camefrom = fsys_hash_find_node(linebuf, 0);
108 oialtname->useinstead = NULL;
110 fgets_must(linebuf, sizeof(linebuf), file, diversionsname);
111 oicontest->useinstead = fsys_hash_find_node(linebuf, 0);
112 oicontest->camefrom = NULL;
114 fgets_must(linebuf, sizeof(linebuf), file, diversionsname);
115 oicontest->pkgset = strcmp(linebuf, ":") ?
116 pkg_hash_find_set(linebuf) : NULL;
117 oialtname->pkgset = oicontest->pkgset;
119 if (oialtname->camefrom->divert ||
120 oicontest->useinstead->divert)
121 ohshit(_("conflicting diversions involving '%.250s' or '%.250s'"),
122 oialtname->camefrom->name, oicontest->useinstead->name);
124 oialtname->camefrom->divert = oicontest;
125 oicontest->useinstead->divert = oialtname;
127 oicontest->next = diversions;
128 diversions = oicontest;
131 onerr_abort--;