agent/
[gnupg.git] / g10 / progress.c
blobd0aa926ed95104b5615bd632fde9c8133bd7de28
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/>.
20 #include <config.h>
21 #include <stdio.h>
22 #include <assert.h>
24 #include "gpg.h"
25 #include "iobuf.h"
26 #include "filter.h"
27 #include "status.h"
28 #include "util.h"
29 #include "options.h"
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
41 requested.
43 progress_filter_context_t *
44 new_progress_context (void)
46 progress_filter_context_t *pfx;
48 if (!opt.enable_progress_filter)
49 return NULL;
51 if (!is_status_enabled ())
52 return NULL;
54 pfx = xcalloc (1, sizeof *pfx);
55 pfx->refcount = 1;
57 return pfx;
60 /* Release a progress filter context. Passing NULL is explicitly
61 allowed and a no-op. */
62 void
63 release_progress_context (progress_filter_context_t *pfx)
65 if (!pfx)
66 return;
67 assert (pfx->refcount);
68 if ( --pfx->refcount )
69 return;
70 xfree (pfx->what);
71 xfree (pfx);
75 /****************
76 * The filter is used to report progress to the user.
78 static int
79 progress_filter (void *opaque, int control,
80 IOBUF a, byte *buf, size_t *ret_len)
82 int rc = 0;
83 progress_filter_context_t *pfx = opaque;
85 if (control == IOBUFCTRL_INIT)
87 char buffer[50];
89 pfx->last = 0;
90 pfx->offset = 0;
91 pfx->last_time = make_timestamp ();
93 sprintf (buffer, "%.20s ? %lu %lu",
94 pfx->what? pfx->what : "?",
95 pfx->offset,
96 pfx->total);
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);
104 if (len >= 0)
106 pfx->offset += len;
107 *ret_len = len;
109 else
111 *ret_len = 0;
112 rc = -1;
114 if ((len == -1 && pfx->offset != pfx->last)
115 || timestamp - pfx->last_time > 0)
117 char buffer[50];
119 sprintf (buffer, "%.20s ? %lu %lu",
120 pfx->what? pfx->what : "?",
121 pfx->offset,
122 pfx->total);
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";
135 return rc;
138 void
139 handle_progress (progress_filter_context_t *pfx, IOBUF inp, const char *name)
141 off_t filesize = 0;
143 if (!pfx)
144 return;
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;
157 pfx->refcount++;
158 iobuf_push_filter (inp, progress_filter, pfx);