From 0b2fa64ba2898cc49b28da3b43b658d728448772 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tim Rühsen?=
Date: Sun, 14 Aug 2016 21:04:58 +0200
Subject: [PATCH] Use wget_XXXXXX.part for temp. file names
* bootstrap.conf: Add gnulib modules fopen, open, mkstemps.
* src/http.c: New static function fopen_tmp(), creates temp.
files named wget_XXXXXX.part.
(open_output_stream): Call fopen_tmp() for temp. files.
Reported-by: "Misra, Deapesh"
---
bootstrap.conf | 5 ++++-
src/http.c | 36 +++++++++++++++++++++++++++++++++++-
2 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/bootstrap.conf b/bootstrap.conf
index 2b225b7..eece390 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -40,6 +40,7 @@ dirname
fcntl
flock
fnmatch
+fopen
futimens
ftello
getaddrinfo
@@ -63,14 +64,16 @@ mbiter
mbtowc
memrchr
mkdir
-mkstemp
mkostemp
+mkstemp
+mkstemps
crypto/md2
crypto/md4
crypto/md5
crypto/sha1
crypto/sha256
crypto/sha512
+open
quote
quotearg
recv
diff --git a/src/http.c b/src/http.c
index 56b8669..728ada7 100644
--- a/src/http.c
+++ b/src/http.c
@@ -39,6 +39,7 @@ as that of the covered work. */
#include
#include
#include
+#include
#include "hash.h"
#include "http.h"
@@ -1568,6 +1569,8 @@ struct http_stat
#ifdef HAVE_METALINK
metalink_t *metalink;
#endif
+ FILE *tmpfp; /* set for temp. files, not closed bertween
+ * download & parsing */
};
static void
@@ -2423,6 +2426,27 @@ check_auth (struct url *u, char *user, char *passwd, struct response *resp,
return auth_err;
}
+static FILE *
+fopen_tmp (char **fname)
+{
+ FILE *fp;
+ char *orig = *fname;
+
+ *fname = xstrdup("wget_XXXXXX.part");
+ fp = fdopen (mkstemps (*fname, 5), "wb");
+
+ if (!fp)
+ {
+ xfree(*fname);
+ *fname = orig;
+ fp = fopen (*fname, "wb");
+ }
+ else
+ xfree(orig);
+
+ return fp;
+}
+
static uerr_t
open_output_stream (struct http_stat *hs, int count, FILE **fp)
{
@@ -2471,7 +2495,17 @@ open_output_stream (struct http_stat *hs, int count, FILE **fp)
open_id = 22;
*fp = fopen (hs->local_file, "wb", FOPEN_OPT_ARGS);
#else /* def __VMS */
- *fp = fopen (hs->local_file, "wb");
+ if (opt.delete_after
+ || opt.spider /* opt.recursive is implicitely true */
+ || !acceptable (hs->local_file))
+ {
+ *fp = fopen_tmp(&hs->local_file);
+ }
+ else
+ {
+ *fp = fopen (hs->local_file, "wb");
+ }
+
#endif /* def __VMS [else] */
}
else
--
2.9.3