Reorganize the tree splitting stuff on its own dir.

--HG--
extra : convert_revision : 87473fb499c42deaf0285f5559dc1cd8d67ab436
This commit is contained in:
Juan RP
2008-12-21 23:57:02 +01:00
parent 9019592d9d
commit e7cf222ac9
35 changed files with 98 additions and 69 deletions

33
bin/Makefile Normal file
View File

@@ -0,0 +1,33 @@
include ../vars.mk
EXTRA_CFLAGS = -funroll-all-loops -ftree-loop-linear
LDFLAGS += -L../lib -Wl,-rpath $$(pwd)/../lib -lxbps
BINS = xbps-bin xbps-cmpver xbps-digest xbps-pkgdb
all: $(BINS)
.PHONY: all
xbps-bin: xbps-bin.o
$(CC) $(LDFLAGS) $^ -o $@
xbps-cmpver: xbps-cmpver.o
$(CC) $^ -o $@
xbps-digest: xbps-digest.o
$(CC) $(EXTRA_CFLAGS) $(LDFLAGS) $^ -o $@
xbps-pkgdb: xbps-pkgdb.o
$(CC) $(LDFLAGS) $^ -o $@
.PHONY: clean
clean: clean-bins clean-objs
clean-bins:
-rm -f $(BINS)
clean-objs:
-rm -f *.o
install: $(BINS)
install -D $(BINS)

265
bin/xbps-bin.c Normal file
View File

@@ -0,0 +1,265 @@
/*-
* 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 <libgen.h>
#include <xbps_api.h>
typedef struct repository_info {
const char *index_version;
const char *location_local;
const char *location_remote;
size_t total_pkgs;
} repo_info_t;
static const char *sanitize_localpath(const char *);
static prop_dictionary_t getrepolist_dict(void);
static bool pkgindex_getinfo(prop_dictionary_t, repo_info_t *);
static void usage(void);
static void
usage(void)
{
printf("Usage: xbps-bin [action] [arguments]\n\n"
" Available actions:\n"
" repo-add, repo-list, repo-rm, search, show\n"
" Action arguments:\n"
" repo-add\t[<URI>]\n"
" repo-list\t[none]\n"
" repo-rm\t[<URI>]\n"
" search\t[<string>]\n"
" show\t[<pkgname>]\n"
"\n"
" Examples:\n"
" $ xbps-bin repo-add /path/to/directory\n"
" $ xbps-bin repo-add http://www.location.org/xbps-repo\n"
" $ xbps-bin repo-list\n"
" $ xbps-bin repo-rm /path/to/directory\n"
" $ xbps-bin search klibc\n"
" $ xbps-bin show klibc\n");
exit(1);
}
static bool
pkgindex_getinfo(prop_dictionary_t dict, repo_info_t *ri)
{
assert(dict != NULL || ri != NULL);
if (!prop_dictionary_get_cstring_nocopy(dict,
"pkgindex-version", &ri->index_version))
return false;
if (!prop_dictionary_get_cstring_nocopy(dict,
"location-local", &ri->location_local))
return false;
/* This one is optional, thus don't panic */
prop_dictionary_get_cstring_nocopy(dict, "location-remote",
&ri->location_remote);
if (!prop_dictionary_get_uint64(dict, "total-pkgs",
&ri->total_pkgs))
return false;
/* Reject empty repositories, how could this happen? :-) */
if (ri->total_pkgs <= 0)
return false;
return true;
}
static prop_dictionary_t
getrepolist_dict(void)
{
prop_dictionary_t dict;
dict = prop_dictionary_internalize_from_file(XBPS_REPOLIST_PATH);
if (dict == NULL) {
printf("ERROR: cannot find repository plist file (%s).\n",
strerror(errno));
exit(EINVAL);
}
return dict;
}
static const char *
sanitize_localpath(const char *path)
{
const char *res;
char strtmp[PATH_MAX];
char *dirnp, *basenp, *dir, *base;
dir = strdup(path);
if (dir == NULL)
return NULL;
base = strdup(path);
if (base == NULL)
goto fail;
dirnp = dirname(dir);
if (strcmp(dirnp, ".") == 0)
goto fail2;
basenp = basename(base);
if (strcmp(basenp, base) == 0)
goto fail2;
strncpy(strtmp, dirnp, sizeof(strtmp) - 1);
strtmp[sizeof(strtmp) - 1] = '\0';
if (strcmp(dirnp, "/"))
strncat(strtmp, "/", sizeof(strtmp) - strlen(strtmp) - 1);
strncat(strtmp, basenp, sizeof(strtmp) - strlen(strtmp) -1);
free(dir);
free(base);
res = strtmp;
return res;
fail:
free(dir);
fail2:
free(base);
return NULL;
}
int
main(int argc, char **argv)
{
prop_dictionary_t dict;
repo_info_t *rinfo = NULL;
const char *dpkgidx, *repolist;
if (argc < 2)
usage();
if (strcasecmp(argv[1], "repo-add") == 0) {
/* Adds a new repository to the pool. */
if (argc != 3)
usage();
dpkgidx = sanitize_localpath(argv[2]);
if (dpkgidx == NULL)
exit(EINVAL);
/* Temp buffer to verify pkgindex file. */
repolist = xbps_get_pkgidx_string(dpkgidx);
if (repolist == NULL)
exit(EINVAL);
dict = prop_dictionary_internalize_from_file(repolist);
if (dict == NULL) {
printf("Directory %s does not contain any "
"xbps pkgindex file.\n", dpkgidx);
exit(EINVAL);
}
rinfo = malloc(sizeof(*rinfo));
if (rinfo == NULL)
exit(ENOMEM);
if (!pkgindex_getinfo(dict, rinfo)) {
printf("'%s' is incomplete.\n", repolist);
free(rinfo);
exit(EINVAL);
}
if (!xbps_register_repository(dpkgidx)) {
printf("ERROR: couldn't register repository (%s)\n",
strerror(errno));
free(rinfo);
exit(EINVAL);
}
printf("Added repository at %s (%s) with %zu packages.\n",
rinfo->location_local, rinfo->index_version,
rinfo->total_pkgs);
free(rinfo);
} else if (strcasecmp(argv[1], "repo-list") == 0) {
/* Lists all repositories registered in pool. */
if (argc != 2)
usage();
xbps_callback_array_iter_in_dict(getrepolist_dict(),
"repository-list", xbps_list_strings_in_array, NULL);
} else if (strcasecmp(argv[1], "repo-rm") == 0) {
/* Remove a repository from the pool. */
if (argc != 3)
usage();
dpkgidx = sanitize_localpath(argv[2]);
if (dpkgidx == NULL)
exit(EINVAL);
if (!xbps_unregister_repository(dpkgidx)) {
if (errno == ENODEV)
printf("Repository '%s' not actually "
"registered.\n", dpkgidx);
else
printf("ERROR: couldn't unregister "
"repository (%s)\n", strerror(errno));
exit(EINVAL);
}
} else if (strcasecmp(argv[1], "search") == 0) {
/* Search for a package by looking at short_desc. */
if (argc != 3)
usage();
xbps_callback_array_iter_in_dict(getrepolist_dict(),
"repository-list", xbps_search_string_in_pkgs, argv[2]);
} else if (strcasecmp(argv[1], "show") == 0) {
/* Shows info about a binary package. */
if (argc != 3)
usage();
if (!xbps_callback_array_iter_in_dict(getrepolist_dict(),
"repository-list",
xbps_show_pkg_info_from_repolist, argv[2])) {
printf("ERROR: unable to locate package '%s'.\n",
argv[2]);
exit(EINVAL);
}
} else if (strcasecmp(argv[1], "install") == 0) {
/* Installs a binary package and required deps. */
if (argc != 3)
usage();
} else {
usage();
}
exit(0);
}

