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 */ /* Initialize libxbps */
memset(&xh, 0, sizeof(xh)); memset(&xh, 0, sizeof(xh));
xh.rootdir = rootdir; strncpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
if ((rv = xbps_init(&xh)) != 0) if ((rv = xbps_init(&xh)) != 0)
die("failed to initialize libxbps: %s", strerror(rv)); 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.state_cb = state_cb;
xh.fetch_cb = fetch_file_progress_cb; xh.fetch_cb = fetch_file_progress_cb;
xh.fetch_cb_data = &xfer; xh.fetch_cb_data = &xfer;
xh.rootdir = rootdir; if (rootdir)
xh.cachedir = cachedir; strncpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
if (cachedir)
strncpy(xh.cachedir, cachedir, sizeof(xh.cachedir));
xh.conffile = conffile; xh.conffile = conffile;
xh.flags = flags; xh.flags = flags;
if (flags & XBPS_FLAG_VERBOSE) if (flags & XBPS_FLAG_VERBOSE)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -29,6 +29,7 @@
#include <stdio.h> #include <stdio.h>
#include <inttypes.h> #include <inttypes.h>
#include <limits.h>
#include <xbps/xbps_array.h> #include <xbps/xbps_array.h>
#include <xbps/xbps_bool.h> #include <xbps/xbps_bool.h>
@ -46,7 +47,7 @@
* *
* This header documents the full API for the XBPS Library. * 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 #ifndef XBPS_VERSION
#define XBPS_VERSION "UNSET" #define XBPS_VERSION "UNSET"
@ -529,27 +530,6 @@ struct xbps_handle {
* the \a xbps_fetch_cb function callback. * the \a xbps_fetch_cb function callback.
*/ */
void *fetch_cb_data; 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 * @var conffile
* *
@ -557,12 +537,38 @@ struct xbps_handle {
*/ */
const char *conffile; 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; 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 * @var fetch_timeout
* *

View File

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

View File

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