libdpkg: Deindent an else clause
[dpkg.git] / src / main / help.c
blob59e730e1c567d0dad38a4745cc58c1d7b077ab04
1 /*
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/>.
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 <unistd.h>
31 #include <stdlib.h>
33 #include <dpkg/i18n.h>
34 #include <dpkg/dpkg.h>
35 #include <dpkg/dpkg-db.h>
36 #include <dpkg/path.h>
37 #include <dpkg/file.h>
38 #include <dpkg/command.h>
39 #include <dpkg/db-fsys.h>
41 #include "main.h"
43 const char *const statusstrings[]= {
44 [PKG_STAT_NOTINSTALLED] = N_("not installed"),
45 [PKG_STAT_CONFIGFILES] = N_("not installed but configs remain"),
46 [PKG_STAT_HALFINSTALLED] = N_("broken due to failed removal or installation"),
47 [PKG_STAT_UNPACKED] = N_("unpacked but not configured"),
48 [PKG_STAT_HALFCONFIGURED] = N_("broken due to postinst failure"),
49 [PKG_STAT_TRIGGERSAWAITED] = N_("awaiting trigger processing by another package"),
50 [PKG_STAT_TRIGGERSPENDING] = N_("triggered"),
51 [PKG_STAT_INSTALLED] = N_("installed")
54 struct fsys_namenode *
55 namenodetouse(struct fsys_namenode *namenode, struct pkginfo *pkg,
56 struct pkgbin *pkgbin)
58 struct fsys_namenode *fnn;
60 if (!namenode->divert)
61 return namenode;
63 debug(dbg_eachfile, "namenodetouse namenode='%s' pkg=%s",
64 namenode->name, pkgbin_name(pkg, pkgbin, pnaw_always));
66 fnn = (namenode->divert->useinstead && namenode->divert->pkgset != pkg->set)
67 ? namenode->divert->useinstead : namenode;
69 debug(dbg_eachfile,
70 "namenodetouse ... useinstead=%s camefrom=%s pkg=%s return %s",
71 namenode->divert->useinstead ? namenode->divert->useinstead->name : "<none>",
72 namenode->divert->camefrom ? namenode->divert->camefrom->name : "<none>",
73 namenode->divert->pkgset ? namenode->divert->pkgset->name : "<none>",
74 fnn->name);
76 return fnn;
79 /**
80 * Verify that some programs can be found in the PATH.
82 void checkpath(void) {
83 static const char *const prog_list[] = {
84 DPKG_DEFAULT_SHELL,
85 RM,
86 TAR,
87 DIFF,
88 BACKEND,
89 /* macOS uses dyld (Mach-O) instead of ld.so (ELF), and does not have
90 * an ldconfig. */
91 #if defined(__APPLE__) && defined(__MACH__)
92 "update_dyld_shared_cache",
93 #elif defined(__GLIBC__) || defined(__UCLIBC__) || \
94 defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
95 "ldconfig",
96 #endif
97 #if BUILD_START_STOP_DAEMON
98 "start-stop-daemon",
99 #endif
100 NULL
103 const char *const *prog;
104 int warned= 0;
106 for (prog = prog_list; *prog; prog++) {
107 if (!command_in_path(*prog)) {
108 warning(_("'%s' not found in PATH or not executable"), *prog);
109 warned++;
113 if (warned)
114 forcibleerr(FORCE_BAD_PATH,
115 P_("%d expected program not found in PATH or not executable\n%s",
116 "%d expected programs not found in PATH or not executable\n%s",
117 warned),
118 warned, _("Note: root's PATH should usually contain "
119 "/usr/local/sbin, /usr/sbin and /sbin"));
122 bool
123 ignore_depends(const struct pkginfo *pkg)
125 struct pkg_list *id;
126 for (id= ignoredependss; id; id= id->next)
127 if (id->pkg == pkg)
128 return true;
129 return false;
132 static bool
133 ignore_depends_possi(struct deppossi *possi)
135 struct deppossi_pkg_iterator *possi_iter;
136 struct pkginfo *pkg;
138 possi_iter = deppossi_pkg_iter_new(possi, wpb_installed);
139 while ((pkg = deppossi_pkg_iter_next(possi_iter))) {
140 if (ignore_depends(pkg)) {
141 deppossi_pkg_iter_free(possi_iter);
142 return true;
145 deppossi_pkg_iter_free(possi_iter);
147 return false;
150 bool
151 force_depends(struct deppossi *possi)
153 return in_force(FORCE_DEPENDS) ||
154 ignore_depends_possi(possi) ||
155 ignore_depends(possi->up->up);
158 bool
159 force_breaks(struct deppossi *possi)
161 return in_force(FORCE_BREAKS) ||
162 ignore_depends_possi(possi) ||
163 ignore_depends(possi->up->up);
166 bool
167 force_conflicts(struct deppossi *possi)
169 return in_force(FORCE_CONFLICTS);
172 void clear_istobes(void) {
173 struct pkg_hash_iter *iter;
174 struct pkginfo *pkg;
176 iter = pkg_hash_iter_new();
177 while ((pkg = pkg_hash_iter_next_pkg(iter)) != NULL) {
178 ensure_package_clientdata(pkg);
179 pkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
180 pkg->clientdata->replacingfilesandsaid= 0;
182 pkg_hash_iter_free(iter);
186 * Returns true if the directory contains conffiles belonging to pkg,
187 * false otherwise.
189 bool
190 dir_has_conffiles(struct fsys_namenode *file, struct pkginfo *pkg)
192 struct conffile *conff;
193 size_t namelen;
195 debug(dbg_veryverbose, "dir_has_conffiles '%s' (from %s)", file->name,
196 pkg_name(pkg, pnaw_always));
197 namelen = strlen(file->name);
198 for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
199 if (conff->obsolete || conff->remove_on_upgrade)
200 continue;
201 if (strncmp(file->name, conff->name, namelen) == 0 &&
202 strlen(conff->name) > namelen && conff->name[namelen] == '/') {
203 debug(dbg_veryverbose, "directory %s has conffile %s from %s",
204 file->name, conff->name, pkg_name(pkg, pnaw_always));
205 return true;
208 debug(dbg_veryverbose, "dir_has_conffiles no");
209 return false;
213 * Returns true if the file is used by packages other than pkg,
214 * false otherwise.
216 bool
217 dir_is_used_by_others(struct fsys_namenode *file, struct pkginfo *pkg)
219 struct fsys_node_pkgs_iter *iter;
220 struct pkginfo *other_pkg;
222 debug(dbg_veryverbose, "dir_is_used_by_others '%s' (except %s)", file->name,
223 pkg ? pkg_name(pkg, pnaw_always) : "<none>");
225 iter = fsys_node_pkgs_iter_new(file);
226 while ((other_pkg = fsys_node_pkgs_iter_next(iter))) {
227 debug(dbg_veryverbose, "dir_is_used_by_others considering %s ...",
228 pkg_name(other_pkg, pnaw_always));
229 if (other_pkg == pkg)
230 continue;
232 fsys_node_pkgs_iter_free(iter);
233 debug(dbg_veryverbose, "dir_is_used_by_others yes");
234 return true;
236 fsys_node_pkgs_iter_free(iter);
238 debug(dbg_veryverbose, "dir_is_used_by_others no");
239 return false;
243 * Returns true if the file is used by pkg, false otherwise.
245 bool
246 dir_is_used_by_pkg(struct fsys_namenode *file, struct pkginfo *pkg,
247 struct fsys_namenode_list *list)
249 struct fsys_namenode_list *node;
250 size_t namelen;
252 debug(dbg_veryverbose, "dir_is_used_by_pkg '%s' (by %s)",
253 file->name, pkg ? pkg_name(pkg, pnaw_always) : "<none>");
255 namelen = strlen(file->name);
257 for (node = list; node; node = node->next) {
258 debug(dbg_veryverbose, "dir_is_used_by_pkg considering %s ...",
259 node->namenode->name);
261 if (strncmp(file->name, node->namenode->name, namelen) == 0 &&
262 strlen(node->namenode->name) > namelen &&
263 node->namenode->name[namelen] == '/') {
264 debug(dbg_veryverbose, "dir_is_used_by_pkg yes");
265 return true;
269 debug(dbg_veryverbose, "dir_is_used_by_pkg no");
271 return false;
275 * Mark a conffile as obsolete.
277 * @param pkg The package owning the conffile.
278 * @param namenode The namenode for the obsolete conffile.
280 void
281 conffile_mark_obsolete(struct pkginfo *pkg, struct fsys_namenode *namenode)
283 struct conffile *conff;
285 for (conff = pkg->installed.conffiles; conff; conff = conff->next) {
286 if (strcmp(conff->name, namenode->name) == 0) {
287 debug(dbg_conff, "marking %s conffile %s as obsolete",
288 pkg_name(pkg, pnaw_always), conff->name);
289 conff->obsolete = true;
290 return;
296 * Mark all package conffiles as old.
298 * @param pkg The package owning the conffiles.
300 void
301 pkg_conffiles_mark_old(struct pkginfo *pkg)
303 const struct conffile *conff;
305 for (conff = pkg->installed.conffiles; conff; conff = conff->next) {
306 struct fsys_namenode *namenode;
308 namenode = fsys_hash_find_node(conff->name, FHFF_NONE); /* XXX */
309 namenode->flags |= FNNF_OLD_CONFF;
310 if (!namenode->oldhash)
311 namenode->oldhash = conff->hash;
312 debug(dbg_conffdetail, "%s '%s' namenode '%s' flags %o", __func__,
313 conff->name, namenode->name, namenode->flags);
317 void
318 log_action(const char *action, struct pkginfo *pkg, struct pkgbin *pkgbin)
320 log_message("%s %s %s %s", action, pkgbin_name(pkg, pkgbin, pnaw_always),
321 versiondescribe_c(&pkg->installed.version, vdew_nonambig),
322 versiondescribe_c(&pkg->available.version, vdew_nonambig));
323 statusfd_send("processing: %s: %s", action,
324 pkgbin_name(pkg, pkgbin, pnaw_nonambig));