GNUnet  0.19.4
bio.c File Reference

functions for buffering IO More...

#include "platform.h"
#include "gnunet_util_lib.h"
Include dependency graph for bio.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_BIO_ReadHandle
 Handle for buffered reading. More...
 
struct  GNUNET_BIO_WriteHandle
 Handle for buffered writing. More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "util-bio", __VA_ARGS__)
 
#define PATH_MAX   4096
 Assumed maximum path length (for source file names). More...
 
#define BIO_BUFFER_SIZE   65536
 Size for I/O buffers. More...
 

Enumerations

enum  IOType { IO_FILE = 0 , IO_BUFFER }
 Enum used internally to know how buffering is handled. More...
 

Functions

struct GNUNET_BIO_ReadHandleGNUNET_BIO_read_open_file (const char *fn)
 Open a file for reading. More...
 
struct GNUNET_BIO_ReadHandleGNUNET_BIO_read_open_buffer (void *buffer, size_t size)
 Create a handle from an existing allocated buffer. More...
 
int GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg)
 Close an open handle. More...
 
void GNUNET_BIO_read_set_error (struct GNUNET_BIO_ReadHandle *h, const char *emsg)
 Set read error to handle. More...
 
static int read_from_file (struct GNUNET_BIO_ReadHandle *h, const char *what, char *result, size_t len)
 Function used internally to read the contents of a file into a buffer. More...
 
static int read_from_buffer (struct GNUNET_BIO_ReadHandle *h, const char *what, char *result, size_t len)
 Function used internally to read the content of a buffer into a buffer. More...
 
int GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what, void *result, size_t len)
 Read some contents into a buffer. More...
 
int GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what, char **result, size_t max_length)
 Read 0-terminated string. More...
 
int GNUNET_BIO_read_float (struct GNUNET_BIO_ReadHandle *h, const char *what, float *f)
 Read a float. More...
 
int GNUNET_BIO_read_double (struct GNUNET_BIO_ReadHandle *h, const char *what, double *f)
 Read a double. More...
 
int GNUNET_BIO_read_int32 (struct GNUNET_BIO_ReadHandle *h, const char *what, int32_t *i)
 Read an (u)int32_t. More...
 
int GNUNET_BIO_read_int64 (struct GNUNET_BIO_ReadHandle *h, const char *what, int64_t *i)
 Read an (u)int64_t. More...
 
struct GNUNET_BIO_WriteHandleGNUNET_BIO_write_open_file (const char *fn)
 Open a file for writing. More...
 
struct GNUNET_BIO_WriteHandleGNUNET_BIO_write_open_buffer (void)
 Create a handle backed by an in-memory buffer. More...
 
enum GNUNET_GenericReturnValue GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h, char **emsg)
 Close an IO handle. More...
 
enum GNUNET_GenericReturnValue GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h)
 Force a file-based buffered writer to flush its buffer. More...
 
enum GNUNET_GenericReturnValue GNUNET_BIO_get_buffer_contents (struct GNUNET_BIO_WriteHandle *h, char **emsg, void **contents, size_t *size)
 Get the IO handle's contents. More...
 
static enum GNUNET_GenericReturnValue write_to_file (struct GNUNET_BIO_WriteHandle *h, const char *what, const char *source, size_t len)
 Function used internally to write the contents of a buffer into a file. More...
 
static int write_to_buffer (struct GNUNET_BIO_WriteHandle *h, const char *what, const char *source, size_t len)
 Function used internally to write the contents of a buffer to another buffer. More...
 
int GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const char *what, const void *buffer, size_t n)
 Write a buffer to a handle. More...
 
int GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, const char *what, const char *s)
 Write a 0-terminated string. More...
 
int GNUNET_BIO_write_float (struct GNUNET_BIO_WriteHandle *h, const char *what, float f)
 Write a float. More...
 
int GNUNET_BIO_write_double (struct GNUNET_BIO_WriteHandle *h, const char *what, double f)
 Write a double. More...
 
int GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, const char *what, int32_t i)
 Write an (u)int32_t. More...
 
int GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, const char *what, int64_t i)
 Write an (u)int64_t. More...
 
static int read_spec_handler_object (void *cls, struct GNUNET_BIO_ReadHandle *h, const char *what, void *target, size_t target_size)
 Function used internally to read some bytes from within a read spec. More...
 
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_object (const char *what, void *result, size_t len)
 Create the specification to read a certain amount of bytes. More...
 
static int read_spec_handler_string (void *cls, struct GNUNET_BIO_ReadHandle *h, const char *what, void *target, size_t target_size)
 Function used internally to read a string from within a read spec. More...
 
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_string (const char *what, char **result, size_t max_length)
 Create the specification to read a 0-terminated string. More...
 
static int read_spec_handler_int32 (void *cls, struct GNUNET_BIO_ReadHandle *h, const char *what, void *target, size_t target_size)
 Function used internally to read an (u)int32_t from within a read spec. More...
 
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_int32 (const char *what, int32_t *i)
 Create the specification to read an (u)int32_t. More...
 
static int read_spec_handler_int64 (void *cls, struct GNUNET_BIO_ReadHandle *h, const char *what, void *target, size_t target_size)
 Function used internally to read an (u)int64_t from within a read spec. More...
 
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_int64 (const char *what, int64_t *i)
 Create the specification to read an (u)int64_t. More...
 
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_float (const char *what, float *f)
 Create the specification to read a float. More...
 
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_double (const char *what, double *f)
 Create the specification to read a double. More...
 
int GNUNET_BIO_read_spec_commit (struct GNUNET_BIO_ReadHandle *h, struct GNUNET_BIO_ReadSpec *rs)
 Execute the read specifications in order. More...
 
static int write_spec_handler_object (void *cls, struct GNUNET_BIO_WriteHandle *h, const char *what, void *source, size_t source_size)
 Function used internally to write some bytes from within a write spec. More...
 
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_object (const char *what, void *source, size_t size)
 Create the specification to read some bytes. More...
 
static int write_spec_handler_string (void *cls, struct GNUNET_BIO_WriteHandle *h, const char *what, void *source, size_t source_size)
 Function used internally to write a 0-terminated string from within a write spec. More...
 
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_string (const char *what, const char *s)
 Create the specification to write a 0-terminated string. More...
 
static int write_spec_handler_int32 (void *cls, struct GNUNET_BIO_WriteHandle *h, const char *what, void *source, size_t source_size)
 Function used internally to write an (u)int32_t from within a write spec. More...
 
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_int32 (const char *what, int32_t *i)
 Create the specification to write an (u)int32_t. More...
 
static int write_spec_handler_int64 (void *cls, struct GNUNET_BIO_WriteHandle *h, const char *what, void *source, size_t source_size)
 Function used internally to write an (u)int64_t from within a write spec. More...
 
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_int64 (const char *what, int64_t *i)
 Create the specification to write an (u)int64_t. More...
 
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_float (const char *what, float *f)
 Create the specification to write a float. More...
 
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_double (const char *what, double *f)
 Create the specification to write an double. More...
 
int GNUNET_BIO_write_spec_commit (struct GNUNET_BIO_WriteHandle *h, struct GNUNET_BIO_WriteSpec *ws)
 Execute the write specifications in order. More...
 

Detailed Description

functions for buffering IO

Author
Christian Grothoff

Definition in file bio.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "util-bio", __VA_ARGS__)

Definition at line 29 of file bio.c.

◆ PATH_MAX

#define PATH_MAX   4096

Assumed maximum path length (for source file names).

Definition at line 35 of file bio.c.

◆ BIO_BUFFER_SIZE

#define BIO_BUFFER_SIZE   65536

Size for I/O buffers.

Definition at line 42 of file bio.c.

Enumeration Type Documentation

◆ IOType

enum IOType

Enum used internally to know how buffering is handled.

The idea is that by using an enum, BIO can be extended to support other kinds of "backend" for buffering (or just formatted I/O.)

Enumerator
IO_FILE 

The handle uses a file to read/write data.

IO_BUFFER 

