<commit_before>#include <dlfcn.h>
#include "blockdev.h"
#include "plugins.h"

#include "plugin_apis/lvm.h"
#include "plugin_apis/lvm.c"
#include "plugin_apis/btrfs.h"
#include "plugin_apis/btrfs.c"
#include "plugin_apis/swap.h"
#include "plugin_apis/swap.c"
#include "plugin_apis/loop.h"
#include "plugin_apis/loop.c"
#include "plugin_apis/crypto.h"
#include "plugin_apis/crypto.c"
#include "plugin_apis/mpath.c"
#include "plugin_apis/mpath.h"
#include "plugin_apis/dm.c"
#include "plugin_apis/dm.h"

/**
 * SECTION: libblockdev
 * @short_description: a library for doing low-level operations with block devices
 * @title: libblockdev library
 * @include: blockdev.h
 *
 */

typedef struct BDPluginStatus {
    BDPluginSpec spec;
    gpointer handle;
} BDPluginStatus;

/* KEEP THE ORDERING OF THESE ARRAYS MATCHING THE BDPluginName ENUM! */
static BDPluginStatus plugins[BD_PLUGIN_UNDEF] = {
    {{BD_PLUGIN_LVM, "libbd_lvm.so"}, NULL},
    {{BD_PLUGIN_BTRFS, "libbd_btrfs.so"}, NULL},
    {{BD_PLUGIN_SWAP, "libbd_swap.so"}, NULL},
    {{BD_PLUGIN_LOOP, "libbd_loop.so"}, NULL},
    {{BD_PLUGIN_CRYPTO, "libbd_crypto.so"}, NULL},
    {{BD_PLUGIN_MPATH, "libbd_mpath.so"}, NULL},
    {{BD_PLUGIN_DM, "libbd_dm.so"}, NULL}
};
static gchar* plugin_names[BD_PLUGIN_UNDEF] = {
    "lvm", "btrfs", "swap", "loop", "crypto", "mpath", "dm"
};

static void set_plugin_so_name (BDPlugin name, gchar *so_name) {
    plugins[name].spec.so_name = so_name;
}

/**
 * bd_init:
 * @force_plugins: (allow-none): null-terminated list of plugins that should be loaded (even if
 *                 other plugins for the same technologies are found)
 *
 * Returns: whether the library was successfully initialized or not
 */
