From 690c47e3b18c099843cdf557a0425d701fca4957 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Sun, 21 Aug 2016 15:21:44 +0200 Subject: [PATCH] Append .tmp to temporary files * src/http.c (struct http_stat): Add `temporary` flag. (check_file_output): Append .tmp to temporary files. (open_output_stream): Refactor condition to use hs->temporary instead. Reported-by: "Misra, Deapesh" Discovered by: Dawid Golunski (http://legalhackers.com) --- NEWS | 6 ++++++ src/http.c | 14 +++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/NEWS b/NEWS index 5073d7e8..56c21a58 100644 --- a/NEWS +++ b/NEWS @@ -7,6 +7,12 @@ See the end for copying conditions. Please send GNU Wget bug reports to . +* Changes in Wget X.Y.Z + +* On a recursive download, append a .tmp suffix to temporary files + that will be deleted after being parsed, and create them + readable/writable only by the owner. + * Changes in Wget 1.18 * By default, on server redirects to a FTP resource, use the original diff --git a/src/http.c b/src/http.c index fd1abab4..3cafd1c4 100644 --- a/src/http.c +++ b/src/http.c @@ -1569,6 +1569,7 @@ struct http_stat #ifdef HAVE_METALINK metalink_t *metalink; #endif + bool temporary; /* downloading a temporary file */ }; static void @@ -2259,6 +2260,15 @@ check_file_output (struct url *u, struct http_stat *hs, xfree (local_file); } + hs->temporary = opt.delete_after || opt.spider || !acceptable (hs->local_file); + if (hs->temporary) + { + char *tmp = NULL; + asprintf (&tmp, "%s.tmp", hs->local_file); + xfree (hs->local_file); + hs->local_file = tmp; + } + /* TODO: perform this check only once. */ if (!hs->existence_checked && file_exists_p (hs->local_file)) { @@ -2472,9 +2482,7 @@ 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 */ - if (opt.delete_after - || opt.spider /* opt.recursive is implicitely true */ - || !acceptable (hs->local_file)) + if (hs->temporary) { *fp = fdopen (open (hs->local_file, O_BINARY | O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR), "wb"); }