XBPS Library API 20240111
The X Binary Package System
package_state.c
1/*-
2 * Copyright (c) 2009-2013 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
34struct state {
35 const char *string;
36 pkg_state_t number;
37};
38
39static 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
55static int
56set_new_state(xbps_dictionary_t dict, pkg_state_t state)
57{
58 const struct state *stp;
59
60 assert(xbps_object_type(dict) == XBPS_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 (!xbps_dictionary_set_cstring_nocopy(dict, "state", stp->string))
70 return EINVAL;
71
72 return 0;
73}
74
75static pkg_state_t
76get_state(xbps_dictionary_t dict)
77{
78 const struct state *stp;
79 const char *state_str;
80
81 assert(xbps_object_type(dict) == XBPS_TYPE_DICTIONARY);
82
83 if (!xbps_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
94int
96 const char *pkgver,
97 pkg_state_t *state)
98{
99 xbps_dictionary_t pkgd;
100
101 assert(pkgver != NULL);
102 assert(state != NULL);
103
104 pkgd = xbps_pkgdb_get_pkg(xhp, pkgver);
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
115int
116xbps_pkg_state_dictionary(xbps_dictionary_t dict, pkg_state_t *state)
117{
118 assert(xbps_object_type(dict) == XBPS_TYPE_DICTIONARY);
119 assert(state != NULL);
120
121 if ((*state = get_state(dict)) == 0)
122 return EINVAL;
123
124 return 0;
125}
126
127int
128xbps_set_pkg_state_dictionary(xbps_dictionary_t dict, pkg_state_t state)
129{
130 assert(xbps_object_type(dict) == XBPS_TYPE_DICTIONARY);
131
132 return set_new_state(dict, state);
133}
134
135int
137 const char *pkgver,
138 pkg_state_t state)
139{
140 xbps_dictionary_t pkgd;
141 char pkgname[XBPS_NAME_SIZE];
142 int rv = 0;
143
144 assert(pkgver != NULL);
145
146 pkgd = xbps_pkgdb_get_pkg(xhp, pkgver);
147 if (pkgd == NULL) {
148 pkgd = xbps_dictionary_create();
149 if (pkgd == NULL)
150 return ENOMEM;
151
152 if (!xbps_dictionary_set_cstring_nocopy(pkgd,
153 "pkgver", pkgver)) {
154 xbps_object_release(pkgd);
155 return EINVAL;
156 }
157 if ((rv = set_new_state(pkgd, state)) != 0) {
158 xbps_object_release(pkgd);
159 return rv;
160 }
161 if (!xbps_pkg_name(pkgname, XBPS_NAME_SIZE, pkgver)) {
162 abort();
163 }
164 if (!xbps_dictionary_set(xhp->pkgdb, pkgname, pkgd)) {
165 xbps_object_release(pkgd);
166 return EINVAL;
167 }
168 xbps_object_release(pkgd);
169 } else {
170 if ((rv = set_new_state(pkgd, state)) != 0)
171 return rv;
172
173 if (!xbps_pkg_name(pkgname, XBPS_NAME_SIZE, pkgver)) {
174 abort();
175 }
176 if (!xbps_dictionary_set(xhp->pkgdb, pkgname, pkgd)) {
177 return EINVAL;
178 }
179 }
180
181 return rv;
182}
xbps_dictionary_t pkgdb
Definition xbps.h:576
Generic XBPS structure handler for initialization.
Definition xbps.h:550
xbps_dictionary_t xbps_pkgdb_get_pkg(struct xbps_handle *xhp, const char *pkg)
Definition pkgdb.c:379
int xbps_pkg_state_installed(struct xbps_handle *xhp, const char *pkgver, pkg_state_t *state)
int xbps_set_pkg_state_dictionary(xbps_dictionary_t dict, pkg_state_t state)
pkg_state_t
Definition xbps.h:1768
int xbps_set_pkg_state_installed(struct xbps_handle *xhp, const char *pkgver, pkg_state_t state)
int xbps_pkg_state_dictionary(xbps_dictionary_t dict, pkg_state_t *state)
bool xbps_pkg_name(char *dst, size_t len, const char *pkg)
Definition util.c:253