diff options
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/include/gnunet_my_lib.h | 76 | ||||
-rw-r--r-- | src/my/my.c | 202 | ||||
-rw-r--r-- | src/my/my_result_helper.c | 310 | ||||
-rw-r--r-- | src/util/time.c | 9 |
5 files changed, 350 insertions, 249 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 39731b288..5582c178b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am | |||
@@ -45,7 +45,7 @@ endif | |||
45 | endif | 45 | endif |
46 | 46 | ||
47 | if HAVE_MYSQL | 47 | if HAVE_MYSQL |
48 | MYSQL_DIR = mysql my | 48 | MYSQL_DIR = mysql |
49 | endif | 49 | endif |
50 | 50 | ||
51 | if HAVE_POSTGRESQL | 51 | if HAVE_POSTGRESQL |
diff --git a/src/include/gnunet_my_lib.h b/src/include/gnunet_my_lib.h index c0a6f4b90..1ab139d31 100644 --- a/src/include/gnunet_my_lib.h +++ b/src/include/gnunet_my_lib.h | |||
@@ -95,7 +95,7 @@ struct GNUNET_MY_QueryParam | |||
95 | /** | 95 | /** |
96 | * Information to pass to @e conv. Size of @a data. | 96 | * Information to pass to @e conv. Size of @a data. |
97 | */ | 97 | */ |
98 | unsigned long data_len ; | 98 | unsigned long data_len; |
99 | 99 | ||
100 | }; | 100 | }; |
101 | 101 | ||
@@ -149,15 +149,31 @@ struct GNUNET_MY_ResultSpec; | |||
149 | * Function called to convert input argument into SQL parameters. | 149 | * Function called to convert input argument into SQL parameters. |
150 | * | 150 | * |
151 | * @param cls closure | 151 | * @param cls closure |
152 | * @param pq data about the query | 152 | * @param[in,out] rs |
153 | * @param stmt the mysql statement that is being run | ||
154 | * @param column the column that is being processed | ||
155 | * @param[out] results | ||
153 | * @return -1 on error | 156 | * @return -1 on error |
154 | */ | 157 | */ |
155 | typedef int | 158 | typedef int |
156 | (*GNUNET_MY_ResultConverter)(void *cls, | 159 | (*GNUNET_MY_ResultConverter)(void *cls, |
157 | struct GNUNET_MY_ResultSpec *rs, | 160 | struct GNUNET_MY_ResultSpec *rs, |
161 | MYSQL_STMT *stmt, | ||
162 | unsigned int column, | ||
158 | MYSQL_BIND *results); | 163 | MYSQL_BIND *results); |
159 | 164 | ||
160 | /** | 165 | /** |
166 | * Function called to cleanup result data. | ||
167 | * | ||
168 | * @param cls closure | ||
169 | * @param rs spec to clean up | ||
170 | */ | ||
171 | typedef void | ||
172 | (*GNUNET_MY_ResultCleanup)(void *cls, | ||
173 | struct GNUNET_MY_ResultSpec *rs); | ||
174 | |||
175 | |||
176 | /** | ||
161 | * Information we pass to #GNUNET_MY_extract_result() to | 177 | * Information we pass to #GNUNET_MY_extract_result() to |
162 | * initialize the arguments of the prepared statement. | 178 | * initialize the arguments of the prepared statement. |
163 | */ | 179 | */ |
@@ -165,9 +181,19 @@ struct GNUNET_MY_ResultSpec | |||
165 | { | 181 | { |
166 | 182 | ||
167 | /** | 183 | /** |
168 | * Function to call for the type conversion. | 184 | * Function to call to initialize the MYSQL_BIND array. |
169 | */ | 185 | */ |
170 | GNUNET_MY_ResultConverter conv; | 186 | GNUNET_MY_ResultConverter pre_conv; |
187 | |||
188 | /** | ||
189 | * Function to call for converting the result. Can be NULL. | ||
190 | */ | ||
191 | GNUNET_MY_ResultConverter post_conv; | ||
192 | |||
193 | /** | ||
194 | * Function to call for cleaning up the result. Can be NULL. | ||
195 | */ | ||
196 | GNUNET_MY_ResultCleanup cleaner; | ||
171 | 197 | ||
172 | /** | 198 | /** |
173 | * Closure for @e conv. | 199 | * Closure for @e conv. |
@@ -191,6 +217,18 @@ struct GNUNET_MY_ResultSpec | |||
191 | */ | 217 | */ |
192 | size_t *result_size; | 218 | size_t *result_size; |
193 | 219 | ||
220 | /** | ||
221 | * How many fields does this result specification occupy | ||
222 | * in the result returned by MySQL. | ||
223 | */ | ||
224 | unsigned int num_fields; | ||
225 | |||
226 | /** | ||
227 | * Location where we temporarily store the output buffer | ||
228 | * length from MySQL. Internal to libgnunetmy. | ||
229 | */ | ||
230 | unsigned long mysql_bind_output_length; | ||
231 | |||
194 | }; | 232 | }; |
195 | 233 | ||
196 | 234 | ||
@@ -199,7 +237,7 @@ struct GNUNET_MY_ResultSpec | |||
199 | * | 237 | * |
200 | * @return array last entry for the result specification to use | 238 | * @return array last entry for the result specification to use |
201 | */ | 239 | */ |
202 | #define GNUNET_MY_result_spec_end { NULL, NULL, NULL, 0, NULL } | 240 | #define GNUNET_MY_result_spec_end { NULL, NULL, NULL, 0, NULL, 0 } |
203 | 241 | ||
204 | 242 | ||
205 | 243 | ||
@@ -392,11 +430,33 @@ GNUNET_MY_result_spec_uint32 (uint32_t *u32); | |||
392 | struct GNUNET_MY_ResultSpec | 430 | struct GNUNET_MY_ResultSpec |
393 | GNUNET_MY_result_spec_uint64 (uint64_t *u64); | 431 | GNUNET_MY_result_spec_uint64 (uint64_t *u64); |
394 | 432 | ||
433 | |||
434 | /** | ||
435 | * Extract results from a query result according to the given | ||
436 | * specification. Always fetches the next row. | ||
437 | * | ||
438 | * @param sh statement that returned results | ||
439 | * @param rs specification to extract for | ||
440 | * @return | ||
441 | * #GNUNET_YES if all results could be extracted | ||
442 | * #GNUNET_NO if there is no more data in the result set | ||
443 | * #GNUNET_SYSERR if a result was invalid | ||
444 | */ | ||
395 | int | 445 | int |
396 | GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh, | 446 | GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh, |
397 | struct GNUNET_MY_QueryParam *qp, | 447 | struct GNUNET_MY_ResultSpec *specs); |
398 | struct GNUNET_MY_ResultSpec *specs, | 448 | |
399 | int row); | 449 | |
450 | |||
451 | /** | ||
452 | * Free all memory that was allocated in @a rs during | ||
453 | * #GNUNET_MY_extract_result(). | ||
454 | * | ||
455 | * @param rs reult specification to clean up | ||
456 | */ | ||
457 | void | ||
458 | GNUNET_MY_cleanup_result (struct GNUNET_PQ_ResultSpec *rs); | ||
459 | |||
400 | 460 | ||
401 | #if 0 /* keep Emacsens' auto-indent happy */ | 461 | #if 0 /* keep Emacsens' auto-indent happy */ |
402 | { | 462 | { |
diff --git a/src/my/my.c b/src/my/my.c index 54b2a49b0..1d78a08cf 100644 --- a/src/my/my.c +++ b/src/my/my.c | |||
@@ -35,7 +35,7 @@ | |||
35 | * @param mc mysql context | 35 | * @param mc mysql context |
36 | * @param sh handle to SELECT statment | 36 | * @param sh handle to SELECT statment |
37 | * @param params parameters to the statement | 37 | * @param params parameters to the statement |
38 | * @return | 38 | * @return |
39 | #GNUNET_YES if we can prepare all statement | 39 | #GNUNET_YES if we can prepare all statement |
40 | #GNUNET_SYSERR if we can't prepare all statement | 40 | #GNUNET_SYSERR if we can't prepare all statement |
41 | */ | 41 | */ |
@@ -98,37 +98,24 @@ GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc, | |||
98 | 98 | ||
99 | 99 | ||
100 | /** | 100 | /** |
101 | * Extract results from a query result according | 101 | * Extract results from a query result according to the given |
102 | * to the given specification. If colums are NULL, | 102 | * specification. Always fetches the next row. |
103 | * the destination is not modified, and #GNUNET_NO is returned4 | ||
104 | * | 103 | * |
105 | * | 104 | * |
106 | * @param result | 105 | * @param sh statement that returned results |
107 | * @param row, the row from the result to extract | 106 | * @param rs specification to extract for |
108 | * @param result specificatio to extract for | ||
109 | * @return | 107 | * @return |
110 | #GNUNET_YES if all results could be extracted | 108 | * #GNUNET_YES if all results could be extracted |
111 | #GNUNET_NO if at least one result was NULL | 109 | * #GNUNET_NO if there is no more data in the result set |
112 | #GNUNET_SYSERR if a result was invalid | 110 | * #GNUNET_SYSERR if a result was invalid |
113 | */ | 111 | */ |
114 | int | 112 | int |
115 | GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh, | 113 | GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh, |
116 | struct GNUNET_MY_QueryParam *qp, | 114 | struct GNUNET_MY_ResultSpec *rs) |
117 | struct GNUNET_MY_ResultSpec *rs, | ||
118 | int row) | ||
119 | { | 115 | { |
120 | MYSQL_BIND *result; | 116 | unsigned int num_fields; |
121 | |||
122 | int num_fields; | ||
123 | MYSQL_FIELD *fields; | ||
124 | MYSQL_RES *res; | ||
125 | |||
126 | unsigned int i; | 117 | unsigned int i; |
127 | unsigned int j; | ||
128 | int had_null = GNUNET_NO; | ||
129 | int ret; | 118 | int ret; |
130 | |||
131 | result = NULL; | ||
132 | MYSQL_STMT *stmt; | 119 | MYSQL_STMT *stmt; |
133 | 120 | ||
134 | stmt = GNUNET_MYSQL_statement_get_stmt (NULL /* FIXME */, sh); | 121 | stmt = GNUNET_MYSQL_statement_get_stmt (NULL /* FIXME */, sh); |
@@ -141,115 +128,104 @@ GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh, | |||
141 | return GNUNET_SYSERR; | 128 | return GNUNET_SYSERR; |
142 | } | 129 | } |
143 | 130 | ||
131 | num_fields = 0; | ||
132 | for (i=0;NULL != rs[i].conv;i++) | ||
133 | num_fields += rs[i].num_fields; | ||
144 | 134 | ||
145 | num_fields = mysql_stmt_field_count (stmt); | 135 | if (mysql_stmt_field_count (stmt) != num_fields) |
146 | res = mysql_stmt_result_metadata (stmt); | ||
147 | fields = mysql_fetch_fields (res); | ||
148 | |||
149 | int int_data[num_fields]; | ||
150 | long int long_data[num_fields]; | ||
151 | short short_data[num_fields]; | ||
152 | char str_data[STRING_SIZE]; | ||
153 | int error[num_fields]; | ||
154 | |||
155 | result = (MYSQL_BIND *)malloc (sizeof (MYSQL_BIND)*num_fields); | ||
156 | if(!result) | ||
157 | { | 136 | { |
158 | fprintf(stderr, "Error to allocate output buffers\n"); | 137 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
138 | "Number of fields missmatch between SQL result and result specification\n"); | ||
159 | return GNUNET_SYSERR; | 139 | return GNUNET_SYSERR; |
160 | } | 140 | } |
161 | 141 | ||
162 | memset(result, 0, sizeof (MYSQL_BIND) * num_fields); | ||
163 | |||
164 | /** INITIALISER LE MYSQL_BIND ****/ | ||
165 | |||
166 | for(i = 0 ; i< num_fields ;i++) | ||
167 | { | 142 | { |
168 | result[i].buffer_type = fields[i].type; | 143 | MYSQL_BIND result[num_fields]; |
169 | result[i].is_null = 0; | 144 | unsigned int field_off; |
170 | result[i].error = &error[i]; | ||
171 | 145 | ||
172 | switch (fields[i].type) | 146 | memset (result, 0, sizeof (MYSQL_BIND) * num_fields); |
147 | field_off = 0; | ||
148 | for (i=0;NULL != rs[i].conv;i++) | ||
173 | { | 149 | { |
174 | case MYSQL_TYPE_LONG: | 150 | struct GNUNET_MY_ResultSpec *rp = &rs[i]; |
175 | result[i].buffer = &(int_data[i]); | ||
176 | result[i].buffer_length = sizeof (int_data); | ||
177 | break; | ||
178 | |||
179 | case MYSQL_TYPE_LONGLONG: | ||
180 | result[i].buffer = &(long_data[i]); | ||
181 | result[i].buffer_length = sizeof (long_data); | ||
182 | break; | ||
183 | |||
184 | case MYSQL_TYPE_STRING: | ||
185 | result[i].buffer = (char *)str_data; | ||
186 | result[i].buffer_length = sizeof (str_data); | ||
187 | break; | ||
188 | |||
189 | case MYSQL_TYPE_SHORT: | ||
190 | result[i].buffer = &(short_data[i]); | ||
191 | result[i].buffer_length = sizeof (short_data); | ||
192 | break; | ||
193 | |||
194 | default: | ||
195 | fprintf(stderr, "Failed : wrong type : %d!\n", fields[i].type); | ||
196 | } | ||
197 | } | ||
198 | 151 | ||
199 | if (mysql_stmt_bind_result(stmt, result)) | 152 | if (GNUNET_OK != |
200 | { | 153 | rp->pre_conv (rp->cls, |
201 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql", | 154 | rp, |
155 | stmt, | ||
156 | field_off, | ||
157 | &result[field_off])) | ||
158 | { | ||
159 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
160 | "Pre-conversion for MySQL result failed at offset %u\n", | ||
161 | i); | ||
162 | GNUNET_MY_cleanup_result (rs); | ||
163 | return GNUNET_SYSERR; | ||
164 | } | ||
165 | field_off += rp->num_fields; | ||
166 | } | ||
167 | if (mysql_stmt_bind_result (stmt, result)) | ||
168 | { | ||
169 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | ||
170 | "my", | ||
202 | _("`%s' failed at %s:%d with error: %s\n"), | 171 | _("`%s' failed at %s:%d with error: %s\n"), |
203 | "mysql_stmt_bind_result", __FILE__, __LINE__, | 172 | "mysql_stmt_bind_result", __FILE__, __LINE__, |
204 | mysql_stmt_error (stmt)); | 173 | mysql_stmt_error (stmt)); |
205 | return GNUNET_SYSERR; | 174 | return GNUNET_SYSERR; |
206 | } | ||
207 | |||
208 | /*** FAILED HERE ***/ | ||
209 | if (mysql_stmt_fetch (stmt)) | ||
210 | { | ||
211 | for(j = 0 ; j < num_fields ;j++) | ||
212 | { | ||
213 | fprintf(stderr, "Error Bind [%d] : %d\n", j, error[j]); | ||
214 | } | 175 | } |
215 | 176 | ret = mysql_stmt_fetch (stmt); | |
216 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql", | 177 | if (MYSQL_NO_DATA == ret) |
217 | _("`%s' failed at %s:%d with error: %s\n"), | 178 | return GNUNET_NO; |
218 | "mysql_stmt_fetch", __FILE__, __LINE__, | 179 | if (0 != ret) |
180 | { | ||
181 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | ||
182 | "my", | ||
183 | _("mysql_stmt_fetch failed at %s:%d with error: %s\n"), | ||
184 | __FILE__, __LINE__, | ||
219 | mysql_stmt_error (stmt)); | 185 | mysql_stmt_error (stmt)); |
220 | return GNUNET_SYSERR; | 186 | return GNUNET_SYSERR; |
221 | } | 187 | } |
222 | 188 | field_off = 0; | |
223 | /* | 189 | for (i=0;NULL != rs[i].conv;i++) |
224 | while (1) | ||
225 | { | ||
226 | mysql_stmt_fetch (stmt); | ||
227 | |||
228 | for (i = 0 ; NULL != rs[i].conv ; i++) | ||
229 | { | 190 | { |
230 | struct GNUNET_MY_ResultSpec *spec; | 191 | struct GNUNET_MY_ResultSpec *rp = &rs[i]; |
231 | 192 | ||
232 | spec = &rs[i]; | 193 | if (NULL != rp->post_conv) |
233 | ret = spec->conv (spec->conv_cls, | 194 | if (GNUNET_OK != |
234 | spec, | 195 | rp->post_conv (rp->cls, |
235 | result); | 196 | rp, |
236 | 197 | stmt, | |
237 | if (GNUNET_SYSERR == ret) | 198 | field_off, |
238 | { | 199 | &result[field_off])) |
239 | return GNUNET_SYSERR; | 200 | { |
240 | } | 201 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
241 | 202 | "Post-conversion for MySQL result failed at offset %u\n", | |
242 | if (NULL != spec->result_size) | 203 | i); |
243 | *spec->result_size = spec->dst_size; | 204 | GNUNET_MY_cleanup_result (rs); |
205 | return GNUNET_SYSERR; | ||
206 | } | ||
207 | field_off += rp->num_fields; | ||
244 | } | 208 | } |
245 | } | 209 | } |
210 | return GNUNET_OK; | ||
211 | } | ||
246 | 212 | ||
247 | if (GNUNET_YES == had_null) | ||
248 | return GNUNET_NO; | ||
249 | */ | ||
250 | 213 | ||
251 | free (result); | 214 | /** |
252 | return GNUNET_OK; | 215 | * Free all memory that was allocated in @a rs during |
216 | * #GNUNET_MY_extract_result(). | ||
217 | * | ||
218 | * @param rs reult specification to clean up | ||
219 | */ | ||
220 | void | ||
221 | GNUNET_MY_cleanup_result (struct GNUNET_PQ_ResultSpec *rs) | ||
222 | { | ||
223 | unsigned int i; | ||
224 | |||
225 | for (i=0;NULL != rs[i].conv;i++) | ||
226 | rs[i].cleaner (rs[i].cls, | ||
227 | &rs[i]); | ||
253 | } | 228 | } |
254 | 229 | ||
230 | |||
255 | /* end of my.c */ | 231 | /* end of my.c */ |
diff --git a/src/my/my_result_helper.c b/src/my/my_result_helper.c index a5c39913c..108edb62d 100644 --- a/src/my/my_result_helper.c +++ b/src/my/my_result_helper.c | |||
@@ -21,116 +21,166 @@ | |||
21 | #include "gnunet_my_lib.h" | 21 | #include "gnunet_my_lib.h" |
22 | 22 | ||
23 | /** | 23 | /** |
24 | * extract data from a Mysql database @a result at row @a row | 24 | * extract data from a Mysql database @a result at row @a row |
25 | * | 25 | * |
26 | * @param cls closure | 26 | * @param cls closure |
27 | * @param qp data about the query | 27 | * @param qp data about the query |
28 | * @param result mysql result | 28 | * @param result mysql result |
29 | * @return | 29 | * @return |
30 | * #GNUNET_OK if all results could be extracted | 30 | * #GNUNET_OK if all results could be extracted |
31 | * #GNUNET_SYSERR if a result was invalid | 31 | * #GNUNET_SYSERR if a result was invalid |
32 | */ | 32 | */ |
33 | |||
34 | static int | 33 | static int |
35 | extract_varsize_blob (void *cls, | 34 | pre_extract_varsize_blob (void *cls, |
36 | struct GNUNET_MY_ResultSpec *rs, | 35 | struct GNUNET_MY_ResultSpec *rs, |
37 | MYSQL_BIND *results) | 36 | MYSQL_BIND *results) |
38 | { | 37 | { |
39 | size_t len; | 38 | results[0].buffer = NULL; |
40 | void *idst; | 39 | results[0].buffer_length = 0; |
41 | char * res; | 40 | results[0].length = &rs->mysql_bind_output_length; |
41 | return GNUNET_OK; | ||
42 | } | ||
42 | 43 | ||
43 | if (results->is_null) | 44 | |
45 | /** | ||
46 | * extract data from a Mysql database @a result at row @a row | ||
47 | * | ||
48 | * @param cls closure | ||
49 | * @param[in,out] rs | ||
50 | * @param stmt the mysql statement that is being run | ||
51 | * @param column the column that is being processed | ||
52 | * @param[out] results | ||
53 | * @return | ||
54 | * #GNUNET_OK if all results could be extracted | ||
55 | * #GNUNET_SYSERR if a result was invalid | ||
56 | */ | ||
57 | static int | ||
58 | post_extract_varsize_blob (void *cls, | ||
59 | struct GNUNET_MY_ResultSpec *rs, | ||
60 | MYSQL_STMT *stmt, | ||
61 | unsigned int column, | ||
62 | MYSQL_BIND *results) | ||
63 | { | ||
64 | void *buf; | ||
65 | size_t size; | ||
66 | |||
67 | size = (size_t) rs->mysql_bind_output_length; | ||
68 | if (rs->mysql_bind_output_length != size) | ||
69 | return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */ | ||
70 | buf = GNUNET_malloc (size); | ||
71 | results[0].buffer = buf; | ||
72 | results[0].buffer_length = size; | ||
73 | if (0 != | ||
74 | mysql_stmt_fetch_column (stmt, | ||
75 | results, | ||
76 | column, | ||
77 | 0)) | ||
44 | { | 78 | { |
79 | GNUNET_free (buf); | ||
45 | return GNUNET_SYSERR; | 80 | return GNUNET_SYSERR; |
46 | } | 81 | } |
82 | *(void **) rs->dst = buf; | ||
83 | *rs->result_size = size; | ||
84 | return GNUNET_OK; | ||
85 | } | ||
47 | 86 | ||
48 | len = results->buffer_length; | ||
49 | res = results->buffer; | ||
50 | |||
51 | GNUNET_assert (NULL != res); | ||
52 | |||
53 | rs->dst_size = len; | ||
54 | |||
55 | idst = GNUNET_malloc (len); | ||
56 | *(void **)rs->dst = idst; | ||
57 | |||
58 | memcpy (idst, | ||
59 | res, | ||
60 | len); | ||
61 | 87 | ||
62 | return GNUNET_OK; | 88 | /** |
89 | * extract data from a Mysql database @a result at row @a row | ||
90 | * | ||
91 | * @param cls closure | ||
92 | * @param[in,out] rs | ||
93 | */ | ||
94 | static void | ||
95 | cleanup_varsize_blob (void *cls, | ||
96 | struct GNUNET_MY_ResultSpec *rs) | ||
97 | { | ||
98 | void *ptr; | ||
99 | |||
100 | ptr = * (void **) rs->dst; | ||
101 | if (NULL == ptr) | ||
102 | return; | ||
103 | GNUNET_free (ptr); | ||
104 | *(void **) rs->dst = NULL; | ||
105 | *rs->result_size = 0; | ||
63 | } | 106 | } |
64 | 107 | ||
108 | |||
65 | /** | 109 | /** |
66 | * Variable-size result expected | 110 | * Variable-size result expected |
67 | * | 111 | * |
68 | * @param[out] dst where to store the result, allocated | 112 | * @param[out] dst where to store the result, allocated |
69 | * @param[out] sptr where to store the size of @a dst | 113 | * @param[out] sptr where to store the size of @a dst |
70 | * @return array entru for the result specification to use | 114 | * @return array entru for the result specification to use |
71 | */ | 115 | */ |
72 | struct GNUNET_MY_ResultSpec | 116 | struct GNUNET_MY_ResultSpec |
73 | GNUNET_MY_result_spec_variable_size (void **dst, | 117 | GNUNET_MY_result_spec_variable_size (void **dst, |
74 | size_t *ptr_size) | 118 | size_t *ptr_size) |
75 | { | 119 | { |
76 | struct GNUNET_MY_ResultSpec res = | 120 | struct GNUNET_MY_ResultSpec res = |
77 | { | 121 | { |
78 | &extract_varsize_blob, | 122 | .pre_conv = &pre_extract_varsize_blob, |
79 | NULL, | 123 | .post_conv = &post_extract_varsize_blob, |
80 | (void *)(dst), | 124 | .cleaner = &cleanup_varsize_blob, |
81 | 0, | 125 | .dst = (void *)(dst), |
82 | ptr_size | 126 | .result_size = ptr_size, |
127 | .num_fields = 1 | ||
83 | }; | 128 | }; |
84 | 129 | ||
85 | return res; | 130 | return res; |
86 | } | 131 | } |
87 | 132 | ||
133 | |||
88 | /** | 134 | /** |
89 | * Extract data from a Mysql database @a result at row @a row | 135 | * Extract data from a Mysql database @a result at row @a row |
90 | * | 136 | * |
91 | * @param cls closure | 137 | * @param cls closure |
92 | * @param result where to extract data from | 138 | * @param result where to extract data from |
93 | * @param int row to extract data from | 139 | * @param int row to extract data from |
94 | * @param fname name (or prefix) of the fields to extract from | 140 | * @param fname name (or prefix) of the fields to extract from |
95 | * @param[in] dst_size desired size, never NULL | 141 | * @param[in] dst_size desired size, never NULL |
96 | * @param[out] dst where to store the result | 142 | * @param[out] dst where to store the result |
97 | * @return | 143 | * @return |
98 | * #GNUNET_OK if all results could be extracted | 144 | * #GNUNET_OK if all results could be extracted |
99 | * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL) | 145 | * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL) |
100 | * | 146 | */ |
101 | */ | ||
102 | static int | 147 | static int |
103 | extract_fixed_blob (void *cls, | 148 | pre_extract_fixed_blob (void *cls, |
104 | struct GNUNET_MY_ResultSpec *rs, | 149 | struct GNUNET_MY_ResultSpec *rs, |
105 | MYSQL_BIND *results) | 150 | MYSQL_BIND *results) |
106 | { | 151 | { |
107 | size_t len; | 152 | results[0].buffer = rs->dst; |
108 | const char *res; | 153 | results[0].buffer_length = rs->dst_size; |
154 | results[0].length = &rs->mysql_bind_output_length; | ||
155 | return GNUNET_OK; | ||
156 | } | ||
109 | 157 | ||
110 | if (results->is_null) | ||
111 | { | ||
112 | return GNUNET_SYSERR; | ||
113 | } | ||
114 | 158 | ||
115 | len = results->buffer_length; | 159 | /** |
116 | if (rs->dst_size != len) | 160 | * Check size of extracted fixed size data from a Mysql database @a |
117 | { | 161 | * result at row @a row |
118 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 162 | * |
119 | "Results has wrong size (got %u, expected %u)\n", | 163 | * @param cls closure |
120 | (unsigned int)len, | 164 | * @param result where to extract data from |
121 | (unsigned int)rs->dst_size); | 165 | * @param int row to extract data from |
166 | * @param fname name (or prefix) of the fields to extract from | ||
167 | * @param[in] dst_size desired size, never NULL | ||
168 | * @param[out] dst where to store the result | ||
169 | * @return | ||
170 | * #GNUNET_OK if all results could be extracted | ||
171 | * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL) | ||
172 | */ | ||
173 | static int | ||
174 | post_extract_fixed_blob (void *cls, | ||
175 | struct GNUNET_MY_ResultSpec *rs, | ||
176 | MYSQL_BIND *results) | ||
177 | { | ||
178 | if (rs->dst_size != rs->mysql_bind_output_length) | ||
122 | return GNUNET_SYSERR; | 179 | return GNUNET_SYSERR; |
123 | } | ||
124 | |||
125 | res = results->buffer; | ||
126 | |||
127 | GNUNET_assert (NULL != res); | ||
128 | memcpy (rs->dst, | ||
129 | res, | ||
130 | len); | ||
131 | |||
132 | return GNUNET_OK; | 180 | return GNUNET_OK; |
133 | } | 181 | } |
182 | |||
183 | |||
134 | /** | 184 | /** |
135 | * Fixed-size result expected. | 185 | * Fixed-size result expected. |
136 | * | 186 | * |
@@ -143,15 +193,15 @@ struct GNUNET_MY_ResultSpec | |||
143 | GNUNET_MY_result_spec_fixed_size (void *ptr, | 193 | GNUNET_MY_result_spec_fixed_size (void *ptr, |
144 | size_t ptr_size) | 194 | size_t ptr_size) |
145 | { | 195 | { |
146 | struct GNUNET_MY_ResultSpec res = | 196 | struct GNUNET_MY_ResultSpec res = |
147 | { | 197 | { |
148 | &extract_fixed_blob, | 198 | .pre_conv = &pre_extract_fixed_blob, |
149 | NULL, | 199 | .post_conv = &post_extract_fixed_blob, |
150 | (void *)(ptr), | 200 | .dst = (void *)(ptr), |
151 | ptr_size, | 201 | .dst_size = ptr_size, |
152 | NULL | 202 | .num_fields = 1 |
153 | }; | 203 | }; |
154 | 204 | ||
155 | return res; | 205 | return res; |
156 | } | 206 | } |
157 | 207 | ||
@@ -175,7 +225,7 @@ extract_rsa_public_key (void *cls, | |||
175 | 225 | ||
176 | { | 226 | { |
177 | struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst; | 227 | struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst; |
178 | 228 | ||
179 | size_t len; | 229 | size_t len; |
180 | const char *res; | 230 | const char *res; |
181 | 231 | ||
@@ -215,7 +265,8 @@ GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa) | |||
215 | NULL, | 265 | NULL, |
216 | (void *) rsa, | 266 | (void *) rsa, |
217 | 0, | 267 | 0, |
218 | NULL | 268 | NULL, |
269 | 1 | ||
219 | }; | 270 | }; |
220 | 271 | ||
221 | return res; | 272 | return res; |
@@ -273,13 +324,14 @@ extract_rsa_signature (void *cls, | |||
273 | struct GNUNET_MY_ResultSpec | 324 | struct GNUNET_MY_ResultSpec |
274 | GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig) | 325 | GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig) |
275 | { | 326 | { |
276 | struct GNUNET_MY_ResultSpec res = | 327 | struct GNUNET_MY_ResultSpec res = |
277 | { | 328 | { |
278 | &extract_rsa_signature, | 329 | &extract_rsa_signature, |
279 | NULL, | 330 | NULL, |
280 | (void *)sig, | 331 | (void *)sig, |
281 | 0, | 332 | 0, |
282 | NULL | 333 | NULL, |
334 | 1 | ||
283 | }; | 335 | }; |
284 | return res; | 336 | return res; |
285 | } | 337 | } |
@@ -326,13 +378,15 @@ extract_string (void * cls, | |||
326 | return GNUNET_SYSERR; | 378 | return GNUNET_SYSERR; |
327 | } | 379 | } |
328 | return GNUNET_OK; | 380 | return GNUNET_OK; |
329 | } | 381 | } |
382 | |||
383 | |||
330 | /** | 384 | /** |
331 | * 0- terminated string exprected. | 385 | * 0- terminated string exprected. |
332 | * | 386 | * |
333 | * @param[out] dst where to store the result, allocated | 387 | * @param[out] dst where to store the result, allocated |
334 | * @return array entry for the result specification to use | 388 | * @return array entry for the result specification to use |
335 | */ | 389 | */ |
336 | struct GNUNET_MY_ResultSpec | 390 | struct GNUNET_MY_ResultSpec |
337 | GNUNET_MY_result_spec_string (char **dst) | 391 | GNUNET_MY_result_spec_string (char **dst) |
338 | { | 392 | { |
@@ -341,17 +395,19 @@ GNUNET_MY_result_spec_string (char **dst) | |||
341 | NULL, | 395 | NULL, |
342 | (void *) dst, | 396 | (void *) dst, |
343 | 0, | 397 | 0, |
344 | NULL | 398 | NULL, |
399 | 1 | ||
345 | }; | 400 | }; |
346 | return res; | 401 | return res; |
347 | } | 402 | } |
348 | 403 | ||
404 | |||
349 | /** | 405 | /** |
350 | * Absolute time expected | 406 | * Absolute time expected |
351 | * | 407 | * |
352 | * @param name name of the field in the table | 408 | * @param name name of the field in the table |
353 | * @param[out] at where to store the result | 409 | * @param[out] at where to store the result |
354 | * @return array entry for the result specification to use | 410 | * @return array entry for the result specification to use |
355 | */ | 411 | */ |
356 | struct GNUNET_MY_ResultSpec | 412 | struct GNUNET_MY_ResultSpec |
357 | GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at) | 413 | GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at) |
@@ -359,6 +415,7 @@ GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at) | |||
359 | return GNUNET_MY_result_spec_uint64 (&at->abs_value_us); | 415 | return GNUNET_MY_result_spec_uint64 (&at->abs_value_us); |
360 | } | 416 | } |
361 | 417 | ||
418 | |||
362 | /** | 419 | /** |
363 | * Absolute time in network byte order expected | 420 | * Absolute time in network byte order expected |
364 | * | 421 | * |
@@ -368,11 +425,12 @@ GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at) | |||
368 | struct GNUNET_MY_ResultSpec | 425 | struct GNUNET_MY_ResultSpec |
369 | GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at) | 426 | GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at) |
370 | { | 427 | { |
371 | struct GNUNET_MY_ResultSpec res = | 428 | struct GNUNET_MY_ResultSpec res = |
372 | GNUNET_MY_result_spec_auto_from_type (&at->abs_value_us__); | 429 | GNUNET_MY_result_spec_auto_from_type (&at->abs_value_us__); |
373 | return res; | 430 | return res; |
374 | } | 431 | } |
375 | 432 | ||
433 | |||
376 | /** | 434 | /** |
377 | * Extract data from a Postgres database @a result at row @a row. | 435 | * Extract data from a Postgres database @a result at row @a row. |
378 | * | 436 | * |
@@ -390,7 +448,7 @@ static int | |||
390 | extract_uint16 (void *cls, | 448 | extract_uint16 (void *cls, |
391 | struct GNUNET_MY_ResultSpec *rs, | 449 | struct GNUNET_MY_ResultSpec *rs, |
392 | MYSQL_BIND *results) | 450 | MYSQL_BIND *results) |
393 | { | 451 | { |
394 | uint16_t *udst = rs->dst; | 452 | uint16_t *udst = rs->dst; |
395 | const uint16_t *res; | 453 | const uint16_t *res; |
396 | 454 | ||
@@ -412,12 +470,13 @@ extract_uint16 (void *cls, | |||
412 | return GNUNET_OK; | 470 | return GNUNET_OK; |
413 | } | 471 | } |
414 | 472 | ||
473 | |||
415 | /** | 474 | /** |
416 | * uint16_t expected | 475 | * uint16_t expected |
417 | * | 476 | * |
418 | * @param[out] u16 where to store the result | 477 | * @param[out] u16 where to store the result |
419 | * @return array entry for the result specification to use | 478 | * @return array entry for the result specification to use |
420 | */ | 479 | */ |
421 | struct GNUNET_MY_ResultSpec | 480 | struct GNUNET_MY_ResultSpec |
422 | GNUNET_MY_result_spec_uint16 (uint16_t *u16) | 481 | GNUNET_MY_result_spec_uint16 (uint16_t *u16) |
423 | { | 482 | { |
@@ -426,7 +485,8 @@ GNUNET_MY_result_spec_uint16 (uint16_t *u16) | |||
426 | NULL, | 485 | NULL, |
427 | (void *) u16, | 486 | (void *) u16, |
428 | sizeof (*u16), | 487 | sizeof (*u16), |
429 | NULL | 488 | NULL, |
489 | 1 | ||
430 | }; | 490 | }; |
431 | return res; | 491 | return res; |
432 | } | 492 | } |
@@ -467,7 +527,7 @@ extract_uint32 (void *cls, | |||
467 | res = (uint32_t *)results->buffer; | 527 | res = (uint32_t *)results->buffer; |
468 | 528 | ||
469 | *udst = ntohl (*res); | 529 | *udst = ntohl (*res); |
470 | 530 | ||
471 | return GNUNET_OK; | 531 | return GNUNET_OK; |
472 | } | 532 | } |
473 | 533 | ||
@@ -485,7 +545,8 @@ GNUNET_MY_result_spec_uint32 (uint32_t *u32) | |||
485 | NULL, | 545 | NULL, |
486 | (void *) u32, | 546 | (void *) u32, |
487 | sizeof (*u32), | 547 | sizeof (*u32), |
488 | NULL | 548 | NULL, |
549 | 1 | ||
489 | }; | 550 | }; |
490 | return res; | 551 | return res; |
491 | } | 552 | } |
@@ -511,10 +572,8 @@ extract_uint64 (void *cls, | |||
511 | uint64_t *udst = rs->dst; | 572 | uint64_t *udst = rs->dst; |
512 | const uint64_t *res; | 573 | const uint64_t *res; |
513 | 574 | ||
514 | if (results->is_null) | 575 | results[0].buffer = &rs->dst; |
515 | { | 576 | results[0].buffer_length = 42; |
516 | return GNUNET_SYSERR; | ||
517 | } | ||
518 | 577 | ||
519 | GNUNET_assert (NULL != rs->dst); | 578 | GNUNET_assert (NULL != rs->dst); |
520 | if (sizeof (uint64_t) != rs->dst_size) | 579 | if (sizeof (uint64_t) != rs->dst_size) |
@@ -540,13 +599,12 @@ struct GNUNET_MY_ResultSpec | |||
540 | GNUNET_MY_result_spec_uint64 (uint64_t *u64) | 599 | GNUNET_MY_result_spec_uint64 (uint64_t *u64) |
541 | { | 600 | { |
542 | struct GNUNET_MY_ResultSpec res = { | 601 | struct GNUNET_MY_ResultSpec res = { |
543 | &extract_uint64, | 602 | .pre_conv = &extract_uint64, |
544 | NULL, | 603 | .dst = (void *) u64, |
545 | (void *) u64, | 604 | .dst_size = sizeof (*u64), |
546 | sizeof (*u64), | 605 | .num_fields = 1 |
547 | NULL | ||
548 | }; | 606 | }; |
549 | return res; | 607 | return res; |
550 | } | 608 | } |
551 | 609 | ||
552 | /* end of pq_result_helper.c */ \ No newline at end of file | 610 | /* end of pq_result_helper.c */ |
diff --git a/src/util/time.c b/src/util/time.c index 654b567f0..91bbbf72a 100644 --- a/src/util/time.c +++ b/src/util/time.c | |||
@@ -149,6 +149,7 @@ struct GNUNET_TIME_Relative | |||
149 | GNUNET_TIME_relative_get_unit_ () | 149 | GNUNET_TIME_relative_get_unit_ () |
150 | { | 150 | { |
151 | static struct GNUNET_TIME_Relative one = { 1 }; | 151 | static struct GNUNET_TIME_Relative one = { 1 }; |
152 | |||
152 | return one; | 153 | return one; |
153 | } | 154 | } |
154 | 155 | ||
@@ -160,6 +161,7 @@ struct GNUNET_TIME_Relative | |||
160 | GNUNET_TIME_relative_get_millisecond_ () | 161 | GNUNET_TIME_relative_get_millisecond_ () |
161 | { | 162 | { |
162 | static struct GNUNET_TIME_Relative one = { 1000 }; | 163 | static struct GNUNET_TIME_Relative one = { 1000 }; |
164 | |||
163 | return one; | 165 | return one; |
164 | } | 166 | } |
165 | 167 | ||
@@ -171,6 +173,7 @@ struct GNUNET_TIME_Relative | |||
171 | GNUNET_TIME_relative_get_second_ () | 173 | GNUNET_TIME_relative_get_second_ () |
172 | { | 174 | { |
173 | static struct GNUNET_TIME_Relative one = { 1000 * 1000LL }; | 175 | static struct GNUNET_TIME_Relative one = { 1000 * 1000LL }; |
176 | |||
174 | return one; | 177 | return one; |
175 | } | 178 | } |
176 | 179 | ||
@@ -182,6 +185,7 @@ struct GNUNET_TIME_Relative | |||
182 | GNUNET_TIME_relative_get_minute_ () | 185 | GNUNET_TIME_relative_get_minute_ () |
183 | { | 186 | { |
184 | static struct GNUNET_TIME_Relative one = { 60 * 1000 * 1000LL }; | 187 | static struct GNUNET_TIME_Relative one = { 60 * 1000 * 1000LL }; |
188 | |||
185 | return one; | 189 | return one; |
186 | } | 190 | } |
187 | 191 | ||
@@ -193,6 +197,7 @@ struct GNUNET_TIME_Relative | |||
193 | GNUNET_TIME_relative_get_hour_ () | 197 | GNUNET_TIME_relative_get_hour_ () |
194 | { | 198 | { |
195 | static struct GNUNET_TIME_Relative one = { 60 * 60 * 1000 * 1000LL }; | 199 | static struct GNUNET_TIME_Relative one = { 60 * 60 * 1000 * 1000LL }; |
200 | |||
196 | return one; | 201 | return one; |
197 | } | 202 | } |
198 | 203 | ||
@@ -204,9 +209,11 @@ struct GNUNET_TIME_Relative | |||
204 | GNUNET_TIME_relative_get_forever_ () | 209 | GNUNET_TIME_relative_get_forever_ () |
205 | { | 210 | { |
206 | static struct GNUNET_TIME_Relative forever = { UINT64_MAX }; | 211 | static struct GNUNET_TIME_Relative forever = { UINT64_MAX }; |
212 | |||
207 | return forever; | 213 | return forever; |
208 | } | 214 | } |
209 | 215 | ||
216 | |||
210 | /** | 217 | /** |
211 | * Return "forever". | 218 | * Return "forever". |
212 | */ | 219 | */ |
@@ -217,6 +224,7 @@ GNUNET_TIME_absolute_get_forever_ () | |||
217 | return forever; | 224 | return forever; |
218 | } | 225 | } |
219 | 226 | ||
227 | |||
220 | /** | 228 | /** |
221 | * Convert relative time to an absolute time in the | 229 | * Convert relative time to an absolute time in the |
222 | * future. | 230 | * future. |
@@ -564,7 +572,6 @@ GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a) | |||
564 | 572 | ||
565 | ret.rel_value_us = GNUNET_ntohll (a.rel_value_us__); | 573 | ret.rel_value_us = GNUNET_ntohll (a.rel_value_us__); |
566 | return ret; | 574 | return ret; |
567 | |||
568 | } | 575 | } |
569 | 576 | ||
570 | 577 | ||