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> 2006-10-01 Per Cederqvist <ceder@lysator.liu.se>
Check that conferences read from the database are the expected Check that conferences read from the database are the expected
......
...@@ -64,7 +64,7 @@ endif ...@@ -64,7 +64,7 @@ endif
check_LIBRARIES = libcheck.a check_LIBRARIES = libcheck.a
libcheck_a_SOURCES = local-to-global.c ram-parse.c ram-output.c \ libcheck_a_SOURCES = local-to-global.c ram-parse.c ram-output.c \
log.c ram-smalloc.c memory.c getopt.c getopt1.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) \ lyskomd_SOURCES = $(DISKOBJS) $(GENOBJS) \
aux-item-def-parse.y \ aux-item-def-parse.y \
...@@ -75,7 +75,8 @@ lyskomd_SOURCES = $(DISKOBJS) $(GENOBJS) \ ...@@ -75,7 +75,8 @@ lyskomd_SOURCES = $(DISKOBJS) $(GENOBJS) \
text.h isc-interface.h isc-malloc.h isc-parse.h \ text.h isc-interface.h isc-malloc.h isc-parse.h \
kom-memory.h local-to-global.h log.h lyskomd.h manipulate.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 \ 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 \ rfc931.h send-async.h server-config.h string-malloc.h \
text-garb.h version-info.h sigflags.h trace-alloc.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 \ 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 \ ...@@ -153,7 +154,7 @@ DISKOBJS = ramkomd.c ram-smalloc.c simple-cache.c ram-parse.c ram-output.c \
GETOPT = getopt.c getopt1.c GETOPT = getopt.c getopt1.c
NODIST_DBCK = version-info.c NODIST_DBCK = version-info.c
DBCK = dbck.c dbck-cache.c ram-smalloc.c ram-parse.c server-config.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 local-to-global.c lockdb.h lockdb.c
# Files for encrypt (a program to transform the database from unencrypted # Files for encrypt (a program to transform the database from unencrypted
......
...@@ -69,6 +69,7 @@ ...@@ -69,6 +69,7 @@
#include "log.h" #include "log.h"
#include "lyskomd.h" #include "lyskomd.h"
#include "kom-memory.h" #include "kom-memory.h"
#include "ram-io.h"
#include "ram-parse.h" #include "ram-parse.h"
#include "ram-output.h" #include "ram-output.h"
#include "conf-file.h" #include "conf-file.h"
...@@ -512,7 +513,7 @@ get_version(const char *fn) ...@@ -512,7 +513,7 @@ get_version(const char *fn)
return -1; return -1;
fseek(fp, 5, SEEK_SET); fseek(fp, 5, SEEK_SET);
if ( (c = getc(fp)) == '\n') if ( (c = dbfile_getc(fp)) == '\n')
{ {
i_fclose(fp); i_fclose(fp);
return 0; return 0;
...@@ -652,13 +653,13 @@ cache_sync_all(void) ...@@ -652,13 +653,13 @@ cache_sync_all(void)
{ {
case 0: case 0:
if (conf_arr[ic] == NULL) if (conf_arr[ic] == NULL)
fprintf(fp, "@"); foutput_atsign(fp);
else else
{ {
fprintf(fp, "+ "); fprintf(fp, "+ ");
foutput_conference(fp, conf_arr[ic]); foutput_conference(fp, conf_arr[ic]);
} }
putc('\n', fp); foutput_newline(fp);
break; break;
case 1: case 1:
case 2: case 2:
...@@ -666,7 +667,7 @@ cache_sync_all(void) ...@@ -666,7 +667,7 @@ cache_sync_all(void)
{ {
fprintf(fp, "C %lu", (unsigned long)ic); fprintf(fp, "C %lu", (unsigned long)ic);
foutput_conference(fp, conf_arr[ic]); foutput_conference(fp, conf_arr[ic]);
putc('\n', fp); foutput_newline(fp);
} }
break; break;
...@@ -684,7 +685,7 @@ cache_sync_all(void) ...@@ -684,7 +685,7 @@ cache_sync_all(void)
{ {
case 0: case 0:
if (pers_arr[ic] == NULL) if (pers_arr[ic] == NULL)
fprintf(fp, "@"); foutput_atsign(fp);
else else
{ {
fprintf(fp, "+ "); fprintf(fp, "+ ");
...@@ -692,7 +693,7 @@ cache_sync_all(void) ...@@ -692,7 +693,7 @@ cache_sync_all(void)
fwrite(pers_arr[ic]->pwd, PASSWD_LEN, 1, fp); fwrite(pers_arr[ic]->pwd, PASSWD_LEN, 1, fp);
foutput_person(fp, pers_arr[ic]); foutput_person(fp, pers_arr[ic]);
} }
putc('\n', fp); foutput_newline(fp);
break; break;
case 1: case 1:
case 2: case 2:
...@@ -702,7 +703,7 @@ cache_sync_all(void) ...@@ -702,7 +703,7 @@ cache_sync_all(void)
fprintf(fp, " %dH", PASSWD_LEN); fprintf(fp, " %dH", PASSWD_LEN);
fwrite(pers_arr[ic]->pwd, PASSWD_LEN, 1, fp); fwrite(pers_arr[ic]->pwd, PASSWD_LEN, 1, fp);
foutput_person(fp, pers_arr[ic]); foutput_person(fp, pers_arr[ic]);
putc('\n', fp); foutput_newline(fp);
} }
break; break;
...@@ -738,13 +739,13 @@ cache_sync_all(void) ...@@ -738,13 +739,13 @@ cache_sync_all(void)
{ {
case 0: case 0:
if (text_arr[it] == NULL) if (text_arr[it] == NULL)
fprintf(fp, "@"); foutput_atsign(fp);
else else
{ {
fprintf(fp, "+ "); fprintf(fp, "+ ");
foutput_text_stat(fp, text_arr[it]); foutput_text_stat(fp, text_arr[it]);
} }
putc('\n', fp); foutput_newline(fp);
break; break;
case 1: case 1:
case 2: case 2:
...@@ -752,7 +753,7 @@ cache_sync_all(void) ...@@ -752,7 +753,7 @@ cache_sync_all(void)
{ {
fprintf(fp, "T %lu", it); fprintf(fp, "T %lu", it);
foutput_text_stat(fp, text_arr[it]); foutput_text_stat(fp, text_arr[it]);
putc('\n', fp); foutput_newline(fp);
} }
break; break;
default: default:
...@@ -924,7 +925,7 @@ init_cache(void) ...@@ -924,7 +925,7 @@ init_cache(void)
{ {
kom_log("Data file version is '%ld'\n", data_file_version); kom_log("Data file version is '%ld'\n", data_file_version);
kom_log("Reading %d conferences, starting at pos %ld.\n", 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; break;
case 1: case 1:
...@@ -958,7 +959,7 @@ init_cache(void) ...@@ -958,7 +959,7 @@ init_cache(void)
num = i; num = i;
fskipwhite(fp); fskipwhite(fp);
switch(c = getc(fp)) switch(c = dbfile_getc(fp))
{ {
case EOF: case EOF:
switch (data_file_version) switch (data_file_version)
...@@ -988,8 +989,8 @@ init_cache(void) ...@@ -988,8 +989,8 @@ init_cache(void)
break; break;
case 1: case 1:
case 2: case 2:
restart_kom("@ record in data file version %ld at %lu.\n", restart_kom("@ record in data file version %ld at %ld.\n",
data_file_version, (unsigned long)ftell(fp)); data_file_version, dbfile_ftell(fp));
break; break;
default: default:
restart_kom("Unknown input file format: %ld\n", restart_kom("Unknown input file format: %ld\n",
...@@ -1001,14 +1002,14 @@ init_cache(void) ...@@ -1001,14 +1002,14 @@ init_cache(void)
switch (data_file_version) switch (data_file_version)
{ {
case 0: case 0:
restart_kom("I record in data file version %ld at %lu.\n", restart_kom("I record in data file version %ld at %ld.\n",
data_file_version, (unsigned long)ftell(fp)); data_file_version, dbfile_ftell(fp));
break; break;
case 1: case 1:
case 2: case 2:
if (fparse_info(fp, &kom_info) != OK) if (fparse_info(fp, &kom_info) != OK)
restart_kom("Invalid I record in data file version %ld at %lu.\n", restart_kom("Invalid I record in data file version %ld at %ld.\n",
data_file_version, (unsigned long)ftell(fp)); data_file_version, dbfile_ftell(fp));
if (pers_pres_conf != -1) if (pers_pres_conf != -1)
{ {
...@@ -1051,8 +1052,8 @@ init_cache(void) ...@@ -1051,8 +1052,8 @@ init_cache(void)
switch (data_file_version) switch (data_file_version)
{ {
case 0: case 0:
restart_kom("C record in data file version %ld at %lu.\n", restart_kom("C record in data file version %ld at %ld.\n",
data_file_version, (unsigned long)ftell(fp)); data_file_version, dbfile_ftell(fp));
break; break;
case 1: case 1:
...@@ -1078,8 +1079,8 @@ init_cache(void) ...@@ -1078,8 +1079,8 @@ init_cache(void)
switch (data_file_version) switch (data_file_version)
{ {
case 0: case 0:
restart_kom("P record in data file version %ld at %lu.\n", restart_kom("P record in data file version %ld at %ld.\n",
data_file_version, (unsigned long)ftell(fp)); data_file_version, dbfile_ftell(fp));
break; break;
case 1: case 1:
...@@ -1103,8 +1104,8 @@ init_cache(void) ...@@ -1103,8 +1104,8 @@ init_cache(void)
switch (data_file_version) switch (data_file_version)
{ {
case 0: case 0:
restart_kom("T record in data file version %ld at %lu\n", restart_kom("T record in data file version %ld at %ld\n",
data_file_version, (unsigned long)ftell(fp)); data_file_version, dbfile_ftell(fp));
break; break;
case 1: case 1:
...@@ -1147,8 +1148,8 @@ init_cache(void) ...@@ -1147,8 +1148,8 @@ init_cache(void)
break; break;
case 1: case 1:
case 2: case 2:
restart_kom("+ record in data file version %ld at %lu.\n", restart_kom("+ record in data file version %ld at %ld.\n",
data_file_version, (unsigned long)ftell(fp)); data_file_version, dbfile_ftell(fp));
break; break;
default: default:
restart_kom("Unknown input file format: %ld\n", restart_kom("Unknown input file format: %ld\n",
...@@ -1160,13 +1161,13 @@ init_cache(void) ...@@ -1160,13 +1161,13 @@ init_cache(void)
switch (data_file_version) switch (data_file_version)
{ {
case 0: case 0:
restart_kom("#record in data file version %ld at %lu.\n", restart_kom("#record in data file version %ld at %ld.\n",
data_file_version, (unsigned long)ftell(fp)); data_file_version, dbfile_ftell(fp));
break; break;
case 1: case 1:
case 2: case 2:
switch (c = getc(fp)) switch (c = dbfile_getc(fp))
{ {
case 'C': case 'C':
next_free_num = fparse_long(fp); next_free_num = fparse_long(fp);
...@@ -1178,9 +1179,9 @@ init_cache(void) ...@@ -1178,9 +1179,9 @@ init_cache(void)
break; break;
default: default:
restart_kom("Unknown # record (#%c) in data " restart_kom("Unknown # record (#%c) in data "
"file version %ld at %lu.\n", "file version %ld at %ld.\n",
c, data_file_version, c, data_file_version,
(unsigned long)ftell(fp)); dbfile_ftell(fp));
} }
break; break;
...@@ -1194,13 +1195,13 @@ init_cache(void) ...@@ -1194,13 +1195,13 @@ init_cache(void)
switch (data_file_version) switch (data_file_version)
{ {
case 0: case 0:
restart_kom("- record in data file version %ld at %lu.\n", restart_kom("- record in data file version %ld at %ld.\n",
data_file_version, (unsigned long)ftell(fp)); data_file_version, dbfile_ftell(fp));
break; break;
case 1: case 1:
case 2: case 2:
switch(c = getc(fp)) switch(c = dbfile_getc(fp))
{ {
case 'C': case 'C':
num = fparse_long(fp); num = fparse_long(fp);
...@@ -1222,9 +1223,9 @@ init_cache(void) ...@@ -1222,9 +1223,9 @@ init_cache(void)
break; break;
default: default:
restart_kom("Unknown - record (%c) in data file " restart_kom("Unknown - record (%c) in data file "
"version %ld at %lu.\n", "version %ld at %ld.\n",
c, data_file_version, c, data_file_version,
(unsigned long)ftell(fp)); dbfile_ftell(fp));
} }
break; break;
...@@ -1236,8 +1237,8 @@ init_cache(void) ...@@ -1236,8 +1237,8 @@ init_cache(void)
default: default:
restart_kom("Unknown record type in data file version " restart_kom("Unknown record type in data file version "
"%ld at %lu.\n", "%ld at %ld.\n",
data_file_version, (unsigned long)ftell(fp)); data_file_version, dbfile_ftell(fp));
} }
} }
...@@ -1249,12 +1250,12 @@ init_cache(void) ...@@ -1249,12 +1250,12 @@ init_cache(void)
if (data_file_version == 0) if (data_file_version == 0)
{ {
if ( vflag ) if ( vflag )
kom_log("Reading %d persons, starting at pos %lu.\n", kom_log("Reading %d persons, starting at pos %ld.\n",
next_free_num-1, (unsigned long)ftell(fp)); next_free_num-1, dbfile_ftell(fp));
for ( i = 1; i < next_free_num; i++ ) /* PERSONS */ for ( i = 1; i < next_free_num; i++ ) /* PERSONS */
{ {
fskipwhite(fp); fskipwhite(fp);
switch(getc(fp)) switch(dbfile_getc(fp))
{ {
case '@': case '@':
pers_arr[ i ] = NULL; pers_arr[ i ] = NULL;
...@@ -1273,13 +1274,13 @@ init_cache(void) ...@@ -1273,13 +1274,13 @@ init_cache(void)
read_text_num = 1; read_text_num = 1;
if ( vflag ) if ( vflag )
kom_log("Reading %ld texts, starting at pos %lu.\n", kom_log("Reading %ld texts, starting at pos %ld.\n",
next_text_num-1, (unsigned long)ftell(fp)); next_text_num-1, dbfile_ftell(fp));
for ( i = 1; i < next_text_num; i++ ) /* TEXT_STATS */ for ( i = 1; i < next_text_num; i++ ) /* TEXT_STATS */
{ {
fskipwhite(fp); fskipwhite(fp);
switch(getc(fp)) switch(dbfile_getc(fp))
{ {
case '@': case '@':
text_arr[ i ] = NULL; text_arr[ i ] = NULL;
...@@ -1303,8 +1304,8 @@ init_cache(void) ...@@ -1303,8 +1304,8 @@ init_cache(void)
} }
} /* if (datafile_version == 0) */ } /* if (datafile_version == 0) */
kom_log("Read %d confs/persons and %ld texts, eof at %lu\n", kom_log("Read %d confs/persons and %ld texts, eof at %ld\n",
next_free_num-1, next_text_num-1, (unsigned long)ftell(fp)); next_free_num-1, next_text_num-1, dbfile_ftell(fp));
if (read_text_num == 0 || read_conf_num == 0) if (read_text_num == 0 || read_conf_num == 0)
restart_kom("init_cache(): Failed to read next_conf or text num.\n"); restart_kom("init_cache(): Failed to read next_conf or text num.\n");
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "s-string.h" #include "s-string.h"
#include "kom-types.h" #include "kom-types.h"
#include "ram-io.h"
#include "local-to-global.h" #include "local-to-global.h"
#include "log.h" #include "log.h"
#include "ram-parse.h" #include "ram-parse.h"
...@@ -1182,10 +1183,10 @@ l2g_read(FILE *fp, Local_to_global *l2g) ...@@ -1182,10 +1183,10 @@ l2g_read(FILE *fp, Local_to_global *l2g)
/* Read past the start marker */ /* Read past the start marker */
fskipwhite(fp); 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", kom_log("l2g_read() failed to find ``['' marker at pos %ld.\n",
(unsigned long) ftell(fp)); dbfile_ftell(fp));
return FAILURE; return FAILURE;
} }
...@@ -1194,10 +1195,10 @@ l2g_read(FILE *fp, Local_to_global *l2g) ...@@ -1194,10 +1195,10 @@ l2g_read(FILE *fp, Local_to_global *l2g)
/* Read numbers until the EOL (end-of-l2g :-) ) marker is read. */ /* Read numbers until the EOL (end-of-l2g :-) ) marker is read. */
while (1) while (1)
{ {
if ((c = getc(fp)) == EOF) if ((c = dbfile_getc(fp)) == EOF)
{ {
kom_log("l2g_read(): unexpected EOF at pos %lu.\n", kom_log("l2g_read(): unexpected EOF at pos %ld.\n",
(unsigned long) ftell(fp)); dbfile_ftell(fp));
return FAILURE; return FAILURE;
} }
...@@ -1207,16 +1208,16 @@ l2g_read(FILE *fp, Local_to_global *l2g) ...@@ -1207,16 +1208,16 @@ l2g_read(FILE *fp, Local_to_global *l2g)
lno = fparse_long(fp); lno = fparse_long(fp);
if (lno == 0) if (lno == 0)
{ {
kom_log("l2g_read(): got local number 0 at pos %lu.\n", kom_log("l2g_read(): got local number 0 at pos %ld.\n",
(unsigned long) ftell(fp)); dbfile_ftell(fp));
return FAILURE; return FAILURE;
} }
break; break;
case ',': case ',':
if (lno == 0) if (lno == 0)
{ {
kom_log("l2g_read(): missing local number at pos %lu.\n", kom_log("l2g_read(): missing local number at pos %ld.\n",
(unsigned long)ftell(fp)); dbfile_ftell(fp));
return FAILURE; return FAILURE;
} }
++lno; ++lno;
...@@ -1224,8 +1225,8 @@ l2g_read(FILE *fp, Local_to_global *l2g) ...@@ -1224,8 +1225,8 @@ l2g_read(FILE *fp, Local_to_global *l2g)
case ':': case ':':
if (lno == 0) if (lno == 0)
{ {
kom_log("l2g_read(): missing local number at pos %lu.\n", kom_log("l2g_read(): missing local number at pos %ld.\n",
(unsigned long)ftell(fp)); dbfile_ftell(fp));
return FAILURE; return FAILURE;
} }
tno = fparse_long(fp); tno = fparse_long(fp);
...@@ -1236,8 +1237,8 @@ l2g_read(FILE *fp, Local_to_global *l2g) ...@@ -1236,8 +1237,8 @@ l2g_read(FILE *fp, Local_to_global *l2g)
l2g->first_unused = first_unused; l2g->first_unused = first_unused;
return OK; return OK;
default: default:
kom_log("l2g_read(): unexpected character ``%c'' at pos %lu.\n", kom_log("l2g_read(): unexpected character ``%c'' at pos %ld.\n",
c, (unsigned long) ftell(fp)); c, dbfile_ftell(fp));
return FAILURE; return FAILURE;
} }
} }
......