aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-04 14:36:25 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-04 15:09:05 +0200
commit723306c417b3b0f8e6a3904906d6c5612cace432 (patch)
tree34235c4e6e11a1702e61d81f4f58bc9f3da57c58
parent061fbc12fe49ff49088a644def3227d3800cd8a7 (diff)
libio: Move istream_t precache logic into backend implementation
The end goal is to remove direct buffer access from the istream_t interfaces and make that opaque. For the tar implementation, this already safes us needless buffer copying, as we essentially allow the user to read-through from the underlying stream. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r--include/io/istream.h5
-rw-r--r--lib/io/src/istream.c20
-rw-r--r--lib/io/src/mem.c15
-rw-r--r--lib/io/src/unix/istream.c18
-rw-r--r--lib/io/src/win32/istream.c15
-rw-r--r--lib/io/src/xfrm/istream.c15
-rw-r--r--lib/tar/src/iterator.c6
7 files changed, 68 insertions, 26 deletions
diff --git a/include/io/istream.h b/include/io/istream.h
index 3280327..9c34678 100644
--- a/include/io/istream.h
+++ b/include/io/istream.h
@@ -96,7 +96,10 @@ SQFS_INTERNAL sqfs_s32 istream_read(istream_t *strm, void *data, size_t size);
*
* @return 0 on success, -1 on failure.
*/
-SQFS_INTERNAL int istream_precache(istream_t *strm);
+SQFS_INLINE int istream_precache(istream_t *strm)
+{
+ return strm->precache(strm);
+}
/**
* @brief Get the underlying filename of an input stream.
diff --git a/lib/io/src/istream.c b/lib/io/src/istream.c
index 64fa478..051fac8 100644
--- a/lib/io/src/istream.c
+++ b/lib/io/src/istream.c
@@ -38,26 +38,6 @@ sqfs_s32 istream_read(istream_t *strm, void *data, size_t size)
return total;
}
-int istream_precache(istream_t *strm)
-{
- if (strm->buffer_offset >= strm->buffer_used) {
- strm->buffer_offset = 0;
- strm->buffer_used = 0;
- } else if (strm->buffer_offset > 0) {
- memmove(strm->buffer,
- strm->buffer + strm->buffer_offset,
- strm->buffer_used - strm->buffer_offset);
-
- strm->buffer_used -= strm->buffer_offset;
- strm->buffer_offset = 0;
- }
-
- if (strm->eof)
- return 0;
-
- return strm->precache(strm);
-}
-
int istream_skip(istream_t *strm, sqfs_u64 size)
{
size_t diff;
diff --git a/lib/io/src/mem.c b/lib/io/src/mem.c
index 4e9f9af..d305a83 100644
--- a/lib/io/src/mem.c
+++ b/lib/io/src/mem.c
@@ -26,8 +26,21 @@ typedef struct {
static int mem_in_precache(istream_t *strm)
{
mem_istream_t *mem = (mem_istream_t *)strm;
- size_t diff = mem->bufsz - strm->buffer_used;
+ size_t diff;
+
+ if (strm->buffer_offset >= strm->buffer_used) {
+ strm->buffer_offset = 0;
+ strm->buffer_used = 0;
+ } else if (strm->buffer_offset > 0) {
+ memmove(strm->buffer,
+ strm->buffer + strm->buffer_offset,
+ strm->buffer_used - strm->buffer_offset);
+
+ strm->buffer_used -= strm->buffer_offset;
+ strm->buffer_offset = 0;
+ }
+ diff = mem->bufsz - strm->buffer_used;
if (diff > mem->size)
diff = mem->size;
diff --git a/lib/io/src/unix/istream.c b/lib/io/src/unix/istream.c
index f8cffad..24b18d6 100644
--- a/lib/io/src/unix/istream.c
+++ b/lib/io/src/unix/istream.c
@@ -20,15 +20,25 @@ static int file_precache(istream_t *strm)
ssize_t ret;
size_t diff;
- while (strm->buffer_used < sizeof(file->buffer)) {
+ if (strm->buffer_offset >= strm->buffer_used) {
+ strm->buffer_offset = 0;
+ strm->buffer_used = 0;
+ } else if (strm->buffer_offset > 0) {
+ memmove(strm->buffer,
+ strm->buffer + strm->buffer_offset,
+ strm->buffer_used - strm->buffer_offset);
+
+ strm->buffer_used -= strm->buffer_offset;
+ strm->buffer_offset = 0;
+ }
+
+ while (!strm->eof && strm->buffer_used < sizeof(file->buffer)) {
diff = sizeof(file->buffer) - strm->buffer_used;
ret = read(file->fd, strm->buffer + strm->buffer_used, diff);
- if (ret == 0) {
+ if (ret == 0)
strm->eof = true;
- break;
- }
if (ret < 0) {
if (errno == EINTR)
diff --git a/lib/io/src/win32/istream.c b/lib/io/src/win32/istream.c
index be3d829..918b868 100644
--- a/lib/io/src/win32/istream.c
+++ b/lib/io/src/win32/istream.c
@@ -23,6 +23,21 @@ static int file_precache(istream_t *strm)
DWORD diff, actual;
HANDLE hnd;
+ if (strm->buffer_offset >= strm->buffer_used) {
+ strm->buffer_offset = 0;
+ strm->buffer_used = 0;
+ } else if (strm->buffer_offset > 0) {
+ memmove(strm->buffer,
+ strm->buffer + strm->buffer_offset,
+ strm->buffer_used - strm->buffer_offset);
+
+ strm->buffer_used -= strm->buffer_offset;
+ strm->buffer_offset = 0;
+ }
+
+ if (strm->eof)
+ return 0;
+
hnd = file->path == NULL ? GetStdHandle(STD_INPUT_HANDLE) : file->hnd;
while (strm->buffer_used < sizeof(file->buffer)) {
diff --git a/lib/io/src/xfrm/istream.c b/lib/io/src/xfrm/istream.c
index 4a1ad77..b301209 100644
--- a/lib/io/src/xfrm/istream.c
+++ b/lib/io/src/xfrm/istream.c
@@ -20,6 +20,21 @@ static int xfrm_precache(istream_t *base)
istream_xfrm_t *xfrm = (istream_xfrm_t *)base;
int ret;
+ if (base->buffer_offset >= base->buffer_used) {
+ base->buffer_offset = 0;
+ base->buffer_used = 0;
+ } else if (base->buffer_offset > 0) {
+ memmove(base->buffer,
+ base->buffer + base->buffer_offset,
+ base->buffer_used - base->buffer_offset);
+
+ base->buffer_used -= base->buffer_offset;
+ base->buffer_offset = 0;
+ }
+
+ if (base->eof)
+ return 0;
+
ret = istream_precache(xfrm->wrapped);
if (ret != 0)
return ret;
diff --git a/lib/tar/src/iterator.c b/lib/tar/src/iterator.c
index 0940905..532eb8c 100644
--- a/lib/tar/src/iterator.c
+++ b/lib/tar/src/iterator.c
@@ -104,6 +104,12 @@ static int strm_precache(istream_t *strm)
tar_istream_t *tar = (tar_istream_t *)strm;
sqfs_u64 diff;
+ strm->buffer_used -= strm->buffer_offset;
+ strm->buffer_offset = 0;
+
+ if (strm->eof)
+ return 0;
+
tar->parent->offset += tar->parent->last_chunk;
if (!tar->parent->last_sparse) {