diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/driver/tpax_amain.c | 2 | ||||
-rw-r--r-- | src/driver/tpax_driver_ctx.c | 584 | ||||
-rw-r--r-- | src/driver/tpax_unit_ctx.c | 2 | ||||
-rw-r--r-- | src/internal/argv/argv.h | 281 | ||||
-rw-r--r-- | src/internal/tpax_driver_impl.h | 57 | ||||
-rw-r--r-- | src/internal/tpax_ftime_impl.c | 48 | ||||
-rw-r--r-- | src/internal/tpax_ftime_impl.h | 24 | ||||
-rw-r--r-- | src/io/tpax_create_memory_snapshot.c | 8 | ||||
-rw-r--r-- | src/io/tpax_create_tmpfs_snapshot.c | 10 | ||||
-rw-r--r-- | src/logic/tpax_archive_enqueue.c (renamed from src/logic/tpax_archive_append.c) | 267 | ||||
-rw-r--r-- | src/logic/tpax_archive_reset.c | 59 | ||||
-rw-r--r-- | src/logic/tpax_archive_write.c | 353 | ||||
-rw-r--r-- | src/logic/tpax_queue_vector.c | 144 | ||||
-rw-r--r-- | src/meta/tpax_init_cpio_header.c | 145 | ||||
-rw-r--r-- | src/meta/tpax_init_ustar_header.c | 27 | ||||
-rw-r--r-- | src/skin/tpax_skin_default.c | 103 | ||||
-rw-r--r-- | src/util/tpax_path_replstr.c | 120 |
17 files changed, 1983 insertions, 251 deletions
diff --git a/src/driver/tpax_amain.c b/src/driver/tpax_amain.c index 61f53d1..429633e 100644 --- a/src/driver/tpax_amain.c +++ b/src/driver/tpax_amain.c @@ -54,7 +54,7 @@ static void tpax_perform_unit_actions( struct tpax_unit_ctx * uctx) { if (dctx->cctx->drvflags & TPAX_DRIVER_EXEC_MODE_WRITE) - tpax_archive_append(dctx,uctx); + tpax_archive_enqueue(dctx,uctx); } static void tpax_archive_write_and_seal( diff --git a/src/driver/tpax_driver_ctx.c b/src/driver/tpax_driver_ctx.c index 8ab684d..b7b949e 100644 --- a/src/driver/tpax_driver_ctx.c +++ b/src/driver/tpax_driver_ctx.c @@ -6,6 +6,7 @@ #define _DEFAULT_SOURCE 1 +#include <regex.h> #include <stdint.h> #include <stdlib.h> #include <string.h> @@ -33,6 +34,9 @@ | TPAX_DRIVER_WRITE_FORMAT_USTAR \ | TPAX_DRIVER_WRITE_FORMAT_RUSTAR) +#define TPAX_CACHE_MIN (1 << 12) +#define TPAX_CACHE_MAX (1 << 24) + /* package info */ static const struct tpax_source_version tpax_src_version = { TPAX_TAG_VER_MAJOR, @@ -51,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; @@ -74,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, @@ -86,9 +108,9 @@ static int tpax_driver_usage( snprintf(header,sizeof(header), "%s — topological pax implementation\n\n" "Synopsis:\n" - " %s [-d]\n" - " %s -r [-d]\n" - " %s -w [−x format] [-b blocksize] [-d]\n" + " %s [-d] [-f archive]\n" + " %s -r [-d] [-f archive]\n" + " %s -w [−x format] [-b blocksize] [-dtv] [-H|-L] [-f archive] [-s replstr]... \n" " %s -r -w [-d]\n\n" "Options:\n", program,program,program,program,program); @@ -112,9 +134,9 @@ static int tpax_driver_usage_exec_mode( tpax_dprintf( fdout, - "\nWhen using explicit (long) mode options, " + "\n%s: usage error: When using explicit (long) mode options, " "only one of --list, --read, --write, --copy " - "may be used.\n"); + "may be used.\n",program); return TPAX_USAGE; } @@ -125,19 +147,27 @@ static int tpax_driver_usage_copy_mode( const char * arg, const struct argv_option ** optv, struct argv_meta * meta, - struct argv_entry * operand) + struct argv_entry * operand, + struct argv_entry * archive) { const char * errdesc; - if (!operand) { + if (archive || !operand) { tpax_driver_usage( fdout,program, arg,optv,meta); - tpax_dprintf( - fdout, - "\nThe copy mode requires a destination " - "directory argument.\n\n"); + if (archive) { + tpax_dprintf( + fdout, + "\n%s: usage error: the __copy__ mode does not permit specifying " + "an archive path.\n\n",program); + } else { + tpax_dprintf( + fdout, + "\n%s: usage error: the __copy__ mode requires a destination " + "directory argument.\n\n",program); + } return TPAX_USAGE; } @@ -146,7 +176,7 @@ static int tpax_driver_usage_copy_mode( case ENOENT: tpax_dprintf( fdout, - "%s: error: " + "%s: file-system error: " "the destination directory '%s' " "does not exist.\n\n", program,operand->arg); @@ -155,18 +185,17 @@ static int tpax_driver_usage_copy_mode( case ENOTDIR: tpax_dprintf( fdout, - "%s: error: " + "%s: file-system error: " "'%s' is not a directory.\n\n", program,operand->arg); break; default: - if (!(errdesc = strerror(errno))) - errdesc = "<no error description>"; + errdesc = tpax_driver_errdesc(); tpax_dprintf( fdout, - "%s: error: while opening the " + "%s: general error: while opening the " "destination directory '%s': %s.\n\n", program,operand->arg,errdesc); break; @@ -190,8 +219,8 @@ static int tpax_driver_usage_write_format( tpax_dprintf( fdout, - "\nArchive format may only be specified " - "in write mode.\n"); + "\n%s: usage error: Archive format may only be specified " + "in write mode.\n",program); return TPAX_USAGE; } @@ -200,17 +229,14 @@ static int tpax_driver_usage_block_size( int fdout, const char * program, const char * arg, - const struct argv_option ** optv, struct argv_meta * meta) { - tpax_driver_usage( - fdout,program, - arg,optv,meta); - tpax_dprintf( fdout, - "`%s' is not a valid positive decimal integer.\n", - arg); + "%s: usage error: the requested block size `%s' is not a positive decimal integer.\n", + program,arg); + + argv_free(meta); return TPAX_USAGE; } @@ -219,21 +245,62 @@ static int tpax_driver_usage_block_size_range( int fdout, const char * program, const char * arg, - const struct argv_option ** optv, struct argv_meta * meta) { - tpax_driver_usage( - fdout,program, - arg,optv,meta); + tpax_dprintf( + fdout, + "%s: usage error: `%s' is outside the specified range of 512 to 32256.\n", + program,arg); + + argv_free(meta); + return TPAX_USAGE; +} + +static int tpax_driver_usage_block_constraints( + int fdout, + const char * program, + const char * arg, + struct argv_meta * meta) +{ tpax_dprintf( fdout, - "`%s' is outside the specified range of 512 to 32256.\n", - arg); + "%s: usage error: the specified block size `%s' is not a multiple of 512 bytes.\n", + program,arg); + + argv_free(meta); return TPAX_USAGE; } +static int tpax_driver_error_archive_path( + int fdout, + const char * program, + const char * arg, + struct argv_meta * meta, + bool fwrite) +{ + const char * errstr = tpax_driver_errdesc(); + + if (fwrite) { + tpax_dprintf( + fdout, + "%s: file-system error: archive file <%s> could not be created " + "or opened for writing (%s).\n", + program,arg,errstr); + } else { + tpax_dprintf( + fdout, + "%s: file-system error: archive file <%s> could not be opened " + "for reading (%s).\n", + program,arg,errstr); + } + + argv_free(meta); + + return TPAX_FATAL; +} + static int tpax_driver_error_not_implemented( int fdout, const char * program, @@ -250,17 +317,149 @@ static int tpax_driver_error_not_implemented( return TPAX_FATAL; } +static void tpax_set_archive_block_size(struct tpax_common_ctx * cctx) +{ + if (cctx->blksize) + (void)0; + + else if (cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_PAX) + cctx->blksize = TPAX_PAX_BLOCK_SIZE; + + else if (cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_CPIO) + cctx->blksize = TPAX_CPIO_BLOCK_SIZE; + + else if (cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_USTAR) + cctx->blksize = TPAX_USTAR_BLOCK_SIZE; + + else if (cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_RUSTAR) + cctx->blksize = TPAX_USTAR_BLOCK_SIZE; +} + +static int tpax_add_replstr( + struct argv_entry * entry, + struct tpax_replstr * replstr, + char ** mark) +{ + const char * src; + char * dst; + char sep; + int nsep; + + /* non-null separator character */ + if (!(sep = entry->arg[0])) + return -1; + + /* exactly three separator characters */ + for (nsep=1,src=&entry->arg[1]; *src; src++) { + if ((src[0] == '\\') && (src[1] == sep)) { + src++; + + } else if (src[0] == sep) { + nsep++; + } + } + + if (nsep != 3) + return -1; + + /* regexp */ + for (src=&entry->arg[1],dst=*mark; (*src != sep); src++) { + if ((src[0] == '\\') && (src[1] == sep)) + src++; + + *dst++ = *src; + } + + replstr->replarg = entry->arg; + replstr->replstr = ++dst; + replstr->regexp = *mark; + + /* replstr */ + for (++src; (*src != sep); src++) { + if ((src[0] == '\\') && (src[1] == sep)) + src++; + + *dst++ = *src; + } + + src++; + dst++; + + *mark = dst; + + /* flags */ + if (src[0] && src[1] && src[2]) + return -1; + + if (src[0] && (src[0] == src[1])) + return -1; + + if (src[0] && (src[0] != 'g') && (src[0] != 'p')) + return -1; + + if (src[0] && src[1] && (src[1] != 'g') && (src[1] != 'p')) + return -1; + + if (src[0] && ((src[0] == 'g') || (src[1] == 'g'))) + replstr->flags |= TPAX_REPL_GLOBAL; + + if (src[0] && ((src[0] == 'p') || (src[1] == 'p'))) + replstr->flags |= TPAX_REPL_PRINT; + + /* regex */ + if (regcomp(&replstr->regex,replstr->regexp,0)) { + replstr->regexp = 0; + return -1; + } + + return 0; +} + +static int tpax_init_replstr_vector( + struct tpax_driver_ctx_impl * ctx, + struct argv_meta * meta) +{ + struct argv_entry * entry; + struct tpax_replstr * replstr; + char * mark; + + if (!(replstr = ctx->replstrv)) + return 0; + + for (entry=meta->entries,mark=ctx->replstrs; entry->fopt || entry->arg; entry++) { + if (entry->tag == TAG_REPLSTR) { + if (tpax_add_replstr(entry,replstr,&mark) < 0) + return -1; + + replstr++; + } + } + + return 0; +} + +static int tpax_driver_is_valid_keyval(struct argv_keyval * keyval) +{ + (void)keyval; + return 0; +} + static struct tpax_driver_ctx_impl * tpax_driver_ctx_alloc( struct argv_meta * meta, const struct tpax_fd_ctx * fdctx, const struct tpax_common_ctx * cctx, - size_t nunits) + size_t nunits, + size_t nreplstr, + size_t sreplstr) { struct tpax_driver_ctx_alloc * ictx; size_t size; struct argv_entry * entry; const char ** units; int elements; + int nkeyval; + struct argv_keyval ** pkeyval; + struct argv_keyval * keyval; size = sizeof(struct tpax_driver_ctx_alloc); size += (nunits+1)*sizeof(const char *); @@ -288,33 +487,65 @@ static struct tpax_driver_ctx_impl * tpax_driver_ctx_alloc( if (!entry->fopt) *units++ = entry->arg; + for (entry=meta->entries,nkeyval=0; entry->fopt || entry->arg; entry++) + if (entry->keyv) + for (keyval=entry->keyv; keyval->keyword; keyval++) + nkeyval++; + + if (nkeyval && !(ictx->ctx.keyvalv = calloc(nkeyval+1,sizeof(*ictx->ctx.keyvalv)))) { + free(ictx); + return 0; + } + + if (nreplstr && !(ictx->ctx.replstrv = calloc(++nreplstr,sizeof(*ictx->ctx.replstrv)))) { + free(ictx->ctx.keyvalv); + free(ictx); + return 0; + } + + if (sreplstr && !(ictx->ctx.replstrs = calloc(sreplstr,1))) { + free(ictx->ctx.replstrv); + free(ictx->ctx.keyvalv); + free(ictx); + return 0; + } + + if ((pkeyval = ictx->ctx.keyvalv)) + for (entry=meta->entries; entry->fopt || entry->arg; entry++) + if (entry->keyv) + for (keyval=entry->keyv; keyval->keyword; keyval++) + *pkeyval++ = keyval; + + ictx->ctx.bufsize = TPAX_FILEIO_BUFLEN; + ictx->ctx.bufaddr = mmap( + 0,ictx->ctx.bufsize, + PROT_READ|PROT_WRITE, + MAP_PRIVATE|MAP_ANONYMOUS, + -1,0); + + if (ictx->ctx.bufaddr == MAP_FAILED) { + free(ictx->ctx.keyvalv); + free(ictx); + return 0; + } + if (cctx->drvflags & TPAX_DRIVER_EXEC_MODE_WRITE_COPY) { - ictx->ctx.bufsize = TPAX_FILEIO_BUFLEN; - ictx->ctx.bufaddr = mmap( - 0,ictx->ctx.bufsize, + ictx->ctx.dirbuff = mmap( + 0,TPAX_DIRENT_BUFLEN, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,0); - if (ictx->ctx.bufaddr == MAP_FAILED) { - free(ictx); - return 0; - } - - if (cctx->drvflags & TPAX_DRIVER_DIR_MEMBER_RECURSE) - ictx->ctx.dirbuff = mmap( - 0,TPAX_DIRENT_BUFLEN, - PROT_READ|PROT_WRITE, - MAP_PRIVATE|MAP_ANONYMOUS, - -1,0); - if (ictx->ctx.dirbuff == MAP_FAILED) { munmap(ictx->ctx.bufaddr,ictx->ctx.bufsize); + free(ictx->ctx.keyvalv); free(ictx); return 0; } } + tpax_set_archive_block_size(&ictx->ctx.cctx); + ictx->ctx.ctx.units = ictx->units; ictx->ctx.ctx.errv = ictx->ctx.errinfp; return &ictx->ctx; @@ -326,6 +557,77 @@ static int tpax_get_driver_ctx_fail(struct argv_meta * meta) return -1; } +static int tpax_driver_keyval_error( + struct tpax_driver_ctx_impl * ctx, + struct argv_keyval * keyval, + const char * program) +{ + const char * equal; + + switch (keyval->flags) { + case ARGV_KEYVAL_ASSIGN: + equal = "="; + break; + + case ARGV_KEYVAL_OVERRIDE: + equal = ":="; + break; + + default: + equal = ""; + } + + tpax_dprintf( + ctx->fdctx.fderr, + "%s: unsupported keyval argument (%s%s%s)\n", + program,keyval->keyword,equal, + keyval->value ? keyval->value : ""); + + tpax_lib_free_driver_ctx(&ctx->ctx); + + return TPAX_ERROR; +} + +static int tpax_driver_srcstat_error( + struct tpax_driver_ctx_impl * ctx, + const struct argv_entry * archive, + const char * program) +{ + const char * errstr = tpax_driver_errdesc(); + + if (archive) { + tpax_dprintf( + ctx->fdctx.fderr, + "%s: could not stat archive file '%s' (%s).\n", + program,archive->arg,errstr); + } else { + tpax_dprintf( + ctx->fdctx.fderr, + "%s: could not stat input source file <fd=%d> (%s).\n", + program,ctx->fdctx.fdin,errstr); + } + + tpax_lib_free_driver_ctx(&ctx->ctx); + + return TPAX_ERROR; +} + +static int tpax_driver_cache_error( + struct tpax_driver_ctx_impl * ctx, + const char * program) +{ + const char * errstr = tpax_driver_errdesc(); + + tpax_dprintf( + ctx->fdctx.fderr, + "%s: failed to allocate source data cache (%s).\n", + program,errstr); + + tpax_lib_free_driver_ctx(&ctx->ctx); + + return TPAX_ERROR; +} + int tpax_lib_get_driver_ctx( char ** argv, char ** envp, @@ -338,8 +640,14 @@ int tpax_lib_get_driver_ctx( const struct argv_option * optv[TPAX_OPTV_ELEMENTS]; struct argv_meta * meta; struct argv_entry * entry; + struct argv_entry * archive; struct argv_entry * operand; + struct argv_keyval ** pkeyval; + struct tpax_fd_ctx lfdctx; size_t nunits; + size_t nreplstr; + size_t sreplstr; + size_t cachesize; const char * program; int fddst; const char * ch; @@ -358,10 +666,14 @@ int tpax_lib_get_driver_ctx( return -1; nunits = 0; + archive = 0; operand = 0; program = argv_program_name(argv[0]); memset(&cctx,0,sizeof(cctx)); + nreplstr = 0; + sreplstr = 0; + cctx.drvflags = flags; fddst = fdctx->fddst; @@ -381,6 +693,10 @@ int tpax_lib_get_driver_ctx( cctx.drvflags |= TPAX_DRIVER_VERSION; break; + case TAG_VERBOSE: + cctx.drvflags |= TPAX_DRIVER_VERBOSE; + break; + case TAG_LIST: cctx.drvflags |= TPAX_DRIVER_EXEC_MODE_LIST; break; @@ -397,6 +713,10 @@ int tpax_lib_get_driver_ctx( cctx.drvflags |= TPAX_DRIVER_EXEC_MODE_COPY; break; + case TAG_FILE: + archive = entry; + break; + case TAG_FORMAT: cctx.drvflags &= ~(uint64_t)(TPAX_DRIVER_WRITE_FORMAT_MASK); @@ -419,17 +739,29 @@ int tpax_lib_get_driver_ctx( for (; *ch; ch++) if ((*ch < '0') || (*ch > '9')) return tpax_driver_usage_block_size( - fdctx->fdout, + fdctx->fderr, program,entry->arg, - optv,meta); + meta); cctx.blksize = atoi(entry->arg); if ((cctx.blksize < 512) || (cctx.blksize > 32256)) return tpax_driver_usage_block_size_range( - fdctx->fdout, + fdctx->fderr, program,entry->arg, - optv,meta); + meta); + + if (cctx.blksize % 512) + return tpax_driver_usage_block_constraints( + fdctx->fderr, + program,entry->arg, + meta); + break; + + case TAG_REPLSTR: + sreplstr += strlen(entry->arg); + sreplstr++; + nreplstr++; break; case TAG_RECURSE: @@ -440,6 +772,24 @@ int tpax_lib_get_driver_ctx( cctx.drvflags &= ~(uintptr_t)TPAX_DRIVER_DIR_MEMBER_RECURSE; break; + case TAG_PRESERVE_ATIME: + cctx.drvflags |= TPAX_DRIVER_PRESERVE_ATIME; + break; + + case TAG_PAX_SYMLINK_ARGS: + cctx.drvflags |= TPAX_DRIVER_PAX_SYMLINK_ARGS; + cctx.drvflags &= ~(uint64_t)TPAX_DRIVER_PAX_SYMLINK_ITEMS; + break; + + case TAG_PAX_SYMLINK_ITEMS: + cctx.drvflags |= TPAX_DRIVER_PAX_SYMLINK_ARGS; + cctx.drvflags |= TPAX_DRIVER_PAX_SYMLINK_ITEMS; + break; + + case TAG_STRICT_DEVICE_ID: + cctx.drvflags |= TPAX_DRIVER_STRICT_DEVICE_ID; + break; + case TAG_STRICT_PATH: cctx.drvflags |= TPAX_DRIVER_STRICT_PATH_INPUT; break; @@ -482,11 +832,11 @@ int tpax_lib_get_driver_ctx( /* copy mode: destination directory */ if (cctx.drvflags & TPAX_DRIVER_EXEC_MODE_COPY) { - if (!operand) + if (archive || !operand) return tpax_driver_usage_copy_mode( fdctx->fderr, program,entry->arg, - optv,meta,operand); + optv,meta,operand,archive); fddst = openat( fdctx->fdcwd, @@ -497,7 +847,52 @@ int tpax_lib_get_driver_ctx( return tpax_driver_usage_copy_mode( fdctx->fderr, program,entry->arg, - optv,meta,operand); + optv,meta,operand,archive); + } + + /* archive path */ + if (archive && (cctx.drvflags & TPAX_DRIVER_EXEC_MODE_WRITE)) { + memcpy(&lfdctx,fdctx,sizeof(*fdctx)); + + lfdctx.fdout = openat( + fdctx->fdcwd, + archive->arg, + O_WRONLY|O_CREAT|O_TRUNC, + 0644); + + if (lfdctx.fdout < 0) + return tpax_driver_error_archive_path( + fdctx->fderr, + program,archive->arg, + meta,true); + + fdctx = &lfdctx; + + } else if (archive) { + memcpy(&lfdctx,fdctx,sizeof(*fdctx)); + + cctx.srcflags = TPAX_SOURCE_DATA_PENDING; + + lfdctx.fdin = openat( + fdctx->fdcwd, + archive->arg, + O_RDONLY,0); + + if (lfdctx.fdin < 0) + return tpax_driver_error_archive_path( + fdctx->fderr, + program,archive->arg, + meta,false); + + cctx.srcflags = TPAX_SOURCE_DATA_OPENED; + + fdctx = &lfdctx; + + } else if (cctx.drvflags & TPAX_DRIVER_EXEC_MODE_WRITE) { + cctx.srcflags = TPAX_SOURCE_DATA_NONE; + + } else { + cctx.srcflags = TPAX_SOURCE_DATA_PENDING; } /* not implemented mode(s) */ @@ -540,22 +935,70 @@ int tpax_lib_get_driver_ctx( return tpax_driver_error_not_implemented( fdctx->fderr,program,"the pax format",meta); - case TPAX_DRIVER_WRITE_FORMAT_CPIO: - return tpax_driver_error_not_implemented( - fdctx->fderr,program,"the cpio format",meta); - default: break; } /* driver ctx */ - if (!(ctx = tpax_driver_ctx_alloc(meta,fdctx,&cctx,nunits))) { + if (!(ctx = tpax_driver_ctx_alloc(meta,fdctx,&cctx,nunits,nreplstr,sreplstr))) { if (cctx.drvflags & TPAX_DRIVER_EXEC_MODE_COPY) close(fddst); return tpax_get_driver_ctx_fail(meta); } + /* replstr validation and vector initialization */ + if (tpax_init_replstr_vector(ctx,meta) < 0) { + tpax_lib_free_driver_ctx(&ctx->ctx); + return TPAX_ERROR; + } + + /* keyval validation */ + for (pkeyval=ctx->keyvalv; pkeyval && *pkeyval; pkeyval++) + if (!tpax_driver_is_valid_keyval(*pkeyval)) + return tpax_driver_keyval_error(ctx,*pkeyval,program); + + /* source data mapping (non-critical) */ + if (cctx.srcflags) { + if (fstat(fdctx->fdin,&ctx->srcstat) < 0) + return tpax_driver_srcstat_error(ctx,archive,program); + + ctx->mapsize = ctx->srcstat.st_size; + ctx->mapaddr = mmap( + 0,ctx->mapsize, + PROT_READ,MAP_PRIVATE, + fdctx->fdin,0); + + if (ctx->mapaddr == MAP_FAILED) { + ctx->cctx.srcflags = TPAX_SOURCE_DATA_FILEIO; + ctx->mapaddr = 0; + ctx->mapsize = 0; + } else { + ctx->cctx.srcflags = TPAX_SOURCE_DATA_MAPPED; + } + } + + /* allocate source data cache as needed */ + if (ctx->cctx.srcflags == TPAX_SOURCE_DATA_FILEIO) { + cachesize = TPAX_CACHE_MAX; + + for (; !ctx->cacheaddr && (cachesize >= TPAX_CACHE_MIN); ) + if (!(ctx->cacheaddr = calloc(cachesize,1))) + cachesize >>= 1; + + if (!ctx->cacheaddr) + return tpax_driver_cache_error(ctx,program); + + ctx->cachemark = ctx->cacheaddr; + ctx->cachecap = &ctx->cacheaddr[cachesize]; + } + + /* all done */ + if (archive) { + ctx->file = archive->arg; + ctx->ctx.file = &ctx->file; + } + ctx->ctx.program = program; ctx->ctx.cctx = &ctx->cctx; @@ -569,6 +1012,8 @@ static void tpax_free_driver_ctx_impl(struct tpax_driver_ctx_alloc * ictx) size_t size; char ** ppref; + struct tpax_replstr * replstrv; + for (; ictx->ctx.dirents; ) { next = ictx->ctx.dirents->next; size = ictx->ctx.dirents->size; @@ -577,6 +1022,24 @@ static void tpax_free_driver_ctx_impl(struct tpax_driver_ctx_alloc * ictx) ictx->ctx.dirents = (struct tpax_dirent_buffer *)next; } + for (replstrv=ictx->ctx.replstrv; replstrv && replstrv->regexp; replstrv++) + regfree(&replstrv->regex); + + if (ictx->ctx.replstrv) + free(ictx->ctx.replstrv); + + if (ictx->ctx.replstrs) + free(ictx->ctx.replstrs); + + if (ictx->ctx.keyvalv) + free(ictx->ctx.keyvalv); + + if (ictx->ctx.cacheaddr) + free(ictx->ctx.cacheaddr); + + if (ictx->ctx.mapaddr) + munmap(ictx->ctx.mapaddr,ictx->ctx.mapsize); + if (ictx->ctx.bufaddr) munmap(ictx->ctx.bufaddr,ictx->ctx.bufsize); @@ -592,6 +1055,9 @@ static void tpax_free_driver_ctx_impl(struct tpax_driver_ctx_alloc * ictx) if (ictx->ctx.direntv) free(ictx->ctx.direntv); + if (ictx->ctx.cpiov) + free(ictx->ctx.cpiov); + argv_free(ictx->meta); free(ictx); } diff --git a/src/driver/tpax_unit_ctx.c b/src/driver/tpax_unit_ctx.c index 1fbc510..5c4cf80 100644 --- a/src/driver/tpax_unit_ctx.c +++ b/src/driver/tpax_unit_ctx.c @@ -65,7 +65,7 @@ int tpax_lib_get_unit_ctx( } ctx->path = path; - ctx->link = ctx->linkbuf; + ctx->link = ctx->linkbuf[0] ? ctx->linkbuf : 0; ctx->uctx.path = &ctx->path; ctx->uctx.link = &ctx->link; diff --git a/src/internal/argv/argv.h b/src/internal/argv/argv.h index 87c60d2..8efedda 100644 --- a/src/internal/argv/argv.h +++ b/src/internal/argv/argv.h @@ -13,6 +13,7 @@ #include <string.h> #include <stdlib.h> #include <stdio.h> +#include <ctype.h> #include <unistd.h> #define ARGV_VERBOSITY_NONE 0x00 @@ -57,16 +58,27 @@ #define ARGV_OPTION_HYBRID_EQUAL 0x04 #define ARGV_OPTION_HYBRID_COMMA 0x08 #define ARGV_OPTION_HYBRID_JOINED 0x10 + +#define ARGV_OPTION_KEYVAL_PAIR 0X20 +#define ARGV_OPTION_KEYVAL_ARRAY 0X40 +#define ARGV_OPTION_KEYVAL_MASK (ARGV_OPTION_KEYVAL_PAIR \ + | ARGV_OPTION_KEYVAL_ARRAY) + #define ARGV_OPTION_HYBRID_CIRCUS (ARGV_OPTION_HYBRID_SPACE \ | ARGV_OPTION_HYBRID_JOINED) + #define ARGV_OPTION_HYBRID_DUAL (ARGV_OPTION_HYBRID_SPACE \ | ARGV_OPTION_HYBRID_EQUAL) + #define ARGV_OPTION_HYBRID_SWITCH (ARGV_OPTION_HYBRID_ONLY \ | ARGV_OPTION_HYBRID_SPACE \ | ARGV_OPTION_HYBRID_EQUAL \ | ARGV_OPTION_HYBRID_COMMA \ | ARGV_OPTION_HYBRID_JOINED) +#define ARGV_KEYVAL_ASSIGN 0x01 +#define ARGV_KEYVAL_OVERRIDE 0x02 + enum argv_optarg { ARGV_OPTARG_NONE, ARGV_OPTARG_REQUIRED, @@ -92,6 +104,9 @@ enum argv_error { ARGV_ERROR_HYBRID_SPACE, ARGV_ERROR_HYBRID_EQUAL, ARGV_ERROR_HYBRID_COMMA, + ARGV_ERROR_KEYVAL_KEY, + ARGV_ERROR_KEYVAL_VALUE, + ARGV_ERROR_KEYVAL_ALLOC, }; struct argv_option { @@ -105,13 +120,20 @@ struct argv_option { const char * description; }; +struct argv_keyval { + const char * keyword; + const char * value; + int flags; +}; + struct argv_entry { - const char * arg; - int tag; - bool fopt; - bool fval; - bool fnoscan; - enum argv_error errcode; + const char * arg; + struct argv_keyval * keyv; + int tag; + bool fopt; + bool fval; + bool fnoscan; + enum argv_error errcode; }; struct argv_meta { @@ -129,6 +151,7 @@ struct argv_ctx { const char * errch; const struct argv_option * erropt; const char * program; + size_t keyvlen; }; #ifdef ARGV_DRIVER @@ -136,6 +159,8 @@ struct argv_ctx { struct argv_meta_impl { char ** argv; char * strbuf; + char * keyvbuf; + char * keyvmark; struct argv_meta meta; }; @@ -305,12 +330,131 @@ static inline const struct argv_option * option_from_tag( return 0; } +static inline int argv_scan_keyval_array(struct argv_meta_impl * meta, struct argv_entry * entry) +{ + const char * ch; + char * dst; + int ncomma; + int cint; + struct argv_keyval * keyv; + + /* count key[val] elements, assume no comma after last element */ + for (ch=entry->arg,ncomma=1; *ch; ch++) { + if ((ch[0]=='\\') && (ch[1]==',')) { + ch++; + + } else if ((ch[0]==',')) { + ncomma++; + } + } + + /* keyval string buffer */ + dst = meta->keyvmark; + + /* allocate keyval array */ + if (!(entry->keyv = calloc(ncomma+1,sizeof(*entry->keyv)))) + return ARGV_ERROR_KEYVAL_ALLOC; + + /* create keyval array */ + entry->keyv->keyword = dst; + + for (ch=entry->arg,keyv=entry->keyv; *ch; ch++) { + if ((ch[0]=='\\') && (ch[1]==',')) { + *dst++ = ','; + ch++; + + } else if ((ch[0]==':') && (ch[1]=='=')) { + if (!keyv->keyword[0]) + return ARGV_ERROR_KEYVAL_KEY; + + keyv->flags = ARGV_KEYVAL_OVERRIDE; + keyv->value = ++dst; + ch++; + + } else if ((ch[0]=='=')) { + if (!keyv->keyword[0]) + return ARGV_ERROR_KEYVAL_KEY; + + keyv->flags = ARGV_KEYVAL_ASSIGN; + keyv->value = ++dst; + + } else if ((ch[0]==',')) { + for (; isblank(cint = ch[1]); ) + ch++; + + if (ch[1]) { + keyv++; + keyv->keyword = ++dst; + } + } else { + *dst++ = *ch; + } + } + + /* keyval string buffer */ + meta->keyvmark = ++dst; + + return ARGV_ERROR_OK; +} + +static inline int argv_scan_keyval_pair(struct argv_meta_impl * meta, struct argv_entry * entry) +{ + const char * ch; + char * dst; + struct argv_keyval * keyv; + + /* keyval string buffer */ + dst = meta->keyvmark; + + /* allocate keyval array */ + if (!(entry->keyv = calloc(2,sizeof(*entry->keyv)))) + return ARGV_ERROR_KEYVAL_ALLOC; + + /* create keyval array */ + entry->keyv->keyword = dst; + + for (ch=entry->arg,keyv=entry->keyv; *ch && !keyv->flags; ch++) { + if ((ch[0]=='\\') && (ch[1]==',')) { + *dst++ = ','; + ch++; + + } else if ((ch[0]==':') && (ch[1]=='=')) { + if (!keyv->keyword[0]) + return ARGV_ERROR_KEYVAL_KEY; + + keyv->flags = ARGV_KEYVAL_OVERRIDE; + keyv->value = ++dst; + ch++; + + } else if ((ch[0]=='=')) { + if (!keyv->keyword[0]) + return ARGV_ERROR_KEYVAL_KEY; + + keyv->flags = ARGV_KEYVAL_ASSIGN; + keyv->value = ++dst; + + } else { + *dst++ = *ch; + } + } + + for (; *ch; ) + *dst++ = *ch++; + + /* keyval string buffer */ + meta->keyvmark = ++dst; + + return ARGV_ERROR_OK; +} + static void argv_scan( char ** argv, const struct argv_option ** optv, struct argv_ctx * ctx, struct argv_meta * meta) { + struct argv_meta_impl * imeta; + uintptr_t addr; char ** parg; const char * ch; const char * val; @@ -324,6 +468,9 @@ static void argv_scan( bool fhybrid; bool fnoscan; + addr = (uintptr_t)meta - offsetof(struct argv_meta_impl,meta); + imeta = (struct argv_meta_impl *)addr; + parg = &argv[1]; ch = *parg; ferr = ARGV_ERROR_OK; @@ -495,13 +642,20 @@ static void argv_scan( if (!option && !ctx->unitidx) ctx->unitidx = parg - argv; + if (ferr == ARGV_ERROR_OK) + if (option && (option->flags & ARGV_OPTION_KEYVAL_MASK)) + if (ctx->mode == ARGV_MODE_SCAN) + ctx->keyvlen += strlen(ch) + 1; + if (ferr != ARGV_ERROR_OK) { ctx->errcode = ferr; ctx->errch = ctx->errch ? ctx->errch : ch; ctx->erropt = option; ctx->erridx = parg - argv; return; - } else if (ctx->mode == ARGV_MODE_SCAN) { + } + + if (ctx->mode == ARGV_MODE_SCAN) { if (!fnoscan) ctx->nentries++; else if (fval) @@ -511,12 +665,12 @@ static void argv_scan( parg++; ch = *parg; } + } else if (ctx->mode == ARGV_MODE_COPY) { if (fnoscan) { if (fval) { mentry->arg = ch; mentry->fnoscan = true; - mentry++; } parg++; @@ -526,7 +680,6 @@ static void argv_scan( mentry->tag = option->tag; mentry->fopt = true; mentry->fval = fval; - mentry++; if (fval) { parg++; @@ -534,11 +687,28 @@ static void argv_scan( } } else { mentry->arg = ch; - mentry++; parg++; ch = *parg; } } + + if (option && (option->flags & ARGV_OPTION_KEYVAL_PAIR)) + if (ctx->mode == ARGV_MODE_COPY) + ferr = argv_scan_keyval_pair(imeta,mentry); + + if (option && (option->flags & ARGV_OPTION_KEYVAL_ARRAY)) + if (ctx->mode == ARGV_MODE_COPY) + ferr = argv_scan_keyval_array(imeta,mentry); + + if (ferr != ARGV_ERROR_OK) { + ctx->errcode = ferr; + ctx->errch = ctx->errch ? ctx->errch : ch; + ctx->erropt = option; + ctx->erridx = parg - argv; + return; + } + + mentry++; } } @@ -663,6 +833,14 @@ static void argv_show_error(int fd, struct argv_ctx * ctx) break; + case ARGV_ERROR_KEYVAL_KEY: + argv_dprintf(fd,"illegal key detected in keyval argument\n"); + break; + + case ARGV_ERROR_KEYVAL_VALUE: + argv_dprintf(fd,"illegal value detected in keyval argument\n"); + break; + case ARGV_ERROR_INTERNAL: argv_dprintf(fd,"internal error"); break; @@ -678,8 +856,10 @@ static void argv_show_status( struct argv_ctx * ctx, struct argv_meta * meta) { + int i; int argc; char ** argv; + struct argv_keyval * keyv; struct argv_entry * entry; const struct argv_option * option; char short_name[2] = {0}; @@ -698,7 +878,7 @@ static void argv_show_status( argv_dprintf(fd,"argv[%d]: %s\n",argc,*argv); argv_dprintf(fd,"\n\nparsed entries:\n"); - for (entry=meta->entries; entry->arg || entry->fopt; entry++) + for (entry=meta->entries; entry->arg || entry->fopt; entry++) { if (entry->fopt) { option = option_from_tag(optv,entry->tag); short_name[0] = option->short_name; @@ -709,14 +889,48 @@ static void argv_show_status( else argv_dprintf(fd,"[-%s,--%s]\n", short_name,option->long_name); - } else + + if (entry->keyv) { + for (i=0,keyv=entry->keyv; keyv->keyword; i++,keyv++) { + switch (keyv->flags) { + case ARGV_KEYVAL_ASSIGN: + argv_dprintf(fd,"\tkeyval[%d]: <%s>=%s\n", + i,keyv->keyword,keyv->value); + break; + + case ARGV_KEYVAL_OVERRIDE: + argv_dprintf(fd,"\tkeyval[%d]: <%s>:=%s\n", + i,keyv->keyword,keyv->value); + break; + + default: + argv_dprintf(fd,"\tkeyval[%d]: <%s>\n", + i,keyv->keyword); + break; + } + } + } + } else { argv_dprintf(fd,"<program arg> := %s\n",entry->arg); + } + } argv_dprintf(fd,"\n\n"); } static struct argv_meta * argv_free_impl(struct argv_meta_impl * imeta) { + struct argv_entry * entry; + void * addr; + + if (imeta->keyvbuf) + for (entry=imeta->meta.entries; entry->fopt || entry->arg; entry++) + if (entry->keyv) + free((addr = entry->keyv)); + + if (imeta->keyvbuf) + free(imeta->keyvbuf); + if (imeta->argv) free(imeta->argv); @@ -750,7 +964,8 @@ static struct argv_meta * argv_alloc(char ** argv, struct argv_ctx * ctx) if (!(imeta->argv = calloc(argc+1,sizeof(char *)))) return argv_free_impl(imeta); - else if (!(imeta->strbuf = calloc(1,size+1))) + + if (!(imeta->strbuf = calloc(1,size+1))) return argv_free_impl(imeta); for (i=0,dst=imeta->strbuf; i<argc; i++) { @@ -760,15 +975,27 @@ static struct argv_meta * argv_alloc(char ** argv, struct argv_ctx * ctx) } imeta->meta.argv = imeta->argv; - } else + } else { imeta->meta.argv = argv; + } - if (!(imeta->meta.entries = calloc( - ctx->nentries+1, - sizeof(struct argv_entry)))) + imeta->meta.entries = calloc( + ctx->nentries+1, + sizeof(struct argv_entry)); + + if (!imeta->meta.entries) return argv_free_impl(imeta); - else - return &imeta->meta; + + if (ctx->keyvlen) { + imeta->keyvbuf = calloc( + ctx->keyvlen, + sizeof(char)); + + if (!(imeta->keyvmark = imeta->keyvbuf)) + return argv_free_impl(imeta); + } + + return &imeta->meta; } static struct argv_meta * argv_get( @@ -778,7 +1005,7 @@ static struct argv_meta * argv_get( int fd) { struct argv_meta * meta; - struct argv_ctx ctx = {flags,ARGV_MODE_SCAN,0,0,0,0,0,0,0}; + struct argv_ctx ctx = {flags,ARGV_MODE_SCAN,0,0,0,0,0,0,0,0}; argv_scan(argv,optv,&ctx,0); @@ -833,6 +1060,7 @@ static void argv_usage_impl( { const struct argv_option ** optv; const struct argv_option * option; + int nlong; bool fshort,flong,fboth; size_t len,optlen,desclen; char cache; @@ -865,7 +1093,7 @@ static void argv_usage_impl( if (header) argv_dprintf(fd,"%s",header); - for (optlen=0,optv=options; *optv; optv++) { + for (optlen=0,nlong=0,optv=options; *optv; optv++) { option = *optv; /* indent + comma */ @@ -884,6 +1112,11 @@ static void argv_usage_impl( /* optlen */ if (len > optlen) optlen = len; + + /* long (vs. hybrid-only) option? */ + if (option->long_name) + if (!(option->flags & ARGV_OPTION_HYBRID_ONLY)) + nlong++; } if (optlen >= optcap) { @@ -916,7 +1149,11 @@ static void argv_usage_impl( /* long/hybrid option prefix (-/--) */ prefix = option->flags & ARGV_OPTION_HYBRID_ONLY - ? " -" : "--"; + ? " -" : " --"; + + /* avoid extra <stace> when all long opts are hybrid-only */ + if (nlong == 0) + prefix++; /* option string */ if (fboth && option->short_name && option->long_name) diff --git a/src/internal/tpax_driver_impl.h b/src/internal/tpax_driver_impl.h index cd734b0..02e0d84 100644 --- a/src/internal/tpax_driver_impl.h +++ b/src/internal/tpax_driver_impl.h @@ -7,6 +7,7 @@ #ifndef TPAX_DRIVER_IMPL_H #define TPAX_DRIVER_IMPL_H +#include <regex.h> #include <stdint.h> #include <dirent.h> #include <stdio.h> @@ -28,28 +29,47 @@ #define TPAX_ITEM_EXPLICIT 0X1 #define TPAX_ITEM_IMPLICIT 0X2 +#define TPAX_ITEM_SYMLINK 0X4 +#define TPAX_ITEM_NAMEREF 0x8 + +#define TPAX_REPL_GLOBAL 0x01 +#define TPAX_REPL_PRINT 0x02 extern const struct argv_option tpax_default_options[]; enum app_tags { TAG_HELP, TAG_VERSION, + TAG_VERBOSE, TAG_LIST, TAG_READ, TAG_WRITE, TAG_COPY, + TAG_FILE, TAG_FORMAT, TAG_BLKSIZE, + TAG_OPTIONS, + TAG_REPLSTR, TAG_RECURSE, TAG_NORECURSE, TAG_STRICT_PATH, TAG_PURE_PATH, + TAG_PRESERVE_ATIME, + TAG_PAX_SYMLINK_ARGS, + TAG_PAX_SYMLINK_ITEMS, + TAG_STRICT_DEVICE_ID, }; struct tpax_dirent { int fdat; int depth; int flags; + int nlink; + int cpdev; + int cpino; + dev_t srdev; + dev_t stdev; + ino_t stino; size_t nsize; const char * prefix; const struct tpax_dirent * parent; @@ -64,12 +84,25 @@ struct tpax_dirent_buffer { struct tpax_dirent dbuf[]; }; +struct tpax_replstr { + const char * replarg; + const char * replstr; + const char * regexp; + regex_t regex; + uint32_t flags; +}; + struct tpax_driver_ctx_impl { + const char * file; struct tpax_common_ctx cctx; struct tpax_driver_ctx ctx; struct tpax_fd_ctx fdctx; const struct tpax_unit_ctx * euctx; const char * eunit; + struct stat srcstat; + struct argv_keyval ** keyvalv; + struct tpax_replstr * replstrv; + char * replstrs; struct tpax_error_info ** errinfp; struct tpax_error_info ** erricap; struct tpax_error_info * erriptr[64]; @@ -78,12 +111,18 @@ struct tpax_driver_ctx_impl { char ** prefixp; char ** prefcap; char * prefptr[64]; + struct tpax_dirent ** cpiov; struct tpax_dirent ** direntv; struct tpax_dirent_buffer * dirents; struct tpax_dirent * dirmark; void * dirbuff; void * bufaddr; size_t bufsize; + void * mapaddr; + size_t mapsize; + char * cacheaddr; + char * cachemark; + char * cachecap; size_t nqueued; off_t cpos; }; @@ -258,24 +297,6 @@ static inline void tpax_set_unit_dpos(const struct tpax_unit_ctx * uctx, off_t d ictx->dpos = dpos; } -static inline ssize_t tpax_get_archive_block_size(const struct tpax_driver_ctx * dctx) -{ - if (dctx->cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_PAX) - return TPAX_PAX_BLOCK_SIZE; - - else if (dctx->cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_CPIO) - return TPAX_CPIO_BLOCK_SIZE; - - else if (dctx->cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_USTAR) - return TPAX_USTAR_BLOCK_SIZE; - - else if (dctx->cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_RUSTAR) - return TPAX_USTAR_BLOCK_SIZE; - - else - return 0; -} - int tpax_update_queue_vector(const struct tpax_driver_ctx * dctx); const char * tpax_queue_item_full_path( diff --git a/src/internal/tpax_ftime_impl.c b/src/internal/tpax_ftime_impl.c new file mode 100644 index 0000000..19c6d7b --- /dev/null +++ b/src/internal/tpax_ftime_impl.c @@ -0,0 +1,48 @@ +/**************************************************************/ +/* tpax: a topological pax implementation */ +/* Copyright (C) 2020--2024 SysDeer Technologies, LLC */ +/* Released under GPLv2 and GPLv3; see COPYING.TPAX. */ +/**************************************************************/ + +#include <time.h> +#include <sys/time.h> +#include <sys/stat.h> + +#include "tpax_ftime_impl.h" +#include "tpax_driver_impl.h" +#include "tpax_visibility_impl.h" + +tpax_hidden void tpax_ftime_restore( + const struct tpax_driver_ctx * dctx, + int fd, + const struct stat * refst) +{ + struct timespec ts[2]; + + if (dctx->cctx->drvflags & TPAX_DRIVER_PRESERVE_ATIME) { + ts[0].tv_sec = refst->st_atim.tv_sec; + ts[0].tv_nsec = refst->st_atim.tv_nsec; + } else { + ts[0].tv_nsec = UTIME_OMIT; + } + + if (dctx->cctx->drvflags & TPAX_DRIVER_PRESERVE_MTIME) { + ts[1].tv_sec = refst->st_mtim.tv_sec; + ts[1].tv_nsec = refst->st_mtim.tv_nsec; + } else { + ts[1].tv_nsec = UTIME_OMIT; + } + + if ((ts[0].tv_nsec != UTIME_OMIT) || (ts[1].tv_nsec != UTIME_OMIT)) { + futimens(fd,ts); + } +} + +tpax_hidden void tpax_ftime_restore_and_close( + const struct tpax_driver_ctx * dctx, + int fd, + const struct stat * refst) +{ + tpax_ftime_restore(dctx,fd,refst); + close(fd); +} diff --git a/src/internal/tpax_ftime_impl.h b/src/internal/tpax_ftime_impl.h new file mode 100644 index 0000000..2d0e981 --- /dev/null +++ b/src/internal/tpax_ftime_impl.h @@ -0,0 +1,24 @@ +/**************************************************************/ +/* tpax: a topological pax implementation */ +/* Copyright (C) 2020--2024 SysDeer Technologies, LLC */ +/* Released under GPLv2 and GPLv3; see COPYING.TPAX. */ +/**************************************************************/ + +#ifndef TPAX_FTIME_IMPL_H +#define TPAX_FTIME_IMPL_H + +#include <time.h> +#include <sys/time.h> +#include <sys/stat.h> + +#include "tpax_driver_impl.h" + +void tpax_ftime_restore( + const struct tpax_driver_ctx *, + int fd, const struct stat *); + +void tpax_ftime_restore_and_close( + const struct tpax_driver_ctx *, + int fd, const struct stat *); + +#endif diff --git a/src/io/tpax_create_memory_snapshot.c b/src/io/tpax_create_memory_snapshot.c index 7c3dd7e..6be087f 100644 --- a/src/io/tpax_create_memory_snapshot.c +++ b/src/io/tpax_create_memory_snapshot.c @@ -18,6 +18,7 @@ #include <tpax/tpax_specs.h> #include "tpax_driver_impl.h" #include "tpax_errinfo_impl.h" +#include "tpax_ftime_impl.h" #ifndef ssizeof #define ssizeof(x) (ssize_t)(sizeof(x)) @@ -69,11 +70,11 @@ int tpax_io_create_memory_snapshot( nread = read(fd,ch,cap-ch); if (nread < 0) { - close(fd); + tpax_ftime_restore_and_close(dctx,fd,&dstst); return TPAX_SYSTEM_ERROR(dctx); } else if (nread == 0) { - close(fd); + tpax_ftime_restore_and_close(dctx,fd,&dstst); return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FLOW_ERROR); } else { @@ -81,6 +82,9 @@ int tpax_io_create_memory_snapshot( } } + /* preserve last data access time as needed */ + tpax_ftime_restore(dctx,fd,&dstst); + /* stat compare */ if ((fstat(fd,&dstst)) < 0) { close(fd); diff --git a/src/io/tpax_create_tmpfs_snapshot.c b/src/io/tpax_create_tmpfs_snapshot.c index 9891e98..99a1665 100644 --- a/src/io/tpax_create_tmpfs_snapshot.c +++ b/src/io/tpax_create_tmpfs_snapshot.c @@ -19,6 +19,7 @@ #include "tpax_driver_impl.h" #include "tpax_tmpfile_impl.h" #include "tpax_errinfo_impl.h" +#include "tpax_ftime_impl.h" #ifndef ssizeof #define ssizeof(x) (ssize_t)(sizeof(x)) @@ -78,12 +79,12 @@ int tpax_io_create_tmpfs_snapshot( nbytes = read(fdsrc,buf,buflen); if (nbytes < 0) { - close(fdsrc); + tpax_ftime_restore_and_close(dctx,fdsrc,&dstst); close(fdtmp); return TPAX_SYSTEM_ERROR(dctx); } else if (nbytes == 0) { - close(fdsrc); + tpax_ftime_restore_and_close(dctx,fdsrc,&dstst); close(fdtmp); return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FLOW_ERROR); @@ -98,7 +99,7 @@ int tpax_io_create_tmpfs_snapshot( ret = write(fdtmp,ch,nbytes); if (ret < 0) { - close(fdsrc); + tpax_ftime_restore_and_close(dctx,fdsrc,&dstst); close(fdtmp); return TPAX_SYSTEM_ERROR(dctx); @@ -108,6 +109,9 @@ int tpax_io_create_tmpfs_snapshot( } } + /* preserve last data access time as needed */ + tpax_ftime_restore(dctx,fdsrc,&dstst); + /* stat compare */ if ((fstat(fdsrc,&dstst)) < 0) { close(fdsrc); diff --git a/src/logic/tpax_archive_append.c b/src/logic/tpax_archive_enqueue.c index 6ed414b..7d8c278 100644 --- a/src/logic/tpax_archive_append.c +++ b/src/logic/tpax_archive_enqueue.c @@ -19,10 +19,11 @@ #include <tpax/tpax_specs.h> #include "tpax_driver_impl.h" #include "tpax_getdents_impl.h" +#include "tpax_readlink_impl.h" #include "tpax_tmpfile_impl.h" #include "tpax_errinfo_impl.h" -static char * tpax_append_prefix_item( +static char * tpax_add_prefix_item( const struct tpax_driver_ctx * dctx, const char * prefix) { @@ -65,7 +66,7 @@ static char * tpax_append_prefix_item( return pitem; } -static char * tpax_append_prefix_item_from_path( +static char * tpax_add_prefix_item_from_path( const struct tpax_driver_ctx * dctx, const char * path, const char * mark) @@ -80,7 +81,46 @@ static char * tpax_append_prefix_item_from_path( pathbuf[nbytes++] = '/'; pathbuf[nbytes] = '\0'; - return tpax_append_prefix_item(dctx,pathbuf); + return tpax_add_prefix_item(dctx,pathbuf); +} + +static int tpax_dirent_init_from_stat( + const struct stat * st, + const char * basename, + struct dirent * dirent) +{ + /* st_mode to d_type translation */ + if (S_ISREG(st->st_mode)) + dirent->d_type = DT_REG; + else if (S_ISLNK(st->st_mode)) + dirent->d_type = DT_LNK; + else if (S_ISDIR(st->st_mode)) + dirent->d_type = DT_DIR; + else if (S_ISCHR(st->st_mode)) + dirent->d_type = DT_CHR; + else if (S_ISBLK(st->st_mode)) + dirent->d_type = DT_CHR; + else if (S_ISFIFO(st->st_mode)) + dirent->d_type = DT_CHR; + else + return -1; + + /* d_off, d_ino */ + dirent->d_off = 0; + dirent->d_ino = st->st_ino; + + /* d_reclen */ + dirent->d_reclen = offsetof(struct dirent,d_name); + dirent->d_reclen += strlen(basename) + 1; + + dirent->d_reclen += 0x1; + dirent->d_reclen |= 0x1; + dirent->d_reclen ^= 0x1; + + /* d_name */ + strcpy(dirent->d_name,basename); + + return 0; } static struct tpax_dirent_buffer * tpax_dirent_buf_first_alloc( @@ -137,7 +177,7 @@ static struct tpax_dirent_buffer * tpax_dirent_buf_next_alloc( return current->next; } -static int tpax_archive_append_ret( +static int tpax_archive_enqueue_ret( int ret, struct tpax_unit_ctx * unit) { @@ -147,11 +187,14 @@ static int tpax_archive_append_ret( return ret; } -static int tpax_archive_append_queue_item( +static int tpax_archive_add_queue_item( const struct tpax_driver_ctx * dctx, const struct dirent * dirent, const struct tpax_dirent * parent, const char * prefix, + dev_t srdev, + dev_t stdev, + ino_t stino, int depth, int flags, int fdat, @@ -166,7 +209,7 @@ static int tpax_archive_append_queue_item( if (!(dentbuf = tpax_get_driver_dirents(dctx))) if (!(dentbuf = tpax_dirent_buf_first_alloc(dctx))) - return tpax_archive_append_ret( + return tpax_archive_enqueue_ret( TPAX_SYSTEM_ERROR(dctx), 0); @@ -181,7 +224,7 @@ static int tpax_archive_append_queue_item( if (dentbuf->nfree < needed) if (!(dentbuf = tpax_dirent_buf_next_alloc(dentbuf))) - return tpax_archive_append_ret( + return tpax_archive_enqueue_ret( TPAX_SYSTEM_ERROR(dctx), 0); @@ -191,10 +234,22 @@ static int tpax_archive_append_queue_item( cdent->fdat = fdat; cdent->depth = depth; cdent->flags = flags; + cdent->stdev = stdev; + cdent->stino = stino; cdent->nsize = needed; cdent->parent = parent; cdent->prefix = prefix; + switch (dirent->d_type) { + case DT_CHR: + case DT_BLK: + cdent->srdev = srdev; + break; + + default: + cdent->srdev = 0777777; + } + memset(&cdent->dirent,0,offsetof(struct dirent,d_name)); cdent->dirent.d_ino = dirent->d_ino; @@ -222,20 +277,28 @@ static int tpax_archive_append_queue_item( return 0; } -static int tpax_archive_append_dir_entries( +static int tpax_archive_enqueue_dir_entries( const struct tpax_driver_ctx * dctx, struct tpax_dirent * dent) { int fd; int fdat; + int fdlnk; int depth; bool fkeep; + bool flinks; + bool fstdev; long nbytes; + struct dirent * lnkent; struct dirent * dirent; struct dirent * dirents; + struct tpax_dirent * cdent; struct tpax_unit_ctx * uctx; struct stat st; + struct stat lnkst; uintptr_t addr; + char lnktgt[PATH_MAX]; + char lnkbuf[PATH_MAX + sizeof(struct dirent)]; /* init */ fdat = dent->fdat; @@ -250,10 +313,15 @@ static int tpax_archive_append_dir_entries( /* verify that recursion item is still a directory */ if (!S_ISDIR(uctx->st->st_mode)) - return tpax_archive_append_ret( + return tpax_archive_enqueue_ret( TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FLOW_ERROR), uctx); + /* ensure physical device identity as needed */ + if (dctx->cctx->drvflags & TPAX_DRIVER_STRICT_DEVICE_ID) + if (dent->parent && (uctx->st->st_dev != dent->parent->stdev)) + return 0; + /* obtain buffer for file-system directory entries */ dirents = tpax_get_driver_getdents_buffer(dctx); dirent = dirents; @@ -263,22 +331,28 @@ static int tpax_archive_append_dir_entries( /* open directory and obtain first directory entries */ if ((fd = openat(fdat,dent->dirent.d_name,O_RDONLY|O_DIRECTORY|O_CLOEXEC,0)) < 0) - return tpax_archive_append_ret( + return tpax_archive_enqueue_ret( TPAX_SYSTEM_ERROR(dctx), uctx); + lnkent = (struct dirent *)lnkbuf; + flinks = (dctx->cctx->drvflags & TPAX_DRIVER_PAX_SYMLINK_ITEMS); + fstdev = (dctx->cctx->drvflags & TPAX_DRIVER_STRICT_DEVICE_ID); nbytes = tpax_getdents(fd,dirents,TPAX_DIRENT_BUFLEN); + /* debugging, struct stat initialization when fstdev is false */ + memset(&st,0,sizeof(st)); + while ((nbytes == -EINTR) || ((nbytes < 0) && (errno == EINTR))) nbytes = tpax_getdents(fd,dirents,TPAX_DIRENT_BUFLEN); if (nbytes < 0) - return tpax_archive_append_ret( + return tpax_archive_enqueue_ret( TPAX_SYSTEM_ERROR(dctx), uctx); /* iterate */ - for (; nbytes>0; ) { + for (; nbytes; ) { if (!strcmp(dirent->d_name,".")) { (void)0; @@ -288,23 +362,72 @@ static int tpax_archive_append_dir_entries( } else { if (dirent->d_type == DT_UNKNOWN) { if (fstatat(fd,dirent->d_name,&st,AT_SYMLINK_NOFOLLOW)) - return tpax_archive_append_ret( + return tpax_archive_enqueue_ret( TPAX_SYSTEM_ERROR(dctx), uctx); if (S_ISDIR(st.st_mode)) { dirent->d_type = DT_DIR; } + } else if (fstdev) { + if (fstatat(fd,dirent->d_name,&st,AT_SYMLINK_NOFOLLOW)) + return tpax_archive_enqueue_ret( + TPAX_SYSTEM_ERROR(dctx), + uctx); } - if (tpax_archive_append_queue_item( - dctx,dirent, - dent,0,depth, + if (tpax_archive_add_queue_item( + dctx,dirent,dent,0, + st.st_rdev, + st.st_dev,st.st_ino, + depth, TPAX_ITEM_IMPLICIT, fd,&fkeep) < 0) - return tpax_archive_append_ret( + return tpax_archive_enqueue_ret( TPAX_NESTED_ERROR(dctx), uctx); + + /* follow encountered symlink arguments as needed */ + fdlnk = (flinks && (dirent->d_type == DT_LNK)) + ? openat(fd,dirent->d_name,O_RDONLY|O_CLOEXEC) + : (-1); + + if (fdlnk >= 0) { + if (fstat(fdlnk,&lnkst) <0) { + close(fdlnk); + return tpax_archive_enqueue_ret( + TPAX_SYSTEM_ERROR(dctx), + uctx); + } + + if (tpax_readlinkat(fd,dirent->d_name,lnktgt,PATH_MAX) < 0) + return tpax_archive_enqueue_ret( + TPAX_SYSTEM_ERROR(dctx), + uctx); + + close(fdlnk); + + if (tpax_dirent_init_from_stat(&lnkst,lnktgt,lnkent) < 0) + return tpax_archive_enqueue_ret( + TPAX_CUSTOM_ERROR( + dctx, + TPAX_ERR_FLOW_ERROR), + 0); + + cdent = tpax_get_driver_dirmark(dctx); + cdent->flags |= TPAX_ITEM_NAMEREF; + + if (tpax_archive_add_queue_item( + dctx,lnkent,cdent,0, + lnkst.st_rdev, + lnkst.st_dev,lnkst.st_ino, + depth+1, + TPAX_ITEM_IMPLICIT|TPAX_ITEM_SYMLINK, + fd,&fkeep) < 0) + return tpax_archive_enqueue_ret( + TPAX_NESTED_ERROR(dctx), + 0); + } } addr = (uintptr_t)dirent; @@ -319,7 +442,7 @@ static int tpax_archive_append_dir_entries( nbytes = tpax_getdents(fd,dirents,TPAX_DIRENT_BUFLEN); if (nbytes < 0) - tpax_archive_append_ret( + tpax_archive_enqueue_ret( TPAX_SYSTEM_ERROR(dctx), uctx); @@ -328,7 +451,7 @@ static int tpax_archive_append_dir_entries( } /* all done */ - return tpax_archive_append_ret( + return tpax_archive_enqueue_ret( fkeep ? 0 : close(fd), uctx); } @@ -356,50 +479,12 @@ static const char * tpax_path_prefix_mark(const char * path) return (mark <= pathbuf) ? 0 : &path[--mark-pathbuf]; } -static int tpax_dirent_init_from_uctx( - const struct stat * st, - const char * basename, - struct dirent * dirent) -{ - /* st_mode to d_type translation */ - if (S_ISREG(st->st_mode)) - dirent->d_type = DT_REG; - else if (S_ISLNK(st->st_mode)) - dirent->d_type = DT_LNK; - else if (S_ISDIR(st->st_mode)) - dirent->d_type = DT_DIR; - else if (S_ISCHR(st->st_mode)) - dirent->d_type = DT_CHR; - else if (S_ISBLK(st->st_mode)) - dirent->d_type = DT_CHR; - else if (S_ISFIFO(st->st_mode)) - dirent->d_type = DT_CHR; - else - return -1; - - /* d_off, d_ino */ - dirent->d_off = 0; - dirent->d_ino = st->st_ino; - - /* d_reclen */ - dirent->d_reclen = offsetof(struct dirent,d_name); - dirent->d_reclen += strlen(basename) + 1; - - dirent->d_reclen += 0x1; - dirent->d_reclen |= 0x1; - dirent->d_reclen ^= 0x1; - - /* d_name */ - strcpy(dirent->d_name,basename); - - return 0; -} - -int tpax_archive_append( +int tpax_archive_enqueue( const struct tpax_driver_ctx * dctx, const struct tpax_unit_ctx * uctx) { int fdat; + int fdlnk; uintptr_t addr; const char * name; const char * mark; @@ -409,18 +494,22 @@ int tpax_archive_append( struct tpax_dirent * cdent; struct tpax_dirent * cnext; struct dirent * dirent; + struct dirent * lnkent; + struct stat lnkst; char entbuf[PATH_MAX + sizeof(struct dirent)]; + char lnkbuf[PATH_MAX + sizeof(struct dirent)]; /* init */ fdat = tpax_driver_fdcwd(dctx); dirent = (struct dirent *)entbuf; + lnkent = (struct dirent *)lnkbuf; prefix = 0; /* split path to prefix + basename */ if ((mark = tpax_path_prefix_mark(*uctx->path))) - if (!(prefix = tpax_append_prefix_item_from_path( + if (!(prefix = tpax_add_prefix_item_from_path( dctx,*uctx->path,mark))) - return tpax_archive_append_ret( + return tpax_archive_enqueue_ret( TPAX_BUFFER_ERROR(dctx), 0); @@ -428,35 +517,75 @@ int tpax_archive_append( if (prefix) if ((fdat = openat(fdat,prefix,O_RDONLY|O_DIRECTORY|O_CLOEXEC,0)) < 0) - return tpax_archive_append_ret( + return tpax_archive_enqueue_ret( TPAX_SYSTEM_ERROR(dctx), 0); /* explicit item directory entry */ - if (tpax_dirent_init_from_uctx(uctx->st,name,dirent) < 0) - return tpax_archive_append_ret( + if (tpax_dirent_init_from_stat(uctx->st,name,dirent) < 0) + return tpax_archive_enqueue_ret( TPAX_CUSTOM_ERROR( dctx, TPAX_ERR_FLOW_ERROR), 0); /* add to queue */ - if (tpax_archive_append_queue_item( - dctx,dirent,0,prefix,0, + if (tpax_archive_add_queue_item( + dctx,dirent,0,prefix, + uctx->st->st_rdev, + uctx->st->st_dev, + uctx->st->st_ino,0, TPAX_ITEM_EXPLICIT, fdat,&fkeep) < 0) - return tpax_archive_append_ret( + return tpax_archive_enqueue_ret( TPAX_NESTED_ERROR(dctx), 0); + /* follow command-line symlink arguments as needed */ + fdlnk = (uctx->link[0] && (dctx->cctx->drvflags & TPAX_DRIVER_PAX_SYMLINK_ARGS)) + ? openat(fdat,uctx->link[0],O_RDONLY|O_CLOEXEC) : (-1); + + if (fdlnk >= 0) { + if (fstat(fdlnk,&lnkst) <0) { + close(fdlnk); + return tpax_archive_enqueue_ret( + TPAX_SYSTEM_ERROR(dctx), + 0); + } + + close(fdlnk); + + if (tpax_dirent_init_from_stat(&lnkst,uctx->link[0],lnkent) < 0) + return tpax_archive_enqueue_ret( + TPAX_CUSTOM_ERROR( + dctx, + TPAX_ERR_FLOW_ERROR), + 0); + + cdent = tpax_get_driver_dirmark(dctx); + cdent->flags |= TPAX_ITEM_NAMEREF; + + if (tpax_archive_add_queue_item( + dctx,lnkent,cdent,0, + lnkst.st_rdev, + lnkst.st_dev,lnkst.st_ino, + 1, + TPAX_ITEM_EXPLICIT|TPAX_ITEM_SYMLINK, + fdat,&fkeep) < 0) + return tpax_archive_enqueue_ret( + TPAX_NESTED_ERROR(dctx), + 0); + } + /* queue directory child items */ dentbuf = tpax_get_driver_dirents(dctx); cdent = tpax_get_driver_dirmark(dctx); for (; cdent; ) { if (cdent->dirent.d_type == DT_DIR) - if (tpax_archive_append_dir_entries(dctx,cdent) < 0) - return TPAX_NESTED_ERROR(dctx); + if (dctx->cctx->drvflags & TPAX_DRIVER_DIR_MEMBER_RECURSE) + if (tpax_archive_enqueue_dir_entries(dctx,cdent) < 0) + return TPAX_NESTED_ERROR(dctx); addr = (uintptr_t)cdent; addr += cdent->nsize; diff --git a/src/logic/tpax_archive_reset.c b/src/logic/tpax_archive_reset.c new file mode 100644 index 0000000..52a9008 --- /dev/null +++ b/src/logic/tpax_archive_reset.c @@ -0,0 +1,59 @@ +/**************************************************************/ +/* tpax: a topological pax implementation */ +/* Copyright (C) 2020--2024 SysDeer Technologies, LLC */ +/* Released under GPLv2 and GPLv3; see COPYING.TPAX. */ +/**************************************************************/ + +#include <stdint.h> +#include <stdlib.h> +#include <sys/mman.h> + +#include <tpax/tpax.h> +#include "tpax_driver_impl.h" + +/**********************************************/ +/* release and reset the following objects: */ +/* */ +/* - item queue */ +/* - queue vector */ +/* - cached prefixes */ +/* - prefix vector */ +/* */ +/**********************************************/ + +int tpax_archive_reset(const struct tpax_driver_ctx * dctx) +{ + struct tpax_driver_ctx_impl * ictx; + void * next; + size_t size; + char ** ppref; + + ictx = tpax_get_driver_ictx(dctx); + + for (; ictx->dirents; ) { + next = ictx->dirents->next; + size = ictx->dirents->size; + + munmap(ictx->dirents,size); + ictx->dirents = (struct tpax_dirent_buffer *)next; + } + + for (ppref=ictx->prefixv; *ppref; ppref++) + free(*ppref); + + for (ppref=ictx->prefptr; ppref<ictx->prefcap; ppref++) + *ppref = 0; + + if (ictx->prefixv != ictx->prefptr) + free(ictx->prefixv); + + if (ictx->direntv) + free(ictx->direntv); + + ictx->nqueued = 0; + ictx->dirents = 0; + ictx->direntv = 0; + ictx->prefixv = ictx->prefptr; + + return 0; +} diff --git a/src/logic/tpax_archive_write.c b/src/logic/tpax_archive_write.c index 5214787..492d3b1 100644 --- a/src/logic/tpax_archive_write.c +++ b/src/logic/tpax_archive_write.c @@ -13,13 +13,23 @@ #define ssizeof(x) (ssize_t)(sizeof(x)) #endif +static const char tpax_cpio_trailer[11] = TPAX_CPIO_TRAILER; + +static void tpax_cpio_octal_write(char * ch, ssize_t len, uint64_t val) +{ + for (; len; ) { + ch[--len] = val % 8 + '0'; + val /= 8; + } +} + static int tpax_archive_append_memory_data( int fdout, - void * buf, + const void * buf, ssize_t nbytes) { - ssize_t ret; - char * ch; + ssize_t ret; + const char * ch; for (ch=buf; nbytes; ch+=ret) { ret = write(fdout,ch,nbytes); @@ -46,6 +56,9 @@ static int tpax_archive_append_pad( ssize_t nbytes; char buf[512]; + if (dctx->cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_CPIO) + return 0; + nbytes = st->st_size; nbytes += 0x1ff; nbytes |= 0x1ff; @@ -64,31 +77,79 @@ static int tpax_archive_append_pad( } static int tpax_archive_write_ret( - int ret, - struct tpax_unit_ctx * uctx) + int ret, + const struct tpax_driver_ctx * dctx, + struct tpax_unit_ctx * uctx) { + if (dctx->cctx->drvflags & TPAX_DRIVER_VERBOSE) + tpax_dprintf(tpax_driver_fderr(dctx),"\n",0); + tpax_lib_free_unit_ctx(uctx); 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, int fdcwd, int fdout) { + int ret; struct tpax_unit_ctx * uctx; + struct tpax_cpio_header chdr; 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; size_t buflen; size_t cmplen; void * membuf; + char * ch; + char replbuf[PATH_MAX]; + char pathbuf[PATH_MAX]; + + /* followed symlink? */ + if (cdent->flags & TPAX_ITEM_NAMEREF) + return 0; /* full path */ if (!(path = tpax_queue_item_full_path(dctx,cdent))) @@ -96,85 +157,183 @@ 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",apath); + /* uctx */ if (tpax_lib_get_unit_ctx(dctx,fdcwd,path,&uctx) < 0) - return TPAX_NESTED_ERROR(dctx); + return tpax_archive_write_ret( + TPAX_NESTED_ERROR(dctx), + dctx,0); + + st = uctx->st; + slnk = uctx->link[0]; + mlnk = 0; + + if (cdent->flags & TPAX_ITEM_SYMLINK) { + st = &stbuf; + mlnk = slnk; + slnk = 0; + ch = 0; + + if (mlnk[0] != '/') { + if (strlen(path) >= PATH_MAX) + return tpax_archive_write_ret( + TPAX_CUSTOM_ERROR( + dctx, + TPAX_ERR_FLOW_ERROR), + dctx,uctx); + + strcpy(pathbuf,path); + + ch = strrchr(pathbuf,'/'); + } + + if (ch && (++ch - pathbuf >= PATH_MAX)) + return tpax_archive_write_ret( + TPAX_CUSTOM_ERROR( + dctx, + TPAX_ERR_FLOW_ERROR), + dctx,uctx); + + if (ch) { + strcpy(ch,mlnk); + mlnk = pathbuf; + } + + if (fstatat(fdcwd,mlnk,&stbuf,0) < 0) + return tpax_archive_write_ret( + TPAX_SYSTEM_ERROR(dctx), + dctx,uctx); + } /* record errors */ tpax_driver_set_ectx( dctx,0,path); - /* header and data offsets: todo pax and cpio */ + /* header offset */ hpos = tpax_get_driver_cpos(dctx); - dpos = hpos + sizeof(uhdr); /* header */ - if (tpax_meta_init_ustar_header( - dctx,path,uctx->st, - *uctx->link,&uhdr) < 0) + if (dctx->cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_RUSTAR) { + ret = tpax_meta_init_rustar_header(apath,st,slnk,&uhdr); + + } else if (dctx->cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_USTAR) { + ret = tpax_meta_init_ustar_header(apath,st,slnk,&uhdr); + + } else if (dctx->cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_CPIO) { + ret = tpax_meta_init_cpio_header( + apath,st,slnk, + cdent->cpdev, + cdent->cpino, + cdent->nlink, + &chdr); + } + + if (ret < 0) return tpax_archive_write_ret( TPAX_NESTED_ERROR(dctx), - uctx); + dctx,uctx); /* buffer */ membuf = 0; fdtmp = -1; /* associated data? */ - if S_ISREG(uctx->st->st_mode) { + if S_ISREG(st->st_mode) { buf = tpax_get_driver_anon_map_addr( dctx,&buflen); - if (buflen >= (cmplen = uctx->st->st_size)) + if (buflen >= (cmplen = st->st_size)) membuf = buf; /* snapshot */ if (membuf) { if (tpax_io_create_memory_snapshot( - dctx,fdcwd,path, - uctx->st,membuf) < 0) + dctx,fdcwd, + mlnk ? mlnk : path, + st,membuf) < 0) return tpax_archive_write_ret( TPAX_NESTED_ERROR(dctx), - uctx); + dctx,uctx); } else { if ((fdtmp = tpax_io_create_tmpfs_snapshot( - dctx,fdcwd,path, - uctx->st)) < 0) + dctx,fdcwd, + mlnk ? mlnk : path, + st)) < 0) return tpax_archive_write_ret( TPAX_NESTED_ERROR(dctx), - uctx); + dctx,uctx); if (lseek(fdtmp,0,SEEK_SET) < 0) return tpax_archive_write_ret( TPAX_SYSTEM_ERROR(dctx), - uctx); + dctx,uctx); } } - /* append header */ - if (tpax_archive_append_memory_data(fdout,&uhdr,ssizeof(uhdr)) < 0) { + /* append header (and cpio symbolic link data) */ + if (dctx->cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_CPIO) { + ret = tpax_archive_append_memory_data( + fdout,&chdr, + offsetof(struct tpax_cpio_header,c_namedata)); + + if (ret == 0) + ret = tpax_archive_append_memory_data( + fdout,apath, + strlen(apath) + 1); + + if ((ret == 0) && slnk) + ret = tpax_archive_append_memory_data( + fdout,slnk, + strlen(slnk)); + } else { + ret = tpax_archive_append_memory_data( + fdout,&uhdr, + ssizeof(uhdr)); + } + + if (ret < 0) { if (fdtmp >= 0) close(fdtmp); return tpax_archive_write_ret( TPAX_SYSTEM_ERROR(dctx), - uctx); + dctx,uctx); + } + + /* data offset */ + if (dctx->cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_CPIO) { + dpos = hpos; + dpos += offsetof(struct tpax_cpio_header,c_namedata); + dpos += strlen(apath) + 1; + dpos += slnk ? strlen(slnk) : 0; + } else { + dpos = hpos + ssizeof(uhdr); } tpax_set_driver_cpos(dctx,dpos); /* all done? */ - if (!(S_ISREG(uctx->st->st_mode))) { - tpax_lib_free_unit_ctx(uctx); - return 0; - } + if (!(S_ISREG(st->st_mode))) + return tpax_archive_write_ret( + 0,dctx,uctx); /* append data from snapshot */ if (fdtmp >= 0) { buf = tpax_get_driver_anon_map_addr( dctx,&buflen); - for (nread=0; nread<uctx->st->st_size; ) { + for (nread=0; nread<st->st_size; ) { nbytes = read(fdtmp,buf,buflen); while ((nbytes < 0) && (errno == EINTR)) @@ -184,13 +343,13 @@ static int tpax_archive_write_impl( close(fdtmp); return tpax_archive_write_ret( TPAX_SYSTEM_ERROR(dctx), - uctx); + dctx,uctx); } else if (nbytes == 0) { close(fdtmp); return tpax_archive_write_ret( TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FLOW_ERROR), - uctx); + dctx,uctx); } else { nread += nbytes; @@ -200,7 +359,7 @@ static int tpax_archive_write_impl( close(fdtmp); return tpax_archive_write_ret( TPAX_SYSTEM_ERROR(dctx), - uctx); + dctx,uctx); } } @@ -208,15 +367,15 @@ static int tpax_archive_write_impl( } else { if (tpax_archive_append_memory_data( fdout,membuf, - uctx->st->st_size) < 0) + st->st_size) < 0) return tpax_archive_write_ret( TPAX_SYSTEM_ERROR(dctx), - uctx); + dctx,uctx); } return tpax_archive_write_ret( - tpax_archive_append_pad(dctx,fdout,uctx->st), - uctx); + tpax_archive_append_pad(dctx,fdout,st), + dctx,uctx); } int tpax_archive_write(const struct tpax_driver_ctx * dctx) @@ -242,6 +401,83 @@ int tpax_archive_write(const struct tpax_driver_ctx * dctx) return 0; } +static int tpax_archive_seal_cpio(const struct tpax_driver_ctx * dctx) +{ + int fdout; + off_t cpos; + ssize_t nbytes; + ssize_t blksize; + ssize_t nwritten; + char buf[1024]; + struct tpax_cpio_header chdr; + + /* trailer initialization */ + memset(buf,0,sizeof(buf)); + memset(&chdr,0,sizeof(chdr)); + + tpax_cpio_octal_write(chdr.c_magic,sizeof(chdr.c_magic),0070707); + tpax_cpio_octal_write(chdr.c_dev,sizeof(chdr.c_dev),0); + tpax_cpio_octal_write(chdr.c_ino,sizeof(chdr.c_ino),0); + tpax_cpio_octal_write(chdr.c_mode,sizeof(chdr.c_mode),0); + tpax_cpio_octal_write(chdr.c_uid,sizeof(chdr.c_uid),0); + tpax_cpio_octal_write(chdr.c_gid,sizeof(chdr.c_gid),0); + tpax_cpio_octal_write(chdr.c_nlink,sizeof(chdr.c_nlink),1); + tpax_cpio_octal_write(chdr.c_rdev,sizeof(chdr.c_rdev),0); + tpax_cpio_octal_write(chdr.c_mtime,sizeof(chdr.c_mtime),0); + + tpax_cpio_octal_write(chdr.c_filesize,sizeof(chdr.c_filesize),0); + tpax_cpio_octal_write(chdr.c_namesize,sizeof(chdr.c_namesize),sizeof(tpax_cpio_trailer)); + + /* fdout, archive block size, current position */ + fdout = tpax_driver_fdout(dctx); + blksize = dctx->cctx->blksize; + cpos = tpax_get_driver_cpos(dctx); + + /* trailer header */ + nbytes = offsetof(struct tpax_cpio_header,c_namedata); + + if (tpax_archive_append_memory_data(fdout,&chdr,nbytes) < 0) + return TPAX_SYSTEM_ERROR(dctx); + + cpos += nbytes; + + /* trailer c_namedata[] */ + nbytes = sizeof(tpax_cpio_trailer); + + if (tpax_archive_append_memory_data(fdout,tpax_cpio_trailer,nbytes) < 0) + return TPAX_SYSTEM_ERROR(dctx); + + cpos += nbytes; + + /* pad the current block to a 512 byte boundary */ + nbytes = (cpos % 512) ? 512 - (cpos % 512) : 0; + + if (tpax_archive_append_memory_data(fdout,buf,nbytes) < 0) + return TPAX_SYSTEM_ERROR(dctx); + + cpos += nbytes; + + /* already at block boundary? */ + if ((cpos % blksize) == 0) { + tpax_set_driver_cpos(dctx,cpos); + return 0; + } + + /* zero-fill the remainder of the block */ + nbytes = cpos / blksize; + nbytes *= blksize; + nbytes += blksize; + + for (nwritten=cpos; nwritten<nbytes; nwritten+=512) + if (tpax_archive_append_memory_data(fdout,buf,512) < 0) + return TPAX_SYSTEM_ERROR(dctx); + + /* all done */ + tpax_set_driver_cpos(dctx,nwritten); + + return 0; +} + int tpax_archive_seal(const struct tpax_driver_ctx * dctx) { int fdout; @@ -249,37 +485,52 @@ int tpax_archive_seal(const struct tpax_driver_ctx * dctx) ssize_t nbytes; ssize_t nwritten; ssize_t blksize; - char buf[512]; + char buf[1024]; + + /* cpio trailer? */ + if (dctx->cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_CPIO) + return tpax_archive_seal_cpio(dctx); - blksize = tpax_get_archive_block_size(dctx); + /* archive block size, current position */ + blksize = dctx->cctx->blksize; cpos = tpax_get_driver_cpos(dctx); + /* internal error? */ if (cpos % 512) return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FLOW_ERROR); + /* ustar, pax */ fdout = tpax_driver_fdout(dctx); memset(buf,0,sizeof(buf)); - switch (cpos % blksize) { - case 0: - nbytes = cpos + blksize; - break; + if (tpax_archive_append_memory_data(fdout,buf,2*512) < 0) + return TPAX_SYSTEM_ERROR(dctx); - default: - nbytes = cpos / blksize; - nbytes *= blksize; - nbytes += blksize; + cpos += 2*512; - if (nbytes-cpos == 512) - nbytes += blksize; + /* pax? */ + if (dctx->cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_PAX) { + tpax_set_driver_cpos(dctx,cpos); + return 0; } - for (nwritten=cpos; nwritten<nbytes; nwritten+=512) { + /* already at block boundary? */ + if ((cpos % blksize) == 0) { + tpax_set_driver_cpos(dctx,cpos); + return 0; + } + + /* zero-fill the remainder of the block */ + nbytes = cpos / blksize; + nbytes *= blksize; + nbytes += blksize; + + for (nwritten=cpos; nwritten<nbytes; nwritten+=512) if (tpax_archive_append_memory_data(fdout,buf,512) < 0) return TPAX_SYSTEM_ERROR(dctx); - tpax_set_driver_cpos(dctx,nwritten); - } + /* all done */ + tpax_set_driver_cpos(dctx,nwritten); return 0; } diff --git a/src/logic/tpax_queue_vector.c b/src/logic/tpax_queue_vector.c index 82930b1..a74cf1f 100644 --- a/src/logic/tpax_queue_vector.c +++ b/src/logic/tpax_queue_vector.c @@ -38,15 +38,137 @@ tpax_hidden const char * tpax_queue_item_full_path( ch += sprintf(ch,"%s",pdirent[0]->prefix); for (; pdirent > dirstck; ) { - ch += sprintf(ch,"%s/",pdirent[0]->dirent.d_name); + if (!(pdirent[0]->flags & TPAX_ITEM_SYMLINK)) + ch += sprintf(ch,"%s/",pdirent[0]->dirent.d_name); + pdirent--; } - sprintf(ch,"%s",pdirent[0]->dirent.d_name); + if (pdirent[0]->flags & TPAX_ITEM_SYMLINK) { + *--ch = '\0'; + } else { + sprintf(ch,"%s",pdirent[0]->dirent.d_name); + } return pathbuf; } +static int tpax_cpio_dirent_compare(const void * a, const void * b) +{ + const struct tpax_dirent * direnta; + const struct tpax_dirent * direntb; + + direnta = *(const struct tpax_dirent **)a; + direntb = *(const struct tpax_dirent **)b; + + return (direnta->stdev == direntb->stdev) + ? direnta->stino - direntb->stino + : direnta->stdev - direntb->stdev; +} + +static int tpax_update_cpio_queue_vector(const struct tpax_driver_ctx * dctx) +{ + struct tpax_driver_ctx_impl * ictx; + struct tpax_dirent ** cpiov; + struct tpax_dirent ** direntv; + struct tpax_dirent * cdent; + dev_t stdev; + ino_t stino; + int cpdev; + int cpino; + int nlink; + int idx; + + /* driver */ + ictx = tpax_get_driver_ictx(dctx); + + /* not needed? */ + if (ictx->nqueued == 0) + return 0; + + /* vector copy */ + direntv = ictx->direntv; + cpiov = ictx->cpiov; + + for (; *direntv; ) + *cpiov++ = *direntv++; + + /* sort by real st_dev, st_ino */ + qsort(ictx->cpiov,ictx->nqueued,sizeof(*cpiov),tpax_cpio_dirent_compare); + + /* set the cpio internal c_dev, c_ino, and c_nlink fields (U+1F620) */ + cpiov = ictx->cpiov; + cdent = cpiov[0]; + cpiov++; + + cpdev = 1; + cpino = 1; + nlink = 1; + + stdev = cdent->stdev; + stino = cdent->stino; + + cdent->cpdev = cpdev; + cdent->cpino = cpino; + cdent->nlink = nlink; + + for (; *cpiov; ) { + cdent = *cpiov; + + if (cdent->srdev > 0777777) + return TPAX_CUSTOM_ERROR( + dctx, + TPAX_ERR_FLOW_ERROR); + + if ((cdent->stdev == stdev) && (cdent->stino == stino)) { + nlink++; + + } else if (cdent->stdev > stdev) { + if (nlink > 1) + for (idx=2; idx<=nlink; idx++) + cpiov[-idx]->nlink = nlink; + + cpdev++; + cpino = 1; + nlink = 1; + + stdev = cdent->stdev; + stino = cdent->stino; + + if (cpdev > 0777777) + return TPAX_CUSTOM_ERROR( + dctx, + TPAX_ERR_FLOW_ERROR); + + } else if (cdent->stino > stino) { + if (nlink > 1) + for (idx=2; idx<=nlink; idx++) + cpiov[-idx]->nlink = nlink; + + cpino++; + stino = cdent->stino; + nlink = 1; + + if (cpino > 0777777) + return TPAX_CUSTOM_ERROR( + dctx, + TPAX_ERR_FLOW_ERROR); + } + + cdent->cpdev = cpdev; + cdent->cpino = cpino; + cdent->nlink = nlink; + + cpiov++; + } + + if (nlink > 1) + for (idx=2; idx<=nlink; idx++) + cpiov[-idx]->nlink = nlink; + + return 0; +} + tpax_hidden int tpax_update_queue_vector(const struct tpax_driver_ctx * dctx) { uintptr_t addr; @@ -61,20 +183,26 @@ tpax_hidden int tpax_update_queue_vector(const struct tpax_driver_ctx * dctx) ictx = tpax_get_driver_ictx(dctx); /* vector alloc */ - if (ictx->direntv) + if (ictx->direntv) { free(ictx->direntv); + free(ictx->cpiov); + + ictx->direntv = 0; + ictx->cpiov = 0; + } arrsize = ictx->nqueued + 1; if (!(ictx->direntv = calloc(arrsize,sizeof(struct tpax_dirent *)))) return TPAX_SYSTEM_ERROR(dctx); - if (ictx->nqueued == 0) - return 0; + if (dctx->cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_CPIO) + if (!(ictx->cpiov = calloc(arrsize,sizeof(struct tpax_dirent *)))) + return TPAX_SYSTEM_ERROR(dctx); /* queue vector */ dentbuf = tpax_get_driver_dirents(dctx); - cdent = dentbuf->dbuf; + cdent = ictx->nqueued ? dentbuf->dbuf : 0; for (direntv=ictx->direntv; cdent; direntv++) { *direntv = cdent; @@ -91,5 +219,9 @@ tpax_hidden int tpax_update_queue_vector(const struct tpax_driver_ctx * dctx) cdent = cnext; } + if (dctx->cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_CPIO) + if (tpax_update_cpio_queue_vector(dctx) < 0) + return TPAX_NESTED_ERROR(dctx); + return 0; } diff --git a/src/meta/tpax_init_cpio_header.c b/src/meta/tpax_init_cpio_header.c new file mode 100644 index 0000000..3de22f6 --- /dev/null +++ b/src/meta/tpax_init_cpio_header.c @@ -0,0 +1,145 @@ +/**************************************************************/ +/* tpax: a topological pax implementation */ +/* Copyright (C) 2020--2024 SysDeer Technologies, LLC */ +/* Released under GPLv2 and GPLv3; see COPYING.TPAX. */ +/**************************************************************/ + +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <errno.h> +#include <grp.h> +#include <pwd.h> +#include <sys/stat.h> + +#include <tpax/tpax.h> +#include <tpax/tpax_specs.h> +#include "tpax_driver_impl.h" + +#ifndef ssizeof +#define ssizeof(x) (ssize_t)(sizeof(x)) +#endif + +#define TPAX_CPIO_PERM_MASK \ + ( S_ISUID | S_ISGID \ + | S_IRUSR | S_IWUSR | S_IXUSR \ + | S_IRGRP | S_IWGRP | S_IXGRP \ + | S_IROTH | S_IWOTH | S_IXOTH ) + +static void tpax_octal_write(char * ch, ssize_t len, uint64_t val) +{ + for (; len; ) { + ch[--len] = val % 8 + '0'; + val /= 8; + } +} + +int tpax_meta_init_cpio_header( + const char * path, + const struct stat * st, + const char * linkname, + int cdev, + int cino, + int cnlink, + struct tpax_cpio_header * chdr) +{ + size_t fnsize; + size_t lnklen; + size_t stsize; + int64_t stmtim; + uint32_t typeflag; + uint32_t permbits; + uint32_t modebits; + + /* filename size */ + fnsize = strlen(path) + 1; + + /* size & mtime validation */ + stsize = S_ISREG(st->st_mode) ? st->st_size : 0; + stmtim = st->st_mtim.tv_sec; + + if (stsize > 077777777777) + return -1; + + if ((stmtim < 0) || (stmtim > 077777777777)) + return -1; + + /* linkname validation */ + if (S_ISLNK(st->st_mode) && !linkname) + return -1; + + lnklen = S_ISLNK(st->st_mode) + ? strlen(linkname) + : 0; + + /* typeflag validation */ + if (S_ISREG(st->st_mode)) + typeflag = TPAX_CPIO_FILEMODE_ISREG; + else if (S_ISLNK(st->st_mode)) + typeflag = TPAX_CPIO_FILEMODE_ISLNK; + else if (S_ISDIR(st->st_mode)) + typeflag = TPAX_CPIO_FILEMODE_ISDIR; + else if (S_ISCHR(st->st_mode)) + typeflag = TPAX_CPIO_FILEMODE_ISCHR; + else if (S_ISBLK(st->st_mode)) + typeflag = TPAX_CPIO_FILEMODE_ISBLK; + else if (S_ISFIFO(st->st_mode)) + typeflag = TPAX_CPIO_FILEMODE_ISFIFO; + else if (S_ISSOCK(st->st_mode)) + typeflag = TPAX_CPIO_FILEMODE_ISSOCK; + else + return -1; + + /* permbits, modeflag */ + permbits = st->st_mode & TPAX_CPIO_PERM_MASK; + modebits = permbits | typeflag; + + /* one shot */ + memset(chdr,0,sizeof(*chdr)); + + /* c_magic */ + chdr->c_magic[0] = '0'; + chdr->c_magic[1] = '7'; + chdr->c_magic[2] = '0'; + chdr->c_magic[3] = '7'; + chdr->c_magic[4] = '0'; + chdr->c_magic[5] = '7'; + + /* c_dev, c_ino */ + tpax_octal_write(chdr->c_dev,ssizeof(chdr->c_dev),cdev); + tpax_octal_write(chdr->c_ino,ssizeof(chdr->c_ino),cino); + + /* c_mode */ + tpax_octal_write(chdr->c_mode,ssizeof(chdr->c_mode),modebits); + + /* c_uid, c_gid */ + if ((uint64_t)st->st_uid <= 0777777) + tpax_octal_write(chdr->c_uid,ssizeof(chdr->c_uid),st->st_uid); + else + tpax_octal_write(chdr->c_uid,ssizeof(chdr->c_uid),0); + + if ((uint64_t)st->st_gid <= 0777777) + tpax_octal_write(chdr->c_gid,ssizeof(chdr->c_gid),st->st_gid); + else + tpax_octal_write(chdr->c_gid,ssizeof(chdr->c_gid),0); + + /* c_nlink */ + tpax_octal_write(chdr->c_nlink,ssizeof(chdr->c_nlink),cnlink); + + /* c_rdev */ + tpax_octal_write(chdr->c_rdev,ssizeof(chdr->c_rdev),st->st_rdev); + + /* c_mtime */ + tpax_octal_write(chdr->c_mtime,ssizeof(chdr->c_mtime),stmtim); + + /* c_namesize */ + tpax_octal_write(chdr->c_namesize,ssizeof(chdr->c_namesize),fnsize); + + /* c_filesize */ + tpax_octal_write(chdr->c_filesize,ssizeof(chdr->c_filesize),lnklen ? lnklen : stsize); + + /* all done; c_name to be written along with c_filedata */ + return 0; +} diff --git a/src/meta/tpax_init_ustar_header.c b/src/meta/tpax_init_ustar_header.c index 2479e28..5078344 100644 --- a/src/meta/tpax_init_ustar_header.c +++ b/src/meta/tpax_init_ustar_header.c @@ -10,6 +10,7 @@ #include <unistd.h> #include <fcntl.h> #include <errno.h> +#include <stdbool.h> #include <grp.h> #include <pwd.h> #include <sys/stat.h> @@ -30,12 +31,12 @@ static void tpax_octal_write(char * ch, ssize_t len, uint64_t val) } } -int tpax_meta_init_ustar_header( - const struct tpax_driver_ctx * dctx, +static int tpax_meta_init_ustar_header_impl( const char * path, const struct stat * st, const char * linkname, - struct tpax_ustar_header * uhdr) + struct tpax_ustar_header * uhdr, + bool frustar) { size_t len; const char * cap; @@ -140,7 +141,7 @@ int tpax_meta_init_ustar_header( tpax_octal_write(uhdr->u_mode,ssizeof(uhdr->u_mode),st->st_mode & TPAX_USTAR_MODE_MASK); /* u_uid, u_gid, u_uname, u_gname */ - if (dctx->cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_RUSTAR) { + if (frustar) { tpax_octal_write(uhdr->u_uid,ssizeof(uhdr->u_uid),0); tpax_octal_write(uhdr->u_gid,ssizeof(uhdr->u_gid),0); } else { @@ -216,3 +217,21 @@ int tpax_meta_init_ustar_header( /* all done; caller may now change REGFILE to HARDLINK */ return 0; } + +int tpax_meta_init_ustar_header( + const char * path, + const struct stat * st, + const char * linkname, + struct tpax_ustar_header * uhdr) +{ + return tpax_meta_init_ustar_header_impl(path,st,linkname,uhdr,false); +} + +int tpax_meta_init_rustar_header( + const char * path, + const struct stat * st, + const char * linkname, + struct tpax_ustar_header * uhdr) +{ + return tpax_meta_init_ustar_header_impl(path,st,linkname,uhdr,true); +} diff --git a/src/skin/tpax_skin_default.c b/src/skin/tpax_skin_default.c index 68491df..dd62b86 100644 --- a/src/skin/tpax_skin_default.c +++ b/src/skin/tpax_skin_default.c @@ -3,31 +3,50 @@ #include "argv/argv.h" const tpax_hidden struct argv_option tpax_default_options[] = { - {"version", 0,TAG_VERSION,ARGV_OPTARG_NONE,0,0,0, + {"Wversion", 0,TAG_VERSION,ARGV_OPTARG_NONE, + ARGV_OPTION_HYBRID_ONLY,0,0, "show version information"}, - {"help", 0,TAG_HELP,ARGV_OPTARG_OPTIONAL,0,"short|long",0, - "show usage information [listing %s options only]"}, + {"Whelp", 0,TAG_HELP,ARGV_OPTARG_NONE, + ARGV_OPTION_HYBRID_ONLY,0,0, + "show usage information"}, - {"list", 0,TAG_LIST,ARGV_OPTARG_NONE,0,0,0, + {"Wlist", 0,TAG_LIST,ARGV_OPTARG_NONE, + ARGV_OPTION_HYBRID_ONLY,0,0, "list mode (output names of archive members)"}, - {"read", 'r',TAG_READ,ARGV_OPTARG_NONE,0,0,0, + {"Wread", 'r',TAG_READ,ARGV_OPTARG_NONE, + ARGV_OPTION_HYBRID_ONLY,0,0, "read mode (extract matching archive members)"}, - {"write", 'w',TAG_WRITE,ARGV_OPTARG_NONE,0,0,0, + {"Write", 'w',TAG_WRITE,ARGV_OPTARG_NONE, + ARGV_OPTION_HYBRID_ONLY,0,0, "write mode (add specified files to archive)"}, - {"copy", 0,TAG_COPY,ARGV_OPTARG_NONE,0,0,0, + {"Wcopy", 0,TAG_COPY,ARGV_OPTARG_NONE, + ARGV_OPTION_HYBRID_ONLY,0,0, "copy mode (copy specified files " "to a specified destination directory)"}, + {"Wfile", 'f',TAG_FILE,ARGV_OPTARG_REQUIRED, + ARGV_OPTION_HYBRID_ONLY|ARGV_OPTION_HYBRID_EQUAL, + 0,"<ARCHIVE>", + "read from (in read or list modes), " + "or write to (in write mode) the specified %s " + "after (in write mode) creating it as necessary"}, - {"format", 'x',TAG_FORMAT,ARGV_OPTARG_REQUIRED,0, + {"Wformat", 'x',TAG_FORMAT,ARGV_OPTARG_REQUIRED, + ARGV_OPTION_HYBRID_ONLY|ARGV_OPTION_HYBRID_EQUAL, "pax|cpio|ustar|rustar",0, "archive format [%s]"}, - {"blksize", 'b',TAG_BLKSIZE,ARGV_OPTARG_REQUIRED,0,0,0, + {"Wverbose", 'v',TAG_VERBOSE,ARGV_OPTARG_NONE, + ARGV_OPTION_HYBRID_ONLY,0,0, + "write pathnames to stderr in read, write, and copy modes; " + "produce verbose output in list mode."}, + + {"Wblksize", 'b',TAG_BLKSIZE,ARGV_OPTARG_REQUIRED, + ARGV_OPTION_HYBRID_ONLY|ARGV_OPTION_HYBRID_EQUAL,0,0, "(non-default) block-size; valid values are " "in the range of 512 to 32256; keeping " "the default format-specific block size " @@ -35,20 +54,74 @@ const tpax_hidden struct argv_option tpax_default_options[] = { " 10240 for the ustar format) " "is strongly recommended."}, - {"recurse", 0,TAG_RECURSE,ARGV_OPTARG_NONE,0,0,0, + {"Wrecurse", 0,TAG_RECURSE,ARGV_OPTARG_NONE, + ARGV_OPTION_HYBRID_ONLY,0,0, "recurse into directory archive members " "(this is the tpax_main() default)"}, - {"no-recurse", 'd',TAG_NORECURSE,ARGV_OPTARG_NONE,0,0,0, + {"Wno-recurse",'d',TAG_NORECURSE,ARGV_OPTARG_NONE, + ARGV_OPTION_HYBRID_ONLY,0,0, "do not recurse into directory archive members"}, - {"strict-path-input", - 0,TAG_STRICT_PATH,ARGV_OPTARG_NONE,0,0,0, + {"Wpreserve-atime", + 't',TAG_PRESERVE_ATIME,ARGV_OPTARG_NONE, + ARGV_OPTION_HYBRID_ONLY,0,0, + "when user has the necessary permissions, " + "set the access time of each file to the access " + "time that was reported by fstatat(3) prior to the " + "first read operation performed by pax"}, + + {"Wpax-symlink-args", + 'H',TAG_PAX_SYMLINK_ARGS,ARGV_OPTARG_NONE, + ARGV_OPTION_HYBRID_ONLY,0,0, + "when a command-line argument is a symbolic link, " + "add the underlying (referenced) file-system object " + "or directory to the archive using the name of the " + "symbolic link."}, + + {"Wpax-symlink-items", + 'L',TAG_PAX_SYMLINK_ITEMS,ARGV_OPTARG_NONE, + ARGV_OPTION_HYBRID_ONLY,0,0, + "when a command-line argument is a symbolic link, or when " + "an item added by way of recursion is a symbolic link, " + "add the underlying (referenced) file-system object " + "or directory to the archive using the name of the " + "symbolic link."}, + + {"Woptions", 'o',TAG_OPTIONS,ARGV_OPTARG_REQUIRED, + ARGV_OPTION_HYBRID_ONLY|ARGV_OPTION_HYBRID_SPACE|ARGV_OPTION_KEYVAL_ARRAY,0,0, + "a user-provided, format-specific keyval array of the form " + "keyword[[:]=value][,keyword[[:]=value], ...]"}, + + {"Wreplstr", 's',TAG_REPLSTR,ARGV_OPTARG_REQUIRED, + ARGV_OPTION_HYBRID_ONLY|ARGV_OPTION_HYBRID_SPACE,0,0, + "rename files and archive members as they are being added to " + "or extracted from the archive according to the specified " + "ed(1) style replacement string, which should be in the format " + "<sep><regex><sep><replstr><sep>[gp]; as an example, " + "-s ',^/git/tpax/,tpax-1.2.3/,' uses <comma> as the separator " + "character, and instructs pax to prefix all files rooted in " + "'/git/tpax/' with 'tpax-1.2.3/' while leaving the names of files which " + "do not match the regex expression unchanged. " + "When this option is repeated, pax shall attempt to match each file or " + "member name against all of the provided repalcement-string arguments " + "in the order of appearnce on the command line until the first " + "successful match."}, + + {"Wstrict-device-id", + 'X',TAG_STRICT_DEVICE_ID,ARGV_OPTARG_NONE, + ARGV_OPTION_HYBRID_ONLY,0,0, + "do not recurse into directories across device boundaries"}, + + {"Wstrict-path-input", + 0,TAG_STRICT_PATH,ARGV_OPTARG_NONE, + ARGV_OPTION_HYBRID_ONLY,0,0, "do not allow file arguments (in write and copy modes) " "to contain parent-directoy (dot dot) references"}, - {"pure-path-output", - 0,TAG_PURE_PATH,ARGV_OPTARG_NONE,0,0,0, + {"Wpure-path-output", + 0,TAG_PURE_PATH,ARGV_OPTARG_NONE, + ARGV_OPTION_HYBRID_ONLY,0,0, "output (in list mode) or store (in write mode) path " "names in pure form, specifically by liminating all " "this-dir (dot) elements from the listed/stored path " diff --git a/src/util/tpax_path_replstr.c b/src/util/tpax_path_replstr.c new file mode 100644 index 0000000..1935628 --- /dev/null +++ b/src/util/tpax_path_replstr.c @@ -0,0 +1,120 @@ +/**************************************************************/ +/* tpax: a topological pax implementation */ +/* Copyright (C) 2020--2024 SysDeer Technologies, LLC */ +/* Released under GPLv2 and GPLv3; see COPYING.TPAX. */ +/**************************************************************/ + +#include <regex.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +#include <tpax/tpax.h> +#include "tpax_driver_impl.h" + +static int tpax_backref_idx(const char c) +{ + return ((c >= '1') && (c <= '9')) ? c - '0' : 0; +} + +int tpax_util_path_replstr( + char * dstpath, + const char * srcpath, + const char * replstr, + const regex_t * regex, + size_t buflen, + int flags) +{ + int ret; + int idx; + regoff_t ro; + const char * ch; + char * dst; + size_t explen; + regmatch_t pmatch[11]; + + /* attempt to match */ + switch (regexec(regex,srcpath,11,pmatch,0)) { + case 0: + break; + + case REG_NOMATCH: + return 0; + + default: + return -1; + } + + /* copy bytes leading up to match */ + if (buflen <= (explen = pmatch[0].rm_so)) { + errno = ENOBUFS; + return -1; + } + + for (ro=0,dst=dstpath; ro<pmatch[0].rm_so; ro++) + *dst++ = srcpath[ro]; + + buflen -= explen; + + /* copy replacement string */ + for (ch=replstr,ret=0; buflen && *ch; ch++) { + /* <ampersand> stands for the entire matched string */ + if (ch[0] == '&') { + idx = 0; + + /* back-reference semantics: a matched subexpression or an empty string */ + } else if ((ch[0] == '\\') && (idx = tpax_backref_idx(ch[1]))) { + if (pmatch[idx].rm_so < 0) + idx = -1; + + ch++; + + /* all other escaped characters */ + } else if (ch[0] == '\\') { + *dst++ = *++ch; + idx = -1; + buflen--; + + /* all other characters */ + } else { + *dst++ = *ch; + idx = -1; + buflen--; + } + + /* copy matched string or matched subexpression, if any */ + if (idx >= 0) { + if (buflen <= (explen = (pmatch[idx].rm_eo - pmatch[idx].rm_so))) { + errno = ENOBUFS; + return -1; + } + + for (ro=pmatch[idx].rm_so; ro<pmatch[idx].rm_eo; ro++) + *dst++ = srcpath[ro]; + + buflen -= explen; + } + } + + /* replace further occurrences as needed */ + if ((flags & TPAX_REPL_GLOBAL) && srcpath[pmatch[0].rm_eo]) + ret = tpax_util_path_replstr( + dst,&srcpath[pmatch[0].rm_eo],replstr, + regex,buflen,flags); + + if (ret < 0) + return -1; + + /* copy remaining, non-matching bytes as needed */ + if (ret == 0) { + for (ch=&srcpath[pmatch[0].rm_eo]; *ch; ch++) + *dst++ = *ch; + + *dst = '\0'; + } + + /* all done */ + ret += (dst - dstpath); + + return ret; +} |