mescc: Mes C Library: lseek: Be careful clearing read buffer.

* lib/linux/lseek.c (_lseek): New function.
* lib/linux/lseek.c (lseek): Use it to check if we should reset read
buffer.
* lib/mes/__buffered_read.c (__read_buffer_max): New variable.
(__buffered_read_init): Add environment override: MES_READ_BUFFER.
(__buffered_read): Use it.
This commit is contained in:
Jan Nieuwenhuizen 2019-12-17 22:44:24 +01:00
parent 2f72303e6c
commit 3abd3e0bcc
No known key found for this signature in database
GPG key ID: F3C1A0D9C1D65273
4 changed files with 50 additions and 10 deletions

View file

@ -24,9 +24,21 @@
#include <stdio.h>
#include <sys/types.h>
#if !__MESC__ /* FIXME: We want bin/mes-mescc's x86-linux sha256sum to stay the same. */
off_t
_lseek (int filedes, off_t offset, int whence)
{
return _sys_call3 (SYS_lseek, (int) filedes, (long) offset, (int) whence);
}
#endif
off_t
lseek (int filedes, off_t offset, int whence)
{
#if !__MESC__ /* FIXME: We want bin/mes-mescc's x86-linux sha256sum to stay the same. */
if (_lseek (filedes, 0, SEEK_CUR) == -1)
return -1;
#endif
size_t skip = __buffered_read_clear (filedes);
if (whence == SEEK_CUR)
offset -= skip;

View file

@ -22,7 +22,13 @@
#include <stdlib.h>
#include <string.h>
#if !__MESC__
#define __READ_BUFFER_MAX 128
int __read_buffer_max;
#else /* FIXME: We want bin/mes-mescc's x86-linux sha256sum to stay the same. */
#define __READ_BUFFER_MAX 100
#define __read_buffer_max 100
#endif
struct __read_buffer
{
@ -36,7 +42,21 @@ void
__buffered_read_init (int filedes)
{
if (!__read_cache)
{
__read_cache = (struct __read_buffer *) malloc (sizeof (struct __read_buffer) * __FILEDES_MAX);
#if !__MESC__
__read_buffer_max = __READ_BUFFER_MAX;
char *p = getenv ("MES_READ_BUFFER");
if (p)
{
__read_buffer_max = atoi (p);
if (__read_buffer_max < 0)
__read_buffer_max = 0;
if (__read_buffer_max > __READ_BUFFER_MAX)
__read_buffer_max = __READ_BUFFER_MAX;
}
#endif
}
}
size_t
@ -55,23 +75,33 @@ __buffered_read (int filedes, void *buffer, size_t size)
__buffered_read_init (filedes);
struct __read_buffer *cache = &__read_cache[filedes];
char *p = buffer;
if (!cache->size && size > __READ_BUFFER_MAX)
if (!cache->size && size > __read_buffer_max)
return _read (filedes, buffer, size);
while (cache->size > 0 && todo)
{
todo--;
*p++ = cache->string[__READ_BUFFER_MAX - cache->size--];
*p++ = cache->string[__read_buffer_max - cache->size--];
}
if (todo)
{
ssize_t bytes = _read (filedes, cache->string, __READ_BUFFER_MAX);
#if !__MESC__
if (todo > __read_buffer_max)
return size - todo + _read (filedes, p, todo);
if (__mes_debug () > 4)
{
eputs ("__buffered_read: ");
eputs (itoa (__read_buffer_max));
eputs ("\n");
}
#endif
ssize_t bytes = _read (filedes, cache->string, __read_buffer_max);
if (bytes < 0)
return -1;
if (bytes)
{
cache->size = bytes;
if (bytes < __READ_BUFFER_MAX)
memmove (cache->string + __READ_BUFFER_MAX - bytes, cache->string, bytes);
if (bytes < __read_buffer_max)
memmove (cache->string + __read_buffer_max - bytes, cache->string, bytes);
return size - todo + __buffered_read (filedes, p, todo);
}
}

View file

@ -42,5 +42,5 @@ open (char const *file_name, int flags, ...)
return r;
}
else
return _open2(file_name, flags);
return _open2 (file_name, flags);
}

View file

@ -45,7 +45,7 @@ fopen (char const *file_name, char const *opentype)
int flags = O_RDWR;
if (opentype[0] == 'a')
flags |= O_APPEND;
fd = open (file_name, flags, mode);
fd = _open3 (file_name, flags, mode);
}
else if (opentype[0] == 'w' || opentype[0] == 'a' || !strcmp (opentype, "r+"))
{
@ -72,5 +72,3 @@ fopen (char const *file_name, char const *opentype)
fd = 0;
return (FILE *) (long) fd;
}
#undef open