84
bin/xbps-cmpver.c Normal file
View File

@@ -0,0 +1,84 @@
/*
* Compare package and version strings
* @ 2008
* Author: pancake <youterm.com>
*/
#include <stdio.h>
#include <string.h>
#include <xbps_api.h>
static int chkchr(const char *ch)
{
if (*ch>='0' && *ch<='9')
return *ch-'0';
if (ch[1]=='\0') {
if (*ch>='a'&&*ch<='z'){
return *ch-'a';
}
}
switch(*ch) {
case 'a': if (ch[1]=='l')
return 0xa;
return -1;
case 'b': return 0xb;
case 'r': return 0xc;
}
return -1;
}
static int ver2int(const char *a0, int *pow, int mpow)
{
int r,ret = 0;
int pos = 0;
const char *a = a0+strlen(a0)-1;
for(*pow=0;a>=a0;a=a-1) {
if (*a=='.') {
*pow=*pow+1;
} else {
r = chkchr(a);
if (r != -1)
ret+=((r+1)*((*pow)+1))<<pos++;
}
if (mpow>0 && *pow > mpow)
break;
}
return ret;
}
int chkver(const char *a0, const char *a1)
{
int p0,p1;
int v0 = ver2int(a0, &p0, 0);
int v1 = ver2int(a1, &p1, p0);
return v1-v0;
}
int chkpkg(const char *a0, const char *b0)
{
char *a = strrchr(a0, '-');
char *b = strrchr(b0, '-');
assert(a != NULL || b != NULL);
return chkver(a+1, b+1);
}
#if UNIT_TEST
printf("1.2 2.2 = %d\n", chkver("1.2", "2.2"));
printf("1.0 10.3 = %d\n", chkver("1.0", "10.3"));
printf("1.0 1.0 = %d\n", chkver("1.0", "1.0"));
printf("1.0 1.2 = %d\n", chkver("1.0", "1.2"));
printf("1.0.1 1.0.2 = %d\n", chkver("1.0.1", "1.0.2"));
printf("1.0beta 1.0rc1 = %d\n", chkver("1.0beta", "1.0rc1"));
#endif
int main(int argc, char **argv)
{
if (argc<3) {
printf("Usage: ./xbps-cmpver [old] [new]\n");
printf(" ./xbpks-cmpver foo-1.2 foo-2.2 # $? = 1\n");
printf(" ./xbpks-cmpver foo-1.2 foo-1.2 # $? = 0\n");
return 1;
}
return (chkpkg(argv[1], argv[2]) > 0)?1:0;
}

82
bin/xbps-digest.c Normal file
View File

