<commit_before>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <alloca.h>
#include <endian.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <uuid/uuid.h>

#include "pgdb-internal.h"

static void __pgdb_free(pgdb_t *db)
{
	if (!db)
		return;

	free(db->pathname);

	if (db->superblock)
		pgcodec__superblock__free_unpacked(db->superblock, NULL);

	memset(db, 0xff, sizeof(*db));
	free(db);
}

void pgdb_close(pgdb_t* db)
{
	__pgdb_free(db);
}

static bool is_dir(const char *pathname, bool readonly, char **errptr)
{
	struct stat st;

	if (stat(pathname, &st) < 0) {
		*errptr = strdup(strerror(errno));
		return false;
	}
	if (!S_ISDIR(st.st_mode)) {
		*errptr = "not a directory";
		return false;
	}

	int mode = R_OK;
	if (!readonly)
		mode |= W_OK;
	if (access(pathname, mode)) {
		*errptr = strdup(strerror(errno));
		return false;
	}

	return true;
}

static bool pg_create_db(pgdb_t *db, char **errptr)
{
	// generate root table UUID
	uuid_t tab_uuid;
	char tab_uuid_s[128];
	uuid_generate_random(tab_uuid);
	uuid_unparse(tab_uuid, tab_uuid_s);

	// generate initial root table
	PGcodec__TableMeta table;
	table.name = "root";
	table.uuid = tab_uuid_s;
	table.root_id = 0;
	PGcodec__TableMeta *tables[1] = { &table };

	// generate root superblock UUID
	uuid_t sb_uuid;
	char sb_uuid_s[128];
	uuid_generate_random(sb_uuid);
	uuid_unparse(sb_uuid, sb_uuid_s);

	// generate initial superblock
	PGcodec__Superblock sb = PGCODEC__SUPERBLOCK__INIT;
	sb.uuid = sb_uuid_s;
	sb.n_tables = 1;
	sb.tables = tables;

	// create database directory
	if (mkdir(db->pathname, 0666) < 0) {
		*errptr = strdup(strerror(errno));
		return false;
	}

	// write superblock file
	if (!pg_write_superblock(db, &sb, errptr))
		return false;
<commit_msg>rename default table to "master"<commit_after>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <alloca.h>
#include <endian.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <uuid/uuid.h>

#include "pgdb-internal.h"

static void __pgdb_free(pgdb_t *db)
{
	if (!db)
		return;

	free(db->pathname);

	if (db->superblock)
		pgcodec__superblock__free_unpacked(db->superblock, NULL);

	memset(db, 0xff, sizeof(*db));
	free(db);
}

void pgdb_close(pgdb_t* db)
{
	__pgdb_free(db);
}

static bool is_dir(const char *pathname, bool readonly, char **errptr)
{
	struct stat st;

	if (stat(pathname, &st) < 0) {
		*errptr = strdup(strerror(errno));
		return false;
	}
	if (!S_ISDIR(st.st_mode)) {
		*errptr = "not a directory";
		return false;
	}

	int mode = R_OK;
	if (!readonly)
		mode |= W_OK;
	if (access(pathname, mode)) {
		*errptr = strdup(strerror(errno));
		return false;
	}

	return true;
}

static bool pg_create_db(pgdb_t *db, char **errptr)
{
	// generate root table UUID
	uuid_t tab_uuid;
	char tab_uuid_s[128];
	uuid_generate_random(tab_uuid);
	uuid_unparse(tab_uuid, tab_uuid_s);

	// generate initial root table
	PGcodec__TableMeta table;
	table.name = "master";
	table.uuid = tab_uuid_s;
	table.root_id = 0;
	PGcodec__TableMeta *tables[1] = { &table };

	// generate root superblock UUID
	uuid_t sb_uuid;
	char sb_uuid_s[128];
	uuid_generate_random(sb_uuid);
	uuid_unparse(sb_uuid, sb_uuid_s);

	// generate initial superblock
	PGcodec__Superblock sb = PGCODEC__SUPERBLOCK__INIT;
	sb.uuid = sb_uuid_s;
	sb.n_tables = 1;
	sb.tables = tables;

	// create database directory
	if (mkdir(db->pathname, 0666) < 0) {
		*errptr = strdup(strerror(errno));
		return false;
	}

	// write superblock file
	if (!pg_write_superblock(db, &sb, errptr))
		return false;

{'fraction_non_alphanumeric': 0.14451334591534107, 'fraction_numerical': 0.00862766244270693, 'mean_word_length': 4.612708018154311, 'pattern_counts': {'":': 0, '<': 31, '<?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': '2412067', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1513, 'n_words': 336}