about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorMarco Peereboom <marco@conformal.com>2011-07-07 17:56:48 +0000
committerMarco Peereboom <marco@conformal.com>2011-07-07 17:56:48 +0000
commitb66192b43452083e9df83efcabe8c814f3947a60 (patch)
treec3401bb021240acfc51ccddfa378e42c2124af4e
parent9acfe5283aa71b01212f1f7d4b8c959f868da58d (diff)
downloadxombrero-b66192b43452083e9df83efcabe8c814f3947a60.tar.gz
Prevent files in download dir from being overwritten. FS#80
from Cody Wright <writecode@mailinator.com>
-rw-r--r--xxxterm.c29
1 files changed, 24 insertions, 5 deletions
diff --git a/xxxterm.c b/xxxterm.c
index 0c91ca6..6f98938 100644
--- a/xxxterm.c
+++ b/xxxterm.c
@@ -6244,21 +6244,37 @@ int
 webview_download_cb(WebKitWebView *wv, WebKitDownload *wk_download,
     struct tab *t)
 {
-	const gchar		*filename;
+	struct stat		sb;
+	const gchar		*suggested_name;
+	gchar			*filename = NULL;
 	char			*uri = NULL;
 	struct download		*download_entry;
-	int			ret = TRUE;
+	int			i, ret = TRUE;
 
 	if (wk_download == NULL || t == NULL) {
 		show_oops_s("%s invalid parameters", __func__);
 		return (FALSE);
 	}
 
-	filename = webkit_download_get_suggested_filename(wk_download);
-	if (filename == NULL)
+	suggested_name = webkit_download_get_suggested_filename(wk_download);
+	if (suggested_name == NULL)
 		return (FALSE); /* abort download */
 
-	uri = g_strdup_printf("file://%s/%s", download_dir, filename);
+	i = 0;
+	do {
+		if (filename) {
+			g_free(filename);
+			filename = NULL;
+		}
+		if (i) {
+			g_free(uri);
+			uri = NULL;
+			filename = g_strdup_printf("%d%s", i, suggested_name);
+		}
+		uri = g_strdup_printf("file://%s/%s", download_dir, i ?
+		    filename : suggested_name);
+		i++;
+	} while (!stat(uri + strlen("file://"), &sb));
 
 	DNPRINTF(XT_D_DOWNLOAD, "%s: tab %d filename %s "
 	    "local %s\n", __func__, t->tab_id, filename, uri);
@@ -6290,6 +6306,9 @@ webview_download_cb(WebKitWebView *wv, WebKitDownload *wk_download,
 	if (uri)
 		g_free(uri);
 
+	if (filename)
+		g_free(filename);
+
 	/* sync other download manager tabs */
 	update_download_tabs(NULL);