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