<commit_before>/* chunks_nbdkit.c
 * See https://github.com/pepaslabs/nbdkit-chunks-plugin
 * Copyright (C) 2015 <NAME>.
 * Released under the terms of the MIT license.  See https://opensource.org/licenses/MIT
 */

// see also https://github.com/libguestfs/nbdkit/blob/master/docs/nbdkit-plugin.pod

#include <config.h>
#include <nbdkit-plugin.h>

#include "chunks_metadata.h"

#include <string.h> // strcmp(), etc.



#define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS


char *chunks_dir_path = NULL;
metadata_t metadata;


struct _chunks_handle_t
{

};
typedef struct _chunks_handle_t chunks_handle_t;

chunks_handle_t handle;


#define chunks_config_help "dir: absolute path to the directory where the metadata and chunks are stored."


int chunks_config(const char *key, const char *value)
{
    if (strcmp(key, "dir") == 0)
    {
        chunks_dir_path = nbdkit_absolute_path(value);
        if (chunks_dir_path == NULL)
        {
            nbdkit_error("nbdkit_absolute_path() failed on dir '%s'", value);
            return -1;
        }

        return 0;
    }
    else
    {
        nbdkit_error("Unrecognized parameter: '%s'", key);
        return -1;
    }
}

int chunks_config_complete()
{
    if (chunks_dir_path == NULL)
    {
        nbdkit_error("'dir' is a required parameter");
        return -1;
    }

    if (read_metadata() != 0)
    {
        return -1;
    }

    return 0;
}

void* chunks_open(int readonly)
{
    return (void*)(&handle);
}

void chunks_close(void *handle)
{
    ;
}

int64_t chunks_get_size(void *handle)
{
    return (int64_t)(metadata.v1.dev_size);
}

static struct nbdkit_plugin plugin = {
  .name              = "chunks",
  .longname          = "nbdkit chunks plugin",
  .description       = "An nbdkit plugin which stores data in many small files, rather than one large backing file.",
  .version           = "0.0",

  .config            = chunks_config,
  .config_complete   = chunks_config_complete,
  .config_help       = chunks_config_help,

  .open              = chunks_open,
  .close             = chunks_close,

  .get_size          = chunks_get_size
};

NBDKIT_REGISTER_PLUGIN(plugin)
<commit_msg>implementing chunks_can_write<commit_after>/* chunks_nbdkit.c
 * See https://github.com/pepaslabs/nbdkit-chunks-plugin
 * Copyright (C) 2015 <NAME>.
 * Released under the terms of the MIT license.  See https://opensource.org/licenses/MIT
 */

// see also https://github.com/libguestfs/nbdkit/blob/master/docs/nbdkit-plugin.pod

#include <config.h>
#include <nbdkit-plugin.h>

#include "chunks_metadata.h"

#include <string.h> // strcmp(), etc.



#define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS


char *chunks_dir_path = NULL;
metadata_t metadata;


struct _chunks_handle_t
{

};
typedef struct _chunks_handle_t chunks_handle_t;

chunks_handle_t handle;


#define chunks_config_help "dir: absolute path to the directory where the metadata and chunks are stored."


int chunks_config(const char *key, const char *value)
{
    if (strcmp(key, "dir") == 0)
    {
        chunks_dir_path = nbdkit_absolute_path(value);
        if (chunks_dir_path == NULL)
        {
            nbdkit_error("nbdkit_absolute_path() failed on dir '%s'", value);
            return -1;
        }

        return 0;
    }
    else
    {
        nbdkit_error("Unrecognized parameter: '%s'", key);
        return -1;
    }
}

int chunks_config_complete()
{
    if (chunks_dir_path == NULL)
    {
        nbdkit_error("'dir' is a required parameter");
        return -1;
    }

    if (read_metadata() != 0)
    {
        return -1;
    }

    return 0;
}

void* chunks_open(int readonly)
{
    return (void*)(&handle);
}

void chunks_close(void *passed_handle)
{
    ;
}

int64_t chunks_get_size(void *passed_handle)
{
    return (int64_t)(metadata.v1.dev_size);
}

#include <stdio.h> // snprintf(), etc.
#include <stdlib.h> // mkdtemp(), etc.
#include <unistd.h> // unlink(), etc.

int can_make_dir()
{
    char *template = "/chunks/tmp.XXXXXX";
    size_t buff_size = strlen(chunks_dir_path) + strlen(template) + 1;
    char tmpd_path[buff_size];
    snprintf(tmpd_path, buff_size, "%s%s", chunks_dir_path, template);

    char *created_tmpd_path = mkdtemp(tmpd_path);
    if (created_tmpd_path == NULL)
    {
        nbdkit_error("Can't mkdtemp '%s'", tmpd_path);
        return -1;
    }

    if (unlink(created_tmpd_path) != 0)
    {
        nbdkit_error("Can't unlink '%s'", created_tmpd_path);
        return -1;
    }

    return 0;
}

int can_make_file()
{
    char *template = "/chunks/tmp.XXXXXX";
    size_t buff_size = strlen(chunks_dir_path) + strlen(template) + 1;
    char tmpf_path[buff_size];
    snprintf(tmpf_path, buff_size, "%s%s", chunks_dir_path, template);

    if (mkstemp(tmpf_path) == -1)
    {
        nbdkit_error("Can't mkstemp '%s'", tmpf_path);
        return -1;
    }

    if (unlink(tmpf_path) != 0)
    {
        nbdkit_error("Can't unlink '%s'", tmpf_path);
        return -1;
    }

    return 0;
}

int chunks_can_write(void *passed_handle)
{
    if (can_make_dir() != 0)
    {
        return -1;
    }

    if (can_make_file() != 0)
    {
        return -1;
    }

    return 0;
}

static struct nbdkit_plugin plugin = {
  .name              = "chunks",
  .longname          = "nbdkit chunks plugin",
  .description       = "An nbdkit plugin which stores data in many small files, rather than one large backing file.",
  .version           = "0.0",

  .config            = chunks_config,
  .config_complete   = chunks_config_complete,
  .config_help       = chunks_config_help,

  .open              = chunks_open,
  .close             = chunks_close,

  .get_size          = chunks_get_size,
  .can_write         = chunks_can_write
};

NBDKIT_REGISTER_PLUGIN(plugin)

{'fraction_non_alphanumeric': 0.1218370883882149, 'fraction_numerical': 0.009358752166377816, 'mean_word_length': 2.6828334396936824, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 6, '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': '713477', 'n_tokens_mistral': 2109, 'n_tokens_neox': 2045, 'n_words': 445}