2012-12-14 09:29:47 +05:30
|
|
|
/*-
|
2013-03-05 08:38:42 +05:30
|
|
|
* Copyright (c) 2012-2013 Juan Romero Pardines.
|
2012-12-14 09:29:47 +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>
|
|
|
|
#include <pthread.h>
|
2013-06-10 13:58:39 +05:30
|
|
|
#include <fcntl.h>
|
2012-12-14 09:29:47 +05:30
|
|
|
|
2013-06-20 16:01:02 +05:30
|
|
|
#include <xbps.h>
|
2012-12-14 09:29:47 +05:30
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
struct thread_data {
|
|
|
|
pthread_t thread;
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t idx;
|
|
|
|
xbps_dictionary_t idxfiles;
|
|
|
|
xbps_array_t result;
|
|
|
|
xbps_array_t result_files;
|
2012-12-14 09:29:47 +05:30
|
|
|
struct xbps_handle *xhp;
|
|
|
|
unsigned int start;
|
|
|
|
unsigned int end;
|
|
|
|
int thread_num;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void *
|
|
|
|
cleaner_thread(void *arg)
|
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_t obj;
|
|
|
|
xbps_dictionary_t pkgd;
|
|
|
|
xbps_array_t array;
|
2012-12-14 09:29:47 +05:30
|
|
|
struct thread_data *thd = arg;
|
2013-03-05 08:38:42 +05:30
|
|
|
char *filen;
|
|
|
|
const char *pkgver, *arch, *sha256;
|
2012-12-14 09:29:47 +05:30
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
/* process pkgs from start until end */
|
2013-06-20 13:56:12 +05:30
|
|
|
array = xbps_dictionary_all_keys(thd->idx);
|
2012-12-14 09:29:47 +05:30
|
|
|
|
|
|
|
for (i = thd->start; i < thd->end; i++) {
|
2013-06-20 13:56:12 +05:30
|
|
|
obj = xbps_array_get(array, i);
|
|
|
|
pkgd = xbps_dictionary_get_keysym(thd->idx, obj);
|
|
|
|
xbps_dictionary_get_cstring_nocopy(pkgd, "architecture", &arch);
|
|
|
|
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
|
2013-03-05 09:33:15 +05:30
|
|
|
filen = xbps_xasprintf("%s.%s.xbps", pkgver, arch);
|
2012-12-14 09:29:47 +05:30
|
|
|
xbps_dbg_printf(thd->xhp, "thread[%d] checking %s\n",
|
|
|
|
thd->thread_num, pkgver);
|
|
|
|
if (access(filen, R_OK) == -1) {
|
|
|
|
/*
|
|
|
|
* File cannot be read, might be permissions,
|
|
|
|
* broken or simply unexistent; either way, remove it.
|
|
|
|
*/
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_add_cstring_nocopy(thd->result, pkgver);
|
2013-03-05 08:38:42 +05:30
|
|
|
free(filen);
|
2012-12-14 09:29:47 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* File can be read; check its hash.
|
|
|
|
*/
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(pkgd,
|
2012-12-14 09:29:47 +05:30
|
|
|
"filename-sha256", &sha256);
|
|
|
|
if (xbps_file_hash_check(filen, sha256) != 0)
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_add_cstring_nocopy(thd->result, pkgver);
|
2013-03-05 08:38:42 +05:30
|
|
|
free(filen);
|
2012-12-14 09:29:47 +05:30
|
|
|
}
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_release(array);
|
2012-12-14 09:29:47 +05:30
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-04-07 01:00:31 +05:30
|
|
|
static void *
|
|
|
|
cleaner_files_thread(void *arg)
|
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_t obj;
|
|
|
|
xbps_array_t array;
|
|
|
|
xbps_dictionary_t ipkgd;
|
2013-04-07 01:00:31 +05:30
|
|
|
struct thread_data *thd = arg;
|
2013-06-10 13:58:39 +05:30
|
|
|
const char *pkgver, *ipkgver;
|
|
|
|
char *pkgname;
|
2013-04-07 01:00:31 +05:30
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
/* process pkgs from start until end */
|
2013-06-20 13:56:12 +05:30
|
|
|
array = xbps_dictionary_all_keys(thd->idxfiles);
|
2013-04-07 01:00:31 +05:30
|
|
|
|
|
|
|
for (i = thd->start; i < thd->end; i++) {
|
2013-06-20 13:56:12 +05:30
|
|
|
obj = xbps_array_get(array, i);
|
|
|
|
pkgver = xbps_dictionary_keysym_cstring_nocopy(obj);
|
2013-06-10 13:58:39 +05:30
|
|
|
pkgname = xbps_pkg_name(pkgver);
|
|
|
|
assert(pkgname);
|
2013-06-20 13:56:12 +05:30
|
|
|
ipkgd = xbps_dictionary_get(thd->idx, pkgname);
|
2013-04-07 01:00:31 +05:30
|
|
|
/* If pkg is not registered in index, remove it */
|
2013-06-10 13:58:39 +05:30
|
|
|
if (ipkgd == NULL)
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_add_cstring_nocopy(thd->result_files, pkgver);
|
2013-06-10 13:58:39 +05:30
|
|
|
/* if another version is registered in index, remove it */
|
|
|
|
else {
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(ipkgd, "pkgver", &ipkgver);
|
2013-06-10 13:58:39 +05:30
|
|
|
if (strcmp(ipkgver, pkgver))
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_add_cstring_nocopy(thd->result_files, pkgver);
|
2013-06-10 13:58:39 +05:30
|
|
|
}
|
|
|
|
free(pkgname);
|
2013-04-07 01:00:31 +05:30
|
|
|
}
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_release(array);
|
2013-04-07 01:00:31 +05:30
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-12-14 09:29:47 +05:30
|
|
|
/*
|
|
|
|
* Removes stalled pkg entries in repository's index.plist file, if any
|
|
|
|
* binary package cannot be read (unavailable, not enough perms, etc).
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
index_clean(struct xbps_handle *xhp, const char *repodir)
|
|
|
|
{
|
2013-06-10 13:58:39 +05:30
|
|
|
struct xbps_repo *repo;
|
2012-12-14 09:29:47 +05:30
|
|
|
struct thread_data *thd;
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t idx, idxfiles;
|
2012-12-14 09:29:47 +05:30
|
|
|
const char *keyname;
|
2013-06-10 13:58:39 +05:30
|
|
|
char *pkgname;
|
2013-06-12 13:34:10 +05:30
|
|
|
unsigned int x, pkgcount, slicecount;
|
2012-12-14 09:29:47 +05:30
|
|
|
int i, maxthreads, rv = 0;
|
|
|
|
bool flush = false;
|
|
|
|
|
2013-06-10 13:58:39 +05:30
|
|
|
repo = xbps_repo_open(xhp, repodir);
|
|
|
|
if (repo == NULL) {
|
|
|
|
if (errno == ENOENT)
|
2012-12-14 09:29:47 +05:30
|
|
|
return 0;
|
2013-06-10 13:58:39 +05:30
|
|
|
fprintf(stderr, "index: cannot read repository data: %s\n", strerror(errno));
|
|
|
|
return -1;
|
2012-12-14 09:29:47 +05:30
|
|
|
}
|
2013-06-10 13:58:39 +05:30
|
|
|
idx = xbps_repo_get_plist(repo, XBPS_PKGINDEX);
|
|
|
|
idxfiles = xbps_repo_get_plist(repo, XBPS_PKGINDEX_FILES);
|
|
|
|
xbps_repo_close(repo);
|
|
|
|
if (idx == NULL || idxfiles == NULL) {
|
|
|
|
fprintf(stderr, "incomplete repository data file!");
|
|
|
|
return -1;
|
2012-12-14 09:29:47 +05:30
|
|
|
}
|
|
|
|
if (chdir(repodir) == -1) {
|
|
|
|
fprintf(stderr, "index: cannot chdir to %s: %s\n",
|
|
|
|
repodir, strerror(errno));
|
2012-12-14 16:28:30 +05:30
|
|
|
return -1;
|
2012-12-14 09:29:47 +05:30
|
|
|
}
|
|
|
|
printf("Cleaning `%s' index, please wait...\n", repodir);
|
|
|
|
|
|
|
|
maxthreads = (int)sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
thd = calloc(maxthreads, sizeof(*thd));
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
slicecount = xbps_dictionary_count(idx) / maxthreads;
|
2012-12-14 09:29:47 +05:30
|
|
|
pkgcount = 0;
|
|
|
|
|
2013-04-07 01:00:31 +05:30
|
|
|
/* Setup threads to cleanup index and index-files */
|
2012-12-14 09:29:47 +05:30
|
|
|
for (i = 0; i < maxthreads; i++) {
|
|
|
|
thd[i].thread_num = i;
|
|
|
|
thd[i].idx = idx;
|
2013-06-20 13:56:12 +05:30
|
|
|
thd[i].result = xbps_array_create();
|
2012-12-14 09:29:47 +05:30
|
|
|
thd[i].xhp = xhp;
|
|
|
|
thd[i].start = pkgcount;
|
|
|
|
if (i + 1 >= maxthreads)
|
2013-06-20 13:56:12 +05:30
|
|
|
thd[i].end = xbps_dictionary_count(idx);
|
2012-12-14 09:29:47 +05:30
|
|
|
else
|
|
|
|
thd[i].end = pkgcount + slicecount;
|
|
|
|
pthread_create(&thd[i].thread, NULL, cleaner_thread, &thd[i]);
|
|
|
|
pkgcount += slicecount;
|
|
|
|
}
|
|
|
|
/* wait for all threads */
|
|
|
|
for (i = 0; i < maxthreads; i++)
|
|
|
|
pthread_join(thd[i].thread, NULL);
|
|
|
|
|
2013-04-07 01:00:31 +05:30
|
|
|
/* Setup threads to cleanup index-files */
|
2013-06-20 13:56:12 +05:30
|
|
|
slicecount = xbps_dictionary_count(idxfiles) / maxthreads;
|
2013-04-07 01:00:31 +05:30
|
|
|
pkgcount = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < maxthreads; i++) {
|
|
|
|
thd[i].thread_num = i;
|
|
|
|
thd[i].idx = idx;
|
|
|
|
thd[i].idxfiles = idxfiles;
|
2013-06-20 13:56:12 +05:30
|
|
|
thd[i].result_files = xbps_array_create();
|
2013-04-07 01:00:31 +05:30
|
|
|
thd[i].xhp = xhp;
|
|
|
|
thd[i].start = pkgcount;
|
|
|
|
if (i + 1 >= maxthreads)
|
2013-06-20 13:56:12 +05:30
|
|
|
thd[i].end = xbps_dictionary_count(idxfiles);
|
2013-04-07 01:00:31 +05:30
|
|
|
else
|
|
|
|
thd[i].end = pkgcount + slicecount;
|
|
|
|
pthread_create(&thd[i].thread, NULL, cleaner_files_thread, &thd[i]);
|
|
|
|
pkgcount += slicecount;
|
|
|
|
}
|
|
|
|
/* wait for all threads */
|
|
|
|
for (i = 0; i < maxthreads; i++)
|
|
|
|
pthread_join(thd[i].thread, NULL);
|
|
|
|
|
2012-12-14 09:29:47 +05:30
|
|
|
for (i = 0; i < maxthreads; i++) {
|
2013-06-20 13:56:12 +05:30
|
|
|
for (x = 0; x < xbps_array_count(thd[i].result); x++) {
|
|
|
|
xbps_array_get_cstring_nocopy(thd[i].result,
|
2012-12-14 09:29:47 +05:30
|
|
|
x, &keyname);
|
|
|
|
printf("index: removed entry %s\n", keyname);
|
|
|
|
pkgname = xbps_pkg_name(keyname);
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_remove(idx, pkgname);
|
|
|
|
xbps_dictionary_remove(idxfiles, keyname);
|
2012-12-14 09:29:47 +05:30
|
|
|
free(pkgname);
|
|
|
|
flush = true;
|
|
|
|
}
|
2013-06-20 13:56:12 +05:30
|
|
|
for (x = 0; x < xbps_array_count(thd[i].result_files); x++) {
|
|
|
|
xbps_array_get_cstring_nocopy(thd[i].result_files,
|
2013-04-07 01:00:31 +05:30
|
|
|
x, &keyname);
|
|
|
|
printf("index-files: removed entry %s\n", keyname);
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_remove(idxfiles, keyname);
|
2013-04-07 01:00:31 +05:30
|
|
|
flush = true;
|
|
|
|
}
|
2012-12-14 09:29:47 +05:30
|
|
|
}
|
2013-06-10 13:58:39 +05:30
|
|
|
if (flush) {
|
|
|
|
rv = repodata_flush(xhp, repodir, idx, idxfiles);
|
|
|
|
if (rv != 0)
|
|
|
|
return rv;
|
|
|
|
}
|
2012-12-14 09:29:47 +05:30
|
|
|
printf("index: %u packages registered.\n",
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_count(idx));
|
2012-12-14 09:29:47 +05:30
|
|
|
printf("index-files: %u packages registered.\n",
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_count(idxfiles));
|
|
|
|
xbps_object_release(idx);
|
|
|
|
xbps_object_release(idxfiles);
|
2012-12-14 09:29:47 +05:30
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|