2012-11-02 19:34:25 +05:30
|
|
|
/*-
|
2013-03-05 08:38:42 +05:30
|
|
|
* Copyright (c) 2012-2013 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"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Adds a binary package into the index and removes old binary package
|
|
|
|
* and entry when it's necessary.
|
|
|
|
*/
|
|
|
|
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-06-20 13:56:12 +05:30
|
|
|
xbps_array_t filespkgar, pkg_files, pkg_links, pkg_cffiles;
|
|
|
|
xbps_dictionary_t idx, idxfiles, newpkgd, newpkgfilesd, curpkgd;
|
|
|
|
xbps_object_t obj, fileobj;
|
2013-06-10 13:58:39 +05:30
|
|
|
struct xbps_repo *repo;
|
2012-11-02 19:34:25 +05:30
|
|
|
struct stat st;
|
2013-03-05 08:38:42 +05:30
|
|
|
const char *oldpkgver, *arch, *oldarch;
|
|
|
|
char *pkgver, *pkgname, *sha256, *repodir, *buf;
|
2013-06-10 13:58:39 +05:30
|
|
|
char *tmprepodir;
|
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
|
|
|
|
|
|
|
idx = idxfiles = newpkgd = newpkgfilesd = curpkgd = NULL;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2012-12-14 16:28:30 +05:30
|
|
|
if ((tmprepodir = strdup(argv[0])) == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
|
2013-06-10 13:58:39 +05:30
|
|
|
/*
|
|
|
|
* Read the repository data or create index dictionaries otherwise.
|
|
|
|
*/
|
2012-11-02 19:34:25 +05:30
|
|
|
repodir = dirname(tmprepodir);
|
|
|
|
|
2013-06-10 13:58:39 +05:30
|
|
|
repo = xbps_repo_open(xhp, repodir);
|
|
|
|
if (repo != NULL) {
|
2013-10-05 15:08:04 +05:30
|
|
|
idx = xbps_repo_get_plist(repo, XBPS_REPOIDX);
|
|
|
|
idxfiles = xbps_repo_get_plist(repo, XBPS_REPOIDX_FILES);
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
2013-06-10 13:58:39 +05:30
|
|
|
if (idx == NULL)
|
2013-06-20 13:56:12 +05:30
|
|
|
idx = xbps_dictionary_create();
|
2013-06-10 13:58:39 +05:30
|
|
|
if (idxfiles == NULL)
|
2013-06-20 13:56:12 +05:30
|
|
|
idxfiles = xbps_dictionary_create();
|
2013-06-10 13:58:39 +05:30
|
|
|
if (repo != NULL)
|
|
|
|
xbps_repo_close(repo);
|
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++) {
|
2012-11-02 19:34:25 +05:30
|
|
|
/*
|
|
|
|
* Read metadata props plist dictionary from binary package.
|
|
|
|
*/
|
2012-11-30 11:41:51 +05:30
|
|
|
newpkgd = xbps_get_pkg_plist_from_binpkg(argv[i],
|
2012-11-02 19:34:25 +05:30
|
|
|
"./props.plist");
|
|
|
|
if (newpkgd == NULL) {
|
2012-11-30 11:41:51 +05:30
|
|
|
fprintf(stderr, "failed to read %s metadata for `%s',"
|
2012-11-02 19:34:25 +05:30
|
|
|
" skipping!\n", XBPS_PKGPROPS, argv[i]);
|
|
|
|
continue;
|
|
|
|
}
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(newpkgd, "architecture",
|
2012-11-30 11:41:51 +05:30
|
|
|
&arch);
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring(newpkgd, "pkgver", &pkgver);
|
2012-11-30 11:41:51 +05:30
|
|
|
if (!xbps_pkg_arch_match(xhp, arch, NULL)) {
|
|
|
|
fprintf(stderr, "index: ignoring %s, unmatched "
|
|
|
|
"arch (%s)\n", pkgver, arch);
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_release(newpkgd);
|
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) {
|
|
|
|
free(pkgver);
|
|
|
|
free(pkgname);
|
2012-12-14 16:28:30 +05:30
|
|
|
return errno;
|
2013-03-05 08:38:42 +05:30
|
|
|
}
|
2013-05-02 14:16:30 +05:30
|
|
|
} else if (!force) {
|
|
|
|
/* Only check version if !force */
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(curpkgd,
|
2012-11-02 19:34:25 +05:30
|
|
|
"pkgver", &oldpkgver);
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(curpkgd,
|
2012-11-02 19:34:25 +05:30
|
|
|
"architecture", &oldarch);
|
2013-03-05 08:38:42 +05:30
|
|
|
ret = xbps_cmpver(pkgver, oldpkgver);
|
2012-12-15 15:43:32 +05:30
|
|
|
if (ret <= 0) {
|
|
|
|
/* Same version or index version greater */
|
2013-03-05 08:38:42 +05:30
|
|
|
fprintf(stderr, "index: skipping `%s' "
|
2012-11-02 19:34:25 +05:30
|
|
|
"(%s), already registered.\n",
|
2013-03-05 08:38:42 +05:30
|
|
|
pkgver, arch);
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_release(newpkgd);
|
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.
|
|
|
|
*/
|
|
|
|
buf = xbps_xasprintf("`%s' (%s)", oldpkgver, oldarch);
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_remove(idx, pkgname);
|
2013-09-16 14:11:42 +05:30
|
|
|
xbps_dictionary_remove(idxfiles, pkgname);
|
2012-12-15 15:43:32 +05:30
|
|
|
printf("index: removed obsolete entry %s.\n", buf);
|
2012-11-02 19:34:25 +05:30
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* We have the dictionary now, add the required
|
|
|
|
* objects for the index.
|
|
|
|
*/
|
2013-03-05 08:38:42 +05:30
|
|
|
if ((sha256 = xbps_file_hash(argv[i])) == NULL) {
|
|
|
|
free(pkgver);
|
|
|
|
free(pkgname);
|
2012-12-14 16:28:30 +05:30
|
|
|
return errno;
|
2013-03-05 08:38:42 +05:30
|
|
|
}
|
2013-06-20 13:56:12 +05:30
|
|
|
if (!xbps_dictionary_set_cstring(newpkgd, "filename-sha256",
|
2013-03-05 08:38:42 +05:30
|
|
|
sha256)) {
|
|
|
|
free(pkgver);
|
|
|
|
free(pkgname);
|
2012-12-14 16:28:30 +05:30
|
|
|
return errno;
|
2013-03-05 08:38:42 +05:30
|
|
|
}
|
2012-12-14 16:28:30 +05:30
|
|
|
|
2012-11-02 19:34:25 +05:30
|
|
|
free(sha256);
|
2013-03-05 08:38:42 +05:30
|
|
|
if (stat(argv[i], &st) == -1) {
|
|
|
|
free(pkgver);
|
|
|
|
free(pkgname);
|
2012-12-14 16:28:30 +05:30
|
|
|
return errno;
|
2013-03-05 08:38:42 +05:30
|
|
|
}
|
2012-12-14 16:28:30 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
if (!xbps_dictionary_set_uint64(newpkgd, "filename-size",
|
2012-11-02 19:34:25 +05:30
|
|
|
(uint64_t)st.st_size)) {
|
2013-03-05 08:38:42 +05:30
|
|
|
free(pkgver);
|
|
|
|
free(pkgname);
|
2012-12-14 16:28:30 +05:30
|
|
|
return errno;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
2013-03-05 08:38:42 +05:30
|
|
|
/*
|
|
|
|
* Remove obsolete package objects.
|
|
|
|
*/
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_remove(newpkgd, "pkgname");
|
|
|
|
xbps_dictionary_remove(newpkgd, "version");
|
2012-11-02 19:34:25 +05:30
|
|
|
/*
|
|
|
|
* Add new pkg dictionary into the index.
|
|
|
|
*/
|
2013-06-20 13:56:12 +05:30
|
|
|
if (!xbps_dictionary_set(idx, pkgname, newpkgd)) {
|
2013-03-05 08:38:42 +05:30
|
|
|
free(pkgname);
|
2012-12-14 16:28:30 +05:30
|
|
|
return EINVAL;
|
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);
|
2012-11-30 11:41:51 +05:30
|
|
|
/*
|
|
|
|
* Add new pkg dictionary into the index-files.
|
|
|
|
*/
|
|
|
|
found = false;
|
|
|
|
newpkgfilesd = xbps_get_pkg_plist_from_binpkg(argv[i],
|
|
|
|
"./files.plist");
|
2013-03-05 08:38:42 +05:30
|
|
|
if (newpkgfilesd == NULL) {
|
|
|
|
free(pkgver);
|
|
|
|
free(pkgname);
|
2012-12-14 16:28:30 +05:30
|
|
|
return EINVAL;
|
2013-03-05 08:38:42 +05:30
|
|
|
}
|
2012-11-30 11:41:51 +05:30
|
|
|
|
|
|
|
/* Find out if binary pkg stored in index contain any file */
|
2013-06-20 13:56:12 +05:30
|
|
|
pkg_cffiles = xbps_dictionary_get(newpkgfilesd, "conf_files");
|
|
|
|
if (xbps_array_count(pkg_cffiles))
|
2012-11-30 11:41:51 +05:30
|
|
|
found = true;
|
|
|
|
else
|
|
|
|
pkg_cffiles = NULL;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
pkg_files = xbps_dictionary_get(newpkgfilesd, "files");
|
|
|
|
if (xbps_array_count(pkg_files))
|
2012-11-30 11:41:51 +05:30
|
|
|
found = true;
|
|
|
|
else
|
|
|
|
pkg_files = NULL;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
pkg_links = xbps_dictionary_get(newpkgfilesd, "links");
|
|
|
|
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-06-20 13:56:12 +05:30
|
|
|
xbps_object_release(newpkgfilesd);
|
|
|
|
xbps_object_release(newpkgd);
|
2013-03-05 08:38:42 +05:30
|
|
|
free(pkgver);
|
|
|
|
free(pkgname);
|
2012-11-30 11:41:51 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* create pkg files array */
|
2013-06-20 13:56:12 +05:30
|
|
|
filespkgar = xbps_array_create();
|
2012-11-30 11:41:51 +05:30
|
|
|
assert(filespkgar);
|
|
|
|
|
|
|
|
/* 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");
|
|
|
|
xbps_array_add(filespkgar, fileobj);
|
2012-11-30 11:41:51 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
/* add files array in pkg array */
|
|
|
|
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");
|
|
|
|
xbps_array_add(filespkgar, fileobj);
|
2012-11-30 11:41:51 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
/* add links array in pkgd */
|
|
|
|
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");
|
|
|
|
xbps_array_add(filespkgar, fileobj);
|
2012-11-30 11:41:51 +05:30
|
|
|
}
|
|
|
|
}
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_release(newpkgfilesd);
|
2012-11-30 11:41:51 +05:30
|
|
|
|
2013-06-10 13:58:39 +05:30
|
|
|
/* add pkg files array into index-files */
|
2013-09-16 14:11:42 +05:30
|
|
|
xbps_dictionary_set(idxfiles, pkgname, filespkgar);
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_release(filespkgar);
|
2012-11-30 11:41:51 +05:30
|
|
|
|
|
|
|
printf("index-files: added `%s' (%s)\n", pkgver, arch);
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_release(newpkgd);
|
2013-03-05 08:38:42 +05:30
|
|
|
free(pkgver);
|
|
|
|
free(pkgname);
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
2013-06-10 13:58:39 +05:30
|
|
|
/*
|
|
|
|
* Generate repository data file.
|
|
|
|
*/
|
|
|
|
if (flush) {
|
2013-10-05 15:08:04 +05:30
|
|
|
struct repodata *rd;
|
|
|
|
char *xml;
|
|
|
|
|
|
|
|
rd = repodata_init(xhp, repodir);
|
|
|
|
xml = xbps_dictionary_externalize(idx);
|
|
|
|
assert(idx);
|
|
|
|
rv = repodata_add_buf(rd, xml, XBPS_REPOIDX);
|
|
|
|
free(xml);
|
|
|
|
xml = xbps_dictionary_externalize(idxfiles);
|
|
|
|
assert(idx);
|
|
|
|
rv = repodata_add_buf(rd, xml, XBPS_REPOIDX_FILES);
|
|
|
|
free(xml);
|
|
|
|
repodata_flush(rd);
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
2012-11-30 11:41:51 +05:30
|
|
|
printf("index: %u packages registered.\n",
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_count(idx));
|
2012-11-30 11:41:51 +05:30
|
|
|
printf("index-files: %u packages registered.\n",
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_count(idxfiles));
|
2013-06-19 12:50:44 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_release(idx);
|
|
|
|
xbps_object_release(idxfiles);
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2013-10-05 15:08:04 +05:30
|
|
|
return rv;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|