1 /* progress.c - emit progress status lines
2 * Copyright (C) 2003, 2006 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 3 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, see <http://www.gnu.org/licenses/>.
31 /* Create a new context for use with the progress filter. We need to
32 allocate such contexts on the heap because there is no guarantee
33 that at the end of a function the filter has already been popped
34 off. In general this will happen but with malformed packets it is
35 possible that a filter has not yet reached the end-of-stream when
36 the function has done all processing. Checking in each function
37 that end-of-stream has been reached would be to cumbersome.
39 What we also do is to shortcut the progress handler by having this
40 function return NULL if progress information has not been
43 progress_filter_context_t
*
44 new_progress_context (void)
46 progress_filter_context_t
*pfx
;
48 if (!opt
.enable_progress_filter
)
51 if (!is_status_enabled ())
54 pfx
= xcalloc (1, sizeof *pfx
);
60 /* Release a progress filter context. Passing NULL is explicitly
61 allowed and a no-op. */
63 release_progress_context (progress_filter_context_t
*pfx
)
67 assert (pfx
->refcount
);
68 if ( --pfx
->refcount
)
76 * The filter is used to report progress to the user.
79 progress_filter (void *opaque
, int control
,
80 IOBUF a
, byte
*buf
, size_t *ret_len
)
83 progress_filter_context_t
*pfx
= opaque
;
85 if (control
== IOBUFCTRL_INIT
)
91 pfx
->last_time
= make_timestamp ();
93 sprintf (buffer
, "%.20s ? %lu %lu",
94 pfx
->what
? pfx
->what
: "?",
97 write_status_text (STATUS_PROGRESS
, buffer
);
99 else if (control
== IOBUFCTRL_UNDERFLOW
)
101 u32 timestamp
= make_timestamp ();
102 int len
= iobuf_read (a
, buf
, *ret_len
);
114 if ((len
== -1 && pfx
->offset
!= pfx
->last
)
115 || timestamp
- pfx
->last_time
> 0)
119 sprintf (buffer
, "%.20s ? %lu %lu",
120 pfx
->what
? pfx
->what
: "?",
123 write_status_text (STATUS_PROGRESS
, buffer
);
125 pfx
->last
= pfx
->offset
;
126 pfx
->last_time
= timestamp
;
129 else if (control
== IOBUFCTRL_FREE
)
131 release_progress_context (pfx
);
133 else if (control
== IOBUFCTRL_DESC
)
134 *(char**)buf
= "progress_filter";
139 handle_progress (progress_filter_context_t
*pfx
, IOBUF inp
, const char *name
)
146 assert (opt
.enable_progress_filter
);
147 assert (is_status_enabled ());
149 if ( !iobuf_is_pipe_filename (name
) && *name
)
150 filesize
= iobuf_get_filelength (inp
, NULL
);
151 else if (opt
.set_filesize
)
152 filesize
= opt
.set_filesize
;
154 /* register the progress filter */
155 pfx
->what
= xstrdup (name
? name
: "stdin");
156 pfx
->total
= filesize
;
158 iobuf_push_filter (inp
, progress_filter
, pfx
);