Make some reorganization in struct xbps_handle.

The variables to set cachedir, rootdir and metadir have been
changed to "array of chars", this way there are no extra allocations.

Update clients accordingly and bump API version.
This commit is contained in:
Juan RP 2013-12-16 11:46:39 +01:00
parent 2c16e23646
commit 2a21354a1c
10 changed files with 107 additions and 110 deletions

View File

@ -479,7 +479,7 @@ main(int argc, char **argv)
/* Initialize libxbps */
memset(&xh, 0, sizeof(xh));
xh.rootdir = rootdir;
strncpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
if ((rv = xbps_init(&xh)) != 0)
die("failed to initialize libxbps: %s", strerror(rv));

View File

@ -187,8 +187,10 @@ main(int argc, char **argv)
xh.state_cb = state_cb;
xh.fetch_cb = fetch_file_progress_cb;
xh.fetch_cb_data = &xfer;
xh.rootdir = rootdir;
xh.cachedir = cachedir;
if (rootdir)
strncpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
if (cachedir)
strncpy(xh.cachedir, cachedir, sizeof(xh.cachedir));
xh.conffile = conffile;
xh.flags = flags;
if (flags & XBPS_FLAG_VERBOSE)

View File

@ -135,7 +135,8 @@ main(int argc, char **argv)
usage(true);
memset(&xh, 0, sizeof(xh));
xh.rootdir = rootdir;
if (rootdir)
strncpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
xh.conffile = conffile;
xh.flags = flags;

View File

@ -193,8 +193,10 @@ main(int argc, char **argv)
/*
* Initialize libxbps.
*/
xh.rootdir = rootdir;
xh.cachedir = cachedir;
if (rootdir)
strncpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
if (cachedir)
strncpy(xh.cachedir, cachedir, sizeof(xh.cachedir));
xh.conffile = conffile;
xh.flags = flags;

View File

@ -145,7 +145,8 @@ main(int argc, char **argv)
memset(&xh, 0, sizeof(xh));
xh.state_cb = state_cb;
xh.rootdir = rootdir;
if (rootdir)
strncpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
xh.conffile = conffile;
xh.flags = flags;

View File

@ -310,8 +310,10 @@ main(int argc, char **argv)
*/
memset(&xh, 0, sizeof(xh));
xh.state_cb = state_cb_rm;
xh.rootdir = rootdir;
xh.cachedir = cachedir;
if (rootdir)
strncpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
if (cachedir)
strncpy(xh.cachedir, cachedir, sizeof(xh.cachedir));
xh.conffile = conffile;
xh.flags = flags;

View File

