Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Per Cederqvist
lyskom-server-ceder-1616-generations-topgit
Commits
e208f3a5
Commit
e208f3a5
authored
Jul 05, 1999
by
Per Cederqvist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(copy_file): Use a static buffer instead of allocating and releasing a
buffer each time this function is called.
parent
ccf697f9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
87 additions
and
65 deletions
+87
-65
src/server/simple-cache.c
src/server/simple-cache.c
+87
-65
No files found.
src/server/simple-cache.c
View file @
e208f3a5
/*
* $Id: simple-cache.c,v 0.8
4
1999/07/05
06:57:06
ceder Exp $
* $Id: simple-cache.c,v 0.8
5
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.8
4
1999/07/05
06:57:06
ceder Exp $"
;
rcsid
=
"$Id: simple-cache.c,v 0.8
5
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.
\n
from_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.
\n
from_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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment