2012-11-02 19:34:25 +05:30
|
|
|
/*-
|
2014-01-30 17:37:34 +05:30
|
|
|
* Copyright (c) 2012-2014 Juan Romero Pardines.
|
2012-11-02 19:34:25 +05:30
|
|
|
* 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/stat.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <assert.h>
|
2013-06-10 13:58:39 +05:30
|
|
|
#include <fcntl.h>
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2013-06-20 16:01:02 +05:30
|
|
|
#include <xbps.h>
|
2012-11-02 19:34:25 +05:30
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
int
|
2013-05-02 14:16:30 +05:30
|
|
|
index_add(struct xbps_handle *xhp, int argc, char **argv, bool force)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
2013-12-24 15:13:55 +05:30
|
|
|
xbps_array_t array, pkg_files, pkg_links, pkg_cffiles;
|
2014-01-30 17:37:34 +05:30
|
|
|
xbps_dictionary_t idx, idxmeta, idxfiles, binpkgd, pkg_filesd, curpkgd;
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_t obj, fileobj;
|
2014-09-05 15:56:42 +05:30
|
|
|
struct xbps_repo *repo = NULL;
|
2012-11-02 19:34:25 +05:30
|
|
|
struct stat st;
|
2014-04-20 13:32:54 +05:30
|
|
|
char *tmprepodir = NULL, *repodir = NULL;
|
2013-10-05 15:08:04 +05:30
|
|
|
int rv = 0, ret = 0;
|
2013-06-10 13:58:39 +05:30
|
|
|
bool flush = false, found = false;
|
2012-11-30 11:41:51 +05:30
|
|
|
|
2014-10-06 20:43:06 +05:30
|
|
|
assert(argv);
|
2013-06-10 13:58:39 +05:30
|
|
|
/*
|
|
|
|
* Read the repository data or create index dictionaries otherwise.
|
|
|
|
*/
|
2014-09-05 15:56:42 +05:30
|
|
|
if ((tmprepodir = strdup(argv[0])) == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
|
2012-11-02 19:34:25 +05:30
|
|
|
repodir = dirname(tmprepodir);
|
2014-09-05 15:56:42 +05:30
|
|
|
repo = xbps_repo_open(xhp, repodir, true);
|
2014-09-15 15:45:42 +05:30
|
|
|
if (repo == NULL && errno != ENOENT) {
|
|
|
|
fprintf(stderr, "xbps-rindex: cannot open/lock repository "
|
|
|
|
"%s: %s\n", repodir, strerror(errno));
|
|
|
|
rv = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2013-12-24 15:13:55 +05:30
|
|
|
if (repo && repo->idx) {
|
2013-10-18 13:06:25 +05:30
|
|
|
xbps_repo_open_idxfiles(repo);
|
2013-10-07 13:53:14 +05:30
|
|
|
idx = xbps_dictionary_copy(repo->idx);
|
2014-01-30 17:37:34 +05:30
|
|
|
idxmeta = xbps_dictionary_copy(repo->idxmeta);
|
2013-10-07 13:53:14 +05:30
|
|
|
idxfiles = xbps_dictionary_copy(repo->idxfiles);
|
2013-12-24 15:13:55 +05:30
|
|
|
} else {
|
|
|
|
idx = xbps_dictionary_create();
|
2014-01-30 17:37:34 +05:30
|
|
|
idxmeta = NULL;
|
2013-12-24 15:13:55 +05:30
|
|
|
idxfiles = xbps_dictionary_create();
|
2013-10-07 13:53:14 +05:30
|
|
|
}
|
2012-11-02 19:34:25 +05:30
|
|
|
/*
|
|
|
|
* Process all packages specified in argv.
|
|
|
|
*/
|
2013-09-15 13:36:49 +05:30
|
|
|
for (int i = 0; i < argc; i++) {
|
2014-10-05 13:05:42 +05:30
|
|
|
const char *arch = NULL;
|
|
|
|
char *sha256 = NULL, *pkgver = NULL, *pkgname = NULL;
|
2014-10-06 20:43:06 +05:30
|
|
|
|
|
|
|
assert(argv[i]);
|
2012-11-02 19:34:25 +05:30
|
|
|
/*
|
|
|
|
* Read metadata props plist dictionary from binary package.
|
|
|
|
*/
|
2013-12-24 15:13:55 +05:30
|
|
|
binpkgd = xbps_get_pkg_plist_from_binpkg(argv[i],
|
2012-11-02 19:34:25 +05:30
|
|
|
"./props.plist");
|
2013-12-24 15:13:55 +05:30
|
|
|
if (binpkgd == NULL) {
|
2014-01-31 16:05:31 +05:30
|
|
|
fprintf(stderr, "index: failed to read %s metadata for "
|
|
|
|
"`%s', skipping!\n", XBPS_PKGPROPS, argv[i]);
|
2012-11-02 19:34:25 +05:30
|
|
|
continue;
|
|
|
|
}
|
2013-12-24 15:13:55 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(binpkgd, "architecture", &arch);
|
|
|
|
xbps_dictionary_get_cstring(binpkgd, "pkgver", &pkgver);
|
2012-11-30 11:41:51 +05:30
|
|
|
if (!xbps_pkg_arch_match(xhp, arch, NULL)) {
|
2013-12-24 15:13:55 +05:30
|
|
|
fprintf(stderr, "index: ignoring %s, unmatched arch (%s)\n", pkgver, arch);
|
|
|
|
xbps_object_release(binpkgd);
|
2014-10-05 11:36:12 +05:30
|
|
|
free(pkgver);
|
2012-11-30 11:41:51 +05:30
|
|
|
continue;
|
|
|
|
}
|
2013-03-05 08:38:42 +05:30
|
|
|
pkgname = xbps_pkg_name(pkgver);
|
|
|
|
assert(pkgname);
|
2012-11-02 19:34:25 +05:30
|
|
|
/*
|
|
|
|
* Check if this package exists already in the index, but first
|
|
|
|
* checking the version. If current package version is greater
|
|
|
|
* than current registered package, update the index; otherwise
|
|
|
|
* pass to the next one.
|
|
|
|
*/
|
2013-06-20 13:56:12 +05:30
|
|
|
curpkgd = xbps_dictionary_get(idx, pkgname);
|
2012-11-02 19:34:25 +05:30
|
|
|
if (curpkgd == NULL) {
|
2013-03-05 08:38:42 +05:30
|
|
|
if (errno && errno != ENOENT) {
|
2014-01-31 16:05:31 +05:30
|
|
|
rv = errno;
|
2013-03-05 08:38:42 +05:30
|
|
|
free(pkgver);
|
|
|
|
free(pkgname);
|
2014-01-31 16:05:31 +05:30
|
|
|
goto out;
|
2013-03-05 08:38:42 +05:30
|
|
|
}
|
2013-05-02 14:16:30 +05:30
|
|
|
} else if (!force) {
|
2014-10-05 12:53:03 +05:30
|
|
|
char *opkgver = NULL, *oarch = NULL;
|
|
|
|
|
2013-05-02 14:16:30 +05:30
|
|
|
/* Only check version if !force */
|
2013-11-29 22:00:45 +05:30
|
|
|
xbps_dictionary_get_cstring(curpkgd, "pkgver", &opkgver);
|
|
|
|
xbps_dictionary_get_cstring(curpkgd, "architecture", &oarch);
|
|
|
|
ret = xbps_cmpver(pkgver, opkgver);
|
2014-09-23 00:53:40 +05:30
|
|
|
|
|
|
|
/*
|
2014-09-23 22:44:45 +05:30
|
|
|
* If the considered package reverts the package in the index,
|
|
|
|
* consider the current package as the newer one.
|
2014-09-23 00:53:40 +05:30
|
|
|
*/
|
2014-10-05 12:53:03 +05:30
|
|
|
if (ret < 0 && xbps_pkg_reverts(binpkgd, opkgver)) {
|
2014-09-23 00:53:40 +05:30
|
|
|
ret = 1;
|
|
|
|
/*
|
2014-09-23 22:44:45 +05:30
|
|
|
* If package in the index reverts considered package, consider the
|
2014-09-23 00:53:40 +05:30
|
|
|
* package in the index as the newer one.
|
|
|
|
*/
|
2014-10-05 12:53:03 +05:30
|
|
|
} else if (ret > 0 && xbps_pkg_reverts(curpkgd, pkgver)) {
|
2014-09-23 00:53:40 +05:30
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
2012-12-15 15:43:32 +05:30
|
|
|
if (ret <= 0) {
|
|
|
|
/* Same version or index version greater */
|
2013-12-24 15:13:55 +05:30
|
|
|
fprintf(stderr, "index: skipping `%s' (%s), already registered.\n", pkgver, arch);
|
|
|
|
xbps_object_release(binpkgd);
|
2013-11-29 22:00:45 +05:30
|
|
|
free(opkgver);
|
|
|
|
free(oarch);
|
2013-03-05 08:38:42 +05:30
|
|
|
free(pkgver);
|
|
|
|
free(pkgname);
|
2012-11-02 19:34:25 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Current package version is greater than
|
|
|
|
* index version.
|
|
|
|
*/
|
2014-10-05 12:53:03 +05:30
|
|
|
printf("index: removed obsolete entry `%s' (%s).\n", opkgver, oarch);
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_remove(idx, pkgname);
|
2013-11-29 22:00:45 +05:30
|
|
|
xbps_dictionary_remove(idxfiles, opkgver);
|
|
|
|
free(opkgver);
|
|
|
|
free(oarch);
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
2013-03-05 08:38:42 +05:30
|
|
|
/*
|
2013-12-24 15:13:55 +05:30
|
|
|
* Add additional objects for repository ops:
|
|
|
|
* - filename-size
|
|
|
|
* - filename-sha256
|
2013-03-05 08:38:42 +05:30
|
|
|
*/
|
2013-12-24 15:13:55 +05:30
|
|
|
if ((sha256 = xbps_file_hash(argv[i])) == NULL) {
|
|
|
|
free(pkgver);
|
|
|
|
free(pkgname);
|
2014-01-31 16:05:31 +05:30
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
2013-12-24 15:13:55 +05:30
|
|
|
}
|
2013-12-30 14:57:54 +05:30
|
|
|
if (!xbps_dictionary_set_cstring(binpkgd, "filename-sha256", sha256)) {
|
2014-10-05 11:01:07 +05:30
|
|
|
free(sha256);
|
2013-12-24 15:13:55 +05:30
|
|
|
free(pkgver);
|
|
|
|
free(pkgname);
|
2014-01-31 16:05:31 +05:30
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
2013-12-24 15:13:55 +05:30
|
|
|
}
|
2014-10-05 11:01:07 +05:30
|
|
|
free(sha256);
|
2013-12-24 15:13:55 +05:30
|
|
|
if (stat(argv[i], &st) == -1) {
|
|
|
|
free(pkgver);
|
|
|
|
free(pkgname);
|
2014-01-31 16:05:31 +05:30
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
2013-12-24 15:13:55 +05:30
|
|
|
}
|
2013-12-30 14:57:54 +05:30
|
|
|
if (!xbps_dictionary_set_uint64(binpkgd, "filename-size", (uint64_t)st.st_size)) {
|
2013-12-24 15:13:55 +05:30
|
|
|
free(pkgver);
|
|
|
|
free(pkgname);
|
2014-01-31 16:05:31 +05:30
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
2013-12-24 15:13:55 +05:30
|
|
|
}
|
2013-12-30 14:57:54 +05:30
|
|
|
/* Remove unneeded objects */
|
|
|
|
xbps_dictionary_remove(binpkgd, "pkgname");
|
|
|
|
xbps_dictionary_remove(binpkgd, "version");
|
|
|
|
|
2012-11-02 19:34:25 +05:30
|
|
|
/*
|
|
|
|
* Add new pkg dictionary into the index.
|
|
|
|
*/
|
2013-12-30 14:57:54 +05:30
|
|
|
if (!xbps_dictionary_set(idx, pkgname, binpkgd)) {
|
2013-03-05 08:38:42 +05:30
|
|
|
free(pkgname);
|
2014-10-05 13:05:42 +05:30
|
|
|
free(pkgver);
|
2014-01-31 16:05:31 +05:30
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
2013-03-05 08:38:42 +05:30
|
|
|
}
|
2012-11-02 19:34:25 +05:30
|
|
|
flush = true;
|
2013-03-05 08:38:42 +05:30
|
|
|
printf("index: added `%s' (%s).\n", pkgver, arch);
|
2013-12-24 15:13:55 +05:30
|
|
|
xbps_object_release(binpkgd);
|
|
|
|
free(pkgname);
|
2012-11-30 11:41:51 +05:30
|
|
|
/*
|
|
|
|
* Add new pkg dictionary into the index-files.
|
|
|
|
*/
|
|
|
|
found = false;
|
2013-12-24 15:13:55 +05:30
|
|
|
pkg_filesd = xbps_get_pkg_plist_from_binpkg(argv[i], "./files.plist");
|
|
|
|
if (pkg_filesd == NULL) {
|
2013-03-05 08:38:42 +05:30
|
|
|
free(pkgver);
|
2014-01-31 16:05:31 +05:30
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
2013-03-05 08:38:42 +05:30
|
|
|
}
|
2012-11-30 11:41:51 +05:30
|
|
|
|
2013-12-24 15:13:55 +05:30
|
|
|
pkg_cffiles = xbps_dictionary_get(pkg_filesd, "conf_files");
|
2013-06-20 13:56:12 +05:30
|
|
|
if (xbps_array_count(pkg_cffiles))
|
2012-11-30 11:41:51 +05:30
|
|
|
found = true;
|
|
|
|
else
|
|
|
|
pkg_cffiles = NULL;
|
|
|
|
|
2013-12-24 15:13:55 +05:30
|
|
|
pkg_files = xbps_dictionary_get(pkg_filesd, "files");
|
2013-06-20 13:56:12 +05:30
|
|
|
if (xbps_array_count(pkg_files))
|
2012-11-30 11:41:51 +05:30
|
|
|
found = true;
|
|
|
|
else
|
|
|
|
pkg_files = NULL;
|
|
|
|
|
2013-12-24 15:13:55 +05:30
|
|
|
pkg_links = xbps_dictionary_get(pkg_filesd, "links");
|
2013-06-20 13:56:12 +05:30
|
|
|
if (xbps_array_count(pkg_links))
|
2012-11-30 11:41:51 +05:30
|
|
|
found = true;
|
|
|
|
else
|
|
|
|
pkg_links = NULL;
|
|
|
|
|
|
|
|
/* If pkg does not contain any file, ignore it */
|
|
|
|
if (!found) {
|
2013-12-24 15:13:55 +05:30
|
|
|
xbps_object_release(pkg_filesd);
|
2013-03-05 08:38:42 +05:30
|
|
|
free(pkgver);
|
2012-11-30 11:41:51 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* create pkg files array */
|
2013-12-24 15:13:55 +05:30
|
|
|
array = xbps_array_create();
|
|
|
|
assert(array);
|
2012-11-30 11:41:51 +05:30
|
|
|
|
|
|
|
/* add conf_files in pkg files array */
|
|
|
|
if (pkg_cffiles != NULL) {
|
2013-09-15 13:36:49 +05:30
|
|
|
for (unsigned int x = 0; x < xbps_array_count(pkg_cffiles); x++) {
|
2013-06-20 13:56:12 +05:30
|
|
|
obj = xbps_array_get(pkg_cffiles, x);
|
|
|
|
fileobj = xbps_dictionary_get(obj, "file");
|
2013-12-24 15:13:55 +05:30
|
|
|
xbps_array_add(array, fileobj);
|
2012-11-30 11:41:51 +05:30
|
|
|
}
|
|
|
|
}
|
2013-12-24 15:13:55 +05:30
|
|
|
/* add files array in pkg files array */
|
2012-11-30 11:41:51 +05:30
|
|
|
if (pkg_files != NULL) {
|
2013-09-15 13:36:49 +05:30
|
|
|
for (unsigned int x = 0; x < xbps_array_count(pkg_files); x++) {
|
2013-06-20 13:56:12 +05:30
|
|
|
obj = xbps_array_get(pkg_files, x);
|
|
|
|
fileobj = xbps_dictionary_get(obj, "file");
|
2013-12-24 15:13:55 +05:30
|
|
|
xbps_array_add(array, fileobj);
|
2012-11-30 11:41:51 +05:30
|
|
|
}
|
|
|
|
}
|
2013-12-24 15:13:55 +05:30
|
|
|
/* add links array in pkg files array */
|
2012-11-30 11:41:51 +05:30
|
|
|
if (pkg_links != NULL) {
|
2013-09-15 13:36:49 +05:30
|
|
|
for (unsigned int x = 0; x < xbps_array_count(pkg_links); x++) {
|
2013-06-20 13:56:12 +05:30
|
|
|
obj = xbps_array_get(pkg_links, x);
|
|
|
|
fileobj = xbps_dictionary_get(obj, "file");
|
2013-12-24 15:13:55 +05:30
|
|
|
xbps_array_add(array, fileobj);
|
2012-11-30 11:41:51 +05:30
|
|
|
}
|
|
|
|
}
|
2013-06-10 13:58:39 +05:30
|
|
|
/* add pkg files array into index-files */
|
2013-12-24 15:13:55 +05:30
|
|
|
xbps_dictionary_set(idxfiles, pkgver, array);
|
|
|
|
xbps_object_release(array);
|
|
|
|
xbps_object_release(pkg_filesd);
|
2013-03-05 08:38:42 +05:30
|
|
|
free(pkgver);
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
2013-06-10 13:58:39 +05:30
|
|
|
/*
|
2013-10-07 13:53:14 +05:30
|
|
|
* Generate repository data files.
|
2013-06-10 13:58:39 +05:30
|
|
|
*/
|
|
|
|
if (flush) {
|
2014-01-30 17:37:34 +05:30
|
|
|
if (!repodata_flush(xhp, repodir, idx, idxfiles, idxmeta)) {
|
2014-01-31 16:05:31 +05:30
|
|
|
fprintf(stderr, "%s: failed to write repodata: %s\n",
|
|
|
|
_XBPS_RINDEX, strerror(errno));
|
2014-04-20 13:32:54 +05:30
|
|
|
goto out;
|
2013-10-07 13:53:14 +05:30
|
|
|
}
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
2013-12-24 15:13:55 +05:30
|
|
|
printf("index: %u packages registered.\n", xbps_dictionary_count(idx));
|
|
|
|
printf("index-files: %u packages registered.\n", xbps_dictionary_count(idxfiles));
|
2013-06-19 12:50:44 +05:30
|
|
|
|
2014-01-31 16:05:31 +05:30
|
|
|
out:
|
2014-09-05 15:56:42 +05:30
|
|
|
if (repo)
|
|
|
|
xbps_repo_close(repo, true);
|
|
|
|
if (tmprepodir)
|
2014-04-20 13:32:54 +05:30
|
|
|
free(tmprepodir);
|
2014-09-05 15:56:42 +05:30
|
|
|
|
2013-10-05 15:08:04 +05:30
|
|
|
return rv;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|