aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-10 17:51:03 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-10 17:56:27 +0200
commitad1691aa33cfc1b1558ce10e93552d0eb1cdcd63 (patch)
tree8c53c5cd0202ed638e9c45d1eaece8e486f17a0c
parent605fd3e3a2656438dc0283699fc4a4b44b8b71db (diff)
libio: add desired read size to istream_get_buffered_data
This properly maps to all of our use cases and makes istream_precache obsolete. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r--include/io/istream.h25
-rw-r--r--lib/io/src/get_line.c2
-rw-r--r--lib/io/src/istream.c6
-rw-r--r--lib/io/src/xfrm/istream.c7
-rw-r--r--lib/io/test/istream_mem.c26
-rw-r--r--lib/tar/src/iterator.c9
6 files changed, 26 insertions, 49 deletions
diff --git a/include/io/istream.h b/include/io/istream.h
index f091aa1..8edd61d 100644
--- a/include/io/istream.h
+++ b/include/io/istream.h
@@ -82,24 +82,6 @@ SQFS_INTERNAL int istream_get_line(istream_t *strm, char **out,
SQFS_INTERNAL sqfs_s32 istream_read(istream_t *strm, void *data, size_t size);
/**
- * @brief Adjust and refill the internal buffer of an input stream
- *
- * @memberof istream_t
- *
- * This function resets the buffer offset of an input stream (moving any unread
- * data up front if it has to) and calls an internal callback of the input
- * stream to fill the rest of the buffer to the extent possible.
- *
- * @param strm A pointer to an input stream.
- *
- * @return 0 on success, -1 on failure.
- */
-SQFS_INLINE int istream_precache(istream_t *strm)
-{
- return strm->precache(strm);
-}
-
-/**
* @brief Get the underlying filename of an input stream.
*
* @memberof istream_t
@@ -129,14 +111,17 @@ SQFS_INLINE const char *istream_get_filename(istream_t *strm)
* @param strm A pointer to an istream_t implementation.
* @param out Returns a pointer into an internal buffer on success.
* @param size Returns the number of bytes available in the buffer.
+ * @param want A number of bytes that the reader would like to have. If there is
+ * less than this available, the implementation can choose to do a
+ * blocking precache.
*
* @return Zero on success, a negative error code on failure,
* a postive number on EOF.
*/
SQFS_INLINE int istream_get_buffered_data(istream_t *strm, const sqfs_u8 **out,
- size_t *size)
+ size_t *size, size_t want)
{
- if (strm->buffer_used == 0) {
+ if (strm->buffer_used == 0 || strm->buffer_used < want) {
int ret = strm->precache(strm);
if (ret)
return ret;
diff --git a/lib/io/src/get_line.c b/lib/io/src/get_line.c
index a939a6c..94ae9ce 100644
--- a/lib/io/src/get_line.c
+++ b/lib/io/src/get_line.c
@@ -50,7 +50,7 @@ int istream_get_line(istream_t *strm, char **out,
const sqfs_u8 *ptr;
int ret;
- ret = istream_get_buffered_data(strm, &ptr, &avail);
+ ret = istream_get_buffered_data(strm, &ptr, &avail, 0);
if (ret < 0)
goto fail_free;
if (ret > 0) {
diff --git a/lib/io/src/istream.c b/lib/io/src/istream.c
index 6171f2c..b4c709c 100644
--- a/lib/io/src/istream.c
+++ b/lib/io/src/istream.c
@@ -19,7 +19,7 @@ sqfs_s32 istream_read(istream_t *strm, void *data, size_t size)
size_t diff;
int ret;
- ret = istream_get_buffered_data(strm, &ptr, &diff);
+ ret = istream_get_buffered_data(strm, &ptr, &diff, size);
if (ret > 0)
break;
if (ret < 0)
@@ -48,7 +48,7 @@ int istream_skip(istream_t *strm, sqfs_u64 size)
size_t diff;
int ret;
- ret = istream_get_buffered_data(strm, &ptr, &diff);
+ ret = istream_get_buffered_data(strm, &ptr, &diff, size);
if (ret < 0)
return ret;
if (ret > 0) {
@@ -81,7 +81,7 @@ sqfs_s32 istream_splice(istream_t *in, ostream_t *out, sqfs_u32 size)
size_t diff;
int ret;
- ret = istream_get_buffered_data(in, &ptr, &diff);
+ ret = istream_get_buffered_data(in, &ptr, &diff, size);
if (ret < 0)
return ret;
if (ret > 0)
diff --git a/lib/io/src/xfrm/istream.c b/lib/io/src/xfrm/istream.c
index 2ada5db..8755cb6 100644
--- a/lib/io/src/xfrm/istream.c
+++ b/lib/io/src/xfrm/istream.c
@@ -37,11 +37,8 @@ static int xfrm_precache(istream_t *base)
const sqfs_u8 *ptr;
size_t avail;
- ret = istream_precache(xfrm->wrapped);
- if (ret != 0)
- return ret;
-
- ret = istream_get_buffered_data(xfrm->wrapped, &ptr, &avail);
+ ret = istream_get_buffered_data(xfrm->wrapped, &ptr, &avail,
+ sizeof(xfrm->uncompressed));
if (ret < 0)
return ret;
if (ret > 0) {
diff --git a/lib/io/test/istream_mem.c b/lib/io/test/istream_mem.c
index 632b1f5..f1849dd 100644
--- a/lib/io/test/istream_mem.c
+++ b/lib/io/test/istream_mem.c
@@ -32,8 +32,9 @@ static void init_buffer(void)
int main(int argc, char **argv)
{
+ size_t i, diff, size;
bool eat_all = true;
- size_t i, diff;
+ const sqfs_u8 *ptr;
istream_t *in;
int ret;
(void)argc; (void)argv;
@@ -45,33 +46,30 @@ int main(int argc, char **argv)
TEST_EQUAL_UI(((sqfs_object_t *)in)->refcount, 1);
TEST_STR_EQUAL(istream_get_filename(in), "memstream.txt");
- TEST_NOT_NULL(in->buffer);
- TEST_EQUAL_UI(in->buffer_used, 0);
for (i = 0; i < end2; i += diff) {
- ret = istream_precache(in);
+ ret = istream_get_buffered_data(in, &ptr, &size, 61);
TEST_EQUAL_I(ret, 0);
if ((end2 - i) >= 61) {
- TEST_NOT_NULL(in->buffer);
- TEST_EQUAL_UI(in->buffer_used, 61);
+ TEST_NOT_NULL(ptr);
+ TEST_EQUAL_UI(size, 61);
} else {
- TEST_NOT_NULL(in->buffer);
- TEST_EQUAL_UI(in->buffer_used, (end2 - i));
+ TEST_NOT_NULL(ptr);
+ TEST_EQUAL_UI(size, (end2 - i));
}
- for (size_t j = 0; j < in->buffer_used; ++j) {
- TEST_EQUAL_UI(in->buffer[j], byte_at_offset(i + j));
+ for (size_t j = 0; j < size; ++j) {
+ TEST_EQUAL_UI(ptr[j], byte_at_offset(i + j));
}
- diff = eat_all ? in->buffer_used : (in->buffer_used / 2);
+ diff = eat_all ? size : (size / 2);
eat_all = !eat_all;
- in->buffer += diff;
- in->buffer_used -= diff;
+ ret = istream_advance_buffer(in, diff);
+ TEST_EQUAL_I(ret, 0);
}
- TEST_EQUAL_UI(in->buffer_used, 0);
sqfs_drop(in);
return EXIT_SUCCESS;
}
diff --git a/lib/tar/src/iterator.c b/lib/tar/src/iterator.c
index 66b5dfc..f46a9c2 100644
--- a/lib/tar/src/iterator.c
+++ b/lib/tar/src/iterator.c
@@ -132,7 +132,7 @@ static int strm_precache(istream_t *strm)
int ret;
ret = istream_get_buffered_data(tar->parent->stream,
- &strm->buffer, &avail);
+ &strm->buffer, &avail, diff);
if (ret > 0)
goto fail_borked;
if (ret < 0)
@@ -373,11 +373,8 @@ dir_iterator_t *tar_open_stream(istream_t *strm)
it->read_xattr = it_read_xattr;
/* proble if the stream is compressed */
- ret = istream_precache(strm);
- if (ret != 0)
- goto out_strm;
-
- ret = istream_get_buffered_data(strm, &ptr, &size);
+ ret = istream_get_buffered_data(strm, &ptr, &size,
+ sizeof(tar_header_t));
if (ret != 0)
goto out_strm;