1
0
mirror of https://git.code.sf.net/p/fuse-emulator/fuse-utils synced 2025-08-06 02:35:52 +03:00

Remove use of mmap(): added complexity for no real benefit.

Legacy-ID: 3079
This commit is contained in:
Philip Kendall
2007-07-27 13:56:22 +00:00
parent f63582fbf7
commit 3ea6d7502c
12 changed files with 69 additions and 96 deletions

View File

@@ -186,3 +186,6 @@
address (Stuart). address (Stuart).
20070611 man/fuse-utils.1: mention tape2wav (Stuart). 20070611 man/fuse-utils.1: mention tape2wav (Stuart).
20070611 .: ignore tape2wav. 20070611 .: ignore tape2wav.
20070727 listbasic.c,rzxcheck.c,rzxdump.c,rzxtool.c,snap2tzx.c,snapconv.c,
tape2wav.c,tapeconv.c,tzxlist.c,utils.[ch]: remove use of mmap():
added complexity for no real benefit.

View File

@@ -75,44 +75,39 @@ int main(int argc, char* argv[])
error = init_libspectrum(); if( error ) return error; error = init_libspectrum(); if( error ) return error;
error = mmap_file( argv[1], &buffer, &length ); if( error ) return error; error = read_file( argv[1], &buffer, &length ); if( error ) return error;
error = libspectrum_identify_file_with_class( &type, &class, argv[1], buffer, error = libspectrum_identify_file_with_class( &type, &class, argv[1], buffer,
length ); length );
if( error ) { munmap( buffer, length ); return error; } if( error ) { free( buffer ); return error; }
switch( class ) { switch( class ) {
case LIBSPECTRUM_CLASS_SNAPSHOT: case LIBSPECTRUM_CLASS_SNAPSHOT:
error = parse_snapshot_file( buffer, length, type ); error = parse_snapshot_file( buffer, length, type );
if( error ) { munmap( buffer, length ); return error; } if( error ) { free( buffer ); return error; }
break; break;
case LIBSPECTRUM_CLASS_TAPE: case LIBSPECTRUM_CLASS_TAPE:
error = parse_tape_file( buffer, length, type ); error = parse_tape_file( buffer, length, type );
if( error ) { munmap( buffer, length ); return error; } if( error ) { free( buffer ); return error; }
break; break;
case LIBSPECTRUM_CLASS_UNKNOWN: case LIBSPECTRUM_CLASS_UNKNOWN:
fprintf( stderr, "%s: couldn't identify the file type of `%s'\n", fprintf( stderr, "%s: couldn't identify the file type of `%s'\n",
progname, argv[1] ); progname, argv[1] );
munmap( buffer, length ); free( buffer );
return 1; return 1;
default: default:
fprintf( stderr, "%s: `%s' is an unsupported file type\n", fprintf( stderr, "%s: `%s' is an unsupported file type\n",
progname, argv[1] ); progname, argv[1] );
munmap( buffer, length ); free( buffer );
return 1; return 1;
} }
error = munmap( buffer, length ); free( buffer );
if( error ) {
fprintf( stderr, "%s: couldn't munmap `%s': %s\n", progname, argv[1],
strerror( errno ) );
return 1;
}
return 0; return 0;
} }

View File

