Another checkpoint for installing binary packages.

At least now dependencies are tracked, not sure it will be
enough for real life :-)

--HG--
extra : convert_revision : 962de22d515efa5599c148f918c14d32b5d9496d
This commit is contained in:
Juan RP 2008-12-24 07:20:19 +01:00
parent 30997094d6
commit fa3b59b246
7 changed files with 266 additions and 111 deletions

View file

@ -108,7 +108,7 @@ usage(void)
" unregister\t[<pkgname> <version>]\n" " unregister\t[<pkgname> <version>]\n"
" version\t[<pkgname>]\n" " version\t[<pkgname>]\n"
" Environment:\n" " Environment:\n"
" XBPS_REGPKGDB_PATH\tPath to xbps pkgdb plist file\n\n" " XBPS_META_PATH\tPath to xbps metadata root directory\n\n"
" Examples:\n" " Examples:\n"
" $ xbps-pkgdb list\n" " $ xbps-pkgdb list\n"
" $ xbps-pkgdb register pkgname 2.0 \"A short description\"\n" " $ xbps-pkgdb register pkgname 2.0 \"A short description\"\n"

View file

@ -80,6 +80,9 @@ xbps_callback_array_iter_in_dict(prop_dictionary_t, const char *,
prop_dictionary_t prop_dictionary_t
xbps_find_pkg_in_dict(prop_dictionary_t, const char *); xbps_find_pkg_in_dict(prop_dictionary_t, const char *);
prop_dictionary_t
xbps_find_pkg_from_plist(const char *, const char *);
/* /*
* Finds a string object in an array. * Finds a string object in an array.
* *
@ -153,6 +156,7 @@ int xbps_check_reqdeps_in_pkg(const char *, prop_dictionary_t);
/* Utils */ /* Utils */
bool xbps_append_full_path(char *, const char *, const char *); bool xbps_append_full_path(char *, const char *, const char *);
int xbps_check_is_installed_pkg(const char *, const char *);
int xbps_cmpver_packages(const char *, const char *); int xbps_cmpver_packages(const char *, const char *);
int xbps_cmpver_versions(const char *, const char *); int xbps_cmpver_versions(const char *, const char *);
const char * xbps_get_pkg_version(const char *); const char * xbps_get_pkg_version(const char *);

View file

@ -8,7 +8,8 @@ LIBXBPS_SO = $(LIBXBPS).$(MAJOR).$(MINOR).$(MICRO)
LIBXBPS = libxbps.so LIBXBPS = libxbps.so
LIBXBPS_LDFLAGS = -larchive -lprop -shared -Wl,-soname,$(LIBXBPS).$(MAJOR) LIBXBPS_LDFLAGS = -larchive -lprop -shared -Wl,-soname,$(LIBXBPS).$(MAJOR)
OBJECTS = cmpver.o depends.o humanize_number.o install.o plist.o sha256.o OBJECTS = cmpver.o depends.o humanize_number.o install.o plist.o
OBJECTS += sha256.o util.o
all: $(LIBXBPS) all: $(LIBXBPS)
.PHONY: all .PHONY: all

View file

@ -32,70 +32,16 @@
#include <xbps_api.h> #include <xbps_api.h>
const char * typedef struct pkg_dependency {
xbps_get_pkg_version(const char *pkg) LIST_ENTRY(pkg_dependency) deps;
{ prop_dictionary_t dict;
const char *tmp; char *name;
} pkg_dep_t;
/* Get the required version */ static LIST_HEAD(, pkg_dependency) pkg_deps_list =
tmp = strrchr(pkg, '-'); LIST_HEAD_INITIALIZER(pkg_deps_list);
assert(tmp != NULL);
return tmp + 1; /* skip first '-' */
}
char * int
xbps_get_pkg_name(const char *pkg)
{
const char *tmp;
char *pkgname;
size_t len;
/* Get the required version */
tmp = strrchr(pkg, '-');
assert(tmp != NULL);
len = strlen(pkg) - strlen(tmp);
/* Get package name */
pkgname = malloc(len + 1);
memcpy(pkgname, pkg, len);
pkgname[len + 1] = '\0';
return pkgname;
}
bool
xbps_append_full_path(char *buf, const char *root, const char *plistf)
{
const char *env, *tmp;
size_t len = 0;
assert(buf != NULL);
assert(plistf != NULL);
if (root)
env = root;
else {
env = getenv("XBPS_META_PATH");
if (env == NULL)
env = XBPS_META_PATH;
}
tmp = strncpy(buf, env, PATH_MAX - 1);
if (sizeof(*tmp) >= PATH_MAX) {
errno = ENOSPC;
return false;
}
len = strlen(buf);
buf[len + 1] = '\0';
if (buf[len - 2] != '/')
strncat(buf, "/", 1);
strncat(buf, plistf, sizeof(buf) - strlen(buf) - 1);
return true;
}
static int
xbps_check_is_installed_pkg(const char *plist, const char *pkg) xbps_check_is_installed_pkg(const char *plist, const char *pkg)
{ {
prop_dictionary_t dict, pkgdict; prop_dictionary_t dict, pkgdict;
@ -132,58 +78,117 @@ xbps_check_is_installed_pkg(const char *plist, const char *pkg)
return (xbps_cmpver_versions(instver, reqver) > 0) ? 1 : 0; return (xbps_cmpver_versions(instver, reqver) > 0) ? 1 : 0;
} }
int void
xbps_check_reqdeps_in_pkg(const char *plist, prop_dictionary_t pkg) xbps_add_pkg_dependency(const char *pkgname, prop_dictionary_t dict)
{ {
pkg_dep_t *dep;
size_t len = 0;
assert(pkgname != NULL);
assert(dict != NULL);
LIST_FOREACH(dep, &pkg_deps_list, deps)
if (strcmp(dep->name, pkgname) == 0)
return;
dep = NULL;
dep = malloc(sizeof(*dep));
assert(dep != NULL);
len = strlen(pkgname) + 1;
dep->name = malloc(len);
if (dep->name == NULL)
return;
memcpy(dep->name, pkgname, len);
dep->name[len + 1] = '\0';
dep->dict = prop_dictionary_copy(dict);
LIST_INSERT_HEAD(&pkg_deps_list, dep, deps);
}
static bool
pkg_has_rundeps(prop_dictionary_t pkg)
{
prop_array_t array;
assert(pkg != NULL);
array = prop_dictionary_get(pkg, "run_depends");
if (array && prop_array_count(array) > 0)
return true;
return false;
}
static int
find_deps_in_pkg(const char *plist, prop_dictionary_t pkg)
{
prop_dictionary_t pkgdict;
prop_array_t array; prop_array_t array;
prop_object_t obj; prop_object_t obj;
prop_object_iterator_t iter; prop_object_iterator_t iter;
const char *reqpkg; const char *reqpkg;
int rv = 0; char *pkgname;
bool need_deps = false;
array = prop_dictionary_get(pkg, "run_depends");
if (array == NULL || prop_array_count(array) == 0)
return 0;
iter = prop_array_iterator(array);
if (iter == NULL)
return -1;
/* Iterate over the list of required run dependencies for a pkg */
while ((obj = prop_object_iterator_next(iter))) {
reqpkg = prop_string_cstring_nocopy(obj);
pkgname = xbps_get_pkg_name(reqpkg);
pkgdict = xbps_find_pkg_from_plist(plist, pkgname);
xbps_add_pkg_dependency(pkgname, pkgdict);
free(pkgname);
/* Iterate on required pkg to find more deps */
if (pkg_has_rundeps(pkgdict)) {
/* more deps? */
prop_object_iterator_release(iter);
if (!find_deps_in_pkg(plist, pkgdict)) {
prop_object_release(pkgdict);
return 0;
}
}
prop_object_release(pkgdict);
}
prop_object_iterator_release(iter);
return 0;
}
int
xbps_check_reqdeps_in_pkg(const char *plist, prop_dictionary_t pkg)
{
char repolist[PATH_MAX];
assert(pkg != NULL); assert(pkg != NULL);
assert(prop_object_type(pkg) == PROP_TYPE_DICTIONARY); assert(prop_object_type(pkg) == PROP_TYPE_DICTIONARY);
assert(prop_dictionary_count(pkg) != 0); assert(prop_dictionary_count(pkg) != 0);
assert(plist != NULL); assert(plist != NULL);
array = prop_dictionary_get(pkg, "run_depends"); if (!pkg_has_rundeps(pkg)) {
if (array == NULL) {
/* Package has no required rundeps */ /* Package has no required rundeps */
return 0; return 0;
} }
assert(prop_object_type(array) == PROP_TYPE_ARRAY); if (!xbps_append_full_path(repolist,
assert(prop_array_count(array) != 0); "/storage/xbps/binpkgs", XBPS_PKGINDEX)) {
errno = ENOENT;
iter = prop_array_iterator(array);
if (iter == NULL) {
errno = ENOMEM;
return -1; return -1;
} }
while ((obj = prop_object_iterator_next(iter))) { if (find_deps_in_pkg(repolist, pkg) == -1) {
assert(prop_object_type(obj) == PROP_TYPE_STRING); errno = XBPS_PKG_EINDEPS;
reqpkg = prop_string_cstring_nocopy(obj); return -1;
rv = xbps_check_is_installed_pkg(plist, reqpkg);
if (rv == XBPS_PKG_EEMPTY) {
/* No packages registered yet. */
need_deps = true;
printf("Package '%s' not installed\n", reqpkg);
//xbps_add_pkg_dependency(reqpkg);
} else if (rv == 1) {
need_deps = true;
printf("Package '%s' required.\n", reqpkg);
} else if (rv == 0) {
printf("Package '%s' already installed.\n", reqpkg);
}
} }
prop_object_iterator_release(iter); return 1;
return need_deps;
} }

View file

@ -51,33 +51,56 @@ xbps_install_binary_pkg(const char *pkgname, const char *dest)
/* Get pkg metadata from a repository */ /* Get pkg metadata from a repository */
repo_dict = prop_dictionary_internalize_from_file(repo); repo_dict = prop_dictionary_internalize_from_file(repo);
pkg_rdict = xbps_find_pkg_in_dict(repo_dict, pkgname); if (repo_dict == NULL)
if (pkg_rdict == NULL) return -1;
return XBPS_PKG_ENOTINREPO;
if (!xbps_append_full_path(dbfile, NULL, XBPS_REGPKGDB)) pkg_rdict = xbps_find_pkg_in_dict(repo_dict, pkgname);
if (pkg_rdict == NULL) {
prop_object_release(repo_dict);
return XBPS_PKG_ENOTINREPO;
}
if (!xbps_append_full_path(dbfile, NULL, XBPS_REGPKGDB)) {
prop_object_release(repo_dict);
return EINVAL; return EINVAL;
}
/* Check if package is already installed. */ /* Check if package is already installed. */
dict = prop_dictionary_internalize_from_file(dbfile); dict = prop_dictionary_internalize_from_file(dbfile);
if (dict && xbps_find_pkg_in_dict(dict, pkgname)) if (dict && xbps_find_pkg_in_dict(dict, pkgname)) {
prop_object_release(repo_dict);
prop_object_release(dict);
return XBPS_PKG_EEXIST; return XBPS_PKG_EEXIST;
}
/* Append filename to the full path for binary pkg */
obj = prop_dictionary_get(pkg_rdict, "filename");
if (!xbps_append_full_path(binfile, "/storage/xbps/binpkgs",
prop_string_cstring_nocopy(obj))) {
prop_object_release(repo_dict);
return EINVAL;
}
obj = prop_dictionary_get(pkg_rdict, "version");
printf("=> Found package: %s-%s.",
pkgname, prop_string_cstring_nocopy(obj));
printf("\n");
(void)fflush(stdout);
printf("==> Checking dependencies... ");
(void)fflush(stdout);
/* Looks like it's not, check dependencies and install */ /* Looks like it's not, check dependencies and install */
switch (xbps_check_reqdeps_in_pkg(dbfile, pkg_rdict)) { switch (xbps_check_reqdeps_in_pkg(dbfile, pkg_rdict)) {
case -1: case -1:
/* There was an error checking pkg deps */ /* There was an error checking pkg deps */
prop_object_release(repo_dict);
printf("error, exiting!\n");
fflush(stdout);
return XBPS_PKG_EINDEPS; return XBPS_PKG_EINDEPS;
case 0: case 0:
/* Package has no deps, just install it */ /* Package has no deps, just install it */
obj = prop_dictionary_get(pkg_rdict, "filename"); printf("none, unpacking... ");
strncpy(binfile, "/storage/xbps/binpkgs/", PATH_MAX - 1); (void)fflush(stdout);
strncat(binfile, prop_string_cstring_nocopy(obj), PATH_MAX - 1);
obj = prop_dictionary_get(pkg_rdict, "version");
printf("=> Installing %s-%s ... ", pkgname,
prop_string_cstring_nocopy(obj));
rv = xbps_unpack_binary_pkg(binfile, unpack_archive_cb); rv = xbps_unpack_binary_pkg(binfile, unpack_archive_cb);
break; break;
case 1: case 1:
@ -85,6 +108,7 @@ xbps_install_binary_pkg(const char *pkgname, const char *dest)
break; break;
} }
prop_object_release(repo_dict);
return rv; return rv;
} }

