XBPS Library API  0.19
The X Binary Package System
package_state.c
1 /*-
2  * Copyright (c) 2009-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 struct state {
35  const char *string;
36  pkg_state_t number;
37 };
38 
39 static const struct state states[] = {
40  { "unpacked", XBPS_PKG_STATE_UNPACKED },
41  { "installed", XBPS_PKG_STATE_INSTALLED },
42  { "broken", XBPS_PKG_STATE_BROKEN },
43  { "half-removed", XBPS_PKG_STATE_HALF_REMOVED },
44  { "not-installed", XBPS_PKG_STATE_NOT_INSTALLED },
45  { NULL, 0 }
46 };
47 
48 
49 /**
50  * @file lib/package_state.c
51  * @brief Package state handling routines
52  * @defgroup pkgstates Package state handling functions
53  */
54 
55 static int
56 set_new_state(prop_dictionary_t dict, pkg_state_t state)
57 {
58  const struct state *stp;
59 
60  assert(prop_object_type(dict) == PROP_TYPE_DICTIONARY);
61 
62  for (stp = states; stp->string != NULL; stp++)
63  if (state == stp->number)
64  break;
65 
66  if (stp->string == NULL)
67  return EINVAL;
68 
69  if (!prop_dictionary_set_cstring_nocopy(dict, "state", stp->string))
70  return EINVAL;
71 
72  return 0;
73 }
74 
75 static pkg_state_t
76 get_state(prop_dictionary_t dict)
77 {
78  const struct state *stp;
79  const char *state_str;
80 
81  assert(prop_object_type(dict) == PROP_TYPE_DICTIONARY);
82 
83  if (!prop_dictionary_get_cstring_nocopy(dict,
84  "state", &state_str))
85  return 0;
86 
87  for (stp = states; stp->string != NULL; stp++)
88  if (strcmp(state_str, stp->string) == 0)
89  break;
90 
91  return stp->number;
92 }
93 
94 int
96  const char *pkgname,
97  pkg_state_t *state)
98 {
99  prop_dictionary_t pkgd;
100 
101  assert(pkgname != NULL);
102  assert(state != NULL);
103 
104  pkgd = xbps_pkgdb_get_pkg(xhp, pkgname);
105  if (pkgd == NULL)
106  return ENOENT;
107 
108  *state = get_state(pkgd);
109  if (*state == 0)
110  return EINVAL;
111 
112  return 0;
113 }
114 
115 int
116 xbps_pkg_state_dictionary(prop_dictionary_t dict, pkg_state_t *state)
117 {
118  assert(prop_object_type(dict) == PROP_TYPE_DICTIONARY);
119  assert(state != NULL);
120 
121  if ((*state = get_state(dict)) == 0)
122  return EINVAL;
123 
124  return 0;
125 }
126 
127 int
128 xbps_set_pkg_state_dictionary(prop_dictionary_t dict, pkg_state_t state)
129 {
130  assert(prop_object_type(dict) == PROP_TYPE_DICTIONARY);
131 
132  return set_new_state(dict, state);
133 }
134 
135 static int
136 set_pkg_objs(prop_dictionary_t pkgd, const char *name, const char *version)
137 {
138  char *pkgver;
139 
140  if (!prop_dictionary_set_cstring_nocopy(pkgd, "pkgname", name))
141  return EINVAL;
142 
143  if (!prop_dictionary_set_cstring_nocopy(pkgd, "version", version))
144  return EINVAL;
145 
146  pkgver = xbps_xasprintf("%s-%s", name, version);
147  assert(pkgver != NULL);
148 
149  if (!prop_dictionary_set_cstring_nocopy(pkgd, "pkgver", pkgver)) {
150  free(pkgver);
151  return EINVAL;
152  }
153  free(pkgver);
154 
155  return 0;
156 }
157 
158 int
160  const char *pkgname,
161  const char *version,
162  pkg_state_t state)
163 {
164  prop_dictionary_t pkgd;
165  int rv = 0;
166 
167  assert(pkgname != NULL);
168 
169  pkgd = xbps_pkgdb_get_pkg(xhp, pkgname);
170  if (pkgd == NULL) {
171  pkgd = prop_dictionary_create();
172  if (pkgd == NULL)
173  return ENOMEM;
174 
175  if ((rv = set_pkg_objs(pkgd, pkgname, version)) != 0) {
176  prop_object_release(pkgd);
177  return rv;
178  }
179  if ((rv = set_new_state(pkgd, state)) != 0) {
180  prop_object_release(pkgd);
181  return rv;
182  }
183  if (!xbps_add_obj_to_array(xhp->pkgdb, pkgd)) {
184  prop_object_release(pkgd);
185  return EINVAL;
186  }
187  } else {
188  if ((rv = set_new_state(pkgd, state)) != 0)
189  return rv;
190 
191  if ((rv = xbps_array_replace_dict_by_name(xhp->pkgdb,
192  pkgd, pkgname)) != 0)
193  return rv;
194  }
195 
196  return rv;
197 }