apk: add --wait option to wait for exclusive lock (fixes #26)

cute-signatures
Timo Teras 2009-07-07 10:30:54 +03:00
parent 1531192cb9
commit c84196e0d3
3 changed files with 37 additions and 9 deletions

View File

@ -24,23 +24,27 @@
const char *apk_root; const char *apk_root;
struct apk_repository_url apk_repository_list; struct apk_repository_url apk_repository_list;
int apk_verbosity = 1, apk_cwd_fd; int apk_verbosity = 1, apk_cwd_fd, apk_wait;
unsigned int apk_flags = 0; unsigned int apk_flags = 0;
static struct apk_option generic_options[] = { static struct apk_option generic_options[] = {
{ 'h', "help", "Show generic help or applet specific help" }, { 'h', "help", "Show generic help or applet specific help" },
{ 'p', "root", "Install packages to DIR", { 'p', "root", "Install packages to DIR",
required_argument, "DIR" }, required_argument, "DIR" },
{ 'X', "repository", "Use packages from REPO", { 'X', "repository", "Use packages from REPO",
required_argument, "REPO" }, required_argument, "REPO" },
{ 'q', "quiet", "Print less information" }, { 'q', "quiet", "Print less information" },
{ 'v', "verbose", "Print more information" }, { 'v', "verbose", "Print more information" },
{ 'V', "version", "Print program version and exit" }, { 'V', "version", "Print program version and exit" },
{ 'f', "force", "Do what was asked even if it looks dangerous" }, { 'f', "force", "Do what was asked even if it looks dangerous" },
{ 0x101, "progress", "Show a progress bar" }, { 0x101, "progress", "Show a progress bar" },
{ 0x102, "clean-protected", { 0x102, "clean-protected", "Do not create .apk-new files to "
"Do not create .apk-new files to configuration dirs" }, "configuration dirs" },
{ 0x104, "simulate", "Show what would be done without actually doing it" }, { 0x104, "simulate", "Show what would be done without actually "
"doing it" },
{ 0x105, "wait", "Wait for TIME seconds to get an exclusive "
"repository lock before failing",
required_argument, "TIME" },
}; };
void apk_log(const char *prefix, const char *format, ...) void apk_log(const char *prefix, const char *format, ...)
@ -305,6 +309,9 @@ int main(int argc, char **argv)
case 0x104: case 0x104:
apk_flags |= APK_SIMULATE; apk_flags |= APK_SIMULATE;
break; break;
case 0x105:
apk_wait = atoi(optarg);
break;
default: default:
if (applet == NULL || applet->parse == NULL || if (applet == NULL || applet->parse == NULL ||
applet->parse(ctx, r, applet->parse(ctx, r,

View File

@ -50,7 +50,7 @@ extern csum_t bad_checksum;
#define csum_valid(buf) memcmp(buf, bad_checksum, sizeof(csum_t)) #define csum_valid(buf) memcmp(buf, bad_checksum, sizeof(csum_t))
#endif #endif
extern int apk_cwd_fd, apk_verbosity; extern int apk_cwd_fd, apk_verbosity, apk_wait;
extern unsigned int apk_flags; extern unsigned int apk_flags;
#define APK_FORCE 0x0001 #define APK_FORCE 0x0001

View File

@ -17,6 +17,7 @@
#include <malloc.h> #include <malloc.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <signal.h>
#include <sys/file.h> #include <sys/file.h>
#include "apk_defines.h" #include "apk_defines.h"
@ -653,6 +654,10 @@ static int apk_db_create(struct apk_database *db)
return 0; return 0;
} }
static void handle_alarm(int sig)
{
}
int apk_db_open(struct apk_database *db, const char *root, unsigned int flags) int apk_db_open(struct apk_database *db, const char *root, unsigned int flags)
{ {
const char *apk_repos = getenv("APK_REPOS"), *msg = NULL; const char *apk_repos = getenv("APK_REPOS"), *msg = NULL;
@ -698,8 +703,24 @@ int apk_db_open(struct apk_database *db, const char *root, unsigned int flags)
} }
if (db->lock_fd < 0 || if (db->lock_fd < 0 ||
flock(db->lock_fd, LOCK_EX | LOCK_NB) < 0) { flock(db->lock_fd, LOCK_EX | LOCK_NB) < 0) {
msg = "Unable to lock database"; if (apk_wait) {
goto ret_errno; struct sigaction sa, old_sa;
apk_message("Waiting for repository lock");
memset(&sa, 0, sizeof sa);
sa.sa_handler = handle_alarm;
sa.sa_flags = SA_ONESHOT;
sigaction(SIGALRM, &sa, &old_sa);
alarm(apk_wait);
if (flock(db->lock_fd, LOCK_EX) < 0) {
msg = "Unable to lock database";
goto ret_errno;
}
alarm(0);
sigaction(SIGALRM, &old_sa, NULL);
}
} }
} }
} }