diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/driver/tpax_driver_ctx.c | 51 | ||||
-rw-r--r-- | src/io/tpax_io_read_ahead.c | 75 | ||||
-rw-r--r-- | src/io/tpax_io_read_next.c | 81 | ||||
-rw-r--r-- | src/io/tpax_io_seek.c | 86 |
4 files changed, 264 insertions, 29 deletions
diff --git a/src/driver/tpax_driver_ctx.c b/src/driver/tpax_driver_ctx.c index 4348ebc..b7b949e 100644 --- a/src/driver/tpax_driver_ctx.c +++ b/src/driver/tpax_driver_ctx.c @@ -55,6 +55,9 @@ static const struct tpax_fd_ctx tpax_default_fdctx = { .fdlog = (-1), }; +/* fallback error description */ +static const char tpax_null_errdesc[] = "<no error description>"; + struct tpax_driver_ctx_alloc { struct argv_meta * meta; struct tpax_driver_ctx_impl ctx; @@ -78,6 +81,21 @@ static uint32_t tpax_argv_flags(uint32_t flags) return ret; } +static const char * tpax_driver_errdesc(void) +{ + int lerrno; + const char * errstr; + + lerrno = errno; + errno = 0; + + errstr = strerror(lerrno); + errstr = errno ? tpax_null_errdesc : errstr; + errno = lerrno; + + return errstr; +} + static int tpax_driver_usage( int fdout, const char * program, @@ -173,8 +191,7 @@ static int tpax_driver_usage_copy_mode( break; default: - if (!(errdesc = strerror(errno))) - errdesc = "<no error description>"; + errdesc = tpax_driver_errdesc(); tpax_dprintf( fdout, @@ -263,15 +280,7 @@ static int tpax_driver_error_archive_path( struct argv_meta * meta, bool fwrite) { - int lerrno; - const char * errstr; - - lerrno = errno; - errno = 0; - - errstr = strerror(lerrno); - errstr = errno ? "" : errstr; - errno = lerrno; + const char * errstr = tpax_driver_errdesc(); if (fwrite) { tpax_dprintf( @@ -584,15 +593,7 @@ static int tpax_driver_srcstat_error( const struct argv_entry * archive, const char * program) { - int lerrno; - const char * errstr; - - lerrno = errno; - errno = 0; - - errstr = strerror(lerrno); - errstr = errno ? "" : errstr; - errno = lerrno; + const char * errstr = tpax_driver_errdesc(); if (archive) { tpax_dprintf( @@ -615,15 +616,7 @@ static int tpax_driver_cache_error( struct tpax_driver_ctx_impl * ctx, const char * program) { - int lerrno; - const char * errstr; - - lerrno = errno; - errno = 0; - - errstr = strerror(lerrno); - errstr = errno ? tpax_null_errdesc : errstr; - errno = lerrno; + const char * errstr = tpax_driver_errdesc(); tpax_dprintf( ctx->fdctx.fderr, 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 <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_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); +} 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); +} 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); +} |