2019-10-04 18:44:25 +02:00
|
|
|
/* Read an image and check that file handles are being closed on minimise.
|
|
|
|
*
|
|
|
|
* This will only work on linux: we signal success and do nothing if /dev/proc
|
|
|
|
* does not exist.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <vips/vips.h>
|
|
|
|
|
|
|
|
/* Count the number of files in a directory, -1 for directory not found etc.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
count_files( const char *dirname )
|
|
|
|
{
|
|
|
|
GDir *dir;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if( !(dir = g_dir_open( dirname, 0, NULL )) )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
for( n = 0; g_dir_read_name( dir ); n++ )
|
|
|
|
;
|
|
|
|
|
|
|
|
g_dir_close( dir );
|
|
|
|
|
|
|
|
return( n );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main( int argc, char **argv )
|
|
|
|
{
|
2019-11-10 12:30:21 +01:00
|
|
|
VipsStreami *streami;
|
2019-10-04 18:44:25 +02:00
|
|
|
VipsImage *image, *x;
|
|
|
|
char fd_dir[256];
|
|
|
|
int n_files;
|
|
|
|
double average;
|
|
|
|
|
|
|
|
if( VIPS_INIT( argv[0] ) )
|
|
|
|
vips_error_exit( "unable to start" );
|
|
|
|
|
2019-10-06 11:55:19 +02:00
|
|
|
if( argc != 2 )
|
|
|
|
vips_error_exit( "usage: %s test-image", argv[0] );
|
|
|
|
|
2019-10-04 18:44:25 +02:00
|
|
|
vips_snprintf( fd_dir, 256, "/proc/%d/fd", getpid() );
|
|
|
|
n_files = count_files( fd_dir );
|
|
|
|
if( n_files == -1 )
|
|
|
|
/* Probably not linux, silent success.
|
|
|
|
*/
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
/* This is usually 4. stdout / stdin / stderr plus one more made for
|
|
|
|
* us by glib, I think, doing what I don't know.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Opening an image should read the header, then close the fd.
|
|
|
|
*/
|
2019-10-27 19:19:43 +01:00
|
|
|
printf( "** seq open ..\n" );
|
2019-11-10 12:30:21 +01:00
|
|
|
if( !(streami = vips_streami_new_from_filename( argv[1] )) )
|
|
|
|
vips_error_exit( NULL );
|
|
|
|
if( !(image = vips_image_new_from_stream( streami, "",
|
2019-10-04 18:44:25 +02:00
|
|
|
"access", VIPS_ACCESS_SEQUENTIAL,
|
|
|
|
NULL )) )
|
|
|
|
vips_error_exit( NULL );
|
|
|
|
if( count_files( fd_dir ) != n_files )
|
2019-10-06 11:55:19 +02:00
|
|
|
vips_error_exit( "%s: fd not closed after header read",
|
|
|
|
argv[1] );
|
2019-10-04 18:44:25 +02:00
|
|
|
|
|
|
|
/* We should be able to read a chunk near the top, then have the fd
|
|
|
|
* closed again.
|
|
|
|
*/
|
2019-10-27 19:19:43 +01:00
|
|
|
printf( "** crop1, avg ..\n" );
|
2019-10-04 18:44:25 +02:00
|
|
|
if( vips_crop( image, &x, 0, 0, image->Xsize, 10, NULL ) ||
|
|
|
|
vips_avg( x, &average, NULL ) )
|
|
|
|
vips_error_exit( NULL );
|
|
|
|
g_object_unref( x );
|
|
|
|
if( count_files( fd_dir ) != n_files )
|
2019-10-06 11:55:19 +02:00
|
|
|
vips_error_exit( "%s: fd not closed after first read",
|
|
|
|
argv[1] );
|
2019-10-04 18:44:25 +02:00
|
|
|
|
|
|
|
/* We should be able to read again, a little further down, and have
|
|
|
|
* the input restarted and closed again.
|
|
|
|
*/
|
2019-10-27 19:19:43 +01:00
|
|
|
printf( "** crop2, avg ..\n" );
|
2019-10-04 18:44:25 +02:00
|
|
|
if( vips_crop( image, &x, 0, 20, image->Xsize, 10, NULL ) ||
|
|
|
|
vips_avg( x, &average, NULL ) )
|
|
|
|
vips_error_exit( NULL );
|
|
|
|
g_object_unref( x );
|
|
|
|
if( count_files( fd_dir ) != n_files )
|
2019-10-06 11:55:19 +02:00
|
|
|
vips_error_exit( "%s: fd not closed after second read",
|
|
|
|
argv[1] );
|
2019-10-04 18:44:25 +02:00
|
|
|
|
|
|
|
/* Clean up, and we should still just have three open.
|
|
|
|
*/
|
2019-10-27 19:19:43 +01:00
|
|
|
printf( "** unref ..\n" );
|
2019-10-06 08:25:09 +02:00
|
|
|
g_object_unref( image );
|
2019-11-10 12:30:21 +01:00
|
|
|
g_object_unref( streami );
|
2019-10-27 19:19:43 +01:00
|
|
|
printf( "** shutdown ..\n" );
|
2019-10-04 18:44:25 +02:00
|
|
|
vips_shutdown();
|
|
|
|
|
|
|
|
if( count_files( fd_dir ) != n_files )
|
2019-10-06 11:55:19 +02:00
|
|
|
vips_error_exit( "%s: fd not closed after shutdown",
|
|
|
|
argv[1] );
|
2019-10-04 18:44:25 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|