Commit e208f3a5 authored by Per Cederqvist's avatar Per Cederqvist

(copy_file): Use a static buffer instead of allocating and releasing a

	buffer each time this function is called.
parent ccf697f9
/*
* $Id: simple-cache.c,v 0.84 1999/07/05 06:57:06 ceder Exp $
* $Id: simple-cache.c,v 0.85 1999/07/05 21:24:15 ceder Exp $
* Copyright (C) 1991-1999 Lysator Academic Computer Association.
*
* This file is part of the LysKOM server.
......@@ -40,7 +40,7 @@
#endif
static const char *
rcsid = "$Id: simple-cache.c,v 0.84 1999/07/05 06:57:06 ceder Exp $";
rcsid = "$Id: simple-cache.c,v 0.85 1999/07/05 21:24:15 ceder Exp $";
#include "rcs.h"
USE(rcsid);
......@@ -1796,93 +1796,115 @@ copy_file(FILE *from,
long len,
long no)
{
char *buf;
static char buf[BUFSIZ];
long result;
long num;
long new_num;
long num_ix;
long chunk_len;
long orig_len = len;
int first_chunk = 1;
if (len < 3)
{
restart_kom("copy_file: insane len %ld\n", len);
}
buf = smalloc(len+1);
/* Include the terminating newline in the length. */
++len;
if ( fseek(from, from_pos, SEEK_SET) == -1 )
{
sync_state = sync_error;
restart_kom("sync: copy_file(): fseek failed.\n");
sfree(buf);
restart_kom("sync: copy_file(): src fseek failed.\n");
return;
}
if ( (result = fread(buf, 1, len+1, from)) != len+1 )
{
restart_kom("%s.\nfrom_pos = %ld, len = %ld, result = %ld\n",
"sync: copy_file(): fread failed",
from_pos, len, result);
if ( fseek(to, 0, SEEK_END) == -1 )
{
sync_state = sync_error;
sfree(buf);
kom_log("sync: copy_file(): dst fseek failed.\n");
return;
}
if (buf[len] != '\n')
{
restart_kom("Failed to find a newline at %ld + %ld - 1\n",
from_pos, len);
}
if (buf[0] != 'T' && buf[0] != 'C' && buf[0] != 'P')
{
restart_kom("Found char %d at pos %ld; expected T, C or P\n",
buf[0], from_pos);
}
if (buf[1] != ' ')
{
restart_kom("Expected space after T, C or P but got %d at %ld\n",
buf[1], from_pos);
}
num = 0;
for (num_ix = 2; num_ix < len && buf[num_ix] >= '0' && buf[num_ix] <= '9';
++num_ix)
first_chunk = 1;
while (len > 0)
{
new_num = 10 * num + buf[num_ix] - '0';
if (new_num / 10 != num)
chunk_len = len;
if (chunk_len > BUFSIZ)
chunk_len = BUFSIZ;
if ((result = fread(buf, 1, chunk_len, from)) != chunk_len)
{
restart_kom("copy_file: number overflow at %ld\n", from_pos);
restart_kom("%s.\nfrom_pos = %ld, len = %ld, result = %ld\n",
"sync: copy_file(): fread failed",
from_pos, len, result);
sync_state = sync_error;
return;
}
num = new_num;
}
if (num != no)
{
restart_kom("copy_file: expected %ld, got %ld; no sanity at %ld\n",
no, num, from_pos);
}
if (num_ix >= len)
{
restart_kom("copy_file: to little data at %ld\n", from_pos);
}
if (buf[num_ix] != ' ')
{
restart_kom("copy_file: expected space after number %ld at %ld; got %d\n",
num, from_pos, buf[num_ix]);
}
if ( fseek(to, 0, SEEK_END) == -1 )
{
sync_state = sync_error;
kom_log("sync: copy_file(): second fseek failed.\n");
sfree(buf);
return;
}
if (first_chunk)
{
/* Check the start of the first chunk. */
if (buf[0] != 'T' && buf[0] != 'C' && buf[0] != 'P')
{
restart_kom("Found char %d at pos %ld; expected T, C or P\n",
buf[0], from_pos);
}
if (buf[1] != ' ')
{
restart_kom("Expected space after T, C or P but got "
"%d at %ld\n", buf[1], from_pos);
}
num = 0;
for (num_ix = 2;
num_ix < chunk_len
&& buf[num_ix] >= '0' && buf[num_ix] <= '9';
++num_ix)
{
new_num = 10 * num + buf[num_ix] - '0';
if (new_num / 10 != num)
{
restart_kom("copy_file: number overflow at %ld\n",
from_pos);
}
num = new_num;
}
if (num != no)
{
restart_kom("copy_file: expected %ld, got %ld; "
"no sanity at %ld\n", no, num, from_pos);
}
if (num_ix >= chunk_len)
{
restart_kom("copy_file: to little data at %ld\n", from_pos);
}
if (buf[num_ix] != ' ')
{
restart_kom("copy_file: expected space after number "
"%ld at %ld; got %d\n",
num, from_pos, buf[num_ix]);
}
first_chunk = 0;
}
if ( fwrite(buf, 1, len, to) != (size_t)len )
{
sync_state = sync_error;
kom_log("sync: copy_file(): fwrite failed.\n");
sfree(buf);
return;
}
/* The last chunk should end with a newline. */
if (len == result && buf[len-1] != '\n')
{
restart_kom("Failed to find a newline at %ld + %ld - 1\n",
from_pos, orig_len);
}
sfree(buf);
/* Write this chunk. */
if (fwrite(buf, 1, chunk_len, to) != (size_t)chunk_len)
{
sync_state = sync_error;
kom_log("sync: copy_file(): fwrite failed.\n");
return;
}
len -= chunk_len;
}
}
static void
......
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