/[winpt]/trunk/Src/wptFileCBS.cpp
ViewVC logotype

Contents of /trunk/Src/wptFileCBS.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 197 - (show annotations)
Mon Apr 10 07:38:06 2006 UTC (18 years, 10 months ago) by twoaday
File size: 4275 byte(s)
2006-04-09  Timo Schulz  <ts@g10code.de>
 
        * wptGPGPrefsDlg.cpp (gpgprefs_dlg_proc): Only return true
        if the homedir value was changed.
        * wptGPG.cpp (default_key_from_cache): Only return secret key
        if public part is available.
        (set_gnupg_default_key): Fix NULL problem.
        * wptKeyEditDlgs.cpp (do_editkey_clean): Set update flag.
        * wptFileCBS.cpp (write_cb, read_cb): Better error handling.
        * wptFileManagerDlg.cpp (file_manager_dlg_proc): Handle
        'always-on-top' correctly.
        * wptKeylist.cpp (keylist_get_recipients): Allocate enough
        mem to hold all possible keys.
        (keylist_enum_keys): Likewise.


1 /* wptFileCBS.cpp - Customized I/O callbacks for GPGME
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
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 #include <malloc.h>
30
31 #include "gpgme.h"
32 #include "wptListView.h"
33 #include "wptGPG.h"
34 #include "wptFileManager.h"
35 #include "wptErrors.h"
36 #include "wptTypes.h"
37
38 void progress_callback (void *opaque, const char *what, int type,
39 int off, int max);
40
41
42 /* Predefined read callback. */
43 static long
44 read_cb (void *handle, void *buffer, size_t size)
45 {
46 file_data_t cb = (file_data_t)handle;
47 struct progress_filter_s *pfx = (struct progress_filter_s *)cb->cb_value;
48 DWORD nread = 0;
49
50 if (cb->error)
51 return -1;
52
53 if (!ReadFile (cb->handle, buffer, size, &nread, NULL)) {
54 cb->error = (int)GetLastError ();
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 return -1;
87 }
88 }
89
90 if (!WriteFile (cb->handle, buffer, size, &nwritten, NULL)) {
91 cb->error = (int)GetLastError ();
92 return -1;
93 }
94 return (long)nwritten;
95 }
96
97
98 /* Create a new data -> file association with a static callback.
99 @fname is the file which is associated to the object.
100 @r_cb is the context which holds all information.
101 @for_read is 1 if the file is opened for read only.
102 Return value: 0 on success. */
103 gpgme_error_t
104 gpg_file_data_new (const char *fname, int flags, file_data_t *r_cb)
105
106 {
107 gpgme_error_t err;
108 file_data_t cb;
109 HANDLE fd = NULL;
110 SECURITY_ATTRIBUTES sec_attr;
111
112 memset (&sec_attr, 0, sizeof (sec_attr));
113 sec_attr.bInheritHandle = FALSE;
114 sec_attr.nLength = sizeof (sec_attr);
115 if (flags & F_DATA_READ) {
116 fd = CreateFile (fname, GENERIC_READ, FILE_SHARE_READ,
117 &sec_attr, OPEN_EXISTING, 0, NULL);
118 if (fd == INVALID_HANDLE_VALUE)
119 return gpgme_err_code_from_errno (ENOENT);
120 }
121 cb = (file_data_t)calloc (1, sizeof *cb);
122 if (!cb)
123 BUG (NULL);
124 cb->name = strdup (fname);
125 if (!cb->name)
126 BUG (NULL);
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 CloseHandle (fd);
138 free (cb);
139 return err;
140 }
141
142 *r_cb = cb;
143 return err;
144 }
145
146
147 /* Activate 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 /* Release the context in @cb. Close all internal handles if possible. */
156 void
157 gpg_file_data_release (file_data_t cb)
158 {
159 if (!cb)
160 return;
161 if (cb->handle) {
162 CloseHandle (cb->handle);
163 cb->handle = NULL;
164 }
165 if (cb->dat)
166 gpgme_data_release (cb->dat);
167 safe_free (cb->name);
168 safe_free (cb);
169 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26