Skip to content

Commit

Permalink
Worked on tests and code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimmetz committed Nov 19, 2019
1 parent b7e994f commit c3a3cef
Show file tree
Hide file tree
Showing 20 changed files with 2,673 additions and 127 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -143,6 +143,8 @@ stamp-h[1-9]
/tests/fsapfs_test_container_reaper
/tests/fsapfs_test_container_superblock
/tests/fsapfs_test_data_block
/tests/fsapfs_test_data_block_data_handle
/tests/fsapfs_test_data_block_vector
/tests/fsapfs_test_data_stream
/tests/fsapfs_test_deflate
/tests/fsapfs_test_directory_record
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Expand Up @@ -2,7 +2,7 @@ AC_PREREQ( 2.59 )

AC_INIT(
[libfsapfs],
[20191118],
[20191119],
[joachim.metz@gmail.com])

AC_CONFIG_SRCDIR(
Expand Down
1 change: 1 addition & 0 deletions libfsapfs/Makefile.am
Expand Up @@ -58,6 +58,7 @@ libfsapfs_la_SOURCES = \
libfsapfs_container_superblock.c libfsapfs_container_superblock.h \
libfsapfs_data_block.c libfsapfs_data_block.h \
libfsapfs_data_block_data_handle.c libfsapfs_data_block_data_handle.h \
libfsapfs_data_block_vector.c libfsapfs_data_block_vector.h \
libfsapfs_data_stream.c libfsapfs_data_stream.h \
libfsapfs_debug.c libfsapfs_debug.h \
libfsapfs_definitions.h \
Expand Down
4 changes: 2 additions & 2 deletions libfsapfs/libfsapfs_buffer_data_handle.c
Expand Up @@ -27,7 +27,7 @@
#include "libfsapfs_libcerror.h"
#include "libfsapfs_unused.h"

/* Creates buffer data handle
/* Creates a buffer data handle
* Make sure the value data_handle is referencing, is set to NULL
* Returns 1 if successful or -1 on error
*/
Expand Down Expand Up @@ -128,7 +128,7 @@ int libfsapfs_buffer_data_handle_initialize(
return( -1 );
}

/* Frees data handle
/* Frees a buffer data handle
* Returns 1 if successful or -1 on error
*/
int libfsapfs_buffer_data_handle_free(
Expand Down
184 changes: 65 additions & 119 deletions libfsapfs/libfsapfs_data_block_data_handle.c
Expand Up @@ -25,16 +25,17 @@

#include "libfsapfs_data_block.h"
#include "libfsapfs_data_block_data_handle.h"
#include "libfsapfs_data_block_vector.h"
#include "libfsapfs_definitions.h"
#include "libfsapfs_file_extent.h"
#include "libfsapfs_file_system_data_handle.h"
#include "libfsapfs_libbfio.h"
#include "libfsapfs_libcdata.h"
#include "libfsapfs_libcerror.h"
#include "libfsapfs_libfcache.h"
#include "libfsapfs_libfdata.h"
#include "libfsapfs_unused.h"

/* Creates data handle
/* Creates a data block data handle
* Make sure the value data_handle is referencing, is set to NULL
* Returns 1 if successful or -1 on error
*/
Expand All @@ -46,13 +47,7 @@ int libfsapfs_data_block_data_handle_initialize(
uint8_t is_sparse,
libcerror_error_t **error )
{
libfsapfs_file_extent_t *file_extent = NULL;
static char *function = "libfsapfs_data_block_data_handle_initialize";
uint64_t logical_offset = 0;
uint32_t segment_flags = 0;
int extent_index = 0;
int number_of_extents = 0;
int segment_index = 0;
static char *function = "libfsapfs_data_block_data_handle_initialize";

if( data_handle == NULL )
{
Expand Down Expand Up @@ -136,15 +131,12 @@ int libfsapfs_data_block_data_handle_initialize(

goto on_error;
}
if( libfdata_vector_initialize(
if( libfsapfs_data_block_vector_initialize(
&( ( *data_handle )->data_block_vector ),
(size64_t) io_handle->block_size,
(intptr_t *) ( *data_handle )->file_system_data_handle,
(int (*)(intptr_t **, libcerror_error_t **)) &libfsapfs_file_system_data_handle_free,
NULL,
(int (*)(intptr_t *, intptr_t *, libfdata_vector_t *, libfdata_cache_t *, int, int, off64_t, size64_t, uint32_t, uint8_t, libcerror_error_t **)) &libfsapfs_file_system_data_handle_read_data_block,
NULL,
LIBFDATA_DATA_HANDLE_FLAG_NON_MANAGED,
io_handle,
( *data_handle )->file_system_data_handle,
file_extents,
is_sparse,
error ) != 1 )
{
libcerror_error_set(
Expand All @@ -156,96 +148,6 @@ int libfsapfs_data_block_data_handle_initialize(

goto on_error;
}
if( libcdata_array_get_number_of_entries(
file_extents,
&number_of_extents,
error ) != 1 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_GET_FAILED,
"%s: unable to retrieve number of entries from array.",
function );

goto on_error;
}
for( extent_index = 0;
extent_index < number_of_extents;
extent_index++ )
{
if( libcdata_array_get_entry_by_index(
file_extents,
extent_index,
(intptr_t **) &file_extent,
error ) != 1 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_GET_FAILED,
"%s: unable to retrieve file extent: %d.",
function,
extent_index );

goto on_error;
}
if( file_extent == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: missing file extent: %d.",
function,
extent_index );

goto on_error;
}
if( ( is_sparse == 0 )
|| ( file_extent->physical_block_number != 0 ) )
{
if( file_extent->logical_offset != logical_offset )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
"%s: invalid file extent: %d - logical offset value out of bounds.",
function,
extent_index );

goto on_error;
}
}
segment_flags = 0;

if( ( is_sparse != 0 )
&& ( file_extent->physical_block_number == 0 ) )
{
segment_flags = LIBFDATA_RANGE_FLAG_IS_SPARSE;
}
if( libfdata_vector_append_segment(
( *data_handle )->data_block_vector,
&segment_index,
extent_index,
file_extent->physical_block_number * io_handle->block_size,
file_extent->data_size,
segment_flags,
error ) != 1 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
"%s: unable to append extent: %d as data block vector segment.",
function,
extent_index );

goto on_error;
}
logical_offset += file_extent->data_size;
}
if( libfcache_cache_initialize(
&( ( *data_handle )->data_block_cache ),
LIBFSAPFS_MAXIMUM_CACHE_ENTRIES_DATA_BLOCKS,
Expand Down Expand Up @@ -293,7 +195,7 @@ int libfsapfs_data_block_data_handle_initialize(
return( -1 );
}

/* Frees data handle
/* Frees a data block data handle
* Returns 1 if successful or -1 on error
*/
int libfsapfs_data_block_data_handle_free(
Expand Down Expand Up @@ -370,7 +272,7 @@ int libfsapfs_data_block_data_handle_free(
ssize_t libfsapfs_data_block_data_handle_read_segment_data(
libfsapfs_data_block_data_handle_t *data_handle,
libbfio_handle_t *file_io_handle,
int segment_index LIBFSAPFS_ATTRIBUTE_UNUSED,
int segment_index,
int segment_file_index LIBFSAPFS_ATTRIBUTE_UNUSED,
uint8_t *segment_data,
size_t segment_data_size,
Expand All @@ -380,12 +282,12 @@ ssize_t libfsapfs_data_block_data_handle_read_segment_data(
{
libfsapfs_data_block_t *data_block = NULL;
static char *function = "libfsapfs_data_block_data_handle_read_segment_data";
size64_t data_size = 0;
size_t data_block_offset = 0;
size_t read_size = 0;
size_t segment_data_offset = 0;
uint64_t block_number = 0;

LIBFSAPFS_UNREFERENCED_PARAMETER( segment_index )
LIBFSAPFS_UNREFERENCED_PARAMETER( segment_file_index )
LIBFSAPFS_UNREFERENCED_PARAMETER( segment_flags )
LIBFSAPFS_UNREFERENCED_PARAMETER( read_flags )
Expand All @@ -412,13 +314,24 @@ ssize_t libfsapfs_data_block_data_handle_read_segment_data(

return( -1 );
}
if( data_handle->current_segment_offset < 0 )
if( data_handle->current_offset < 0 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
"%s: invalid data handle - current offset value out of bounds.",
function );

return( -1 );
}
if( segment_index < 0 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
"%s: invalid data handle - current segment offset value out of bounds.",
"%s: invalid segment index value out of bounds.",
function );

return( -1 );
Expand All @@ -445,8 +358,26 @@ ssize_t libfsapfs_data_block_data_handle_read_segment_data(

return( -1 );
}
block_number = data_handle->current_segment_offset / data_handle->io_handle->block_size;
data_block_offset = data_handle->current_segment_offset % data_handle->io_handle->block_size;
if( libfdata_vector_get_size(
data_handle->data_block_vector,
&data_size,
error ) != 1 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_GET_FAILED,
"%s: unable to retrieve size of data block vector.",
function );

return( -1 );
}
if( (size64_t) data_handle->current_offset >= data_size )
{
return( 0 );
}
block_number = data_handle->current_offset / data_handle->io_handle->block_size;
data_block_offset = data_handle->current_offset % data_handle->io_handle->block_size;

while( segment_data_size > 0 )
{
Expand Down Expand Up @@ -530,9 +461,14 @@ ssize_t libfsapfs_data_block_data_handle_read_segment_data(
segment_data_size -= read_size;

block_number++;
}
data_handle->current_segment_offset += segment_data_offset;

data_handle->current_offset += read_size;

if( (size64_t) data_handle->current_offset >= data_size )
{
break;
}
}
return( (ssize_t) segment_data_offset );
}

Expand All @@ -543,15 +479,14 @@ ssize_t libfsapfs_data_block_data_handle_read_segment_data(
off64_t libfsapfs_data_block_data_handle_seek_segment_offset(
libfsapfs_data_block_data_handle_t *data_handle,
intptr_t *file_io_handle LIBFSAPFS_ATTRIBUTE_UNUSED,
int segment_index LIBFSAPFS_ATTRIBUTE_UNUSED,
int segment_index,
int segment_file_index LIBFSAPFS_ATTRIBUTE_UNUSED,
off64_t segment_offset,
libcerror_error_t **error )
{
static char *function = "libfsapfs_data_block_data_handle_seek_segment_offset";

LIBFSAPFS_UNREFERENCED_PARAMETER( file_io_handle )
LIBFSAPFS_UNREFERENCED_PARAMETER( segment_index )
LIBFSAPFS_UNREFERENCED_PARAMETER( segment_file_index )

if( data_handle == NULL )
Expand All @@ -565,6 +500,17 @@ off64_t libfsapfs_data_block_data_handle_seek_segment_offset(

return( -1 );
}
if( segment_index < 0 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
"%s: invalid segment index value out of bounds.",
function );

return( -1 );
}
if( segment_offset < 0 )
{
libcerror_error_set(
Expand All @@ -576,7 +522,7 @@ off64_t libfsapfs_data_block_data_handle_seek_segment_offset(

return( -1 );
}
data_handle->current_segment_offset = segment_offset;
data_handle->current_offset = segment_offset;

return( segment_offset );
}
Expand Down
4 changes: 2 additions & 2 deletions libfsapfs/libfsapfs_data_block_data_handle.h
Expand Up @@ -42,9 +42,9 @@ typedef struct libfsapfs_data_block_data_handle libfsapfs_data_block_data_handle

struct libfsapfs_data_block_data_handle
{
/* The current segment offset
/* The current offset
*/
off64_t current_segment_offset;
off64_t current_offset;

/* The IO handle
*/
Expand Down

0 comments on commit c3a3cef

Please sign in to comment.