XBPS Library API  0.19
The X Binary Package System
rindex_get.c
1 /*-
2  * Copyright (c) 2012 Juan Romero Pardines.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <stdio.h>
27 #include <stdbool.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 
32 #include "xbps_api_impl.h"
33 
34 /**
35  * @file lib/rindex_get.c
36  * @brief Repository index functions
37  * @defgroup rindex Repository index functions
38  */
39 static prop_dictionary_t
40 match_pkg_by_pkgver(prop_dictionary_t repod, const char *p)
41 {
42  prop_dictionary_t d = NULL;
43  const char *pkgver;
44  char *pkgname;
45 
46  /* exact match by pkgver */
47  if ((pkgname = xbps_pkg_name(p)) == NULL)
48  return NULL;
49 
50  d = prop_dictionary_get(repod, pkgname);
51  if (d) {
52  prop_dictionary_get_cstring_nocopy(d, "pkgver", &pkgver);
53  if (strcmp(pkgver, p))
54  d = NULL;
55  }
56 
57  free(pkgname);
58  return d;
59 }
60 
61 static prop_dictionary_t
62 match_pkg_by_pattern(prop_dictionary_t repod, const char *p)
63 {
64  prop_dictionary_t d = NULL;
65  const char *pkgver;
66  char *pkgname;
67 
68  /* match by pkgpattern in pkgver */
69  if ((pkgname = xbps_pkgpattern_name(p)) == NULL) {
70  if ((pkgname = xbps_pkg_name(p)))
71  return match_pkg_by_pkgver(repod, p);
72 
73  return NULL;
74  }
75 
76  d = prop_dictionary_get(repod, pkgname);
77  if (d) {
78  prop_dictionary_get_cstring_nocopy(d, "pkgver", &pkgver);
79  assert(pkgver);
80  if (!xbps_pkgpattern_match(pkgver, p))
81  d = NULL;
82  }
83 
84  free(pkgname);
85  return d;
86 }
87 
88 const char HIDDEN *
89 vpkg_user_conf(struct xbps_handle *xhp,
90  const char *vpkg,
91  bool bypattern)
92 {
93  const char *vpkgver, *pkg = NULL;
94  char *vpkgname = NULL, *tmp;
95  size_t i, j, cnt;
96 
97  if (xhp->cfg == NULL)
98  return NULL;
99 
100  if ((cnt = cfg_size(xhp->cfg, "virtual-package")) == 0) {
101  /* no virtual packages configured */
102  return NULL;
103  }
104 
105  for (i = 0; i < cnt; i++) {
106  cfg_t *sec = cfg_getnsec(xhp->cfg, "virtual-package", i);
107  for (j = 0; j < cfg_size(sec, "targets"); j++) {
108  tmp = NULL;
109  vpkgver = cfg_getnstr(sec, "targets", j);
110  if (strchr(vpkgver, '_') == NULL) {
111  tmp = xbps_xasprintf("%s_1", vpkgver);
112  vpkgname = xbps_pkg_name(tmp);
113  free(tmp);
114  } else {
115  vpkgname = xbps_pkg_name(vpkgver);
116  }
117  if (vpkgname == NULL)
118  break;
119  if (bypattern) {
120  if (!xbps_pkgpattern_match(vpkgver, vpkg)) {
121  free(vpkgname);
122  continue;
123  }
124  } else {
125  if (strcmp(vpkg, vpkgname)) {
126  free(vpkgname);
127  continue;
128  }
129  }
130  /* virtual package matched in conffile */
131  pkg = cfg_title(sec);
132  xbps_dbg_printf(xhp,
133  "matched vpkg in conf `%s' for %s\n",
134  pkg, vpkg);
135  free(vpkgname);
136  break;
137  }
138  }
139  return pkg;
140 }
141 
142 prop_dictionary_t
143 xbps_rindex_get_virtualpkg(struct xbps_rindex *rpi, const char *pkg)
144 {
145  prop_object_t obj;
146  prop_object_iterator_t iter;
147  prop_dictionary_t pkgd = NULL;
148  const char *vpkg;
149  bool found = false, bypattern = false;
150 
151  if (xbps_pkgpattern_version(pkg))
152  bypattern = true;
153 
154  /* Try matching vpkg from configuration files */
155  vpkg = vpkg_user_conf(rpi->xhp, pkg, bypattern);
156  if (vpkg != NULL) {
157  if (xbps_pkgpattern_version(vpkg))
158  pkgd = match_pkg_by_pattern(rpi->repod, vpkg);
159  else if (xbps_pkg_version(vpkg))
160  pkgd = match_pkg_by_pkgver(rpi->repod, vpkg);
161  else
162  pkgd = prop_dictionary_get(rpi->repod, vpkg);
163 
164  if (pkgd) {
165  found = true;
166  goto out;
167  }
168  }
169 
170  /* ... otherwise match the first one in dictionary */
171  iter = prop_dictionary_iterator(rpi->repod);
172  assert(iter);
173 
174  while ((obj = prop_object_iterator_next(iter))) {
175  pkgd = prop_dictionary_get_keysym(rpi->repod, obj);
176  if (xbps_match_virtual_pkg_in_dict(pkgd, pkg, bypattern)) {
177  found = true;
178  break;
179  }
180  }
181  prop_object_iterator_release(iter);
182 
183 out:
184  if (found) {
185  prop_dictionary_set_cstring_nocopy(pkgd,
186  "repository", rpi->uri);
187  return pkgd;
188  }
189  return NULL;
190 }
191 
192 prop_dictionary_t
193 xbps_rindex_get_pkg(struct xbps_rindex *rpi, const char *pkg)
194 {
195  prop_dictionary_t pkgd = NULL;
196 
197  if (xbps_pkgpattern_version(pkg))
198  pkgd = match_pkg_by_pattern(rpi->repod, pkg);
199  else if (xbps_pkg_version(pkg))
200  pkgd = match_pkg_by_pkgver(rpi->repod, pkg);
201  else
202  pkgd = prop_dictionary_get(rpi->repod, pkg);
203 
204  if (pkgd) {
205  prop_dictionary_set_cstring_nocopy(pkgd,
206  "repository", rpi->uri);
207  return pkgd;
208  }
209 
210  return NULL;
211 }