Coverage Report

Created: 2026-01-09 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnupg/g10/progress.c
Line
Count
Source
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 <https://www.gnu.org/licenses/>.
18
 */
19
20
#include <config.h>
21
#include <stdio.h>
22
23
#include "gpg.h"
24
#include "../common/iobuf.h"
25
#include "filter.h"
26
#include "../common/status.h"
27
#include "../common/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
313k
{
45
313k
  progress_filter_context_t *pfx;
46
47
313k
  if (!opt.enable_progress_filter)
48
313k
    return NULL;
49
50
0
  if (!is_status_enabled ())
51
0
    return NULL;
52
53
0
  pfx = xcalloc (1, sizeof *pfx);
54
0
  pfx->refcount = 1;
55
56
0
  return pfx;
57
0
}
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
313k
{
64
313k
  if (!pfx)
65
313k
    return;
66
313k
  log_assert (pfx->refcount);
67
0
  if ( --pfx->refcount )
68
0
    return;
69
0
  xfree (pfx->what);
70
0
  xfree (pfx);
71
0
}
72
73
74
static void
75
write_status_progress (const char *what, uint64_t current, uint64_t total)
76
0
{
77
0
  char buffer[60];
78
0
  char units[] = "BKMGTPEZY?";
79
0
  int unitidx = 0;
80
81
  /* Although we use an unsigned long for the values, 32 bit
82
   * applications using GPGME will use an "int" and thus are limited
83
   * in the total size which can be represented.  On Windows, where
84
   * sizeof(int)==sizeof(long), this is even worse and will lead to an
85
   * integer overflow for all files larger than 2 GiB.  Although, the
86
   * allowed value range of TOTAL and CURRENT is nowhere specified, we
87
   * better protect applications from the need to handle negative
88
   * values.  The common usage pattern of the progress information is
89
   * to display how many percent of the operation has been done and
90
   * thus scaling CURRENT and TOTAL down before they get to large,
91
   * should not have a noticeable effect except for rounding
92
   * imprecision.
93
   * Update 2023-06-13: We now use uint64_t but to keep the API stable
94
   * we still do the scaling.
95
   */
96
97
0
  if (!total && opt.input_size_hint)
98
0
    total = opt.input_size_hint;
99
100
0
  if (total)
101
0
    {
102
0
      if (current > total)
103
0
        current = total;
104
105
0
      while (total > 1024*1024)
106
0
        {
107
0
          total /= 1024;
108
0
          current /= 1024;
109
0
          unitidx++;
110
0
        }
111
0
    }
112
0
  else
113
0
    {
114
0
      while (current > 1024*1024)
115
0
        {
116
0
          current /= 1024;
117
0
          unitidx++;
118
0
        }
119
0
    }
120
121
0
  if (unitidx > 9)
122
0
    unitidx = 9;
123
124
0
  snprintf (buffer, sizeof buffer, "%.20s ? %lu %lu %c%s",
125
0
            what? what : "?", (unsigned long)current, (unsigned long)total,
126
0
            units[unitidx],
127
0
            unitidx? "iB" : "");
128
0
  write_status_text (STATUS_PROGRESS, buffer);
129
0
}
130
131
132
/****************
133
 * The filter is used to report progress to the user.
134
 */
135
static int
136
progress_filter (void *opaque, int control,
137
     IOBUF a, byte *buf, size_t *ret_len)
138
0
{
139
0
  int rc = 0;
140
0
  progress_filter_context_t *pfx = opaque;
141
142
0
  if (control == IOBUFCTRL_INIT)
143
0
    {
144
0
      pfx->last = 0;
145
0
      pfx->offset = 0;
146
0
      pfx->last_time = make_timestamp ();
147
148
0
      write_status_progress (pfx->what, pfx->offset, pfx->total);
149
0
    }
150
0
  else if (control == IOBUFCTRL_UNDERFLOW)
151
0
    {
152
0
      u32 timestamp = make_timestamp ();
153
0
      int len = iobuf_read (a, buf, *ret_len);
154
155
0
      if (len >= 0)
156
0
  {
157
0
    pfx->offset += len;
158
0
    *ret_len = len;
159
0
  }
160
0
      else
161
0
  {
162
0
    *ret_len = 0;
163
0
    rc = -1;
164
0
  }
165
0
      if ((len == -1 && pfx->offset != pfx->last)
166
0
    || timestamp - pfx->last_time > 0)
167
0
  {
168
0
          write_status_progress (pfx->what, pfx->offset, pfx->total);
169
0
    pfx->last = pfx->offset;
170
0
    pfx->last_time = timestamp;
171
0
  }
172
0
    }
173
0
  else if (control == IOBUFCTRL_FREE)
174
0
    {
175
0
      release_progress_context (pfx);
176
0
    }
177
0
  else if (control == IOBUFCTRL_DESC)
178
0
    mem2str (buf, "progress_filter", *ret_len);
179
0
  return rc;
180
0
}
181
182
void
183
handle_progress (progress_filter_context_t *pfx, IOBUF inp, const char *name)
184
16.8k
{
185
16.8k
  uint64_t filesize = 0;
186
187
16.8k
  if (!pfx)
188
16.8k
    return;
189
190
16.8k
  log_assert (opt.enable_progress_filter);
191
0
  log_assert (is_status_enabled ());
192
193
0
  if ( !iobuf_is_pipe_filename (name) && *name )
194
0
    filesize = iobuf_get_filelength (inp);
195
0
  else if (opt.set_filesize)
196
0
    filesize = opt.set_filesize;
197
198
  /* register the progress filter */
199
0
  pfx->what = xstrdup (name ? name : "stdin");
200
0
  pfx->total = filesize;
201
0
  pfx->refcount++;
202
0
  iobuf_push_filter (inp, progress_filter, pfx);
203
0
}