A bit of reorganization in C/shell code.
With these changes: - Added 'sanitize-plist' action to xbps-pkgdb. It takes a plist file as argument and writes another one "sanitized" by proplib. Use it to sanitize the pkgindex plist file. - Split xbps-pkgdb functions to be shared by other files. - Split xbps-digest functions to be shared by other files. - Rename the plist file to register/unregister installed pkgs to regpkgdb.plist, and related stuff in shell scripts. --HG-- extra : convert_revision : 37731b04c6b41aebac629dfa06106175b9b5e59c
This commit is contained in:
@@ -30,9 +30,7 @@
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include <prop/proplib.h>
|
||||
|
||||
#define _XBPS_PKGDB_DEFPATH "/var/cache/xbps/pkgdb.plist"
|
||||
#include "xbps_api.h"
|
||||
|
||||
typedef struct pkg_data {
|
||||
const char *pkgname;
|
||||
@@ -40,101 +38,10 @@ typedef struct pkg_data {
|
||||
const char *short_desc;
|
||||
} pkg_data_t;
|
||||
|
||||
static void usage(void);
|
||||
static void add_array_to_dict(prop_dictionary_t, prop_array_t, const char *);
|
||||
static void add_obj_to_array(prop_array_t, prop_object_t);
|
||||
static prop_dictionary_t make_dict_from_pkg(pkg_data_t *);
|
||||
static prop_dictionary_t find_pkg_in_dict(prop_dictionary_t, const char *);
|
||||
static void register_pkg(prop_dictionary_t, pkg_data_t *, const char *);
|
||||
static void unregister_pkg(prop_dictionary_t, const char *, const char *);
|
||||
static void write_plist_file(prop_dictionary_t, const char *);
|
||||
static void list_pkgs_in_dict(prop_dictionary_t);
|
||||
static prop_dictionary_t get_dict_from_dbfile(const char *);
|
||||
|
||||
static void
|
||||
usage(void)
|
||||
{
|
||||
printf("usage: xbps-pkgdb <action> [args]\n");
|
||||
printf("\n");
|
||||
printf(" Available actions:\n");
|
||||
printf(" list, register, unregister, version\n");
|
||||
printf(" Action arguments:\n");
|
||||
printf(" list\t[none]\n");
|
||||
printf(" register\t[<pkgname> <version> <shortdesc>]\n");
|
||||
printf(" unregister\t[<pkgname> <version>]\n");
|
||||
printf(" version\t[<pkgname>]\n");
|
||||
printf(" Environment:\n");
|
||||
printf(" XBPS_PKGDB_FPATH\tPath to xbps pkgdb plist file\n");
|
||||
printf("\n");
|
||||
printf(" Examples:\n");
|
||||
printf(" $ xbps-pkgdb list\n");
|
||||
printf(" $ xbps-pkgdb register pkgname 2.0 \"A short description\"\n");
|
||||
printf(" $ xbps-pkgdb unregister pkgname 2.0\n");
|
||||
printf(" $ xbps-pkgdb version pkgname\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static prop_dictionary_t
|
||||
find_pkg_in_dict(prop_dictionary_t dict, const char *pkgname)
|
||||
{
|
||||
prop_array_t array;
|
||||
prop_object_iterator_t iter;
|
||||
prop_object_t obj;
|
||||
const char *dpkgn;
|
||||
|
||||
if (dict == NULL || pkgname == NULL)
|
||||
return NULL;
|
||||
|
||||
array = prop_dictionary_get(dict, "packages_installed");
|
||||
if (array == NULL || prop_object_type(array) != PROP_TYPE_ARRAY)
|
||||
return NULL;
|
||||
|
||||
iter = prop_array_iterator(array);
|
||||
if (iter == NULL)
|
||||
return NULL;
|
||||
|
||||
while ((obj = prop_object_iterator_next(iter))) {
|
||||
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &dpkgn);
|
||||
if (strcmp(dpkgn, pkgname) == 0)
|
||||
break;
|
||||
}
|
||||
prop_object_iterator_release(iter);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
static void
|
||||
add_obj_to_array(prop_array_t array, prop_object_t obj)
|
||||
{
|
||||
if (array == NULL || obj == NULL) {
|
||||
printf("%s: NULL array/obj\n", __func__);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (!prop_array_add(array, obj)) {
|
||||
prop_object_release(array);
|
||||
printf("%s: couldn't add obj into array: %s\n", __func__,
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
prop_object_release(obj);
|
||||
}
|
||||
|
||||
static void
|
||||
add_array_to_dict(prop_dictionary_t dict, prop_array_t array, const char *key)
|
||||
{
|
||||
if (dict == NULL || array == NULL || key == NULL) {
|
||||
printf("%s: NULL dict/array/key\n", __func__);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (!prop_dictionary_set(dict, key, array)) {
|
||||
printf("%s: couldn't add array (%s): %s\n",
|
||||
__func__, key, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
prop_object_release(array);
|
||||
}
|
||||
|
||||
static prop_dictionary_t
|
||||
make_dict_from_pkg(pkg_data_t *pkg)
|
||||
@@ -179,7 +86,11 @@ register_pkg(prop_dictionary_t dict, pkg_data_t *pkg, const char *dbfile)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
add_obj_to_array(array, pkgdict);
|
||||
if (!xbps_add_obj_to_array(array, pkgdict)) {
|
||||
printf("ERROR: couldn't register package in database!\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
write_plist_file(dict, dbfile);
|
||||
}
|
||||
|
||||
@@ -226,7 +137,12 @@ unregister_pkg(prop_dictionary_t dict, const char *pkgname, const char *dbfile)
|
||||
}
|
||||
|
||||
prop_array_remove(array, i);
|
||||
add_array_to_dict(dict, array, "packages_installed");
|
||||
if (!xbps_add_array_to_dict(dict, array, "packages_installed")) {
|
||||
printf("=> ERROR: couldn't unregister %s from database\n",
|
||||
pkgname);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
write_plist_file(dict, dbfile);
|
||||
}
|
||||
|
||||
@@ -246,49 +162,26 @@ write_plist_file(prop_dictionary_t dict, const char *file)
|
||||
}
|
||||
|
||||
static void
|
||||
list_pkgs_in_dict(prop_dictionary_t dict)
|
||||
usage(void)
|
||||
{
|
||||
prop_array_t array;
|
||||
prop_object_t obj;
|
||||
prop_object_iterator_t iter;
|
||||
const char *pkgname, *version, *short_desc;
|
||||
|
||||
if (dict == NULL)
|
||||
exit(1);
|
||||
|
||||
array = prop_dictionary_get(dict, "packages_installed");
|
||||
if (array == NULL || prop_object_type(array) != PROP_TYPE_ARRAY) {
|
||||
printf("%s: NULL or incorrect array type\n", __func__);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
iter = prop_array_iterator(array);
|
||||
if (iter == NULL) {
|
||||
printf("%s: NULL iter\n", __func__);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
while ((obj = prop_object_iterator_next(iter))) {
|
||||
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
|
||||
prop_dictionary_get_cstring_nocopy(obj, "version", &version);
|
||||
prop_dictionary_get_cstring_nocopy(obj, "short_desc", &short_desc);
|
||||
if (pkgname && version && short_desc)
|
||||
printf("%s (%s)\t%s\n", pkgname, version, short_desc);
|
||||
}
|
||||
prop_object_iterator_release(iter);
|
||||
}
|
||||
|
||||
static prop_dictionary_t
|
||||
get_dict_from_dbfile(const char *file)
|
||||
{
|
||||
prop_dictionary_t dict;
|
||||
|
||||
dict = prop_dictionary_internalize_from_file(file);
|
||||
if (dict == NULL) {
|
||||
perror("=> ERROR: couldn't find database file");
|
||||
exit(1);
|
||||
}
|
||||
return dict;
|
||||
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
|
||||
@@ -304,15 +197,15 @@ main(int argc, char **argv)
|
||||
if (argc < 2)
|
||||
usage();
|
||||
|
||||
if ((dbfileenv = getenv("XBPS_PKGDB_FPATH")) != NULL) {
|
||||
/* Use path as defined by XBPS_PKGDB_FPATH env var */
|
||||
if ((dbfileenv = getenv("XBPS_REGPKGDB_PATH")) != NULL) {
|
||||
/* Use path as defined by XBPS_REGPKGDB_PATH env var */
|
||||
tmppath = strncpy(dbfile, dbfileenv, sizeof(dbfile));
|
||||
if (sizeof(*tmppath) >= sizeof(dbfile))
|
||||
exit(1);
|
||||
} else {
|
||||
/* Use default path */
|
||||
tmppath =
|
||||
strncpy(dbfile, _XBPS_PKGDB_DEFPATH, sizeof(dbfile));
|
||||
strncpy(dbfile, XBPS_REGPKGDB_DEFPATH, sizeof(dbfile));
|
||||
if (sizeof(*tmppath) >= sizeof(dbfile))
|
||||
exit(1);
|
||||
}
|
||||
@@ -342,11 +235,18 @@ main(int argc, char **argv)
|
||||
|
||||
/* Add pkg dictionary into array. */
|
||||
dbarray = prop_array_create();
|
||||
add_obj_to_array(dbarray, pkgdict);
|
||||
if (!xbps_add_obj_to_array(dbarray, pkgdict)) {
|
||||
printf("=> ERROR: couldn't register pkg\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Add array into main dictionary. */
|
||||
dbdict = prop_dictionary_create();
|
||||
add_array_to_dict(dbdict, dbarray, "packages_installed");
|
||||
if (!xbps_add_array_to_dict(dbdict, dbarray,
|
||||
"packages_installed")) {
|
||||
printf("=> ERROR: couldn't register pkg\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Write main dictionary to file. */
|
||||
write_plist_file(dbdict, dbfile);
|
||||
@@ -357,7 +257,8 @@ main(int argc, char **argv)
|
||||
prop_object_release(dbdict);
|
||||
} else {
|
||||
/* Check if pkg is already registered. */
|
||||
pkgdict = find_pkg_in_dict(dbdict, argv[2]);
|
||||
pkgdict = xbps_find_pkg_in_dict(dbdict,
|
||||
"packages_installed", argv[2]);
|
||||
if (pkgdict != NULL) {
|
||||
printf("%s=> Package %s-%s already registered.\n",
|
||||
in_chroot ? "[chroot] " : "",
|
||||
@@ -379,7 +280,8 @@ main(int argc, char **argv)
|
||||
if (argc != 4)
|
||||
usage();
|
||||
|
||||
unregister_pkg(get_dict_from_dbfile(dbfile), argv[2], dbfile);
|
||||
unregister_pkg(prop_dictionary_internalize_from_file(dbfile),
|
||||
argv[2], dbfile);
|
||||
|
||||
printf("%s=> %s-%s unregistered successfully.\n",
|
||||
in_chroot ? "[chroot] " : "", argv[2], argv[3]);
|
||||
@@ -389,20 +291,40 @@ main(int argc, char **argv)
|
||||
if (argc != 2)
|
||||
usage();
|
||||
|
||||
list_pkgs_in_dict(get_dict_from_dbfile(dbfile));
|
||||
dbdict = prop_dictionary_internalize_from_file(dbfile);
|
||||
xbps_list_pkgs_in_dict(dbdict, "packages_installed");
|
||||
|
||||
} else if (strcmp(argv[1], "version") == 0) {
|
||||
/* Prints version of an installed package */
|
||||
if (argc != 3)
|
||||
usage();
|
||||
|
||||
pkgdict = find_pkg_in_dict(get_dict_from_dbfile(dbfile), argv[2]);
|
||||
pkgdict = xbps_find_pkg_in_dict(
|
||||
prop_dictionary_internalize_from_file(dbfile),
|
||||
"packages_installed", argv[2]);
|
||||
if (pkgdict == NULL)
|
||||
exit(1);
|
||||
if (!prop_dictionary_get_cstring_nocopy(pkgdict, "version", &version))
|
||||
if (!prop_dictionary_get_cstring_nocopy(pkgdict, "version",
|
||||
&version))
|
||||
exit(1);
|
||||
printf("%s\n", version);
|
||||
|
||||
} else if (strcmp(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\n",
|
||||
argv[2]);
|
||||
exit(1);
|
||||
}
|
||||
if (!prop_dictionary_externalize_to_file(dbdict, argv[2])) {
|
||||
printf("=> ERROR: couldn't write new plist file\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
} else {
|
||||
usage();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user