XBPS Library API 20250705
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.
84 */
85 if (((instd = xbps_pkgdb_get_pkg(xhp, pattern)) == NULL) &&
86 ((instd = xbps_pkgdb_get_virtualpkg(xhp, pattern)) == NULL))
87 continue;
88
89 if (!xbps_dictionary_get_cstring_nocopy(instd, "pkgver", &curpkgver)) {
90 xbps_object_iterator_release(iter);
91 return false;
92 }
93 /* ignore pkgs on hold mode */
94 if (xbps_dictionary_get_bool(instd, "hold", &hold) && hold)
95 continue;
96
97 if (!xbps_pkg_name(curpkgname, XBPS_NAME_SIZE, curpkgver)) {
98 xbps_object_iterator_release(iter);
99 return false;
100 }
101 /*
102 * Check that we are not replacing the same package,
103 * due to virtual packages.
104 */
105 if (strcmp(pkgname, curpkgname) == 0) {
106 continue;
107 }
108 /*
109 * Make sure to not add duplicates.
110 */
111 xbps_dictionary_get_bool(instd, "automatic-install", &instd_auto);
112 reppkgd = xbps_find_pkg_in_array(pkgs, curpkgname, 0);
113 if (reppkgd) {
114 ttype = xbps_transaction_pkg_type(reppkgd);
115 if (ttype == XBPS_TRANS_REMOVE || ttype == XBPS_TRANS_HOLD)
116 continue;
117 if (!xbps_dictionary_get_cstring_nocopy(reppkgd,
118 "pkgver", &curpkgver)) {
119 xbps_object_iterator_release(iter);
120 return false;
121 }
122 if (!xbps_match_virtual_pkg_in_dict(reppkgd, pattern) &&
123 !xbps_pkgpattern_match(curpkgver, pattern))
124 continue;
125 /*
126 * Package contains replaces="pkgpattern", but the
127 * package that should be replaced is also in the
128 * transaction and it's going to be updated.
129 */
130 if (!instd_auto) {
131 xbps_dictionary_remove(obj, "automatic-install");
132 }
133 if (!xbps_dictionary_set_bool(reppkgd, "replaced", true)) {
134 xbps_object_iterator_release(iter);
135 return false;
136 }
137 if (!xbps_transaction_pkg_type_set(reppkgd, XBPS_TRANS_REMOVE)) {
138 xbps_object_iterator_release(iter);
139 return false;
140 }
141 if (xbps_array_replace_dict_by_name(pkgs, reppkgd, curpkgname) != 0) {
142 xbps_object_iterator_release(iter);
143 return false;
144 }
146 "Package `%s' in transaction will be "
147 "replaced by `%s', matched with `%s'\n",
148 curpkgver, pkgver, pattern);
149 continue;
150 }
151 /*
152 * If new package is providing a virtual package to the
153 * package that we want to replace we should respect
154 * the automatic-install object.
155 */
156 if (xbps_match_virtual_pkg_in_dict(obj, pattern)) {
157 if (!instd_auto) {
158 xbps_dictionary_remove(obj, "automatic-install");
159 }
160 }
161 /*
162 * Add package dictionary into the transaction and mark
163 * it as to be "removed".
164 */
165 if (!xbps_transaction_pkg_type_set(instd, XBPS_TRANS_REMOVE)) {
166 xbps_object_iterator_release(iter);
167 return false;
168 }
169 if (!xbps_dictionary_set_bool(instd, "replaced", true)) {
170 xbps_object_iterator_release(iter);
171 return false;
172 }
173 if (!xbps_array_add_first(pkgs, instd)) {
174 xbps_object_iterator_release(iter);
175 return false;
176 }
178 "Package `%s' will be replaced by `%s', "
179 "matched with `%s'\n", curpkgver, pkgver, pattern);
180 }
181 xbps_object_iterator_release(iter);
182 }
183
184 return true;
185}
Generic XBPS structure handler for initialization.
Definition xbps.h:560
void xbps_dbg_printf(const char *fmt,...)
Prints debug messages to stderr.
Definition log.c:72
xbps_dictionary_t xbps_pkgdb_get_virtualpkg(struct xbps_handle *xhp, const char *pkg)
Definition pkgdb.c:415
xbps_dictionary_t xbps_pkgdb_get_pkg(struct xbps_handle *xhp, const char *pkg)
Definition pkgdb.c:401
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:253
int xbps_pkgpattern_match(const char *pkgver, const char *pattern)
Definition util.c:530