gboolean bd_init (BDPluginSpec *force_plugins) {
    guint8 i = 0;
    gboolean all_loaded = TRUE;

    if (force_plugins)
        for (i=0; force_plugins + i; i++)
            set_plugin_so_name(force_plugins[i].name, force_plugins[i].so_name);

    plugins[BD_PLUGIN_LVM].handle = load_lvm_from_plugin(plugins[BD_PLUGIN_LVM].spec.so_name);
    plugins[BD_PLUGIN_BTRFS].handle = load_btrfs_from_plugin(plugins[BD_PLUGIN_BTRFS].spec.so_name);
    plugins[BD_PLUGIN_SWAP].handle = load_swap_from_plugin(plugins[BD_PLUGIN_SWAP].spec.so_name);
    plugins[BD_PLUGIN_LOOP].handle = load_loop_from_plugin(plugins[BD_PLUGIN_LOOP].spec.so_name);
    plugins[BD_PLUGIN_CRYPTO].handle = load_crypto_from_plugin(plugins[BD_PLUGIN_CRYPTO].spec.so_name);
    plugins[BD_PLUGIN_MPATH].handle = load_mpath_from_plugin(plugins[BD_PLUGIN_MPATH].spec.so_name);
    plugins[BD_PLUGIN_DM].handle = load_dm_from_plugin(plugins[BD_PLUGIN_DM].spec.so_name);

    for (i=0; (i < BD_PLUGIN_UNDEF) && all_loaded; i++)
        all_loaded = all_loaded && plugins[i].handle;

    return all_loaded;<commit_msg>Add function for library/plugins reinitialization<commit_after>#include <dlfcn.h>
#include "blockdev.h"
#include "plugins.h"

#include "plugin_apis/lvm.h"
#include "plugin_apis/lvm.c"
#include "plugin_apis/btrfs.h"
#include "plugin_apis/btrfs.c"
#include "plugin_apis/swap.h"
#include "plugin_apis/swap.c"
#include "plugin_apis/loop.h"
#include "plugin_apis/loop.c"
#include "plugin_apis/crypto.h"
#include "plugin_apis/crypto.c"
#include "plugin_apis/mpath.c"
#include "plugin_apis/mpath.h"
#include "plugin_apis/dm.c"
#include "plugin_apis/dm.h"

/**
 * SECTION: libblockdev
 * @short_description: a library for doing low-level operations with block devices
 * @title: libblockdev library
 * @include: blockdev.h
 *
 */

typedef struct BDPluginStatus {
    BDPluginSpec spec;
    gpointer handle;
} BDPluginStatus;

/* KEEP THE ORDERING OF THESE ARRAYS MATCHING THE BDPluginName ENUM! */
static BDPluginStatus plugins[BD_PLUGIN_UNDEF] = {
    {{BD_PLUGIN_LVM, "libbd_lvm.so"}, NULL},
    {{BD_PLUGIN_BTRFS, "libbd_btrfs.so"}, NULL},
    {{BD_PLUGIN_SWAP, "libbd_swap.so"}, NULL},
    {{BD_PLUGIN_LOOP, "libbd_loop.so"}, NULL},
    {{BD_PLUGIN_CRYPTO, "libbd_crypto.so"}, NULL},
    {{BD_PLUGIN_MPATH, "libbd_mpath.so"}, NULL},
    {{BD_PLUGIN_DM, "libbd_dm.so"}, NULL}
};
static gchar* plugin_names[BD_PLUGIN_UNDEF] = {
    "lvm", "btrfs", "swap", "loop", "crypto", "mpath", "dm"
};

static void set_plugin_so_name (BDPlugin name, gchar *so_name) {
    plugins[name].spec.so_name = so_name;
}

static gboolean load_plugins (BDPluginSpec *force_plugins, gboolean reload) {
    guint8 i = 0;
    gboolean all_loaded = TRUE;

    if (reload)
        for (i=0; i < BD_PLUGIN_UNDEF; i++) {
            if (plugins[i].handle && (dlclose (plugins[i].handle) != 0))
                g_warning ("Failed to close %s plugin", plugin_names[i]);
            plugins[i].handle = NULL;
        }

    if (force_plugins)
        for (i=0; force_plugins + i; i++)
            set_plugin_so_name(force_plugins[i].name, force_plugins[i].so_name);

    if (!plugins[BD_PLUGIN_LVM].handle)
        plugins[BD_PLUGIN_LVM].handle = load_lvm_from_plugin(plugins[BD_PLUGIN_LVM].spec.so_name);
    if (!plugins[BD_PLUGIN_BTRFS].handle)
        plugins[BD_PLUGIN_BTRFS].handle = load_btrfs_from_plugin(plugins[BD_PLUGIN_BTRFS].spec.so_name);
    if (!plugins[BD_PLUGIN_SWAP].handle)
        plugins[BD_PLUGIN_SWAP].handle = load_swap_from_plugin(plugins[BD_PLUGIN_SWAP].spec.so_name);
    if (!plugins[BD_PLUGIN_LOOP].handle)
        plugins[BD_PLUGIN_LOOP].handle = load_loop_from_plugin(plugins[BD_PLUGIN_LOOP].spec.so_name);
    if (!plugins[BD_PLUGIN_CRYPTO].handle)
        plugins[BD_PLUGIN_CRYPTO].handle = load_crypto_from_plugin(plugins[BD_PLUGIN_CRYPTO].spec.so_name);
    if (!plugins[BD_PLUGIN_MPATH].handle)
        plugins[BD_PLUGIN_MPATH].handle = load_mpath_from_plugin(plugins[BD_PLUGIN_MPATH].spec.so_name);
    if (!plugins[BD_PLUGIN_DM].handle)
        plugins[BD_PLUGIN_DM].handle = load_dm_from_plugin(plugins[BD_PLUGIN_DM].spec.so_name);

    for (i=0; (i < BD_PLUGIN_UNDEF) && all_loaded; i++)
        all_loaded = all_loaded && plugins[i].handle;

    return all_loaded;
}

/**
 * bd_init:
 * @force_plugins: (allow-none): null-terminated list of plugins that should be loaded (even if
 *                 other plugins for the same technologies are found)
 *
 * Returns: whether the library was successfully initialized or not
 */
gboolean bd_init (BDPluginSpec *force_plugins) {
    return load_plugins (force_plugins, FALSE);
}

/**
 * bd_reinit:
 * @force_plugins: (allow-none): null-terminated list of plugins that should be loaded (even if
 *                 other plugins for the same technologies are found)
 * @reload: whether to reload the already loaded plugins or not
 *
 * Returns: whether the library was successfully initialized or not
 *
 * If @reload is %TRUE all the plugins are closed and reloaded otherwise only
 * the missing plugins are loaded.
 */
gboolean bd_reinit (BDPluginSpec *force_plugins, gboolean reload) {
    return load_plugins (force_plugins, reload);
{'fraction_non_alphanumeric': 0.12849243306169966, 'fraction_numerical': 0.0014551804423748546, 'mean_word_length': 4.601466992665037, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3469159', 'n_tokens_mistral': 2565, 'n_tokens_neox': 2528, 'n_words': 489}