From: Vsevolod Stakhov Date: Sun, 11 Mar 2012 12:01:20 +0000 (+0400) Subject: Add offset support to aio operations. X-Git-Tag: 0.5.0~58 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=899c90dd4273245f3e26cb2d2cf311918dcc9068;p=thirdparty%2Frspamd.git Add offset support to aio operations. --- diff --git a/src/aio_event.c b/src/aio_event.c index c171fece53..dc1d8284a2 100644 --- a/src/aio_event.c +++ b/src/aio_event.c @@ -302,7 +302,7 @@ rspamd_aio_open (struct aio_context *ctx, const gchar *path, int flags) * Asynchronous read of file */ gint -rspamd_aio_read (gint fd, gpointer buf, gsize len, struct aio_context *ctx, rspamd_aio_cb cb, gpointer ud) +rspamd_aio_read (gint fd, gpointer buf, gsize len, off_t offset, struct aio_context *ctx, rspamd_aio_cb cb, gpointer ud) { struct io_cbdata *cbdata; gint r = -1; @@ -325,7 +325,7 @@ rspamd_aio_read (gint fd, gpointer buf, gsize len, struct aio_context *ctx, rspa iocb[0]->aio_reqprio = 0; iocb[0]->aio_buf = (guint64)((uintptr_t)buf); iocb[0]->aio_nbytes = len; - iocb[0]->aio_offset = 0; + iocb[0]->aio_offset = offset; iocb[0]->aio_flags |= (1 << 0) /* IOCB_FLAG_RESFD */; iocb[0]->aio_resfd = ctx->event_fd; iocb[0]->aio_data = (guint64)((uintptr_t)cbdata); @@ -348,12 +348,15 @@ rspamd_aio_read (gint fd, gpointer buf, gsize len, struct aio_context *ctx, rspa else { /* Blocking variant */ blocking: - r = read (fd, buf, len); - if (r >= 0) { - cb (fd, 0, r, buf, ud); - } - else { - cb (fd, r, -1, buf, ud); + r = lseek (fd, offset, SEEK_SET); + if (r > 0) { + r = read (fd, buf, len); + if (r >= 0) { + cb (fd, 0, r, buf, ud); + } + else { + cb (fd, r, -1, buf, ud); + } } } @@ -364,7 +367,7 @@ blocking: * Asynchronous write of file */ gint -rspamd_aio_write (gint fd, gpointer buf, gsize len, struct aio_context *ctx, rspamd_aio_cb cb, gpointer ud) +rspamd_aio_write (gint fd, gpointer buf, gsize len, off_t offset, struct aio_context *ctx, rspamd_aio_cb cb, gpointer ud) { struct io_cbdata *cbdata; gint r = -1; @@ -387,7 +390,7 @@ rspamd_aio_write (gint fd, gpointer buf, gsize len, struct aio_context *ctx, rsp iocb[0]->aio_reqprio = 0; iocb[0]->aio_buf = (guint64)((uintptr_t)buf); iocb[0]->aio_nbytes = len; - iocb[0]->aio_offset = 0; + iocb[0]->aio_offset = offset; iocb[0]->aio_flags |= (1 << 0) /* IOCB_FLAG_RESFD */; iocb[0]->aio_resfd = ctx->event_fd; iocb[0]->aio_data = (guint64)((uintptr_t)cbdata); @@ -410,12 +413,15 @@ rspamd_aio_write (gint fd, gpointer buf, gsize len, struct aio_context *ctx, rsp else { /* Blocking variant */ blocking: - r = write (fd, buf, len); - if (r >= 0) { - cb (fd, 0, r, buf, ud); - } - else { - cb (fd, r, -1, buf, ud); + r = lseek (fd, offset, SEEK_SET); + if (r > 0) { + r = write (fd, buf, len); + if (r >= 0) { + cb (fd, 0, r, buf, ud); + } + else { + cb (fd, r, -1, buf, ud); + } } } diff --git a/src/aio_event.h b/src/aio_event.h index 7350d102e7..7fcb69469d 100644 --- a/src/aio_event.h +++ b/src/aio_event.h @@ -50,12 +50,14 @@ gint rspamd_aio_open (struct aio_context *ctx, const gchar *path, int flags); /** * Asynchronous read of file */ -gint rspamd_aio_read (gint fd, gpointer buf, gsize len, struct aio_context *ctx, rspamd_aio_cb cb, gpointer ud); +gint rspamd_aio_read (gint fd, gpointer buf, gsize len, off_t offset, + struct aio_context *ctx, rspamd_aio_cb cb, gpointer ud); /** * Asynchronous write of file */ -gint rspamd_aio_write (gint fd, gpointer buf, gsize len, struct aio_context *ctx, rspamd_aio_cb cb, gpointer ud); +gint rspamd_aio_write (gint fd, gpointer buf, gsize len, off_t offset, + struct aio_context *ctx, rspamd_aio_cb cb, gpointer ud); /** * Close of aio operations