Commit 5c3e55d9 authored by Per Cederqvist's avatar Per Cederqvist
Browse files

(highest_conf_no): Explicitly initialize to 0.

(highest_text_no): Ditto.
(write_number_file): New static function.
(read_number_file): Ditto.
(cached_create_conf): Call write_number_file, and return
	KOM_TEMPFAIL if it fails.
(cached_create_text): Ditto.
(init_cache): Don't set highest_conf_no and highest_text_no.  They
	are only used during the save phase.  Call read_number_file.
parent 3e3483fa
/*
* $Id: simple-cache.c,v 0.99 2002/10/31 09:31:37 ceder Exp $
* $Id: simple-cache.c,v 0.100 2002/11/04 23:20:00 ceder Exp $
* Copyright (C) 1991-2002 Lysator Academic Computer Association.
*
* This file is part of the LysKOM server.
......@@ -161,8 +161,8 @@ static enum {
static unsigned long sync_next;
#endif
static Conf_no highest_conf_no;
static Text_no highest_text_no;
static Conf_no highest_conf_no = 0;
static Text_no highest_text_no = 0;
BUGDECL;
......@@ -507,6 +507,100 @@ mark_text_as_changed( Text_no text_no )
}
/*
* Store and retrieve the highest number used.
*/
static int
write_number_file(void)
{
FILE *fp;
if ((fp = i_fopen(param.numberfile_tmp_name, "w")) == NULL)
{
kom_log("opening %s: %s\n", param.numberfile_tmp_name,
strerror(errno));
return -1;
}
fprintf(fp, "Text_no: %lu Conf_no: %lu End.\n",
(unsigned long)next_text_num,
(unsigned long)next_free_num);
if (fflush(fp) != 0)
{
kom_log("fflush to %s failed\n", param.numberfile_tmp_name);
i_fclose(fp);
return -1;
}
if (ferror(fp) != 0)
{
kom_log("fprintf to %s failed\n", param.numberfile_tmp_name);
i_fclose(fp);
return -1;
}
if (i_fclose(fp) != 0)
{
kom_log("fclose %s failed\n", param.numberfile_tmp_name);
return -1;
}
if (i_rename(param.numberfile_tmp_name, param.numberfile_name) != 0)
{
kom_log("failed to rename %s to %s: %s\n",
param.numberfile_tmp_name, param.numberfile_name,
strerror(errno));
return -1;
}
return 0;
}
static void
read_number_file(void)
{
FILE *fp;
unsigned long txt = 0;
unsigned long cno = 0;
Text_no text_no;
Conf_no conf_no;
if ((fp = i_fopen(param.numberfile_name, "r")) == NULL)
{
if (errno == ENOENT)
kom_log("WARN: %s: No such file\n", param.numberfile_name);
else
restart_kom("opening %s: %s\n", param.numberfile_name,
strerror(errno));
return;
}
if (fscanf(fp, "Text_no: %lu Conf_no: %lu", &txt, &cno) != 2
|| getc(fp) != ' ' || getc(fp) != 'E' || getc(fp) != 'n'
|| getc(fp) != 'd' || getc(fp) != '.')
{
kom_log("WARN: %s: broken file (ignored)\n", param.numberfile_name);
i_fclose(fp);
return;
}
i_fclose(fp);
text_no = txt;
conf_no = cno;
if (text_no > next_text_num)
{
kom_log("WARN: Texts %lu - %lu were lost.\n",
(unsigned long)next_text_num,
(unsigned long)text_no - 1);
next_text_num = text_no;
}
if (conf_no > next_free_num)
{
kom_log("WARN: Confs %lu - %lu were lost.\n",
(unsigned long)next_free_num,
(unsigned long)conf_no - 1);
next_free_num = conf_no;
}
}
/*
* Person-related calls
......@@ -670,6 +764,14 @@ cached_create_conf (String name)
conf_no = next_free_num++;
if (write_number_file() < 0)
{
next_free_num--;
err_stat = 0;
kom_errno = KOM_TEMPFAIL;
return 0;
}
create_cache_node (conf_mcb, conf_no);
node = get_conf_node (conf_no);
......@@ -1004,6 +1106,14 @@ cached_create_text(const String message)
return 0;
}
if (write_number_file() < 0)
{
next_text_num--;
err_stat = 0;
kom_errno = KOM_TEMPFAIL;
return 0;
}
if (fseek(text_file, 0, SEEK_END) != 0)
{
kom_log("ERROR: cannot seek to end of text_file: %s\n", strerror(errno));
......@@ -2583,13 +2693,11 @@ init_cache(void)
switch(getc(file_a))
{
case 'C':
highest_conf_no = fparse_long(file_a);
next_free_num = highest_conf_no;
next_free_num = fparse_long(file_a);
read_conf_no = TRUE;
break;
case 'T':
highest_text_no = fparse_long(file_a);
next_text_num = highest_text_no;
next_text_num = fparse_long(file_a);
read_text_no = TRUE;
break;
default:
......@@ -2739,6 +2847,8 @@ init_cache(void)
kom_log("Read %d confs/persons and %ld texts\n",
next_free_num, next_text_num);
read_number_file();
return OK;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment