chiark / gitweb /
lib/dpkg/tarfn.c: Kludge `tar_header_decode' to handle spurious `errno'.
[dpkg] / src / help.c
1 /*
2  * dpkg - main program for package management
3  * help.c - various helper routines
4  *
5  * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6  * Copyright © 2007-2015 Guillem Jover <guillem@debian.org>
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
22 #include <config.h>
23 #include <compat.h>
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27
28 #include <errno.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32
33 #include <dpkg/i18n.h>
34 #include <dpkg/dpkg.h>
35 #include <dpkg/dpkg-db.h>
36 #include <dpkg/path.h>
37
38 #include "filesdb.h"
39 #include "main.h"
40
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")
50 };
51
52 struct filenamenode *
53 namenodetouse(struct filenamenode *namenode, struct pkginfo *pkg,
54               struct pkgbin *pkgbin)
55 {
56   struct filenamenode *r;
57
58   if (!namenode->divert) {
59     r = namenode;
60     return r;
61   }
62
63   debug(dbg_eachfile, "namenodetouse namenode='%s' pkg=%s",
64         namenode->name, pkgbin_name(pkg, pkgbin, pnaw_always));
65
66   r=
67     (namenode->divert->useinstead && namenode->divert->pkgset != pkg->set)
68       ? namenode->divert->useinstead : namenode;
69
70   debug(dbg_eachfile,
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>",
75         r->name);
76
77   return r;
78 }
79
80 bool
81 find_command(const char *prog)
82 {
83   struct varbuf filename = VARBUF_INIT;
84   struct stat stab;
85   const char *path_list;
86   const char *path, *path_end;
87   size_t path_len;
88
89   path_list = getenv("PATH");
90   if (!path_list)
91     ohshit(_("PATH is not set"));
92
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);
96
97     varbuf_reset(&filename);
98     varbuf_add_buf(&filename, path, path_len);
99     if (path_len)
100       varbuf_add_char(&filename, '/');
101     varbuf_add_str(&filename, prog);
102     varbuf_end_str(&filename);
103
104     if (stat(filename.buf, &stab) == 0 && (stab.st_mode & 0111)) {
105       varbuf_destroy(&filename);
106       return true;
107     }
108   }
109
110   varbuf_destroy(&filename);
111   return false;
112 }
113
114 /**
115  * Verify that some programs can be found in the PATH.
116  */
117 void checkpath(void) {
118   static const char *const prog_list[] = {
119     DEFAULTSHELL,
120     RM,
121     TAR,
122     DIFF,
123     BACKEND,
124     /* Mac OS X uses dyld (Mach-O) instead of ld.so (ELF), and does not have
125      * an ldconfig. */
126 #if defined(__APPLE__) && defined(__MACH__)
127     "update_dyld_shared_cache",
128 #else
129     "ldconfig",
130 #endif
131 #if BUILD_START_STOP_DAEMON
132     "start-stop-daemon",
133 #endif
134     NULL
135   };
136
137   const char *const *prog;
138   int warned= 0;
139
140   for (prog = prog_list; *prog; prog++) {
141     if (!find_command(*prog)) {
142       warning(_("'%s' not found in PATH or not executable"), *prog);
143       warned++;
144     }
145   }
146
147   if (warned)
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",
151                    warned),
152                 warned, _("Note: root's PATH should usually contain "
153                           "/usr/local/sbin, /usr/sbin and /sbin"));
154 }
155
156 bool
157 ignore_depends(struct pkginfo *pkg)
158 {
159   struct pkg_list *id;
160   for (id= ignoredependss; id; id= id->next)
161     if (id->pkg == pkg)
162       return true;
163   return false;
164 }
165
166 static bool
167 ignore_depends_possi(struct deppossi *possi)
168 {
169   struct deppossi_pkg_iterator *possi_iter;
170   struct pkginfo *pkg;
171
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);
176       return true;
177     }
178   }
179   deppossi_pkg_iter_free(possi_iter);
180
181   return false;
182 }
183
184 bool
185 force_depends(struct deppossi *possi)
186 {
187   return fc_depends ||
188          ignore_depends_possi(possi) ||
189          ignore_depends(possi->up->up);
190 }
191
192 bool
193 force_breaks(struct deppossi *possi)
194 {
195   return fc_breaks ||
196          ignore_depends_possi(possi) ||
197          ignore_depends(possi->up->up);
198 }
199
200 bool
201 force_conflicts(struct deppossi *possi)
202 {
203   return fc_conflicts;
204 }
205
206 void clear_istobes(void) {
207   struct pkgiterator *iter;
208   struct pkginfo *pkg;
209
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;
215   }
216   pkg_db_iter_free(iter);
217 }
218
219 /*
220  * Returns true if the directory contains conffiles belonging to pkg,
221  * false otherwise.
222  */
223 bool
224 dir_has_conffiles(struct filenamenode *file, struct pkginfo *pkg)
225 {
226   struct conffile *conff;
227   size_t namelen;
228
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) {
233       if (conff->obsolete)
234         continue;
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));
239         return true;
240       }
241   }
242   debug(dbg_veryverbose, "dir_has_conffiles no");
243   return false;
244 }
245
246 /*
247  * Returns true if the file is used by packages other than pkg,
248  * false otherwise.
249  */
250 bool
251 dir_is_used_by_others(struct filenamenode *file, struct pkginfo *pkg)
252 {
253   struct filepackages_iterator *iter;
254   struct pkginfo *other_pkg;
255
256   debug(dbg_veryverbose, "dir_is_used_by_others '%s' (except %s)", file->name,
257         pkg ? pkg_name(pkg, pnaw_always) : "<none>");
258
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)
264       continue;
265
266     filepackages_iter_free(iter);
267     debug(dbg_veryverbose, "dir_is_used_by_others yes");
268     return true;
269   }
270   filepackages_iter_free(iter);
271
272   debug(dbg_veryverbose, "dir_is_used_by_others no");
273   return false;
274 }
275
276 /*
277  * Returns true if the file is used by pkg, false otherwise.
278  */
279 bool
280 dir_is_used_by_pkg(struct filenamenode *file, struct pkginfo *pkg,
281                    struct fileinlist *list)
282 {
283   struct fileinlist *node;
284   size_t namelen;
285
286   debug(dbg_veryverbose, "dir_is_used_by_pkg '%s' (by %s)",
287         file->name, pkg ? pkg_name(pkg, pnaw_always) : "<none>");
288
289   namelen = strlen(file->name);
290
291   for (node = list; node; node = node->next) {
292     debug(dbg_veryverbose, "dir_is_used_by_pkg considering %s ...",
293           node->namenode->name);
294
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");
299       return true;
300     }
301   }
302
303   debug(dbg_veryverbose, "dir_is_used_by_pkg no");
304
305   return false;
306 }
307
308 /**
309  * Mark a conffile as obsolete.
310  *
311  * @param pkg           The package owning the conffile.
312  * @param namenode      The namenode for the obsolete conffile.
313  */
314 void
315 conffile_mark_obsolete(struct pkginfo *pkg, struct filenamenode *namenode)
316 {
317   struct conffile *conff;
318
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;
324       return;
325     }
326   }
327 }
328
329 /**
330  * Mark all package conffiles as old.
331  *
332  * @param pkg           The package owning the conffiles.
333  */
334 void
335 pkg_conffiles_mark_old(struct pkginfo *pkg)
336 {
337   const struct conffile *conff;
338   struct filenamenode *namenode;
339
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);
347   }
348 }
349
350 void
351 log_action(const char *action, struct pkginfo *pkg, struct pkgbin *pkgbin)
352 {
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));
358 }