2012-11-02 19:34:25 +05:30
|
|
|
/*-
|
2015-01-11 13:41:38 +05:30
|
|
|
* Copyright (c) 2012-2015 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"
|
|
|
|
|
2015-09-02 18:26:40 +05:30
|
|
|
static int
|
|
|
|
set_build_date(const xbps_dictionary_t pkgd, time_t timestamp)
|
|
|
|
{
|
|
|
|
char outstr[64];
|
|
|
|
struct tm tmp;
|
|
|
|
|
|
|
|
if (!localtime_r(×tamp, &tmp))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (strftime(outstr, sizeof(outstr)-1, "%F %R %Z", &tmp) == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!xbps_dictionary_set_cstring(pkgd, "build-date", outstr))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-02 19:34:25 +05:30
|
|
|
int
|
2014-10-07 13:41:14 +05:30
|
|
|
index_add(struct xbps_handle *xhp, int args, int argmax, char **argv, bool force)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
2014-11-13 21:39:43 +05:30
|
|
|
xbps_dictionary_t idx, idxmeta, binpkgd, curpkgd;
|
2014-09-05 15:56:42 +05:30
|
|
|
struct xbps_repo *repo = NULL;
|
2012-11-02 19:34:25 +05:30
|
|
|
struct stat st;
|
2015-03-25 16:30:59 +05:30
|
|
|
char *tmprepodir = NULL, *repodir = NULL, *rlockfname = NULL;
|
|
|
|
int rv = 0, ret = 0, rlockfd = -1;
|
2014-11-13 21:39:43 +05:30
|
|
|
bool flush = 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-11-06 16:03:04 +05:30
|
|
|
if ((tmprepodir = strdup(argv[args])) == NULL)
|
2014-09-05 15:56:42 +05:30
|
|
|
return ENOMEM;
|
|
|
|
|
2012-11-02 19:34:25 +05:30
|
|
|
repodir = dirname(tmprepodir);
|
2015-03-25 16:30:59 +05:30
|
|
|
if (!xbps_repo_lock(xhp, repodir, &rlockfd, &rlockfname)) {
|
|
|
|
fprintf(stderr, "xbps-rindex: cannot lock repository "
|
|
|
|
"%s: %s\n", repodir, strerror(errno));
|
|
|
|
rv = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
repo = xbps_repo_open(xhp, repodir);
|
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;
|
|
|
|
}
|
2014-11-13 21:39:43 +05:30
|
|
|
if (repo) {
|
2015-05-22 15:50:49 +05:30
|
|
|
idx = xbps_dictionary_copy_mutable(repo->idx);
|
|
|
|
idxmeta = xbps_dictionary_copy_mutable(repo->idxmeta);
|
2013-12-24 15:13:55 +05:30
|
|
|
} else {
|
|
|
|
idx = xbps_dictionary_create();
|
2014-01-30 17:37:34 +05:30
|
|
|
idxmeta = NULL;
|
2013-10-07 13:53:14 +05:30
|
|
|
}
|
2012-11-02 19:34:25 +05:30
|
|
|
/*
|
|
|
|
* Process all packages specified in argv.
|
|
|
|
*/
|
2014-10-07 13:41:14 +05:30
|
|
|
for (int i = args; i < argmax; i++) {
|
|
|
|
const char *arch = NULL, *pkg = argv[i];
|
2014-10-05 13:05:42 +05:30
|
|
|
char *sha256 = NULL, *pkgver = NULL, *pkgname = NULL;
|
2014-10-06 20:43:06 +05:30
|
|
|
|
2014-10-07 13:41:14 +05:30
|
|
|
assert(pkg);
|
2012-11-02 19:34:25 +05:30
|
|
|
/*
|
|
|
|
* Read metadata props plist dictionary from binary package.
|
|
|
|
*/
|
2014-11-21 14:21:57 +05:30
|
|
|
binpkgd = xbps_archive_fetch_plist(pkg, "/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 "
|
2014-10-07 13:41:14 +05:30
|
|
|
"`%s', skipping!\n", XBPS_PKGPROPS, pkg);
|
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
|
|
|
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
|
|
|
*/
|
2014-10-07 13:41:14 +05:30
|
|
|
if ((sha256 = xbps_file_hash(pkg)) == NULL) {
|
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
|
|
|
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);
|
2014-10-07 13:41:14 +05:30
|
|
|
if (stat(pkg, &st) == -1) {
|
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
|
|
|
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
|
|
|
}
|
2015-09-02 18:26:40 +05:30
|
|
|
if (set_build_date(binpkgd, st.st_mtime) < 0) {
|
|
|
|
free(pkgver);
|
|
|
|
free(pkgname);
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2013-12-30 14:57:54 +05:30
|
|
|
/* Remove unneeded objects */
|
|
|
|
xbps_dictionary_remove(binpkgd, "pkgname");
|
|
|
|
xbps_dictionary_remove(binpkgd, "version");
|
2014-12-09 17:11:33 +05:30
|
|
|
xbps_dictionary_remove(binpkgd, "packaged-with");
|
2013-12-30 14:57:54 +05:30
|
|
|
|
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);
|
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-11-13 21:39:43 +05:30
|
|
|
if (!repodata_flush(xhp, repodir, idx, 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));
|
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)
|
2015-01-11 13:41:38 +05:30
|
|
|
xbps_repo_close(repo);
|
2015-03-25 16:30:59 +05:30
|
|
|
|
|
|
|
xbps_repo_unlock(rlockfd, rlockfname);
|
|
|
|
|
2014-09-05 15:56:42 +05:30
|
|
|
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
|
|
|
}
|