From 5ea1d7cd4b630a91b4a72fb722a937a107d76fc0 Mon Sep 17 00:00:00 2001 From: midipix Date: Sat, 13 Jul 2024 05:03:48 +0000 Subject: driver: -s support: implementation and integration. --- src/logic/tpax_archive_write.c | 44 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'src/logic') diff --git a/src/logic/tpax_archive_write.c b/src/logic/tpax_archive_write.c index 32a5f2c..a881bd9 100644 --- a/src/logic/tpax_archive_write.c +++ b/src/logic/tpax_archive_write.c @@ -75,6 +75,35 @@ static int tpax_archive_write_ret( return ret; } +static int tpax_apply_string_replacement( + const struct tpax_driver_ctx * dctx, + const char * path, + char * replbuf, + size_t buflen) +{ + int ret; + struct tpax_driver_ctx_impl * ictx; + struct tpax_replstr * replstrv; + + ictx = tpax_get_driver_ictx(dctx); + + if (!(replstrv = ictx->replstrv)) + return 0; + + for (ret=0; !ret && replstrv->regexp; replstrv++) { + ret = tpax_util_path_replstr( + replbuf,path, + replstrv->replstr, + &replstrv->regex, + buflen,replstrv->flags); + + if ((ret > 0) && (replstrv->flags & TPAX_REPL_PRINT)) + tpax_dprintf(tpax_driver_fderr(dctx),"%s >> %s\n",path,replbuf); + } + + return ret; +} + static int tpax_archive_write_impl( const struct tpax_driver_ctx * dctx, const struct tpax_dirent * cdent, @@ -85,12 +114,14 @@ static int tpax_archive_write_impl( struct tpax_ustar_header uhdr; const struct stat * st; struct stat stbuf; + const char * apath; const char * path; const char * slnk; const char * mlnk; off_t hpos; off_t dpos; int fdtmp; + int slen; ssize_t nread; ssize_t nbytes; void * buf; @@ -98,6 +129,7 @@ static int tpax_archive_write_impl( size_t cmplen; void * membuf; char * ch; + char replbuf[PATH_MAX]; char pathbuf[PATH_MAX]; /* followed symlink? */ @@ -110,9 +142,17 @@ static int tpax_archive_write_impl( dctx, TPAX_ERR_FLOW_ERROR); + /* regex matching and patter substitution */ + if ((slen = tpax_apply_string_replacement(dctx,path,replbuf,PATH_MAX)) < 0) + return TPAX_CUSTOM_ERROR( + dctx, + TPAX_ERR_FLOW_ERROR); + + apath = slen ? replbuf : path; + /* verbose mode */ if (dctx->cctx->drvflags & TPAX_DRIVER_VERBOSE) - tpax_dprintf(tpax_driver_fderr(dctx),"%s",path); + tpax_dprintf(tpax_driver_fderr(dctx),"%s",apath); /* uctx */ if (tpax_lib_get_unit_ctx(dctx,fdcwd,path,&uctx) < 0) @@ -171,7 +211,7 @@ static int tpax_archive_write_impl( /* header */ if (tpax_meta_init_ustar_header( - dctx,path,st, + dctx,apath,st, slnk,&uhdr) < 0) return tpax_archive_write_ret( TPAX_NESTED_ERROR(dctx), -- cgit v1.2.3