XBPS Library API 20260225
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 <strings.h>
31#include <unistd.h>
32
33#include "xbps_api_impl.h"
34
35/**
36 * @file lib/initend.c
37 * @brief Initialization and finalization routines
38 * @defgroup initend Initialization and finalization functions
39 *
40 * Use these functions to initialize some parameters before start
41 * using libxbps and finalize usage to release resources at the end.
42 */
43
44int
46{
47 const char *native_arch = NULL, *p;
48 int rv = 0;
49
50 assert(xhp != NULL);
51
52 if (xhp->flags & XBPS_FLAG_DEBUG)
54
55 if (xhp->flags & XBPS_FLAG_VERBOSE)
56 xbps_verbose_level = 1;
57
58 xbps_dbg_printf("%s\n", XBPS_RELVER);
59
60 /* Set rootdir */
61 if (xhp->rootdir[0] == '\0') {
62 xhp->rootdir[0] = '/';
63 xhp->rootdir[1] = '\0';
64 } else if (xhp->rootdir[0] != '/') {
65 char cwd[XBPS_MAXPATH];
66
67 if (getcwd(cwd, sizeof(cwd)) == NULL)
68 return ENOBUFS;
69 if (xbps_path_prepend(xhp->rootdir, sizeof xhp->rootdir, cwd) == -1)
70 return ENOBUFS;
71 }
72 if (xbps_path_clean(xhp->rootdir) == -1)
73 return ENOTSUP;
74
75 /* set confdir */
76 if (xhp->confdir[0] == '\0') {
77 if (xbps_path_join(xhp->confdir, sizeof xhp->confdir,
78 xhp->rootdir, XBPS_SYSCONF_PATH, (char *)NULL) == -1)
79 return ENOBUFS;
80 } else if (xhp->confdir[0] != '/') {
81 /* relative path */
82 if (xbps_path_prepend(xhp->confdir, sizeof xhp->confdir,
83 xhp->rootdir) == -1)
84 return ENOBUFS;
85 }
86 if (xbps_path_clean(xhp->confdir) == -1)
87 return ENOTSUP;
88
89 /* set sysconfdir */
90 if (xhp->sysconfdir[0] == '\0') {
91 if (xbps_path_join(xhp->sysconfdir, sizeof xhp->sysconfdir,
92 xhp->rootdir, XBPS_SYSDEFCONF_PATH, (char *)NULL) == -1)
93 return ENOBUFS;
94 }
95 if (xbps_path_clean(xhp->sysconfdir) == -1)
96 return ENOTSUP;
97
98 xbps_fetch_set_cache_connection(XBPS_FETCH_CACHECONN, XBPS_FETCH_CACHECONN_HOST);
99
100 xhp->vpkgd = xbps_dictionary_create();
101 if (xhp->vpkgd == NULL)
102 return errno ? errno : ENOMEM;
103 xhp->vpkgd_conf = xbps_dictionary_create();
104 if (xhp->vpkgd_conf == NULL)
105 return errno ? errno : ENOMEM;
106
107 /* process xbps.d directories */
108 if ((rv = xbps_conf_init(xhp)) != 0)
109 return rv;
110
111 /* target arch only through env var */
112 xhp->target_arch = getenv("XBPS_TARGET_ARCH");
113 if (xhp->target_arch && *xhp->target_arch == '\0')
114 xhp->target_arch = NULL;
115
116 /* allow to overwrite uname(3) and conf file with env variable */
117 if ((native_arch = getenv("XBPS_ARCH")) && *native_arch != '\0') {
118 if (xbps_strlcpy(xhp->native_arch, native_arch,
119 sizeof xhp->native_arch) >= sizeof xhp->native_arch)
120 return ENOBUFS;
121 }
122
123 if (*xhp->native_arch == '\0') {
124 struct utsname un;
125 if (uname(&un) == -1)
126 return ENOTSUP;
127 if (xbps_strlcpy(xhp->native_arch, un.machine,
128 sizeof xhp->native_arch) >= sizeof xhp->native_arch)
129 return ENOBUFS;
130 }
131 assert(*xhp->native_arch);
132
133 /* Set cachedir */
134 if (xhp->cachedir[0] == '\0') {
135 if (xbps_path_join(xhp->cachedir, sizeof xhp->cachedir,
136 xhp->rootdir, XBPS_CACHE_PATH, (char *)NULL) == -1)
137 return ENOBUFS;
138 } else if (xhp->cachedir[0] != '/') {
139 /* relative path */
140 if (xbps_path_prepend(xhp->cachedir, sizeof xhp->cachedir,
141 xhp->rootdir) == -1)
142 return ENOBUFS;
143 }
144 if (xbps_path_clean(xhp->cachedir) == -1)
145 return ENOTSUP;
146
147 /* Set metadir */
148 if (xhp->metadir[0] == '\0') {
149 if (xbps_path_join(xhp->metadir, sizeof xhp->metadir,
150 xhp->rootdir, XBPS_META_PATH, (char *)NULL) == -1)
151 return ENOBUFS;
152 } else if (xhp->metadir[0] != '/') {
153 /* relative path */
154 if (xbps_path_prepend(xhp->metadir, sizeof xhp->metadir,
155 xhp->rootdir) == -1)
156 return ENOBUFS;
157 }
158 if (xbps_path_clean(xhp->metadir) == -1)
159 return ENOTSUP;
160
161 p = getenv("XBPS_SYSLOG");
162 if (p) {
163 if (strcasecmp(p, "true") == 0)
164 xhp->flags &= ~XBPS_FLAG_DISABLE_SYSLOG;
165 else if (strcasecmp(p, "false") == 0)
166 xhp->flags |= XBPS_FLAG_DISABLE_SYSLOG;
167 }
168
169 p = getenv("XBPS_STAGING");
170 if (p) {
171 if (strcasecmp(p, "true") == 0)
172 xhp->flags &= ~XBPS_FLAG_USE_STAGE;
173 else if (strcasecmp(p, "false") == 0)
174 xhp->flags |= XBPS_FLAG_USE_STAGE;
175 }
176
177 if (xhp->flags & XBPS_FLAG_DEBUG) {
178 const char *repodir;
179 for (unsigned int i = 0; i < xbps_array_count(xhp->repositories); i++) {
180 if (!xbps_array_get_cstring_nocopy(xhp->repositories, i, &repodir))
181 return errno;
182 xbps_dbg_printf("Repository[%u]=%s\n", i, repodir);
183 }
184 }
185
186 return 0;
187}
188
189void
191{
192 assert(xhp);
193
194 xbps_pkgdb_release(xhp);
195}
char rootdir[XBPS_MAXPATH]
Definition xbps.h:664
xbps_array_t repositories
Definition xbps.h:573
const char * target_arch
Definition xbps.h:645
char confdir[XBPS_MAXPATH]
Definition xbps.h:651
int flags
Definition xbps.h:693
char metadir[XBPS_MAXPATH]
Definition xbps.h:678
char native_arch[64]
Definition xbps.h:685
char cachedir[XBPS_MAXPATH]
Definition xbps.h:671
Generic XBPS structure handler for initialization.
Definition xbps.h:560
int xbps_init(struct xbps_handle *xhp)
Definition initend.c:45
void xbps_end(struct xbps_handle *xhp)
Definition initend.c:190
int xbps_debug_level
The Debug level.
Definition log.c:33
void xbps_dbg_printf(const char *fmt,...)
Prints debug messages to stderr.
Definition log.c:67
size_t xbps_strlcpy(char *dst, const char *src, size_t dstsize)
Definition util.c:571
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