Introduce xbps_repo_{un,}lock() to serialize write access to repodata.

We use a simple file lock that is created with O_CREAT|O_EXCL.
This should fix the concurrency issues with multiple processes
running xbps-rindex -a/-c on the same repository/arch combo.
This commit is contained in:
Juan RP
2015-03-25 12:00:59 +01:00
parent bb4ebf8152
commit 9a16283575
7 changed files with 120 additions and 44 deletions

View File

@@ -43,8 +43,8 @@ index_add(struct xbps_handle *xhp, int args, int argmax, char **argv, bool force
xbps_dictionary_t idx, idxmeta, binpkgd, curpkgd;
struct xbps_repo *repo = NULL;
struct stat st;
char *tmprepodir = NULL, *repodir = NULL;
int rv = 0, ret = 0;
char *tmprepodir = NULL, *repodir = NULL, *rlockfname = NULL;
int rv = 0, ret = 0, rlockfd = -1;
bool flush = false;
assert(argv);
@@ -55,7 +55,13 @@ index_add(struct xbps_handle *xhp, int args, int argmax, char **argv, bool force
return ENOMEM;
repodir = dirname(tmprepodir);
repo = xbps_repo_open(xhp, repodir, true);
if (!xbps_repo_lock(xhp, repodir, &rlockfd, &rlockfname)) {
fprintf(stderr, "xbps-rindex: cannot lock repository "
"%s: %s\n", repodir, strerror(errno));
rv = -1;
goto out;
}
repo = xbps_repo_open(xhp, repodir);
if (repo == NULL && errno != ENOENT) {
fprintf(stderr, "xbps-rindex: cannot open/lock repository "
"%s: %s\n", repodir, strerror(errno));
@@ -217,6 +223,9 @@ index_add(struct xbps_handle *xhp, int args, int argmax, char **argv, bool force
out:
if (repo)
xbps_repo_close(repo);
xbps_repo_unlock(rlockfd, rlockfname);
if (tmprepodir)
free(tmprepodir);

View File

@@ -96,10 +96,17 @@ index_clean(struct xbps_handle *xhp, const char *repodir)
xbps_dictionary_t idx = NULL, idxmeta = NULL;
struct xbps_repo *repo;
struct cbdata cbd;
int rv = 0;
char *rlockfname = NULL;
int rv = 0, rlockfd = -1;
bool flush = false;
repo = xbps_repo_open(xhp, repodir, true);
if (!xbps_repo_lock(xhp, repodir, &rlockfd, &rlockfname)) {
rv = errno;
fprintf(stderr, "%s: cannot lock repository: %s\n",
_XBPS_RINDEX, strerror(rv));
return rv;
}
repo = xbps_repo_open(xhp, repodir);
if (repo == NULL) {
rv = errno;
if (rv == ENOENT)
@@ -156,6 +163,8 @@ index_clean(struct xbps_handle *xhp, const char *repodir)
out:
xbps_repo_close(repo);
xbps_repo_unlock(rlockfd, rlockfname);
if (idx)
xbps_object_release(idx);
if (idxmeta)

View File

@@ -118,7 +118,7 @@ remove_obsoletes(struct xbps_handle *xhp, const char *repodir)
char *ext;
int rv = 0;
repo = xbps_repo_open(xhp, repodir, false);
repo = xbps_repo_open(xhp, repodir);
if (repo == NULL) {
if (errno != ENOENT) {
fprintf(stderr, "xbps-rindex: cannot read repository data: %s\n",

View File

@@ -119,7 +119,7 @@ sign_repo(struct xbps_handle *xhp, const char *repodir,
const char *privkey, const char *signedby)
{
struct stat st;
struct xbps_repo *repo;
struct xbps_repo *repo = NULL;
xbps_dictionary_t pkgd, meta = NULL;
xbps_data_t data = NULL, rpubkey = NULL;
xbps_object_iterator_t iter = NULL;
@@ -129,8 +129,9 @@ sign_repo(struct xbps_handle *xhp, const char *repodir,
unsigned int siglen;
uint16_t rpubkeysize, pubkeysize;
const char *arch, *pkgver, *rsignedby = NULL;
char *binpkg = NULL, *binpkg_sig = NULL, *buf = NULL, *defprivkey = NULL;
int binpkg_fd, binpkg_sig_fd, rv = 0;
char *binpkg = NULL, *binpkg_sig = NULL, *buf = NULL;
char *rlockfname = NULL, *defprivkey = NULL;
int binpkg_fd, binpkg_sig_fd, rlockfd = -1, rv = 0;
bool flush = false;
if (signedby == NULL) {
@@ -141,7 +142,13 @@ sign_repo(struct xbps_handle *xhp, const char *repodir,
/*
* Check that repository index exists and not empty, otherwise bail out.
*/
repo = xbps_repo_open(xhp, repodir, true);
if (!xbps_repo_lock(xhp, repodir, &rlockfd, &rlockfname)) {
rv = errno;
fprintf(stderr, "%s: cannot lock repository: %s\n",
_XBPS_RINDEX, strerror(errno));
goto out;
}
repo = xbps_repo_open(xhp, repodir);
if (repo == NULL) {
rv = errno;
fprintf(stderr, "%s: cannot read repository data: %s\n",
@@ -304,5 +311,7 @@ out:
if (repo) {
xbps_repo_close(repo);
}
xbps_repo_unlock(rlockfd, rlockfname);
return rv ? -1 : 0;
}