From 7ca0d146ecaf2f99781653d1203bd3db7afc85ba Mon Sep 17 00:00:00 2001 From: Timo Teräs Date: Fri, 10 Jan 2020 11:02:48 +0200 Subject: istream: add buffering capability Convert all implementations to do buffering. This is in preparation to remove bstream interface as redundant. istream_read() will return full reads unless end-of-file. The backends can return short reads to optimize buffering or due to other reasons like boundary change for gz. --- src/url.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'src/url.c') diff --git a/src/url.c b/src/url.c index cacca81..d233e06 100644 --- a/src/url.c +++ b/src/url.c @@ -80,18 +80,11 @@ static void fetch_get_meta(struct apk_istream *is, struct apk_file_meta *meta) static ssize_t fetch_read(struct apk_istream *is, void *ptr, size_t size) { struct apk_fetch_istream *fis = container_of(is, struct apk_fetch_istream, is); - ssize_t i = 0, r; + ssize_t r; - if (ptr == NULL) return apk_istream_skip(&fis->is, size); - - while (i < size) { - r = fetchIO_read(fis->fetchIO, ptr + i, size - i); - if (r < 0) return -EIO; - if (r == 0) break; - i += r; - } - - return i; + r = fetchIO_read(fis->fetchIO, ptr, size); + if (r < 0) return -EIO; + return r; } static void fetch_close(struct apk_istream *is) @@ -120,7 +113,7 @@ static struct apk_istream *apk_istream_fetch(const char *url, time_t since) rc = -EAPKBADURL; goto err; } - fis = malloc(sizeof(*fis)); + fis = malloc(sizeof *fis + apk_io_bufsize); if (!fis) { rc = -ENOMEM; goto err; @@ -135,6 +128,8 @@ static struct apk_istream *apk_istream_fetch(const char *url, time_t since) *fis = (struct apk_fetch_istream) { .is.ops = &fetch_istream_ops, + .is.buf = (uint8_t*)(fis+1), + .is.buf_size = apk_io_bufsize, .fetchIO = io, .urlstat = fis->urlstat, }; -- cgit v1.2.3-70-g09d2