Skip to content

C development

Joachim Metz edited this page Jul 15, 2022 · 1 revision

TODO: work in progress

Return values

Most of the API functions return 1 if successful or -1 on error.

The close function is an exception since it returns 0 if successful or -1 on error.

More details about the return values for each API function can be found in libvshadow.h

Examples

The following examples require the following headers to be included:

#include <stdlib.h>
#include <stdio.h>

#include <libvshadow.h>

volume structure

Allocate volume structure

libvshadow_error_t *error = NULL;
libvshadow_volume_t *volume = NULL;

if( libvshadow_volume_initialize(&volume, &error) != 1 )
{
    fprintf(stderr, "Unable to initialize volume.\n");

    libvshadow_error_free(&error);

    exit(EXIT_FAILURE);
}

When calling the libvshadow_volume_initialize function the volume argument must refer to NULL to allocate and initialize a volume structure. The error argument is optional and can be NULL.

The function will return 1 if successful or -1 on error. On error an the library creates an error structure except if error is NULL e.g.

libvshadow_volume_initialize(&volume, NULL);

The error structure must be freed by calling the libvshadow_error_free function.

Free volume structure

if( libvshadow_volume_free(&volume, &error) != 1 )
{
    fprintf(stderr, "Unable to free volume.\n");

    libvshadow_error_free(&error);

    exit(EXIT_FAILURE);
}

The function will return 1 if successful or -1 on error. File is set to NULL. The function will also close the volume if it was opened.

Open volume

filename = "image.raw";

if( libvshadow_volume_open(volume, filename, LIBVSHADOW_OPEN_READ, &error) != 1 )
{
    fprintf(stderr, "Unable to open volume.\n" );

    libvshadow_volume_free(&volume, NULL);
    libvshadow_error_free(&error);

    exit(EXIT_FAILURE);
}

libvshadow provides both narrow and wide character string functions for filenames. The wide character equivalent of the open function is libvshadow_volume_open_wide. By default libvshadow will only enable wide character string support on Windows since other operating systems have build-in support for UTF-8 narrow character strings.

To compile with wide character support add --enable-wide-character-type=yes to configure, e.g.:

./configure --enable-wide-character-type=yes

Or on Windows define WINAPI and either _UNICODE or UNICODE

When wide character string support is enabled LIBVSHADOW_HAVE_WIDE_CHARACTER_TYPE is defined in <libvshadow/features.h>

Open volume using a file-like object

TODO describe

libvshadow allows to be compiled with file-like object support using libbfio. The libvshadow configure script will automatically detect if a compatible version of libbfio is available.

When libbfio is support is enabled LIBVSHADOW_HAVE_BFIO is defined in <libvshadow/features.h>

Close volume

if( libvshadow_volume_close(volume, &error) != 0 )
{
    fprintf(stderr, "Unable to close volume.\n" );

    libvshadow_volume_free(&volume, NULL);
    libvshadow_error_free(&error);

    exit(EXIT_FAILURE);
}

Also see

  • libvshadow.h
  • man 3 libvshadow