Imported Upstream version 2.1.12
[platform/upstream/gpg2.git] / g10 / progress.c
1 /* progress.c - emit progress status lines
2  * Copyright (C) 2003, 2006 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <config.h>
21 #include <stdio.h>
22
23 #include "gpg.h"
24 #include "iobuf.h"
25 #include "filter.h"
26 #include "status.h"
27 #include "util.h"
28 #include "options.h"
29
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.
37
38    What we also do is to shortcut the progress handler by having this
39    function return NULL if progress information has not been
40    requested.
41 */
42 progress_filter_context_t *
43 new_progress_context (void)
44 {
45   progress_filter_context_t *pfx;
46
47   if (!opt.enable_progress_filter)
48     return NULL;
49
50   if (!is_status_enabled ())
51     return NULL;
52
53   pfx = xcalloc (1, sizeof *pfx);
54   pfx->refcount = 1;
55
56   return pfx;
57 }
58
59 /* Release a progress filter context.  Passing NULL is explicitly
60    allowed and a no-op.  */
61 void
62 release_progress_context (progress_filter_context_t *pfx)
63 {
64   if (!pfx)
65     return;
66   log_assert (pfx->refcount);
67   if ( --pfx->refcount )
68     return;
69   xfree (pfx->what);
70   xfree (pfx);
71 }
72
73
74 /****************
75  * The filter is used to report progress to the user.
76  */
77 static int
78 progress_filter (void *opaque, int control,
79                  IOBUF a, byte *buf, size_t *ret_len)
80 {
81   int rc = 0;
82   progress_filter_context_t *pfx = opaque;
83
84   if (control == IOBUFCTRL_INIT)
85     {
86       char buffer[50];
87
88       pfx->last = 0;
89       pfx->offset = 0;
90       pfx->last_time = make_timestamp ();
91
92       sprintf (buffer, "%.20s ? %lu %lu",
93                pfx->what? pfx->what : "?",
94                pfx->offset,
95                pfx->total);
96       write_status_text (STATUS_PROGRESS, buffer);
97     }
98   else if (control == IOBUFCTRL_UNDERFLOW)
99     {
100       u32 timestamp = make_timestamp ();
101       int len = iobuf_read (a, buf, *ret_len);
102
103       if (len >= 0)
104         {
105           pfx->offset += len;
106           *ret_len = len;
107         }
108       else
109         {
110           *ret_len = 0;
111           rc = -1;
112         }
113       if ((len == -1 && pfx->offset != pfx->last)
114           || timestamp - pfx->last_time > 0)
115         {
116           char buffer[50];
117
118           sprintf (buffer, "%.20s ? %lu %lu",
119                    pfx->what? pfx->what : "?",
120                    pfx->offset,
121                    pfx->total);
122           write_status_text (STATUS_PROGRESS, buffer);
123
124           pfx->last = pfx->offset;
125           pfx->last_time = timestamp;
126         }
127     }
128   else if (control == IOBUFCTRL_FREE)
129     {
130       release_progress_context (pfx);
131     }
132   else if (control == IOBUFCTRL_DESC)
133     mem2str (buf, "progress_filter", *ret_len);
134   return rc;
135 }
136
137 void
138 handle_progress (progress_filter_context_t *pfx, IOBUF inp, const char *name)
139 {
140   off_t filesize = 0;
141
142   if (!pfx)
143     return;
144
145   log_assert (opt.enable_progress_filter);
146   log_assert (is_status_enabled ());
147
148   if ( !iobuf_is_pipe_filename (name) && *name )
149     filesize = iobuf_get_filelength (inp, NULL);
150   else if (opt.set_filesize)
151     filesize = opt.set_filesize;
152
153   /* register the progress filter */
154   pfx->what = xstrdup (name ? name : "stdin");
155   pfx->total = filesize;
156   pfx->refcount++;
157   iobuf_push_filter (inp, progress_filter, pfx);
158 }