rhythmbox r5928 - in trunk: . lib tests



Author: jmatthew
Date: Wed Sep 17 23:44:03 2008
New Revision: 5928
URL: http://svn.gnome.org/viewvc/rhythmbox?rev=5928&view=rev

Log:
2008-09-18  Jonathan Matthew  <jonathan d14n org>

	* lib/rb-file-helpers.c: (rb_check_dir_has_space),
	(rb_file_find_extant_parent), (rb_uri_get_filesystem_type),
	(rb_sanitize_uri_for_filesystem):
	* lib/rb-file-helpers.h:
	* lib/rb-util.c:
	* lib/rb-util.h:
	rb_check_dir_has_space has to walk up the filesystem to find an
	existing ancestor, like rb_uri_get_filesystem_type, so add a helper
	function to do that part of it.  Rearrange some other things as
	necessary.  Probably fixes #552614 again.

	* tests/test-file-helpers.c: (START_TEST), (rb_file_helpers_suite):
	Add some simple tests for rb_check_dir_has_space.


Modified:
   trunk/ChangeLog
   trunk/lib/rb-file-helpers.c
   trunk/lib/rb-file-helpers.h
   trunk/lib/rb-util.c
   trunk/lib/rb-util.h
   trunk/tests/test-file-helpers.c

Modified: trunk/lib/rb-file-helpers.c
==============================================================================
--- trunk/lib/rb-file-helpers.c	(original)
+++ trunk/lib/rb-file-helpers.c	Wed Sep 17 23:44:03 2008
@@ -810,14 +810,22 @@
 rb_check_dir_has_space (GFile *file,
 			guint64 bytes_needed)
 {
+	GFile *extant;
 	GFileInfo *fs_info;
 	GError *error = NULL;
 	guint64 free_bytes;
 
-	fs_info = g_file_query_filesystem_info (file,
+	extant = rb_file_find_extant_parent (file);
+	if (extant == NULL) {
+		return FALSE;
+	}
+
+	fs_info = g_file_query_filesystem_info (extant,
 						G_FILE_ATTRIBUTE_FILESYSTEM_FREE,
 						NULL,
 						&error);
+	g_object_unref (extant);
+
 	if (error != NULL) {
 		char *uri;
 		uri = g_file_get_uri (file);
@@ -962,8 +970,84 @@
 	return ret;
 }
 
+/**
+ * rb_file_find_extant_parent:
+ * @file: a #GFile to find an extant ancestor of
+ *
+ * Walks up the filesystem hierarchy to find a #GFile representing
+ * the nearest extant ancestor of the specified file, which may be
+ * the file itself if it exists.
+ * 
+ * Return value: #GFile for the nearest extant ancestor
+ */
+GFile *
+rb_file_find_extant_parent (GFile *file)
+{
+	g_object_ref (file);
+	while (g_file_query_exists (file, NULL) == FALSE) {
+		GFile *parent;
+
+		parent = g_file_get_parent (file);
+		g_object_unref (file);
+		file = parent;
+
+		if (file == NULL) {
+			g_warning ("filesystem root apparently doesn't exist!");
+			return NULL;
+		}
+	}
+
+	return file;
+}
+
+/**
+ * rb_uri_get_filesystem_type:
+ * @uri: URI to get filesystem type for
+ *
+ * Return value: a string describing the type of the filesystem containing the URI
+ */
 char *
-rb_sanitize_uri_for_filesystem (char *uri)
+rb_uri_get_filesystem_type (const char *uri)
+{
+	GFile *file;
+	GFile *extant;
+	GFileInfo *info;
+	char *fstype = NULL;
+	GError *error = NULL;
+
+	/* if the file doesn't exist, walk up the directory structure
+	 * until we find something that does.
+	 */
+	file = g_file_new_for_uri (uri);
+
+	extant = rb_file_find_extant_parent (file);
+	if (extant == NULL) {
+		g_object_unref (file);
+		return NULL;
+	}
+
+	info = g_file_query_filesystem_info (extant, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, NULL, &error);
+	if (info != NULL) {
+		fstype = g_file_info_get_attribute_as_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE);
+		g_object_unref (info);
+	} else {
+		rb_debug ("error querying filesystem info: %s", error->message);
+	}
+	g_clear_error (&error);
+	g_object_unref (file);
+	g_object_unref (extant);
+	return fstype;
+}
+
+/**
+ * rb_sanitize_uri_for_filesystem:
+ * @uri: a URI to sanitize
+ *
+ * Return value: a copy of the URI with characters not allowed by the target filesystem
+ *   replaced
+ */
+char *
+rb_sanitize_uri_for_filesystem (const char *uri)
 {
 	char *filesystem = rb_uri_get_filesystem_type (uri);
 	char *sane_uri = NULL;
@@ -1006,3 +1090,4 @@
 	g_free (filesystem);
 	return sane_uri ? sane_uri : g_strdup (uri);
 }
+

Modified: trunk/lib/rb-file-helpers.h
==============================================================================
--- trunk/lib/rb-file-helpers.h	(original)
+++ trunk/lib/rb-file-helpers.h	Wed Sep 17 23:44:03 2008
@@ -57,6 +57,7 @@
 char *		rb_uri_get_short_path_name (const char *uri);
 char *		rb_uri_get_mount_point  (const char *uri);
 
+
 /* return TRUE to recurse further, FALSE to stop */
 typedef gboolean (*RBUriRecurseFunc) (GFile *file, gboolean dir, gpointer data);
 
@@ -79,12 +80,15 @@
 gboolean	rb_check_dir_has_space	(GFile *dir, guint64 bytes_needed);
 gboolean	rb_check_dir_has_space_uri (const char *uri, guint64 bytes_needed);
 
+GFile *		rb_file_find_extant_parent (GFile *file);
+
 gboolean	rb_uri_create_parent_dirs (const char *uri, GError **error);
 
 void		rb_file_helpers_init	(void);
 void		rb_file_helpers_shutdown(void);
 
-char *		rb_sanitize_uri_for_filesystem(gchar *uri);
+char *		rb_uri_get_filesystem_type (const char *uri);
+char *		rb_sanitize_uri_for_filesystem(const char *uri);
 
 G_END_DECLS
 

Modified: trunk/lib/rb-util.c
==============================================================================
--- trunk/lib/rb-util.c	(original)
+++ trunk/lib/rb-util.c	Wed Sep 17 23:44:03 2008
@@ -323,44 +323,6 @@
 	}
 }
 
