pkg.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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-2012 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 <http://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <assert.h>
  24. #include <string.h>
  25. #include <dpkg/dpkg-db.h>
  26. #include <dpkg/pkg.h>
  27. /**
  28. * Set the package installation status.
  29. */
  30. void
  31. pkg_set_status(struct pkginfo *pkg, enum pkgstatus status)
  32. {
  33. if (pkg->status == status)
  34. return;
  35. else if (pkg->status == stat_notinstalled)
  36. pkg->set->installed_instances++;
  37. else if (status == stat_notinstalled)
  38. pkg->set->installed_instances--;
  39. assert(pkg->set->installed_instances >= 0);
  40. pkg->status = status;
  41. }
  42. /**
  43. * Set the specified flags to 1 in the package error flags.
  44. */
  45. void
  46. pkg_set_eflags(struct pkginfo *pkg, enum pkgeflag eflag)
  47. {
  48. pkg->eflag |= eflag;
  49. }
  50. /**
  51. * Clear the specified flags to 0 in the package error flags.
  52. */
  53. void
  54. pkg_clear_eflags(struct pkginfo *pkg, enum pkgeflag eflag)
  55. {
  56. pkg->eflag &= ~eflag;
  57. }
  58. /**
  59. * Reset the package error flags to 0.
  60. */
  61. void
  62. pkg_reset_eflags(struct pkginfo *pkg)
  63. {
  64. pkg->eflag = eflag_ok;
  65. }
  66. /**
  67. * Copy the package error flags to another package.
  68. */
  69. void
  70. pkg_copy_eflags(struct pkginfo *pkg_dst, struct pkginfo *pkg_src)
  71. {
  72. pkg_dst->eflag = pkg_src->eflag;
  73. }
  74. /**
  75. * Set the package selection status.
  76. */
  77. void
  78. pkg_set_want(struct pkginfo *pkg, enum pkgwant want)
  79. {
  80. pkg->want = want;
  81. }
  82. void
  83. pkgbin_blank(struct pkgbin *pkgbin)
  84. {
  85. pkgbin->essential = false;
  86. pkgbin->multiarch = multiarch_no;
  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(arch_none);
  121. pkg->available.arch = dpkg_arch_get(arch_none);
  122. }
  123. void
  124. pkgset_blank(struct pkgset *set)
  125. {
  126. set->name = NULL;
  127. set->depended.available = NULL;
  128. set->depended.installed = NULL;
  129. pkg_blank(&set->pkg);
  130. set->installed_instances = 0;
  131. set->pkg.set = set;
  132. set->pkg.arch_next = NULL;
  133. }
  134. /**
  135. * Link a pkginfo instance into a package set.
  136. *
  137. * @param set The package set to use.
  138. * @param pkg The package to link into the set.
  139. */
  140. void
  141. pkgset_link_pkg(struct pkgset *set, struct pkginfo *pkg)
  142. {
  143. pkg->set = set;
  144. pkg->arch_next = set->pkg.arch_next;
  145. set->pkg.arch_next = pkg;
  146. }
  147. /**
  148. * Get the number of installed package instances in a package set.
  149. *
  150. * @param set The package set to use.
  151. *
  152. * @return The count of installed packages.
  153. */
  154. int
  155. pkgset_installed_instances(struct pkgset *set)
  156. {
  157. return set->installed_instances;
  158. }
  159. static int
  160. nes(const char *s)
  161. {
  162. return s && *s;
  163. }
  164. /**
  165. * Check if a pkg is informative.
  166. *
  167. * Used by dselect and dpkg query options as an aid to decide whether to
  168. * display things, and by dump to decide whether to write them out.
  169. */
  170. bool
  171. pkg_is_informative(struct pkginfo *pkg, struct pkgbin *pkgbin)
  172. {
  173. /* We ignore Section and Priority, as these tend to hang around. */
  174. if (pkgbin == &pkg->installed &&
  175. (pkg->want != want_unknown ||
  176. pkg->eflag != eflag_ok ||
  177. pkg->status != stat_notinstalled ||
  178. informativeversion(&pkg->configversion)))
  179. return true;
  180. if (pkgbin->depends ||
  181. nes(pkgbin->description) ||
  182. nes(pkgbin->maintainer) ||
  183. nes(pkgbin->origin) ||
  184. nes(pkgbin->bugs) ||
  185. nes(pkgbin->installedsize) ||
  186. nes(pkgbin->source) ||
  187. informativeversion(&pkgbin->version) ||
  188. pkgbin->conffiles ||
  189. pkgbin->arbs)
  190. return true;
  191. return false;
  192. }