XBPS Library API 20240111
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 xbps_dbg_printf("%s\n", XBPS_RELVER);
57
58 /* Set rootdir */
59 if (xhp->rootdir[0] == '\0') {
60 xhp->rootdir[0] = '/';
61 xhp->rootdir[1] = '\0';
62 } else if (xhp->rootdir[0] != '/') {
63 char cwd[XBPS_MAXPATH];
64
65 if (getcwd(cwd, sizeof(cwd)) == NULL)
66 return ENOBUFS;
67 if (xbps_path_prepend(xhp->rootdir, sizeof xhp->rootdir, cwd) == -1)
68 return ENOBUFS;
69 }
70 if (xbps_path_clean(xhp->rootdir) == -1)
71 return ENOTSUP;
72
73 /* set confdir */
74 if (xhp->confdir[0] == '\0') {
75 if (xbps_path_join(xhp->confdir, sizeof xhp->confdir,
76 xhp->rootdir, XBPS_SYSCONF_PATH, (char *)NULL) == -1)
77 return ENOBUFS;
78 } else if (xhp->confdir[0] != '/') {
79 /* relative path */
80 if (xbps_path_prepend(xhp->confdir, sizeof xhp->confdir,
81 xhp->rootdir) == -1)
82 return ENOBUFS;
83 }
84 if (xbps_path_clean(xhp->confdir) == -1)
85 return ENOTSUP;
86
87 /* set sysconfdir */
88 if (xhp->sysconfdir[0] == '\0') {
89 if (xbps_path_join(xhp->sysconfdir, sizeof xhp->sysconfdir,
90 xhp->rootdir, XBPS_SYSDEFCONF_PATH, (char *)NULL) == -1)
91 return ENOBUFS;
92 }
93 if (xbps_path_clean(xhp->sysconfdir) == -1)
94 return ENOTSUP;
95
96 xbps_fetch_set_cache_connection(XBPS_FETCH_CACHECONN, XBPS_FETCH_CACHECONN_HOST);
97
98 xhp->vpkgd = xbps_dictionary_create();
99 if (xhp->vpkgd == NULL)
100 return errno ? errno : ENOMEM;
101 xhp->vpkgd_conf = xbps_dictionary_create();
102 if (xhp->vpkgd_conf == NULL)
103 return errno ? errno : ENOMEM;
104
105 /* process xbps.d directories */
106 if ((rv = xbps_conf_init(xhp)) != 0)
107 return rv;
108
109 /* target arch only through env var */
110 xhp->target_arch = getenv("XBPS_TARGET_ARCH");
111 if (xhp->target_arch && *xhp->target_arch == '\0')
112 xhp->target_arch = NULL;
113
114 /* allow to overwrite uname(3) and conf file with env variable */
115 if ((native_arch = getenv("XBPS_ARCH")) && *native_arch != '\0') {
116 if (xbps_strlcpy(xhp->native_arch, native_arch,
117 sizeof xhp->native_arch) >= sizeof xhp->native_arch)
118 return ENOBUFS;
119 }
120
121 if (*xhp->native_arch == '\0') {
122 struct utsname un;
123 if (uname(&un) == -1)
124 return ENOTSUP;
125 if (xbps_strlcpy(xhp->native_arch, un.machine,
126 sizeof xhp->native_arch) >= sizeof xhp->native_arch)
127 return ENOBUFS;
128 }
129 assert(*xhp->native_arch);
130
131 /* Set cachedir */
132 if (xhp->cachedir[0] == '\0') {
133 if (xbps_path_join(xhp->cachedir, sizeof xhp->cachedir,
134 xhp->rootdir, XBPS_CACHE_PATH, (char *)NULL) == -1)
135 return ENOBUFS;
136 } else if (xhp->cachedir[0] != '/') {
137 /* relative path */
138 if (xbps_path_prepend(xhp->cachedir, sizeof xhp->cachedir,
139 xhp->rootdir) == -1)
140 return ENOBUFS;
141 }
142 if (xbps_path_clean(xhp->cachedir) == -1)
143 return ENOTSUP;
144
145 /* Set metadir */
146 if (xhp->metadir[0] == '\0') {
147 if (xbps_path_join(xhp->metadir, sizeof xhp->metadir,
148 xhp->rootdir, XBPS_META_PATH, (char *)NULL) == -1)
149 return ENOBUFS;
150 } else if (xhp->metadir[0] != '/') {
151 /* relative path */
152 if (xbps_path_prepend(xhp->metadir, sizeof xhp->metadir,
153 xhp->rootdir) == -1)
154 return ENOBUFS;
155 }
156 if (xbps_path_clean(xhp->metadir) == -1)
157 return ENOTSUP;
158
159 p = getenv("XBPS_SYSLOG");
160 if (p) {
161 if (strcasecmp(p, "true") == 0)
162 xhp->flags &= ~XBPS_FLAG_DISABLE_SYSLOG;
163 else if (strcasecmp(p, "false") == 0)
164 xhp->flags |= XBPS_FLAG_DISABLE_SYSLOG;
165 }
166
167 if (xhp->flags & XBPS_FLAG_DEBUG) {
168 const char *repodir;
169 for (unsigned int i = 0; i < xbps_array_count(xhp->repositories); i++) {
170 if (!xbps_array_get_cstring_nocopy(xhp->repositories, i, &repodir))
171 return errno;
172 xbps_dbg_printf("Repository[%u]=%s\n", i, repodir);
173 }
174 }
175
176 return 0;
177}
178
179void
181{
182 assert(xhp);
183
184 xbps_pkgdb_release(xhp);
185}
char metadir[XBPS_MAXPATH+sizeof(XBPS_META_PATH)]
Definition xbps.h:664
const char * target_arch
Definition xbps.h:631
char rootdir[XBPS_MAXPATH]
Definition xbps.h:650
xbps_array_t repositories
Definition xbps.h:563
char confdir[XBPS_MAXPATH+sizeof(XBPS_SYSCONF_PATH)]
Definition xbps.h:637
char native_arch[64]
Definition xbps.h:671
char cachedir[XBPS_MAXPATH+sizeof(XBPS_CACHE_PATH)]
Definition xbps.h:657
int flags
Definition xbps.h:679
Generic XBPS structure handler for initialization.
Definition xbps.h:550
int xbps_init(struct xbps_handle *xhp)
Definition initend.c:46
void xbps_end(struct xbps_handle *xhp)
Definition initend.c:180
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