From 67365b92cc146cc475b19b1b4dab73a1dc8cebc0 Mon Sep 17 00:00:00 2001 From: midipix Date: Sat, 20 Jul 2024 23:33:36 +0000 Subject: io interfaces: added tpax_io_seek() [for both mapped and unmapped archives]. --- include/tpax/tpax.h | 2 ++ project/common.mk | 1 + src/io/tpax_io_seek.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 src/io/tpax_io_seek.c diff --git a/include/tpax/tpax.h b/include/tpax/tpax.h index bb6aae7..f5ca8b2 100644 --- a/include/tpax/tpax.h +++ b/include/tpax/tpax.h @@ -207,6 +207,8 @@ tpax_api int tpax_meta_init_rustar_header (const char * pathname, const struct const char * linkname, struct tpax_ustar_header *); /* low-level interfaces */ +tpax_api int tpax_io_seek (struct tpax_driver_ctx *, off_t, int); + 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 d600ff2..a547077 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_seek.c \ src/logic/tpax_archive_enqueue.c \ src/logic/tpax_archive_reset.c \ src/logic/tpax_archive_write.c \ diff --git a/src/io/tpax_io_seek.c b/src/io/tpax_io_seek.c new file mode 100644 index 0000000..01e0683 --- /dev/null +++ b/src/io/tpax_io_seek.c @@ -0,0 +1,86 @@ +/**************************************************************/ +/* 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" + +static int tpax_io_seek_fileio( + struct tpax_driver_ctx * dctx, + off_t offset, + int whence) +{ + int fdin; + + fdin = tpax_driver_fdin(dctx); + + if ((offset = lseek(fdin,offset,whence)) < 0) + return TPAX_SYSTEM_ERROR(dctx); + + tpax_set_driver_cpos(dctx,offset); + + return 0; +} + +static int tpax_io_seek_mapped( + struct tpax_driver_ctx * dctx, + off_t offset, + int whence) +{ + struct tpax_driver_ctx_impl * ictx; + size_t aoffset; + + ictx = tpax_get_driver_ictx(dctx); + + if (whence == SEEK_CUR) { + offset += tpax_get_driver_cpos(dctx); + + } else if (whence == SEEK_END) { + offset += ictx->mapsize; + } + + if ((offset < 0) || ((aoffset = offset) > ictx->mapsize)) + return TPAX_CUSTOM_ERROR( + dctx, + TPAX_ERR_FLOW_ERROR); + + tpax_set_driver_cpos(dctx,offset); + + return 0; +} + +int tpax_io_seek(struct tpax_driver_ctx * dctx, off_t offset, int whence) +{ + switch (whence) { + case SEEK_SET: + case SEEK_CUR: + case SEEK_END: + break; + + default: + return TPAX_CUSTOM_ERROR( + dctx, + TPAX_ERR_FLOW_ERROR); + } + + if (dctx->cctx->srcflags & TPAX_SOURCE_DATA_FILEIO) { + return tpax_io_seek_fileio(dctx,offset,whence); + + } else if (dctx->cctx->srcflags & TPAX_SOURCE_DATA_MAPPED) { + return tpax_io_seek_mapped(dctx,offset,whence); + + } + + return TPAX_CUSTOM_ERROR( + dctx, + TPAX_ERR_FLOW_ERROR); +} -- cgit v1.2.3