summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2024-07-20 23:33:36 +0000
committermidipix <writeonce@midipix.org>2024-07-20 23:38:09 +0000
commit67365b92cc146cc475b19b1b4dab73a1dc8cebc0 (patch)
tree338d51c272172e62dcd6b6f4291afdb84c306552
parentf18eb49fd10d21c6f1c0fa37d3a61a54bf6d9d97 (diff)
downloadtpax-67365b92cc146cc475b19b1b4dab73a1dc8cebc0.tar.bz2
tpax-67365b92cc146cc475b19b1b4dab73a1dc8cebc0.tar.xz
io interfaces: added tpax_io_seek() [for both mapped and unmapped archives].
-rw-r--r--include/tpax/tpax.h2
-rw-r--r--project/common.mk1
-rw-r--r--src/io/tpax_io_seek.c86
3 files changed, 89 insertions, 0 deletions
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 <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"
+
+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);
+}