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 |
#ifdef HAVE_CONFIG_H |
18 |
#include <config.h> |
19 |
#endif |
20 |
|
21 |
#include <windows.h> |
22 |
#include <stdio.h> |
23 |
#include <errno.h> |
24 |
|
25 |
#include "wptListView.h" |
26 |
#include "wptGPG.h" |
27 |
#include "wptFileManager.h" |
28 |
#include "wptErrors.h" |
29 |
#include "wptTypes.h" |
30 |
#include "wptW32API.h" |
31 |
|
32 |
void progress_callback (void *opaque, const char *what, int type, |
33 |
int off, int max); |
34 |
|
35 |
|
36 |
/* Predefined read callback. */ |
37 |
static long |
38 |
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 |
DWORD nread = 0; |
43 |
|
44 |
if (cb->error) |
45 |
return -1; |
46 |
|
47 |
if (!ReadFile (cb->handle, buffer, size, &nread, NULL)) { |
48 |
cb->error = (int)GetLastError (); |
49 |
log_debug ("read_cb: ReadFile() error=%d\r\n", cb->error); |
50 |
return -1; |
51 |
} |
52 |
|
53 |
/* XXX: there is a sync problem with the progress dialog. */ |
54 |
if (pfx != NULL) |
55 |
progress_callback (pfx, NULL, 0, cb->off, cb->size); |
56 |
cb->off += nread; |
57 |
return (long)nread; |
58 |
} |
59 |
|
60 |
|
61 |
/* Predefined write callback. */ |
62 |
static long |
63 |
write_cb (void *handle, const void *buffer, size_t size) |
64 |
{ |
65 |
file_data_t cb = (file_data_t)handle; |
66 |
DWORD nwritten; |
67 |
|
68 |
if (cb->error) |
69 |
return -1; |
70 |
|
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 |
if (cb->handle == INVALID_HANDLE_VALUE) { |
80 |
cb->error = (int)GetLastError (); |
81 |
log_debug ("write_cb: CreateFile() error=%d\r\n", cb->error); |
82 |
return -1; |
83 |
} |
84 |
} |
85 |
|
86 |
if (!WriteFile (cb->handle, buffer, size, &nwritten, NULL)) { |
87 |
cb->error = (int)GetLastError (); |
88 |
log_debug ("write_cb: WriteFile() error=%d\r\n", cb->error); |
89 |
return -1; |
90 |
} |
91 |
return (long)nwritten; |
92 |
} |
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 |
gpg_file_data_new (const char *fname, int flags, file_data_t *r_cb) |
102 |
|
103 |
{ |
104 |
gpgme_error_t err; |
105 |
file_data_t cb; |
106 |
HANDLE fd = NULL; |
107 |
SECURITY_ATTRIBUTES sec_attr; |
108 |
|
109 |
*r_cb = NULL; /* reset */ |
110 |
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 |
if (fd == INVALID_HANDLE_VALUE) { |
117 |
log_debug ("gpg_file_data_new: CreateFile() error=%d\r\n", |
118 |
(int)GetLastError ()); |
119 |
return gpgme_err_code_from_errno (ENOENT); |
120 |
} |
121 |
} |
122 |
cb = new file_data_s; |
123 |
if (!cb) |
124 |
BUG (NULL); |
125 |
memset (cb, 0, sizeof *cb); |
126 |
cb->name = m_strdup (fname); |
127 |
cb->cbs.read = read_cb; |
128 |
cb->cbs.write = write_cb; |
129 |
if (flags & F_DATA_READ) { |
130 |
cb->handle = fd; |
131 |
cb->size = GetFileSize (fd, NULL); |
132 |
cb->off = 0; |
133 |
} |
134 |
|
135 |
err = gpgme_data_new_from_cbs (&cb->dat, &cb->cbs, cb); |
136 |
if (!err) |
137 |
*r_cb = cb; |
138 |
else { |
139 |
CloseHandle (fd); |
140 |
free_if_alloc (cb); |
141 |
} |
142 |
|
143 |
return err; |
144 |
} |
145 |
|
146 |
|
147 |
/* Set the progress callback for the given object @ctx. */ |
148 |
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 |
/* 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 |
/* Release the context in @cb. |
164 |
Close all internal handles if possible. */ |
165 |
void |
166 |
gpg_file_data_release (file_data_t cb) |
167 |
{ |
168 |
if (!cb) |
169 |
return; |
170 |
if (cb->handle) { |
171 |
CloseHandle (cb->handle); |
172 |
cb->handle = NULL; |
173 |
} |
174 |
if (cb->dat) { |
175 |
gpgme_data_release (cb->dat); |
176 |
cb->dat = NULL; |
177 |
} |
178 |
free_if_alloc (cb->name); |
179 |
free_if_alloc (cb); |
180 |
} |