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

Diff of /trunk/Src/wptFileCBS.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 118 by werner, Mon Oct 31 21:14:11 2005 UTC revision 119 by twoaday, Fri Dec 9 08:04:51 2005 UTC
# Line 1  Line 1 
1  /* wptFileCBS.cpp  /* wptFileCBS.cpp - Customized I/O callbacks for GPGME
2   *      Copyright (C) 2005 Timo Schulz   *      Copyright (C) 2005 Timo Schulz
3   *      Copyright (C) 2005 g10 Code GmbH   *      Copyright (C) 2005 g10 Code GmbH
4   *   *
# Line 24  Line 24 
24    
25  #include <windows.h>  #include <windows.h>
26  #include <stdio.h>  #include <stdio.h>
 #include <malloc.h>  
27  #include <errno.h>  #include <errno.h>
28  #include <stdlib.h>  #include <malloc.h>
 #include <sys/stat.h>  
29    
30  #include "gpgme.h"  #include "gpgme.h"
31  #include "wptListView.h"  #include "wptListView.h"
# Line 36  Line 34 
34  #include "wptErrors.h"  #include "wptErrors.h"
35  #include "wptTypes.h"  #include "wptTypes.h"
36    
37  void progress_callback (void *opaque, const char *what, int type, int off, int max);  void progress_callback (void *opaque, const char *what, int type,
38                            int off, int max);
39    
40    
41  /* Predefined read callback. */  /* Predefined read callback. */
# Line 45  read_cb (void *handle, void *buffer, siz Line 44  read_cb (void *handle, void *buffer, siz
44  {  {
45      file_data_t cb = (file_data_t)handle;      file_data_t cb = (file_data_t)handle;
46      struct progress_filter_s *pfx = (struct progress_filter_s *)cb->cb_value;      struct progress_filter_s *pfx = (struct progress_filter_s *)cb->cb_value;
47      int n = fread (buffer, 1, size, cb->handle);      DWORD nread = 0;
48    
49        ReadFile (cb->handle, buffer, size, &nread, NULL);
50    
51      /* XXX: there is a sync problem with the progress dialog. */      /* XXX: there is a sync problem with the progress dialog. */
52      if (pfx)      if (pfx)
53          progress_callback (pfx, NULL, 0, cb->off, cb->size);          progress_callback (pfx, NULL, 0, cb->off, cb->size);
54      cb->off += n;      cb->off += nread;
55      return n;      return (long)nread;
56  }  }
57    
58    
# Line 60  static long Line 61  static long
61  write_cb (void *handle, const void *buffer, size_t size)  write_cb (void *handle, const void *buffer, size_t size)
62  {  {
63      file_data_t cb = (file_data_t)handle;      file_data_t cb = (file_data_t)handle;
64      int n = fwrite (buffer, 1, size, cb->handle);      DWORD nwritten;
65    
66        /* XXX: check if fd == INVALID_HANDLE_VALUE. */
67    
68        if (!cb->handle) {
69            SECURITY_ATTRIBUTES sec_attr;
70    
71      return n;          memset (&sec_attr, 0, sizeof (sec_attr));
72            sec_attr.bInheritHandle = FALSE;
73            sec_attr.nLength = sizeof (sec_attr);
74            cb->handle = CreateFile (cb->name, GENERIC_WRITE, FILE_SHARE_WRITE,
75                                     &sec_attr, CREATE_ALWAYS, 0, NULL);
76        }
77    
78        WriteFile (cb->handle, buffer, size, &nwritten, NULL);
79        return (long)nwritten;
80  }  }
81    
82    
# Line 72  write_cb (void *handle, const void *buff Line 86  write_cb (void *handle, const void *buff
86     @for_read is 1 if the file is opened for read only.     @for_read is 1 if the file is opened for read only.
87     Return value: 0 on success. */     Return value: 0 on success. */
88  gpgme_error_t  gpgme_error_t
89  gpg_file_data_new (const char *fname, int for_read, file_data_t *r_cb)  gpg_file_data_new (const char *fname, int flags, file_data_t *r_cb)
90                                                
91  {  {
92      gpgme_error_t err;      gpgme_error_t err;
93      file_data_t cb;      file_data_t cb;
94      FILE *f;      HANDLE fd = NULL;
95        SECURITY_ATTRIBUTES sec_attr;
     f = fopen (fname, for_read?"rb" : "wb");  
     if (!f)  
         return gpgme_err_code_from_errno (errno);  
96    
97        memset (&sec_attr, 0, sizeof (sec_attr));
98        sec_attr.bInheritHandle = FALSE;
99        sec_attr.nLength = sizeof (sec_attr);
100        if (flags & F_DATA_READ) {
101            fd = CreateFile (fname, GENERIC_READ, FILE_SHARE_READ,
102                             &sec_attr, OPEN_EXISTING, 0, NULL);
103            if (fd == INVALID_HANDLE_VALUE)
104                return gpgme_err_code_from_errno (ENOENT);
105        }
106      cb = (file_data_t)calloc (1, sizeof *cb);      cb = (file_data_t)calloc (1, sizeof *cb);
107      if (!cb)      if (!cb)
108          abort ();          BUG (NULL);
109        cb->name = strdup (fname);
110        if (!cb->name)
111            BUG (NULL);
112      cb->cbs.read = read_cb;      cb->cbs.read = read_cb;
113      cb->cbs.write = write_cb;      cb->cbs.write = write_cb;
114      cb->handle = f;      if (flags & F_DATA_READ) {
115      if (for_read) {          cb->handle = fd;
116          struct stat st;          cb->size = GetFileSize (fd, NULL);
         if (fstat (fileno (f), &st))  
             BUG (NULL);  
         cb->size = st.st_size;  
117          cb->off = 0;          cb->off = 0;
118      }      }
119    
120      err = gpgme_data_new_from_cbs  (&cb->dat, &cb->cbs, cb);      err = gpgme_data_new_from_cbs  (&cb->dat, &cb->cbs, cb);
121      if (err) {      if (err) {
122          fclose (f);          CloseHandle (fd);
123          free (cb);          free (cb);
124          return err;          return err;
125      }      }
# Line 124  gpg_file_data_release (file_data_t cb) Line 144  gpg_file_data_release (file_data_t cb)
144      if (!cb)      if (!cb)
145          return;          return;
146      if (cb->handle) {      if (cb->handle) {
147          FILE *f = (FILE *)cb->handle;          CloseHandle (cb->handle);
148          fclose (f);          cb->handle = NULL;
149      }      }
150      if (cb->dat)      if (cb->dat)
151          gpgme_data_release (cb->dat);          gpgme_data_release (cb->dat);
152      free (cb);      safe_free (cb->name);
153        safe_free (cb);
154  }  }

Legend:
Removed from v.118  
changed lines
  Added in v.119

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26