Commit d7498aeb authored by Per Cederqvist's avatar Per Cederqvist

Added functions wrapper functions around a few stdio functions,

and use them when accessing database files.  This is a preparatory
step for introducing "struct dbfile", which will enable lyskomd to
manipulate more than one database file format at the same time.
That ability is needed for solving bug 1616.
* src/server/ram-io.c, src/server/ram-io.h: New files.
(dbfile_ftell): New function.
(dbfile_getc): New function.
(dbfile_ungetc): New function.
* src/server/Makefile.am (libcheck_a_SOURCES): Added ram-io.c.
(lyskomd_SOURCES): Added ram-io.h and ram-io.c.
(DBCK): Added ram-io.h and ram-io.c.
* src/server/simple-cache.c (read_person): Check the return value
from fparse_set_pos().
(read_conference): Ditto.
(read_text_stat): Ditto.
(write_conf): New static function.
(save_one_conf): Use it to simplify code.
(write_text): New static function.
(save_one_text): Use it to simplify code.
* src/server/ram-parse.h, src/server/ram-parse.c
(fparse_set_pos): New function.
* src/server/ram-output.c: Include ram-io.h.
(foutput_space): New static function.  Use it where appropriate.
(foutput_bool): Ditto.
(foutput_array_start): Ditto.
(foutput_array_end): Ditto.
(foutput_array_nodata): Ditto.
* src/server/ram-output.h, src/server/ram-output.c
(foutput_atsign): New function.
(foutput_newline): New function.
* src/server/dbck-cache.c: Include ram-io.h.  Use dbfile_getc()
instead of getc(), dbfile_ungetc() instead of ungetc(), and
dbfile_ftell() instead of ftell(), when accessing the database
file.  Use foutput_atsign and foutput_newline when appropriate.
* src/server/local-to-global.c: Ditto.
* src/server/ram-parse.c: Ditto.
* src/server/simple-cache.c: Ditto.
parent 4c0b3245
2006-10-17 Per Cederqvist <ceder@lysator.liu.se>
Added functions wrapper functions around a few stdio functions,
and use them when accessing database files. This is a preparatory
step for introducing "struct dbfile", which will enable lyskomd to
manipulate more than one database file format at the same time.
That ability is needed for solving bug 1616.
* src/server/ram-io.c, src/server/ram-io.h: New files.
(dbfile_ftell): New function.
(dbfile_getc): New function.
(dbfile_ungetc): New function.
* src/server/Makefile.am (libcheck_a_SOURCES): Added ram-io.c.
(lyskomd_SOURCES): Added ram-io.h and ram-io.c.
(DBCK): Added ram-io.h and ram-io.c.
* src/server/simple-cache.c (read_person): Check the return value
from fparse_set_pos().
(read_conference): Ditto.
(read_text_stat): Ditto.
(write_conf): New static function.
(save_one_conf): Use it to simplify code.
(write_text): New static function.
(save_one_text): Use it to simplify code.
* src/server/ram-parse.h, src/server/ram-parse.c
(fparse_set_pos): New function.
* src/server/ram-output.c: Include ram-io.h.
(foutput_space): New static function. Use it where appropriate.
(foutput_bool): Ditto.
(foutput_array_start): Ditto.
(foutput_array_end): Ditto.
(foutput_array_nodata): Ditto.
* src/server/ram-output.h, src/server/ram-output.c
(foutput_atsign): New function.
(foutput_newline): New function.
* src/server/dbck-cache.c: Include ram-io.h. Use dbfile_getc()
instead of getc(), dbfile_ungetc() instead of ungetc(), and
dbfile_ftell() instead of ftell(), when accessing the database
file. Use foutput_atsign and foutput_newline when appropriate.
* src/server/local-to-global.c: Ditto.
* src/server/ram-parse.c: Ditto.
* src/server/simple-cache.c: Ditto.
2006-10-01 Per Cederqvist <ceder@lysator.liu.se>
Check that conferences read from the database are the expected
......
......@@ -64,7 +64,7 @@ endif
check_LIBRARIES = libcheck.a
libcheck_a_SOURCES = local-to-global.c ram-parse.c ram-output.c \
log.c ram-smalloc.c memory.c getopt.c getopt1.c \
misc-types.c
misc-types.c ram-io.c
lyskomd_SOURCES = $(DISKOBJS) $(GENOBJS) \
aux-item-def-parse.y \
......@@ -75,7 +75,8 @@ lyskomd_SOURCES = $(DISKOBJS) $(GENOBJS) \
text.h isc-interface.h isc-malloc.h isc-parse.h \
kom-memory.h local-to-global.h log.h lyskomd.h manipulate.h \
minmax.h param.h prot-a-output.h prot-a-parse.h \
prot-a-send-async.h prot-a.h ram-output.h ram-parse.h \
prot-a-send-async.h prot-a.h \
ram-io.h ram-io.c ram-output.h ram-parse.h \
rfc931.h send-async.h server-config.h string-malloc.h \
text-garb.h version-info.h sigflags.h trace-alloc.h \
lockdb.h lockdb.c server-time.h oop-malloc.h oop-malloc.c \
......@@ -153,7 +154,7 @@ DISKOBJS = ramkomd.c ram-smalloc.c simple-cache.c ram-parse.c ram-output.c \
GETOPT = getopt.c getopt1.c
NODIST_DBCK = version-info.c
DBCK = dbck.c dbck-cache.c ram-smalloc.c ram-parse.c server-config.c \
ram-output.c memory.c conf-file.c $(GETOPT) \
ram-io.c ram-output.c memory.c conf-file.c $(GETOPT) \
local-to-global.c lockdb.h lockdb.c
# Files for encrypt (a program to transform the database from unencrypted
......
......@@ -69,6 +69,7 @@
#include "log.h"
#include "lyskomd.h"
#include "kom-memory.h"
#include "ram-io.h"
#include "ram-parse.h"
#include "ram-output.h"
#include "conf-file.h"
......@@ -512,7 +513,7 @@ get_version(const char *fn)
return -1;
fseek(fp, 5, SEEK_SET);
if ( (c = getc(fp)) == '\n')
if ( (c = dbfile_getc(fp)) == '\n')
{
i_fclose(fp);
return 0;
......@@ -652,13 +653,13 @@ cache_sync_all(void)
{
case 0:
if (conf_arr[ic] == NULL)
fprintf(fp, "@");
foutput_atsign(fp);
else
{
fprintf(fp, "+ ");
foutput_conference(fp, conf_arr[ic]);
}
putc('\n', fp);
foutput_newline(fp);
break;
case 1:
case 2:
......@@ -666,7 +667,7 @@ cache_sync_all(void)
{
fprintf(fp, "C %lu", (unsigned long)ic);
foutput_conference(fp, conf_arr[ic]);
putc('\n', fp);
foutput_newline(fp);
}
break;
......@@ -684,7 +685,7 @@ cache_sync_all(void)
{
case 0:
if (pers_arr[ic] == NULL)
fprintf(fp, "@");
foutput_atsign(fp);
else
{
fprintf(fp, "+ ");
......@@ -692,7 +693,7 @@ cache_sync_all(void)
fwrite(pers_arr[ic]->pwd, PASSWD_LEN, 1, fp);
foutput_person(fp, pers_arr[ic]);
}
putc('\n', fp);
foutput_newline(fp);
break;
case 1:
case 2:
......@@ -702,7 +703,7 @@ cache_sync_all(void)
fprintf(fp, " %dH", PASSWD_LEN);
fwrite(pers_arr[ic]->pwd, PASSWD_LEN, 1, fp);
foutput_person(fp, pers_arr[ic]);
putc('\n', fp);
foutput_newline(fp);
}
break;
......@@ -738,13 +739,13 @@ cache_sync_all(void)
{
case 0:
if (text_arr[it] == NULL)
fprintf(fp, "@");
foutput_atsign(fp);
else
{
fprintf(fp, "+ ");
foutput_text_stat(fp, text_arr[it]);
}
putc('\n', fp);
foutput_newline(fp);
break;
case 1:
case 2:
......@@ -752,7 +753,7 @@ cache_sync_all(void)
{
fprintf(fp, "T %lu", it);
foutput_text_stat(fp, text_arr[it]);
putc('\n', fp);
foutput_newline(fp);
}
break;
default:
......@@ -924,7 +925,7 @@ init_cache(void)
{
kom_log("Data file version is '%ld'\n", data_file_version);
kom_log("Reading %d conferences, starting at pos %ld.\n",
next_free_num-1, (unsigned long)ftell(fp));
next_free_num-1, dbfile_ftell(fp));
}
break;
case 1:
......@@ -958,7 +959,7 @@ init_cache(void)
num = i;
fskipwhite(fp);
switch(c = getc(fp))
switch(c = dbfile_getc(fp))
{
case EOF:
switch (data_file_version)
......@@ -988,8 +989,8 @@ init_cache(void)
break;
case 1:
case 2:
restart_kom("@ record in data file version %ld at %lu.\n",
data_file_version, (unsigned long)ftell(fp));
restart_kom("@ record in data file version %ld at %ld.\n",
data_file_version, dbfile_ftell(fp));
break;
default:
restart_kom("Unknown input file format: %ld\n",
......@@ -1001,14 +1002,14 @@ init_cache(void)
switch (data_file_version)
{
case 0:
restart_kom("I record in data file version %ld at %lu.\n",
data_file_version, (unsigned long)ftell(fp));
restart_kom("I record in data file version %ld at %ld.\n",
data_file_version, dbfile_ftell(fp));
break;
case 1:
case 2:
if (fparse_info(fp, &kom_info) != OK)
restart_kom("Invalid I record in data file version %ld at %lu.\n",
data_file_version, (unsigned long)ftell(fp));
restart_kom("Invalid I record in data file version %ld at %ld.\n",
data_file_version, dbfile_ftell(fp));
if (pers_pres_conf != -1)
{
......@@ -1051,8 +1052,8 @@ init_cache(void)
switch (data_file_version)
{
case 0:
restart_kom("C record in data file version %ld at %lu.\n",
data_file_version, (unsigned long)ftell(fp));
restart_kom("C record in data file version %ld at %ld.\n",
data_file_version, dbfile_ftell(fp));
break;
case 1:
......@@ -1078,8 +1079,8 @@ init_cache(void)
switch (data_file_version)
{
case 0:
restart_kom("P record in data file version %ld at %lu.\n",
data_file_version, (unsigned long)ftell(fp));
restart_kom("P record in data file version %ld at %ld.\n",
data_file_version, dbfile_ftell(fp));
break;
case 1:
......@@ -1103,8 +1104,8 @@ init_cache(void)
switch (data_file_version)
{
case 0:
restart_kom("T record in data file version %ld at %lu\n",
data_file_version, (unsigned long)ftell(fp));
restart_kom("T record in data file version %ld at %ld\n",
data_file_version, dbfile_ftell(fp));
break;
case 1:
......@@ -1147,8 +1148,8 @@ init_cache(void)
break;
case 1:
case 2:
restart_kom("+ record in data file version %ld at %lu.\n",
data_file_version, (unsigned long)ftell(fp));
restart_kom("+ record in data file version %ld at %ld.\n",
data_file_version, dbfile_ftell(fp));
break;
default:
restart_kom("Unknown input file format: %ld\n",
......@@ -1160,13 +1161,13 @@ init_cache(void)
switch (data_file_version)
{
case 0:
restart_kom("#record in data file version %ld at %lu.\n",
data_file_version, (unsigned long)ftell(fp));
restart_kom("#record in data file version %ld at %ld.\n",
data_file_version, dbfile_ftell(fp));
break;
case 1:
case 2:
switch (c = getc(fp))
switch (c = dbfile_getc(fp))
{
case 'C':
next_free_num = fparse_long(fp);
......@@ -1178,9 +1179,9 @@ init_cache(void)
break;
default:
restart_kom("Unknown # record (#%c) in data "
"file version %ld at %lu.\n",
"file version %ld at %ld.\n",
c, data_file_version,
(unsigned long)ftell(fp));
dbfile_ftell(fp));
}
break;
......@@ -1194,13 +1195,13 @@ init_cache(void)
switch (data_file_version)
{
case 0:
restart_kom("- record in data file version %ld at %lu.\n",
data_file_version, (unsigned long)ftell(fp));
restart_kom("- record in data file version %ld at %ld.\n",
data_file_version, dbfile_ftell(fp));
break;
case 1:
case 2:
switch(c = getc(fp))
switch(c = dbfile_getc(fp))
{
case 'C':
num = fparse_long(fp);
......@@ -1222,9 +1223,9 @@ init_cache(void)
break;
default:
restart_kom("Unknown - record (%c) in data file "
"version %ld at %lu.\n",
"version %ld at %ld.\n",
c, data_file_version,
(unsigned long)ftell(fp));
dbfile_ftell(fp));
}
break;
......@@ -1236,8 +1237,8 @@ init_cache(void)
default:
restart_kom("Unknown record type in data file version "
"%ld at %lu.\n",
data_file_version, (unsigned long)ftell(fp));
"%ld at %ld.\n",
data_file_version, dbfile_ftell(fp));
}
}
......@@ -1249,12 +1250,12 @@ init_cache(void)
if (data_file_version == 0)
{
if ( vflag )
kom_log("Reading %d persons, starting at pos %lu.\n",
next_free_num-1, (unsigned long)ftell(fp));
kom_log("Reading %d persons, starting at pos %ld.\n",
next_free_num-1, dbfile_ftell(fp));
for ( i = 1; i < next_free_num; i++ ) /* PERSONS */
{
fskipwhite(fp);
switch(getc(fp))
switch(dbfile_getc(fp))
{
case '@':
pers_arr[ i ] = NULL;
......@@ -1273,13 +1274,13 @@ init_cache(void)
read_text_num = 1;
if ( vflag )
kom_log("Reading %ld texts, starting at pos %lu.\n",
next_text_num-1, (unsigned long)ftell(fp));
kom_log("Reading %ld texts, starting at pos %ld.\n",
next_text_num-1, dbfile_ftell(fp));
for ( i = 1; i < next_text_num; i++ ) /* TEXT_STATS */
{
fskipwhite(fp);
switch(getc(fp))
switch(dbfile_getc(fp))
{
case '@':
text_arr[ i ] = NULL;
......@@ -1303,8 +1304,8 @@ init_cache(void)
}
} /* if (datafile_version == 0) */
kom_log("Read %d confs/persons and %ld texts, eof at %lu\n",
next_free_num-1, next_text_num-1, (unsigned long)ftell(fp));
kom_log("Read %d confs/persons and %ld texts, eof at %ld\n",
next_free_num-1, next_text_num-1, dbfile_ftell(fp));
if (read_text_num == 0 || read_conf_num == 0)
restart_kom("init_cache(): Failed to read next_conf or text num.\n");
......
......@@ -24,6 +24,7 @@
#include "s-string.h"
#include "kom-types.h"
#include "ram-io.h"
#include "local-to-global.h"
#include "log.h"
#include "ram-parse.h"
......@@ -1182,10 +1183,10 @@ l2g_read(FILE *fp, Local_to_global *l2g)
/* Read past the start marker */
fskipwhite(fp);
if ( (c = getc(fp)) == EOF || c != '[')
if ( (c = dbfile_getc(fp)) == EOF || c != '[')
{
kom_log("l2g_read() failed to find ``['' marker at pos %lu.\n",
(unsigned long) ftell(fp));
kom_log("l2g_read() failed to find ``['' marker at pos %ld.\n",
dbfile_ftell(fp));
return FAILURE;
}
......@@ -1194,10 +1195,10 @@ l2g_read(FILE *fp, Local_to_global *l2g)
/* Read numbers until the EOL (end-of-l2g :-) ) marker is read. */
while (1)
{
if ((c = getc(fp)) == EOF)
if ((c = dbfile_getc(fp)) == EOF)
{
kom_log("l2g_read(): unexpected EOF at pos %lu.\n",
(unsigned long) ftell(fp));
kom_log("l2g_read(): unexpected EOF at pos %ld.\n",
dbfile_ftell(fp));
return FAILURE;
}
......@@ -1207,16 +1208,16 @@ l2g_read(FILE *fp, Local_to_global *l2g)
lno = fparse_long(fp);
if (lno == 0)
{
kom_log("l2g_read(): got local number 0 at pos %lu.\n",
(unsigned long) ftell(fp));
kom_log("l2g_read(): got local number 0 at pos %ld.\n",
dbfile_ftell(fp));
return FAILURE;
}
break;
case ',':
if (lno == 0)
{
kom_log("l2g_read(): missing local number at pos %lu.\n",
(unsigned long)ftell(fp));
kom_log("l2g_read(): missing local number at pos %ld.\n",
dbfile_ftell(fp));
return FAILURE;
}
++lno;
......@@ -1224,8 +1225,8 @@ l2g_read(FILE *fp, Local_to_global *l2g)
case ':':
if (lno == 0)
{
kom_log("l2g_read(): missing local number at pos %lu.\n",
(unsigned long)ftell(fp));
kom_log("l2g_read(): missing local number at pos %ld.\n",
dbfile_ftell(fp));
return FAILURE;
}
tno = fparse_long(fp);
......@@ -1236,8 +1237,8 @@ l2g_read(FILE *fp, Local_to_global *l2g)
l2g->first_unused = first_unused;
return OK;
default:
kom_log("l2g_read(): unexpected character ``%c'' at pos %lu.\n",
c, (unsigned long) ftell(fp));
kom_log("l2g_read(): unexpected character ``%c'' at pos %ld.\n",
c, dbfile_ftell(fp));
return FAILURE;
}
}
......
/* Database file handling.
* Copyright (C) 1991, 1993-1999, 2001-2003 Lysator Academic Computer Association.
*
* This file is part of the LysKOM server.
*
* LysKOM is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* any later version.
*
* LysKOM 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 General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with LysKOM; see the file COPYING. If not, write to
* Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
* or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
* MA 02139, USA.
*
* Please report bugs at http://bugzilla.lysator.liu.se/.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <assert.h>
#include "timewrap.h"
#include "server/smalloc.h"
#include "eintr.h"
#include "kom-types.h"
#include "ram-io.h"
#include "ram-parse.h"
#include "lyskomd.h"
#include "log.h"
long
dbfile_ftell(FILE *fp)
{
return ftell(fp);
}
int
dbfile_getc(FILE *fp)
{
return getc(fp);
}
int
dbfile_ungetc(FILE *fp,
int c)
{
return ungetc(c, fp);
}
/* Store database file state.
* Copyright (C) 2006 Lysator Academic Computer Association.
*
* This file is part of the LysKOM server.
*
* LysKOM is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* any later version.
*
* LysKOM 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 General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with LysKOM; see the file COPYING. If not, write to
* Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
* or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
* MA 02139, USA.
*
* Please report bugs at http://bugzilla.lysator.liu.se/.
*/
long
dbfile_ftell(FILE *fp);
int
dbfile_getc(FILE *fp);
int
dbfile_ungetc(FILE *fp, int c);
......@@ -44,6 +44,7 @@
#include "s-string.h"
#include "kom-types.h"
#include "ram-io.h"
#include "ram-output.h"
#include "lyskomd.h"
#include "log.h"
......@@ -64,6 +65,11 @@ static void foutput_string(FILE *, String);
static void foutput_text_list(FILE *, const Local_to_global *);
static void foutput_time(FILE *, time_t);
static void foutput_ulong(unsigned long, FILE *);
static void foutput_space(FILE *fp);
static void foutput_bool(FILE *fp, int val);
static void foutput_array_start(FILE *fp);
static void foutput_array_end(FILE *fp);
static void foutput_array_nodata(FILE *fp);
static int output_format = 2;
......@@ -163,7 +169,7 @@ foutput_person_2(FILE *fp,
foutput_priv_bits (fp, person->privileges);
foutput_personal_flags (fp, person->flags);
putc(' ', fp);
foutput_space(fp);
l2g_write (fp, &person->created_texts);
foutput_mark_list (fp, person->marks);
foutput_membership_list (fp, person->conferences);
......@@ -210,7 +216,7 @@ foutput_conference_2(FILE *fp,
{
foutput_string(fp, conf_c->name);
foutput_member_list(fp, conf_c->members);
putc(' ', fp);
foutput_space(fp);
l2g_write(fp, &conf_c->texts);
foutput_conf_type(fp, conf_c->type);
......@@ -314,13 +320,13 @@ foutput_text_stat_0(FILE *fp,
if ( t_stat->misc_items != NULL && t_stat->no_of_misc > 0 )
{
fputs(" {", fp);
foutput_array_start(fp);
for ( i = 0; i < t_stat->no_of_misc; i++ )
foutput_misc_info(fp, t_stat->misc_items[ i ]);
fputs(" }", fp);
foutput_array_end(fp);
}
else
fputs(" *", fp);
foutput_array_nodata(fp);
}
static void
......@@ -340,13 +346,13 @@ foutput_text_stat_2(FILE *fp,
if ( t_stat->misc_items != NULL && t_stat->no_of_misc > 0 )
{
fputs(" {", fp);
foutput_array_start(fp);
for ( i = 0; i < t_stat->no_of_misc; i++ )
foutput_misc_info(fp, t_stat->misc_items[ i ]);
fputs(" }", fp);
foutput_array_end(fp);
}
else
fputs(" *", fp);
foutput_array_nodata(fp);
foutput_ulong((unsigned long) t_stat->highest_aux, fp);
foutput_aux_item_list(fp, &t_stat->aux_item_list);
......@@ -377,15 +383,15 @@ static void
foutput_aux_flags(FILE *fp,
Aux_item_flags f)
{
putc(' ', fp);
putc(f.deleted + '0', fp);
putc(f.inherit + '0', fp);
putc(f.secret + '0', fp);
putc(f.hide_creator + '0', fp);
putc(f.dont_garb + '0', fp);
putc(f.reserved3 + '0', fp);
putc(f.reserved4 + '0', fp);
putc(f.reserved5 + '0', fp);
foutput_space(fp);
foutput_bool(fp, f.deleted);
foutput_bool(fp, f.inherit);
foutput_bool(fp, f.secret);
foutput_bool(fp, f.hide_creator);
foutput_bool(fp, f.dont_garb);
foutput_bool(fp, f.reserved3);
foutput_bool(fp, f.reserved4);
foutput_bool(fp, f.reserved5);
}
static void
......@@ -430,13 +436,13 @@ foutput_aux_item_list(FILE *fp,