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/>.
25 #include <sys/types.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
;
46 ensure_diversions(void)
48 static struct stat sb_prev
;
50 char linebuf
[MAXDIVERTFILENAME
];
51 static FILE *file_prev
;
53 struct fsys_diversion
*ov
, *oicontest
, *oialtname
;
55 if (diversionsname
== NULL
)
56 diversionsname
= dpkg_db_get_path(DIVERSIONSFILE
);
60 file
= fopen(diversionsname
, "r");
63 ohshite(_("failed to open diversions file"));
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.
78 sb_prev
.st_dev
== sb_next
.st_dev
&&
79 sb_prev
.st_ino
== sb_next
.st_ino
) {
82 debug(dbg_general
, "%s: same, skipping", __func__
);
91 for (ov
= diversions
; ov
; ov
= ov
->next
) {
92 ov
->useinstead
->divert
->camefrom
->divert
= NULL
;
93 ov
->useinstead
->divert
= NULL
;
98 debug(dbg_general
, "%s: none, resetting", __func__
);
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
;