xbps-rindex: eliminate locking and update repo index immutable dicts.

This commit is contained in:
Juan RP 2015-05-22 12:20:49 +02:00
parent 4fdc8de9d6
commit b459e4542f
2 changed files with 9 additions and 26 deletions

View File

@ -69,8 +69,8 @@ index_add(struct xbps_handle *xhp, int args, int argmax, char **argv, bool force
goto out; goto out;
} }
if (repo) { if (repo) {
idx = xbps_dictionary_copy(repo->idx); idx = xbps_dictionary_copy_mutable(repo->idx);
idxmeta = xbps_dictionary_copy(repo->idxmeta); idxmeta = xbps_dictionary_copy_mutable(repo->idxmeta);
} else { } else {
idx = xbps_dictionary_create(); idx = xbps_dictionary_create();
idxmeta = NULL; idxmeta = NULL;

View File

@ -39,8 +39,6 @@
#include "defs.h" #include "defs.h"
static xbps_dictionary_t dest; static xbps_dictionary_t dest;
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static bool flush;
static int static int
idx_cleaner_cb(struct xbps_handle *xhp, idx_cleaner_cb(struct xbps_handle *xhp,
@ -64,13 +62,10 @@ idx_cleaner_cb(struct xbps_handle *xhp,
* File cannot be read, might be permissions, * File cannot be read, might be permissions,
* broken or simply unexistent; either way, remove it. * broken or simply unexistent; either way, remove it.
*/ */
pthread_mutex_lock(&mtx);
pkgname = xbps_pkg_name(pkgver); pkgname = xbps_pkg_name(pkgver);
xbps_dictionary_remove(dest, pkgname); xbps_dictionary_remove(dest, pkgname);
free(pkgname); free(pkgname);
flush = true;
printf("index: removed pkg %s\n", pkgver); printf("index: removed pkg %s\n", pkgver);
pthread_mutex_unlock(&mtx);
} else { } else {
/* /*
* File can be read; check its hash. * File can be read; check its hash.
@ -78,13 +73,10 @@ idx_cleaner_cb(struct xbps_handle *xhp,
xbps_dictionary_get_cstring_nocopy(obj, 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) {
pthread_mutex_lock(&mtx);
pkgname = xbps_pkg_name(pkgver); pkgname = xbps_pkg_name(pkgver);
xbps_dictionary_remove(dest, pkgname); xbps_dictionary_remove(dest, pkgname);
free(pkgname); free(pkgname);
printf("index: removed pkg %s\n", pkgver); printf("index: removed pkg %s\n", pkgver);
flush = true;
pthread_mutex_unlock(&mtx);
} }
} }
free(filen); free(filen);
@ -99,7 +91,6 @@ int
index_clean(struct xbps_handle *xhp, const char *repodir) index_clean(struct xbps_handle *xhp, const char *repodir)
{ {
xbps_array_t allkeys; xbps_array_t allkeys;
xbps_dictionary_t idx = NULL, idxmeta = NULL;
struct xbps_repo *repo; struct xbps_repo *repo;
char *rlockfname = NULL; char *rlockfname = NULL;
int rv = 0, rlockfd = -1; int rv = 0, rlockfd = -1;
@ -120,9 +111,7 @@ index_clean(struct xbps_handle *xhp, const char *repodir)
_XBPS_RINDEX, strerror(errno)); _XBPS_RINDEX, strerror(errno));
return rv; return rv;
} }
idx = xbps_dictionary_copy(repo->idx); if (repo->idx == NULL) {
idxmeta = xbps_dictionary_copy(repo->idxmeta);
if (idx == NULL) {
fprintf(stderr, "%s: incomplete repository data file!\n", _XBPS_RINDEX); fprintf(stderr, "%s: incomplete repository data file!\n", _XBPS_RINDEX);
rv = EINVAL; rv = EINVAL;
goto out; goto out;
@ -132,30 +121,24 @@ index_clean(struct xbps_handle *xhp, const char *repodir)
/* /*
* First pass: find out obsolete entries on index and index-files. * First pass: find out obsolete entries on index and index-files.
*/ */
dest = xbps_dictionary_copy(idx); dest = xbps_dictionary_copy_mutable(repo->idx);
allkeys = xbps_dictionary_all_keys(idx); allkeys = xbps_dictionary_all_keys(dest);
(void)xbps_array_foreach_cb_multi(xhp, allkeys, idx, idx_cleaner_cb, __UNCONST(repodir)); (void)xbps_array_foreach_cb_multi(xhp, allkeys, repo->idx, idx_cleaner_cb, __UNCONST(repodir));
xbps_object_release(allkeys); xbps_object_release(allkeys);
if (flush) { if (!xbps_dictionary_equals(dest, repo->idx)) {
if (!repodata_flush(xhp, repodir, dest, idxmeta)) { if (!repodata_flush(xhp, repodir, dest, repo->idxmeta)) {
rv = errno; rv = errno;
fprintf(stderr, "failed to write repodata: %s\n", fprintf(stderr, "failed to write repodata: %s\n",
strerror(errno)); strerror(errno));
goto out; goto out;
} }
} }
printf("index: %u packages registered.\n", printf("index: %u packages registered.\n", xbps_dictionary_count(dest));
xbps_dictionary_count(dest));
out: out:
xbps_repo_close(repo); xbps_repo_close(repo);
xbps_repo_unlock(rlockfd, rlockfname); xbps_repo_unlock(rlockfd, rlockfname);
if (idx)
xbps_object_release(idx);
if (idxmeta)
xbps_object_release(idxmeta);
return rv; return rv;
} }