comparison common/listutil.c @ 1081:edbf5e5e88f4

(issue118) Extend verify_binary to carry an open file * binverify.c: Change result to a structure containing an open fptr Use in Memory data for windows verification. * mainwindow.cpp, selftest.c: Handle the returend structure * binverifytest.cpp: Test for the exclusive read and update signature. * listutil.c: Add optional fptr parameter to read_file
author Andre Heinecke <andre.heinecke@intevation.de>
date Thu, 11 Sep 2014 12:05:24 +0200
parents 698b6a9bd75e
children fd7d04bb37cb
comparison
equal deleted inserted replaced
1080:898b1ddcca11 1081:edbf5e5e88f4
14 #include <unistd.h> 14 #include <unistd.h>
15 #include <sys/types.h> 15 #include <sys/types.h>
16 #include <sys/stat.h> 16 #include <sys/stat.h>
17 #include <string.h> 17 #include <string.h>
18 18
19 #ifdef WIN32
20 #include <share.h>
21 #endif
22
19 #include "strhelp.h" 23 #include "strhelp.h"
20 #include "logging.h" 24 #include "logging.h"
21 25
22 #ifdef RELEASE_BUILD 26 #ifdef RELEASE_BUILD
23 #include "pubkey-release.h" 27 #include "pubkey-release.h"
39 #define READ_FILE_NO_MEMORY -3 43 #define READ_FILE_NO_MEMORY -3
40 #define READ_FILE_READ_FAILED -4 44 #define READ_FILE_READ_FAILED -4
41 #define READ_FILE_INVALID_CALL -5 45 #define READ_FILE_INVALID_CALL -5
42 int 46 int
43 read_file(const char *file_name, char **data, size_t *size, 47 read_file(const char *file_name, char **data, size_t *size,
44 const size_t max_size) 48 const size_t max_size, FILE **fptr)
45 { 49 {
46 FILE *f; 50 FILE *f;
47 long file_size; 51 long file_size;
48 52
49 if (!file_name || !data || !size || !max_size) 53 if (!file_name || !data || !size || !max_size)
50 { 54 {
51 return READ_FILE_INVALID_CALL; 55 return READ_FILE_INVALID_CALL;
52 } 56 }
53 57 #ifdef WIN32
58 {
59 wchar_t *wFilename = utf8_to_wchar(file_name, strlen(file_name));
60 if (!wFilename)
61 {
62 return READ_FILE_UNREADABLE;
63 }
64 /* We open and write protect the file here so that
65 as long as the file is open we can be sure that
66 it was not modified and can use it in subsequent
67 calls based on the filename. */
68 f = _wfsopen(wFilename, L"rb", _SH_DENYWR);
69 xfree(wFilename);
70 }
71 #else
54 f = fopen(file_name, "rb"); 72 f = fopen(file_name, "rb");
73 #endif
55 if (f == NULL) 74 if (f == NULL)
56 return READ_FILE_UNREADABLE; 75 return READ_FILE_UNREADABLE;
57 76
58 fseek(f, 0, SEEK_END); 77 fseek(f, 0, SEEK_END);
59 file_size = ftell(f); 78 file_size = ftell(f);
90 free(*data); 109 free(*data);
91 fclose(f); 110 fclose(f);
92 return READ_FILE_READ_FAILED; 111 return READ_FILE_READ_FAILED;
93 } 112 }
94 113
95 fclose(f); 114 if (fptr)
115 {
116 *fptr = f;
117 }
118 else
119 {
120 fclose(f);
121 }
96 122
97 (*data)[*size] = '\0'; 123 (*data)[*size] = '\0';
98 124
99 return 0; 125 return 0;
100 } 126 }
178 list_status_t retval = UnknownError; 204 list_status_t retval = UnknownError;
179 *data = NULL; 205 *data = NULL;
180 *size = 0; 206 *size = 0;
181 int ret = 0; 207 int ret = 0;
182 208
183 ret = read_file(file_name, data, size, MAX_FILESIZE); 209 ret = read_file(file_name, data, size, MAX_FILESIZE, NULL);
184 210
185 /* printf ("Ret: %i \n", ret); */ 211 /* printf ("Ret: %i \n", ret); */
186 if (ret != 0) 212 if (ret != 0)
187 { 213 {
188 if (ret == READ_FILE_TOO_LARGE) 214 if (ret == READ_FILE_TOO_LARGE)

http://wald.intevation.org/projects/trustbridge/