1
0
Fork 0

Merging upstream version 1.1~rc2.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-02-24 05:43:06 +01:00
parent 24e2ea3a41
commit e13f4df619
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
26 changed files with 1151 additions and 583 deletions

123
zutils.cc
View file

@ -22,11 +22,13 @@
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <stdint.h>
#include <unistd.h>
#include <sys/wait.h>
#include "zutils.h"
#include "rc.h"
const char * invocation_name = 0;
@ -35,7 +37,7 @@ const char * util_name = program_name;
int verbosity = 0;
int get_format_type( const std::string & arg )
int parse_format_type( const std::string & arg )
{
for( int i = 0; i < num_formats; ++i )
if( arg == format_names[i] )
@ -102,56 +104,90 @@ bool feed_data( const int infd, const int outfd,
}
bool set_data_feeder( int * const infdp, pid_t * const pidp,
const int format_type )
bool good_status( const Children & children, const bool finished )
{
bool error = false;
for( int i = 0; i < 2; ++i )
{
const pid_t pid = children.pid[i];
if( pid )
{
const char * const msg =
( i & 1 ) ? children.compressor_name : "data feeder";
if( !finished )
{
const int tmp = child_status( pid, msg );
if( tmp < 0 ) kill( pid, SIGTERM ); // child not terminated
else if( tmp != 0 ) error = true; // child status != 0
}
else
if( wait_for_child( pid, msg ) != 0 ) error = true;
}
}
return !error;
}
bool set_data_feeder( int * const infdp, Children & children, int format_index )
{
const uint8_t * magic_data = 0;
int magic_size = 0;
const char * const decompressor_name = ( format_type >= 0 ) ?
decompressor_names[format_type] :
test_format( *infdp, &magic_data, &magic_size );
if( format_index < 0 )
format_index = test_format( *infdp, &magic_data, &magic_size );
children.compressor_name = get_compressor_name( format_index );
if( decompressor_name ) // compressed
if( children.compressor_name ) // compressed
{
int fda[2]; // pipe from feeder
int fda2[2]; // pipe from decompressor
int fda2[2]; // pipe from compressor
if( pipe( fda ) < 0 || pipe( fda2 ) < 0 )
{ show_error( "Can't create pipe", errno ); return false; }
const int old_infd = *infdp;
*infdp = fda2[0];
const pid_t pid = fork();
if( pid == 0 ) // child (decompressor feeder)
if( pid == 0 ) // child 1 (compressor feeder)
{
const pid_t pid2 = fork();
if( pid2 == 0 ) // grandchild (decompressor)
{
if( dup2( fda[0], STDIN_FILENO ) >= 0 &&
dup2( fda2[1], STDOUT_FILENO ) >= 0 &&
close( fda[0] ) == 0 && close( fda[1] ) == 0 &&
close( fda2[0] ) == 0 && close( fda2[1] ) == 0 )
execlp( decompressor_name, decompressor_name,
(verbosity >= 0) ? "-d" : "-dq", (char *)0 );
show_exec_error( decompressor_name );
_exit( 2 );
}
if( pid2 < 0 )
{ show_fork_error( decompressor_name ); _exit( 2 ); }
if( close( fda[0] ) != 0 ||
close( fda2[0] ) != 0 || close( fda2[1] ) != 0 ||
!feed_data( old_infd, fda[1], magic_data, magic_size ) )
_exit( 2 );
if( close( fda[1] ) != 0 )
{ show_close_error( "decompressor feeder" ); _exit( 2 ); }
_exit( wait_for_child( pid2, decompressor_name ) );
{ show_close_error( "data feeder" ); _exit( 2 ); }
_exit( 0 );
}
// parent
if( pid < 0 ) // parent
{ show_fork_error( "data feeder" ); return false; }
const pid_t pid2 = fork();
if( pid2 == 0 ) // child 2 (compressor)
{
if( dup2( fda[0], STDIN_FILENO ) >= 0 &&
dup2( fda2[1], STDOUT_FILENO ) >= 0 &&
close( fda[0] ) == 0 && close( fda[1] ) == 0 &&
close( fda2[0] ) == 0 && close( fda2[1] ) == 0 )
{
const std::vector< std::string > & compressor_args =
get_compressor_args( format_index );
const int size = compressor_args.size();
const char ** const argv = new const char *[size+3];
argv[0] = children.compressor_name;
for( int i = 0; i < size; ++i )
argv[i+1] = compressor_args[i].c_str();
argv[size+1] = ( verbosity >= 0 ) ? "-d" : "-dq";
argv[size+2] = 0;
execvp( argv[0], (char **)argv );
}
show_exec_error( children.compressor_name );
_exit( 2 );
}
if( pid2 < 0 ) // parent
{ show_fork_error( children.compressor_name ); return false; }
close( fda[0] ); close( fda[1] ); close( fda2[1] );
if( pid < 0 )
{ show_fork_error( "decompressor feeder" ); return false; }
*pidp = pid;
children.pid[0] = pid;
children.pid[1] = pid2;
}
else // not compressed
else // uncompressed
{
int fda[2]; // pipe from feeder
if( pipe( fda ) < 0 )
@ -168,11 +204,11 @@ bool set_data_feeder( int * const infdp, pid_t * const pidp,
{ show_close_error( "data feeder" ); _exit( 2 ); }
_exit( 0 );
}
// parent
close( fda[1] );
if( pid < 0 )
if( pid < 0 ) // parent
{ show_fork_error( "data feeder" ); return false; }
*pidp = pid;
close( fda[1] );
children.pid[0] = pid;
children.pid[1] = 0;
}
return true;
}
@ -187,7 +223,7 @@ void show_help_addr()
void show_version( const char * const Util_name )
{
if( !Util_name || !*Util_name )
if( !Util_name || !Util_name[0] )
std::printf( "%s %s\n", Program_name, PROGVERSION );
else
std::printf( "%s (%s) %s\n", Util_name, program_name, PROGVERSION );
@ -256,9 +292,8 @@ void internal_error( const char * const msg )
}
const char * test_format( const int infd,
const uint8_t ** const magic_datap,
int * const magic_sizep )
int test_format( const int infd, const uint8_t ** const magic_datap,
int * const magic_sizep )
{
enum { buf_size = 5 };
static uint8_t buf[buf_size];
@ -271,13 +306,13 @@ const char * test_format( const int infd,
if( readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == bzip2_magic[1] &&
readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == bzip2_magic[2] )
{ *magic_datap = bzip2_magic; *magic_sizep = bzip2_magic_size;
return decompressor_names[fmt_bz2]; }
return fmt_bz2; }
}
else if( buf[0] == gzip_magic[0] )
{
if( readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == gzip_magic[1] )
{ *magic_datap = gzip_magic; *magic_sizep = gzip_magic_size;
return decompressor_names[fmt_gz]; }
return fmt_gz; }
}
else if( buf[0] == lzip_magic[0] )
{
@ -285,7 +320,7 @@ const char * test_format( const int infd,
readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == lzip_magic[2] &&
readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == lzip_magic[3] )
{ *magic_datap = lzip_magic; *magic_sizep = lzip_magic_size;
return decompressor_names[fmt_lz]; }
return fmt_lz; }
}
else if( buf[0] == xz_magic[0] )
{
@ -294,11 +329,11 @@ const char * test_format( const int infd,
readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == xz_magic[3] &&
readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == xz_magic[4] )
{ *magic_datap = xz_magic; *magic_sizep = xz_magic_size;
return decompressor_names[fmt_xz]; }
return fmt_xz; }
}
}
*magic_datap = buf; *magic_sizep = i;
return 0;
return -1;
}