apk: add --wait option to wait for exclusive lock (fixes #26)
parent
1531192cb9
commit
c84196e0d3
15
src/apk.c
15
src/apk.c
|
@ -24,7 +24,7 @@
|
||||||
|
|
||||||
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[] = {
|
||||||
|
@ -38,9 +38,13 @@ static struct apk_option generic_options[] = {
|
||||||
{ '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,
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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,9 +703,25 @@ 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) {
|
||||||
|
if (apk_wait) {
|
||||||
|
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";
|
msg = "Unable to lock database";
|
||||||
goto ret_errno;
|
goto ret_errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
alarm(0);
|
||||||
|
sigaction(SIGALRM, &old_sa, NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue