mirror of
git://git.gnupg.org/gnupg.git
synced 2025-07-03 22:56:33 +02:00
common: Add an info callback to dotlock.
* common/dotlock.h (enum dotlock_reasons): New. (DOTLOCK_PREPARE_CREATE): New flag. * common/dotlock.c (struct dotlock_handle): Add info_cb and info_cb_value. (dotlock_create): Support the new flag. (dotlock_finish_create): New. (read_lockfile): Silence in case of ENOENT. (dotlock_set_info_cb): New. Use callback after all error and info messages. (dotlock_take_unix, dotlock_take_w32): Allow termination by callback.
This commit is contained in:
parent
f57717bf23
commit
937aeb1904
3 changed files with 203 additions and 11 deletions
|
@ -52,6 +52,7 @@
|
|||
#ifdef HAVE_W32_SYSTEM
|
||||
#define DIM(v) (sizeof(v)/sizeof((v)[0]))
|
||||
|
||||
|
||||
const char *
|
||||
w32_strerror (int ec)
|
||||
{
|
||||
|
@ -174,6 +175,11 @@ strconcat (const char *s1, ...)
|
|||
|
||||
#define PGM "t-dotlock"
|
||||
|
||||
static int opt_silent;
|
||||
|
||||
|
||||
|
||||
|
||||
#ifndef HAVE_W32_SYSTEM
|
||||
static volatile int ctrl_c_pending_flag;
|
||||
static void
|
||||
|
@ -217,6 +223,9 @@ inf (const char *format, ...)
|
|||
{
|
||||
va_list arg_ptr;
|
||||
|
||||
if (opt_silent)
|
||||
return;
|
||||
|
||||
va_start (arg_ptr, format);
|
||||
fprintf (stderr, PGM "[%lu]: ", (unsigned long)getpid ());
|
||||
vfprintf (stderr, format, arg_ptr);
|
||||
|
@ -225,15 +234,35 @@ inf (const char *format, ...)
|
|||
}
|
||||
|
||||
|
||||
static int
|
||||
lock_info_cb (dotlock_t h, void *opaque, enum dotlock_reasons reason,
|
||||
const char *format, ...)
|
||||
{
|
||||
va_list arg_ptr;
|
||||
|
||||
va_start (arg_ptr, format);
|
||||
fprintf (stderr, PGM "[%lu]: info_cb: reason %d, ",
|
||||
(unsigned long)getpid (), (int)reason);
|
||||
vfprintf (stderr, format, arg_ptr);
|
||||
va_end (arg_ptr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
lock_and_unlock (const char *fname)
|
||||
{
|
||||
dotlock_t h;
|
||||
unsigned long usec;
|
||||
|
||||
h = dotlock_create (fname, 0);
|
||||
h = dotlock_create (fname, DOTLOCK_PREPARE_CREATE);
|
||||
if (!h)
|
||||
die ("error creating lock file for '%s': %s", fname, strerror (errno));
|
||||
dotlock_set_info_cb (h, lock_info_cb, NULL);
|
||||
h = dotlock_finish_create (h, fname);
|
||||
if (!h)
|
||||
die ("error finishing lock file creation for '%s': %s",
|
||||
fname, strerror (errno));
|
||||
inf ("lock created");
|
||||
|
||||
do
|
||||
|
@ -270,6 +299,11 @@ main (int argc, char **argv)
|
|||
ctrl_c_pending_flag = 1;
|
||||
argc--;
|
||||
}
|
||||
if (argc > 1 && !strcmp (argv[1], "--silent"))
|
||||
{
|
||||
opt_silent = 1;
|
||||
argc--;
|
||||
}
|
||||
|
||||
if (argc > 1)
|
||||
fname = argv[argc-1];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue