2003-04-15 15:46:13 +00:00
|
|
|
/* progress.c
|
|
|
|
* Copyright (C) 2003 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (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
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-05-31 08:39:18 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
* USA.
|
2003-04-15 15:46:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "iobuf.h"
|
|
|
|
#include "filter.h"
|
|
|
|
#include "status.h"
|
2003-04-23 21:18:39 +00:00
|
|
|
#include "util.h"
|
2003-04-15 15:46:13 +00:00
|
|
|
#include "options.h"
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* The filter is used to report progress to the user.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
progress_filter (void *opaque, int control,
|
|
|
|
IOBUF a, byte *buf, size_t *ret_len)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
progress_filter_context_t *pfx = opaque;
|
|
|
|
|
|
|
|
if (control == IOBUFCTRL_INIT)
|
|
|
|
{
|
|
|
|
char buffer[50];
|
|
|
|
|
|
|
|
pfx->last = 0;
|
|
|
|
pfx->offset = 0;
|
|
|
|
pfx->last_time = make_timestamp ();
|
|
|
|
|
2003-05-26 13:21:12 +00:00
|
|
|
sprintf (buffer, "%.20s ? %lu %lu",
|
|
|
|
pfx->what? pfx->what : "?",
|
|
|
|
pfx->offset,
|
2003-04-15 15:46:13 +00:00
|
|
|
pfx->total);
|
|
|
|
write_status_text (STATUS_PROGRESS, buffer);
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
char buffer[50];
|
|
|
|
|
2003-05-26 13:21:12 +00:00
|
|
|
sprintf (buffer, "%.20s ? %lu %lu",
|
|
|
|
pfx->what? pfx->what : "?",
|
|
|
|
pfx->offset,
|
2003-04-15 15:46:13 +00:00
|
|
|
pfx->total);
|
|
|
|
write_status_text (STATUS_PROGRESS, buffer);
|
|
|
|
|
|
|
|
pfx->last = pfx->offset;
|
|
|
|
pfx->last_time = timestamp;
|
|
|
|
}
|
|
|
|
}
|
2003-05-26 13:21:12 +00:00
|
|
|
else if (control == IOBUFCTRL_FREE)
|
|
|
|
{
|
|
|
|
/* Note, that we must always dealloc resources of a filter
|
|
|
|
within the filter handler and not anywhere else. (We set it
|
|
|
|
to NULL and check all uses just in case.) */
|
2005-07-27 18:10:56 +00:00
|
|
|
xfree (pfx->what);
|
2003-05-26 13:21:12 +00:00
|
|
|
pfx->what = NULL;
|
|
|
|
}
|
2003-04-15 15:46:13 +00:00
|
|
|
else if (control == IOBUFCTRL_DESC)
|
g10: Fix iobuf API of filter function for alignment.
* include/iobuf.h (struct iobuf_struct): Remove DESC.
* util/iobuf.c (iobuf_desc): New.
(print_chain, iobuf_close, iobuf_open, iobuf_fdopen, iobuf_sockopen)
(iobuf_create, iobuf_append, iobuf_openrw, iobuf_ioctl)
(iobuf_push_filter2, pop_filter, underflow): Use iobuf_desc.
(file_filter, sock_filter, block_filter): Fill the description.
* g10/armor.c, g10/cipher.c, g10/compress-bz2.c, g10/compress.c,
g10/encode.c, g10/encr-data.c, g10/mdfilter.c, g10/pipemode.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>
(backported from 2.1 commit 3f52c7da3940ec06572270d511000dc7fe9c27d2)
2016-01-26 15:38:27 +09:00
|
|
|
mem2str (buf, "progress_filter", *ret_len);
|
2003-04-15 15:46:13 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-04-23 21:18:39 +00:00
|
|
|
handle_progress (progress_filter_context_t *pfx, IOBUF inp, const char *name)
|
2003-04-15 15:46:13 +00:00
|
|
|
{
|
|
|
|
off_t filesize = 0;
|
|
|
|
|
|
|
|
if (!opt.enable_progress_filter)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!is_status_enabled ())
|
|
|
|
return;
|
|
|
|
|
2004-11-04 22:28:39 +00:00
|
|
|
if ( !iobuf_is_pipe_filename (name) && *name )
|
2005-10-05 16:58:50 +00:00
|
|
|
filesize = iobuf_get_filelength (inp, NULL);
|
2003-04-15 15:46:13 +00:00
|
|
|
else if (opt.set_filesize)
|
|
|
|
filesize = opt.set_filesize;
|
|
|
|
|
|
|
|
/* register the progress filter */
|
2005-07-27 18:10:56 +00:00
|
|
|
pfx->what = xstrdup (name ? name : "stdin");
|
2003-04-15 15:46:13 +00:00
|
|
|
pfx->total = filesize;
|
|
|
|
iobuf_push_filter (inp, progress_filter, pfx);
|
|
|
|
}
|