The data is stored entirely in memory.

Definition at line 51 of file bio.c.

52 {
56  IO_FILE = 0,
57 
61  IO_BUFFER,
62 };
@ IO_BUFFER
The data is stored entirely in memory.
Definition: bio.c:61
@ IO_FILE
The handle uses a file to read/write data.
Definition: bio.c:56

Function Documentation

◆ read_from_file()

static int read_from_file ( struct GNUNET_BIO_ReadHandle h,
const char *  what,
char *  result,
size_t  len 
)
static

Function used internally to read the contents of a file into a buffer.

Parameters
hthe IO handle to read from
whatdescribes what is being read (for error message creation)
resultthe buffer to write the data to
lenthe number of bytes to read
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 204 of file bio.c.

208 {
209  size_t pos = 0;
210  size_t min;
211  ssize_t ret;
212 
213  do
214  {
215  min = h->have - h->pos;
216  if (0 < min)
217  {
218  if (len - pos < min)
219  min = len - pos;
220  GNUNET_memcpy (&result[pos], &h->buffer[h->pos], min);
221  h->pos += min;
222  pos += min;
223  }
224  if (len == pos)
225  return GNUNET_OK;
226  GNUNET_assert (((off_t) h->have) == h->pos);
227  ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
228  if (-1 == ret)
229  {
230  GNUNET_asprintf (&h->emsg,
231  _ ("Error reading `%s' from file: %s"),
232  what,
233  strerror (errno));
234  return GNUNET_SYSERR;
235  }
236  if (0 == ret)
237  {
238  GNUNET_asprintf (&h->emsg,
239  _ ("Error reading `%s' from file: %s"),
240  what,
241  _ ("End of file"));
242  return GNUNET_SYSERR;
243  }
244  h->pos = 0;
245  h->have = ret;
246  }
247  while (pos < len);
248  return GNUNET_OK;
249 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
static int result
Global testing status.
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:622
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define min(x, y)
#define _(String)
GNU gettext support macro.
Definition: platform.h:177

References _, GNUNET_asprintf(), GNUNET_assert, GNUNET_DISK_file_read(), GNUNET_memcpy, GNUNET_OK, GNUNET_SYSERR, h, len, min, GNUNET_BIO_ReadHandle::pos, result, and ret.

Referenced by GNUNET_BIO_read().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_from_buffer()

static int read_from_buffer ( struct GNUNET_BIO_ReadHandle h,
const char *  what,
char *  result,
size_t  len 
)
static

Function used internally to read the content of a buffer into a buffer.

Parameters
hthe IO handle to read from
whatdescribes what is being read (for error message creation)
resultthe buffer to write the result to
lenthe number of bytes to read
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 262 of file bio.c.

266 {
267  if ((h->size < len) || (h->size - h->pos < len))
268  {
269  GNUNET_asprintf (&h->emsg,
270  _ ("Error while reading `%s' from buffer: %s"),
271  what,
272  _ ("Not enough data left"));
273  return GNUNET_SYSERR;
274  }
275  GNUNET_memcpy (result, h->buffer + h->pos, len);
276  h->pos += len;
277  return GNUNET_OK;
278 }

References _, GNUNET_asprintf(), GNUNET_memcpy, GNUNET_OK, GNUNET_SYSERR, h, len, and result.

Referenced by GNUNET_BIO_read().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_to_file()

static enum GNUNET_GenericReturnValue write_to_file ( struct GNUNET_BIO_WriteHandle h,
const char *  what,
const char *  source,
size_t  len 
)
static

Function used internally to write the contents of a buffer into a file.

Parameters
hthe IO handle to write to
whatdescribes what is being written (for error message creation)
sourcethe buffer to write
lenthe number of bytes to write
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 535 of file bio.c.

679 {
680  size_t min;
681  size_t pos = 0;
682  char *buffer = (char *) h->buffer;
683 
684  if (NULL == h->fd)
685  {
686  GNUNET_asprintf (&h->emsg,
687  _ ("Error while writing `%s' to file: %s"),
688  what,
689  _ ("No associated file"));
690  return GNUNET_SYSERR;
691  }
692 
693  do
694  {
695  min = h->size - h->have;
696  if (len - pos < min)
697  min = len - pos;
698  GNUNET_memcpy (&buffer[h->have], &source[pos], min);
699  pos += min;
700  h->have += min;
701  if (len == pos)
702  return GNUNET_OK;
703  GNUNET_assert (h->have == h->size);
704  if (GNUNET_OK != GNUNET_BIO_flush (h))
705  {
706  char *tmp = h->emsg;
707  GNUNET_asprintf (&h->emsg,
708  _ ("Error while writing `%s' to file: %s"),
709  what,
710  tmp);
711  GNUNET_free (tmp);
712  return GNUNET_SYSERR;
713  }
714  }
715  while (pos < len);
716  GNUNET_break (0);
717  return GNUNET_OK;
718 }
static GstElement * source
Appsrc instance into which we write data for the pipeline.
enum GNUNET_GenericReturnValue GNUNET_BIO_flush(struct GNUNET_BIO_WriteHandle *h)
Force a file-based buffered writer to flush its buffer.
Definition: bio.c:607
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_free(ptr)
Wrapper around free.

Referenced by GNUNET_BIO_write().

Here is the caller graph for this function:

◆ write_to_buffer()

static int write_to_buffer ( struct GNUNET_BIO_WriteHandle h,
const char *  what,
const char *  source,
size_t  len 
)
static

Function used internally to write the contents of a buffer to another buffer.

Parameters
hthe IO handle to write to
whatdescribes what is being written (for error message creation)
sourcethe buffer to write
lenthe number of bytes to write
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 731 of file bio.c.

735 {
736  GNUNET_buffer_write ((struct GNUNET_Buffer *) h->buffer, source, len);
737  h->have += len;
738  return GNUNET_OK;
739 }
void GNUNET_buffer_write(struct GNUNET_Buffer *buf, const char *data, size_t len)
Write bytes to the buffer.
Definition: buffer.c:86
Dynamically growing buffer.

References GNUNET_buffer_write(), GNUNET_OK, h, len, and source.

Referenced by GNUNET_BIO_write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_spec_handler_object()

static int read_spec_handler_object ( void *  cls,
struct GNUNET_BIO_ReadHandle h,
const char *  what,
void *  target,
size_t  target_size 
)
static

Function used internally to read some bytes from within a read spec.

Parameters
clsignored, always NULL
hthe IO handle to read from
whatwhat is being read (for error message creation)
targetwhere to store the data
target_sizehow many bytes to read
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 889 of file bio.c.

894 {
895  return GNUNET_BIO_read (h, what, target, target_size);
896 }
int GNUNET_BIO_read(struct GNUNET_BIO_ReadHandle *h, const char *what, void *result, size_t len)
Read some contents into a buffer.
Definition: bio.c:291

◆ read_spec_handler_string()

static int read_spec_handler_string ( void *  cls,
struct GNUNET_BIO_ReadHandle h,
const char *  what,
void *  target,
size_t  target_size 
)
static

Function used internally to read a string from within a read spec.

Parameters
clsignored, always NULL
hthe IO handle to read from
whatwhat is being read (for error message creation)
targetwhere to store the data
target_sizehow many bytes to read
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 935 of file bio.c.

940 {
941  char **result = target;
942  return GNUNET_BIO_read_string (h, what, result, target_size);
943 }
int GNUNET_BIO_read_string(struct GNUNET_BIO_ReadHandle *h, const char *what, char **result, size_t max_length)
Read 0-terminated string.
Definition: bio.c:330

◆ read_spec_handler_int32()

static int read_spec_handler_int32 ( void *  cls,
struct GNUNET_BIO_ReadHandle h,
const char *  what,
void *  target,
size_t  target_size 
)
static

Function used internally to read an (u)int32_t from within a read spec.

Parameters
clsignored, always NULL
hthe IO handle to read from
whatwhat is being read (for error message creation)
targetwhere to store the data
target_sizeignored
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 982 of file bio.c.

987 {
988  int32_t *result = target;
989  return GNUNET_BIO_read_int32 (h, what, result);
990 }
int GNUNET_BIO_read_int32(struct GNUNET_BIO_ReadHandle *h, const char *what, int32_t *i)
Read an (u)int32_t.
Definition: bio.c:427

◆ read_spec_handler_int64()

static int read_spec_handler_int64 ( void *  cls,
struct GNUNET_BIO_ReadHandle h,
const char *  what,
void *  target,
size_t  target_size 
)
static

Function used internally to read an (u)int64_t from within a read spec.

Parameters
clsignored, always NULL
hthe IO handle to read from
whatwhat is being read (for error message creation)
targetwhere to store the data
target_sizeignored
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 1026 of file bio.c.

1031 {
1032  int64_t *result = target;
1033  return GNUNET_BIO_read_int64 (h, what, result);
1034 }
int GNUNET_BIO_read_int64(struct GNUNET_BIO_ReadHandle *h, const char *what, int64_t *i)
Read an (u)int64_t.
Definition: bio.c:449

◆ write_spec_handler_object()

static int write_spec_handler_object ( void *  cls,
struct GNUNET_BIO_WriteHandle h,
const char *  what,
void *  source,
size_t  source_size 
)
static

Function used internally to write some bytes from within a write spec.

Parameters
clsignored, always NULL
hthe IO handle to write to
whatwhat is being written (for error message creation)
sourcethe data to write
source_sizehow many bytes to write
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 1135 of file bio.c.

1140 {
1141  return GNUNET_BIO_write (h, what, source, source_size);
1142 }
int GNUNET_BIO_write(struct GNUNET_BIO_WriteHandle *h, const char *what, const void *buffer, size_t n)
Write a buffer to a handle.
Definition: bio.c:752

◆ write_spec_handler_string()

static int write_spec_handler_string ( void *  cls,
struct GNUNET_BIO_WriteHandle h,
const char *  what,
void *  source,
size_t  source_size 
)
static

Function used internally to write a 0-terminated string from within a write spec.

Parameters
clsignored, always NULL
hthe IO handle to write to
whatwhat is being written (for error message creation)
sourcethe data to write
source_sizeignored
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 1182 of file bio.c.

1187 {
1188  const char *s = source;
1189  return GNUNET_BIO_write_string (h, what, s);
1190 }
int GNUNET_BIO_write_string(struct GNUNET_BIO_WriteHandle *h, const char *what, const char *s)
Write a 0-terminated string.
Definition: bio.c:789

◆ write_spec_handler_int32()

static int write_spec_handler_int32 ( void *  cls,
struct GNUNET_BIO_WriteHandle h,
const char *  what,
void *  source,
size_t  source_size 
)
static

Function used internally to write an (u)int32_t from within a write spec.

Parameters
clsignored, always NULL
hthe IO handle to write to
whatwhat is being written (for error message creation)
sourcethe data to write
source_sizeignored
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 1227 of file bio.c.

1232 {
1233  int32_t i = *(int32_t *) source;
1234  return GNUNET_BIO_write_int32 (h, what, i);
1235 }
int GNUNET_BIO_write_int32(struct GNUNET_BIO_WriteHandle *h, const char *what, int32_t i)
Write an (u)int32_t.
Definition: bio.c:847

◆ write_spec_handler_int64()

static int write_spec_handler_int64 ( void *  cls,
struct GNUNET_BIO_WriteHandle h,
const char *  what,
void *  source,
size_t  source_size 
)
static

Function used internally to write an (u)int64_t from within a write spec.

Parameters
clsignored, always NULL
hthe IO handle to write to
whatwhat is being written (for error message creation)
sourcethe data to write
source_sizeignored
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 1272 of file bio.c.

1277 {
1278  int64_t i = *(int64_t *) source;
1279  return GNUNET_BIO_write_int64 (h, what, i);
1280 }
int GNUNET_BIO_write_int64(struct GNUNET_BIO_WriteHandle *h, const char *what, int64_t i)
Write an (u)int64_t.
Definition: bio.c:867