1
0
mirror of git://git.gnupg.org/gnupg.git synced 2025-06-09 17:41:05 +02:00

gpg: Split a utility function out of a large function.

* g10/tofu.c (show_statistics): Break the time delta to string code
into...
(time_ago_str): ... this new function.

--
Signed-off-by: Neal H. Walfield <neal@g10code.com>
This commit is contained in:
Neal H. Walfield 2015-11-03 16:24:08 +01:00
parent c8ef9f9a64
commit 3632611229

View File

@ -2184,102 +2184,10 @@ get_trust (struct dbs *dbs, const char *fingerprint, const char *email,
return trust_level;
}
static void
show_statistics (struct dbs *dbs, const char *fingerprint,
const char *email, const char *user_id,
const char *sig_exclude)
static char *
time_ago_str (long long int t)
{
struct db *db;
char *fingerprint_pp;
int rc;
strlist_t strlist = NULL;
char *err = NULL;
db = getdb (dbs, email, DB_EMAIL);
if (! db)
return;
fingerprint_pp = fingerprint_format (fingerprint);
rc = sqlite3_exec_printf
(db->db, strings_collect_cb, &strlist, &err,
"select count (*), strftime('%%s','now') - min (signatures.time)\n"
" from signatures\n"
" left join bindings on signatures.binding = bindings.oid\n"
" where fingerprint = %Q and email = %Q and sig_digest %s%s%s;",
fingerprint, email,
/* We want either: sig_digest != 'SIG_EXCLUDE' or sig_digest is
not NULL. */
sig_exclude ? "!= '" : "is not NULL",
sig_exclude ? sig_exclude : "",
sig_exclude ? "'" : "");
if (rc)
{
log_error (_("error reading from TOFU database"
" (getting statistics): %s\n"),
err);
sqlite3_free (err);
goto out;
}
if (! strlist)
log_info (_("Have never verified a message signed by key %s!\n"),
fingerprint_pp);
else
{
char *tail = NULL;
signed long messages;
signed long first_seen_ago;
assert (strlist_length (strlist) == 2);
errno = 0;
messages = strtol (strlist->d, &tail, 0);
if (errno || *tail != '\0')
/* Abort. */
{
log_debug ("%s:%d: Couldn't convert %s (messages) to an int: %s.\n",
__func__, __LINE__, strlist->d, strerror (errno));
messages = -1;
}
if (messages == 0 && *strlist->next->d == '\0')
/* min(NULL) => NULL => "". */
first_seen_ago = -1;
else
{
errno = 0;
first_seen_ago = strtol (strlist->next->d, &tail, 0);
if (errno || *tail != '\0')
/* Abort. */
{
log_debug ("%s:%d: Cound't convert %s (first_seen) to an int: %s.\n",
__func__, __LINE__,
strlist->next->d, strerror (errno));
first_seen_ago = 0;
}
}
if (messages == -1 || first_seen_ago == 0)
log_info (_("Failed to collect signature statistics for \"%s\" (key %s)\n"),
user_id, fingerprint_pp);
else
{
enum tofu_policy policy = get_policy (dbs, fingerprint, email, NULL);
estream_t fp;
char *msg;
fp = es_fopenmem (0, "rw,samethread");
if (! fp)
log_fatal ("error creating memory stream\n");
if (messages == 0)
es_fprintf (fp,
_("Verified 0 messages signed by \"%s\""
" (key: %s, policy %s)."),
user_id, fingerprint_pp, tofu_policy_str (policy));
else
{
int years = 0;
int months = 0;
int days = 0;
@ -2295,11 +2203,7 @@ show_statistics (struct dbs *dbs, const char *fingerprint,
/* The current unit. */
int i = 0;
es_fprintf (fp,
_("Verified %ld messages signed by \"%s\""
" (key: %s, policy: %s) in the past "),
messages, user_id,
fingerprint_pp, tofu_policy_str (policy));
char *str;
/* It would be nice to use a macro to do this, but gettext
works on the unpreprocessed code. */
@ -2309,32 +2213,32 @@ show_statistics (struct dbs *dbs, const char *fingerprint,
#define MONTH_SECS (30 * DAY_SECS)
#define YEAR_SECS (365 * DAY_SECS)
if (first_seen_ago > YEAR_SECS)
if (t > YEAR_SECS)
{
years = first_seen_ago / YEAR_SECS;
first_seen_ago -= years * YEAR_SECS;
years = t / YEAR_SECS;
t -= years * YEAR_SECS;
}
if (first_seen_ago > MONTH_SECS)
if (t > MONTH_SECS)
{
months = first_seen_ago / MONTH_SECS;
first_seen_ago -= months * MONTH_SECS;
months = t / MONTH_SECS;
t -= months * MONTH_SECS;
}
if (first_seen_ago > DAY_SECS)
if (t > DAY_SECS)
{
days = first_seen_ago / DAY_SECS;
first_seen_ago -= days * DAY_SECS;
days = t / DAY_SECS;
t -= days * DAY_SECS;
}
if (first_seen_ago > HOUR_SECS)
if (t > HOUR_SECS)
{
hours = first_seen_ago / HOUR_SECS;
first_seen_ago -= hours * HOUR_SECS;
hours = t / HOUR_SECS;
t -= hours * HOUR_SECS;
}
if (first_seen_ago > MIN_SECS)
if (t > MIN_SECS)
{
minutes = first_seen_ago / MIN_SECS;
first_seen_ago -= minutes * MIN_SECS;
minutes = t / MIN_SECS;
t -= minutes * MIN_SECS;
}
seconds = first_seen_ago;
seconds = t;
#undef MIN_SECS
#undef HOUR_SECS
@ -2342,6 +2246,10 @@ show_statistics (struct dbs *dbs, const char *fingerprint,
#undef MONTH_SECS
#undef YEAR_SECS
fp = es_fopenmem (0, "rw,samethread");
if (! fp)
log_fatal ("error creating memory stream\n");
if (years)
{
if (years > 1)
@ -2415,7 +2323,121 @@ show_statistics (struct dbs *dbs, const char *fingerprint,
es_fprintf (fp, _("%d second"), seconds);
}
es_fprintf (fp, _("."));
es_fputc (0, fp);
if (es_fclose_snatch (fp, (void **) &str, NULL))
log_fatal ("error snatching memory stream\n");
return str;
}
static void
show_statistics (struct dbs *dbs, const char *fingerprint,
const char *email, const char *user_id,
const char *sig_exclude)
{
struct db *db;
char *fingerprint_pp;
int rc;
strlist_t strlist = NULL;
char *err = NULL;
db = getdb (dbs, email, DB_EMAIL);
if (! db)
return;
fingerprint_pp = fingerprint_format (fingerprint);
rc = sqlite3_exec_printf
(db->db, strings_collect_cb, &strlist, &err,
"select count (*), strftime('%%s','now') - min (signatures.time),\n"
" strftime('%%s','now') - max (signatures.time)\n"
" from signatures\n"
" left join bindings on signatures.binding = bindings.oid\n"
" where fingerprint = %Q and email = %Q and sig_digest %s%s%s;",
fingerprint, email,
/* We want either: sig_digest != 'SIG_EXCLUDE' or sig_digest is
not NULL. */
sig_exclude ? "!= '" : "is not NULL",
sig_exclude ? sig_exclude : "",
sig_exclude ? "'" : "");
if (rc)
{
log_error (_("error reading from TOFU database"
" (getting statistics): %s\n"),
err);
sqlite3_free (err);
goto out;
}
if (! strlist)
log_info (_("Have never verified a message signed by key %s!\n"),
fingerprint_pp);
else
{
char *tail = NULL;
signed long messages;
signed long first_seen_ago;
assert (strlist_length (strlist) == 3);
errno = 0;
messages = strtol (strlist->d, &tail, 0);
if (errno || *tail != '\0')
/* Abort. */
{
log_debug ("%s:%d: Couldn't convert %s (messages) to an int: %s.\n",
__func__, __LINE__, strlist->d, strerror (errno));
messages = -1;
}
if (messages == 0 && *strlist->next->d == '\0')
/* min(NULL) => NULL => "". */
first_seen_ago = -1;
else
{
errno = 0;
first_seen_ago = strtol (strlist->next->d, &tail, 0);
if (errno || *tail != '\0')
/* Abort. */
{
log_debug ("%s:%d: Couldn't convert %s (first_seen) to an int: %s.\n",
__func__, __LINE__,
strlist->next->d, strerror (errno));
first_seen_ago = 0;
}
}
if (messages == -1 || first_seen_ago == 0)
log_info (_("Failed to collect signature statistics for \"%s\" (key %s)\n"),
user_id, fingerprint_pp);
else
{
enum tofu_policy policy = get_policy (dbs, fingerprint, email, NULL);
estream_t fp;
char *msg;
fp = es_fopenmem (0, "rw,samethread");
if (! fp)
log_fatal ("error creating memory stream\n");
if (messages == 0)
es_fprintf (fp,
_("Verified 0 messages signed by \"%s\""
" (key: %s, policy %s)."),
user_id, fingerprint_pp, tofu_policy_str (policy));
else
{
char *first_seen_ago_str = time_ago_str (first_seen_ago);
es_fprintf (fp,
_("Verified %ld messages signed by \"%s\""
" (key: %s, policy: %s) in the past %s."),
messages, user_id,
fingerprint_pp, tofu_policy_str (policy),
first_seen_ago_str);
xfree (first_seen_ago_str);
}
es_fputc (0, fp);
@ -2423,6 +2445,7 @@ show_statistics (struct dbs *dbs, const char *fingerprint,
log_fatal ("error snatching memory stream\n");
log_info ("%s\n", msg);
xfree (msg);
if (policy == TOFU_POLICY_AUTO && messages < 10)
{