@@ -66,10 +66,10 @@ main( int argc, char **argv )
if( libspectrum_rzx_alloc( &rzx ) ) return 16; if( libspectrum_rzx_alloc( &rzx ) ) return 16;
if( mmap_file( rzxfile, &buffer, &length ) ) return 16; if( read_file( rzxfile, &buffer, &length ) ) return 16;
if( libspectrum_rzx_read( rzx, buffer, length ) ) { if( libspectrum_rzx_read( rzx, buffer, length ) ) {
munmap( buffer, length ); free( buffer );
return 16; return 16;
} }
@@ -77,7 +77,7 @@ main( int argc, char **argv )
if( !keyid ) { if( !keyid ) {
printf( "%s: no key ID found in '%s'\n", progname, rzxfile ); printf( "%s: no key ID found in '%s'\n", progname, rzxfile );
libspectrum_rzx_free( rzx ); libspectrum_rzx_free( rzx );
munmap( buffer, length ); free( buffer );
return 16; return 16;
} }
@@ -88,14 +88,14 @@ main( int argc, char **argv )
printf( "%s: don't know anything about key ID %08x\n", progname, printf( "%s: don't know anything about key ID %08x\n", progname,
keyid ); keyid );
libspectrum_rzx_free( rzx ); libspectrum_rzx_free( rzx );
munmap( buffer, length ); free( buffer );
return 16; return 16;
} }
error = libspectrum_rzx_get_signature( rzx, &signature ); error = libspectrum_rzx_get_signature( rzx, &signature );
if( error ) { if( error ) {
libspectrum_rzx_free( rzx ); libspectrum_rzx_free( rzx );
munmap( buffer, length ); free( buffer );
return 16; return 16;
} }
@@ -103,17 +103,11 @@ main( int argc, char **argv )
if( error && error != LIBSPECTRUM_ERROR_SIGNATURE ) { if( error && error != LIBSPECTRUM_ERROR_SIGNATURE ) {
libspectrum_signature_free( &signature ); libspectrum_signature_free( &signature );
libspectrum_rzx_free( rzx ); libspectrum_rzx_free( rzx );
munmap( buffer, length ); free( buffer );
return 16; return 16;
} }
if( munmap( buffer, length ) == -1 ) { free( buffer );
fprintf( stderr, "%s: couldn't munmap `%s': %s\n", progname, rzxfile,
strerror( errno ) );
libspectrum_signature_free( &signature );
libspectrum_rzx_free( rzx );
return 16;
}
libspectrum_rzx_free( rzx ); free( rzx ); libspectrum_rzx_free( rzx ); free( rzx );
libspectrum_signature_free( &signature ); libspectrum_signature_free( &signature );

View File

@@ -108,7 +108,7 @@ do_file( const char *filename )
printf( "Examining file %s\n", filename ); printf( "Examining file %s\n", filename );
error = mmap_file( filename, &buffer, &length ); if( error ) return error; error = read_file( filename, &buffer, &length ); if( error ) return error;
ptr = buffer; end = buffer + length; ptr = buffer; end = buffer + length;
@@ -118,14 +118,14 @@ do_file( const char *filename )
fprintf( stderr, fprintf( stderr,
"%s: Not enough bytes for RZX header (%ld bytes)\n", "%s: Not enough bytes for RZX header (%ld bytes)\n",
progname, (unsigned long)strlen( rzx_signature ) + 6 ); progname, (unsigned long)strlen( rzx_signature ) + 6 );
munmap( buffer, length ); free( buffer );
return 1; return 1;
} }
if( memcmp( ptr, rzx_signature, strlen( rzx_signature ) ) ) { if( memcmp( ptr, rzx_signature, strlen( rzx_signature ) ) ) {
fprintf( stderr, "%s: Wrong signature: expected `%s'\n", progname, fprintf( stderr, "%s: Wrong signature: expected `%s'\n", progname,
rzx_signature ); rzx_signature );
munmap( buffer, length ); free( buffer );
return 1; return 1;
} }
@@ -152,19 +152,15 @@ do_file( const char *filename )
default: default:
fprintf( stderr, "%s: Unknown block type 0x%02x\n", progname, id ); fprintf( stderr, "%s: Unknown block type 0x%02x\n", progname, id );
munmap( buffer, length ); free( buffer );
return 1; return 1;
} }
if( error ) { munmap( buffer, length ); return 1; } if( error ) { free( buffer ); return 1; }
} }
if( munmap( buffer, length ) == -1 ) { free( buffer );
fprintf( stderr, "%s: couldn't munmap `%s': %s\n", progname, filename,
strerror( errno ) );
return 1;
}
return 0; return 0;
} }

View File

@@ -89,18 +89,14 @@ main( int argc, char **argv )
if( libspectrum_rzx_alloc( &rzx ) ) return 1; if( libspectrum_rzx_alloc( &rzx ) ) return 1;
if( mmap_file( options.rzxfile, &buffer, &length ) ) return 1; if( read_file( options.rzxfile, &buffer, &length ) ) return 1;
if( libspectrum_rzx_read( rzx, buffer, length ) ) { if( libspectrum_rzx_read( rzx, buffer, length ) ) {
munmap( buffer, length ); free( buffer );
return 1; return 1;
} }
if( munmap( buffer, length ) == -1 ) { free( buffer );
fprintf( stderr, "%s: couldn't munmap `%s': %s\n", progname,
options.rzxfile, strerror( errno ) );
return 1;
}
if( options.extract ) { if( options.extract ) {

View File

@@ -483,7 +483,7 @@ load_snap( libspectrum_snap **snap, const char *filename )
error = libspectrum_snap_alloc( snap ); if( error ) return error; error = libspectrum_snap_alloc( snap ); if( error ) return error;
if( mmap_file( filename, &buffer, &length ) ) { if( read_file( filename, &buffer, &length ) ) {
libspectrum_snap_free( *snap ); libspectrum_snap_free( *snap );
return 1; return 1;
} }
@@ -491,16 +491,11 @@ load_snap( libspectrum_snap **snap, const char *filename )
error = libspectrum_snap_read( *snap, buffer, length, LIBSPECTRUM_ID_UNKNOWN, error = libspectrum_snap_read( *snap, buffer, length, LIBSPECTRUM_ID_UNKNOWN,
filename ); filename );
if( error ) { if( error ) {
libspectrum_snap_free( *snap ); munmap( buffer, length ); libspectrum_snap_free( *snap ); free( buffer );
return error; return error;
} }
if( munmap( buffer, length ) == -1 ) { free( buffer );
fprintf( stderr, "%s: couldn't munmap '%s': %s\n", progname, filename,
strerror( errno ) );
libspectrum_snap_free( *snap );
return 1;
}
return 0; return 0;
} }

View File

@@ -80,7 +80,7 @@ main( int argc, char **argv )
error = libspectrum_snap_alloc( &snap ); if( error ) return error; error = libspectrum_snap_alloc( &snap ); if( error ) return error;
if( mmap_file( argv[0], &buffer, &length ) ) { if( read_file( argv[0], &buffer, &length ) ) {
libspectrum_snap_free( snap ); libspectrum_snap_free( snap );
return 1; return 1;
} }
@@ -88,16 +88,11 @@ main( int argc, char **argv )
error = libspectrum_snap_read( snap, buffer, length, LIBSPECTRUM_ID_UNKNOWN, error = libspectrum_snap_read( snap, buffer, length, LIBSPECTRUM_ID_UNKNOWN,
argv[0] ); argv[0] );
if( error ) { if( error ) {
libspectrum_snap_free( snap ); munmap( buffer, length ); libspectrum_snap_free( snap ); free( buffer );
return error; return error;
} }
if( munmap( buffer, length ) == -1 ) { free( buffer );
fprintf( stderr, "%s: couldn't munmap '%s': %s\n", progname, argv[1],
strerror( errno ) );
libspectrum_snap_free( snap );
return 1;
}
error = libspectrum_identify_file_with_class( &type, &class, argv[1], NULL, error = libspectrum_identify_file_with_class( &type, &class, argv[1], NULL,
0 ); 0 );

View File

@@ -91,25 +91,20 @@ read_tape( char *filename, libspectrum_tape **tape )
{ {
libspectrum_byte *buffer; size_t length; libspectrum_byte *buffer; size_t length;
if( mmap_file( filename, &buffer, &length ) ) return 1; if( read_file( filename, &buffer, &length ) ) return 1;
if( libspectrum_tape_alloc( tape ) ) { if( libspectrum_tape_alloc( tape ) ) {
munmap( buffer, length ); free( buffer );
return 1; return 1;
} }
if( libspectrum_tape_read( *tape, buffer, length, LIBSPECTRUM_ID_UNKNOWN, if( libspectrum_tape_read( *tape, buffer, length, LIBSPECTRUM_ID_UNKNOWN,
filename ) ) { filename ) ) {
munmap( buffer, length ); free( buffer );
return 1; return 1;
} }
if( munmap( buffer, length ) == -1 ) { free( buffer );
fprintf( stderr, "%s: couldn't munmap `%s': %s\n", progname, filename,
strerror( errno ) );
libspectrum_tape_free( *tape );
return 1;
}
return 0; return 0;
} }

View File

@@ -94,25 +94,20 @@ read_tape( char *filename, libspectrum_tape **tape )
{ {
libspectrum_byte *buffer; size_t length; libspectrum_byte *buffer; size_t length;
if( mmap_file( filename, &buffer, &length ) ) return 1; if( read_file( filename, &buffer, &length ) ) return 1;
if( libspectrum_tape_alloc( tape ) ) { if( libspectrum_tape_alloc( tape ) ) {
munmap( buffer, length ); free( buffer );
return 1; return 1;
} }
if( libspectrum_tape_read( *tape, buffer, length, LIBSPECTRUM_ID_UNKNOWN, if( libspectrum_tape_read( *tape, buffer, length, LIBSPECTRUM_ID_UNKNOWN,
filename ) ) { filename ) ) {
munmap( buffer, length ); free( buffer );
return 1; return 1;
} }
if( munmap( buffer, length ) == -1 ) { free( buffer );
fprintf( stderr, "%s: couldn't munmap `%s': %s\n", progname, filename,
strerror( errno ) );
libspectrum_tape_free( *tape );
return 1;
}
return 0; return 0;
} }

View File

@@ -130,26 +130,22 @@ process_tape( char *filename )
size_t i; size_t i;
error = mmap_file( filename, &buffer, &length ); if( error ) return error; error = read_file( filename, &buffer, &length ); if( error ) return error;
error = libspectrum_tape_alloc( &tape ); error = libspectrum_tape_alloc( &tape );
if( error != LIBSPECTRUM_ERROR_NONE ) { if( error != LIBSPECTRUM_ERROR_NONE ) {
munmap( buffer, length ); free( buffer );
return 1; return 1;
} }
error = libspectrum_tape_read( tape, buffer, length, LIBSPECTRUM_ID_UNKNOWN, error = libspectrum_tape_read( tape, buffer, length, LIBSPECTRUM_ID_UNKNOWN,
filename ); filename );
if( error != LIBSPECTRUM_ERROR_NONE ) { if( error != LIBSPECTRUM_ERROR_NONE ) {
munmap( buffer, length ); free( buffer );
return error; return error;
} }
if( munmap( buffer, length ) == -1 ) { free( buffer );
fprintf( stderr, "%s: couldn't munmap `%s': %s\n", progname, filename,
strerror( errno ) );
return 1;
}
printf("Listing of `%s':\n\n", filename ); printf("Listing of `%s':\n\n", filename );
@@ -322,10 +318,7 @@ process_tape( char *filename )
} }
error = libspectrum_tape_free( tape ); error = libspectrum_tape_free( tape );
if( error != LIBSPECTRUM_ERROR_NONE ) { if( error != LIBSPECTRUM_ERROR_NONE ) return error;
munmap( buffer, length );
return error;
}
return 0; return 0;
} }

30
utils.c
View File

@@ -117,9 +117,10 @@ get_creator( libspectrum_creator **creator, const char *program )
} }
int int
mmap_file( const char *filename, unsigned char **buffer, size_t *length ) read_file( const char *filename, unsigned char **buffer, size_t *length )
{ {
int fd; struct stat file_info; int fd; struct stat file_info;
ssize_t bytes;
if( ( fd = open( filename, O_RDONLY | O_BINARY ) ) == -1 ) { if( ( fd = open( filename, O_RDONLY | O_BINARY ) ) == -1 ) {
fprintf( stderr, "%s: couldn't open `%s': %s\n", progname, filename, fprintf( stderr, "%s: couldn't open `%s': %s\n", progname, filename,
@@ -134,20 +135,35 @@ mmap_file( const char *filename, unsigned char **buffer, size_t *length )
return 1; return 1;
} }
(*length) = file_info.st_size; *length = file_info.st_size;
(*buffer) = mmap( 0, *length, PROT_READ, MAP_SHARED, fd, 0 ); *buffer = malloc( *length );
if( (*buffer) == (void*)-1 ) { if( !*buffer ) {
fprintf( stderr, "%s: couldn't mmap `%s': %s\n", progname, filename, fprintf( stderr, "%s: out of memory allocating %lu bytes\n", progname,
strerror( errno ) ); (unsigned long)*length );
close(fd); close(fd);
return 1; return 1;
} }
bytes = read( fd, *buffer, *length );
if( bytes == -1 ) {
fprintf( stderr, "%s: error reading from `%s': %s\n", progname, filename,
strerror( errno ) );
free( *buffer );
close(fd);
return 1;
} else if( bytes < *length ) {
fprintf( stderr, "%s: could read only %lu out of %lu bytes from `%s'\n",
progname, (unsigned long)bytes, (unsigned long)*length, filename );
free( *buffer );
close(fd);
return 1;
}
if( close(fd) ) { if( close(fd) ) {
fprintf( stderr, "%s: couldn't close `%s': %s\n", progname, filename, fprintf( stderr, "%s: couldn't close `%s': %s\n", progname, filename,
strerror( errno ) ); strerror( errno ) );
munmap( *buffer, *length ); free( *buffer );
return 1; return 1;
} }

View File

@@ -30,7 +30,7 @@
int init_libspectrum( void ); int init_libspectrum( void );
int get_creator( libspectrum_creator **creator, const char *program ); int get_creator( libspectrum_creator **creator, const char *program );
int mmap_file( const char *filename, unsigned char **buffer, size_t *length ); int read_file( const char *filename, unsigned char **buffer, size_t *length );
struct rzx_key { struct rzx_key {
libspectrum_dword id; libspectrum_dword id;