@@ -0,0 +1,82 @@
/*-
* 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 <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <inttypes.h>
#include <libgen.h>
#include <xbps_api.h>
static void
usage(void)
{
fprintf(stderr, "usage: xbps-digest <file> <file1+N> ...\n");
exit(1);
}
int
main(int argc, char **argv)
{
SHA256_CTX ctx;
uint8_t buffer[BUFSIZ * 20], *digest;
ssize_t bytes;
int i, fd;
if (argc < 2)
usage();
for (i = 1; i < argc; i++) {
if ((fd = open(argv[i], O_RDONLY)) == -1) {
printf("xbps-digest: cannot open %s (%s)\n", argv[i],
strerror(errno));
exit(1);
}
digest = malloc(SHA256_DIGEST_LENGTH * 2 + 1);
if (digest == NULL) {
printf("xbps-digest: malloc failed (%s)\n",
strerror(errno));
exit(1);
}
SHA256_Init(&ctx);
while ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
SHA256_Update(&ctx, buffer, (size_t)bytes);
printf("%s\n", SHA256_End(&ctx, digest));
free(digest);
close(fd);
}
exit(0);
}

273
bin/xbps-pkgdb.c Normal file
View File

@@ -0,0 +1,273 @@
/*-
* 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>
typedef struct pkg_data {
const char *pkgname;
const char *version;
const char *short_desc;
} pkg_data_t;
static prop_dictionary_t make_dict_from_pkg(pkg_data_t *);
static void register_pkg(prop_dictionary_t, pkg_data_t *, const char *);
static void write_plist_file(prop_dictionary_t, const char *);
static prop_dictionary_t
make_dict_from_pkg(pkg_data_t *pkg)
{
prop_dictionary_t dict;
assert(pkg != NULL || pkg->pkgname != NULL);
assert(pkg->version != NULL || pkg->short_desc != NULL);
dict = prop_dictionary_create();
assert(dict != NULL);
prop_dictionary_set_cstring_nocopy(dict, "pkgname", pkg->pkgname);
prop_dictionary_set_cstring_nocopy(dict, "version", pkg->version);
prop_dictionary_set_cstring_nocopy(dict, "short_desc", pkg->short_desc);
return dict;
}
static void
register_pkg(prop_dictionary_t dict, pkg_data_t *pkg, const char *dbfile)
{
prop_dictionary_t pkgdict;
prop_array_t array;
assert(dict != NULL || pkg != NULL || dbfile != NULL);
pkgdict = make_dict_from_pkg(pkg);
assert(pkgdict != NULL);
array = prop_dictionary_get(dict, "packages");
assert(array != NULL);
assert(prop_object_type(array) == PROP_TYPE_ARRAY);
if (!xbps_add_obj_to_array(array, pkgdict)) {
printf("ERROR: couldn't register '%s-%s' in database!\n",
pkg->pkgname, pkg->version);
exit(1);
}
write_plist_file(dict, dbfile);
}
static void
write_plist_file(prop_dictionary_t dict, const char *file)
{
assert(dict != NULL || file != NULL);
if (!prop_dictionary_externalize_to_file(dict, file)) {
prop_object_release(dict);
printf("=> ERROR: couldn't write to %s (%s)",
file, strerror(errno));
exit(1);
}
}
static void
usage(void)
{
printf("usage: xbps-pkgdb <action> [args]\n\n"
" Available actions:\n"
" list, register, sanitize-plist, unregister, version\n"
" Action arguments:\n"
" list\t[none]\n"
" register\t[<pkgname> <version> <shortdesc>]\n"
" sanitize-plist\t[<plist>]\n"
" unregister\t[<pkgname> <version>]\n"
" version\t[<pkgname>]\n"
" Environment:\n"
" XBPS_REGPKGDB_PATH\tPath to xbps pkgdb plist file\n\n"
" Examples:\n"
" $ xbps-pkgdb list\n"
" $ xbps-pkgdb register pkgname 2.0 \"A short description\"\n"
" $ xbps-pkgdb sanitize-plist /blah/foo.plist\n"
" $ xbps-pkgdb unregister pkgname 2.0\n"
" $ xbps-pkgdb version pkgname\n");
exit(1);
}
int
main(int argc, char **argv)
{
prop_dictionary_t dbdict = NULL, pkgdict;
prop_array_t dbarray = NULL;
pkg_data_t pkg;
const char *version;
char dbfile[PATH_MAX], *dbfileenv, *tmppath, *in_chroot_env;
bool in_chroot = false;
if (argc < 2)
usage();
if ((dbfileenv = getenv("XBPS_REGPKGDB_PATH")) != NULL) {
/* Use path as defined by XBPS_REGPKGDB_PATH env var */
tmppath = strncpy(dbfile, dbfileenv, sizeof(dbfile) - 1);
if (sizeof(*tmppath) >= sizeof(dbfile))
exit(1);
} else {
/* Use default path */
tmppath =
strncpy(dbfile, XBPS_REGPKGDB_DEFPATH, sizeof(dbfile) - 1);
if (sizeof(*tmppath) >= sizeof(dbfile))
exit(1);
}
/* nul terminate string */
dbfile[sizeof(dbfile) - 1] = '\0';
in_chroot_env = getenv("in_chroot");
if (in_chroot_env != NULL)
in_chroot = true;
if (strcasecmp(argv[1], "register") == 0) {
/* Registers a package into the database */
if (argc != 5)
usage();
dbdict = prop_dictionary_internalize_from_file(dbfile);
if (dbdict == NULL) {
/* Create package dictionary and add its objects. */
pkg.pkgname = argv[2];
pkg.version = argv[3];
pkg.short_desc = argv[4];
pkgdict = make_dict_from_pkg(&pkg);
assert(pkgdict != NULL);
/* Add pkg dictionary into array. */
dbarray = prop_array_create();
if (!xbps_add_obj_to_array(dbarray, pkgdict)) {
printf("=> ERROR: couldn't register "
"%s-%s (%s)\n", pkg.pkgname, pkg.version,
strerror(errno));
exit(1);
}
/* Add array into main dictionary. */
dbdict = prop_dictionary_create();
if (!xbps_add_obj_to_dict(dbdict, dbarray,
"packages")) {
printf("=> ERROR: couldn't register "
"%s-%s (%s)\n", pkg.pkgname, pkg.version,
strerror(errno));
exit(1);
}
/* Write main dictionary to file. */
write_plist_file(dbdict, dbfile);
printf("%s==> Package database file not found, "
"creating it.\n", in_chroot ? "[chroot] " : "");
prop_object_release(dbdict);
} else {
/* Check if pkg is already registered. */
pkgdict = xbps_find_pkg_in_dict(dbdict, argv[2]);
if (pkgdict != NULL) {
printf("%s=> Package %s-%s already registered.\n",
in_chroot ? "[chroot] " : "",
argv[2], argv[3]);
exit(0);
}
pkg.pkgname = argv[2];
pkg.version = argv[3];
pkg.short_desc = argv[4];
register_pkg(dbdict, &pkg, dbfile);
}
printf("%s=> %s-%s registered successfully.\n",
in_chroot ? "[chroot] " : "", argv[2], argv[3]);
} else if (strcasecmp(argv[1], "unregister") == 0) {
/* Unregisters a package from the database */
if (argc != 4)
usage();
if (!xbps_remove_pkg_dict_from_file(argv[2], dbfile)) {
if (errno == ENODEV)
printf("=> ERROR: %s not registered "
"in database.\n", argv[2]);
else
printf("=> ERROR: couldn't unregister %s "
"from database (%s)\n", argv[2],
strerror(errno));
exit(EINVAL);
}
printf("%s=> %s-%s unregistered successfully.\n",
in_chroot ? "[chroot] " : "", argv[2], argv[3]);
} else if (strcasecmp(argv[1], "list") == 0) {
/* Lists packages currently registered in database */
if (argc != 2)
usage();
dbdict = prop_dictionary_internalize_from_file(dbfile);
if (!xbps_callback_array_iter_in_dict(dbdict,
"packages", xbps_list_pkgs_in_dict, NULL))
exit(EINVAL);
} else if (strcasecmp(argv[1], "version") == 0) {
/* Prints version of an installed package */
if (argc != 3)
usage();
pkgdict = xbps_find_pkg_in_dict(
prop_dictionary_internalize_from_file(dbfile), argv[2]);
if (pkgdict == NULL)
exit(1);
if (!prop_dictionary_get_cstring_nocopy(pkgdict, "version",
&version))
exit(1);
printf("%s\n", version);
} else if (strcasecmp(argv[1], "sanitize-plist") == 0) {
/* Sanitize a plist file (indent the file properly) */
if (argc != 3)
usage();
dbdict = prop_dictionary_internalize_from_file(argv[2]);
if (dbdict == NULL) {
printf("=> ERROR: couldn't sanitize %s plist file "
"(%s)\n", argv[2], strerror(errno));
exit(1);
}
write_plist_file(dbdict, argv[2]);
} else {
usage();
}
exit(0);
}