diff options
author | Christian Grothoff <christian@grothoff.org> | 2011-10-11 09:43:04 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2011-10-11 09:43:04 +0000 |
commit | d9d94d0e53d26af75ec8241383d166544ebd79f3 (patch) | |
tree | 9080b73624389403a198257fe0547bb4634e64d2 /src/util/bio.c | |
parent | 2d792ee2e9cc0c993b8907e2c8edb0c2b8465343 (diff) | |
download | gnunet-d9d94d0e53d26af75ec8241383d166544ebd79f3.tar.gz gnunet-d9d94d0e53d26af75ec8241383d166544ebd79f3.zip |
converting to GNUNET_LOG_from*
Diffstat (limited to 'src/util/bio.c')
-rw-r--r-- | src/util/bio.c | 255 |
1 files changed, 132 insertions, 123 deletions
diff --git a/src/util/bio.c b/src/util/bio.c index 404b7aa3b..9c2b9d0dd 100644 --- a/src/util/bio.c +++ b/src/util/bio.c | |||
@@ -26,6 +26,8 @@ | |||
26 | #include "gnunet_bio_lib.h" | 26 | #include "gnunet_bio_lib.h" |
27 | #include "gnunet_disk_lib.h" | 27 | #include "gnunet_disk_lib.h" |
28 | 28 | ||
29 | #define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__) | ||
30 | |||
29 | #define BIO_BUFFER_SIZE 65536 | 31 | #define BIO_BUFFER_SIZE 65536 |
30 | 32 | ||
31 | #define MAX_META_DATA (1024 * 1024) | 33 | #define MAX_META_DATA (1024 * 1024) |
@@ -56,7 +58,8 @@ GNUNET_BIO_read_open (const char *fn) | |||
56 | struct GNUNET_DISK_FileHandle *fd; | 58 | struct GNUNET_DISK_FileHandle *fd; |
57 | struct GNUNET_BIO_ReadHandle *h; | 59 | struct GNUNET_BIO_ReadHandle *h; |
58 | 60 | ||
59 | fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); | 61 | fd = |
62 | GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); | ||
60 | if (NULL == fd) | 63 | if (NULL == fd) |
61 | return NULL; | 64 | return NULL; |
62 | h = GNUNET_malloc (sizeof (struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE); | 65 | h = GNUNET_malloc (sizeof (struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE); |
@@ -102,7 +105,7 @@ GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg) | |||
102 | */ | 105 | */ |
103 | int | 106 | int |
104 | GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what, | 107 | GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what, |
105 | void *result, size_t len) | 108 | void *result, size_t len) |
106 | { | 109 | { |
107 | char *dst = result; | 110 | char *dst = result; |
108 | size_t min; | 111 | size_t min; |
@@ -113,38 +116,38 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what, | |||
113 | return GNUNET_SYSERR; | 116 | return GNUNET_SYSERR; |
114 | pos = 0; | 117 | pos = 0; |
115 | do | 118 | do |
116 | { | ||
117 | /* first, use buffer */ | ||
118 | min = h->have - h->pos; | ||
119 | if (min > 0) | ||
120 | { | ||
121 | if (min > len - pos) | ||
122 | min = len - pos; | ||
123 | memcpy (&dst[pos], &h->buffer[h->pos], min); | ||
124 | h->pos += min; | ||
125 | pos += min; | ||
126 | } | ||
127 | if (pos == len) | ||
128 | return GNUNET_OK; /* done! */ | ||
129 | GNUNET_assert (h->have == h->pos); | ||
130 | /* fill buffer */ | ||
131 | ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size); | ||
132 | if (ret == -1) | ||
133 | { | ||
134 | GNUNET_asprintf (&h->emsg, _("Error reading `%s': %s"), what, | ||
135 | STRERROR (errno)); | ||
136 | return GNUNET_SYSERR; | ||
137 | } | ||
138 | if (ret == 0) | ||
139 | { | 119 | { |
140 | GNUNET_asprintf (&h->emsg, _("Error reading `%s': %s"), what, | 120 | /* first, use buffer */ |
141 | _("End of file")); | 121 | min = h->have - h->pos; |
142 | return GNUNET_SYSERR; | 122 | if (min > 0) |
123 | { | ||
124 | if (min > len - pos) | ||
125 | min = len - pos; | ||
126 | memcpy (&dst[pos], &h->buffer[h->pos], min); | ||
127 | h->pos += min; | ||
128 | pos += min; | ||
129 | } | ||
130 | if (pos == len) | ||
131 | return GNUNET_OK; /* done! */ | ||
132 | GNUNET_assert (h->have == h->pos); | ||
133 | /* fill buffer */ | ||
134 | ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size); | ||
135 | if (ret == -1) | ||
136 | { | ||
137 | GNUNET_asprintf (&h->emsg, _("Error reading `%s': %s"), what, | ||
138 | STRERROR (errno)); | ||
139 | return GNUNET_SYSERR; | ||
140 | } | ||
141 | if (ret == 0) | ||
142 | { | ||
143 | GNUNET_asprintf (&h->emsg, _("Error reading `%s': %s"), what, | ||
144 | _("End of file")); | ||
145 | return GNUNET_SYSERR; | ||
146 | } | ||
147 | h->pos = 0; | ||
148 | h->have = ret; | ||
143 | } | 149 | } |
144 | h->pos = 0; | 150 | while (pos < len); /* should always be true */ |
145 | h->have = ret; | ||
146 | } | ||
147 | while (pos < len); /* should always be true */ | ||
148 | return GNUNET_OK; | 151 | return GNUNET_OK; |
149 | } | 152 | } |
150 | 153 | ||
@@ -160,8 +163,8 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what, | |||
160 | * @return GNUNET_OK on success, GNUNET_SYSERR on failure | 163 | * @return GNUNET_OK on success, GNUNET_SYSERR on failure |
161 | */ | 164 | */ |
162 | int | 165 | int |
163 | GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, const char *file, int line, | 166 | GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, const char *file, |
164 | void *result, size_t len) | 167 | int line, void *result, size_t len) |
165 | { | 168 | { |
166 | char what[1024]; | 169 | char what[1024]; |
167 | 170 | ||
@@ -182,39 +185,41 @@ GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, const char *file, int line, | |||
182 | */ | 185 | */ |
183 | int | 186 | int |
184 | GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what, | 187 | GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what, |
185 | char **result, size_t maxLen) | 188 | char **result, size_t maxLen) |
186 | { | 189 | { |
187 | char *buf; | 190 | char *buf; |
188 | uint32_t big; | 191 | uint32_t big; |
189 | 192 | ||
190 | if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big)) | 193 | if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big)) |
191 | { | 194 | { |
192 | GNUNET_free_non_null (h->emsg); | 195 | GNUNET_free_non_null (h->emsg); |
193 | GNUNET_asprintf (&h->emsg, _("Error reading length of string `%s'"), what); | 196 | GNUNET_asprintf (&h->emsg, _("Error reading length of string `%s'"), |
194 | return GNUNET_SYSERR; | 197 | what); |
195 | } | 198 | return GNUNET_SYSERR; |
199 | } | ||
196 | if (big == 0) | 200 | if (big == 0) |
197 | { | 201 | { |
198 | *result = NULL; | 202 | *result = NULL; |
199 | return GNUNET_OK; | 203 | return GNUNET_OK; |
200 | } | 204 | } |
201 | if (big > maxLen) | 205 | if (big > maxLen) |
202 | { | 206 | { |
203 | GNUNET_asprintf (&h->emsg, _("String `%s' longer than allowed (%u > %u)"), | 207 | GNUNET_asprintf (&h->emsg, |
204 | what, big, maxLen); | 208 | _("String `%s' longer than allowed (%u > %u)"), what, |
205 | return GNUNET_SYSERR; | 209 | big, maxLen); |
206 | } | 210 | return GNUNET_SYSERR; |
211 | } | ||
207 | buf = GNUNET_malloc (big); | 212 | buf = GNUNET_malloc (big); |
208 | *result = buf; | 213 | *result = buf; |
209 | buf[--big] = '\0'; | 214 | buf[--big] = '\0'; |
210 | if (big == 0) | 215 | if (big == 0) |
211 | return GNUNET_OK; | 216 | return GNUNET_OK; |
212 | if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big)) | 217 | if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big)) |
213 | { | 218 | { |
214 | GNUNET_free (buf); | 219 | GNUNET_free (buf); |
215 | *result = NULL; | 220 | *result = NULL; |
216 | return GNUNET_SYSERR; | 221 | return GNUNET_SYSERR; |
217 | } | 222 | } |
218 | return GNUNET_OK; | 223 | return GNUNET_OK; |
219 | } | 224 | } |
220 | 225 | ||
@@ -229,7 +234,7 @@ GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what, | |||
229 | */ | 234 | */ |
230 | int | 235 | int |
231 | GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what, | 236 | GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what, |
232 | struct GNUNET_CONTAINER_MetaData **result) | 237 | struct GNUNET_CONTAINER_MetaData **result) |
233 | { | 238 | { |
234 | uint32_t size; | 239 | uint32_t size; |
235 | char *buf; | 240 | char *buf; |
@@ -238,30 +243,32 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what, | |||
238 | if (GNUNET_BIO_read_int32 (h, (int32_t *) & size) != GNUNET_OK) | 243 | if (GNUNET_BIO_read_int32 (h, (int32_t *) & size) != GNUNET_OK) |
239 | return GNUNET_SYSERR; | 244 | return GNUNET_SYSERR; |
240 | if (size == 0) | 245 | if (size == 0) |
241 | { | 246 | { |
242 | *result = NULL; | 247 | *result = NULL; |
243 | return GNUNET_OK; | 248 | return GNUNET_OK; |
244 | } | 249 | } |
245 | if (size > MAX_META_DATA) | 250 | if (size > MAX_META_DATA) |
246 | { | 251 | { |
247 | GNUNET_asprintf (&h->emsg, | 252 | GNUNET_asprintf (&h->emsg, |
248 | _("Serialized metadata `%s' larger than allowed (%u>%u)"), | 253 | _ |
249 | what, size, MAX_META_DATA); | 254 | ("Serialized metadata `%s' larger than allowed (%u>%u)"), |
250 | return GNUNET_SYSERR; | 255 | what, size, MAX_META_DATA); |
251 | } | 256 | return GNUNET_SYSERR; |
257 | } | ||
252 | buf = GNUNET_malloc (size); | 258 | buf = GNUNET_malloc (size); |
253 | if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size)) | 259 | if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size)) |
254 | { | 260 | { |
255 | GNUNET_free (buf); | 261 | GNUNET_free (buf); |
256 | return GNUNET_SYSERR; | 262 | return GNUNET_SYSERR; |
257 | } | 263 | } |
258 | meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size); | 264 | meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size); |
259 | if (meta == NULL) | 265 | if (meta == NULL) |
260 | { | 266 | { |
261 | GNUNET_free (buf); | 267 | GNUNET_free (buf); |
262 | GNUNET_asprintf (&h->emsg, _("Metadata `%s' failed to deserialize"), what); | 268 | GNUNET_asprintf (&h->emsg, _("Metadata `%s' failed to deserialize"), |
263 | return GNUNET_SYSERR; | 269 | what); |
264 | } | 270 | return GNUNET_SYSERR; |
271 | } | ||
265 | GNUNET_free (buf); | 272 | GNUNET_free (buf); |
266 | *result = meta; | 273 | *result = meta; |
267 | return GNUNET_OK; | 274 | return GNUNET_OK; |
@@ -279,7 +286,7 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what, | |||
279 | */ | 286 | */ |
280 | int | 287 | int |
281 | GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file, | 288 | GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file, |
282 | int line, int32_t * i) | 289 | int line, int32_t * i) |
283 | { | 290 | { |
284 | int32_t big; | 291 | int32_t big; |
285 | 292 | ||
@@ -301,7 +308,7 @@ GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file, | |||
301 | */ | 308 | */ |
302 | int | 309 | int |
303 | GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, const char *file, | 310 | GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, const char *file, |
304 | int line, int64_t * i) | 311 | int line, int64_t * i) |
305 | { | 312 | { |
306 | int64_t big; | 313 | int64_t big; |
307 | 314 | ||
@@ -337,13 +344,15 @@ GNUNET_BIO_write_open (const char *fn) | |||
337 | struct GNUNET_BIO_WriteHandle *h; | 344 | struct GNUNET_BIO_WriteHandle *h; |
338 | 345 | ||
339 | fd = GNUNET_DISK_file_open (fn, | 346 | fd = GNUNET_DISK_file_open (fn, |
340 | GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | 347 | GNUNET_DISK_OPEN_WRITE | |
341 | | GNUNET_DISK_OPEN_CREATE, | 348 | GNUNET_DISK_OPEN_TRUNCATE | |
342 | GNUNET_DISK_PERM_USER_READ | | 349 | GNUNET_DISK_OPEN_CREATE, |
343 | GNUNET_DISK_PERM_USER_WRITE); | 350 | GNUNET_DISK_PERM_USER_READ | |
351 | GNUNET_DISK_PERM_USER_WRITE); | ||
344 | if (NULL == fd) | 352 | if (NULL == fd) |
345 | return NULL; | 353 | return NULL; |
346 | h = GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE); | 354 | h = |
355 | GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE); | ||
347 | h->buffer = (char *) &h[1]; | 356 | h->buffer = (char *) &h[1]; |
348 | h->size = BIO_BUFFER_SIZE; | 357 | h->size = BIO_BUFFER_SIZE; |
349 | h->fd = fd; | 358 | h->fd = fd; |
@@ -365,18 +374,18 @@ GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h) | |||
365 | int ret; | 374 | int ret; |
366 | 375 | ||
367 | if (NULL == h->fd) | 376 | if (NULL == h->fd) |
368 | { | 377 | { |
369 | ret = GNUNET_SYSERR; | ||
370 | } | ||
371 | else | ||
372 | { | ||
373 | wrt = GNUNET_DISK_file_write (h->fd, h->buffer, h->have); | ||
374 | if (wrt == h->have) | ||
375 | ret = GNUNET_OK; | ||
376 | else | ||
377 | ret = GNUNET_SYSERR; | 378 | ret = GNUNET_SYSERR; |
378 | GNUNET_DISK_file_close (h->fd); | 379 | } |
379 | } | 380 | else |
381 | { | ||
382 | wrt = GNUNET_DISK_file_write (h->fd, h->buffer, h->have); | ||
383 | if (wrt == h->have) | ||
384 | ret = GNUNET_OK; | ||
385 | else | ||
386 | ret = GNUNET_SYSERR; | ||
387 | GNUNET_DISK_file_close (h->fd); | ||
388 | } | ||
380 | GNUNET_free (h); | 389 | GNUNET_free (h); |
381 | return ret; | 390 | return ret; |
382 | } | 391 | } |
@@ -392,7 +401,7 @@ GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h) | |||
392 | */ | 401 | */ |
393 | int | 402 | int |
394 | GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer, | 403 | GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer, |
395 | size_t n) | 404 | size_t n) |
396 | { | 405 | { |
397 | const char *src = buffer; | 406 | const char *src = buffer; |
398 | size_t min; | 407 | size_t min; |
@@ -403,27 +412,27 @@ GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer, | |||
403 | return GNUNET_SYSERR; | 412 | return GNUNET_SYSERR; |
404 | pos = 0; | 413 | pos = 0; |
405 | do | 414 | do |
406 | { | ||
407 | /* first, just use buffer */ | ||
408 | min = h->size - h->have; | ||
409 | if (min > n - pos) | ||
410 | min = n - pos; | ||
411 | memcpy (&h->buffer[h->have], &src[pos], min); | ||
412 | pos += min; | ||
413 | h->have += min; | ||
414 | if (pos == n) | ||
415 | return GNUNET_OK; /* done */ | ||
416 | GNUNET_assert (h->have == h->size); | ||
417 | ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->size); | ||
418 | if (ret != h->size) | ||
419 | { | 415 | { |
420 | GNUNET_DISK_file_close (h->fd); | 416 | /* first, just use buffer */ |
421 | h->fd = NULL; | 417 | min = h->size - h->have; |
422 | return GNUNET_SYSERR; /* error */ | 418 | if (min > n - pos) |
419 | min = n - pos; | ||
420 | memcpy (&h->buffer[h->have], &src[pos], min); | ||
421 | pos += min; | ||
422 | h->have += min; | ||
423 | if (pos == n) | ||
424 | return GNUNET_OK; /* done */ | ||
425 | GNUNET_assert (h->have == h->size); | ||
426 | ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->size); | ||
427 | if (ret != h->size) | ||
428 | { | ||
429 | GNUNET_DISK_file_close (h->fd); | ||
430 | h->fd = NULL; | ||
431 | return GNUNET_SYSERR; /* error */ | ||
432 | } | ||
433 | h->have = 0; | ||
423 | } | 434 | } |
424 | h->have = 0; | 435 | while (pos < n); /* should always be true */ |
425 | } | ||
426 | while (pos < n); /* should always be true */ | ||
427 | GNUNET_break (0); | 436 | GNUNET_break (0); |
428 | return GNUNET_OK; | 437 | return GNUNET_OK; |
429 | } | 438 | } |
@@ -459,7 +468,7 @@ GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, const char *s) | |||
459 | */ | 468 | */ |
460 | int | 469 | int |
461 | GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h, | 470 | GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h, |
462 | const struct GNUNET_CONTAINER_MetaData *m) | 471 | const struct GNUNET_CONTAINER_MetaData *m) |
463 | { | 472 | { |
464 | ssize_t size; | 473 | ssize_t size; |
465 | char *buf; | 474 | char *buf; |
@@ -468,19 +477,19 @@ GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h, | |||
468 | return GNUNET_BIO_write_int32 (h, 0); | 477 | return GNUNET_BIO_write_int32 (h, 0); |
469 | buf = NULL; | 478 | buf = NULL; |
470 | size = | 479 | size = |
471 | GNUNET_CONTAINER_meta_data_serialize (m, &buf, MAX_META_DATA, | 480 | GNUNET_CONTAINER_meta_data_serialize (m, &buf, MAX_META_DATA, |
472 | GNUNET_CONTAINER_META_DATA_SERIALIZE_PART); | 481 | GNUNET_CONTAINER_META_DATA_SERIALIZE_PART); |
473 | if (size == -1) | 482 | if (size == -1) |
474 | { | 483 | { |
475 | GNUNET_free (buf); | 484 | GNUNET_free (buf); |
476 | return GNUNET_SYSERR; | 485 | return GNUNET_SYSERR; |
477 | } | 486 | } |
478 | if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) || | 487 | if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) || |
479 | (GNUNET_OK != GNUNET_BIO_write (h, buf, size))) | 488 | (GNUNET_OK != GNUNET_BIO_write (h, buf, size))) |
480 | { | 489 | { |
481 | GNUNET_free (buf); | 490 | GNUNET_free (buf); |
482 | return GNUNET_SYSERR; | 491 | return GNUNET_SYSERR; |
483 | } | 492 | } |
484 | GNUNET_free (buf); | 493 | GNUNET_free (buf); |
485 | return GNUNET_OK; | 494 | return GNUNET_OK; |
486 | } | 495 | } |