2 * dpkg - main program for package management
3 * help.c - various helper routines
5 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2007-2015 Guillem Jover <guillem@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>
33 #include <dpkg/i18n.h>
34 #include <dpkg/dpkg.h>
35 #include <dpkg/dpkg-db.h>
36 #include <dpkg/path.h>
41 const char *const statusstrings[]= {
42 [PKG_STAT_NOTINSTALLED] = N_("not installed"),
43 [PKG_STAT_CONFIGFILES] = N_("not installed but configs remain"),
44 [PKG_STAT_HALFINSTALLED] = N_("broken due to failed removal or installation"),
45 [PKG_STAT_UNPACKED] = N_("unpacked but not configured"),
46 [PKG_STAT_HALFCONFIGURED] = N_("broken due to postinst failure"),
47 [PKG_STAT_TRIGGERSAWAITED] = N_("awaiting trigger processing by another package"),
48 [PKG_STAT_TRIGGERSPENDING] = N_("triggered"),
49 [PKG_STAT_INSTALLED] = N_("installed")
53 namenodetouse(struct filenamenode *namenode, struct pkginfo *pkg,
54 struct pkgbin *pkgbin)
56 struct filenamenode *r;
58 if (!namenode->divert) {
63 debug(dbg_eachfile, "namenodetouse namenode='%s' pkg=%s",
64 namenode->name, pkgbin_name(pkg, pkgbin, pnaw_always));
67 (namenode->divert->useinstead && namenode->divert->pkgset != pkg->set)
68 ? namenode->divert->useinstead : namenode;
71 "namenodetouse ... useinstead=%s camefrom=%s pkg=%s return %s",
72 namenode->divert->useinstead ? namenode->divert->useinstead->name : "<none>",
73 namenode->divert->camefrom ? namenode->divert->camefrom->name : "<none>",
74 namenode->divert->pkgset ? namenode->divert->pkgset->name : "<none>",
81 find_command(const char *prog)
83 struct varbuf filename = VARBUF_INIT;
85 const char *path_list;
86 const char *path, *path_end;
89 path_list = getenv("PATH");
91 ohshit(_("PATH is not set"));
93 for (path = path_list; path; path = path_end ? path_end + 1 : NULL) {
94 path_end = strchr(path, ':');
95 path_len = path_end ? (size_t)(path_end - path) : strlen(path);
97 varbuf_reset(&filename);
98 varbuf_add_buf(&filename, path, path_len);
100 varbuf_add_char(&filename, '/');
101 varbuf_add_str(&filename, prog);
102 varbuf_end_str(&filename);
104 if (stat(filename.buf, &stab) == 0 && (stab.st_mode & 0111)) {
105 varbuf_destroy(&filename);
110 varbuf_destroy(&filename);
115 * Verify that some programs can be found in the PATH.
117 void checkpath(void) {
118 static const char *const prog_list[] = {
124 /* Mac OS X uses dyld (Mach-O) instead of ld.so (ELF), and does not have
126 #if defined(__APPLE__) && defined(__MACH__)
127 "update_dyld_shared_cache",
131 #if BUILD_START_STOP_DAEMON
137 const char *const *prog;
140 for (prog = prog_list; *prog; prog++) {
141 if (!find_command(*prog)) {
142 warning(_("'%s' not found in PATH or not executable"), *prog);
148 forcibleerr(fc_badpath,
149 P_("%d expected program not found in PATH or not executable\n%s",
150 "%d expected programs not found in PATH or not executable\n%s",
152 warned, _("Note: root's PATH should usually contain "
153 "/usr/local/sbin, /usr/sbin and /sbin"));
157 ignore_depends(struct pkginfo *pkg)
160 for (id= ignoredependss; id; id= id->next)
167 ignore_depends_possi(struct deppossi *possi)
169 struct deppossi_pkg_iterator *possi_iter;
172 possi_iter = deppossi_pkg_iter_new(possi, wpb_installed);
173 while ((pkg = deppossi_pkg_iter_next(possi_iter))) {
174 if (ignore_depends(pkg)) {
175 deppossi_pkg_iter_free(possi_iter);
179 deppossi_pkg_iter_free(possi_iter);
185 force_depends(struct deppossi *possi)
188 ignore_depends_possi(possi) ||
189 ignore_depends(possi->up->up);
193 force_breaks(struct deppossi *possi)
196 ignore_depends_possi(possi) ||
197 ignore_depends(possi->up->up);
201 force_conflicts(struct deppossi *possi)
206 void clear_istobes(void) {
207 struct pkgiterator *iter;
210 iter = pkg_db_iter_new();
211 while ((pkg = pkg_db_iter_next_pkg(iter)) != NULL) {
212 ensure_package_clientdata(pkg);
213 pkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
214 pkg->clientdata->replacingfilesandsaid= 0;
216 pkg_db_iter_free(iter);
220 * Returns true if the directory contains conffiles belonging to pkg,
224 dir_has_conffiles(struct filenamenode *file, struct pkginfo *pkg)
226 struct conffile *conff;
229 debug(dbg_veryverbose, "dir_has_conffiles '%s' (from %s)", file->name,
230 pkg_name(pkg, pnaw_always));
231 namelen = strlen(file->name);
232 for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
235 if (strncmp(file->name, conff->name, namelen) == 0 &&
236 strlen(conff->name) > namelen && conff->name[namelen] == '/') {
237 debug(dbg_veryverbose, "directory %s has conffile %s from %s",
238 file->name, conff->name, pkg_name(pkg, pnaw_always));
242 debug(dbg_veryverbose, "dir_has_conffiles no");
247 * Returns true if the file is used by packages other than pkg,
251 dir_is_used_by_others(struct filenamenode *file, struct pkginfo *pkg)
253 struct filepackages_iterator *iter;
254 struct pkginfo *other_pkg;
256 debug(dbg_veryverbose, "dir_is_used_by_others '%s' (except %s)", file->name,
257 pkg ? pkg_name(pkg, pnaw_always) : "<none>");
259 iter = filepackages_iter_new(file);
260 while ((other_pkg = filepackages_iter_next(iter))) {
261 debug(dbg_veryverbose, "dir_is_used_by_others considering %s ...",
262 pkg_name(other_pkg, pnaw_always));
263 if (other_pkg == pkg)
266 filepackages_iter_free(iter);
267 debug(dbg_veryverbose, "dir_is_used_by_others yes");
270 filepackages_iter_free(iter);
272 debug(dbg_veryverbose, "dir_is_used_by_others no");
277 * Returns true if the file is used by pkg, false otherwise.
280 dir_is_used_by_pkg(struct filenamenode *file, struct pkginfo *pkg,
281 struct fileinlist *list)
283 struct fileinlist *node;
286 debug(dbg_veryverbose, "dir_is_used_by_pkg '%s' (by %s)",
287 file->name, pkg ? pkg_name(pkg, pnaw_always) : "<none>");
289 namelen = strlen(file->name);
291 for (node = list; node; node = node->next) {
292 debug(dbg_veryverbose, "dir_is_used_by_pkg considering %s ...",
293 node->namenode->name);
295 if (strncmp(file->name, node->namenode->name, namelen) == 0 &&
296 strlen(node->namenode->name) > namelen &&
297 node->namenode->name[namelen] == '/') {
298 debug(dbg_veryverbose, "dir_is_used_by_pkg yes");
303 debug(dbg_veryverbose, "dir_is_used_by_pkg no");
309 * Mark a conffile as obsolete.
311 * @param pkg The package owning the conffile.
312 * @param namenode The namenode for the obsolete conffile.
315 conffile_mark_obsolete(struct pkginfo *pkg, struct filenamenode *namenode)
317 struct conffile *conff;
319 for (conff = pkg->installed.conffiles; conff; conff = conff->next) {
320 if (strcmp(conff->name, namenode->name) == 0) {
321 debug(dbg_conff, "marking %s conffile %s as obsolete",
322 pkg_name(pkg, pnaw_always), conff->name);
323 conff->obsolete = true;
330 * Mark all package conffiles as old.
332 * @param pkg The package owning the conffiles.
335 pkg_conffiles_mark_old(struct pkginfo *pkg)
337 const struct conffile *conff;
338 struct filenamenode *namenode;
340 for (conff = pkg->installed.conffiles; conff; conff = conff->next) {
341 namenode = findnamenode(conff->name, 0); /* XXX */
342 namenode->flags |= fnnf_old_conff;
343 if (!namenode->oldhash)
344 namenode->oldhash = conff->hash;
345 debug(dbg_conffdetail, "%s '%s' namenode '%s' flags %o", __func__,
346 conff->name, namenode->name, namenode->flags);
351 log_action(const char *action, struct pkginfo *pkg, struct pkgbin *pkgbin)
353 log_message("%s %s %s %s", action, pkgbin_name(pkg, pkgbin, pnaw_always),
354 versiondescribe(&pkg->installed.version, vdew_nonambig),
355 versiondescribe(&pkg->available.version, vdew_nonambig));
356 statusfd_send("processing: %s: %s", action,
357 pkgbin_name(pkg, pkgbin, pnaw_nonambig));