XBPS Library API 20240111
The X Binary Package System
transaction_store.c
1/*-
2 * Copyright (c) 2014-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 <stdlib.h>
28#include <string.h>
29#include <errno.h>
30
31#include "xbps_api_impl.h"
32
33bool HIDDEN
34xbps_transaction_store(struct xbps_handle *xhp, xbps_array_t pkgs,
35 xbps_dictionary_t pkgrd, bool autoinst)
36{
37 xbps_dictionary_t d, pkgd;
38 xbps_array_t replaces;
39 const char *pkgver, *pkgname, *curpkgver, *repo;
40 char *self_replaced;
41 int rv;
42
43 assert(xhp);
44 assert(pkgs);
45 assert(pkgrd);
46
47 if (!xbps_dictionary_get_cstring_nocopy(pkgrd, "pkgver", &pkgver)) {
48 return false;
49 }
50 if (!xbps_dictionary_get_cstring_nocopy(pkgrd, "pkgname", &pkgname)) {
51 return false;
52 }
53 d = xbps_find_pkg_in_array(pkgs, pkgname, 0);
54 if (xbps_object_type(d) == XBPS_TYPE_DICTIONARY) {
55 /* compare version stored in transaction vs current */
56 if (!xbps_dictionary_get_cstring_nocopy(d, "pkgver", &curpkgver)) {
57 return false;
58 }
59 rv = xbps_cmpver(pkgver, curpkgver);
60 if (rv == 0 || rv == -1) {
61 /* same version or stored version greater than current */
62 return true;
63 } else {
64 /*
65 * Current version is greater than stored,
66 * replace stored with current.
67 */
68 if (!xbps_remove_pkg_from_array_by_pkgver(pkgs, curpkgver)) {
69 return false;
70 }
71 xbps_dbg_printf("[trans] replaced %s with %s\n", curpkgver, pkgver);
72 }
73 }
74
75 if ((pkgd = xbps_dictionary_copy_mutable(pkgrd)) == NULL)
76 return false;
77
78 /*
79 * Add required objects into package dep's dictionary.
80 */
81 if (autoinst && !xbps_dictionary_set_bool(pkgd, "automatic-install", true))
82 goto err;
83
84 /*
85 * Set a replaces to itself, so that virtual packages are always replaced.
86 */
87 if ((replaces = xbps_dictionary_get(pkgd, "replaces")) == NULL)
88 replaces = xbps_array_create();
89
90 self_replaced = xbps_xasprintf("%s>=0", pkgname);
91 xbps_array_add_cstring(replaces, self_replaced);
92 free(self_replaced);
93
94 if (!xbps_dictionary_set(pkgd, "replaces", replaces))
95 goto err;
96
97 /*
98 * Add the dictionary into the unsorted queue.
99 */
100 if (!xbps_array_add(pkgs, pkgd))
101 goto err;
102
103 xbps_dictionary_get_cstring_nocopy(pkgd, "repository", &repo);
104
105 xbps_set_cb_state(xhp, XBPS_STATE_TRANS_ADDPKG, 0, pkgver,
106 "Found %s in repository %s", pkgver, repo);
107
108 xbps_dbg_printf("[trans] `%s' stored%s (%s)\n", pkgver,
109 autoinst ? " as automatic install" : "", repo);
110 xbps_object_release(pkgd);
111
112 return true;
113err:
114 xbps_object_release(pkgd);
115 return false;
116}
Generic XBPS structure handler for initialization.
Definition xbps.h:550
char * xbps_xasprintf(const char *fmt,...) __attribute__((format(printf
int xbps_cmpver(const char *pkg1, const char *pkg2)
Definition dewey.c:273