XBPS Library API 20250624
The X Binary Package System
initend.c
1/*-
2 * Copyright (c) 2011-2015 Juan Romero Pardines.
3 * Copyright (c) 2014 Enno Boland.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26#include <sys/utsname.h>
27
28#include <errno.h>
29#include <stdlib.h>
30#include <string.h>
31#include <strings.h>
32#include <unistd.h>
33
34#include "xbps_api_impl.h"
35
36/**
37 * @file lib/initend.c
38 * @brief Initialization and finalization routines
39 * @defgroup initend Initialization and finalization functions
40 *
41 * Use these functions to initialize some parameters before start
42 * using libxbps and finalize usage to release resources at the end.
43 */
44
45int
47{
48 const char *native_arch = NULL, *p;
49 int rv = 0;
50
51 assert(xhp != NULL);
52
53 if (xhp->flags & XBPS_FLAG_DEBUG)
54 xbps_debug_level = 1;
55
56 if (xhp->flags & XBPS_FLAG_VERBOSE)
57 xbps_verbose_level = 1;
58
59 xbps_dbg_printf("%s\n", XBPS_RELVER);
60
61 /* Set rootdir */
62 if (xhp->rootdir[0] == '\0') {
63 xhp->rootdir[0] = '/';
64 xhp->rootdir[1] = '\0';
65 } else if (xhp->rootdir[0] != '/') {
66 char cwd[XBPS_MAXPATH];
67
68 if (getcwd(cwd, sizeof(cwd)) == NULL)
69 return ENOBUFS;
70 if (xbps_path_prepend(xhp->rootdir, sizeof xhp->rootdir, cwd) == -1)
71 return ENOBUFS;
72 }
73 if (xbps_path_clean(xhp->rootdir) == -1)
74 return ENOTSUP;
75
76 /* set confdir */
77 if (xhp->confdir[0] == '\0') {
78 if (xbps_path_join(xhp->confdir, sizeof xhp->confdir,
79 xhp->rootdir, XBPS_SYSCONF_PATH, (char *)NULL) == -1)
80 return ENOBUFS;
81 } else if (xhp->confdir[0] != '/') {
82 /* relative path */
83 if (xbps_path_prepend(xhp->confdir, sizeof xhp->confdir,
84 xhp->rootdir) == -1)
85 return ENOBUFS;
86 }
87 if (xbps_path_clean(xhp->confdir) == -1)
88 return ENOTSUP;
89
90 /* set sysconfdir */
91 if (xhp->sysconfdir[0] == '\0') {
92 if (xbps_path_join(xhp->sysconfdir, sizeof xhp->sysconfdir,
93 xhp->rootdir, XBPS_SYSDEFCONF_PATH, (char *)NULL) == -1)
94 return ENOBUFS;
95 }
96 if (xbps_path_clean(xhp->sysconfdir) == -1)
97 return ENOTSUP;
98
99 xbps_fetch_set_cache_connection(XBPS_FETCH_CACHECONN, XBPS_FETCH_CACHECONN_HOST);
100
101 xhp->vpkgd = xbps_dictionary_create();
102 if (xhp->vpkgd == NULL)
103 return errno ? errno : ENOMEM;
104 xhp->vpkgd_conf = xbps_dictionary_create();
105 if (xhp->vpkgd_conf == NULL)
106 return errno ? errno : ENOMEM;
107
108 /* process xbps.d directories */
109 if ((rv = xbps_conf_init(xhp)) != 0)
110 return rv;
111
112 /* target arch only through env var */
113 xhp->target_arch = getenv("XBPS_TARGET_ARCH");
114 if (xhp->target_arch && *xhp->target_arch == '\0')
115 xhp->target_arch = NULL;
116
117 /* allow to overwrite uname(3) and conf file with env variable */
118 if ((native_arch = getenv("XBPS_ARCH")) && *native_arch != '\0') {
119 if (xbps_strlcpy(xhp->native_arch, native_arch,
120 sizeof xhp->native_arch) >= sizeof xhp->native_arch)
121 return ENOBUFS;
122 }
123
124 if (*xhp->native_arch == '\0') {
125 struct utsname un;
126 if (uname(&un) == -1)
127 return ENOTSUP;
128 if (xbps_strlcpy(xhp->native_arch, un.machine,
129 sizeof xhp->native_arch) >= sizeof xhp->native_arch)
130 return ENOBUFS;
131 }
132 assert(*xhp->native_arch);
133
134 /* Set cachedir */
135 if (xhp->cachedir[0] == '\0') {
136 if (xbps_path_join(xhp->cachedir, sizeof xhp->cachedir,
137 xhp->rootdir, XBPS_CACHE_PATH, (char *)NULL) == -1)
138 return ENOBUFS;
139 } else if (xhp->cachedir[0] != '/') {
140 /* relative path */
141 if (xbps_path_prepend(xhp->cachedir, sizeof xhp->cachedir,
142 xhp->rootdir) == -1)
143 return ENOBUFS;
144 }
145 if (xbps_path_clean(xhp->cachedir) == -1)
146 return ENOTSUP;
147
148 /* Set metadir */
149 if (xhp->metadir[0] == '\0') {
150 if (xbps_path_join(xhp->metadir, sizeof xhp->metadir,
151 xhp->rootdir, XBPS_META_PATH, (char *)NULL) == -1)
152 return ENOBUFS;
153 } else if (xhp->metadir[0] != '/') {
154 /* relative path */
155 if (xbps_path_prepend(xhp->metadir, sizeof xhp->metadir,
156 xhp->rootdir) == -1)
157 return ENOBUFS;
158 }
159 if (xbps_path_clean(xhp->metadir) == -1)
160 return ENOTSUP;
161
162 p = getenv("XBPS_SYSLOG");
163 if (p) {
164 if (strcasecmp(p, "true") == 0)
165 xhp->flags &= ~XBPS_FLAG_DISABLE_SYSLOG;
166 else if (strcasecmp(p, "false") == 0)
167 xhp->flags |= XBPS_FLAG_DISABLE_SYSLOG;
168 }
169
170 p = getenv("XBPS_STAGING");
171 if (p) {
172 if (strcasecmp(p, "true") == 0)
173 xhp->flags &= ~XBPS_FLAG_USE_STAGE;
174 else if (strcasecmp(p, "false") == 0)
175 xhp->flags |= XBPS_FLAG_USE_STAGE;
176 }
177
178 if (xhp->flags & XBPS_FLAG_DEBUG) {
179 const char *repodir;
180 for (unsigned int i = 0; i < xbps_array_count(xhp->repositories); i++) {
181 if (!xbps_array_get_cstring_nocopy(xhp->repositories, i, &repodir))
182 return errno;
183 xbps_dbg_printf("Repository[%u]=%s\n", i, repodir);
184 }
185 }
186
187 return 0;
188}
189
190void
192{
193 assert(xhp);
194
195 xbps_pkgdb_release(xhp);
196}
char rootdir[XBPS_MAXPATH]
Definition xbps.h:659
xbps_array_t repositories
Definition xbps.h:572
const char * target_arch
Definition xbps.h:640
char confdir[XBPS_MAXPATH]
Definition xbps.h:646
int flags
Definition xbps.h:688
char metadir[XBPS_MAXPATH]
Definition xbps.h:673
char native_arch[64]
Definition xbps.h:680
char cachedir[XBPS_MAXPATH]
Definition xbps.h:666
Generic XBPS structure handler for initialization.
Definition xbps.h:559
int xbps_init(struct xbps_handle *xhp)
Definition initend.c:46
void xbps_end(struct xbps_handle *xhp)
Definition initend.c:191
size_t xbps_strlcpy(char *dst, const char *src, size_t dstsize)
Definition util.c:575
ssize_t xbps_path_clean(char *path)
Definition util_path.c:67
ssize_t xbps_path_prepend(char *dst, size_t len, const char *prefix)
Definition util_path.c:249
ssize_t xbps_path_join(char *dst, size_t len,...)
Definition util_path.c:208