xbps-rindex: register pkgs in index-files by pkgname and use xbps_array_foreach_cb().

This simplifies accessing/removing pkgs in this dictionary.
This commit is contained in:
Juan RP 2013-09-16 10:41:42 +02:00
parent 98e1687ef5
commit af4e90aae8
2 changed files with 46 additions and 142 deletions

View File

@ -138,6 +138,7 @@ index_add(struct xbps_handle *xhp, int argc, char **argv, bool force)
*/ */
buf = xbps_xasprintf("`%s' (%s)", oldpkgver, oldarch); buf = xbps_xasprintf("`%s' (%s)", oldpkgver, oldarch);
xbps_dictionary_remove(idx, pkgname); xbps_dictionary_remove(idx, pkgname);
xbps_dictionary_remove(idxfiles, pkgname);
printf("index: removed obsolete entry %s.\n", buf); printf("index: removed obsolete entry %s.\n", buf);
free(buf); free(buf);
} }
@ -254,7 +255,7 @@ index_add(struct xbps_handle *xhp, int argc, char **argv, bool force)
xbps_object_release(newpkgfilesd); xbps_object_release(newpkgfilesd);
/* add pkg files array into index-files */ /* add pkg files array into index-files */
xbps_dictionary_set(idxfiles, pkgver, filespkgar); xbps_dictionary_set(idxfiles, pkgname, filespkgar);
xbps_object_release(filespkgar); xbps_object_release(filespkgar);
printf("index-files: added `%s' (%s)\n", pkgver, arch); printf("index-files: added `%s' (%s)\n", pkgver, arch);

View File

