<commit_before>#include <apr_file_io.h>

#include "col-internal.h"
#include "net/network.h"
#include "router.h"
#include "types/catalog.h"

void
col_initialize(void)
{
    apr_status_t s = apr_initialize();
    if (s != APR_SUCCESS)
        FAIL_APR(s);
}

void
col_terminate(void)
{
    apr_terminate();
}

static Datum
get_local_addr(int port, apr_pool_t *pool)
{
    char buf[APRMAXHOSTLEN + 1];
    char addr[APRMAXHOSTLEN + 1 + 20];
    apr_status_t s;

    /* XXX: use temporary pool? No leak with current APR implementation. */
    s = apr_gethostname(buf, sizeof(buf), pool);
    if (s != APR_SUCCESS)
        FAIL_APR(s);

    snprintf(addr, sizeof(addr), "tcp:%s:%d", buf, port);
    printf("Local address = %s\n", addr);
    return datum_from_str(TYPE_STRING, addr);
}

ColInstance *
col_make(int port)
{
    apr_status_t s;
    apr_pool_t *pool;
    ColInstance *col;

    s = apr_pool_create(&pool, NULL);
    if (s != APR_SUCCESS)
        FAIL_APR(s);

    col = apr_pcalloc(pool, sizeof(*col));
    col->pool = pool;
    col->log = logger_make(col);
    col->cat = cat_make(col);
    col->router = router_make(col);
    col->net = network_make(col, port);
    col->port = network_get_port(col->net);
    col->local_addr = get_local_addr(col->port, col->pool);
    return col;
}

ColStatus
col_destroy(ColInstance *col)
{
    network_destroy(col->net);
    router_destroy(col->router);
    apr_pool_destroy(col->pool);

    return COL_OK;
}

/*
 * Read the file at the specified filesystem path into memory, parse it, and
 * then install the resulting program into the specified COL runtime. XXX:
 * We assume that the file is small enough that it can be slurped into a
 * single memory buffer without too much pain.
 */
ColStatus
col_install_file(ColInstance *col, const char *path)
{
    apr_status_t s;
    apr_file_t *file;
    apr_pool_t *file_pool;
    apr_finfo_t finfo;
    char *buf;
    apr_size_t file_size;
    ColStatus result;

    file_pool = make_subpool(col->pool);
    s = apr_file_open(&file, path, APR_READ | APR_BUFFERED,
                      APR_OS_DEFAULT, file_pool);
    if (s != APR_SUCCESS)
    {
        result = COL_ERROR;
        goto done;
    }

    /*
     * Get the file size, and allocate an appropriately-sized buffer to hold
     * the file contents. XXX: There is a trivial race condition here.
     */
    s = apr_file_info_get(&finfo, APR_FINFO_SIZE, file);
    if (s != APR_SUCCESS)
        FAIL_APR(s);

    file_size = (apr_size_t) finfo.size;
    buf = apr_palloc(file_pool, file_size + 1);

    s = apr_file_read(file, buf, &file_size);
    if (s != APR_SUCCESS)
        FAIL_APR(s);
    if (file_size != (apr_size_t) finfo.size)
        FAIL();

    buf[file_size] = '\0';
    result = col_install_str(col, buf);

done:
    apr_pool_destroy(file_pool);
    return result;
}

/*
 * Install the program contained in the specified program into the COL
 * runtime.
 *
 * XXX: Note that this is asynchronous; should we provide a convenient means
 * for the caller to wait until the program has been installed?
 */
ColStatus
col_install_str(ColInstance *col, const char *str)
{
    router_enqueue_program(col->router, str);
<commit_msg>Fix typo.<commit_after>#include <apr_file_io.h>

#include "col-internal.h"
#include "net/network.h"
#include "router.h"
#include "types/catalog.h"

void
col_initialize(void)
{
    apr_status_t s = apr_initialize();
    if (s != APR_SUCCESS)
        FAIL_APR(s);
}

void
col_terminate(void)
{
    apr_terminate();
}

static Datum
get_local_addr(int port, apr_pool_t *pool)
{
    char buf[APRMAXHOSTLEN + 1];
    char addr[APRMAXHOSTLEN + 1 + 20];
    apr_status_t s;

    /* XXX: use temporary pool? No leak with current APR implementation. */
    s = apr_gethostname(buf, sizeof(buf), pool);
    if (s != APR_SUCCESS)
        FAIL_APR(s);

    snprintf(addr, sizeof(addr), "tcp:%s:%d", buf, port);
    printf("Local address = %s\n", addr);
    return datum_from_str(TYPE_STRING, addr);
}

ColInstance *
col_make(int port)
{
    apr_status_t s;
    apr_pool_t *pool;
    ColInstance *col;

    s = apr_pool_create(&pool, NULL);
    if (s != APR_SUCCESS)
        FAIL_APR(s);

    col = apr_pcalloc(pool, sizeof(*col));
    col->pool = pool;
    col->log = logger_make(col);
    col->cat = cat_make(col);
    col->router = router_make(col);
    col->net = network_make(col, port);
    col->port = network_get_port(col->net);
    col->local_addr = get_local_addr(col->port, col->pool);
    return col;
}

ColStatus
col_destroy(ColInstance *col)
{
    network_destroy(col->net);
    router_destroy(col->router);
    apr_pool_destroy(col->pool);

    return COL_OK;
}

/*
 * Read the file at the specified filesystem path into memory, parse it, and
 * then install the resulting program into the specified COL runtime. XXX:
 * We assume that the file is small enough that it can be slurped into a
 * single memory buffer without too much pain.
 */
ColStatus
col_install_file(ColInstance *col, const char *path)
{
    apr_status_t s;
    apr_file_t *file;
    apr_pool_t *file_pool;
    apr_finfo_t finfo;
    char *buf;
    apr_size_t file_size;
    ColStatus result;

    file_pool = make_subpool(col->pool);
    s = apr_file_open(&file, path, APR_READ | APR_BUFFERED,
                      APR_OS_DEFAULT, file_pool);
    if (s != APR_SUCCESS)
    {
        result = COL_ERROR;
        goto done;
    }

    /*
     * Get the file size, and allocate an appropriately-sized buffer to hold
     * the file contents. XXX: There is a trivial race condition here.
     */
    s = apr_file_info_get(&finfo, APR_FINFO_SIZE, file);
    if (s != APR_SUCCESS)
        FAIL_APR(s);

    file_size = (apr_size_t) finfo.size;
    buf = apr_palloc(file_pool, file_size + 1);

    s = apr_file_read(file, buf, &file_size);
    if (s != APR_SUCCESS)
        FAIL_APR(s);
    if (file_size != (apr_size_t) finfo.size)
        FAIL();

    buf[file_size] = '\0';
    result = col_install_str(col, buf);

done:
    apr_pool_destroy(file_pool);
    return result;
}

/*
 * Install the program contained in the specified string into the COL
 * runtime.
 *
 * XXX: Note that this is asynchronous; should we provide a convenient means
 * for the caller to wait until the program has been installed?
 */
ColStatus
col_install_str(ColInstance *col, const char *str)
{
    router_enqueue_program(col->router, str);

{'fraction_non_alphanumeric': 0.11682609377450212, 'fraction_numerical': 0.0018817625842872823, 'mean_word_length': 3.20435069215557, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '1737606', 'n_tokens_mistral': 2283, 'n_tokens_neox': 2256, 'n_words': 593}