2009-08-17 22:37:20 +05:30
|
|
|
/*-
|
2012-01-05 20:47:37 +05:30
|
|
|
* Copyright (c) 2009-2012 Juan Romero Pardines.
|
2009-08-17 22:37:20 +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 <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2010-11-19 18:10:13 +05:30
|
|
|
#include <unistd.h>
|
2011-06-01 11:01:38 +05:30
|
|
|
#include <sys/param.h>
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
#include <xbps_api.h>
|
|
|
|
#include "defs.h"
|
|
|
|
|
2011-12-22 18:55:27 +05:30
|
|
|
struct checkpkg {
|
2012-05-07 16:03:50 +05:30
|
|
|
size_t totalpkgs;
|
2011-12-22 18:55:27 +05:30
|
|
|
size_t npkgs;
|
|
|
|
size_t nbrokenpkgs;
|
2012-01-20 15:40:52 +05:30
|
|
|
bool flush;
|
2011-12-22 18:55:27 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2012-06-14 11:52:11 +05:30
|
|
|
cb_pkg_integrity(struct xbps_handle *xhp,
|
|
|
|
prop_object_t obj,
|
|
|
|
void *arg,
|
|
|
|
bool *done)
|
2011-12-22 18:55:27 +05:30
|
|
|
{
|
|
|
|
struct checkpkg *cpkg = arg;
|
|
|
|
const char *pkgname, *version;
|
2012-01-20 15:40:52 +05:30
|
|
|
bool flush = false;
|
2011-12-22 18:55:27 +05:30
|
|
|
|
|
|
|
(void)done;
|
|
|
|
|
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
|
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "version", &version);
|
2012-05-07 16:03:50 +05:30
|
|
|
printf("[%zu/%zu] checking %s-%s ...\n",
|
|
|
|
cpkg->npkgs, cpkg->totalpkgs, pkgname, version);
|
2012-06-14 11:52:11 +05:30
|
|
|
if (check_pkg_integrity(xhp, obj, pkgname, false, &flush) != 0)
|
2011-12-22 18:55:27 +05:30
|
|
|
cpkg->nbrokenpkgs++;
|
2012-01-25 05:47:18 +05:30
|
|
|
else
|
|
|
|
printf("\033[1A\033[K");
|
2011-12-22 18:55:27 +05:30
|
|
|
|
2012-05-07 16:03:50 +05:30
|
|
|
|
2012-04-05 14:28:52 +05:30
|
|
|
if (flush && !cpkg->flush)
|
|
|
|
cpkg->flush = flush;
|
2011-12-22 18:55:27 +05:30
|
|
|
cpkg->npkgs++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-06 04:12:37 +05:30
|
|
|
int
|
2012-06-14 11:52:11 +05:30
|
|
|
check_pkg_integrity_all(struct xbps_handle *xhp)
|
2009-10-06 04:12:37 +05:30
|
|
|
{
|
2012-01-20 15:40:52 +05:30
|
|
|
struct checkpkg cpkg;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
memset(&cpkg, 0, sizeof(cpkg));
|
2012-05-07 16:03:50 +05:30
|
|
|
/* force an update to get total pkg count */
|
2012-06-14 11:52:11 +05:30
|
|
|
(void)xbps_pkgdb_update(xhp, false);
|
2012-05-07 16:03:50 +05:30
|
|
|
cpkg.totalpkgs = prop_array_count(xhp->pkgdb);
|
|
|
|
|
2012-06-14 11:52:11 +05:30
|
|
|
(void)xbps_pkgdb_foreach_cb(xhp, cb_pkg_integrity, &cpkg);
|
2012-01-20 15:40:52 +05:30
|
|
|
if (cpkg.flush) {
|
2012-06-14 11:52:11 +05:30
|
|
|
if ((rv = xbps_pkgdb_update(xhp, true)) != 0) {
|
2012-01-20 15:40:52 +05:30
|
|
|
xbps_error_printf("failed to write pkgdb: %s\n",
|
|
|
|
strerror(rv));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("%zu package%s processed: %zu broken.\n", cpkg.npkgs,
|
|
|
|
cpkg.npkgs == 1 ? "" : "s", cpkg.nbrokenpkgs);
|
2011-07-28 19:54:16 +05:30
|
|
|
return 0;
|
2009-10-06 04:12:37 +05:30
|
|
|
}
|
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
int
|
2012-06-14 11:52:11 +05:30
|
|
|
check_pkg_integrity(struct xbps_handle *xhp,
|
|
|
|
prop_dictionary_t pkgd,
|
2012-01-20 15:40:52 +05:30
|
|
|
const char *pkgname,
|
|
|
|
bool flush,
|
|
|
|
bool *setflush)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
2011-12-22 18:55:27 +05:30
|
|
|
prop_dictionary_t opkgd, propsd, filesd;
|
2009-08-17 22:37:20 +05:30
|
|
|
int rv = 0;
|
2012-01-20 15:40:52 +05:30
|
|
|
bool pkgdb_update = false, broken = false;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2011-12-22 18:55:27 +05:30
|
|
|
propsd = filesd = opkgd = NULL;
|
2011-10-28 13:05:34 +05:30
|
|
|
|
2011-10-27 20:38:48 +05:30
|
|
|
/* find real pkg by name */
|
2009-08-17 22:37:20 +05:30
|
|
|
if (pkgd == NULL) {
|
2012-06-14 11:52:11 +05:30
|
|
|
opkgd = xbps_find_pkg_dict_installed(xhp, pkgname, false);
|
2011-12-22 18:55:27 +05:30
|
|
|
if (opkgd == NULL) {
|
|
|
|
/* find virtual pkg by name */
|
2012-06-14 11:52:11 +05:30
|
|
|
opkgd = xbps_find_virtualpkg_dict_installed(xhp,
|
|
|
|
pkgname, false);
|
2011-12-22 18:55:27 +05:30
|
|
|
}
|
|
|
|
if (opkgd == NULL) {
|
|
|
|
printf("Package %s is not installed.\n", pkgname);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Check for props.plist metadata file.
|
|
|
|
*/
|
2012-06-14 11:52:11 +05:30
|
|
|
propsd = xbps_dictionary_from_metadata_plist(xhp, pkgname, XBPS_PKGPROPS);
|
2012-01-20 15:40:52 +05:30
|
|
|
if (propsd == NULL) {
|
2011-07-28 19:54:16 +05:30
|
|
|
xbps_error_printf("%s: unexistent %s or invalid metadata "
|
|
|
|
"file.\n", pkgname, XBPS_PKGPROPS);
|
|
|
|
broken = true;
|
2009-08-17 22:37:20 +05:30
|
|
|
goto out;
|
|
|
|
} else if (prop_dictionary_count(propsd) == 0) {
|
2011-07-28 19:54:16 +05:30
|
|
|
xbps_error_printf("%s: incomplete %s metadata file.\n",
|
|
|
|
pkgname, XBPS_PKGPROPS);
|
|
|
|
broken = true;
|
2010-01-25 20:45:33 +05:30
|
|
|
goto out;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Check for files.plist metadata file.
|
|
|
|
*/
|
2012-06-14 11:52:11 +05:30
|
|
|
filesd = xbps_dictionary_from_metadata_plist(xhp, pkgname, XBPS_PKGFILES);
|
2012-01-20 15:40:52 +05:30
|
|
|
if (filesd == NULL) {
|
2011-07-28 19:54:16 +05:30
|
|
|
xbps_error_printf("%s: unexistent %s or invalid metadata "
|
|
|
|
"file.\n", pkgname, XBPS_PKGFILES);
|
|
|
|
broken = true;
|
2010-01-25 20:45:33 +05:30
|
|
|
goto out;
|
2009-08-17 22:37:20 +05:30
|
|
|
} else if (prop_dictionary_count(filesd) == 0) {
|
2011-07-28 19:54:16 +05:30
|
|
|
xbps_error_printf("%s: incomplete %s metadata file.\n",
|
|
|
|
pkgname, XBPS_PKGFILES);
|
|
|
|
broken = true;
|
2010-01-25 20:45:33 +05:30
|
|
|
goto out;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
|
2012-06-14 11:52:11 +05:30
|
|
|
#define RUN_PKG_CHECK(x, name, arg, arg2) \
|
2011-12-29 21:13:52 +05:30
|
|
|
do { \
|
2012-06-14 11:52:11 +05:30
|
|
|
rv = check_pkg_##name(x, pkgname, arg, arg2); \
|
2011-12-29 21:13:52 +05:30
|
|
|
if (rv) \
|
|
|
|
broken = true; \
|
|
|
|
else if (rv == -1) { \
|
|
|
|
xbps_error_printf("%s: the %s test " \
|
|
|
|
"returned error!\n", pkgname, #name); \
|
|
|
|
goto out; \
|
|
|
|
} \
|
2011-10-28 04:27:16 +05:30
|
|
|
} while (0)
|
|
|
|
|
2011-10-27 20:38:48 +05:30
|
|
|
/* Execute pkg checks */
|
2012-06-14 11:52:11 +05:30
|
|
|
RUN_PKG_CHECK(xhp, files, filesd, &pkgdb_update);
|
|
|
|
RUN_PKG_CHECK(xhp, symlinks, filesd, &pkgdb_update);
|
|
|
|
RUN_PKG_CHECK(xhp, rundeps, propsd, &pkgdb_update);
|
|
|
|
RUN_PKG_CHECK(xhp, requiredby, pkgd ? pkgd : opkgd, &pkgdb_update);
|
|
|
|
RUN_PKG_CHECK(xhp, autoinstall, pkgd ? pkgd : opkgd, &pkgdb_update);
|
2012-01-20 15:40:52 +05:30
|
|
|
|
|
|
|
if (flush && pkgdb_update) {
|
2012-06-14 11:52:11 +05:30
|
|
|
if (!xbps_pkgdb_replace_pkgd(xhp, opkgd, pkgname, false, true)) {
|
2012-01-20 15:40:52 +05:30
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2012-04-05 14:28:52 +05:30
|
|
|
if (setflush && pkgdb_update)
|
2012-01-20 15:40:52 +05:30
|
|
|
*setflush = true;
|
2011-10-28 04:27:16 +05:30
|
|
|
|
|
|
|
#undef RUN_PKG_CHECK
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
out:
|
2011-10-27 20:38:48 +05:30
|
|
|
if (prop_object_type(filesd) == PROP_TYPE_DICTIONARY)
|
2010-01-25 20:45:33 +05:30
|
|
|
prop_object_release(filesd);
|
2011-10-27 20:38:48 +05:30
|
|
|
if (prop_object_type(propsd) == PROP_TYPE_DICTIONARY)
|
2010-01-25 20:45:33 +05:30
|
|
|
prop_object_release(propsd);
|
2009-10-06 04:12:37 +05:30
|
|
|
if (broken)
|
2011-10-27 20:38:48 +05:30
|
|
|
return 1;
|
2009-10-06 04:12:37 +05:30
|
|
|
|
2012-05-30 17:51:34 +05:30
|
|
|
return rv;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|