help.c 9.5 KB

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