From 3e45a561ddfed71c4e377f3450d6e8e31c702c96 Mon Sep 17 00:00:00 2001 From: midipix Date: Jul 22 2024 03:21:42 +0000 Subject: io interfaces: added tpax_io_range_read_ahead(). --- diff --git a/include/tpax/tpax.h b/include/tpax/tpax.h index eef7092..66bef6d 100644 --- a/include/tpax/tpax.h +++ b/include/tpax/tpax.h @@ -211,6 +211,8 @@ tpax_api int tpax_io_seek (struct tpax_driver_ctx *, off_t, in tpax_api int tpax_io_range_read_next (struct tpax_driver_ctx *, void *, size_t); +tpax_api int tpax_io_range_read_ahead (struct tpax_driver_ctx *, void *, size_t); + tpax_api int tpax_io_create_memory_snapshot(const struct tpax_driver_ctx *, int, const char *, const struct stat *, void *); diff --git a/project/common.mk b/project/common.mk index 8d2dee3..04220ea 100644 --- a/project/common.mk +++ b/project/common.mk @@ -4,6 +4,7 @@ API_SRCS = \ src/driver/tpax_unit_ctx.c \ src/io/tpax_create_memory_snapshot.c \ src/io/tpax_create_tmpfs_snapshot.c \ + src/io/tpax_io_read_ahead.c \ src/io/tpax_io_read_next.c \ src/io/tpax_io_seek.c \ src/logic/tpax_archive_enqueue.c \ diff --git a/src/io/tpax_io_read_ahead.c b/src/io/tpax_io_read_ahead.c new file mode 100644 index 0000000..c92c08c --- /dev/null +++ b/src/io/tpax_io_read_ahead.c @@ -0,0 +1,75 @@ +/**************************************************************/ +/* tpax: a topological pax implementation */ +/* Copyright (C) 2020--2024 SysDeer Technologies, LLC */ +/* Released under GPLv2 and GPLv3; see COPYING.TPAX. */ +/**************************************************************/ + +#include +#include +#include +#include + +#include +#include +#include "tpax_driver_impl.h" +#include "tpax_errinfo_impl.h" + +#define TPAX_IO_READ_MAX (2147483647) + +static int tpax_io_range_read_ahead_fileio( + struct tpax_driver_ctx * dctx, + void * buf, + size_t size) +{ + int fdin; + ssize_t nbytes; + off_t cpos; + + fdin = tpax_driver_fdin(dctx); + cpos = tpax_get_driver_cpos(dctx); + + nbytes = pread(fdin,buf,size,cpos); + + while ((nbytes < 0) && (errno = EINTR)) + nbytes = pread(fdin,buf,size,cpos); + + return nbytes; +} + +static int tpax_io_range_read_ahead_mapped( + struct tpax_driver_ctx * dctx, + void * buf, + size_t size) +{ + struct tpax_driver_ctx_impl * ictx; + char * ch; + off_t cpos; + + ictx = tpax_get_driver_ictx(dctx); + cpos = tpax_get_driver_cpos(dctx); + size = (cpos + size <= ictx->mapsize) ? size : ictx->mapsize - cpos; + + ch = ictx->mapaddr; + ch = &ch[cpos]; + + memcpy(buf,ch,size); + + return size; +} + +int tpax_io_range_read_ahead(struct tpax_driver_ctx * dctx, void * buf, size_t size) +{ + size = (size <= TPAX_IO_READ_MAX) ? size : TPAX_IO_READ_MAX; + + if (dctx->cctx->srcflags & TPAX_SOURCE_DATA_FILEIO) { + return tpax_io_range_read_ahead_fileio(dctx,buf,size); + + } else if (dctx->cctx->srcflags & TPAX_SOURCE_DATA_MAPPED) { + return tpax_io_range_read_ahead_mapped(dctx,buf,size); + + } + + return TPAX_CUSTOM_ERROR( + dctx, + TPAX_ERR_FLOW_ERROR); +}