~ubuntu-branches/ubuntu/jaunty/gnupg2/jaunty

« back to all changes in this revision

Viewing changes to g10/progress.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Mueller
  • Date: 2005-03-29 10:30:32 UTC
  • Revision ID: james.westby@ubuntu.com-20050329103032-sj42n2ain3ipx310
Tags: upstream-1.9.15
ImportĀ upstreamĀ versionĀ 1.9.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* progress.c
 
2
 * Copyright (C) 2003 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 2 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, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
#include <stdio.h>
 
23
 
 
24
#include "gpg.h"
 
25
#include "iobuf.h"
 
26
#include "filter.h"
 
27
#include "status.h"
 
28
#include "util.h"
 
29
#include "options.h"
 
30
 
 
31
/****************
 
32
 * The filter is used to report progress to the user.
 
33
 */
 
34
int
 
35
progress_filter (void *opaque, int control,
 
36
                 iobuf_t a, byte *buf, size_t *ret_len)
 
37
{
 
38
  int rc = 0;
 
39
  progress_filter_context_t *pfx = opaque;
 
40
 
 
41
  if (control == IOBUFCTRL_INIT)
 
42
    {
 
43
      char buffer[50];
 
44
 
 
45
      pfx->last = 0;
 
46
      pfx->offset = 0;
 
47
      pfx->last_time = make_timestamp ();
 
48
 
 
49
      sprintf (buffer, "%.20s ? %lu %lu",
 
50
               pfx->what? pfx->what : "?",
 
51
               pfx->offset,
 
52
               pfx->total);
 
53
      write_status_text (STATUS_PROGRESS, buffer);
 
54
    }
 
55
  else if (control == IOBUFCTRL_UNDERFLOW)
 
56
    {
 
57
      u32 timestamp = make_timestamp ();
 
58
      int len = iobuf_read (a, buf, *ret_len);
 
59
 
 
60
      if (len >= 0)
 
61
        {
 
62
          pfx->offset += len;
 
63
          *ret_len = len;
 
64
        }
 
65
      else
 
66
        {
 
67
          *ret_len = 0;
 
68
          rc = -1;
 
69
        }
 
70
      if ((len == -1 && pfx->offset != pfx->last)
 
71
          || timestamp - pfx->last_time > 0)
 
72
        {
 
73
          char buffer[50];
 
74
          
 
75
          sprintf (buffer, "%.20s ? %lu %lu",
 
76
                   pfx->what? pfx->what : "?", 
 
77
                   pfx->offset,
 
78
                   pfx->total);
 
79
          write_status_text (STATUS_PROGRESS, buffer);
 
80
 
 
81
          pfx->last = pfx->offset;
 
82
          pfx->last_time = timestamp;
 
83
        }
 
84
    }
 
85
  else if (control == IOBUFCTRL_FREE)
 
86
    {
 
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;
 
92
    }
 
93
  else if (control == IOBUFCTRL_DESC)
 
94
    *(char**)buf = "progress_filter";
 
95
  return rc;
 
96
}
 
97
 
 
98
void
 
99
handle_progress (progress_filter_context_t *pfx, iobuf_t inp, const char *name)
 
100
{
 
101
  off_t filesize = 0;
 
102
 
 
103
  if (!opt.enable_progress_filter)
 
104
    return;
 
105
 
 
106
  if (!is_status_enabled ())
 
107
    return;
 
108
 
 
109
  if (name && *name && !(*name == '-' && !name[1]))
 
110
    filesize = iobuf_get_filelength (inp);
 
111
  else if (opt.set_filesize)
 
112
    filesize = opt.set_filesize;
 
113
 
 
114
  /* register the progress filter */
 
115
  pfx->what = xstrdup (name ? name : "stdin");
 
116
  pfx->total = filesize;
 
117
  iobuf_push_filter (inp, progress_filter, pfx);
 
118
}