changeset 24070:f23d1643808e

Uniquify auto-accepted file names to foo-2.tar.gz, not foo.tar.gz-2. Fixes a recently-filed bug whose number I didn't look up before getting onto the train.
author Will Thompson <will.thompson@collabora.co.uk>
date Fri, 05 Sep 2008 14:32:00 +0000
parents ac3db6ba7078
children b6e9e794f0ba
files libpurple/plugins/autoaccept.c
diffstat 1 files changed, 21 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/plugins/autoaccept.c	Fri Sep 05 12:07:37 2008 +0000
+++ b/libpurple/plugins/autoaccept.c	Fri Sep 05 14:32:00 2008 +0000
@@ -117,6 +117,9 @@
 			{
 				int count = 1;
 				const char *escape;
+				gchar **name_and_ext;
+				const gchar *name;
+				gchar *ext;
 
 				if (purple_prefs_get_bool(PREF_NEWDIR))
 					dirname = g_build_filename(pref, purple_normalize(account, xfer->who), NULL);
@@ -132,9 +135,24 @@
 				escape = purple_escape_filename(xfer->filename);
 				filename = g_build_filename(dirname, escape, NULL);
 
+				/* Split at the first dot, to avoid uniquifying "foo.tar.gz" to "foo.tar-2.gz" */
+				name_and_ext = g_strsplit(escape, ".", 2);
+				name = name_and_ext[0];
+				g_return_if_fail(name != NULL);
+				if (name_and_ext[1] != NULL) {
+					/* g_strsplit does not include the separator in each chunk. */
+					ext = g_strdup_printf(".%s", name_and_ext[1]);
+				} else {
+					ext = g_strdup("");
+				}
+
 				/* Make sure the file doesn't exist. Do we want some better checking than this? */
+				/* FIXME: There is a race here: if the newly uniquified file name gets created between
+				 *        this g_file_test and the transfer starting, the file created in the meantime
+				 *        will be clobbered. But it's not at all straightforward to fix.
+				 */
 				while (g_file_test(filename, G_FILE_TEST_EXISTS)) {
-					char *file = g_strdup_printf("%s-%d", escape, count++);
+					char *file = g_strdup_printf("%s-%d%s", name, count++, ext);
 					g_free(filename);
 					filename = g_build_filename(dirname, file, NULL);
 					g_free(file);
@@ -142,6 +160,8 @@
 
 				purple_xfer_request_accepted(xfer, filename);
 
+				g_strfreev(name_and_ext);
+				g_free(ext);
 				g_free(dirname);
 				g_free(filename);
 			}