From: Hristo Venev <hristo@venev.name>
To: cgit@lists.zx2c4.com, Gianni Ceccarelli <dakkar@thenautilus.net>
Cc: Filips R <frfilips@gmail.com>, Hristo Venev <hristo@venev.name>
Subject: [PATCH RESEND v2] cache: Tolerate short writes in print_slot
Date: Sat, 7 May 2022 20:07:00 +0300 [thread overview]
Message-ID: <20220507170700.612935-2-hristo@venev.name> (raw)
In-Reply-To: <20220507170700.612935-1-hristo@venev.name>
sendfile() can return after a short read/write, so we may need to call
it more than once. As suggested in the manual page, we fall back to
read/write if sendfile fails with EINVAL or ENOSYS.
On the read/write path, use write_in_full which deals with short writes.
Signed-off-by: Hristo Venev <hristo@venev.name>
---
cache.c | 45 +++++++++++++++++++++++++--------------------
1 file changed, 25 insertions(+), 20 deletions(-)
diff --git a/cache.c b/cache.c
index 55199e8..1c843ba 100644
--- a/cache.c
+++ b/cache.c
@@ -85,40 +85,45 @@ static int close_slot(struct cache_slot *slot)
/* Print the content of the active cache slot (but skip the key). */
static int print_slot(struct cache_slot *slot)
{
+ off_t off;
#ifdef HAVE_LINUX_SENDFILE
- off_t start_off;
- int ret;
+ off_t size;
+#endif
+
+ off = slot->keylen + 1;
- start_off = slot->keylen + 1;
+#ifdef HAVE_LINUX_SENDFILE
+ size = slot->cache_st.st_size;
do {
- ret = sendfile(STDOUT_FILENO, slot->cache_fd, &start_off,
- slot->cache_st.st_size - start_off);
+ ssize_t ret;
+ ret = sendfile(STDOUT_FILENO, slot->cache_fd, &off, size - off);
if (ret < 0) {
if (errno == EAGAIN || errno == EINTR)
continue;
+ /* Fall back to read/write on EINVAL or ENOSYS */
+ if (errno == EINVAL || errno == ENOSYS)
+ break;
return errno;
}
- return 0;
+ if (off == size)
+ return 0;
} while (1);
-#else
- ssize_t i, j;
+#endif
- i = lseek(slot->cache_fd, slot->keylen + 1, SEEK_SET);
- if (i != slot->keylen + 1)
+ if (lseek(slot->cache_fd, off, SEEK_SET) != off)
return errno;
do {
- i = j = xread(slot->cache_fd, slot->buf, sizeof(slot->buf));
- if (i > 0)
- j = xwrite(STDOUT_FILENO, slot->buf, i);
- } while (i > 0 && j == i);
-
- if (i < 0 || j != i)
- return errno;
- else
- return 0;
-#endif
+ ssize_t ret;
+ ret = xread(slot->cache_fd, slot->buf, sizeof(slot->buf));
+ if (ret < 0)
+ return errno;
+ if (ret == 0)
+ return 0;
+ if (write_in_full(STDOUT_FILENO, slot->buf, ret) < 0)
+ return errno;
+ } while (1);
}
/* Check if the slot has expired */
--
2.35.3
prev parent reply other threads:[~2022-05-07 17:07 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-07 12:49 Truncated output when writing to a pipe using sendfile Filips R
2022-05-07 13:03 ` Gianni Ceccarelli
2022-05-07 17:06 ` Hristo Venev
2022-05-07 17:07 ` Hristo Venev [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220507170700.612935-2-hristo@venev.name \
--to=hristo@venev.name \
--cc=cgit@lists.zx2c4.com \
--cc=dakkar@thenautilus.net \
--cc=frfilips@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).