help.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <stdlib.h>
  29. #include <dpkg/i18n.h>
  30. #include <dpkg/dpkg.h>
  31. #include <dpkg/dpkg-db.h>
  32. #include <dpkg/path.h>
  33. #include "filesdb.h"
  34. #include "main.h"
  35. const char *const statusstrings[]= {
  36. [PKG_STAT_NOTINSTALLED] = N_("not installed"),
  37. [PKG_STAT_CONFIGFILES] = N_("not installed but configs remain"),
  38. [PKG_STAT_HALFINSTALLED] = N_("broken due to failed removal or installation"),
  39. [PKG_STAT_UNPACKED] = N_("unpacked but not configured"),
  40. [PKG_STAT_HALFCONFIGURED] = N_("broken due to postinst failure"),
  41. [PKG_STAT_TRIGGERSAWAITED] = N_("awaiting trigger processing by another package"),
  42. [PKG_STAT_TRIGGERSPENDING] = N_("triggered"),
  43. [PKG_STAT_INSTALLED] = N_("installed")
  44. };
  45. struct filenamenode *
  46. namenodetouse(struct filenamenode *namenode, struct pkginfo *pkg,
  47. struct pkgbin *pkgbin)
  48. {
  49. struct filenamenode *r;
  50. if (!namenode->divert) {
  51. r = namenode;
  52. return r;
  53. }
  54. debug(dbg_eachfile, "namenodetouse namenode='%s' pkg=%s",
  55. namenode->name, pkgbin_name(pkg, pkgbin, pnaw_always));
  56. r=
  57. (namenode->divert->useinstead && namenode->divert->pkgset != pkg->set)
  58. ? namenode->divert->useinstead : namenode;
  59. debug(dbg_eachfile,
  60. "namenodetouse ... useinstead=%s camefrom=%s pkg=%s return %s",
  61. namenode->divert->useinstead ? namenode->divert->useinstead->name : "<none>",
  62. namenode->divert->camefrom ? namenode->divert->camefrom->name : "<none>",
  63. namenode->divert->pkgset ? namenode->divert->pkgset->name : "<none>",
  64. r->name);
  65. return r;
  66. }
  67. bool
  68. find_command(const char *prog)
  69. {
  70. struct varbuf filename = VARBUF_INIT;
  71. struct stat stab;
  72. const char *path_list;
  73. const char *path, *path_end;
  74. size_t path_len;
  75. path_list = getenv("PATH");
  76. if (!path_list)
  77. ohshit(_("PATH is not set"));
  78. for (path = path_list; path; path = path_end ? path_end + 1 : NULL) {
  79. path_end = strchr(path, ':');
  80. path_len = path_end ? (size_t)(path_end - path) : strlen(path);
  81. varbuf_reset(&filename);
  82. varbuf_add_buf(&filename, path, path_len);
  83. if (path_len)
  84. varbuf_add_char(&filename, '/');
  85. varbuf_add_str(&filename, prog);
  86. varbuf_end_str(&filename);
  87. if (stat(filename.buf, &stab) == 0 && (stab.st_mode & 0111)) {
  88. varbuf_destroy(&filename);
  89. return true;
  90. }
  91. }
  92. varbuf_destroy(&filename);
  93. return false;
  94. }
  95. /**
  96. * Verify that some programs can be found in the PATH.
  97. */
  98. void checkpath(void) {
  99. static const char *const prog_list[] = {
  100. DEFAULTSHELL,
  101. RM,
  102. TAR,
  103. DIFF,
  104. BACKEND,
  105. /* Mac OS X uses dyld (Mach-O) instead of ld.so (ELF), and does not have
  106. * an ldconfig. */
  107. #if defined(__APPLE__) && defined(__MACH__)
  108. "update_dyld_shared_cache",
  109. #else
  110. "ldconfig",
  111. #endif
  112. #if BUILD_START_STOP_DAEMON
  113. "start-stop-daemon",
  114. #endif
  115. NULL
  116. };
  117. const char *const *prog;
  118. int warned= 0;
  119. for (prog = prog_list; *prog; prog++) {
  120. if (!find_command(*prog)) {
  121. warning(_("'%s' not found in PATH or not executable"), *prog);
  122. warned++;
  123. }
  124. }
  125. if (warned)
  126. forcibleerr(fc_badpath,
  127. P_("%d expected program not found in PATH or not executable\n%s",
  128. "%d expected programs not found in PATH or not executable\n%s",
  129. warned),
  130. warned, _("Note: root's PATH should usually contain "
  131. "/usr/local/sbin, /usr/sbin and /sbin"));
  132. }
  133. bool
  134. ignore_depends(struct pkginfo *pkg)
  135. {
  136. struct pkg_list *id;
  137. for (id= ignoredependss; id; id= id->next)
  138. if (id->pkg == pkg)
  139. return true;
  140. return false;
  141. }
  142. static bool
  143. ignore_depends_possi(struct deppossi *possi)
  144. {
  145. struct deppossi_pkg_iterator *possi_iter;
  146. struct pkginfo *pkg;
  147. possi_iter = deppossi_pkg_iter_new(possi, wpb_installed);
  148. while ((pkg = deppossi_pkg_iter_next(possi_iter))) {
  149. if (ignore_depends(pkg)) {
  150. deppossi_pkg_iter_free(possi_iter);
  151. return true;
  152. }
  153. }
  154. deppossi_pkg_iter_free(possi_iter);
  155. return false;
  156. }
  157. bool
  158. force_depends(struct deppossi *possi)
  159. {
  160. return fc_depends ||
  161. ignore_depends_possi(possi) ||
  162. ignore_depends(possi->up->up);
  163. }
  164. bool
  165. force_breaks(struct deppossi *possi)
  166. {
  167. return fc_breaks ||
  168. ignore_depends_possi(possi) ||
  169. ignore_depends(possi->up->up);
  170. }
  171. bool
  172. force_conflicts(struct deppossi *possi)
  173. {
  174. return fc_conflicts;
  175. }
  176. void clear_istobes(void) {
  177. struct pkgiterator *iter;
  178. struct pkginfo *pkg;
  179. iter = pkg_db_iter_new();
  180. while ((pkg = pkg_db_iter_next_pkg(iter)) != NULL) {
  181. ensure_package_clientdata(pkg);
  182. pkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
  183. pkg->clientdata->replacingfilesandsaid= 0;
  184. }
  185. pkg_db_iter_free(iter);
  186. }
  187. /*
  188. * Returns true if the directory contains conffiles belonging to pkg,
  189. * false otherwise.
  190. */
  191. bool
  192. dir_has_conffiles(struct filenamenode *file, struct pkginfo *pkg)
  193. {
  194. struct conffile *conff;
  195. size_t namelen;
  196. debug(dbg_veryverbose, "dir_has_conffiles '%s' (from %s)", file->name,
  197. pkg_name(pkg, pnaw_always));
  198. namelen = strlen(file->name);
  199. for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
  200. if (conff->obsolete)
  201. continue;
  202. if (strncmp(file->name, conff->name, namelen) == 0 &&
  203. strlen(conff->name) > namelen && conff->name[namelen] == '/') {
  204. debug(dbg_veryverbose, "directory %s has conffile %s from %s",
  205. file->name, conff->name, pkg_name(pkg, pnaw_always));
  206. return true;
  207. }
  208. }
  209. debug(dbg_veryverbose, "dir_has_conffiles no");
  210. return false;
  211. }
  212. /*
  213. * Returns true if the file is used by packages other than pkg,
  214. * false otherwise.
  215. */
  216. bool
  217. dir_is_used_by_others(struct filenamenode *file, struct pkginfo *pkg)
  218. {
  219. struct filepackages_iterator *iter;
  220. struct pkginfo *other_pkg;
  221. debug(dbg_veryverbose, "dir_is_used_by_others '%s' (except %s)", file->name,
  222. pkg ? pkg_name(pkg, pnaw_always) : "<none>");
  223. iter = filepackages_iter_new(file);
  224. while ((other_pkg = filepackages_iter_next(iter))) {
  225. debug(dbg_veryverbose, "dir_is_used_by_others considering %s ...",
  226. pkg_name(other_pkg, pnaw_always));
  227. if (other_pkg == pkg)
  228. continue;
  229. filepackages_iter_free(iter);
  230. debug(dbg_veryverbose, "dir_is_used_by_others yes");
  231. return true;
  232. }
  233. filepackages_iter_free(iter);
  234. debug(dbg_veryverbose, "dir_is_used_by_others no");
  235. return false;
  236. }
  237. /*
  238. * Returns true if the file is used by pkg, false otherwise.
  239. */
  240. bool
  241. dir_is_used_by_pkg(struct filenamenode *file, struct pkginfo *pkg,
  242. struct fileinlist *list)
  243. {
  244. struct fileinlist *node;
  245. size_t namelen;
  246. debug(dbg_veryverbose, "dir_is_used_by_pkg '%s' (by %s)",
  247. file->name, pkg ? pkg_name(pkg, pnaw_always) : "<none>");
  248. namelen = strlen(file->name);
  249. for (node = list; node; node = node->next) {
  250. debug(dbg_veryverbose, "dir_is_used_by_pkg considering %s ...",
  251. node->namenode->name);
  252. if (strncmp(file->name, node->namenode->name, namelen) == 0 &&
  253. strlen(node->namenode->name) > namelen &&
  254. node->namenode->name[namelen] == '/') {
  255. debug(dbg_veryverbose, "dir_is_used_by_pkg yes");
  256. return true;
  257. }
  258. }
  259. debug(dbg_veryverbose, "dir_is_used_by_pkg no");
  260. return false;
  261. }
  262. /**
  263. * Mark a conffile as obsolete.
  264. *
  265. * @param pkg The package owning the conffile.
  266. * @param namenode The namenode for the obsolete conffile.
  267. */
  268. void
  269. conffile_mark_obsolete(struct pkginfo *pkg, struct filenamenode *namenode)
  270. {
  271. struct conffile *conff;
  272. for (conff = pkg->installed.conffiles; conff; conff = conff->next) {
  273. if (strcmp(conff->name, namenode->name) == 0) {
  274. debug(dbg_conff, "marking %s conffile %s as obsolete",
  275. pkg_name(pkg, pnaw_always), conff->name);
  276. conff->obsolete = true;
  277. return;
  278. }
  279. }
  280. }
  281. /**
  282. * Mark all package conffiles as old.
  283. *
  284. * @param pkg The package owning the conffiles.
  285. */
  286. void
  287. pkg_conffiles_mark_old(struct pkginfo *pkg)
  288. {
  289. const struct conffile *conff;
  290. struct filenamenode *namenode;
  291. for (conff = pkg->installed.conffiles; conff; conff = conff->next) {
  292. namenode = findnamenode(conff->name, 0); /* XXX */
  293. namenode->flags |= fnnf_old_conff;
  294. if (!namenode->oldhash)
  295. namenode->oldhash = conff->hash;
  296. debug(dbg_conffdetail, "%s '%s' namenode '%s' flags %o", __func__,
  297. conff->name, namenode->name, namenode->flags);
  298. }
  299. }
  300. void
  301. log_action(const char *action, struct pkginfo *pkg, struct pkgbin *pkgbin)
  302. {
  303. log_message("%s %s %s %s", action, pkgbin_name(pkg, pkgbin, pnaw_always),
  304. versiondescribe(&pkg->installed.version, vdew_nonambig),
  305. versiondescribe(&pkg->available.version, vdew_nonambig));
  306. statusfd_send("processing: %s: %s", action,
  307. pkgbin_name(pkg, pkgbin, pnaw_nonambig));
  308. }