-char *
-rb_uri_get_filesystem_type (const char *uri)
-{
-	GFile *file;
-	GFileInfo *info;
-	char *fstype = NULL;
-	GError *error = NULL;
-
-	/* if the file doesn't exist, walk up the directory structure
-	 * until we find something that does.
-	 */
-	file = g_file_new_for_uri (uri);
-	info = g_file_query_filesystem_info (file, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, NULL, &error);
-	while (error != NULL && error->code == G_IO_ERROR_NOT_FOUND) {
-		GFile *parent;
-		
-		g_clear_error (&error);
-		parent = g_file_get_parent (file);
-		g_object_unref (file);
-		file = parent;
-		if (file == NULL) {
-			g_warning ("filesystem root apparently doesn't exist!");
-			return NULL;
-		}
-	
-		info = g_file_query_filesystem_info (file, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, NULL, &error);
-	}
-
-	if (info != NULL) {
-		fstype = g_file_info_get_attribute_as_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE);
-		g_object_unref (info);
-	} else {
-		rb_debug ("error querying filesystem info: %s", error->message);
-	}
-	g_clear_error (&error);
-	g_object_unref (file);
-	return fstype;
-}
 
 gboolean
 rb_is_main_thread (void)

Modified: trunk/lib/rb-util.h
==============================================================================
--- trunk/lib/rb-util.h	(original)
+++ trunk/lib/rb-util.h	Wed Sep 17 23:44:03 2008
@@ -58,8 +58,6 @@
 
 GtkWidget *rb_image_new_from_stock (const gchar *stock_id, GtkIconSize size);
 
-char *rb_uri_get_filesystem_type(const char *uri);
-
 void rb_threads_init (void);
 gboolean rb_is_main_thread (void);
 

Modified: trunk/tests/test-file-helpers.c
==============================================================================
--- trunk/tests/test-file-helpers.c	(original)
+++ trunk/tests/test-file-helpers.c	Wed Sep 17 23:44:03 2008
@@ -87,6 +87,15 @@
 }
 END_TEST
 
+START_TEST (test_rb_check_dir_has_space)
+{
+	fail_unless (rb_check_dir_has_space_uri ("file:///tmp", 1));
+	fail_unless (rb_check_dir_has_space_uri ("file:///etc/passwd", 1));
+	fail_unless (rb_check_dir_has_space_uri ("file:///tmp/NONEXISTANT_FILE", 1));
+	fail_unless (rb_check_dir_has_space_uri ("file:///tmp/NONEXISTANT/THISDOESNTEXISTEITHER/NORDOESTHIS", G_MAXUINT64) == FALSE);
+}
+END_TEST
+
 static Suite *
 rb_file_helpers_suite ()
 {
@@ -96,6 +105,7 @@
 	suite_add_tcase (s, tc_chain);
 
 	tcase_add_test (tc_chain, test_rb_uri_get_short_path_name);
+	tcase_add_test (tc_chain, test_rb_check_dir_has_space);
 
 	return s;
 }



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