<commit_before>/******************************************************************************
Copyright 2006 Palmsource, Inc (an ACCESS company).

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*****************************************************************************/
/*!
 * @file sqlfs.c
 *
 * @brief file system on top of a SQL database library
 *  API implementation
 *
 *****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <pthread.h>
#include <time.h>
#include "sqlfs.h"

#ifdef HAVE_LIBSQLCIPHER
# include "sqlcipher/sqlite3.h"
#else
# include "sqlite3.h"
#endif


#define INDEX 0

#define PREPARE_STMT\
    stmt = get_sqlfs(sqlfs)->stmts[INDEX];\
    r = SQLITE_OK; \
    if (stmt)\
    {\
        if (sqlite3_expired(stmt))\
        {\
            sqlite3_finalize(stmt);\
            r = ~SQLITE_OK;\
        }\
    }\
    else r = ~SQLITE_OK;\
    if (r != SQLITE_OK)

#define DONE_PREPARE  if (r == SQLITE_OK) get_sqlfs(sqlfs)->stmts[INDEX] = stmt; else get_sqlfs(sqlfs)->stmts[INDEX] = 0;

#define SQLITE3_PREPARE(a, b, c, d, e)\
    PREPARE_STMT \
    r = sqlite3_prepare((a), (b), (c), (d), (e));\
    DONE_PREPARE


static const int BLOCK_SIZE = 8192;

static pthread_key_t sql_key;

static char default_db_file[PATH_MAX] = { 0 };
#define MAX_DB_KEY_LENGTH 512
static char default_db_key[MAX_DB_KEY_LENGTH] = { 0 };

static int max_inode = 0;

static void * sqlfs_t_init(const char *db_file, const char *db_key);
static void sqlfs_t_finalize(void *arg);

static __inline__ int sql_step(sqlite3_stmt *stmt)
{
    int r, i;
    for (i = 0; i < (1 * 1000 / 100); i++)
    {
        r = sqlite3_step(stmt);
        if (r != SQLITE_BUSY)
            break;
    }

    return r;
}

static __inline__ sqlfs_t *get_sqlfs(sqlfs_t *p)
{
    sqlfs_t *sqlfs;

    if (p)
        return p;

    sqlfs = (sqlfs_t *) (pthread_getspecific(sql_key));
    if (sqlfs)
        return sqlfs;

    sqlfs = (sqlfs_t*) sqlfs_t_init(default_db_file, default_db_key);
    pthread_setspecific(sql_key, sqlfs);
    return sqlfs;
}
static __inline__ int get_new_inode(void)
{
    return ++max_inode;
}

static __inline__ void remove_tail_slash(char *str)
{
    char *s = str + strlen(str) - 1;
    while (s != str - 1)
    {
        if (*s != '/')
            break;
        *s = 0;
        s--;
    }
}

static __inline__ char *make_str_copy(const char *str)
{
    if (str == 0)
        return 0;
    return strdup(str);
}

#ifdef __ANDROID__
#include <android/log.h>
#define LOG_TAG "libsqlfs"
#define  LOGW(...)  __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
static void show_msg(FILE *f, char *fmt, ...)
{
    char buf[1000];
    va_list ap;

    va_start(ap, fmt);
    vsnprintf(buf, 1000, fmt, ap);
    va_end(ap);
    __android_log_print(ANDROID_LOG_WARN, LOG_TAG, buf);
}
#else
static void show_msg(FILE *f, char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);

    vfprintf(f, fmt, ap);
    va_end(ap);
}
#endif /* __ANDROID__ */



void clean_attr(key_attr *attr)
{
    free(attr->path);<commit_msg>fix issue spotted by -Wformat-security<commit_after>/******************************************************************************
Copyright 2006 Palmsource, Inc (an ACCESS company).

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*****************************************************************************/
/*!
 * @file sqlfs.c
 *
 * @brief file system on top of a SQL database library
 *  API implementation
 *
 *****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <pthread.h>
#include <time.h>
#include "sqlfs.h"

#ifdef HAVE_LIBSQLCIPHER
# include "sqlcipher/sqlite3.h"
#else
# include "sqlite3.h"
#endif


#define INDEX 0

#define PREPARE_STMT\
    stmt = get_sqlfs(sqlfs)->stmts[INDEX];\
    r = SQLITE_OK; \
    if (stmt)\
    {\
        if (sqlite3_expired(stmt))\
        {\
            sqlite3_finalize(stmt);\
            r = ~SQLITE_OK;\
        }\
    }\
    else r = ~SQLITE_OK;\
    if (r != SQLITE_OK)

#define DONE_PREPARE  if (r == SQLITE_OK) get_sqlfs(sqlfs)->stmts[INDEX] = stmt; else get_sqlfs(sqlfs)->stmts[INDEX] = 0;

#define SQLITE3_PREPARE(a, b, c, d, e)\
    PREPARE_STMT \
    r = sqlite3_prepare((a), (b), (c), (d), (e));\
    DONE_PREPARE


static const int BLOCK_SIZE = 8192;

static pthread_key_t sql_key;

static char default_db_file[PATH_MAX] = { 0 };
#define MAX_DB_KEY_LENGTH 512
static char default_db_key[MAX_DB_KEY_LENGTH] = { 0 };

static int max_inode = 0;

static void * sqlfs_t_init(const char *db_file, const char *db_key);
static void sqlfs_t_finalize(void *arg);

static __inline__ int sql_step(sqlite3_stmt *stmt)
{
    int r, i;
    for (i = 0; i < (1 * 1000 / 100); i++)
    {
        r = sqlite3_step(stmt);
        if (r != SQLITE_BUSY)
            break;
    }

    return r;
}

static __inline__ sqlfs_t *get_sqlfs(sqlfs_t *p)
{
    sqlfs_t *sqlfs;

    if (p)
        return p;

    sqlfs = (sqlfs_t *) (pthread_getspecific(sql_key));
    if (sqlfs)
        return sqlfs;

    sqlfs = (sqlfs_t*) sqlfs_t_init(default_db_file, default_db_key);
    pthread_setspecific(sql_key, sqlfs);
    return sqlfs;
}
static __inline__ int get_new_inode(void)
{
    return ++max_inode;
}

static __inline__ void remove_tail_slash(char *str)
{
    char *s = str + strlen(str) - 1;
    while (s != str - 1)
    {
        if (*s != '/')
            break;
        *s = 0;
        s--;
    }
}

static __inline__ char *make_str_copy(const char *str)
{
    if (str == 0)
        return 0;
    return strdup(str);
}

#ifdef __ANDROID__
#include <android/log.h>
#define LOG_TAG "libsqlfs"
#define  LOGW(...)  __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
static void show_msg(FILE *f, char *fmt, ...)
{
    char buf[1000];
    va_list ap;

    va_start(ap, fmt);
    vsnprintf(buf, 1000, fmt, ap);
    va_end(ap);
    __android_log_print(ANDROID_LOG_WARN, LOG_TAG, "%s", buf);
}
#else
static void show_msg(FILE *f, char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);

    vfprintf(f, fmt, ap);
    va_end(ap);
}
#endif /* __ANDROID__ */



void clean_attr(key_attr *attr)
{
    free(attr->path);
{'fraction_non_alphanumeric': 0.17342940209834407, 'fraction_numerical': 0.014915939830615598, 'mean_word_length': 3.459977452085682, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 39, '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': '3758723', 'n_tokens_mistral': 2902, 'n_tokens_neox': 2743, 'n_words': 798}