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:
parent
2f72303e6c
commit
3abd3e0bcc
|
@ -24,9 +24,21 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/types.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
|
off_t
|
||||||
lseek (int filedes, off_t offset, int whence)
|
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);
|
size_t skip = __buffered_read_clear (filedes);
|
||||||
if (whence == SEEK_CUR)
|
if (whence == SEEK_CUR)
|
||||||
offset -= skip;
|
offset -= skip;
|
||||||
|
|
|
@ -22,7 +22,13 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.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
|
||||||
|
#define __read_buffer_max 100
|
||||||
|
#endif
|
||||||
|
|
||||||
struct __read_buffer
|
struct __read_buffer
|
||||||
{
|
{
|
||||||
|
@ -36,7 +42,21 @@ void
|
||||||
__buffered_read_init (int filedes)
|
__buffered_read_init (int filedes)
|
||||||
{
|
{
|
||||||
if (!__read_cache)
|
if (!__read_cache)
|
||||||
__read_cache = (struct __read_buffer *) malloc (sizeof (struct __read_buffer) * __FILEDES_MAX);
|
{
|
||||||
|
__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
|
size_t
|
||||||
|
@ -55,23 +75,33 @@ __buffered_read (int filedes, void *buffer, size_t size)
|
||||||
__buffered_read_init (filedes);
|
__buffered_read_init (filedes);
|
||||||
struct __read_buffer *cache = &__read_cache[filedes];
|
struct __read_buffer *cache = &__read_cache[filedes];
|
||||||
char *p = buffer;
|
char *p = buffer;
|
||||||
if (!cache->size && size > __READ_BUFFER_MAX)
|
if (!cache->size && size > __read_buffer_max)
|
||||||
return _read (filedes, buffer, size);
|
return _read (filedes, buffer, size);
|
||||||
while (cache->size > 0 && todo)
|
while (cache->size > 0 && todo)
|
||||||
{
|
{
|
||||||
todo--;
|
todo--;
|
||||||
*p++ = cache->string[__READ_BUFFER_MAX - cache->size--];
|
*p++ = cache->string[__read_buffer_max - cache->size--];
|
||||||
}
|
}
|
||||||
if (todo)
|
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)
|
if (bytes < 0)
|
||||||
return -1;
|
return -1;
|
||||||
if (bytes)
|
if (bytes)
|
||||||
{
|
{
|
||||||
cache->size = bytes;
|
cache->size = bytes;
|
||||||
if (bytes < __READ_BUFFER_MAX)
|
if (bytes < __read_buffer_max)
|
||||||
memmove (cache->string + __READ_BUFFER_MAX - bytes, cache->string, bytes);
|
memmove (cache->string + __read_buffer_max - bytes, cache->string, bytes);
|
||||||
return size - todo + __buffered_read (filedes, p, todo);
|
return size - todo + __buffered_read (filedes, p, todo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,5 +42,5 @@ open (char const *file_name, int flags, ...)
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return _open2(file_name, flags);
|
return _open2 (file_name, flags);
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,7 +45,7 @@ fopen (char const *file_name, char const *opentype)
|
||||||
int flags = O_RDWR;
|
int flags = O_RDWR;
|
||||||
if (opentype[0] == 'a')
|
if (opentype[0] == 'a')
|
||||||
flags |= O_APPEND;
|
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+"))
|
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;
|
fd = 0;
|
||||||
return (FILE *) (long) fd;
|
return (FILE *) (long) fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef open
|
|
||||||
|
|
Loading…
Reference in a new issue