diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/driver/tpax_driver_ctx.c | 160 | ||||
-rw-r--r-- | src/internal/tpax_driver_impl.h | 12 | ||||
-rw-r--r-- | src/logic/tpax_archive_enqueue.c | 29 | ||||
-rw-r--r-- | src/logic/tpax_archive_write.c | 162 | ||||
-rw-r--r-- | src/logic/tpax_queue_vector.c | 134 | ||||
-rw-r--r-- | src/meta/tpax_init_cpio_header.c | 145 | ||||
-rw-r--r-- | src/meta/tpax_init_ustar_header.c | 27 |
7 files changed, 612 insertions, 57 deletions
diff --git a/src/driver/tpax_driver_ctx.c b/src/driver/tpax_driver_ctx.c index 5593ec6..b7b949e 100644 --- a/src/driver/tpax_driver_ctx.c +++ b/src/driver/tpax_driver_ctx.c @@ -34,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, @@ -52,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; @@ -75,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, @@ -89,7 +110,7 @@ static int tpax_driver_usage( "Synopsis:\n" " %s [-d] [-f archive]\n" " %s -r [-d] [-f archive]\n" - " %s -w [−x format] [-b blocksize] [-dtv] [-H|-L] [-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); @@ -170,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, @@ -260,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( @@ -504,20 +516,20 @@ static struct tpax_driver_ctx_impl * tpax_driver_ctx_alloc( for (keyval=entry->keyv; keyval->keyword; keyval++) *pkeyval++ = keyval; - if (cctx->drvflags & TPAX_DRIVER_EXEC_MODE_WRITE_COPY) { - ictx->ctx.bufsize = TPAX_FILEIO_BUFLEN; - ictx->ctx.bufaddr = mmap( - 0,ictx->ctx.bufsize, - PROT_READ|PROT_WRITE, - MAP_PRIVATE|MAP_ANONYMOUS, - -1,0); + 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 (ictx->ctx.bufaddr == MAP_FAILED) { + free(ictx->ctx.keyvalv); + free(ictx); + return 0; + } + if (cctx->drvflags & TPAX_DRIVER_EXEC_MODE_WRITE_COPY) { ictx->ctx.dirbuff = mmap( 0,TPAX_DIRENT_BUFLEN, PROT_READ|PROT_WRITE, @@ -576,6 +588,46 @@ static int tpax_driver_keyval_error( 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, @@ -595,6 +647,7 @@ int tpax_lib_get_driver_ctx( size_t nunits; size_t nreplstr; size_t sreplstr; + size_t cachesize; const char * program; int fddst; const char * ch; @@ -818,6 +871,8 @@ int tpax_lib_get_driver_ctx( } else if (archive) { memcpy(&lfdctx,fdctx,sizeof(*fdctx)); + cctx.srcflags = TPAX_SOURCE_DATA_PENDING; + lfdctx.fdin = openat( fdctx->fdcwd, archive->arg, @@ -829,7 +884,15 @@ int tpax_lib_get_driver_ctx( 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) */ @@ -872,10 +935,6 @@ 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; } @@ -899,6 +958,42 @@ int tpax_lib_get_driver_ctx( 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; @@ -939,6 +1034,12 @@ static void tpax_free_driver_ctx_impl(struct tpax_driver_ctx_alloc * ictx) 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); @@ -954,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/internal/tpax_driver_impl.h b/src/internal/tpax_driver_impl.h index 3df8244..02e0d84 100644 --- a/src/internal/tpax_driver_impl.h +++ b/src/internal/tpax_driver_impl.h @@ -64,7 +64,12 @@ 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; @@ -94,6 +99,7 @@ struct tpax_driver_ctx_impl { 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; @@ -105,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; }; diff --git a/src/logic/tpax_archive_enqueue.c b/src/logic/tpax_archive_enqueue.c index 8685cad..7d8c278 100644 --- a/src/logic/tpax_archive_enqueue.c +++ b/src/logic/tpax_archive_enqueue.c @@ -192,7 +192,9 @@ static int tpax_archive_add_queue_item( 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, @@ -233,10 +235,21 @@ static int tpax_archive_add_queue_item( 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; @@ -365,7 +378,9 @@ static int tpax_archive_enqueue_dir_entries( if (tpax_archive_add_queue_item( dctx,dirent,dent,0, - st.st_dev,depth, + st.st_rdev, + st.st_dev,st.st_ino, + depth, TPAX_ITEM_IMPLICIT, fd,&fkeep) < 0) return tpax_archive_enqueue_ret( @@ -404,7 +419,9 @@ static int tpax_archive_enqueue_dir_entries( if (tpax_archive_add_queue_item( dctx,lnkent,cdent,0, - lnkst.st_dev,depth+1, + 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( @@ -515,7 +532,9 @@ int tpax_archive_enqueue( /* add to queue */ if (tpax_archive_add_queue_item( dctx,dirent,0,prefix, - uctx->st->st_dev,0, + uctx->st->st_rdev, + uctx->st->st_dev, + uctx->st->st_ino,0, TPAX_ITEM_EXPLICIT, fdat,&fkeep) < 0) return tpax_archive_enqueue_ret( @@ -548,7 +567,9 @@ int tpax_archive_enqueue( if (tpax_archive_add_queue_item( dctx,lnkent,cdent,0, - lnkst.st_dev,1, + lnkst.st_rdev, + lnkst.st_dev,lnkst.st_ino, + 1, TPAX_ITEM_EXPLICIT|TPAX_ITEM_SYMLINK, fdat,&fkeep) < 0) return tpax_archive_enqueue_ret( diff --git a/src/logic/tpax_archive_write.c b/src/logic/tpax_archive_write.c index 96af625..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; @@ -110,7 +123,9 @@ static int tpax_archive_write_impl( 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; @@ -143,7 +158,7 @@ static int tpax_archive_write_impl( TPAX_ERR_FLOW_ERROR); /* regex matching and patter substitution */ - if ((slen = tpax_apply_string_replacement(dctx,path,replbuf,buflen)) < 0) + if ((slen = tpax_apply_string_replacement(dctx,path,replbuf,PATH_MAX)) < 0) return TPAX_CUSTOM_ERROR( dctx, TPAX_ERR_FLOW_ERROR); @@ -205,14 +220,26 @@ static int tpax_archive_write_impl( 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,apath,st, - slnk,&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), dctx,uctx); @@ -254,8 +281,28 @@ static int tpax_archive_write_impl( } } - /* 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); @@ -264,6 +311,16 @@ static int tpax_archive_write_impl( 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? */ @@ -346,8 +403,79 @@ int tpax_archive_write(const struct tpax_driver_ctx * dctx) static int tpax_archive_seal_cpio(const struct tpax_driver_ctx * dctx) { - (void)dctx; - return -1; + 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) @@ -359,6 +487,10 @@ int tpax_archive_seal(const struct tpax_driver_ctx * dctx) ssize_t blksize; char buf[1024]; + /* cpio trailer? */ + if (dctx->cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_CPIO) + return tpax_archive_seal_cpio(dctx); + /* archive block size, current position */ blksize = dctx->cctx->blksize; cpos = tpax_get_driver_cpos(dctx); @@ -367,10 +499,6 @@ int tpax_archive_seal(const struct tpax_driver_ctx * dctx) if (cpos % 512) return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FLOW_ERROR); - /* cpio trailer? */ - if (dctx->cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_CPIO) - return tpax_archive_seal_cpio(dctx); - /* ustar, pax */ fdout = tpax_driver_fdout(dctx); memset(buf,0,sizeof(buf)); diff --git a/src/logic/tpax_queue_vector.c b/src/logic/tpax_queue_vector.c index cae4da0..a74cf1f 100644 --- a/src/logic/tpax_queue_vector.c +++ b/src/logic/tpax_queue_vector.c @@ -53,6 +53,122 @@ tpax_hidden const char * tpax_queue_item_full_path( 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; @@ -67,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; @@ -97,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); +} |