X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Futil%2Fbio.c;h=059638ff1f492414c25a1ac1db9abe65dbe7aab9;hb=0ea8e006d5f5ef84e31e000607bd24a23f8fc1ed;hp=060b6f94b8f13f44f01affd454d0d1c988f16b93;hpb=da7cc55488f90b3294dbb7aa186184e6e8501d7e;p=oweals%2Fgnunet.git diff --git a/src/util/bio.c b/src/util/bio.c index 060b6f94b..059638ff1 100644 --- a/src/util/bio.c +++ b/src/util/bio.c @@ -17,8 +17,6 @@ Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - - /** * @file util/bio.c * @brief functions for buffering IO @@ -26,13 +24,23 @@ */ #include "platform.h" #include "gnunet_bio_lib.h" +#include "gnunet_disk_lib.h" + +#define BIO_BUFFER_SIZE 65536 +#define MAX_META_DATA (1024 * 1024) /** * Handle for buffered reading. */ struct GNUNET_BIO_ReadHandle { + struct GNUNET_DISK_FileHandle *fd; + char *emsg; + char *buffer; + size_t have; + size_t size; + off_t pos; }; @@ -42,9 +50,21 @@ struct GNUNET_BIO_ReadHandle * @param fn file name to be opened * @return IO handle on success, NULL on error */ -struct GNUNET_BIO_ReadHandle *GNUNET_BIO_read_open (const char *fn) +struct GNUNET_BIO_ReadHandle * +GNUNET_BIO_read_open (const char *fn) { - return NULL; + struct GNUNET_DISK_FileHandle *fd; + struct GNUNET_BIO_ReadHandle *h; + + fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, + GNUNET_DISK_PERM_NONE); + if (NULL == fd) + return NULL; + h = GNUNET_malloc (sizeof (struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE); + h->buffer = (char *) &h[1]; + h->size = BIO_BUFFER_SIZE; + h->fd = fd; + return h; } @@ -56,10 +76,13 @@ struct GNUNET_BIO_ReadHandle *GNUNET_BIO_read_open (const char *fn) * @param emsg set to the error message * @return GNUNET_OK on success, GNUNET_SYSERR otherwise */ -int GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, - char **emsg) +int +GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg) { - return GNUNET_SYSERR; + *emsg = h->emsg; + GNUNET_DISK_file_close (h->fd); + GNUNET_free (h); + return (NULL == *emsg) ? GNUNET_OK : GNUNET_SYSERR; } @@ -70,13 +93,80 @@ int GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, * @param what describes what is being read (for error message creation) * @param result the buffer to write the result to * @param len the number of bytes to read - * @return len on success, GNUNET_SYSERR on failure + * @return GNUNET_OK on success, GNUNET_SYSERR on failure + */ +int +GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, + const char *what, void *result, size_t len) +{ + char *dst = result; + size_t min; + size_t pos; + ssize_t ret; + + if (h->emsg != NULL) + return GNUNET_SYSERR; + pos = 0; + do + { + /* first, use buffer */ + min = h->have - h->pos; + if (min > 0) + { + if (min > len - pos) + min = len - pos; + memcpy (&dst[pos], &h->buffer[h->pos], min); + h->pos += min; + pos += min; + } + if (pos == len) + return GNUNET_OK; /* done! */ + GNUNET_assert (h->have == h->pos); + /* fill buffer */ + ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size); + if (ret == -1) + { + GNUNET_asprintf (&h->emsg, + _("Error reading `%s': %s"), + what, STRERROR (errno)); + return GNUNET_SYSERR; + } + if (ret == 0) + { + GNUNET_asprintf (&h->emsg, + _("Error reading `%s': %s"), + what, _("End of file")); + return GNUNET_SYSERR; + } + h->pos = 0; + h->have = ret; + } + while (pos < len); /* should always be true */ + return GNUNET_OK; +} + + +/** + * Read the contents of a binary file into a buffer. + * + * @param h handle to an open file + * @param file name of the source file + * @param line line number in the source file + * @param result the buffer to write the result to + * @param len the number of bytes to read + * @return GNUNET_OK on success, GNUNET_SYSERR on failure */ -ssize_t GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, - const char *what, - void *result, - size_t len) +int GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, + const char *file, int line, + void *result, + size_t len) { + char what[1024]; + GNUNET_snprintf (what, + sizeof(what), + "%s:%d", + file, line); + return GNUNET_BIO_read (h, what, result, len); } @@ -87,12 +177,48 @@ ssize_t GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, * @param what describes what is being read (for error message creation) * @param result the buffer to store a pointer to the (allocated) string to * (note that *result could be set to NULL as well) + * @param maxLen maximum allowed length for the string * @return GNUNET_OK on success, GNUNET_SYSERR on failure */ -int GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, - const char *what, - char **result) +int +GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, + const char *what, char **result, size_t maxLen) { + char *buf; + uint32_t big; + + if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big)) + { + GNUNET_free_non_null (h->emsg); + GNUNET_asprintf (&h->emsg, + _("Error reading length of string `%s'"), + what); + return GNUNET_SYSERR; + } + if (big == 0) + { + *result = NULL; + return GNUNET_OK; + } + if (big > maxLen) + { + GNUNET_asprintf (&h->emsg, + _("String `%s' longer than allowed (%u > %u)"), + what, big, maxLen); + return GNUNET_SYSERR; + } + buf = GNUNET_malloc (big); + *result = buf; + buf[--big] = '\0'; + if (big == 0) + return GNUNET_OK; + if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big)) + { + GNUNET_free (buf); + *result = NULL; + return GNUNET_SYSERR; + } + return GNUNET_OK; } @@ -104,10 +230,46 @@ int GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, * @param result the buffer to store a pointer to the (allocated) metadata * @return GNUNET_OK on success, GNUNET_SYSERR on failure */ -int GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, - const char *what, - struct GNUNET_CONTAINER_MetaData **result) +int +GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, + const char *what, + struct GNUNET_CONTAINER_MetaData **result) { + uint32_t size; + char *buf; + struct GNUNET_CONTAINER_MetaData *meta; + + if (GNUNET_BIO_read_int32 (h, (int32_t *) &size) != GNUNET_OK) + return GNUNET_SYSERR; + if (size == 0) + { + *result = NULL; + return GNUNET_OK; + } + if (size > MAX_META_DATA) + { + GNUNET_asprintf (&h->emsg, + _("Serialized metadata `%s' larger than allowed (%u>%u)"), + what, size, MAX_META_DATA); + return GNUNET_SYSERR; + } + buf = GNUNET_malloc (size); + if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size)) + { + GNUNET_free (buf); + return GNUNET_SYSERR; + } + meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size); + if (meta == NULL) + { + GNUNET_free (buf); + GNUNET_asprintf (&h->emsg, + _("Metadata `%s' failed to deserialize"), what); + return GNUNET_SYSERR; + } + GNUNET_free (buf); + *result = meta; + return GNUNET_OK; } @@ -115,32 +277,59 @@ int GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, * Read an (u)int32_t. * * @param h hande to open file - * @param what describes what is being read (for error message creation) + * @param file name of the source file + * @param line line number in the source file * @param i address of 32-bit integer to read * @return GNUNET_OK on success, GNUNET_SYSERR on error - */ -int GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, - const char *what, - int32_t *i); + */ +int +GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, + const char *file, + int line, + int32_t * i) +{ + int32_t big; + + if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int32_t))) + return GNUNET_SYSERR; + *i = ntohl (big); + return GNUNET_OK; +} /** * Read an (u)int64_t. * * @param h hande to open file - * @param what describes what is being read (for error message creation) + * @param file name of the source file + * @param line line number in the source file * @param i address of 64-bit integer to read * @return GNUNET_OK on success, GNUNET_SYSERR on error - */ -int GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, - const char *what, - int64_t *i); + */ +int +GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, + const char *file, + int line, + int64_t * i) +{ + int64_t big; + + if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int64_t))) + return GNUNET_SYSERR; + *i = GNUNET_ntohll (big); + return GNUNET_OK; +} + /** * Handle for buffered writing. */ struct GNUNET_BIO_WriteHandle { + struct GNUNET_DISK_FileHandle *fd; + char *buffer; + size_t have; + size_t size; }; @@ -150,9 +339,27 @@ struct GNUNET_BIO_WriteHandle * @param fn file name to be opened * @return IO handle on success, NULL on error */ -struct GNUNET_BIO_WriteHandle *GNUNET_BIO_write_open (const char *fn) +struct GNUNET_BIO_WriteHandle * +GNUNET_BIO_write_open (const char *fn) { - return NULL; + struct GNUNET_DISK_FileHandle *fd; + struct GNUNET_BIO_WriteHandle *h; + + fd = GNUNET_DISK_file_open (fn, + GNUNET_DISK_OPEN_WRITE | + GNUNET_DISK_OPEN_TRUNCATE | + GNUNET_DISK_OPEN_CREATE, + GNUNET_DISK_PERM_USER_READ | + GNUNET_DISK_PERM_USER_WRITE); + if (NULL == fd) + return NULL; + h = + GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE); + h->buffer = (char *) &h[1]; + h->size = BIO_BUFFER_SIZE; + h->fd = fd; + + return h; } @@ -162,7 +369,28 @@ struct GNUNET_BIO_WriteHandle *GNUNET_BIO_write_open (const char *fn) * @param h file handle * @return GNUNET_OK on success, GNUNET_SYSERR otherwise */ -int GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h); +int +GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h) +{ + ssize_t wrt; + int ret; + + if (NULL == h->fd) + { + ret = GNUNET_SYSERR; + } + else + { + wrt = GNUNET_DISK_file_write (h->fd, h->buffer, h->have); + if (wrt == h->have) + ret = GNUNET_OK; + else + ret = GNUNET_SYSERR; + GNUNET_DISK_file_close (h->fd); + } + GNUNET_free (h); + return ret; +} /** @@ -173,373 +401,133 @@ int GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h); * @param n number of bytes to write * @return GNUNET_OK on success, GNUNET_SYSERR on error */ -ssize_t GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, - const void *buffer, - size_t n); - - -/** - * Write a string to a file. - * - * @param h handle to open file - * @param s string to write (can be NULL) - * @return GNUNET_OK on success, GNUNET_SYSERR on error - */ -int GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, - const char *s); - - - - -/** - * Write metadata container to a file. - * - * @param h handle to open file - * @param m metadata to write - * @return GNUNET_OK on success, GNUNET_SYSERR on error - */ -int GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h, - const struct GNUNET_CONTAINER_MetaData *m); - - - -/** - * Write a float. - * - * @param h hande to open file - * @param f float to write (must be a variable) - */ -#define GNUNET_BIO_write_float(h, f) (sizeof(float) == GNUNET_BIO_write (h, &f, sizeof(float))) - - - -/** - * Write a double. - * - * @param h hande to open file - * @param f double to write (must be a variable) - */ -#define GNUNET_BIO_write_float(h, f) (sizeof(double) == GNUNET_BIO_write (h, &f, sizeof(double))) - - -/** - * Write an (u)int32_t. - * - * @param h hande to open file - * @param i address of 32-bit integer to write - * @return GNUNET_OK on success, GNUNET_SYSERR on error - */ -int GNUNET_BIO_write_int32 (struct GNUNET_BIO_ReadHandle *h, - int32_t i); - - -/** - * Write an (u)int64_t. - * - * @param h hande to open file - * @param i address of 64-bit integer to write - * @return GNUNET_OK on success, GNUNET_SYSERR on error - */ -int GNUNET_BIO_write_int64 (struct GNUNET_BIO_ReadHandle *h, - int64_t i); - - - - - -typedef struct -{ - int fd; - unsigned int have; - unsigned int size; - char *buffer; -} WriteBuffer; - -static void -write_buffered (WriteBuffer * wb, const void *s, unsigned int size) +int +GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, + const void *buffer, size_t n) { - const char *src = s; - unsigned int min; - unsigned int pos; - int ret; + const char *src = buffer; + size_t min; + size_t pos; + ssize_t ret; - if (wb->fd == -1) - return; + if (NULL == h->fd) + return GNUNET_SYSERR; pos = 0; do { /* first, just use buffer */ - min = wb->size - wb->have; - if (min > size - pos) - min = size - pos; - memcpy (&wb->buffer[wb->have], &src[pos], min); + min = h->size - h->have; + if (min > n - pos) + min = n - pos; + memcpy (&h->buffer[h->have], &src[pos], min); pos += min; - wb->have += min; - if (pos == size) - return; /* done */ - GNUNET_GE_ASSERT (NULL, wb->have == wb->size); - ret = WRITE (wb->fd, wb->buffer, wb->size); - if (ret != wb->size) - { - CLOSE (wb->fd); - wb->fd = -1; - return; /* error */ - } - wb->have = 0; - } - while (pos < size); /* should always be true */ -} - - -static void -WRITEINT (WriteBuffer * wb, int val) -{ - int big; - big = htonl (val); - write_buffered (wb, &big, sizeof (int)); -} - -static void -WRITELONG (WriteBuffer * wb, long long val) -{ - long long big; - big = GNUNET_htonll (val); - write_buffered (wb, &big, sizeof (long long)); -} - -static void -writeURI (WriteBuffer * wb, const struct GNUNET_ECRS_URI *uri) -{ - char *buf; - unsigned int size; - - buf = GNUNET_ECRS_uri_to_string (uri); - size = strlen (buf); - WRITEINT (wb, size); - write_buffered (wb, buf, size); - GNUNET_free (buf); -} - -static void -WRITESTRING (WriteBuffer * wb, const char *name) -{ - GNUNET_GE_BREAK (NULL, name != NULL); - WRITEINT (wb, strlen (name)); - write_buffered (wb, name, strlen (name)); -} - -static void -writeMetaData (struct GNUNET_GE_Context *ectx, - WriteBuffer * wb, const struct GNUNET_MetaData *meta) -{ - unsigned int size; - char *buf; - - size = GNUNET_meta_data_get_serialized_size (meta, - GNUNET_SERIALIZE_FULL - | - GNUNET_SERIALIZE_NO_COMPRESS); - if (size > 1024 * 1024) - size = 1024 * 1024; - buf = GNUNET_malloc (size); - GNUNET_meta_data_serialize (ectx, - meta, - buf, - size, - GNUNET_SERIALIZE_PART | - GNUNET_SERIALIZE_NO_COMPRESS); - WRITEINT (wb, size); - write_buffered (wb, buf, size); - GNUNET_free (buf); -} - - -static void -writeFileInfo (struct GNUNET_GE_Context *ectx, WriteBuffer * wb, - const GNUNET_ECRS_FileInfo * fi) -{ - writeMetaData (ectx, wb, fi->meta); - writeURI (wb, fi->uri); -} - - - - -typedef struct -{ - int fd; - unsigned int have; - unsigned int size; - unsigned int pos; - char *buffer; -} ReadBuffer; - -static int -read_buffered (ReadBuffer * rb, void *d, unsigned int size) -{ - char *dst = d; - unsigned int min; - unsigned int pos; - int ret; - - if (rb->fd == -1) - return -1; - pos = 0; - do - { - /* first, use buffer */ - min = rb->have - rb->pos; - if (min > 0) - { - if (min > size - pos) - min = size - pos; - memcpy (&dst[pos], &rb->buffer[rb->pos], min); - rb->pos += min; - pos += min; - } - if (pos == size) - return pos; /* done! */ - GNUNET_GE_ASSERT (NULL, rb->have == rb->pos); - /* fill buffer */ - ret = READ (rb->fd, rb->buffer, rb->size); - if (ret == -1) + h->have += min; + if (pos == n) + return GNUNET_OK; /* done */ + GNUNET_assert (h->have == h->size); + ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->size); + if (ret != h->size) { - CLOSE (rb->fd); - rb->fd = -1; - return -1; + GNUNET_DISK_file_close (h->fd); + h->fd = NULL; + return GNUNET_SYSERR; /* error */ } - if (ret == 0) - return 0; - rb->pos = 0; - rb->have = ret; + h->have = 0; } - while (pos < size); /* should always be true */ - return pos; -} - - -static int -read_int (ReadBuffer * rb, int *val) -{ - int big; - - if (sizeof (int) != read_buffered (rb, &big, sizeof (int))) - return GNUNET_SYSERR; - *val = ntohl (big); + while (pos < n); /* should always be true */ + GNUNET_break (0); return GNUNET_OK; } -static unsigned int -read_uint (ReadBuffer * rb, unsigned int *val) -{ - unsigned int big; - if (sizeof (unsigned int) != - read_buffered (rb, &big, sizeof (unsigned int))) - return GNUNET_SYSERR; - *val = ntohl (big); - return GNUNET_OK; -} - -#define READINT(a) if (GNUNET_OK != read_int(rb, (int*) &a)) return GNUNET_SYSERR; - -static int -read_long (ReadBuffer * rb, long long *val) +/** + * Write a string to a file. + * + * @param h handle to open file + * @param s string to write (can be NULL) + * @return GNUNET_OK on success, GNUNET_SYSERR on error + */ +int +GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, const char *s) { - long long big; + uint32_t slen; - if (sizeof (long long) != read_buffered (rb, &big, sizeof (long long))) + slen = (uint32_t) ((s == NULL) ? 0 : strlen (s) + 1); + if (GNUNET_OK != GNUNET_BIO_write_int32 (h, slen)) return GNUNET_SYSERR; - *val = GNUNET_ntohll (big); + if (0 != slen) + return GNUNET_BIO_write (h, s, slen - 1); return GNUNET_OK; } -#define READLONG(a) if (GNUNET_OK != read_long(rb, (long long*) &a)) return GNUNET_SYSERR; -static struct GNUNET_ECRS_URI * -read_uri (struct GNUNET_GE_Context *ectx, ReadBuffer * rb) +/** + * Write metadata container to a file. + * + * @param h handle to open file + * @param m metadata to write + * @return GNUNET_OK on success, GNUNET_SYSERR on error + */ +int +GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h, + const struct GNUNET_CONTAINER_MetaData *m) { + ssize_t size; char *buf; - struct GNUNET_ECRS_URI *ret; - unsigned int size; - - if (GNUNET_OK != read_uint (rb, &size)) - return NULL; - buf = GNUNET_malloc (size + 1); - buf[size] = '\0'; - if (size != read_buffered (rb, buf, size)) + + if (m == NULL) + return GNUNET_BIO_write_int32 (h, 0); + buf = NULL; + size = GNUNET_CONTAINER_meta_data_serialize (m, + &buf, + MAX_META_DATA, + GNUNET_CONTAINER_META_DATA_SERIALIZE_PART); + if (size == -1) + { + GNUNET_free (buf); + return GNUNET_SYSERR; + } + if ( (GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) || + (GNUNET_OK != GNUNET_BIO_write (h, buf, size)) ) { GNUNET_free (buf); - return NULL; + return GNUNET_SYSERR; } - ret = GNUNET_ECRS_string_to_uri (ectx, buf); - GNUNET_GE_BREAK (ectx, ret != NULL); GNUNET_free (buf); - return ret; + return GNUNET_OK; } -#define READURI(u) if (NULL == (u = read_uri(ectx, rb))) return GNUNET_SYSERR; -static char * -read_string (ReadBuffer * rb, unsigned int maxLen) +/** + * Write an (u)int32_t. + * + * @param h hande to open file + * @param i address of 32-bit integer to write + * @return GNUNET_OK on success, GNUNET_SYSERR on error + */ +int +GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, int32_t i) { - char *buf; - unsigned int big; - - if (GNUNET_OK != read_uint (rb, &big)) - return NULL; - if (big > maxLen) - return NULL; - buf = GNUNET_malloc (big + 1); - buf[big] = '\0'; - if (big != read_buffered (rb, buf, big)) - { - GNUNET_free (buf); - return NULL; - } - return buf; + int32_t big; + big = htonl (i); + return GNUNET_BIO_write (h, &big, sizeof (int32_t)); } -#define READSTRING(c, max) if (NULL == (c = read_string(rb, max))) return GNUNET_SYSERR; /** - * Read file info from file. + * Write an (u)int64_t. * + * @param h hande to open file + * @param i address of 64-bit integer to write * @return GNUNET_OK on success, GNUNET_SYSERR on error */ -static struct GNUNET_MetaData * -read_meta (struct GNUNET_GE_Context *ectx, ReadBuffer * rb) +int +GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, int64_t i) { - unsigned int size; - char *buf; - struct GNUNET_MetaData *meta; - - if (read_uint (rb, &size) != GNUNET_OK) - { - GNUNET_GE_BREAK (ectx, 0); - return NULL; - } - if (size > 1024 * 1024) - { - GNUNET_GE_BREAK (ectx, 0); - return NULL; - } - buf = GNUNET_malloc (size); - if (size != read_buffered (rb, buf, size)) - { - GNUNET_free (buf); - GNUNET_GE_BREAK (ectx, 0); - return NULL; - } - meta = GNUNET_meta_data_deserialize (ectx, buf, size); - if (meta == NULL) - { - GNUNET_free (buf); - GNUNET_GE_BREAK (ectx, 0); - return NULL; - } - GNUNET_free (buf); - return meta; + int64_t big; + big = GNUNET_htonll (i); + return GNUNET_BIO_write (h, &big, sizeof (int64_t)); } + /* end of bio.c */