* gpg.texi (GPG Configuration Options): Make http_proxy option
[gnupg.git] / g10 / progress.c
blob8ccc4536f65c219b6be2a6dc9ce48722ecab5b71
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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 * USA.
22 #include <config.h>
23 #include <stdio.h>
24 #include <assert.h>
26 #include "gpg.h"
27 #include "iobuf.h"
28 #include "filter.h"
29 #include "status.h"
30 #include "util.h"
31 #include "options.h"
33 /* Create a new context for use with the progress filter. We need to
34 allocate such contexts on the heap because there is no guarantee
35 that at the end of a function the filter has already been popped
36 off. In general this will happen but with malformed packets it is
37 possible that a filter has not yet reached the end-of-stream when
38 the function has done all processing. Checking in each function
39 that end-of-stream has been reached would be to cumbersome.
41 What we also do is to shortcut the progress handler by having this
42 function return NULL if progress information has not been
43 requested.
45 progress_filter_context_t *
46 new_progress_context (void)
48 progress_filter_context_t *pfx;
50 if (!opt.enable_progress_filter)
51 return NULL;
53 if (!is_status_enabled ())
54 return NULL;
56 pfx = xcalloc (1, sizeof *pfx);
57 pfx->refcount = 1;
59 return pfx;
62 /* Release a progress filter context. Passing NULL is explicitly
63 allowed and a no-op. */
64 void
65 release_progress_context (progress_filter_context_t *pfx)
67 if (!pfx)
68 return;
69 assert (pfx->refcount);
70 if ( --pfx->refcount )
71 return;
72 xfree (pfx->what);
73 xfree (pfx);
77 /****************
78 * The filter is used to report progress to the user.
80 static int
81 progress_filter (void *opaque, int control,
82 IOBUF a, byte *buf, size_t *ret_len)
84 int rc = 0;
85 progress_filter_context_t *pfx = opaque;
87 if (control == IOBUFCTRL_INIT)
89 char buffer[50];
91 pfx->last = 0;
92 pfx->offset = 0;
93 pfx->last_time = make_timestamp ();
95 sprintf (buffer, "%.20s ? %lu %lu",
96 pfx->what? pfx->what : "?",
97 pfx->offset,
98 pfx->total);
99 write_status_text (STATUS_PROGRESS, buffer);
101 else if (control == IOBUFCTRL_UNDERFLOW)
103 u32 timestamp = make_timestamp ();
104 int len = iobuf_read (a, buf, *ret_len);
106 if (len >= 0)
108 pfx->offset += len;
109 *ret_len = len;
111 else
113 *ret_len = 0;
114 rc = -1;
116 if ((len == -1 && pfx->offset != pfx->last)
117 || timestamp - pfx->last_time > 0)
119 char buffer[50];
121 sprintf (buffer, "%.20s ? %lu %lu",
122 pfx->what? pfx->what : "?",
123 pfx->offset,
124 pfx->total);
125 write_status_text (STATUS_PROGRESS, buffer);
127 pfx->last = pfx->offset;
128 pfx->last_time = timestamp;
131 else if (control == IOBUFCTRL_FREE)
133 release_progress_context (pfx);
135 else if (control == IOBUFCTRL_DESC)
136 *(char**)buf = "progress_filter";
137 return rc;
140 void
141 handle_progress (progress_filter_context_t *pfx, IOBUF inp, const char *name)
143 off_t filesize = 0;
145 if (!pfx)
146 return;
148 assert (opt.enable_progress_filter);
149 assert (is_status_enabled ());
151 if ( !iobuf_is_pipe_filename (name) && *name )
152 filesize = iobuf_get_filelength (inp, NULL);
153 else if (opt.set_filesize)
154 filesize = opt.set_filesize;
156 /* register the progress filter */
157 pfx->what = xstrdup (name ? name : "stdin");
158 pfx->total = filesize;
159 pfx->refcount++;
160 iobuf_push_filter (inp, progress_filter, pfx);