Commit 39b34b6a authored by Per Cederqvist's avatar Per Cederqvist

Let dbfile_open_write() write the file header. Extract the code

that changes the state to dbfile_change_magic().  Reduce code
duplication by teaching foutput_header() to handle old file
formats.
* src/server/simple-cache.c (pre_sync): Adjusted to new API of
dbfile_open_write(), including removing calls to foutput_header().
(save_one_text): Extracted parts of the functionality to
dbfile_change_magic().
* src/server/ram-output.c (foutput_header): Moved support for file
formats 0, 1 and 2 here from sync_output_header().
* src/server/ram-io.c, src/server/ram-io.h
(dbfile_open_write): New argument: magic.  Write a file header
with the specified state.
(dbfile_change_magic): New function, extracted from
cache_sync_all() in dbck-cache.c and save_one_text().
* src/server/dbck-cache.c (sync_output_header): Removed.
The ability to emit old-style headers moved to foutput_header().
(cache_sync_all): Adjusted to new API of dbfile_open_write,
including removing calls to sync_output_header().  Use
dbfile_change_magic() instead of rewind() and
sync_output_header().
* src/server/Makefile.am (libcheck_a_SOURCES): Added
server-time.c.
parent c1eb1fc5
2007-07-03 Per Cederqvist <ceder@lysator.liu.se>
Let dbfile_open_write() write the file header. Extract the code
that changes the state to dbfile_change_magic(). Reduce code
duplication by teaching foutput_header() to handle old file
formats.
* src/server/simple-cache.c (pre_sync): Adjusted to new API of
dbfile_open_write(), including removing calls to foutput_header().
(save_one_text): Extracted parts of the functionality to
dbfile_change_magic().
* src/server/ram-output.c (foutput_header): Moved support for file
formats 0, 1 and 2 here from sync_output_header().
* src/server/ram-io.c, src/server/ram-io.h
(dbfile_open_write): New argument: magic. Write a file header
with the specified state.
(dbfile_change_magic): New function, extracted from
cache_sync_all() in dbck-cache.c and save_one_text().
* src/server/dbck-cache.c (sync_output_header): Removed.
The ability to emit old-style headers moved to foutput_header().
(cache_sync_all): Adjusted to new API of dbfile_open_write,
including removing calls to sync_output_header(). Use
dbfile_change_magic() instead of rewind() and
sync_output_header().
* src/server/Makefile.am (libcheck_a_SOURCES): Added
server-time.c.
2007-07-01 Per Cederqvist <ceder@lysator.liu.se>
Use a modern autoconf.
......
......@@ -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 ram-io.c
misc-types.c ram-io.c server-time.c
lyskomd_SOURCES = $(DISKOBJS) $(GENOBJS) \
aux-item-def-parse.y \
......
......@@ -534,28 +534,6 @@ is_clean(const char *fn)
}
}
static void
sync_output_header(struct dbfile* fp, const char *state)
{
switch (fp->format)
{
case 0:
fprintf(fp->fp, "%s\n", state);
break;
case 1:
fprintf(fp->fp, "%s:%05d\n", state, fp->format);
break;
case 2:
case 3:
foutput_header(fp, state, 1);
break;
default:
restart_kom("sync_output_header(): Unknown output format %d",
fp->format);
}
}
#ifdef TIME_SYNC
static long
......@@ -603,7 +581,7 @@ cache_sync_all(void)
else
kom_log("cache_sync_all: datafile not clean. No backup taken.\n");
if ((fp = dbfile_open_write(param.datafile_name)) == NULL)
if ((fp = dbfile_open_write(param.datafile_name, "DIRTY")) == NULL)
{
kom_log("WARNING: cache_sync_all: can't open file to save in.\n");
return;
......@@ -616,13 +594,11 @@ cache_sync_all(void)
switch (fp->format)
{
case 0:
sync_output_header(fp, "DIRTY");
fprintf(fp->fp, "%d\n", next_free_num); /* NEXT_FREE_NUM */
break;
case 1:
case 2:
case 3:
sync_output_header(fp, "DIRTY");
fprintf(fp->fp, "#C %d\n", next_free_num);
fprintf(fp->fp, "#T %ld\n", next_text_num);
fprintf(fp->fp, "I");
......@@ -780,8 +756,7 @@ cache_sync_all(void)
time(&atm);
#endif
rewind(fp->fp);
sync_output_header(fp, "CLEAN");
dbfile_change_magic(fp, "CLEAN");
dbfile_delete(fp);
fp = NULL;
......
......@@ -36,6 +36,7 @@
#include "kom-types.h"
#include "ram-io.h"
#include "ram-parse.h"
#include "ram-output.h"
#include "lyskomd.h"
#include "log.h"
......@@ -136,7 +137,8 @@ dbfile_open_read(const char *filename)
}
struct dbfile *
dbfile_open_write(const char *filename)
dbfile_open_write(const char *filename,
const char *magic)
{
struct dbfile *res = dbfile_new();
if (res == NULL)
......@@ -150,10 +152,48 @@ dbfile_open_write(const char *filename)
return NULL;
}
foutput_header(res, magic, 1);
++nr_dbfile_files;
return res;
}
Success
dbfile_change_magic(struct dbfile *fp,
const char *magic)
{
rewind(fp->fp);
if (ferror(fp->fp) != 0)
{
kom_log("dbfile_change_magic: rewind failed.\n");
return FAILURE;
}
foutput_header(fp, magic, 0);
if (ferror(fp->fp) != 0)
{
kom_log("dbfile_change_magic: writing new header %s failed.\n",
magic);
return FAILURE;
}
if (fflush(fp->fp) != 0)
{
kom_log("dbfile_change_magic(): fflush failed.\n");
return FAILURE;
}
if (ferror(fp->fp) != 0)
{
kom_log("dbfile_change_magic(): ferror after fflush failed.\n");
return FAILURE;
}
return OK;
}
void
dump_dbfile_stats(FILE *fp)
{
......
......@@ -40,9 +40,19 @@ int dbfile_delete(struct dbfile *obj);
set_output_format()). */
void set_output_format(int new_format);
/* Open the file for writing. */
/* Open the file for writing, and write a header that contains the
specified magic marker. The magic string must be 5 characters
long. */
extern struct dbfile *
dbfile_open_write(const char *filename);
dbfile_open_write(const char *filename,
const char *magic);
/* Change the magic cookie in the file. This is typically done to
mark the file as clean once it is completely written. The magic
string must be 5 characters long. */
Success
dbfile_change_magic(struct dbfile *fp,
const char *magic);
/* Open the file for reading. The file format version will be read
from the file. */
......
......@@ -41,6 +41,7 @@
#include <stdio.h>
#include "timewrap.h"
#include <sys/types.h>
#include <string.h>
#include "s-string.h"
#include "kom-types.h"
......@@ -74,11 +75,29 @@ static void foutput_array_end(struct dbfile *fp);
static void foutput_array_nodata(struct dbfile *fp);
void
foutput_header(struct dbfile* fp, const char *state, int include_timestamp)
foutput_header(struct dbfile* fp, const char *magic, int include_timestamp)
{
fprintf(fp->fp, "%s:%05d\n", state, fp->format); /* DIRTY-FLAG and VERSION*/
if (include_timestamp)
fprintf(fp->fp, "%020lu\n", (unsigned long)current_time.tv_sec);
if (strlen(magic) != 5)
restart_kom("Bad magic %s: not 5 chars long.\n", magic);
switch (fp->format)
{
case 0:
fprintf(fp->fp, "%s\n", magic);
break;
case 1:
fprintf(fp->fp, "%s:%05d\n", magic, fp->format);
break;
case 2:
case 3:
fprintf(fp->fp, "%s:%05d\n", magic, fp->format);
if (include_timestamp)
fprintf(fp->fp, "%020lu\n", (unsigned long)current_time.tv_sec);
break;
default:
restart_kom("foutput_header(): Unknown output format %d",
fp->format);
}
}
static void
......
......@@ -1909,7 +1909,7 @@ pre_sync(void)
dbfile_delete(file_b);
}
if ((file_b = dbfile_open_write(param.datafile_name)) == NULL)
if ((file_b = dbfile_open_write(param.datafile_name, "DIRTY")) == NULL)
{
kom_log("WARNING: pre_sync: can't open file %s to save in: %s.\n",
param.datafile_name, strerror(errno));
......@@ -1929,10 +1929,7 @@ pre_sync(void)
return;
}
#endif
foutput_header(file_b, "DIRTY", 1);
fprintf(file_b->fp, "#C %d\n", highest_conf_no);
fprintf(file_b->fp, "#T %ld\n", highest_text_no);
fprintf(file_b->fp, "I");
......@@ -2406,35 +2403,8 @@ save_one_text(void)
return;
}
rewind(file_b->fp);
if ( ferror(file_b->fp) != 0 )
{
kom_log ("save_one_text(): rewind failed.\n");
sync_state = sync_error;
return;
}
foutput_header(file_b, "CLEAN", 0);
if ( ferror(file_b->fp) != 0 )
{
kom_log ("save_one_text(): Set state to CLEAN failed.\n");
sync_state = sync_error;
return;
}
if (fflush(file_b->fp) != 0)
{
kom_log ("save_one_text(): fflush failed.\n");
sync_state = sync_error;
return;
}
if ( ferror(file_b->fp) != 0 )
if (dbfile_change_magic(file_b, "CLEAN") != OK)
{
kom_log ("save_one_text(): ferror after fflush failed.\n");
sync_state = sync_error;
return;
}
......
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