#include <stdio.h>
#include <malloc.h>
#include <string.h>
#ifdef TDRPC
#include <sysent.h>
#endif
#include <unistd.h>
#include "nisdb_rw.h"
#include "db_headers.h"
#include "db_dictlog.h"
#include "nisdb_mt.h"
db_dictlog_entry::db_dictlog_entry(int a, vers * v, char *tname,
table_obj *obj)
{
action = a;
aversion.assign(v);
table_name = tname;
table_object = obj;
next = NULL;
bversion.assign(v);
}
db_dictlog_entry::~db_dictlog_entry()
{
}
void
db_dictlog_entry::print()
{
switch (action) {
case DB_ADD_TABLE:
printf ("add: ");
break;
case DB_REMOVE_TABLE:
printf ("remove: ");
break;
default:
printf ("action(%d): ", action);
break;
}
aversion.print(stdout);
putchar(' ');
if (table_name != NULL)
printf ("table %s\n", table_name);
else
printf("no table!\n");
bversion.print(stdout);
putchar('\n');
}
static void
free_table_entry(table_obj* obj)
{
if (obj == NULL)
return;
if (obj->ta_type != NULL)
free(obj->ta_type);
table_col* tcs = obj->ta_cols.ta_cols_val;
int i;
for (i = 0; i < obj->ta_cols.ta_cols_len; i++) {
if (tcs[i].tc_name != NULL)
delete tcs[i].tc_name;
}
if (tcs != NULL)
delete tcs;
if (obj->ta_path != NULL)
free(obj->ta_path);
free(obj);
}
static void
delete_log_entry(db_dictlog_entry *lentry)
{
char *tname;
table_obj *obj;
if (lentry) {
if ((tname = lentry->get_table_name())) {
delete tname;
}
if ((obj = lentry->get_table_object())) {
free_table_entry(obj);
}
delete lentry;
}
}
int
db_dictlog::execute_on_log(bool_t (*func) (db_dictlog_entry *,
char *, int *),
char* dict, bool_t clean)
{
db_dictlog_entry *j;
int count = 0;
bool_t done = FALSE;
WRITELOCK(this, 0, "w db_dictlog::execute_on_log");
if (open() == FALSE) {
WRITEUNLOCK(this, 0, "wu db_dictlog::execute_on_log");
return (0);
}
while (!done) {
j = get();
if (j == NULL)
break;
if ((*func)(j, dict, &count) == FALSE) done = TRUE;
if (clean) delete_log_entry(j);
}
close();
WRITEUNLOCK(this, count, "wu db_dictlog::execute_on_log");
return (count);
}
static bool_t
print_log_entry(db_dictlog_entry *j, char*, int *count)
{
j->print();
++ *count;
return (TRUE);
}
int
db_dictlog::print()
{
return (execute_on_log(&(print_log_entry), NULL));
}
db_dictlog_entry
*db_dictlog::get()
{
db_dictlog_entry *j;
READLOCK(this, NULL, "r db_dictlog::get");
if (mode != PICKLE_READ) {
READUNLOCK(this, NULL, "ru db_dictlog::get");
return (NULL);
}
j = new db_dictlog_entry;
if (j == NULL) {
READUNLOCK(this, NULL, "ru db_dictlog::get");
return (NULL);
}
if (xdr_db_dictlog_entry(&(xdr), j) == FALSE) {
delete_log_entry (j);
READUNLOCK(this, NULL, "ru db_dictlog::get");
return (NULL);
}
if (! j->sane()) {
WARNING("truncated log entry found");
delete_log_entry(j);
j = NULL;
}
READUNLOCK(this, j, "ru db_dictlog::get");
return (j);
}
int
db_dictlog::append(db_dictlog_entry *j)
{
int status;
WRITELOCK(this, -1, "w db_dictlog::append");
if (mode != PICKLE_APPEND) {
WRITEUNLOCK(this, -1, "wu db_dictlog::append");
return (-1);
}
status = ((xdr_db_dictlog_entry(&(xdr), j)) ? 0 : -1);
if (status < 0) {
WARNING("db_dictlog: could not write log entry");
WRITEUNLOCK(this, status, "wu db_dictlog::append");
return (status);
}
status = fflush(file);
if (status < 0) {
WARNING("db_dictlog: could not flush log entry to disk");
WRITEUNLOCK(this, status, "wu db_dictlog::append");
return (status);
}
status = fsync(fileno(file));
if (status < 0) {
WARNING("db_dictlog: could not sync log entry to disk");
}
WRITEUNLOCK(this, status, "wu db_dictlog::append");
return (status);
}