xbps/bin/xbps-pkgdb/convert.c

119 lines
3.5 KiB
C

/*-
* Copyright (c) 2013 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 <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#include <xbps_api.h>
#include "defs.h"
/*
* Convert pkgdb format to 0.21.
*/
static void
pkgdb_format_021(struct xbps_handle *xhp, const char *plist_new)
{
prop_array_t array, rdeps;
prop_dictionary_t pkgdb, pkgd;
size_t i;
char *pkgname, *plist;
plist = xbps_xasprintf("%s/pkgdb.plist", xhp->metadir);
if (access(plist, R_OK) == -1) {
if (errno == ENOENT) {
/* missing file, no conversion needed */
free(plist);
return;
}
xbps_error_printf("cannot read %s: %s\n",
plist, strerror(errno));
exit(EXIT_FAILURE);
}
array = prop_array_internalize_from_zfile(plist);
if (prop_object_type(array) != PROP_TYPE_ARRAY) {
xbps_error_printf("unknown object type for %s\n",
plist, strerror(errno));
exit(EXIT_FAILURE);
}
pkgdb = prop_dictionary_create();
assert(pkgdb);
for (i = 0; i < prop_array_count(array); i++) {
pkgd = prop_array_get(array, i);
prop_dictionary_get_cstring(pkgd, "pkgname", &pkgname);
rdeps = prop_dictionary_get(pkgd, "run_depends");
/* remove unneeded objs */
prop_dictionary_remove(pkgd, "pkgname");
prop_dictionary_remove(pkgd, "version");
if (prop_array_count(rdeps) == 0)
prop_dictionary_remove(pkgd, "run_depends");
prop_dictionary_set(pkgdb, pkgname, pkgd);
free(pkgname);
}
if (prop_array_count(array) != prop_dictionary_count(pkgdb)) {
xbps_error_printf("failed conversion! unmatched obj count "
"(got %zu, need %zu)\n", prop_dictionary_count(pkgdb),
prop_array_count(array));
exit(EXIT_FAILURE);
}
if (!prop_dictionary_externalize_to_file(pkgdb, plist_new)) {
xbps_error_printf("failed to write %s: %s\n",
plist_new, strerror(errno));
exit(EXIT_FAILURE);
}
prop_object_release(array);
prop_object_release(pkgdb);
free(plist);
printf("Conversion to 0.21 pkgdb format successfully\n");
}
void
convert_pkgdb_format(struct xbps_handle *xhp)
{
char *plist;
plist = xbps_xasprintf("%s/%s", xhp->metadir, XBPS_PKGDB);
if ((access(plist, R_OK) == -1) && (errno == ENOENT))
pkgdb_format_021(xhp, plist);
free(plist);
}