pkg.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * pkg.c - primitives for pkg handling
  4. *
  5. * Copyright © 1995, 1996 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2009-2014 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 <assert.h>
  24. #include <string.h>
  25. #include <dpkg/string.h>
  26. #include <dpkg/dpkg-db.h>
  27. #include <dpkg/pkg.h>
  28. /**
  29. * Set the package installation status.
  30. */
  31. void
  32. pkg_set_status(struct pkginfo *pkg, enum pkgstatus status)
  33. {
  34. if (pkg->status == status)
  35. return;
  36. else if (pkg->status == stat_notinstalled)
  37. pkg->set->installed_instances++;
  38. else if (status == stat_notinstalled)
  39. pkg->set->installed_instances--;
  40. assert(pkg->set->installed_instances >= 0);
  41. pkg->status = status;
  42. }
  43. /**
  44. * Set the specified flags to 1 in the package error flags.
  45. */
  46. void
  47. pkg_set_eflags(struct pkginfo *pkg, enum pkgeflag eflag)
  48. {
  49. pkg->eflag |= eflag;
  50. }
  51. /**
  52. * Clear the specified flags to 0 in the package error flags.
  53. */
  54. void
  55. pkg_clear_eflags(struct pkginfo *pkg, enum pkgeflag eflag)
  56. {
  57. pkg->eflag &= ~eflag;
  58. }
  59. /**
  60. * Reset the package error flags to 0.
  61. */
  62. void
  63. pkg_reset_eflags(struct pkginfo *pkg)
  64. {
  65. pkg->eflag = eflag_ok;
  66. }
  67. /**
  68. * Copy the package error flags to another package.
  69. */
  70. void
  71. pkg_copy_eflags(struct pkginfo *pkg_dst, struct pkginfo *pkg_src)
  72. {
  73. pkg_dst->eflag = pkg_src->eflag;
  74. }
  75. /**
  76. * Set the package selection status.
  77. */
  78. void
  79. pkg_set_want(struct pkginfo *pkg, enum pkgwant want)
  80. {
  81. pkg->want = want;
  82. }
  83. void
  84. pkgbin_blank(struct pkgbin *pkgbin)
  85. {
  86. pkgbin->essential = false;
  87. pkgbin->depends = NULL;
  88. pkgbin->pkgname_archqual = NULL;
  89. pkgbin->description = NULL;
  90. pkgbin->maintainer = NULL;
  91. pkgbin->source = NULL;
  92. pkgbin->installedsize = NULL;
  93. pkgbin->bugs = NULL;
  94. pkgbin->origin = NULL;
  95. dpkg_version_blank(&pkgbin->version);
  96. pkgbin->conffiles = NULL;
  97. pkgbin->arbs = NULL;
  98. }
  99. void
  100. pkg_blank(struct pkginfo *pkg)
  101. {
  102. pkg->status = stat_notinstalled;
  103. pkg->eflag = eflag_ok;
  104. pkg->want = want_unknown;
  105. pkg->priority = pri_unknown;
  106. pkg->otherpriority = NULL;
  107. pkg->section = NULL;
  108. dpkg_version_blank(&pkg->configversion);
  109. pkg->files = NULL;
  110. pkg->clientdata = NULL;
  111. pkg->trigaw.head = NULL;
  112. pkg->trigaw.tail = NULL;
  113. pkg->othertrigaw_head = NULL;
  114. pkg->trigpend_head = NULL;
  115. pkgbin_blank(&pkg->installed);
  116. pkgbin_blank(&pkg->available);
  117. /* The architectures are reset here (instead of in pkgbin_blank),
  118. * because they are part of the package specification, and needed
  119. * for selections. */
  120. pkg->installed.arch = dpkg_arch_get(DPKG_ARCH_NONE);
  121. pkg->installed.multiarch = PKG_MULTIARCH_NO;
  122. pkg->available.arch = dpkg_arch_get(DPKG_ARCH_NONE);
  123. pkg->available.multiarch = PKG_MULTIARCH_NO;
  124. }
  125. void
  126. pkgset_blank(struct pkgset *set)
  127. {
  128. set->name = NULL;
  129. set->depended.available = NULL;
  130. set->depended.installed = NULL;
  131. pkg_blank(&set->pkg);
  132. set->installed_instances = 0;
  133. set->pkg.set = set;
  134. set->pkg.arch_next = NULL;
  135. }
  136. /**
  137. * Link a pkginfo instance into a package set.
  138. *
  139. * @param set The package set to use.
  140. * @param pkg The package to link into the set.
  141. */
  142. void
  143. pkgset_link_pkg(struct pkgset *set, struct pkginfo *pkg)
  144. {
  145. pkg->set = set;
  146. pkg->arch_next = set->pkg.arch_next;
  147. set->pkg.arch_next = pkg;
  148. }
  149. /**
  150. * Get the number of installed package instances in a package set.
  151. *
  152. * @param set The package set to use.
  153. *
  154. * @return The count of installed packages.
  155. */
  156. int
  157. pkgset_installed_instances(struct pkgset *set)
  158. {
  159. return set->installed_instances;
  160. }
  161. /**
  162. * Check if a pkg is informative.
  163. *
  164. * Used by dselect and dpkg query options as an aid to decide whether to
  165. * display things, and by dump to decide whether to write them out.
  166. */
  167. bool
  168. pkg_is_informative(struct pkginfo *pkg, struct pkgbin *pkgbin)
  169. {
  170. /* We ignore Section and Priority, as these tend to hang around. */
  171. if (pkgbin == &pkg->installed &&
  172. (pkg->want != want_unknown ||
  173. pkg->eflag != eflag_ok ||
  174. pkg->status != stat_notinstalled ||
  175. dpkg_version_is_informative(&pkg->configversion)))
  176. return true;
  177. if (pkgbin->depends ||
  178. str_is_set(pkgbin->description) ||
  179. str_is_set(pkgbin->maintainer) ||
  180. str_is_set(pkgbin->origin) ||
  181. str_is_set(pkgbin->bugs) ||
  182. str_is_set(pkgbin->installedsize) ||
  183. str_is_set(pkgbin->source) ||
  184. dpkg_version_is_informative(&pkgbin->version) ||
  185. pkgbin->conffiles ||
  186. pkgbin->arbs)
  187. return true;
  188. return false;
  189. }