summaryrefslogtreecommitdiff
path: root/buffer.c
diff options
context:
space:
mode:
authorNiels Provos <provos@gmail.com>2009-01-29 03:22:47 +0000
committerNiels Provos <provos@gmail.com>2009-01-29 03:22:47 +0000
commita0cae310d05d99a1f235a1814ba5ef7893959ec8 (patch)
treef842095932253da16e5be4ab4837ce71c060f1da /buffer.c
parent66b2a7ffb72e68a059c7479362bfe4863e2331df (diff)
downloadlibevent-a0cae310d05d99a1f235a1814ba5ef7893959ec8.tar.gz
make it so that evbuffer_add_file where we read the complete contents of the file can fail without side effects
svn:r1069
Diffstat (limited to 'buffer.c')
-rw-r--r--buffer.c18
1 files changed, 15 insertions, 3 deletions
diff --git a/buffer.c b/buffer.c
index a6de9518..04b88a8f 100644
--- a/buffer.c
+++ b/buffer.c
@@ -1496,21 +1496,33 @@ evbuffer_add_file(struct evbuffer *outbuf, int fd,
#endif
{
/* the default implementation */
+ struct evbuffer *tmp = evbuffer_new();
ssize_t read;
- if (lseek(fd, offset, SEEK_SET) == -1)
+ if (tmp == NULL)
+ return (-1);
+
+ if (lseek(fd, offset, SEEK_SET) == -1) {
+ evbuffer_free(tmp);
return (-1);
+ }
+ /* we add everything to a temporary buffer, so that we
+ * can abort without side effects if the read fails.
+ */
while (length) {
- read = evbuffer_read(outbuf, fd, length);
+ read = evbuffer_read(tmp, fd, length);
if (read == -1) {
- /* TODO(niels): deal with rewinding */
+ evbuffer_free(tmp);
return (-1);
}
length -= read;
}
+ evbuffer_add_buffer(outbuf, tmp);
+ evbuffer_free(tmp);
+
close(fd);
}