Commit c92949c3 authored by Simon McVittie's avatar Simon McVittie

capture-libs: Allow patterns to be read from a file

The pressure-vessel tool used in the Steam Runtime has an increasingly
long list of graphics libraries which might be better as a file
than on the command-line.
Signed-off-by: Simon McVittie's avatarSimon McVittie <smcv@collabora.com>
parent e8079fed
Pipeline #10868 passed with stage
in 7 minutes and 41 seconds
......@@ -49,6 +49,19 @@ my $container = "${test_tempdir}/container";
mkdir($container);
my $libdir = "${test_tempdir}/libdir";
{
open(my $fh, '>', "${test_tempdir}/empty");
close $fh;
}
{
open(my $fh, '>', "${test_tempdir}/libc");
print $fh "# comment\n";
print $fh "\n";
print $fh "soname:libc.so.6\n";
print $fh "soname:libc.so.6"; # deliberately no trailing newline
close $fh;
}
run_ok(['rm', '-fr', $libdir]);
mkdir($libdir);
run_ok([$CAPSULE_CAPTURE_LIBS_TOOL, 'soname:libc.so.6'], '>&2',
......@@ -97,7 +110,9 @@ run_ok([qw(bwrap --ro-bind / / --ro-bind /), $host,
qw(--dev-bind /dev /dev),
$CAPSULE_CAPTURE_LIBS_TOOL,
"--dest=$libdir",
"--provider=$host", 'soname:libc.so.6'], '>&2');
"--provider=$host",
"from:$test_tempdir/empty",
"from:$test_tempdir/libc"], '>&2');
{
opendir(my $dir_iter, $libdir);
my @links = sort grep { $_ ne '.' and $_ ne '..' } readdir $dir_iter;
......
......@@ -246,6 +246,8 @@ static void usage (int code)
fprintf( fh, "\n" );
fprintf( fh, "Each PATTERN is one of:\n" );
fprintf( fh, "\n" );
fprintf( fh, "from:FILE\n"
"\tRead PATTERNs from FILE, one per line.\n" );
fprintf( fh, "soname:SONAME\n"
"\tCapture the library in ld.so.cache whose name is\n"
"\texactly SONAME\n" );
......@@ -325,6 +327,9 @@ fail:
return false;
}
static bool capture_pattern( const char *pattern, capture_flags flags,
int *code, char **message );
static bool capture_patterns( const char * const *patterns,
capture_flags flags,
int *code, char **message );
......@@ -823,6 +828,54 @@ capture_path_match( const char *pattern, capture_flags flags,
return ret;
}
static bool
capture_lines( const char *filename, capture_flags flags,
int *code, char **message )
{
FILE *fp = NULL;
bool ret = true;
char *line = NULL;
size_t len = 0;
ssize_t chars;
if( strcmp( filename, "-" ) != 0 )
{
fp = fopen( filename, "re" );
if( fp == NULL )
{
_capsule_set_error( code, message, errno,
"Unable to open \"%s\": %s",
filename, strerror( errno ) );
return false;
}
}
while( ( chars = getline( &line, &len, fp ? fp : stdin ) ) > -1 )
{
/* Ignore blank lines and shell-style comments (which must
* currently be at the beginning of the line) */
if( chars == 0 || line[0] == '\0' || line[0] == '\n' || line[0] == '#' )
continue;
if( line[chars - 1] == '\n' )
line[chars - 1] = '\0';
if( !capture_pattern( line, flags, code, message ) )
{
ret = false;
break;
}
}
free( line );
if( fp != NULL )
fclose( fp );
return ret;
}
static bool
capture_pattern( const char *pattern, capture_flags flags,
int *code, char **message )
......@@ -1013,6 +1066,12 @@ capture_pattern( const char *pattern, capture_flags flags,
code, message );
}
if( strstarts( pattern, "from:" ) )
{
return capture_lines( pattern + strlen( "from:" ),
flags, code, message );
}
if( strchr( pattern, ':' ) != NULL )
{
_capsule_set_error( code, message, EINVAL,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment