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