2 * Copyright (C) 2003 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
32 * The filter is used to report progress to the user.
35 progress_filter (void *opaque
, int control
,
36 iobuf_t a
, byte
*buf
, size_t *ret_len
)
39 progress_filter_context_t
*pfx
= opaque
;
41 if (control
== IOBUFCTRL_INIT
)
47 pfx
->last_time
= make_timestamp ();
49 sprintf (buffer
, "%.20s ? %lu %lu",
50 pfx
->what
? pfx
->what
: "?",
53 write_status_text (STATUS_PROGRESS
, buffer
);
55 else if (control
== IOBUFCTRL_UNDERFLOW
)
57 u32 timestamp
= make_timestamp ();
58 int len
= iobuf_read (a
, buf
, *ret_len
);
70 if ((len
== -1 && pfx
->offset
!= pfx
->last
)
71 || timestamp
- pfx
->last_time
> 0)
75 sprintf (buffer
, "%.20s ? %lu %lu",
76 pfx
->what
? pfx
->what
: "?",
79 write_status_text (STATUS_PROGRESS
, buffer
);
81 pfx
->last
= pfx
->offset
;
82 pfx
->last_time
= timestamp
;
85 else if (control
== IOBUFCTRL_FREE
)
87 /* Note, that we must always dealloc resources of a filter
88 within the filter handler and not anywhere else. (We set it
89 to NULL and check all uses just in case.) */
93 else if (control
== IOBUFCTRL_DESC
)
94 *(char**)buf
= "progress_filter";
99 handle_progress (progress_filter_context_t
*pfx
, iobuf_t inp
, const char *name
)
103 if (!opt
.enable_progress_filter
)
106 if (!is_status_enabled ())
109 if (name
&& *name
&& !(*name
== '-' && !name
[1]))
110 filesize
= iobuf_get_filelength (inp
);
111 else if (opt
.set_filesize
)
112 filesize
= opt
.set_filesize
;
114 /* register the progress filter */
115 pfx
->what
= xstrdup (name
? name
: "stdin");
116 pfx
->total
= filesize
;
117 iobuf_push_filter (inp
, progress_filter
, pfx
);