diff options
author | midipix <writeonce@midipix.org> | 2024-07-13 05:03:48 +0000 |
---|---|---|
committer | midipix <writeonce@midipix.org> | 2024-07-13 05:56:57 +0000 |
commit | c2c816111c629f4602cda556fdf37bb64572660f (patch) | |
tree | 41d5bbd5b5bb3d72a1d2072ab3286d4e17606743 /src/logic/tpax_archive_write.c | |
parent | c1438ffaef9c2f9ae116ebc375db2cc1468c341c (diff) | |
download | tpax-c2c816111c629f4602cda556fdf37bb64572660f.tar.bz2 tpax-c2c816111c629f4602cda556fdf37bb64572660f.tar.xz |
driver: -s <replstr> support: implementation and integration.
Diffstat (limited to 'src/logic/tpax_archive_write.c')
-rw-r--r-- | src/logic/tpax_archive_write.c | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/src/logic/tpax_archive_write.c b/src/logic/tpax_archive_write.c index 32a5f2c..96af625 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,buflen)) < 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), |