1 |
/* wptFileCBS.cpp - Customized I/O callbacks for GPGME |
2 |
* Copyright (C) 2005, 2006 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 |
|
22 |
#ifdef HAVE_CONFIG_H |
23 |
#include <config.h> |
24 |
#endif |
25 |
|
26 |
#include <windows.h> |
27 |
#include <stdio.h> |
28 |
#include <errno.h> |
29 |
|
30 |
#include "wptListView.h" |
31 |
#include "wptGPG.h" |
32 |
#include "wptFileManager.h" |
33 |
#include "wptErrors.h" |
34 |
#include "wptTypes.h" |
35 |
#include "wptW32API.h" |
36 |
|
37 |
void progress_callback (void *opaque, const char *what, int type, |
38 |
int off, int max); |
39 |
|
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 |
DWORD nread = 0; |
48 |
|
49 |
if (cb->error) |
50 |
return -1; |
51 |
|
52 |
if (!ReadFile (cb->handle, buffer, size, &nread, NULL)) { |
53 |
cb->error = (int)GetLastError (); |
54 |
log_debug ("read_cb: ReadFile() error=%d\r\n", cb->error); |
55 |
return -1; |
56 |
} |
57 |
|
58 |
/* XXX: there is a sync problem with the progress dialog. */ |
59 |
if (pfx) |
60 |
progress_callback (pfx, NULL, 0, cb->off, cb->size); |
61 |
cb->off += nread; |
62 |
return (long)nread; |
63 |
} |
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 |
DWORD nwritten; |
72 |
|
73 |
if (cb->error) |
74 |
return -1; |
75 |
|
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 |
if (cb->handle == INVALID_HANDLE_VALUE) { |
85 |
cb->error = (int)GetLastError (); |
86 |
log_debug ("write_cb: CreateFile() error=%d\r\n", cb->error); |
87 |
return -1; |
88 |
} |
89 |
} |
90 |
|
91 |
if (!WriteFile (cb->handle, buffer, size, &nwritten, NULL)) { |
92 |
cb->error = (int)GetLastError (); |
93 |
log_debug ("write_cb: WriteFile() error=%d\r\n", cb->error); |
94 |
return -1; |
95 |
} |
96 |
return (long)nwritten; |
97 |
} |
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 |
gpg_file_data_new (const char *fname, int flags, file_data_t *r_cb) |
107 |
|
108 |
{ |
109 |
gpgme_error_t err; |
110 |
file_data_t cb; |
111 |
HANDLE fd = NULL; |
112 |
SECURITY_ATTRIBUTES sec_attr; |
113 |
|
114 |
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 |
if (fd == INVALID_HANDLE_VALUE) { |
121 |
log_debug ("gpg_file_data_new: CreateFile() error=%d\r\n", |
122 |
(int)GetLastError ()); |
123 |
return gpgme_err_code_from_errno (ENOENT); |
124 |
} |
125 |
} |
126 |
cb = new file_data_s; |
127 |
if (!cb) |
128 |
BUG (NULL); |
129 |
memset (cb, 0, sizeof *cb); |
130 |
cb->name = m_strdup (fname); |
131 |
cb->cbs.read = read_cb; |
132 |
cb->cbs.write = write_cb; |
133 |
if (flags & F_DATA_READ) { |
134 |
cb->handle = fd; |
135 |
cb->size = GetFileSize (fd, NULL); |
136 |
cb->off = 0; |
137 |
} |
138 |
|
139 |
err = gpgme_data_new_from_cbs (&cb->dat, &cb->cbs, cb); |
140 |
if (err) { |
141 |
CloseHandle (fd); |
142 |
free_if_alloc (cb); |
143 |
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 |
/* Release the context in @cb. Close all internal handles if possible. */ |
160 |
void |
161 |
gpg_file_data_release (file_data_t cb) |
162 |
{ |
163 |
if (!cb) |
164 |
return; |
165 |
if (cb->handle) { |
166 |
CloseHandle (cb->handle); |
167 |
cb->handle = NULL; |
168 |
} |
169 |
if (cb->dat) |
170 |
gpgme_data_release (cb->dat); |
171 |
free_if_alloc (cb->name); |
172 |
free_if_alloc (cb); |
173 |
} |