2005-04-15 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / progress.c
blob9d98050656edd9a04f676acba3bd156d23f935e5
1 /* progress.c
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
21 #include <config.h>
22 #include <stdio.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 /****************
32 * The filter is used to report progress to the user.
34 int
35 progress_filter (void *opaque, int control,
36 iobuf_t a, byte *buf, size_t *ret_len)
38 int rc = 0;
39 progress_filter_context_t *pfx = opaque;
41 if (control == IOBUFCTRL_INIT)
43 char buffer[50];
45 pfx->last = 0;
46 pfx->offset = 0;
47 pfx->last_time = make_timestamp ();
49 sprintf (buffer, "%.20s ? %lu %lu",
50 pfx->what? pfx->what : "?",
51 pfx->offset,
52 pfx->total);
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);
60 if (len >= 0)
62 pfx->offset += len;
63 *ret_len = len;
65 else
67 *ret_len = 0;
68 rc = -1;
70 if ((len == -1 && pfx->offset != pfx->last)
71 || timestamp - pfx->last_time > 0)
73 char buffer[50];
75 sprintf (buffer, "%.20s ? %lu %lu",
76 pfx->what? pfx->what : "?",
77 pfx->offset,
78 pfx->total);
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.) */
90 xfree (pfx->what);
91 pfx->what = NULL;
93 else if (control == IOBUFCTRL_DESC)
94 *(char**)buf = "progress_filter";
95 return rc;
98 void
99 handle_progress (progress_filter_context_t *pfx, iobuf_t inp, const char *name)
101 off_t filesize = 0;
103 if (!opt.enable_progress_filter)
104 return;
106 if (!is_status_enabled ())
107 return;
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);