[git commit] wget: do not ftruncate if -O- is used, closes 13351

Denys Vlasenko vda.linux at googlemail.com
Tue Dec 8 18:06:28 UTC 2020


commit: https://git.busybox.net/busybox/commit/?id=e7d853b4eb080dd8ea6433d010378e119e5a4cb3
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
wget_main                                           2558    2571     +13
retrieve_file_data                                   612     621      +9
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 22/0)               Total: 22 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 networking/wget.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/networking/wget.c b/networking/wget.c
index ea60c18b2..ff0df4ca0 100644
--- a/networking/wget.c
+++ b/networking/wget.c
@@ -287,6 +287,8 @@ enum {
 	WGET_OPT_POST_DATA  = (1 << 12) * ENABLE_FEATURE_WGET_LONG_OPTIONS,
 	WGET_OPT_SPIDER     = (1 << 13) * ENABLE_FEATURE_WGET_LONG_OPTIONS,
 	WGET_OPT_NO_CHECK_CERT = (1 << 14) * ENABLE_FEATURE_WGET_LONG_OPTIONS,
+	/* hijack this bit for other than opts purposes: */
+	WGET_NO_FTRUNCATE   = (1 << 31)
 };
 
 enum {
@@ -1052,8 +1054,13 @@ static void NOINLINE retrieve_file_data(FILE *dfp)
 	 */
 	{
 		off_t pos = lseek(G.output_fd, 0, SEEK_CUR);
-		if (pos != (off_t)-1)
-			ftruncate(G.output_fd, pos);
+		if (pos != (off_t)-1) {
+			/* do not truncate if -O- is in use, a user complained about
+			 * "wget -qO- 'http://example.com/empty' >>FILE" truncating FILE.
+			 */
+			if (!(option_mask32 & WGET_NO_FTRUNCATE))
+				ftruncate(G.output_fd, pos);
+		}
 	}
 
 	if (!(option_mask32 & WGET_OPT_QUIET)) {
@@ -1566,7 +1573,7 @@ IF_DESKTOP(	"no-parent\0"        No_argument       "\xf0")
 	if (G.fname_out) { /* -O FILE ? */
 		if (LONE_DASH(G.fname_out)) { /* -O - ? */
 			G.output_fd = 1;
-			option_mask32 &= ~WGET_OPT_CONTINUE;
+			option_mask32 = (option_mask32 & (~WGET_OPT_CONTINUE)) | WGET_NO_FTRUNCATE;
 		}
 		/* compat with wget: -O FILE can overwrite */
 		G.o_flags = O_WRONLY | O_CREAT | O_TRUNC;


More information about the busybox-cvs mailing list