help.c 9.5 KB

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