<commit_before>/*
 * $Id$
 *
 * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

/*
 * ksm_list.c - List various aspects of the current configuration
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "ksm/database.h"
#include "ksm/database_statement.h"
#include "ksm/datetime.h"
#include "ksm/db_fields.h"
#include "ksm/debug.h"
#include "ksm/ksmdef.h"
#include "ksm/ksm.h"
#include "ksm/ksm_internal.h"
#include "ksm/message.h"
#include "ksm/string_util.h"
#include "ksm/string_util2.h"

/*+
 * KsmListBackups - Output a list of all backups perfomed
 *
 *
 * Arguments:
 *
 *      int repo_id
 *          ID of the repository (-1 for all)
 *
 * Returns:
 *      int
 *          Status return.  0 on success.
 *                          other on fail
 */

int KsmListBackups(int repo_id)
{
    char*       sql = NULL;     /* SQL query */
    char*       sql2 = NULL;     /* SQL query */
    int         status = 0;     /* Status return */
    char        stringval[KSM_INT_STR_SIZE];  /* For Integer to String conversion */
    DB_RESULT	result;         /* Result of the query */
    DB_ROW      row = NULL;     /* Row data */
    DB_RESULT	result2;         /* Result of the query */
    DB_ROW      row2 = NULL;     /* Row data */

    char*       temp_date = NULL; /* place to store date returned */
    char*       temp_repo = NULL; /* place to store repository returned */
    int         temp_backup_req = 0; /* place to store backuprequired returned */

    /* Select rows */
    StrAppend(&sql, "select distinct k.backup, s.name from keypairs k, securitymodules s ");
    StrAppend(&sql, "where s.id = k.securitymodule_id ");
    if (repo_id != -1) {
        StrAppend(&sql, "and s.id = ");
        snprintf(stringval, KSM_INT_STR_SIZE, "%d", repo_id);
        StrAppend(&sql, stringval);
    }
    StrAppend(&sql, " order by backup");

    DusEnd(&sql);

    status = DbExecuteSql(DbHandle(), sql, &result);

    if (status == 0) {
        status = DbFetchRow(result, &row);
        printf("Date:                    Repository:\n");
        while (status == 0) {
            /* Got a row, print it */
            DbString(row, 0, &temp_date);
            DbString(row, 1, &temp_repo);

            if (temp_date != NULL) { /* Ignore non-backup */
                printf("%-24s %s\n", temp_date, temp_repo);
            }
            
            status = DbFetchRow(result, &row);
        }

        /* Convert EOF status to success */

        if (status == -1) {
            status = 0;
        }

        DbFreeResult(result);
    }

    DusFree(sql);
    DbFreeRow(row);
    DbStringFree(temp_date);

    /* List repos which need a backup */
    StrAppend(&sql2, "select s.name, s.requirebackup from keypairs k, securitymodules s ");
    StrAppend(&sql2, "where s.id = k.securitymodule_id ");
    if (repo_id != -1) {
        StrAppend(&sql2, "and s.id = ");
        snprintf(stringval, KSM_INT_STR_SIZE, "%d", repo_id);
        StrAppend(&sql2, stringval);
    }
    StrAppend(&sql2, " and k.backup is null");
    StrAppend(&sql2, " group by s.name order by s.name");

    DusEnd(&sql2);

    status = DbExecuteSql(DbHandle(), sql2, &result2);

    if (status == 0) {
        status = DbFetchRow(result2, &row2);
        while (status == 0) {
            /* Got a row, print it */
            DbString(row, 0, &temp_repo);
            DbInt(row, 1, &temp_backup_req);

            if (temp_backup_req == 0) {
                printf("Repository %s has unbacked up keys (that can be used)\n", temp_repo);
            } else {
                printf("Repository %s has unbacked up keys (that will not be used)\n", temp_repo);
            }
            
            status = DbFetchRow(result2, &row2);
        }

        /* Convert EOF status to success */

        if (status == -1) {
            status = 0;
        }

        DbFreeResult(result2);
    }

    DusFree(sql2);
    DbFreeRow(row2);
    DbStringFree(temp_repo);

    return status;<commit_msg>not sure how this got through.<commit_after>/*
 * $Id$
 *
 * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

/*
 * ksm_list.c - List various aspects of the current configuration
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "ksm/database.h"
#include "ksm/database_statement.h"
#include "ksm/datetime.h"
#include "ksm/db_fields.h"
#include "ksm/debug.h"
#include "ksm/ksmdef.h"
#include "ksm/ksm.h"
#include "ksm/ksm_internal.h"
#include "ksm/message.h"
#include "ksm/string_util.h"
#include "ksm/string_util2.h"

/*+
 * KsmListBackups - Output a list of all backups perfomed
 *
 *
 * Arguments:
 *
 *      int repo_id
 *          ID of the repository (-1 for all)
 *
 * Returns:
 *      int
 *          Status return.  0 on success.
 *                          other on fail
 */

int KsmListBackups(int repo_id)
{
    char*       sql = NULL;     /* SQL query */
    char*       sql2 = NULL;     /* SQL query */
    int         status = 0;     /* Status return */
    char        stringval[KSM_INT_STR_SIZE];  /* For Integer to String conversion */
    DB_RESULT	result;         /* Result of the query */
    DB_ROW      row = NULL;     /* Row data */
    DB_RESULT	result2;         /* Result of the query */
    DB_ROW      row2 = NULL;     /* Row data */

    char*       temp_date = NULL; /* place to store date returned */
    char*       temp_repo = NULL; /* place to store repository returned */
    int         temp_backup_req = 0; /* place to store backuprequired returned */

    /* Select rows */
    StrAppend(&sql, "select distinct k.backup, s.name from keypairs k, securitymodules s ");
    StrAppend(&sql, "where s.id = k.securitymodule_id ");
    if (repo_id != -1) {
        StrAppend(&sql, "and s.id = ");
        snprintf(stringval, KSM_INT_STR_SIZE, "%d", repo_id);
        StrAppend(&sql, stringval);
    }
    StrAppend(&sql, " order by backup");

    DusEnd(&sql);

    status = DbExecuteSql(DbHandle(), sql, &result);

    if (status == 0) {
        status = DbFetchRow(result, &row);
        printf("Date:                    Repository:\n");
        while (status == 0) {
            /* Got a row, print it */
            DbString(row, 0, &temp_date);
            DbString(row, 1, &temp_repo);

            if (temp_date != NULL) { /* Ignore non-backup */
                printf("%-24s %s\n", temp_date, temp_repo);
            }
            
            status = DbFetchRow(result, &row);
        }

        /* Convert EOF status to success */

        if (status == -1) {
            status = 0;
        }

        DbFreeResult(result);
    }

    DusFree(sql);
    DbFreeRow(row);
    DbStringFree(temp_date);

    /* List repos which need a backup */
    StrAppend(&sql2, "select s.name, s.requirebackup from keypairs k, securitymodules s ");
    StrAppend(&sql2, "where s.id = k.securitymodule_id ");
    if (repo_id != -1) {
        StrAppend(&sql2, "and s.id = ");
        snprintf(stringval, KSM_INT_STR_SIZE, "%d", repo_id);
        StrAppend(&sql2, stringval);
    }
    StrAppend(&sql2, " and k.backup is null");
    StrAppend(&sql2, " group by s.name order by s.name");

    DusEnd(&sql2);

    status = DbExecuteSql(DbHandle(), sql2, &result2);

    if (status == 0) {
        status = DbFetchRow(result2, &row2);
        while (status == 0) {
            /* Got a row, print it */
            DbString(row2, 0, &temp_repo);
            DbInt(row2, 1, &temp_backup_req);

            if (temp_backup_req == 0) {
                printf("Repository %s has unbacked up keys (that can be used)\n", temp_repo);
            } else {
                printf("Repository %s has unbacked up keys (that will not be used)\n", temp_repo);
            }
            
            status = DbFetchRow(result2, &row2);
        }

        /* Convert EOF status to success */

        if (status == -1) {
            status = 0;
        }

        DbFreeResult(result2);
    }

    DusFree(sql2);
    DbFreeRow(row2);
    DbStringFree(temp_repo);

    return status;
{'fraction_non_alphanumeric': 0.11441755344194368, 'fraction_numerical': 0.009793765891326868, 'mean_word_length': 2.317713214620431, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '4590041', 'n_tokens_mistral': 3378, 'n_tokens_neox': 3152, 'n_words': 1058}