@ -128,7 +128,8 @@ main(int argc, char **argv)
xh.flags = flags;
xh.fetch_cb = fetch_file_progress_cb;
xh.fetch_cb_data = &xfer;
xh.rootdir = rootdir;
if (rootdir)
strncpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
xh.conffile = confdir;
if ((rv = xbps_init(&xh)) != 0) {
xbps_error_printf("xbps-uhelper: failed to "

View File

@ -29,6 +29,7 @@
#include <stdio.h>
#include <inttypes.h>
#include <limits.h>
#include <xbps/xbps_array.h>
#include <xbps/xbps_bool.h>
@ -46,7 +47,7 @@
*
* This header documents the full API for the XBPS Library.
*/
#define XBPS_API_VERSION "20131216-1"
#define XBPS_API_VERSION "20131216-2"
#ifndef XBPS_VERSION
#define XBPS_VERSION "UNSET"
@ -529,27 +530,6 @@ struct xbps_handle {
* the \a xbps_fetch_cb function callback.
*/
void *fetch_cb_data;
/**
* @var rootdir
*
* Root directory for all operations in XBPS. If NULL,
* by default it's set to /.
*/
const char *rootdir;
/**
* @var cachedir
*
* Cache directory to store downloaded binary packages.
* If NULL default value in \a XBPS_CACHE_PATH is used.
*/
const char *cachedir;
/**
* @var metadir
*
* Metadata directory for all operations in XBPS.
* If NULL, defaults to \a XBPS_CACHE_PATH (relative to rootdir).
*/
const char *metadir;
/**
* @var conffile
*
@ -557,12 +537,38 @@ struct xbps_handle {
*/
const char *conffile;
/**
* @private
* @var target_arch
*
* Target architecture, as set by XBPS_TARGET_ARCH from environment.
*/
char *cachedir_priv;
char *metadir_priv;
char *native_arch;
const char *target_arch;
/**
* @var rootdir
*
* Root directory for all operations in XBPS.
* If unset, defaults to '/'.
*/
char rootdir[PATH_MAX-1];
/**
* @var cachedir
*
* Cache directory to store downloaded binary packages.
* If unset, defaults to \a XBPS_CACHE_PATH (relative to rootdir).
*/
char cachedir[PATH_MAX-1];
/**
* @var metadir
*
* Metadata directory for all operations in XBPS.
* If unset, defaults to \a XBPS_CACHE_PATH (relative to rootdir).
*/
char metadir[PATH_MAX-1];
/**
* @var native_arch
*
* Machine architecture, as returned by uname(2)::machine.
*/
char native_arch[16];
/**
* @var fetch_timeout
*

View File

@ -45,6 +45,7 @@
#include "queue.h"
#include "fetch.h"
#include "compat.h"
#define EXTRACT_FLAGS ARCHIVE_EXTRACT_SECURE_NODOTDOT | \
ARCHIVE_EXTRACT_SECURE_SYMLINKS

View File

@ -46,35 +46,6 @@
* Use these functions to initialize some parameters before start
* using libxbps and finalize usage to release resources at the end.
*/
static char *
set_cachedir(struct xbps_handle *xh)
{
if (xh->cachedir[0] == '/') {
/* full path */
return strdup(xh->cachedir);
} else {
/* relative to rootdir */
if (strcmp(xh->rootdir, "/") == 0)
return xbps_xasprintf("/%s", xh->cachedir);
else
return xbps_xasprintf("%s/%s", xh->rootdir,
xh->cachedir);
}
}
static char *
set_metadir(struct xbps_handle *xh)
{
if (xh->metadir == NULL) {
if (strcmp(xh->rootdir, "/") == 0)
return xbps_xasprintf("/%s", XBPS_META_PATH);
else
return xbps_xasprintf("%s/%s", xh->rootdir, XBPS_META_PATH);
} else {
return strdup(xh->metadir);
}
}
static int
cb_validate_virtual(cfg_t *cfg, cfg_opt_t *opt)
{
@ -115,6 +86,7 @@ xbps_init(struct xbps_handle *xhp)
CFG_END()
};
struct utsname un;
char *buf;
const char *repodir;
int rv, cc, cch;
bool syslog_enabled = false;
@ -150,55 +122,57 @@ xbps_init(struct xbps_handle *xhp)
}
xbps_dbg_printf(xhp, "Configuration file: %s\n",
xhp->conffile ? xhp->conffile : "not found");
/*
* Respect client setting in struct xbps_handle for {root,cache}dir;
* otherwise use values from configuration file or defaults if unset.
*/
if (xhp->rootdir == NULL) {
if (xhp->cfg == NULL)
xhp->rootdir = "/";
else
xhp->rootdir = cfg_getstr(xhp->cfg, "rootdir");
} else {
if (xhp->rootdir[0] != '/') {
/* relative path */
char *buf, path[PATH_MAX-1];
if (getcwd(path, sizeof(path)) == NULL)
return ENOTSUP;
buf = xbps_xasprintf("%s/%s", path, xhp->rootdir);
xhp->rootdir = buf;
/* Set rootdir */
if (xhp->rootdir[0] == '\0') {
if (xhp->cfg != NULL) {
strlcpy(xhp->rootdir,
cfg_getstr(xhp->cfg, "rootdir"),
sizeof(xhp->rootdir));
} else {
xhp->rootdir[0] = '/';
xhp->rootdir[1] = '\0';
}
}
/*
* Append repository list specified in configuration file.
*/
for (unsigned int i = 0; i < cfg_size(xhp->cfg, "repositories"); i++) {
if (xhp->repositories == NULL)
xhp->repositories = xbps_array_create();
} else if (xhp->rootdir[0] != '/') {
/* relative path */
char path[PATH_MAX-1];
repodir = cfg_getnstr(xhp->cfg, "repositories", i);
xbps_array_add_cstring_nocopy(xhp->repositories, repodir);
}
if (getcwd(path, sizeof(path)) == NULL)
return ENOTSUP;
if (xhp->cachedir == NULL) {
if (xhp->cfg == NULL)
xhp->cachedir = XBPS_CACHE_PATH;
else
xhp->cachedir = cfg_getstr(xhp->cfg, "cachedir");
buf = strdup(xhp->rootdir);
snprintf(xhp->rootdir, sizeof(xhp->rootdir),
"%s/%s", path, buf);
free(buf);
}
/* Set cachedir */
if (xhp->cachedir[0] == '\0') {
snprintf(xhp->cachedir, sizeof(xhp->cachedir),
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "",
xhp->cfg ? cfg_getstr(xhp->cfg, "cachedir") : XBPS_CACHE_PATH);
} else if (xhp->cachedir[0] != '/') {
/* relative path */
buf = strdup(xhp->cachedir);
snprintf(xhp->cachedir, sizeof(xhp->cachedir),
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf);
free(buf);
}
/* Set metadir */
if (xhp->metadir[0] == '\0') {
snprintf(xhp->metadir, sizeof(xhp->metadir),
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "",
XBPS_META_PATH);
} else if (xhp->metadir[0] != '/') {
/* relative path */
buf = strdup(xhp->metadir);
snprintf(xhp->metadir, sizeof(xhp->metadir),
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf);
free(buf);
}
if ((xhp->cachedir_priv = set_cachedir(xhp)) == NULL)
return ENOMEM;
xhp->cachedir = xhp->cachedir_priv;
if ((xhp->metadir_priv = set_metadir(xhp)) == NULL)
return ENOMEM;
xhp->metadir = xhp->metadir_priv;
xhp->target_arch = getenv("XBPS_TARGET_ARCH");
uname(&un);
xhp->native_arch = strdup(un.machine);
strlcpy(xhp->native_arch, un.machine, sizeof(xhp->native_arch));
assert(xhp->native_arch);
if (xhp->cfg == NULL) {
@ -228,6 +202,17 @@ xbps_init(struct xbps_handle *xhp)
xbps_dbg_printf(xhp, "Architecture: %s\n", xhp->native_arch);
xbps_dbg_printf(xhp, "Target Architecture: %s\n", xhp->target_arch);
/*
* Append repository list specified in configuration file.
*/
for (unsigned int i = 0; i < cfg_size(xhp->cfg, "repositories"); i++) {
if (xhp->repositories == NULL)
xhp->repositories = xbps_array_create();
xbps_array_add_cstring_nocopy(xhp->repositories,
cfg_getnstr(xhp->cfg, "repositories", i));
}
if (xhp->flags & XBPS_FLAG_DEBUG) {
for (unsigned int i = 0; i < xbps_array_count(xhp->repositories); i++) {
xbps_array_get_cstring_nocopy(xhp->repositories, i, &repodir);
@ -256,10 +241,6 @@ xbps_end(struct xbps_handle *xhp)
xbps_fetch_unset_cache_connection();
cfg_free(xhp->cfg);
free(xhp->cachedir_priv);
free(xhp->metadir_priv);
free(xhp->native_arch);
xhp->initialized = false;
}