1 |
twoaday |
119 |
/* wptFileCBS.cpp - Customized I/O callbacks for GPGME |
2 |
twoaday |
226 |
* Copyright (C) 2005, 2006 Timo Schulz |
3 |
werner |
36 |
* 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 |
twoaday |
197 |
|
22 |
werner |
36 |
#ifdef HAVE_CONFIG_H |
23 |
|
|
#include <config.h> |
24 |
|
|
#endif |
25 |
|
|
|
26 |
|
|
#include <windows.h> |
27 |
|
|
#include <stdio.h> |
28 |
twoaday |
119 |
#include <errno.h> |
29 |
werner |
36 |
|
30 |
|
|
#include "wptListView.h" |
31 |
|
|
#include "wptGPG.h" |
32 |
|
|
#include "wptFileManager.h" |
33 |
|
|
#include "wptErrors.h" |
34 |
|
|
#include "wptTypes.h" |
35 |
twoaday |
219 |
#include "wptW32API.h" |
36 |
werner |
36 |
|
37 |
twoaday |
119 |
void progress_callback (void *opaque, const char *what, int type, |
38 |
|
|
int off, int max); |
39 |
werner |
36 |
|
40 |
|
|
|
41 |
|
|
/* Predefined read callback. */ |
42 |
|
|
static long |
43 |
|
|
read_cb (void *handle, void *buffer, size_t size) |
44 |
|
|
{ |
45 |
|
|
file_data_t cb = (file_data_t)handle; |
46 |
|
|
struct progress_filter_s *pfx = (struct progress_filter_s *)cb->cb_value; |
47 |
twoaday |
119 |
DWORD nread = 0; |
48 |
werner |
36 |
|
49 |
twoaday |
197 |
if (cb->error) |
50 |
|
|
return -1; |
51 |
twoaday |
119 |
|
52 |
twoaday |
219 |
if (!ReadFile (cb->handle, buffer, size, &nread, NULL)) { |
53 |
twoaday |
197 |
cb->error = (int)GetLastError (); |
54 |
twoaday |
219 |
log_debug ("read_cb: ReadFile() error=%d\r\n", cb->error); |
55 |
twoaday |
197 |
return -1; |
56 |
|
|
} |
57 |
|
|
|
58 |
werner |
36 |
/* XXX: there is a sync problem with the progress dialog. */ |
59 |
|
|
if (pfx) |
60 |
|
|
progress_callback (pfx, NULL, 0, cb->off, cb->size); |
61 |
twoaday |
119 |
cb->off += nread; |
62 |
|
|
return (long)nread; |
63 |
werner |
36 |
} |
64 |
|
|
|
65 |
|
|
|
66 |
|
|
/* Predefined write callback. */ |
67 |
|
|
static long |
68 |
|
|
write_cb (void *handle, const void *buffer, size_t size) |
69 |
|
|
{ |
70 |
|
|
file_data_t cb = (file_data_t)handle; |
71 |
twoaday |
119 |
DWORD nwritten; |
72 |
werner |
36 |
|
73 |
twoaday |
197 |
if (cb->error) |
74 |
|
|
return -1; |
75 |
twoaday |
119 |
|
76 |
|
|
if (!cb->handle) { |
77 |
|
|
SECURITY_ATTRIBUTES sec_attr; |
78 |
|
|
|
79 |
|
|
memset (&sec_attr, 0, sizeof (sec_attr)); |
80 |
|
|
sec_attr.bInheritHandle = FALSE; |
81 |
|
|
sec_attr.nLength = sizeof (sec_attr); |
82 |
|
|
cb->handle = CreateFile (cb->name, GENERIC_WRITE, FILE_SHARE_WRITE, |
83 |
|
|
&sec_attr, CREATE_ALWAYS, 0, NULL); |
84 |
twoaday |
197 |
if (cb->handle == INVALID_HANDLE_VALUE) { |
85 |
|
|
cb->error = (int)GetLastError (); |
86 |
twoaday |
219 |
log_debug ("write_cb: CreateFile() error=%d\r\n", cb->error); |
87 |
twoaday |
197 |
return -1; |
88 |
|
|
} |
89 |
twoaday |
119 |
} |
90 |
|
|
|
91 |
twoaday |
197 |
if (!WriteFile (cb->handle, buffer, size, &nwritten, NULL)) { |
92 |
|
|
cb->error = (int)GetLastError (); |
93 |
twoaday |
219 |
log_debug ("write_cb: WriteFile() error=%d\r\n", cb->error); |
94 |
twoaday |
197 |
return -1; |
95 |
|
|
} |
96 |
twoaday |
119 |
return (long)nwritten; |
97 |
werner |
36 |
} |
98 |
|
|
|
99 |
|
|
|
100 |
|
|
/* Create a new data -> file association with a static callback. |
101 |
|
|
@fname is the file which is associated to the object. |
102 |
|
|
@r_cb is the context which holds all information. |
103 |
|
|
@for_read is 1 if the file is opened for read only. |
104 |
|
|
Return value: 0 on success. */ |
105 |
|
|
gpgme_error_t |
106 |
twoaday |
119 |
gpg_file_data_new (const char *fname, int flags, file_data_t *r_cb) |
107 |
werner |
36 |
|
108 |
|
|
{ |
109 |
|
|
gpgme_error_t err; |
110 |
|
|
file_data_t cb; |
111 |
twoaday |
119 |
HANDLE fd = NULL; |
112 |
|
|
SECURITY_ATTRIBUTES sec_attr; |
113 |
werner |
36 |
|
114 |
twoaday |
119 |
memset (&sec_attr, 0, sizeof (sec_attr)); |
115 |
|
|
sec_attr.bInheritHandle = FALSE; |
116 |
|
|
sec_attr.nLength = sizeof (sec_attr); |
117 |
|
|
if (flags & F_DATA_READ) { |
118 |
|
|
fd = CreateFile (fname, GENERIC_READ, FILE_SHARE_READ, |
119 |
|
|
&sec_attr, OPEN_EXISTING, 0, NULL); |
120 |
twoaday |
219 |
if (fd == INVALID_HANDLE_VALUE) { |
121 |
|
|
log_debug ("gpg_file_data_new: CreateFile() error=%d\r\n", |
122 |
|
|
(int)GetLastError ()); |
123 |
twoaday |
119 |
return gpgme_err_code_from_errno (ENOENT); |
124 |
twoaday |
219 |
} |
125 |
twoaday |
119 |
} |
126 |
twoaday |
219 |
cb = new file_data_s; |
127 |
werner |
36 |
if (!cb) |
128 |
twoaday |
119 |
BUG (NULL); |
129 |
twoaday |
219 |
memset (cb, 0, sizeof *cb); |
130 |
|
|
cb->name = m_strdup (fname); |
131 |
werner |
36 |
cb->cbs.read = read_cb; |
132 |
|
|
cb->cbs.write = write_cb; |
133 |
twoaday |
119 |
if (flags & F_DATA_READ) { |
134 |
|
|
cb->handle = fd; |
135 |
|
|
cb->size = GetFileSize (fd, NULL); |
136 |
werner |
36 |
cb->off = 0; |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
err = gpgme_data_new_from_cbs (&cb->dat, &cb->cbs, cb); |
140 |
|
|
if (err) { |
141 |
twoaday |
119 |
CloseHandle (fd); |
142 |
twoaday |
219 |
free_if_alloc (cb); |
143 |
werner |
36 |
return err; |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
*r_cb = cb; |
147 |
|
|
return err; |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
|
151 |
|
|
/* Activate the progress callback for the given object @ctx. */ |
152 |
|
|
void |
153 |
|
|
gpg_file_data_set_cb (file_data_t ctx, struct progress_filter_s *pfx) |
154 |
|
|
{ |
155 |
|
|
ctx->cb_value = (void*)pfx; |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
|
159 |
twoaday |
271 |
/* Set the file pointer to the begin again. */ |
160 |
|
|
void |
161 |
|
|
gpg_file_data_rewind (file_data_t ctx) |
162 |
|
|
{ |
163 |
|
|
SetFilePointer (ctx->handle, 0, NULL, FILE_BEGIN); |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
|
167 |
werner |
36 |
/* Release the context in @cb. Close all internal handles if possible. */ |
168 |
|
|
void |
169 |
|
|
gpg_file_data_release (file_data_t cb) |
170 |
|
|
{ |
171 |
|
|
if (!cb) |
172 |
|
|
return; |
173 |
|
|
if (cb->handle) { |
174 |
twoaday |
119 |
CloseHandle (cb->handle); |
175 |
|
|
cb->handle = NULL; |
176 |
werner |
36 |
} |
177 |
|
|
if (cb->dat) |
178 |
|
|
gpgme_data_release (cb->dat); |
179 |
twoaday |
219 |
free_if_alloc (cb->name); |
180 |
|
|
free_if_alloc (cb); |
181 |
werner |
36 |
} |