1 |
werner |
36 |
/* wptFileCBS.cpp |
2 |
|
|
* Copyright (C) 2005 Timo Schulz |
3 |
|
|
* Copyright (C) 2005 g10 Code GmbH |
4 |
|
|
* |
5 |
|
|
* This file is part of WinPT. |
6 |
|
|
* |
7 |
|
|
* WinPT is free software; you can redistribute it and/or |
8 |
|
|
* modify it under the terms of the GNU General Public License |
9 |
|
|
* as published by the Free Software Foundation; either version 2 |
10 |
|
|
* of the License, or (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* WinPT is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
|
|
* General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License |
18 |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
|
|
*/ |
21 |
|
|
#ifdef HAVE_CONFIG_H |
22 |
|
|
#include <config.h> |
23 |
|
|
#endif |
24 |
|
|
|
25 |
|
|
#include <windows.h> |
26 |
|
|
#include <windows.h> |
27 |
|
|
#include <stdio.h> |
28 |
|
|
#include <malloc.h> |
29 |
|
|
#include <errno.h> |
30 |
|
|
#include <stdlib.h> |
31 |
|
|
#include <sys/stat.h> |
32 |
|
|
|
33 |
|
|
#include "gpgme.h" |
34 |
|
|
#include "wptListView.h" |
35 |
|
|
#include "wptGPG.h" |
36 |
|
|
#include "wptFileManager.h" |
37 |
|
|
#include "wptErrors.h" |
38 |
|
|
#include "wptTypes.h" |
39 |
|
|
|
40 |
|
|
void progress_callback (void *opaque, const char *what, int type, int off, int max); |
41 |
|
|
|
42 |
|
|
|
43 |
|
|
/* Predefined read callback. */ |
44 |
|
|
static long |
45 |
|
|
read_cb (void *handle, void *buffer, size_t size) |
46 |
|
|
{ |
47 |
|
|
file_data_t cb = (file_data_t)handle; |
48 |
|
|
struct progress_filter_s *pfx = (struct progress_filter_s *)cb->cb_value; |
49 |
|
|
int n = fread (buffer, 1, size, cb->handle); |
50 |
|
|
|
51 |
|
|
/* XXX: there is a sync problem with the progress dialog. */ |
52 |
|
|
if (pfx) |
53 |
|
|
progress_callback (pfx, NULL, 0, cb->off, cb->size); |
54 |
|
|
cb->off += n; |
55 |
|
|
return n; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
|
59 |
|
|
/* Predefined write callback. */ |
60 |
|
|
static long |
61 |
|
|
write_cb (void *handle, const void *buffer, size_t size) |
62 |
|
|
{ |
63 |
|
|
file_data_t cb = (file_data_t)handle; |
64 |
|
|
int n = fwrite (buffer, 1, size, cb->handle); |
65 |
|
|
|
66 |
|
|
return n; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
|
70 |
|
|
/* Create a new data -> file association with a static callback. |
71 |
|
|
@fname is the file which is associated to the object. |
72 |
|
|
@r_cb is the context which holds all information. |
73 |
|
|
@for_read is 1 if the file is opened for read only. |
74 |
|
|
Return value: 0 on success. */ |
75 |
|
|
gpgme_error_t |
76 |
|
|
gpg_file_data_new (const char *fname, int for_read, file_data_t *r_cb) |
77 |
|
|
|
78 |
|
|
{ |
79 |
|
|
gpgme_error_t err; |
80 |
|
|
file_data_t cb; |
81 |
|
|
FILE *f; |
82 |
|
|
|
83 |
|
|
f = fopen (fname, for_read?"rb" : "wb"); |
84 |
|
|
if (!f) |
85 |
|
|
return gpgme_err_code_from_errno (errno); |
86 |
|
|
|
87 |
|
|
cb = (file_data_t)calloc (1, sizeof *cb); |
88 |
|
|
if (!cb) |
89 |
|
|
abort (); |
90 |
|
|
cb->cbs.read = read_cb; |
91 |
|
|
cb->cbs.write = write_cb; |
92 |
|
|
cb->handle = f; |
93 |
|
|
if (for_read) { |
94 |
|
|
struct stat st; |
95 |
|
|
if (fstat (fileno (f), &st)) |
96 |
|
|
BUG (NULL); |
97 |
|
|
cb->size = st.st_size; |
98 |
|
|
cb->off = 0; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
err = gpgme_data_new_from_cbs (&cb->dat, &cb->cbs, cb); |
102 |
|
|
if (err) { |
103 |
|
|
fclose (f); |
104 |
|
|
free (cb); |
105 |
|
|
return err; |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
*r_cb = cb; |
109 |
|
|
return err; |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
|
113 |
|
|
/* Activate the progress callback for the given object @ctx. */ |
114 |
|
|
void |
115 |
|
|
gpg_file_data_set_cb (file_data_t ctx, struct progress_filter_s *pfx) |
116 |
|
|
{ |
117 |
|
|
ctx->cb_value = (void*)pfx; |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
|
121 |
|
|
/* Release the context in @cb. Close all internal handles if possible. */ |
122 |
|
|
void |
123 |
|
|
gpg_file_data_release (file_data_t cb) |
124 |
|
|
{ |
125 |
|
|
if (!cb) |
126 |
|
|
return; |
127 |
|
|
if (cb->handle) { |
128 |
|
|
FILE *f = (FILE *)cb->handle; |
129 |
|
|
fclose (f); |
130 |
|
|
} |
131 |
|
|
if (cb->dat) |
132 |
|
|
gpgme_data_release (cb->dat); |
133 |
|
|
free (cb); |
134 |
|
|
} |