[file-roller: 122/123] libarchive: use a temp filename that doesn't change the file extension



commit c459aab9fd2c1d3ce5f31fc8931cd491b45109cc
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Mon Aug 6 15:10:12 2012 +0200

    libarchive: use a temp filename that doesn't change the file extension

 src/fr-archive-libarchive.c |   12 +++++++-----
 src/glib-utils.c            |   16 ++++++++++++----
 src/glib-utils.h            |    3 ++-
 3 files changed, 21 insertions(+), 10 deletions(-)
---
diff --git a/src/fr-archive-libarchive.c b/src/fr-archive-libarchive.c
index 1d64113..4057465 100644
--- a/src/fr-archive-libarchive.c
+++ b/src/fr-archive-libarchive.c
@@ -735,18 +735,20 @@ save_data_open (struct archive *a,
 	SaveData *save_data = client_data;
 	LoadData *load_data = LOAD_DATA (save_data);
 	GFile    *parent;
-	char     *name;
+	char     *basename;
+	char     *tmpname;
 
 	if (load_data->error != NULL)
 		return ARCHIVE_FATAL;
 
-	/* FIXME: use a better temp filename */
 	parent = g_file_get_parent (fr_archive_get_file (load_data->archive));
-	name = _g_filename_get_random (16);
-	save_data->tmp_file = g_file_get_child (parent, name);
+	basename = g_file_get_basename (fr_archive_get_file (load_data->archive));
+	tmpname = _g_filename_get_random (16, basename);
+	save_data->tmp_file = g_file_get_child (parent, tmpname);
 	save_data->ostream = (GOutputStream *) g_file_create (save_data->tmp_file, G_FILE_CREATE_NONE, load_data->cancellable, &load_data->error);
 
-	g_free (name);
+	g_free (tmpname);
+	g_free (basename);
 	g_object_unref (parent);
 
 	return (save_data->ostream != NULL) ? ARCHIVE_OK : ARCHIVE_FATAL;
diff --git a/src/glib-utils.c b/src/glib-utils.c
index 0af5ce8..2f6efb3 100644
--- a/src/glib-utils.c
+++ b/src/glib-utils.c
@@ -1054,22 +1054,30 @@ _g_filename_has_extension (const char *filename,
 
 
 char *
-_g_filename_get_random (int length)
+_g_filename_get_random (int         random_part_len,
+		        const char *suffix)
 {
-	const char *letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
+	const char *letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
 	const int   n_letters = strlen (letters);
+	int         suffix_len;
 	char       *result, *c;
 	GRand      *rand;
 	int         i;
 
-	result = g_new (char, length + 1);
+	suffix_len = suffix != NULL ? strlen (suffix) + 1 : 0;
+	result = g_new (char, suffix_len + random_part_len + 1);
 
 	rand = g_rand_new ();
-	for (i = 0, c = result; i < length; i++, c++)
+	for (i = 0, c = result; i < random_part_len; i++, c++)
 		*c = letters[g_rand_int_range (rand, 0, n_letters)];
 	*c = '\0';
 	g_rand_free (rand);
 
+	if (suffix != NULL) {
+		strcpy (c, ".");
+		strcpy (c + 1, suffix);
+	}
+
 	return result;
 }
 
diff --git a/src/glib-utils.h b/src/glib-utils.h
index b58c934..f002775 100644
--- a/src/glib-utils.h
+++ b/src/glib-utils.h
@@ -142,7 +142,8 @@ gboolean            _g_filename_is_hidden          (const char          *name);
 const char *        _g_filename_get_extension      (const char          *filename);
 gboolean            _g_filename_has_extension      (const char          *filename,
 						    const char          *ext);
-char *              _g_filename_get_random         (int                  length);
+char *              _g_filename_get_random         (int                  random_part_len,
+						    const char          *suffix);
 gboolean            _g_mime_type_matches           (const char          *type,
 						    const char          *pattern);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]