diff options
author | midipix <writeonce@midipix.org> | 2024-07-22 03:15:24 +0000 |
---|---|---|
committer | midipix <writeonce@midipix.org> | 2024-07-22 03:15:24 +0000 |
commit | 6d4065b4f7f51d979708df360df7898d3e67b2ad (patch) | |
tree | 536eeb14d562ae6c1c6b6bac52589628da1dd8ed | |
parent | 67365b92cc146cc475b19b1b4dab73a1dc8cebc0 (diff) | |
download | tpax-6d4065b4f7f51d979708df360df7898d3e67b2ad.tar.bz2 tpax-6d4065b4f7f51d979708df360df7898d3e67b2ad.tar.xz |
io interfaces: added tpax_io_range_read_next().
-rw-r--r-- | include/tpax/tpax.h | 2 | ||||
-rw-r--r-- | project/common.mk | 1 | ||||
-rw-r--r-- | src/io/tpax_io_read_next.c | 81 |
3 files changed, 84 insertions, 0 deletions
diff --git a/include/tpax/tpax.h b/include/tpax/tpax.h index f5ca8b2..eef7092 100644 --- a/include/tpax/tpax.h +++ b/include/tpax/tpax.h @@ -209,6 +209,8 @@ tpax_api int tpax_meta_init_rustar_header (const char * pathname, const struct /* low-level interfaces */ tpax_api int tpax_io_seek (struct tpax_driver_ctx *, off_t, int); +tpax_api int tpax_io_range_read_next (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 a547077..8d2dee3 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_next.c \ src/io/tpax_io_seek.c \ src/logic/tpax_archive_enqueue.c \ src/logic/tpax_archive_reset.c \ diff --git a/src/io/tpax_io_read_next.c b/src/io/tpax_io_read_next.c new file mode 100644 index 0000000..98df299 --- /dev/null +++ b/src/io/tpax_io_read_next.c @@ -0,0 +1,81 @@ +/**************************************************************/ +/* tpax: a topological pax implementation */ +/* Copyright (C) 2020--2024 SysDeer Technologies, LLC */ +/* Released under GPLv2 and GPLv3; see COPYING.TPAX. */ +/**************************************************************/ + +#include <stdint.h> +#include <unistd.h> +#include <fcntl.h> +#include <errno.h> + +#include <tpax/tpax.h> +#include <tpax/tpax_specs.h> +#include "tpax_driver_impl.h" +#include "tpax_errinfo_impl.h" + +#define TPAX_IO_READ_MAX (2147483647) + +static int tpax_io_range_read_next_fileio( + struct tpax_driver_ctx * dctx, + void * buf, + size_t size) +{ + int fdin; + ssize_t nbytes; + off_t cpos; + + fdin = tpax_driver_fdin(dctx); + + nbytes = read(fdin,buf,size); + + while ((nbytes < 0) && (errno = EINTR)) + nbytes = read(fdin,buf,size); + + if (nbytes > 0) { + cpos = tpax_get_driver_cpos(dctx); + tpax_set_driver_cpos(dctx,cpos + nbytes); + } + + return nbytes; +} + +static int tpax_io_range_read_next_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); + + tpax_set_driver_cpos(dctx,cpos + size); + + return size; +} + +int tpax_io_range_read_next(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_next_fileio(dctx,buf,size); + + } else if (dctx->cctx->srcflags & TPAX_SOURCE_DATA_MAPPED) { + return tpax_io_range_read_next_mapped(dctx,buf,size); + + } + + return TPAX_CUSTOM_ERROR( + dctx, + TPAX_ERR_FLOW_ERROR); +} |