2006-04-19 13:26:11 +02:00
|
|
|
/* progress.c - emit progress status lines
|
2006-12-06 11:16:50 +01:00
|
|
|
* Copyright (C) 2003, 2006 Free Software Foundation, Inc.
|
2003-06-05 09:14:21 +02:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 21:49:40 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-06-05 09:14:21 +02:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2003-06-05 09:14:21 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2003-06-18 21:56:13 +02:00
|
|
|
#include "gpg.h"
|
2017-03-07 12:21:23 +01:00
|
|
|
#include "../common/iobuf.h"
|
2003-06-05 09:14:21 +02:00
|
|
|
#include "filter.h"
|
2017-03-07 12:21:23 +01:00
|
|
|
#include "../common/status.h"
|
|
|
|
#include "../common/util.h"
|
2003-06-05 09:14:21 +02:00
|
|
|
#include "options.h"
|
|
|
|
|
2006-12-06 11:16:50 +01:00
|
|
|
/* Create a new context for use with the progress filter. We need to
|
|
|
|
allocate such contexts on the heap because there is no guarantee
|
|
|
|
that at the end of a function the filter has already been popped
|
|
|
|
off. In general this will happen but with malformed packets it is
|
|
|
|
possible that a filter has not yet reached the end-of-stream when
|
|
|
|
the function has done all processing. Checking in each function
|
|
|
|
that end-of-stream has been reached would be to cumbersome.
|
|
|
|
|
|
|
|
What we also do is to shortcut the progress handler by having this
|
|
|
|
function return NULL if progress information has not been
|
|
|
|
requested.
|
|
|
|
*/
|
|
|
|
progress_filter_context_t *
|
|
|
|
new_progress_context (void)
|
|
|
|
{
|
|
|
|
progress_filter_context_t *pfx;
|
|
|
|
|
|
|
|
if (!opt.enable_progress_filter)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!is_status_enabled ())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pfx = xcalloc (1, sizeof *pfx);
|
|
|
|
pfx->refcount = 1;
|
|
|
|
|
|
|
|
return pfx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release a progress filter context. Passing NULL is explicitly
|
|
|
|
allowed and a no-op. */
|
|
|
|
void
|
|
|
|
release_progress_context (progress_filter_context_t *pfx)
|
|
|
|
{
|
|
|
|
if (!pfx)
|
|
|
|
return;
|
2016-04-29 11:05:24 +02:00
|
|
|
log_assert (pfx->refcount);
|
2006-12-06 11:16:50 +01:00
|
|
|
if ( --pfx->refcount )
|
|
|
|
return;
|
|
|
|
xfree (pfx->what);
|
|
|
|
xfree (pfx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-27 11:28:22 +02:00
|
|
|
static void
|
2023-06-13 10:07:07 +02:00
|
|
|
write_status_progress (const char *what, uint64_t current, uint64_t total)
|
2016-05-27 11:28:22 +02:00
|
|
|
{
|
2016-08-09 16:22:24 +02:00
|
|
|
char buffer[60];
|
|
|
|
char units[] = "BKMGTPEZY?";
|
|
|
|
int unitidx = 0;
|
2016-05-27 11:28:22 +02:00
|
|
|
|
|
|
|
/* Although we use an unsigned long for the values, 32 bit
|
|
|
|
* applications using GPGME will use an "int" and thus are limited
|
|
|
|
* in the total size which can be represented. On Windows, where
|
|
|
|
* sizeof(int)==sizeof(long), this is even worse and will lead to an
|
|
|
|
* integer overflow for all files larger than 2 GiB. Although, the
|
|
|
|
* allowed value range of TOTAL and CURRENT is nowhere specified, we
|
|
|
|
* better protect applications from the need to handle negative
|
|
|
|
* values. The common usage pattern of the progress information is
|
|
|
|
* to display how many percent of the operation has been done and
|
|
|
|
* thus scaling CURRENT and TOTAL down before they get to large,
|
|
|
|
* should not have a noticeable effect except for rounding
|
2023-06-13 10:07:07 +02:00
|
|
|
* imprecision.
|
|
|
|
* Update 2023-06-13: We now use uint64_t but to keep the API stable
|
|
|
|
* we still do the scaling.
|
|
|
|
*/
|
2016-08-11 21:31:12 +02:00
|
|
|
|
|
|
|
if (!total && opt.input_size_hint)
|
|
|
|
total = opt.input_size_hint;
|
|
|
|
|
2016-05-27 11:28:22 +02:00
|
|
|
if (total)
|
|
|
|
{
|
|
|
|
if (current > total)
|
|
|
|
current = total;
|
|
|
|
|
|
|
|
while (total > 1024*1024)
|
|
|
|
{
|
|
|
|
total /= 1024;
|
|
|
|
current /= 1024;
|
2016-08-09 16:22:24 +02:00
|
|
|
unitidx++;
|
2016-05-27 11:28:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (current > 1024*1024)
|
|
|
|
{
|
|
|
|
current /= 1024;
|
2016-08-09 16:22:24 +02:00
|
|
|
unitidx++;
|
2016-05-27 11:28:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 16:22:24 +02:00
|
|
|
if (unitidx > 9)
|
|
|
|
unitidx = 9;
|
|
|
|
|
|
|
|
snprintf (buffer, sizeof buffer, "%.20s ? %lu %lu %c%s",
|
2023-06-13 10:07:07 +02:00
|
|
|
what? what : "?", (unsigned long)current, (unsigned long)total,
|
2016-08-09 16:22:24 +02:00
|
|
|
units[unitidx],
|
|
|
|
unitidx? "iB" : "");
|
2016-05-27 11:28:22 +02:00
|
|
|
write_status_text (STATUS_PROGRESS, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
/****************
|
|
|
|
* The filter is used to report progress to the user.
|
|
|
|
*/
|
2006-12-06 11:16:50 +01:00
|
|
|
static int
|
2003-06-05 09:14:21 +02:00
|
|
|
progress_filter (void *opaque, int control,
|
2006-04-19 13:26:11 +02:00
|
|
|
IOBUF a, byte *buf, size_t *ret_len)
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
progress_filter_context_t *pfx = opaque;
|
|
|
|
|
|
|
|
if (control == IOBUFCTRL_INIT)
|
|
|
|
{
|
|
|
|
pfx->last = 0;
|
|
|
|
pfx->offset = 0;
|
|
|
|
pfx->last_time = make_timestamp ();
|
|
|
|
|
2016-05-27 11:28:22 +02:00
|
|
|
write_status_progress (pfx->what, pfx->offset, pfx->total);
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
else if (control == IOBUFCTRL_UNDERFLOW)
|
|
|
|
{
|
|
|
|
u32 timestamp = make_timestamp ();
|
|
|
|
int len = iobuf_read (a, buf, *ret_len);
|
|
|
|
|
|
|
|
if (len >= 0)
|
|
|
|
{
|
|
|
|
pfx->offset += len;
|
|
|
|
*ret_len = len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*ret_len = 0;
|
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
if ((len == -1 && pfx->offset != pfx->last)
|
|
|
|
|| timestamp - pfx->last_time > 0)
|
|
|
|
{
|
2016-05-27 11:28:22 +02:00
|
|
|
write_status_progress (pfx->what, pfx->offset, pfx->total);
|
2003-06-05 09:14:21 +02:00
|
|
|
pfx->last = pfx->offset;
|
|
|
|
pfx->last_time = timestamp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (control == IOBUFCTRL_FREE)
|
|
|
|
{
|
2006-12-06 11:16:50 +01:00
|
|
|
release_progress_context (pfx);
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
else if (control == IOBUFCTRL_DESC)
|
common: Fix iobuf API of filter function for alignment.
* common/iobuf.h (IOBUFCTRL_DESC): Change the call semantics.
* common/iobuf.c (iobuf_desc): Add the second argument DESC.
(print_chain, iobuf_close, do_open, iobuf_sockopen, iobuf_ioctl)
(iobuf_push_filter2, pop_filter, iobuf_write_temp): Change calls
of iobuf_desc.
(file_filter, file_es_filter, sock_filter, block_filter): Fill the
description.
* common/t-iobuf.c (every_other_filter, double_filter): Likewise.
* g10/armor.c, g10/cipher.c, g10/compress-bz2.c, g10/compress.c,
g10/decrypt-data.c, g10/encrypt.c, g10/mdfilter.c, g10/progress.c,
g10/textfilter.c: Likewise.
--
Newer GCC warns against possible alignment difference of pointers.
This change can silence those warnings.
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2016-01-12 02:32:20 +01:00
|
|
|
mem2str (buf, "progress_filter", *ret_len);
|
2003-06-05 09:14:21 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-04-19 13:26:11 +02:00
|
|
|
handle_progress (progress_filter_context_t *pfx, IOBUF inp, const char *name)
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2023-06-13 10:07:07 +02:00
|
|
|
uint64_t filesize = 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2006-12-06 11:16:50 +01:00
|
|
|
if (!pfx)
|
2003-06-05 09:14:21 +02:00
|
|
|
return;
|
|
|
|
|
2016-04-29 11:05:24 +02:00
|
|
|
log_assert (opt.enable_progress_filter);
|
|
|
|
log_assert (is_status_enabled ());
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
if ( !iobuf_is_pipe_filename (name) && *name )
|
2023-06-13 10:07:07 +02:00
|
|
|
filesize = iobuf_get_filelength (inp);
|
2003-06-05 09:14:21 +02:00
|
|
|
else if (opt.set_filesize)
|
|
|
|
filesize = opt.set_filesize;
|
|
|
|
|
|
|
|
/* register the progress filter */
|
2003-06-18 21:56:13 +02:00
|
|
|
pfx->what = xstrdup (name ? name : "stdin");
|
2003-06-05 09:14:21 +02:00
|
|
|
pfx->total = filesize;
|
2006-12-06 11:16:50 +01:00
|
|
|
pfx->refcount++;
|
2003-06-05 09:14:21 +02:00
|
|
|
iobuf_push_filter (inp, progress_filter, pfx);
|
|
|
|
}
|