XBPS Library API 20260214
The X Binary Package System
transaction_check_replaces.c
1/*-
2 * Copyright (c) 2011-2020 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#include <unistd.h>
32#include <libgen.h>
33
34#include "xbps_api_impl.h"
35
36/*
37 * Processes the array of pkg dictionaries in "pkgs" to
38 * find matching package replacements via "replaces" pkg obj.
39 *
40 * This array contains the unordered list of packages in
41 * the transaction dictionary.
42 */
43bool HIDDEN
44xbps_transaction_check_replaces(struct xbps_handle *xhp, xbps_array_t pkgs)
45{
46 assert(xhp);
47 assert(pkgs);
48
49 for (unsigned int i = 0; i < xbps_array_count(pkgs); i++) {
50 xbps_array_t replaces;
51 xbps_object_t obj;
52 xbps_object_iterator_t iter;
53 xbps_dictionary_t instd, reppkgd;
54 const char *pkgver = NULL;
55 char pkgname[XBPS_NAME_SIZE] = {0};
56
57 obj = xbps_array_get(pkgs, i);
58 replaces = xbps_dictionary_get(obj, "replaces");
59 if (replaces == NULL || xbps_array_count(replaces) == 0)
60 continue;
61
62 if (!xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver)) {
63 return false;
64 }
65 if (!xbps_pkg_name(pkgname, XBPS_NAME_SIZE, pkgver)) {
66 return false;
67 }
68
69 iter = xbps_array_iterator(replaces);
70 assert(iter);
71
72 for (unsigned int j = 0; j < xbps_array_count(replaces); j++) {
73 const char *curpkgver = NULL, *pattern = NULL;
74 char curpkgname[XBPS_NAME_SIZE] = {0};
75 bool instd_auto = false, hold = false;
77
78 if(!xbps_array_get_cstring_nocopy(replaces, j, &pattern))
79 abort();
80
81 /*
82 * Find the installed package that matches the pattern
83 * to be replaced. Also check if the package would be
84 * installed in the transaction.
85 */
86 if (((instd = xbps_pkgdb_get_pkg(xhp, pattern)) == NULL) &&
87 ((instd = xbps_pkgdb_get_virtualpkg(xhp, pattern)) == NULL) &&
88 ((instd = xbps_find_pkg_in_array(pkgs, pattern, XBPS_TRANS_INSTALL)) == NULL))
89 continue;
90
91 if (!xbps_dictionary_get_cstring_nocopy(instd, "pkgver", &curpkgver)) {
92 xbps_object_iterator_release(iter);
93 return false;
94 }
95 /* ignore pkgs on hold mode */
96 if (xbps_dictionary_get_bool(instd, "hold", &hold) && hold)
97 continue;
98
99 if (!xbps_pkg_name(curpkgname, XBPS_NAME_SIZE, curpkgver)) {
100 xbps_object_iterator_release(iter);
101 return false;
102 }
103 /*
104 * Check that we are not replacing the same package,
105 * due to virtual packages.
106 */
107 if (strcmp(pkgname, curpkgname) == 0) {
108 continue;
109 }
110 /*
111 * Make sure to not add duplicates.
112 */
113 xbps_dictionary_get_bool(instd, "automatic-install", &instd_auto);
114 reppkgd = xbps_find_pkg_in_array(pkgs, curpkgname, 0);
115 if (reppkgd) {
116 ttype = xbps_transaction_pkg_type(reppkgd);
117 if (ttype == XBPS_TRANS_REMOVE || ttype == XBPS_TRANS_HOLD)
118 continue;
119 if (!xbps_dictionary_get_cstring_nocopy(reppkgd,
120 "pkgver", &curpkgver)) {
121 xbps_object_iterator_release(iter);
122 return false;
123 }
124 if (!xbps_match_virtual_pkg_in_dict(reppkgd, pattern) &&
125 !xbps_pkgpattern_match(curpkgver, pattern))
126 continue;
127 /*
128 * Package contains replaces="pkgpattern", but the
129 * package that should be replaced is also in the
130 * transaction and it's going to be updated.
131 */
132 if (!instd_auto) {
133 xbps_dictionary_remove(obj, "automatic-install");
134 }
135 if (!xbps_dictionary_set_bool(reppkgd, "replaced", true)) {
136 xbps_object_iterator_release(iter);
137 return false;
138 }
139 if (!xbps_transaction_pkg_type_set(reppkgd, XBPS_TRANS_REMOVE)) {
140 xbps_object_iterator_release(iter);
141 return false;
142 }
143 if (xbps_array_replace_dict_by_name(pkgs, reppkgd, curpkgname) != 0) {
144 xbps_object_iterator_release(iter);
145 return false;
146 }
147 xbps_verbose_printf("Package `%s' will be replaced by `%s'\n", curpkgver, pkgver);
149 "Package `%s' in transaction will be "
150 "replaced by `%s', matched with `%s'\n",
151 curpkgver, pkgver, pattern);
152 continue;
153 }
154 /*
155 * If new package is providing a virtual package to the
156 * package that we want to replace we should respect
157 * the automatic-install object.
158 */
159 if (xbps_match_virtual_pkg_in_dict(obj, pattern)) {
160 if (!instd_auto) {
161 xbps_dictionary_remove(obj, "automatic-install");
162 }
163 }
164 /*
165 * Add package dictionary into the transaction and mark
166 * it as to be "removed".
167 */
168 if (!xbps_transaction_pkg_type_set(instd, XBPS_TRANS_REMOVE)) {
169 xbps_object_iterator_release(iter);
170 return false;
171 }
172 if (!xbps_dictionary_set_bool(instd, "replaced", true)) {
173 xbps_object_iterator_release(iter);
174 return false;
175 }
176 if (!xbps_array_add_first(pkgs, instd)) {
177 xbps_object_iterator_release(iter);
178 return false;
179 }
180 xbps_verbose_printf("Package `%s' will be replaced by `%s'\n", curpkgver, pkgver);
182 "Package `%s' will be replaced by `%s', "
183 "matched with `%s'\n", curpkgver, pkgver, pattern);
184 }
185 xbps_object_iterator_release(iter);
186 }
187
188 return true;
189}
Generic XBPS structure handler for initialization.
Definition xbps.h:560
void xbps_verbose_printf(const char *,...)
Prints messages to stderr if verbosity is enabled.
Definition log.c:80
void xbps_dbg_printf(const char *fmt,...)
Prints debug messages to stderr.
Definition log.c:67
xbps_dictionary_t xbps_pkgdb_get_virtualpkg(struct xbps_handle *xhp, const char *pkg)
Definition pkgdb.c:422
xbps_dictionary_t xbps_pkgdb_get_pkg(struct xbps_handle *xhp, const char *pkg)
Definition pkgdb.c:408
bool xbps_match_virtual_pkg_in_dict(xbps_dictionary_t pkgd, const char *str)
Definition plist_match.c:58
xbps_trans_type_t xbps_transaction_pkg_type(xbps_dictionary_t pkg_repod)
xbps_trans_type_t
Definition xbps.h:1390
bool xbps_transaction_pkg_type_set(xbps_dictionary_t pkg_repod, xbps_trans_type_t type)
bool xbps_pkg_name(char *dst, size_t len, const char *pkg)
Definition util.c:249
int xbps_pkgpattern_match(const char *pkgver, const char *pattern)
Definition util.c:526