From 899c90dd4273245f3e26cb2d2cf311918dcc9068 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sun, 11 Mar 2012 16:01:20 +0400 Subject: [PATCH] Add offset support to aio operations. --- src/aio_event.c | 38 ++++++++++++++++++++++---------------- src/aio_event.h | 6 ++++-- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/aio_event.c b/src/aio_event.c index c171fece5..dc1d8284a 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 7350d102e..7fcb69469 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 -- 2.39.5