commit 1a9aa3e252e93098505e00636e4a81234dfbcb25
parent 41e5188b6e217a4bfd6d1802b73f49cc49472c17
Author: René Wagner <rwagner@rw-net.de>
Date: Thu, 28 Oct 2021 22:09:46 +0200
fix downloading of files
due to the wrong handling in download_resp() files may get
broken (last chunk missing) and file descriptors have not
been closed correctly.
Additionally we now allow downloading of local ressources
as well - for what its worth.
Diffstat:
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/src/util.c b/src/util.c
@@ -9,6 +9,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
+#include <unistd.h>
#include "util.h"
void
@@ -83,10 +84,13 @@ download_resp(FILE *out, struct gemini_response resp, const char *path,
fprintf(out, "Downloading %s to %s\n", url, path);
char buf[BUFSIZ];
for (int n = 1; n > 0;) {
- n = br_sslio_read(&resp.body, buf, sizeof(buf));
- if (n == -1) {
- fprintf(stderr, "Error: read\n");
- return 1;
+ if (resp.sc) {
+ n = br_sslio_read(&resp.body, buf, BUFSIZ);
+ } else {
+ n = read(resp.fd, buf, BUFSIZ);
+ }
+ if (n < 0) {
+ break;
}
ssize_t w = 0;
while (w < (ssize_t)n) {