filesrc, filesink: fix URI creation regression for non-absolute locations

Passing e.g. location=foo would lead to warnings because g_filename_to_uri()
wants an absolute file path and returns NULL otherwise. Use brand-new
gst_filename_to_uri() instead, which will try harder to create a proper
URI for us.

Also add unit test.
This commit is contained in:
Tim-Philipp Müller 2011-02-24 15:32:00 +00:00
parent 27027a2dd2
commit 1f59906ec1
3 changed files with 69 additions and 3 deletions

View file

@ -302,7 +302,9 @@ gst_file_sink_set_location (GstFileSink * sink, const gchar * location)
/* we store the filename as we received it from the application. On Windows
* this should be in UTF8 */
sink->filename = g_strdup (location);
sink->uri = g_filename_to_uri (sink->filename, NULL, NULL);
sink->uri = gst_filename_to_uri (location, NULL);
GST_INFO ("filename : %s", sink->filename);
GST_INFO ("uri : %s", sink->uri);
} else {
sink->filename = NULL;
sink->uri = NULL;

View file

@ -367,10 +367,12 @@ gst_file_src_set_location (GstFileSrc * src, const gchar * location)
src->filename = NULL;
src->uri = NULL;
} else {
/* we store the filename as received by the application. On Windoes this
/* we store the filename as received by the application. On Windows this
* should be UTF8 */
src->filename = g_strdup (location);
src->uri = g_filename_to_uri (src->filename, NULL, NULL);
src->uri = gst_filename_to_uri (location, NULL);
GST_INFO ("filename : %s", src->filename);
GST_INFO ("uri : %s", src->uri);
}
g_object_notify (G_OBJECT (src), "location");
gst_uri_handler_new_uri (GST_URI_HANDLER (src), src->uri);

View file

@ -384,6 +384,67 @@ GST_START_TEST (test_uri_interface)
GST_END_TEST;
static void
check_uri_for_location (GstElement * e, const gchar * location,
const gchar * uri)
{
GstQuery *query;
gchar *query_uri = NULL;
g_object_set (e, "location", location, NULL);
query = gst_query_new_uri ();
fail_unless (gst_element_query (e, query));
gst_query_parse_uri (query, &query_uri);
gst_query_unref (query);
if (uri != NULL) {
fail_unless_equals_string (query_uri, uri);
} else {
gchar *fn;
fail_unless (gst_uri_is_valid (query_uri));
fn = g_filename_from_uri (query_uri, NULL, NULL);
fail_unless (g_path_is_absolute (fn));
fail_unless (fn != NULL);
g_free (fn);
}
g_free (query_uri);
}
GST_START_TEST (test_uri_query)
{
GstElement *src;
src = setup_filesrc ();
#ifdef G_OS_UNIX
{
GST_INFO ("*nix");
check_uri_for_location (src, "/i/do/not/exist", "file:///i/do/not/exist");
check_uri_for_location (src, "/i/do/not/../exist", "file:///i/do/exist");
check_uri_for_location (src, "/i/do/not/.././exist", "file:///i/do/exist");
check_uri_for_location (src, "/i/./do/not/../exist", "file:///i/do/exist");
check_uri_for_location (src, "/i/do/./not/../exist", "file:///i/do/exist");
check_uri_for_location (src, "/i/do/not/./../exist", "file:///i/do/exist");
check_uri_for_location (src, "/i/./do/./././././exist",
"file:///i/do/exist");
check_uri_for_location (src, "/i/do/not/../../exist", "file:///i/exist");
check_uri_for_location (src, "/i/../not/../exist", "file:///exist");
/* hard to test relative URIs, just make sure it returns an URI of sorts */
check_uri_for_location (src, "foo", NULL);
check_uri_for_location (src, "foo/../bar", NULL);
check_uri_for_location (src, "./foo", NULL);
check_uri_for_location (src, "../foo", NULL);
check_uri_for_location (src, "foo/./bar", NULL);
}
#endif
cleanup_filesrc (src);
}
GST_END_TEST;
static Suite *
filesrc_suite (void)
{
@ -396,6 +457,7 @@ filesrc_suite (void)
tcase_add_test (tc_chain, test_pull);
tcase_add_test (tc_chain, test_coverage);
tcase_add_test (tc_chain, test_uri_interface);
tcase_add_test (tc_chain, test_uri_query);
return s;
}