2012-01-15 18:54:44 +05:30
|
|
|
/*-
|
|
|
|
* Copyright (c) 2012 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 <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <xbps_api.h>
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
struct index_files_data {
|
2012-01-19 16:56:40 +05:30
|
|
|
prop_array_t idx;
|
2012-01-15 18:54:44 +05:30
|
|
|
prop_array_t idxfiles;
|
2012-01-17 16:46:54 +05:30
|
|
|
prop_array_t obsoletes;
|
2012-01-15 18:54:44 +05:30
|
|
|
const char *pkgdir;
|
2012-05-31 21:04:17 +05:30
|
|
|
const char *targetarch;
|
2012-01-17 15:20:35 +05:30
|
|
|
bool flush;
|
|
|
|
bool new;
|
2012-01-15 18:54:44 +05:30
|
|
|
};
|
|
|
|
|
2012-01-17 15:20:35 +05:30
|
|
|
static int
|
|
|
|
rmobsoletes_files_cb(prop_object_t obj, void *arg, bool *done)
|
|
|
|
{
|
|
|
|
struct index_files_data *ifd = arg;
|
2012-05-31 19:59:56 +05:30
|
|
|
const char *pkgver, *arch;
|
2012-05-31 21:04:17 +05:30
|
|
|
char *str;
|
2012-01-17 15:20:35 +05:30
|
|
|
|
|
|
|
(void)done;
|
|
|
|
|
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
2012-05-31 19:59:56 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "architecture", &arch);
|
|
|
|
if (xbps_find_pkg_in_array_by_pkgver(ifd->idx, pkgver, arch)) {
|
2012-01-17 15:20:35 +05:30
|
|
|
/* pkg found, do nothing */
|
|
|
|
return 0;
|
|
|
|
}
|
2012-05-31 21:04:17 +05:30
|
|
|
if ((str = xbps_xasprintf("%s,%s", pkgver, arch)) == NULL)
|
|
|
|
return ENOMEM;
|
2012-01-17 15:20:35 +05:30
|
|
|
|
2012-05-31 21:04:17 +05:30
|
|
|
if (!prop_array_add_cstring(ifd->obsoletes, str)) {
|
|
|
|
free(str);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
free(str);
|
2012-01-17 16:46:54 +05:30
|
|
|
ifd->flush = true;
|
2012-05-31 21:04:17 +05:30
|
|
|
|
2012-01-17 15:20:35 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-15 18:54:44 +05:30
|
|
|
static int
|
|
|
|
genindex_files_cb(prop_object_t obj, void *arg, bool *done)
|
|
|
|
{
|
2012-05-31 19:59:56 +05:30
|
|
|
prop_object_t obj2, fileobj;
|
2012-01-17 15:20:35 +05:30
|
|
|
prop_dictionary_t pkg_filesd, pkgd, regpkgd;
|
2012-05-31 19:59:56 +05:30
|
|
|
prop_array_t array, files;
|
2012-01-15 18:54:44 +05:30
|
|
|
struct index_files_data *ifd = arg;
|
2012-06-01 04:01:18 +05:30
|
|
|
const char *binpkg, *pkgver, *rpkgver, *arch;
|
|
|
|
char *file;
|
2012-01-15 18:54:44 +05:30
|
|
|
bool found = false;
|
2012-05-31 19:59:56 +05:30
|
|
|
size_t i;
|
2012-01-15 18:54:44 +05:30
|
|
|
|
|
|
|
(void)done;
|
|
|
|
|
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "filename", &binpkg);
|
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
2012-05-31 19:59:56 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "architecture", &arch);
|
2012-01-15 18:54:44 +05:30
|
|
|
|
2012-01-17 15:20:35 +05:30
|
|
|
if (ifd->new)
|
|
|
|
goto start;
|
|
|
|
|
2012-06-01 04:01:18 +05:30
|
|
|
regpkgd = xbps_find_pkg_in_array_by_pkgver(ifd->idxfiles, pkgver, arch);
|
2012-01-17 15:20:35 +05:30
|
|
|
if (regpkgd) {
|
|
|
|
/*
|
|
|
|
* pkg already registered, check if same version
|
|
|
|
* is registered.
|
|
|
|
*/
|
|
|
|
prop_dictionary_get_cstring_nocopy(regpkgd, "pkgver", &rpkgver);
|
|
|
|
if (strcmp(pkgver, rpkgver) == 0) {
|
|
|
|
/* same pkg */
|
|
|
|
xbps_warn_printf("skipping `%s', already registered.\n",
|
|
|
|
rpkgver);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* pkgver does not match, remove it from index-files */
|
2012-05-31 19:59:56 +05:30
|
|
|
if (!xbps_remove_pkg_from_array_by_pkgver(ifd->idxfiles,
|
|
|
|
rpkgver, arch))
|
2012-01-17 15:20:35 +05:30
|
|
|
return EINVAL;
|
2012-06-01 04:01:18 +05:30
|
|
|
printf("Removed obsolete entry for `%s' from "
|
|
|
|
"files index.\n", rpkgver);
|
2012-01-17 15:20:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
start:
|
2012-05-31 19:59:56 +05:30
|
|
|
file = xbps_xasprintf("%s/%s/%s", ifd->pkgdir, arch, binpkg);
|
2012-01-15 18:54:44 +05:30
|
|
|
if (file == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
/* internalize files.plist from binary package archive */
|
|
|
|
pkg_filesd = xbps_dictionary_metadata_plist_by_url(file, XBPS_PKGFILES);
|
|
|
|
if (pkg_filesd == NULL) {
|
|
|
|
free(file);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
free(file);
|
|
|
|
|
|
|
|
/* create pkg dictionary */
|
2012-05-31 19:59:56 +05:30
|
|
|
if ((pkgd = prop_dictionary_create()) == NULL) {
|
2012-01-15 18:54:44 +05:30
|
|
|
prop_object_release(pkg_filesd);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
2012-05-31 19:59:56 +05:30
|
|
|
/* add pkgver and architecture objects into pkg dictionary */
|
|
|
|
if (!prop_dictionary_set_cstring(pkgd, "architecture", arch)) {
|
|
|
|
prop_object_release(pkg_filesd);
|
|
|
|
prop_object_release(pkgd);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
if (!prop_dictionary_set_cstring(pkgd, "pkgver", pkgver)) {
|
|
|
|
prop_object_release(pkg_filesd);
|
|
|
|
prop_object_release(pkgd);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
/* add files array obj into pkg dictionary */
|
|
|
|
if ((files = prop_array_create()) == NULL) {
|
|
|
|
prop_object_release(pkg_filesd);
|
|
|
|
prop_object_release(pkgd);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
if (!prop_dictionary_set(pkgd, "files", files)) {
|
|
|
|
prop_object_release(pkg_filesd);
|
|
|
|
prop_object_release(pkgd);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add conf_files in pkgd */
|
2012-01-15 18:54:44 +05:30
|
|
|
array = prop_dictionary_get(pkg_filesd, "conf_files");
|
|
|
|
if (array != NULL && prop_array_count(array)) {
|
|
|
|
found = true;
|
2012-05-31 19:59:56 +05:30
|
|
|
for (i = 0; i < prop_array_count(array); i++) {
|
|
|
|
obj2 = prop_array_get(array, i);
|
|
|
|
fileobj = prop_dictionary_get(obj2, "file");
|
|
|
|
if (!prop_array_add(files, fileobj)) {
|
|
|
|
prop_object_release(pkgd);
|
|
|
|
prop_object_release(pkg_filesd);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
2012-01-15 18:54:44 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
/* add files array in pkgd */
|
|
|
|
array = prop_dictionary_get(pkg_filesd, "files");
|
|
|
|
if (array != NULL && prop_array_count(array)) {
|
|
|
|
found = true;
|
2012-05-31 19:59:56 +05:30
|
|
|
for (i = 0; i < prop_array_count(array); i++) {
|
|
|
|
obj2 = prop_array_get(array, i);
|
|
|
|
fileobj = prop_dictionary_get(obj2, "file");
|
|
|
|
if (!prop_array_add(files, fileobj)) {
|
|
|
|
prop_object_release(pkgd);
|
|
|
|
prop_object_release(pkg_filesd);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
2012-01-15 18:54:44 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
/* add links array in pkgd */
|
|
|
|
array = prop_dictionary_get(pkg_filesd, "links");
|
|
|
|
if (array != NULL && prop_array_count(array)) {
|
|
|
|
found = true;
|
2012-05-31 19:59:56 +05:30
|
|
|
for (i = 0; i < prop_array_count(array); i++) {
|
|
|
|
obj2 = prop_array_get(array, i);
|
|
|
|
fileobj = prop_dictionary_get(obj2, "file");
|
|
|
|
if (!prop_array_add(files, fileobj)) {
|
|
|
|
prop_object_release(pkgd);
|
|
|
|
prop_object_release(pkg_filesd);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
2012-01-15 18:54:44 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
prop_object_release(pkg_filesd);
|
|
|
|
if (!found) {
|
|
|
|
prop_object_release(pkgd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* add pkgd into provided array */
|
|
|
|
if (!prop_array_add(ifd->idxfiles, pkgd)) {
|
|
|
|
prop_object_release(pkgd);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
prop_object_release(pkgd);
|
2012-01-17 15:20:35 +05:30
|
|
|
ifd->flush = true;
|
|
|
|
printf("Registered `%s' in repository files index.\n", pkgver);
|
2012-01-15 18:54:44 +05:30
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the index files cache for all packages in repository.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
repo_genindex_files(const char *pkgdir)
|
|
|
|
{
|
2012-01-19 16:56:40 +05:30
|
|
|
prop_array_t idx;
|
2012-01-17 15:25:28 +05:30
|
|
|
struct index_files_data *ifd = NULL;
|
2012-06-01 03:18:58 +05:30
|
|
|
size_t i, x;
|
|
|
|
const char *p, *arch;
|
|
|
|
char *plist, *pkgver;
|
2012-01-15 18:54:44 +05:30
|
|
|
int rv;
|
|
|
|
|
|
|
|
plist = xbps_pkg_index_plist(pkgdir);
|
|
|
|
if (plist == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
/* internalize repository index plist */
|
2012-01-19 16:56:40 +05:30
|
|
|
idx = prop_array_internalize_from_zfile(plist);
|
|
|
|
if (idx == NULL) {
|
2012-01-15 18:54:44 +05:30
|
|
|
free(plist);
|
|
|
|
return errno;
|
|
|
|
}
|
2012-01-17 15:20:35 +05:30
|
|
|
free(plist);
|
2012-01-15 18:54:44 +05:30
|
|
|
|
2012-01-17 15:20:35 +05:30
|
|
|
/* internalize repository index-files plist (if exists) */
|
|
|
|
plist = xbps_pkg_index_files_plist(pkgdir);
|
|
|
|
if (plist == NULL) {
|
|
|
|
rv = ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
ifd = calloc(1, sizeof(*ifd));
|
2012-01-15 18:54:44 +05:30
|
|
|
if (ifd == NULL) {
|
2012-01-17 15:20:35 +05:30
|
|
|
rv = ENOMEM;
|
|
|
|
goto out;
|
2012-01-15 18:54:44 +05:30
|
|
|
}
|
|
|
|
ifd->pkgdir = pkgdir;
|
2012-01-17 15:20:35 +05:30
|
|
|
ifd->idxfiles = prop_array_internalize_from_zfile(plist);
|
2012-01-19 16:56:40 +05:30
|
|
|
ifd->idx = prop_array_copy(idx);
|
2012-01-17 16:46:54 +05:30
|
|
|
ifd->obsoletes = prop_array_create();
|
2012-01-17 15:20:35 +05:30
|
|
|
if (ifd->idxfiles == NULL) {
|
|
|
|
/* missing file, create new one */
|
|
|
|
ifd->idxfiles = prop_array_create();
|
|
|
|
ifd->new = true;
|
|
|
|
}
|
2012-01-15 18:54:44 +05:30
|
|
|
|
2012-05-31 19:59:56 +05:30
|
|
|
/* iterate over index.plist array */
|
2012-01-19 16:56:40 +05:30
|
|
|
rv = xbps_callback_array_iter(idx, genindex_files_cb, ifd);
|
2012-01-17 15:20:35 +05:30
|
|
|
if (rv != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* remove obsolete pkg entries */
|
|
|
|
if (!ifd->new) {
|
|
|
|
rv = xbps_callback_array_iter(ifd->idxfiles,
|
|
|
|
rmobsoletes_files_cb, ifd);
|
|
|
|
if (rv != 0)
|
|
|
|
goto out;
|
2012-01-19 16:56:40 +05:30
|
|
|
for (i = 0; i < prop_array_count(ifd->obsoletes); i++) {
|
2012-06-01 03:18:58 +05:30
|
|
|
prop_array_get_cstring_nocopy(ifd->obsoletes, i, &p);
|
|
|
|
pkgver = strdup(p);
|
|
|
|
for (x = 0; x < strlen(p); x++) {
|
|
|
|
if ((pkgver[x] = p[x]) == ',') {
|
|
|
|
pkgver[x] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arch = strchr(p, ',') + 1;
|
2012-01-17 16:46:54 +05:30
|
|
|
if (!xbps_remove_pkg_from_array_by_pkgver(
|
2012-05-31 21:04:17 +05:30
|
|
|
ifd->idxfiles, pkgver, arch)) {
|
2012-06-01 03:18:58 +05:30
|
|
|
free(pkgver);
|
2012-01-17 16:46:54 +05:30
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-06-01 04:01:18 +05:30
|
|
|
printf("Removed obsolete entry for `%s' "
|
|
|
|
"from files index.\n", pkgver);
|
2012-06-01 03:18:58 +05:30
|
|
|
free(pkgver);
|
2012-01-17 16:46:54 +05:30
|
|
|
}
|
2012-01-15 18:54:44 +05:30
|
|
|
}
|
2012-01-17 15:20:35 +05:30
|
|
|
if (!ifd->flush)
|
|
|
|
goto out;
|
|
|
|
|
2012-05-31 19:59:56 +05:30
|
|
|
/* externalize index-files array */
|
2012-01-17 15:20:35 +05:30
|
|
|
if (!prop_array_externalize_to_zfile(ifd->idxfiles, plist)) {
|
|
|
|
rv = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
if (rv == 0)
|
|
|
|
printf("%u packages registered in repository files index.\n",
|
|
|
|
prop_array_count(ifd->idxfiles));
|
2012-01-17 16:46:54 +05:30
|
|
|
if (ifd->obsoletes != NULL)
|
|
|
|
prop_object_release(ifd->obsoletes);
|
2012-01-17 15:20:35 +05:30
|
|
|
if (ifd->idxfiles != NULL)
|
2012-01-15 18:54:44 +05:30
|
|
|
prop_object_release(ifd->idxfiles);
|
2012-01-19 16:56:40 +05:30
|
|
|
if (ifd->idx != NULL)
|
|
|
|
prop_object_release(ifd->idx);
|
2012-01-17 15:20:35 +05:30
|
|
|
if (plist != NULL)
|
|
|
|
free(plist);
|
|
|
|
if (ifd != NULL)
|
2012-01-15 18:54:44 +05:30
|
|
|
free(ifd);
|
2012-01-19 16:56:40 +05:30
|
|
|
if (idx != NULL)
|
|
|
|
prop_object_release(idx);
|
2012-01-15 18:54:44 +05:30
|
|
|
|
2012-01-17 15:20:35 +05:30
|
|
|
return rv;
|
2012-01-15 18:54:44 +05:30
|
|
|
}
|