View file

@ -95,6 +95,31 @@ xbps_callback_array_iter_in_dict(prop_dictionary_t dict, const char *key,
return ret; return ret;
} }
prop_dictionary_t
xbps_find_pkg_from_plist(const char *plist, const char *pkgname)
{
prop_dictionary_t dict;
prop_dictionary_t obj;
assert(plist != NULL);
assert(pkgname != NULL);
dict = prop_dictionary_internalize_from_file(plist);
if (dict == NULL) {
errno = ENOENT;
return NULL;
}
obj = xbps_find_pkg_in_dict(dict, pkgname);
if (obj == NULL) {
prop_object_release(dict);
errno = ENOENT;
return NULL;
}
return obj;
}
prop_dictionary_t prop_dictionary_t
xbps_find_pkg_in_dict(prop_dictionary_t dict, const char *pkgname) xbps_find_pkg_in_dict(prop_dictionary_t dict, const char *pkgname)
{ {

96
lib/util.c Normal file
View file

@ -0,0 +1,96 @@
/*-
* Copyright (c) 2008 Juan Romero Pardines.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <xbps_api.h>
const char *
xbps_get_pkg_version(const char *pkg)
{
const char *tmp;
/* Get the required version */
tmp = strrchr(pkg, '-');
assert(tmp != NULL);
return tmp + 1; /* skip first '-' */
}
char *
xbps_get_pkg_name(const char *pkg)
{
const char *tmp;
char *pkgname;
size_t len = 0;
/* Get the required version */
tmp = strrchr(pkg, '-');
assert(tmp != NULL);
len = strlen(pkg) - strlen(tmp) + 1;
/* Get package name */
pkgname = malloc(len);
memcpy(pkgname, pkg, len - 1);
pkgname[len - 1] = '\0';
return pkgname;
}
bool
xbps_append_full_path(char *buf, const char *root, const char *plistf)
{
const char *env, *tmp;
size_t len = 0;
assert(buf != NULL);
assert(plistf != NULL);
if (root)
env = root;
else {
env = getenv("XBPS_META_PATH");
if (env == NULL)
env = XBPS_META_PATH;
}
tmp = strncpy(buf, env, PATH_MAX - 1);
if (sizeof(*tmp) >= PATH_MAX) {
errno = ENOSPC;
return false;
}
len = strlen(buf);
buf[len + 1] = '\0';
if (buf[len - 2] != '/')
strncat(buf, "/", 1);
strncat(buf, plistf, sizeof(buf) - strlen(buf) - 1);
return true;
}