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 <https://www.gnu.org/licenses/>.
30 /* Create a new context for use with the progress filter. We need to
31 allocate such contexts on the heap because there is no guarantee
32 that at the end of a function the filter has already been popped
33 off. In general this will happen but with malformed packets it is
34 possible that a filter has not yet reached the end-of-stream when
35 the function has done all processing. Checking in each function
36 that end-of-stream has been reached would be to cumbersome.
38 What we also do is to shortcut the progress handler by having this
39 function return NULL if progress information has not been
42 progress_filter_context_t *
43 new_progress_context (void)
45 progress_filter_context_t *pfx;
47 if (!opt.enable_progress_filter)
50 if (!is_status_enabled ())
53 pfx = xcalloc (1, sizeof *pfx);
59 /* Release a progress filter context. Passing NULL is explicitly
60 allowed and a no-op. */
62 release_progress_context (progress_filter_context_t *pfx)
66 log_assert (pfx->refcount);
67 if ( --pfx->refcount )
75 write_status_progress (const char *what,
76 unsigned long current, unsigned long total_arg)
79 char units[] = "BKMGTPEZY?";
81 uint64_t total = total_arg;
83 /* Although we use an unsigned long for the values, 32 bit
84 * applications using GPGME will use an "int" and thus are limited
85 * in the total size which can be represented. On Windows, where
86 * sizeof(int)==sizeof(long), this is even worse and will lead to an
87 * integer overflow for all files larger than 2 GiB. Although, the
88 * allowed value range of TOTAL and CURRENT is nowhere specified, we
89 * better protect applications from the need to handle negative
90 * values. The common usage pattern of the progress information is
91 * to display how many percent of the operation has been done and
92 * thus scaling CURRENT and TOTAL down before they get to large,
93 * should not have a noticeable effect except for rounding
96 if (!total && opt.input_size_hint)
97 total = opt.input_size_hint;
104 while (total > 1024*1024)
113 while (current > 1024*1024)
123 snprintf (buffer, sizeof buffer, "%.20s ? %lu %lu %c%s",
124 what? what : "?", current, (unsigned long)total,
127 write_status_text (STATUS_PROGRESS, buffer);
132 * The filter is used to report progress to the user.
135 progress_filter (void *opaque, int control,
136 IOBUF a, byte *buf, size_t *ret_len)
139 progress_filter_context_t *pfx = opaque;
141 if (control == IOBUFCTRL_INIT)
145 pfx->last_time = make_timestamp ();
147 write_status_progress (pfx->what, pfx->offset, pfx->total);
149 else if (control == IOBUFCTRL_UNDERFLOW)
151 u32 timestamp = make_timestamp ();
152 int len = iobuf_read (a, buf, *ret_len);
164 if ((len == -1 && pfx->offset != pfx->last)
165 || timestamp - pfx->last_time > 0)
167 write_status_progress (pfx->what, pfx->offset, pfx->total);
168 pfx->last = pfx->offset;
169 pfx->last_time = timestamp;
172 else if (control == IOBUFCTRL_FREE)
174 release_progress_context (pfx);
176 else if (control == IOBUFCTRL_DESC)
177 mem2str (buf, "progress_filter", *ret_len);
182 handle_progress (progress_filter_context_t *pfx, IOBUF inp, const char *name)
189 log_assert (opt.enable_progress_filter);
190 log_assert (is_status_enabled ());
192 if ( !iobuf_is_pipe_filename (name) && *name )
193 filesize = iobuf_get_filelength (inp, NULL);
194 else if (opt.set_filesize)
195 filesize = opt.set_filesize;
197 /* register the progress filter */
198 pfx->what = xstrdup (name ? name : "stdin");
199 pfx->total = filesize;
201 iobuf_push_filter (inp, progress_filter, pfx);