XBPS Library API  0.19
The X Binary Package System
plist_find.c
1 /*-
2  * Copyright (c) 2008-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 static prop_dictionary_t
35 get_pkg_in_array(prop_array_t array, const char *str, bool virtual)
36 {
37  prop_object_t obj = NULL;
38  prop_object_iterator_t iter;
39  const char *pkgver, *dpkgn;
40  bool found = false;
41 
42  iter = prop_array_iterator(array);
43  assert(iter);
44 
45  while ((obj = prop_object_iterator_next(iter))) {
46  if (virtual) {
47  /*
48  * Check if package pattern matches
49  * any virtual package version in dictionary.
50  */
51  if (xbps_pkgpattern_version(str))
52  found = xbps_match_virtual_pkg_in_dict(obj, str, true);
53  else
54  found = xbps_match_virtual_pkg_in_dict(obj, str, false);
55 
56  if (found)
57  break;
58  } else if (xbps_pkgpattern_version(str)) {
59  /* ,atch by pattern against pkgver */
60  if (!prop_dictionary_get_cstring_nocopy(obj,
61  "pkgver", &pkgver))
62  continue;
63  if (xbps_pkgpattern_match(pkgver, str)) {
64  found = true;
65  break;
66  }
67  } else if (xbps_pkg_version(str)) {
68  /* match by exact pkgver */
69  if (!prop_dictionary_get_cstring_nocopy(obj,
70  "pkgver", &pkgver))
71  continue;
72  if (strcmp(str, pkgver) == 0) {
73  found = true;
74  break;
75  }
76  } else {
77  /* match by pkgname */
78  if (!prop_dictionary_get_cstring_nocopy(obj,
79  "pkgname", &dpkgn))
80  continue;
81  if (strcmp(dpkgn, str) == 0) {
82  found = true;
83  break;
84  }
85  }
86  }
87  prop_object_iterator_release(iter);
88 
89  return found ? obj : NULL;
90 }
91 
92 prop_dictionary_t HIDDEN
93 xbps_find_pkg_in_array(prop_array_t a, const char *s)
94 {
95  assert(prop_object_type(a) == PROP_TYPE_ARRAY);
96  assert(s);
97 
98  return get_pkg_in_array(a, s, false);
99 }
100 
101 prop_dictionary_t HIDDEN
102 xbps_find_virtualpkg_in_array(struct xbps_handle *x,
103  prop_array_t a,
104  const char *s)
105 {
106  prop_dictionary_t pkgd;
107  const char *vpkg;
108  bool bypattern = false;
109 
110  assert(x);
111  assert(prop_object_type(a) == PROP_TYPE_ARRAY);
112  assert(s);
113 
115  bypattern = true;
116 
117  if ((vpkg = vpkg_user_conf(x, s, bypattern))) {
118  if ((pkgd = get_pkg_in_array(a, vpkg, true)))
119  return pkgd;
120  }
121 
122  return get_pkg_in_array(a, s, true);
123 }