@ -38,95 +38,45 @@
#include <xbps.h> #include <xbps.h>
#include "defs.h" #include "defs.h"
struct thread_data { struct cbdata {
pthread_t thread; pthread_mutex_t mtx;
xbps_dictionary_t idx; xbps_array_t array;
xbps_dictionary_t idxfiles;
xbps_array_t result;
xbps_array_t result_files;
struct xbps_handle *xhp;
unsigned int start;
unsigned int end;
int thread_num;
}; };
static void * static int
cleaner_thread(void *arg) idx_cleaner_cb(struct xbps_handle *xhp,
xbps_object_t obj,
const char *key _unused,
void *arg,
bool *done _unused)
{ {
xbps_object_t obj; struct cbdata *cbd = arg;
xbps_dictionary_t pkgd; const char *arch, *pkgver, *sha256;
xbps_array_t array;
struct thread_data *thd = arg;
char *filen; char *filen;
const char *pkgver, *arch, *sha256;
/* process pkgs from start until end */ xbps_dictionary_get_cstring_nocopy(obj, "architecture", &arch);
array = xbps_dictionary_all_keys(thd->idx); xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
for (unsigned int i = thd->start; i < thd->end; i++) { xbps_dbg_printf(xhp, "%s: checking %s [%s] ...", pkgver, arch);
obj = xbps_array_get(array, i);
pkgd = xbps_dictionary_get_keysym(thd->idx, obj); filen = xbps_xasprintf("%s.%s.xbps", pkgver, arch);
xbps_dictionary_get_cstring_nocopy(pkgd, "architecture", &arch); if (access(filen, R_OK) == -1) {
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver); /*
filen = xbps_xasprintf("%s.%s.xbps", pkgver, arch); * File cannot be read, might be permissions,
xbps_dbg_printf(thd->xhp, "thread[%d] checking %s\n", * broken or simply unexistent; either way, remove it.
thd->thread_num, pkgver); */
if (access(filen, R_OK) == -1) { xbps_array_add_cstring_nocopy(cbd->array, pkgver);
/* } else {
* File cannot be read, might be permissions,
* broken or simply unexistent; either way, remove it.
*/
xbps_array_add_cstring_nocopy(thd->result, pkgver);
free(filen);
continue;
}
/* /*
* File can be read; check its hash. * File can be read; check its hash.
*/ */
xbps_dictionary_get_cstring_nocopy(pkgd, xbps_dictionary_get_cstring_nocopy(obj,
"filename-sha256", &sha256); "filename-sha256", &sha256);
if (xbps_file_hash_check(filen, sha256) != 0) if (xbps_file_hash_check(filen, sha256) != 0)
xbps_array_add_cstring_nocopy(thd->result, pkgver); xbps_array_add_cstring_nocopy(cbd->array, pkgver);
free(filen);
} }
xbps_object_release(array); free(filen);
return 0;
return NULL;
}
static void *
cleaner_files_thread(void *arg)
{
xbps_object_t obj;
xbps_array_t array;
xbps_dictionary_t ipkgd;
struct thread_data *thd = arg;
const char *pkgver, *ipkgver;
char *pkgname;
/* process pkgs from start until end */
array = xbps_dictionary_all_keys(thd->idxfiles);
for (unsigned int i = thd->start; i < thd->end; i++) {
obj = xbps_array_get(array, i);
pkgver = xbps_dictionary_keysym_cstring_nocopy(obj);
pkgname = xbps_pkg_name(pkgver);
assert(pkgname);
ipkgd = xbps_dictionary_get(thd->idx, pkgname);
/* If pkg is not registered in index, remove it */
if (ipkgd == NULL)
xbps_array_add_cstring_nocopy(thd->result_files, pkgver);
/* if another version is registered in index, remove it */
else {
xbps_dictionary_get_cstring_nocopy(ipkgd, "pkgver", &ipkgver);
if (strcmp(ipkgver, pkgver))
xbps_array_add_cstring_nocopy(thd->result_files, pkgver);
}
free(pkgname);
}
xbps_object_release(array);
return NULL;
} }
/* /*
@ -137,12 +87,12 @@ int
index_clean(struct xbps_handle *xhp, const char *repodir) index_clean(struct xbps_handle *xhp, const char *repodir)
{ {
struct xbps_repo *repo; struct xbps_repo *repo;
struct thread_data *thd; struct cbdata cbd;
xbps_array_t allkeys;
xbps_dictionary_t idx, idxfiles; xbps_dictionary_t idx, idxfiles;
const char *keyname; const char *keyname;
char *pkgname; char *pkgname;
unsigned int pkgcount, slicecount; int rv = 0;
int maxthreads, rv = 0;
bool flush = false; bool flush = false;
repo = xbps_repo_open(xhp, repodir); repo = xbps_repo_open(xhp, repodir);
@ -166,71 +116,23 @@ index_clean(struct xbps_handle *xhp, const char *repodir)
} }
printf("Cleaning `%s' index, please wait...\n", repodir); printf("Cleaning `%s' index, please wait...\n", repodir);
maxthreads = (int)sysconf(_SC_NPROCESSORS_ONLN); cbd.array = xbps_array_create();
thd = calloc(maxthreads, sizeof(*thd));
slicecount = xbps_dictionary_count(idx) / maxthreads; allkeys = xbps_dictionary_all_keys(idx);
pkgcount = 0; rv = xbps_array_foreach_cb(xhp, allkeys, idx, idx_cleaner_cb, &cbd);
xbps_object_release(allkeys);
/* Setup threads to cleanup index and index-files */ for (unsigned int x = 0; x < xbps_array_count(cbd.array); x++) {
for (int i = 0; i < maxthreads; i++) { xbps_array_get_cstring_nocopy(cbd.array, x, &keyname);
thd[i].thread_num = i; printf("index: removed entry %s\n", keyname);
thd[i].idx = idx; pkgname = xbps_pkg_name(keyname);
thd[i].result = xbps_array_create(); xbps_dictionary_remove(idx, pkgname);
thd[i].xhp = xhp; xbps_dictionary_remove(idxfiles, pkgname);
thd[i].start = pkgcount; free(pkgname);
if (i + 1 >= maxthreads) flush = true;
thd[i].end = xbps_dictionary_count(idx);
else
thd[i].end = pkgcount + slicecount;
pthread_create(&thd[i].thread, NULL, cleaner_thread, &thd[i]);
pkgcount += slicecount;
} }
/* wait for all threads */ xbps_object_release(cbd.array);
for (int i = 0; i < maxthreads; i++)
pthread_join(thd[i].thread, NULL);
/* Setup threads to cleanup index-files */
slicecount = xbps_dictionary_count(idxfiles) / maxthreads;
pkgcount = 0;
for (int i = 0; i < maxthreads; i++) {
thd[i].thread_num = i;
thd[i].idx = idx;
thd[i].idxfiles = idxfiles;
thd[i].result_files = xbps_array_create();
thd[i].xhp = xhp;
thd[i].start = pkgcount;
if (i + 1 >= maxthreads)
thd[i].end = xbps_dictionary_count(idxfiles);
else
thd[i].end = pkgcount + slicecount;
pthread_create(&thd[i].thread, NULL, cleaner_files_thread, &thd[i]);
pkgcount += slicecount;
}
/* wait for all threads */
for (int i = 0; i < maxthreads; i++)
pthread_join(thd[i].thread, NULL);
for (int i = 0; i < maxthreads; i++) {
for (unsigned int x = 0; x < xbps_array_count(thd[i].result); x++) {
xbps_array_get_cstring_nocopy(thd[i].result,
x, &keyname);
printf("index: removed entry %s\n", keyname);
pkgname = xbps_pkg_name(keyname);
xbps_dictionary_remove(idx, pkgname);
xbps_dictionary_remove(idxfiles, keyname);
free(pkgname);
flush = true;
}
for (unsigned int x = 0; x < xbps_array_count(thd[i].result_files); x++) {
xbps_array_get_cstring_nocopy(thd[i].result_files,
x, &keyname);
printf("index-files: removed entry %s\n", keyname);
xbps_dictionary_remove(idxfiles, keyname);
flush = true;
}
}
if (flush) { if (flush) {
rv = repodata_flush(xhp, repodir, idx, idxfiles); rv = repodata_flush(xhp, repodir, idx, idxfiles);
if (rv != 0) if (rv != 0)
@ -240,6 +142,7 @@ index_clean(struct xbps_handle *xhp, const char *repodir)
xbps_dictionary_count(idx)); xbps_dictionary_count(idx));
printf("index-files: %u packages registered.\n", printf("index-files: %u packages registered.\n",
xbps_dictionary_count(idxfiles)); xbps_dictionary_count(idxfiles));
xbps_object_release(idx); xbps_object_release(idx);
xbps_object_release(idxfiles); xbps_object_release(idxfiles);