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

@@ -48,7 +48,7 @@
*
* This header documents the full API for the XBPS Library.
*/
#define XBPS_API_VERSION "20150219-1"
#define XBPS_API_VERSION "20150323"
#ifndef XBPS_VERSION
#define XBPS_VERSION "UNSET"
@@ -1265,7 +1265,6 @@ struct xbps_repo {
* @private
*/
int fd;
bool is_locked;
/**
* var is_remote
*
@@ -1412,16 +1411,36 @@ xbps_dictionary_t xbps_rpool_get_pkg_plist(struct xbps_handle *xhp,
*/
bool xbps_repo_store(struct xbps_handle *xhp, const char *url);
/**
* Creates a lock for a local repository to obtain exclusive access (write).
*
* @param[in] xhp Pointer to the xbps_handle struct.
* @param[in] uri Repository URI to match.
* @param[out] lockfd Lock file descriptor assigned.
* @param[out] lockfname Lock filename assigned.
*
* @return True on success and lockfd/lockfname are assigned appropiately.
* otherwise false and lockfd/lockfname aren't set.
*/
bool xbps_repo_lock(struct xbps_handle *xhp, const char *uri, int *lockfd, char **lockfname);
/**
* Unlocks a local repository and removes its lock file.
*
* @param[in] lockfd Lock file descriptor.
* @param[in] lockfname Lock filename.
*/
void xbps_repo_unlock(int lockfd, char *lockfname);
/**
* Opens a repository and returns a xbps_repo object.
*
* @param[in] xhp Pointer to the xbps_handle struct.
* @param[in] uri Repository URI to match.
* @param[in] lock Set it to true to acquire a POSIX file lock.
*
* @return The matching repository object, NULL otherwise.
*/
struct xbps_repo *xbps_repo_open(struct xbps_handle *xhp, const char *url, bool lock);
struct xbps_repo *xbps_repo_open(struct xbps_handle *xhp, const char *url);
/**
* Closes a repository object and releases resources.